UPSTREAM: mm/damon: simplify stop mechanism
A kernel thread can exit gracefully with kthread_stop(). So we don't need a new flag 'kdamond_stop'. And to make sure the task struct is not freed when accessing it, get reference to it before termination. Link: https://lkml.kernel.org/r/20211027130517.4404-1-changbin.du@gmail.com Signed-off-by: Changbin Du <changbin.du@gmail.com> Reviewed-by: SeongJae Park <sj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> (cherry picked from commit 0f91d13366a402420bf98eaaf393db03946c13e0) Bug: 228223814 Signed-off-by: zhijun wan <wanzhijun@oppo.com> Change-Id: I7b959fc6cd114dc244c895cf19b9fd864b81cd12
This commit is contained in:
parent
fe6bcb8dd4
commit
c91a782b6f
|
@ -381,7 +381,6 @@ struct damon_ctx {
|
|||
|
||||
/* public: */
|
||||
struct task_struct *kdamond;
|
||||
bool kdamond_stop;
|
||||
struct mutex kdamond_lock;
|
||||
|
||||
struct damon_primitive primitive;
|
||||
|
|
|
@ -390,17 +390,6 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
|
|||
return sz;
|
||||
}
|
||||
|
||||
static bool damon_kdamond_running(struct damon_ctx *ctx)
|
||||
{
|
||||
bool running;
|
||||
|
||||
mutex_lock(&ctx->kdamond_lock);
|
||||
running = ctx->kdamond != NULL;
|
||||
mutex_unlock(&ctx->kdamond_lock);
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
static int kdamond_fn(void *data);
|
||||
|
||||
/*
|
||||
|
@ -418,7 +407,6 @@ static int __damon_start(struct damon_ctx *ctx)
|
|||
mutex_lock(&ctx->kdamond_lock);
|
||||
if (!ctx->kdamond) {
|
||||
err = 0;
|
||||
ctx->kdamond_stop = false;
|
||||
ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
|
||||
nr_running_ctxs);
|
||||
if (IS_ERR(ctx->kdamond)) {
|
||||
|
@ -483,12 +471,15 @@ static void kdamond_usleep(unsigned long usecs)
|
|||
*/
|
||||
static int __damon_stop(struct damon_ctx *ctx)
|
||||
{
|
||||
struct task_struct *tsk;
|
||||
|
||||
mutex_lock(&ctx->kdamond_lock);
|
||||
if (ctx->kdamond) {
|
||||
ctx->kdamond_stop = true;
|
||||
tsk = ctx->kdamond;
|
||||
if (tsk) {
|
||||
get_task_struct(tsk);
|
||||
mutex_unlock(&ctx->kdamond_lock);
|
||||
while (damon_kdamond_running(ctx))
|
||||
kdamond_usleep(ctx->sample_interval);
|
||||
kthread_stop(tsk);
|
||||
put_task_struct(tsk);
|
||||
return 0;
|
||||
}
|
||||
mutex_unlock(&ctx->kdamond_lock);
|
||||
|
@ -933,12 +924,8 @@ static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
|
|||
static bool kdamond_need_stop(struct damon_ctx *ctx)
|
||||
{
|
||||
struct damon_target *t;
|
||||
bool stop;
|
||||
|
||||
mutex_lock(&ctx->kdamond_lock);
|
||||
stop = ctx->kdamond_stop;
|
||||
mutex_unlock(&ctx->kdamond_lock);
|
||||
if (stop)
|
||||
if (kthread_should_stop())
|
||||
return true;
|
||||
|
||||
if (!ctx->primitive.target_valid)
|
||||
|
@ -1021,13 +1008,6 @@ static int kdamond_wait_activation(struct damon_ctx *ctx)
|
|||
return -EBUSY;
|
||||
}
|
||||
|
||||
static void set_kdamond_stop(struct damon_ctx *ctx)
|
||||
{
|
||||
mutex_lock(&ctx->kdamond_lock);
|
||||
ctx->kdamond_stop = true;
|
||||
mutex_unlock(&ctx->kdamond_lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* The monitoring daemon that runs as a kernel thread
|
||||
*/
|
||||
|
@ -1038,17 +1018,18 @@ static int kdamond_fn(void *data)
|
|||
struct damon_region *r, *next;
|
||||
unsigned int max_nr_accesses = 0;
|
||||
unsigned long sz_limit = 0;
|
||||
bool done = false;
|
||||
|
||||
pr_debug("kdamond (%d) starts\n", current->pid);
|
||||
|
||||
if (ctx->primitive.init)
|
||||
ctx->primitive.init(ctx);
|
||||
if (ctx->callback.before_start && ctx->callback.before_start(ctx))
|
||||
set_kdamond_stop(ctx);
|
||||
done = true;
|
||||
|
||||
sz_limit = damon_region_sz_limit(ctx);
|
||||
|
||||
while (!kdamond_need_stop(ctx)) {
|
||||
while (!kdamond_need_stop(ctx) && !done) {
|
||||
if (kdamond_wait_activation(ctx))
|
||||
continue;
|
||||
|
||||
|
@ -1056,7 +1037,7 @@ static int kdamond_fn(void *data)
|
|||
ctx->primitive.prepare_access_checks(ctx);
|
||||
if (ctx->callback.after_sampling &&
|
||||
ctx->callback.after_sampling(ctx))
|
||||
set_kdamond_stop(ctx);
|
||||
done = true;
|
||||
|
||||
kdamond_usleep(ctx->sample_interval);
|
||||
|
||||
|
@ -1069,7 +1050,7 @@ static int kdamond_fn(void *data)
|
|||
sz_limit);
|
||||
if (ctx->callback.after_aggregation &&
|
||||
ctx->callback.after_aggregation(ctx))
|
||||
set_kdamond_stop(ctx);
|
||||
done = true;
|
||||
kdamond_apply_schemes(ctx);
|
||||
kdamond_reset_aggregated(ctx);
|
||||
kdamond_split_regions(ctx);
|
||||
|
@ -1088,9 +1069,8 @@ static int kdamond_fn(void *data)
|
|||
damon_destroy_region(r, t);
|
||||
}
|
||||
|
||||
if (ctx->callback.before_terminate &&
|
||||
ctx->callback.before_terminate(ctx))
|
||||
set_kdamond_stop(ctx);
|
||||
if (ctx->callback.before_terminate)
|
||||
ctx->callback.before_terminate(ctx);
|
||||
if (ctx->primitive.cleanup)
|
||||
ctx->primitive.cleanup(ctx);
|
||||
|
||||
|
|
Loading…
Reference in New Issue