Skip to content

Commit 0f91d13

Browse files
changbindutorvalds
authored andcommitted
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/[email protected] Signed-off-by: Changbin Du <[email protected]> Reviewed-by: SeongJae Park <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0d16cfd commit 0f91d13

File tree

2 files changed

+15
-37
lines changed

2 files changed

+15
-37
lines changed

include/linux/damon.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ struct damon_ctx {
381381

382382
/* public: */
383383
struct task_struct *kdamond;
384-
bool kdamond_stop;
385384
struct mutex kdamond_lock;
386385

387386
struct damon_primitive primitive;

mm/damon/core.c

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,6 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
390390
return sz;
391391
}
392392

393-
static bool damon_kdamond_running(struct damon_ctx *ctx)
394-
{
395-
bool running;
396-
397-
mutex_lock(&ctx->kdamond_lock);
398-
running = ctx->kdamond != NULL;
399-
mutex_unlock(&ctx->kdamond_lock);
400-
401-
return running;
402-
}
403-
404393
static int kdamond_fn(void *data);
405394

406395
/*
@@ -418,7 +407,6 @@ static int __damon_start(struct damon_ctx *ctx)
418407
mutex_lock(&ctx->kdamond_lock);
419408
if (!ctx->kdamond) {
420409
err = 0;
421-
ctx->kdamond_stop = false;
422410
ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
423411
nr_running_ctxs);
424412
if (IS_ERR(ctx->kdamond)) {
@@ -474,13 +462,15 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
474462
*/
475463
static int __damon_stop(struct damon_ctx *ctx)
476464
{
465+
struct task_struct *tsk;
466+
477467
mutex_lock(&ctx->kdamond_lock);
478-
if (ctx->kdamond) {
479-
ctx->kdamond_stop = true;
468+
tsk = ctx->kdamond;
469+
if (tsk) {
470+
get_task_struct(tsk);
480471
mutex_unlock(&ctx->kdamond_lock);
481-
while (damon_kdamond_running(ctx))
482-
usleep_range(ctx->sample_interval,
483-
ctx->sample_interval * 2);
472+
kthread_stop(tsk);
473+
put_task_struct(tsk);
484474
return 0;
485475
}
486476
mutex_unlock(&ctx->kdamond_lock);
@@ -925,12 +915,8 @@ static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
925915
static bool kdamond_need_stop(struct damon_ctx *ctx)
926916
{
927917
struct damon_target *t;
928-
bool stop;
929918

930-
mutex_lock(&ctx->kdamond_lock);
931-
stop = ctx->kdamond_stop;
932-
mutex_unlock(&ctx->kdamond_lock);
933-
if (stop)
919+
if (kthread_should_stop())
934920
return true;
935921

936922
if (!ctx->primitive.target_valid)
@@ -1021,13 +1007,6 @@ static int kdamond_wait_activation(struct damon_ctx *ctx)
10211007
return -EBUSY;
10221008
}
10231009

1024-
static void set_kdamond_stop(struct damon_ctx *ctx)
1025-
{
1026-
mutex_lock(&ctx->kdamond_lock);
1027-
ctx->kdamond_stop = true;
1028-
mutex_unlock(&ctx->kdamond_lock);
1029-
}
1030-
10311010
/*
10321011
* The monitoring daemon that runs as a kernel thread
10331012
*/
@@ -1038,25 +1017,26 @@ static int kdamond_fn(void *data)
10381017
struct damon_region *r, *next;
10391018
unsigned int max_nr_accesses = 0;
10401019
unsigned long sz_limit = 0;
1020+
bool done = false;
10411021

10421022
pr_debug("kdamond (%d) starts\n", current->pid);
10431023

10441024
if (ctx->primitive.init)
10451025
ctx->primitive.init(ctx);
10461026
if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1047-
set_kdamond_stop(ctx);
1027+
done = true;
10481028

10491029
sz_limit = damon_region_sz_limit(ctx);
10501030

1051-
while (!kdamond_need_stop(ctx)) {
1031+
while (!kdamond_need_stop(ctx) && !done) {
10521032
if (kdamond_wait_activation(ctx))
10531033
continue;
10541034

10551035
if (ctx->primitive.prepare_access_checks)
10561036
ctx->primitive.prepare_access_checks(ctx);
10571037
if (ctx->callback.after_sampling &&
10581038
ctx->callback.after_sampling(ctx))
1059-
set_kdamond_stop(ctx);
1039+
done = true;
10601040

10611041
usleep_range(ctx->sample_interval, ctx->sample_interval + 1);
10621042

@@ -1069,7 +1049,7 @@ static int kdamond_fn(void *data)
10691049
sz_limit);
10701050
if (ctx->callback.after_aggregation &&
10711051
ctx->callback.after_aggregation(ctx))
1072-
set_kdamond_stop(ctx);
1052+
done = true;
10731053
kdamond_apply_schemes(ctx);
10741054
kdamond_reset_aggregated(ctx);
10751055
kdamond_split_regions(ctx);
@@ -1088,9 +1068,8 @@ static int kdamond_fn(void *data)
10881068
damon_destroy_region(r, t);
10891069
}
10901070

1091-
if (ctx->callback.before_terminate &&
1092-
ctx->callback.before_terminate(ctx))
1093-
set_kdamond_stop(ctx);
1071+
if (ctx->callback.before_terminate)
1072+
ctx->callback.before_terminate(ctx);
10941073
if (ctx->primitive.cleanup)
10951074
ctx->primitive.cleanup(ctx);
10961075

0 commit comments

Comments
 (0)