mm/damon: remove damon_callback
Subsystems (2)
SeongJae Park
maintainercover mm/damon: remove damon_callback
damon_callback was the only way for communicating with DAMON for contexts running on its worker thread. The interface is flexible and simple. But as DAMON evolves with more features, damon_callback has become somewhat too old. With runtime parameters update, for example, its lack of synchronization support was found to be inconvenient. Arguably it is also not easy to use correctly since the callers should understand when each callback is called, and implication of the return values from the callbacks.
To replace it, damon_call() and damos_walk() are introduced. And those replaced a few damon_callback use cases. Some use cases of damon_callback such as parallel or repetitive DAMON internal data reading and additional cleanups cannot simply be replaced by damon_call() and damos_walk(), though.
To allow those replaceable, extend damon_call() for parallel and/or repeated callbacks and modify the core/ops layers for additional resources cleanup. With the updates, replace the remaining damon_callback usages and finally remove damon_callback.
SeongJae Park (14):
mm/damon: accept parallel damon_call() requests mm/damon/core: introduce repeat mode damon_call() mm/damon/stat: use damon_call() repeat mode instead of damon_callback mm/damon/reclaim: use damon_call() repeat mode instead of
damon_callback
mm/damon/lru_sort: use damon_call() repeat mode instead of
damon_callback
samples/damon/prcl: use damon_call() repeat mode instead of
damon_callback
samples/damon/wsse: use damon_call() repeat mode instead of
damon_callback
mm/damon/core: do not call ops.cleanup() when destroying targets mm/damon/core: add cleanup_target() ops callback mm/damon/vaddr: put pid in cleanup_target() mm/damon/sysfs: remove damon_sysfs_destroy_targets() mm/damon/core: destroy targets when kdamond_fn() finish mm/damon/sysfs: remove damon_sysfs_before_terminate() mm/damon/core: remove damon_callback
include/linux/damon.h | 44 ++++--------- mm/damon/core.c | 116 ++++++++++++++++++----------------- mm/damon/lru_sort.c | 70 ++++++++++----------- mm/damon/reclaim.c | 62 +++++++++---------- mm/damon/stat.c | 17 ++++- mm/damon/sysfs.c | 41 +------------ mm/damon/tests/core-kunit.h | 4 +- mm/damon/tests/vaddr-kunit.h | 2 +- mm/damon/vaddr.c | 6 ++ samples/damon/prcl.c | 18 ++++-- samples/damon/wsse.c | 16 ++++- 11 files changed, 189 insertions(+), 207 deletions(-)
base-commit: d7932983411c60940e9b834ac554c33e0ca6f2ef
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 1/14 mm/damon: accept parallel damon_call() requests
Calling damon_call() while it is serving for another parallel thread immediately fails with -EBUSY. The caller should call it again, later. Each caller implementing such retry logic would be redundant. Accept parallel damon_call() requests and do the wait instead of the caller.
--- include/linux/damon.h | 7 +++++-- mm/damon/core.c | 49 ++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 26 deletions(-)
include/linux/damon.h +5 -2
@@ -673,6 +673,8 @@ struct damon_call_control {struct completion completion;/* informs if the kdamond canceled @fn infocation */ bool canceled; +/* List head for siblings. */ +struct list_head list; };/**
@@ -798,8 +800,9 @@ struct damon_ctx {/* for scheme quotas prioritization */ unsigned long *regions_score_histogram; -struct damon_call_control *call_control; -struct mutex call_control_lock; +/* lists of &struct damon_call_control */ +struct list_head call_controls; +struct mutex call_controls_lock;struct damos_walk_control *walk_control;struct mutex walk_control_lock;
mm/damon/core.c +25 -24
78 lines not on this page · raw message
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 2/14 mm/damon/core: introduce repeat mode damon_call()
In reply to SeongJae Parkdamon_call() can be useful for reading or writing DAMON internal data for one time. A common pattern of DAMON core usage from DAMON modules is doing such reads and writes repeatedly, for example, to periodically update the DAMOS stats. To do that with damon_call(), callers should call damon_call() repeatedly, with their own delay loop. Each caller doing that is repetitive. Introduce a repeat mode damon_call(). Callers can use the mode by setting a new field in damon_call_control. If the mode is turned on, damon_call() returns success immediately, and DAMON repeats invoking the callback function inside the kdamond main loop.
--- include/linux/damon.h | 2 ++ mm/damon/core.c | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-)
include/linux/damon.h +2 -0
@@ -659,6 +659,7 @@ struct damon_callback {* * @fn: Function to be called back. * @data: Data that will be passed to @fn. +* @repeat: Repeat invocations. * @return_code: Return code from @fn invocation. * * Control damon_call(), which requests specific kdamond to invoke a given
@@ -667,6 +668,7 @@ struct damon_callback {struct damon_call_control {int (*fn)(void *data);void *data; +bool repeat;int return_code;/* private: internal use only */ /* informs if the kdamond finished handling of the request */
mm/damon/core.c +19 -4
@@ -1339,8 +1339,9 @@ bool damon_is_running(struct damon_ctx *ctx)* * Ask DAMON worker thread (kdamond) of @ctx to call a function with an * argument data that respectively passed via &damon_call_control->fn and -* &damon_call_control->data of @control, and wait until the kdamond finishes -* handling of the request. +* &damon_call_control->data of @control. If &damon_call_control->repeat of +* @control is set, further wait until the kdamond finishes handling of the +* request. Otherwise, return as soon as the request is made. * * The kdamond executes the function with the argument in the main loop, just * after a sampling of the iteration is finished. The function can hence
@@ -1352,7 +1353,8 @@ bool damon_is_running(struct damon_ctx *ctx)*/ int damon_call (struct damon_ctx *ctx,struct damon_call_control *control) { -init_completion (&control->completion); +if (!control->repeat) +init_completion (&control->completion); control->canceled = false;INIT_LIST_HEAD (&control->list);
@@ -1361,6 +1363,8 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)mutex_unlock (&ctx->call_controls_lock);if (!damon_is_running (ctx))return -EINVAL ; +if (control->repeat) +return 0 ;wait_for_completion (&control->completion);if (control->canceled)return -ECANCELED ;
@@ -2389,6 +2393,7 @@ static void kdamond_usleep(unsigned long usecs)static void kdamond_call (struct damon_ctx *ctx,bool cancel) {struct damon_call_control *control; +LIST_HEAD (repeat_controls);int ret =0 ;while (true) {
@@ -2407,8 +2412,18 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)mutex_lock (&ctx->call_controls_lock);list_del (&control->list);mutex_unlock (&ctx->call_controls_lock); -complete (&control->completion); +if (!control->repeat) +complete (&control->completion); +else +list_add (&control->list, &repeat_controls); } + control =list_first_entry_or_null (&repeat_controls, + struct damon_call_control, list); +if (!control || cancel) +return ; +mutex_lock (&ctx->call_controls_lock); +list_add_tail (&control->list, &ctx->call_controls); +mutex_unlock (&ctx->call_controls_lock); }/* Returns negative error code if it's not activated but should return */
Signature · 1 line
2.39.5
SeongJae Park
maintainerRe: [RFC PATCH 02/14] mm/damon/core: introduce repeat mode damon_call()
Quoted 13 lines · On Sun, 6 Jul 2025 13:00:06 -0700 SeongJae Park <[email protected]> wrote:
damon_call()can be useful for reading or writing DAMON internal data for one time. A common pattern of DAMON core usage from DAMON modules is doing such reads and writes repeatedly, for example, to periodically update the DAMOS stats. To do that withdamon_call(), callers should calldamon_call()repeatedly, with their own delay loop. Each caller doing that is repetitive. Introduce a repeat modedamon_call(). Callers can use the mode by setting a new field indamon_call_control. If the mode is turned on,damon_call()returns success immediately, and DAMON repeats invoking the callback function inside the kdamond main loop.Signed-off-by SeongJae Park <[email protected]>---
[...]
Quoted 16 lines
(quoted diff) +4
-1@@ -2389,6 +2393,7 @@ static void kdamond_usleep(unsigned long usecs) static void kdamond_call(struct damon_ctx *ctx, bool cancel) { struct damon_call_control *control; + LIST_HEAD(repeat_controls); int ret = 0; while (true) {@@ -2407,8 +2412,18 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel) mutex_lock(&ctx->call_controls_lock); list_del(&control->list); mutex_unlock(&ctx->call_controls_lock); - complete(&control->completion); + if (!control->repeat) + complete(&control->completion); + else
Review static void kdamond_call(struct damon_ctx *ctx, bool cancel) · line 2427
Quoted 10 lines
(quoted diff) +8
-0+ list_add(&control->list, &repeat_controls); } + control = list_first_entry_or_null(&repeat_controls, + struct damon_call_control, list); + if (!control || cancel) + return; + mutex_lock(&ctx->call_controls_lock); + list_add_tail(&control->list, &ctx->call_controls); + mutex_unlock(&ctx->call_controls_lock); }
Below fixup should be added on this. Without it, the repeat_controls handling is never executed, so callback will be invoked only once.
mm/damon/core.c +1 -1
@@ -2402,7 +2402,7 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel) struct damon_call_control, list);mutex_unlock (&ctx->call_controls_lock);if (!control) -return ; +break ;if (cancel) { control->canceled = true; }else {
I'll add this on next version of this series.
Thanks,
SJ
[...]
SeongJae Park
maintainerRFC PATCH 3/14 mm/damon/stat: use damon_call() repeat mode instead of damon_callback
In reply to SeongJae ParkDAMON_STAT uses damon_callback for periodically reading DAMON internal data. Use its alternative, damon_call() repeat mode.
--- mm/damon/stat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
mm/damon/stat.c +14 -3
@@ -122,8 +122,9 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)kfree (sorted_regions); } -static int damon_stat_after_aggregation (struct damon_ctx *c) +static int damon_stat_damon_call_fn (void *data) { +struct damon_ctx *c = data;static unsigned long last_refresh_jiffies;/* avoid unnecessarily frequent stat update */
@@ -182,19 +183,29 @@ static struct damon_ctx *damon_stat_build_ctx(void)damon_add_target (ctx, target);if (damon_set_region_biggest_system_ram_default (target, &start, &end)) goto free_out; - ctx->callback.after_aggregation = damon_stat_after_aggregation;return ctx; free_out:damon_destroy_ctx (ctx);return NULL ; } +static struct damon_call_control call_control = { + .fn = damon_stat_damon_call_fn, + .repeat = true, +}; +static int damon_stat_start (void ) { +int err; + damon_stat_context =damon_stat_build_ctx ();if (!damon_stat_context)return -ENOMEM ; -return damon_start (&damon_stat_context,1 , true); + err =damon_start (&damon_stat_context,1 , true); +if (err) +return err; + call_control.data = damon_stat_context; +return damon_call (damon_stat_context, &call_control); }static void damon_stat_stop (void )
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 4/14 mm/damon/reclaim: use damon_call() repeat mode instead of damon_callback
In reply to SeongJae ParkDAMON_RECLAIM uses damon_callback for periodically reading and writing DAMON internal data and parameters. Use its alternative, damon_call() repeat mode.
--- mm/damon/reclaim.c | 62 +++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
mm/damon/reclaim.c +31 -31
@@ -238,6 +238,35 @@ static int damon_reclaim_apply_parameters(void)return err; } +static int damon_reclaim_handle_commit_inputs (void ) +{ +int err; + +if (!commit_inputs) +return 0 ; + + err =damon_reclaim_apply_parameters (); + commit_inputs = false; +return err; +} + +static int damon_reclaim_damon_call_fn (void *arg) +{ +struct damon_ctx *c = arg; +struct damos *s; + +/* update the stats parameter */ +damon_for_each_scheme (s, c) + damon_reclaim_stat = s->stat; + +return damon_reclaim_handle_commit_inputs (); +} + +static struct damon_call_control call_control = { + .fn = damon_reclaim_damon_call_fn, + .repeat = true, +}; +static int damon_reclaim_turn (bool on) {int err;
@@ -257,7 +286,7 @@ static int damon_reclaim_turn(bool on)if (err)return err; kdamond_pid = ctx->kdamond->pid; -return 0 ; +return damon_call (ctx, &call_control); } static int damon_reclaim_enabled_store(const char *val,
@@ -296,34 +325,6 @@ module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);MODULE_PARM_DESC (enabled,"Enable or disable DAMON_RECLAIM (default: disabled)" ); -static int damon_reclaim_handle_commit_inputs (void ) -{ -int err; - -if (!commit_inputs) -return 0 ; - - err =damon_reclaim_apply_parameters (); - commit_inputs = false; -return err; -} - -static int damon_reclaim_after_aggregation (struct damon_ctx *c) -{ -struct damos *s; - -/* update the stats parameter */ -damon_for_each_scheme (s, c) - damon_reclaim_stat = s->stat; - -return damon_reclaim_handle_commit_inputs (); -} - -static int damon_reclaim_after_wmarks_check (struct damon_ctx *c) -{ -return damon_reclaim_handle_commit_inputs (); -} -static int __init damon_reclaim_init (void ) {int err =damon_modules_new_paddr_ctx_target (&ctx, &target);
@@ -331,8 +332,7 @@ static int __init damon_reclaim_init(void)if (err) goto out; - ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check; - ctx->callback.after_aggregation = damon_reclaim_after_aggregation; + call_control.data = ctx;/* 'enabled' has set before this function, probably via command line */ if (enabled)
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 5/14 mm/damon/lru_sort: use damon_call() repeat mode instead of damon_callback
In reply to SeongJae ParkDAMON_LRU_SORT uses damon_callback for periodically reading and writing DAMON internal data and parameters. Use its alternative, damon_call() repeat mode.
--- mm/damon/lru_sort.c | 70 ++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 35 deletions(-)
mm/damon/lru_sort.c +35 -35
@@ -230,6 +230,39 @@ static int damon_lru_sort_apply_parameters(void)return err; } +static int damon_lru_sort_handle_commit_inputs (void ) +{ +int err; + +if (!commit_inputs) +return 0 ; + + err =damon_lru_sort_apply_parameters (); + commit_inputs = false; +return err; +} + +static int damon_lru_sort_damon_call_fn (void *arg) +{ +struct damon_ctx *c = arg; +struct damos *s; + +/* update the stats parameter */ +damon_for_each_scheme (s, c) { +if (s->action ==DAMOS_LRU_PRIO ) + damon_lru_sort_hot_stat = s->stat; +else if (s->action ==DAMOS_LRU_DEPRIO ) + damon_lru_sort_cold_stat = s->stat; + } + +return damon_lru_sort_handle_commit_inputs (); +} + +static struct damon_call_control call_control = { + .fn = damon_lru_sort_damon_call_fn, + .repeat = true, +}; +static int damon_lru_sort_turn (bool on) {int err;
@@ -249,7 +282,7 @@ static int damon_lru_sort_turn(bool on)if (err)return err; kdamond_pid = ctx->kdamond->pid; -return 0 ; +return damon_call (ctx, &call_control); } static int damon_lru_sort_enabled_store(const char *val,
@@ -288,38 +321,6 @@ module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);MODULE_PARM_DESC (enabled,"Enable or disable DAMON_LRU_SORT (default: disabled)" ); -static int damon_lru_sort_handle_commit_inputs (void ) -{ -int err; - -if (!commit_inputs) -return 0 ; - - err =damon_lru_sort_apply_parameters (); - commit_inputs = false; -return err; -} - -static int damon_lru_sort_after_aggregation (struct damon_ctx *c) -{ -struct damos *s; - -/* update the stats parameter */ -damon_for_each_scheme (s, c) { -if (s->action ==DAMOS_LRU_PRIO ) - damon_lru_sort_hot_stat = s->stat; -else if (s->action ==DAMOS_LRU_DEPRIO ) - damon_lru_sort_cold_stat = s->stat; - } - -return damon_lru_sort_handle_commit_inputs (); -} - -static int damon_lru_sort_after_wmarks_check (struct damon_ctx *c) -{ -return damon_lru_sort_handle_commit_inputs (); -} -static int __init damon_lru_sort_init (void ) {int err =damon_modules_new_paddr_ctx_target (&ctx, &target);
@@ -327,8 +328,7 @@ static int __init damon_lru_sort_init(void)if (err) goto out; - ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check; - ctx->callback.after_aggregation = damon_lru_sort_after_aggregation; + call_control.data = ctx;/* 'enabled' has set before this function, probably via command line */ if (enabled)
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 6/14 samples/damon/prcl: use damon_call() repeat mode instead of damon_callback
In reply to SeongJae Parkprcl uses damon_callback for periodically reading DAMON internal data. Use its alternative, damon_call() repeat mode.
--- samples/damon/prcl.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
samples/damon/prcl.c +14 -4
@@ -34,8 +34,9 @@ MODULE_PARM_DESC(enable, "Enable of disable DAMON_SAMPLE_WSSE");static struct damon_ctx *ctx;static struct pid *target_pidp; -static int damon_sample_prcl_after_aggregate (struct damon_ctx *c) +static int damon_sample_prcl_repeat_call_fn (void *data) { +struct damon_ctx *c = data;struct damon_target *t;damon_for_each_target (t, c) {
@@ -51,10 +52,16 @@ static int damon_sample_prcl_after_aggregate(struct damon_ctx *c)return 0 ; } +static struct damon_call_control repeat_call_control = { + .fn = damon_sample_prcl_repeat_call_fn, + .repeat = true, +}; +static int damon_sample_prcl_start (void ) {struct damon_target *target;struct damos *scheme; +int err;pr_info ("start\n" );
@@ -79,8 +86,6 @@ static int damon_sample_prcl_start(void) } target->pid = target_pidp; - ctx->callback.after_aggregation = damon_sample_prcl_after_aggregate; - scheme = damon_new_scheme( &(struct damos_access_pattern) { .min_sz_region = PAGE_SIZE,
@@ -100,7 +105,12 @@ static int damon_sample_prcl_start(void) }damon_set_schemes (ctx, &scheme,1 ); -return damon_start (&ctx,1 , true); + err =damon_start (&ctx,1 , true); +if (err) +return err; + + repeat_call_control.data = ctx; +return damon_call (ctx, &repeat_call_control); }static void damon_sample_prcl_stop (void )
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 7/14 samples/damon/wsse: use damon_call() repeat mode instead of damon_callback
In reply to SeongJae Parkwsse uses damon_callback for periodically reading DAMON internal data. Use its alternative, damon_call() repeat mode.
--- samples/damon/wsse.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
samples/damon/wsse.c +13 -3
@@ -35,8 +35,9 @@ MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE");static struct damon_ctx *ctx;static struct pid *target_pidp; -static int damon_sample_wsse_after_aggregate (struct damon_ctx *c) +static int damon_sample_wsse_repeat_call_fn (void *data) { +struct damon_ctx *c = data;struct damon_target *t;damon_for_each_target (t, c) {
@@ -52,9 +53,15 @@ static int damon_sample_wsse_after_aggregate(struct damon_ctx *c)return 0 ; } +static struct damon_call_control repeat_call_control = { + .fn = damon_sample_wsse_repeat_call_fn, + .repeat = true, +}; +static int damon_sample_wsse_start (void ) {struct damon_target *target; +int err;pr_info ("start\n" );
@@ -79,8 +86,11 @@ static int damon_sample_wsse_start(void) } target->pid = target_pidp; - ctx->callback.after_aggregation = damon_sample_wsse_after_aggregate; -return damon_start (&ctx,1 , true); + err =damon_start (&ctx,1 , true); +if (err) +return err; + repeat_call_control.data = ctx; +return damon_call (ctx, &repeat_call_control); }static void damon_sample_wsse_stop (void )
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 8/14 mm/damon/core: do not call ops.cleanup() when destroying targets
In reply to SeongJae Parkdamon_operations.cleanup() is documented to be called for kdamond termination, but also being called for targets destruction, which is done for any damon_ctx destruction. Nobody is using the callback for now, though. Remove the cleanup() call under the destruction.
--- mm/damon/core.c | 5 ----- 1 file changed, 5 deletions(-)
mm/damon/core.c +0 -5
@@ -550,11 +550,6 @@ static void damon_destroy_targets(struct damon_ctx *ctx) {struct damon_target *t, *next_t; -if (ctx->ops.cleanup) { - ctx->ops.cleanup (ctx); -return ; - } -damon_for_each_target_safe (t, next_t, ctx)damon_destroy_target (t); }
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 9/14 mm/damon/core: add cleanup_target() ops callback
In reply to SeongJae ParkSome DAMON operation sets may need additional cleanup per target. For example, [f]vaddr need to put pids of each target. Each user and core logic is doing that redundantly. Add another DAMON ops callback that will be used for doing such cleanups in operations set layer.
--- include/linux/damon.h | 4 +++- mm/damon/core.c | 12 ++++++++---- mm/damon/sysfs.c | 4 ++-- mm/damon/tests/core-kunit.h | 4 ++-- mm/damon/tests/vaddr-kunit.h | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-)
include/linux/damon.h +3 -1
@@ -608,6 +608,7 @@ enum damon_ops_id {* filters (&struct damos_filter) that handled by itself. * @target_valid should check whether the target is still valid for the * monitoring. +* @cleanup_target is called before the target will be deallocated. * @cleanup is called from @kdamond just before its termination. */ struct damon_operations {
@@ -623,6 +624,7 @@ struct damon_operations { struct damon_target *t, struct damon_region *r, struct damos *scheme, unsigned long *sz_filter_passed);bool (*target_valid)(struct damon_target *t); +void (*cleanup_target)(struct damon_target *t);void (*cleanup)(struct damon_ctx *context); };
@@ -933,7 +935,7 @@ struct damon_target *damon_new_target(void);void damon_add_target (struct damon_ctx *ctx,struct damon_target *t);bool damon_targets_empty (struct damon_ctx *ctx);void damon_free_target (struct damon_target *t); -void damon_destroy_target (struct damon_target *t); +void damon_destroy_target (struct damon_target *t,struct damon_ctx *ctx);unsigned int damon_nr_regions (struct damon_target *t);struct damon_ctx *damon_new_ctx (void );
mm/damon/core.c +8 -4
@@ -502,8 +502,12 @@ void damon_free_target(struct damon_target *t)kfree (t); } -void damon_destroy_target (struct damon_target *t) +void damon_destroy_target (struct damon_target *t,struct damon_ctx *ctx) { + +if (ctx && ctx->ops.cleanup_target) + ctx->ops.cleanup_target (t); +damon_del_target (t);damon_free_target (t); }
@@ -551,7 +555,7 @@ static void damon_destroy_targets(struct damon_ctx *ctx)struct damon_target *t, *next_t;damon_for_each_target_safe (t, next_t, ctx) -damon_destroy_target (t); +damon_destroy_target (t, ctx); }void damon_destroy_ctx (struct damon_ctx *ctx)
@@ -1097,7 +1101,7 @@ static int damon_commit_targets(if (damon_target_has_pid (dst))put_pid (dst_target->pid); -damon_destroy_target (dst_target); +damon_destroy_target (dst_target, dst);damon_for_each_scheme (s, dst) {if (s->quota.charge_target_from == dst_target) { s->quota.charge_target_from =NULL ;
@@ -1116,7 +1120,7 @@ static int damon_commit_targets( err =damon_commit_target (new_target, false, src_target,damon_target_has_pid (src));if (err) { -damon_destroy_target (new_target); +damon_destroy_target (new_target,NULL );return err; }damon_add_target (dst, new_target);
mm/damon/sysfs.c +2 -2
@@ -1303,7 +1303,7 @@ static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)damon_for_each_target_safe (t, next, ctx) {if (has_pid)put_pid (t->pid); -damon_destroy_target (t); +damon_destroy_target (t, ctx); } }
@@ -1389,7 +1389,7 @@ static void damon_sysfs_before_terminate(struct damon_ctx *ctx)damon_for_each_target_safe (t, next, ctx) {put_pid (t->pid); -damon_destroy_target (t); +damon_destroy_target (t, ctx); } }
mm/damon/tests/core-kunit.h +2 -2
@@ -58,7 +58,7 @@ static void damon_test_target(struct kunit *test)damon_add_target (c, t);KUNIT_EXPECT_EQ (test,1u ,nr_damon_targets (c)); -damon_destroy_target (t); +damon_destroy_target (t, c);KUNIT_EXPECT_EQ (test,0u ,nr_damon_targets (c));damon_destroy_ctx (c);
@@ -310,7 +310,7 @@ static void damon_test_set_regions(struct kunit *test)KUNIT_EXPECT_EQ (test, r->ar.start, expects[expect_idx++]);KUNIT_EXPECT_EQ (test, r->ar.end, expects[expect_idx++]); } -damon_destroy_target (t); +damon_destroy_target (t,NULL ); }static void damon_test_nr_accesses_to_accesses_bp (struct kunit *test)
mm/damon/tests/vaddr-kunit.h +1 -1
@@ -149,7 +149,7 @@ static void damon_do_test_apply_three_regions(struct kunit *test,KUNIT_EXPECT_EQ (test, r->ar.end, expected[i *2 +1 ]); } -damon_destroy_target (t); +damon_destroy_target (t,NULL ); }/*
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 10/14 mm/damon/vaddr: put pid in cleanup_target()
In reply to SeongJae ParkImplement cleanup_target() callback for [f]vaddr, which calls put_pid() for each target that will be destroyed. Also remove redundant put_pid() calls in core and sysfs, which was required to be done redundantly due to the lack of such self cleanup in vaddr.
--- mm/damon/core.c | 2 -- mm/damon/sysfs.c | 10 ++-------- mm/damon/vaddr.c | 6 ++++++ 3 files changed, 8 insertions(+), 10 deletions(-)
mm/damon/core.c +0 -2
@@ -1099,8 +1099,6 @@ static int damon_commit_targets( }else {struct damos *s; -if (damon_target_has_pid (dst)) -put_pid (dst_target->pid);damon_destroy_target (dst_target, dst);damon_for_each_scheme (s, dst) {if (s->quota.charge_target_from == dst_target) {
mm/damon/sysfs.c +2 -8
@@ -1298,13 +1298,9 @@ static int damon_sysfs_set_attrs(struct damon_ctx *ctx,static void damon_sysfs_destroy_targets (struct damon_ctx *ctx) {struct damon_target *t, *next; -bool has_pid =damon_target_has_pid (ctx); -damon_for_each_target_safe (t, next, ctx) { -if (has_pid) -put_pid (t->pid); +damon_for_each_target_safe (t, next, ctx)damon_destroy_target (t, ctx); - } } static int damon_sysfs_set_regions(struct damon_target *t,
@@ -1387,10 +1383,8 @@ static void damon_sysfs_before_terminate(struct damon_ctx *ctx)if (!damon_target_has_pid (ctx))return ; -damon_for_each_target_safe (t, next, ctx) { -put_pid (t->pid); +damon_for_each_target_safe (t, next, ctx)damon_destroy_target (t, ctx); - } }/*
mm/damon/vaddr.c +6 -0
@@ -627,6 +627,11 @@ static bool damon_va_target_valid(struct damon_target *t)return false; } +static void damon_va_cleanup_target (struct damon_target *t) +{ +put_pid (t->pid); +} +#ifndef CONFIG_ADVISE_SYSCALLS static unsigned long damos_madvise (struct damon_target *target,struct damon_region *r,int behavior)
@@ -711,6 +716,7 @@ static int __init damon_va_initcall(void) .prepare_access_checks = damon_va_prepare_access_checks, .check_accesses = damon_va_check_accesses, .target_valid = damon_va_target_valid, + .cleanup_target = damon_va_cleanup_target, .cleanup = NULL, .apply_scheme = damon_va_apply_scheme, .get_scheme_score = damon_va_scheme_score,
Signature · 1 line
2.39.5
SeongJae Park
maintainerRe: [RFC PATCH 10/14] mm/damon/vaddr: put pid in cleanup_target()
Quoted 11 lines · On Sun, 6 Jul 2025 13:00:14 -0700 SeongJae Park <[email protected]> wrote:
Implement
cleanup_target()callback for [f]vaddr, which callsput_pid()for each target that will be destroyed. Also remove redundantput_pid()calls in core and sysfs, which was required to be done redundantly due to the lack of such self cleanup in vaddr.Signed-off-by SeongJae Park <[email protected]>--- mm/damon/core.c | 2 -- mm/damon/sysfs.c | 10 ++-------- mm/damon/vaddr.c | 6 ++++++ 3 files changed, 8 insertions(+), 10 deletions(-)
This patch forgot removing put_pid() from DAMON sample modules. I will add below fixup on the next version.
Thanks,
SJ
[...]
---------------------- >8 --------------------------
samples/damon/prcl.c +0 -2
@@ -120,8 +120,6 @@ static void damon_sample_prcl_stop(void)damon_stop (&ctx,1 );damon_destroy_ctx (ctx); } -if (target_pidp) -put_pid (target_pidp); }static bool init_called;
samples/damon/wsse.c +0 -2
@@ -100,8 +100,6 @@ static void damon_sample_wsse_stop(void)damon_stop (&ctx,1 );damon_destroy_ctx (ctx); } -if (target_pidp) -put_pid (target_pidp); }static bool init_called;
SeongJae Park
maintainerRFC PATCH 11/14 mm/damon/sysfs: remove damon_sysfs_destroy_targets()
In reply to SeongJae ParkThe function was introduced for putting pids and deallocating unnecessary targets. Hence it is called before damon_destroy_ctx(). Now vaddr puts pid for each target destruction (cleanup_target()). damon_destroy_ctx() deallocates the targets anyway. So damon_sysfs_destroy_targets() has no reason to exist. Remove it.
--- mm/damon/sysfs.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-)
mm/damon/sysfs.c +3 -20
@@ -1295,14 +1295,6 @@ static int damon_sysfs_set_attrs(struct damon_ctx *ctx,return damon_set_attrs (ctx, &attrs); } -static void damon_sysfs_destroy_targets (struct damon_ctx *ctx) -{ -struct damon_target *t, *next; - -damon_for_each_target_safe (t, next, ctx) -damon_destroy_target (t, ctx); -} -static int damon_sysfs_set_regions (struct damon_target *t,struct damon_sysfs_regions *sysfs_regions) {
@@ -1337,7 +1329,6 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, struct damon_ctx *ctx) {struct damon_target *t =damon_new_target (); -int err = -EINVAL ;if (!t)return -ENOMEM ;
@@ -1345,16 +1336,10 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,if (damon_target_has_pid (ctx)) { t->pid =find_get_pid (sys_target->pid);if (!t->pid) - goto destroy_targets_out; +/* caller will destroy targets */ +return -EINVAL ; } - err =damon_sysfs_set_regions (t, sys_target->regions); -if (err) - goto destroy_targets_out; -return 0 ; - -destroy_targets_out: -damon_sysfs_destroy_targets (ctx); -return err; +return damon_sysfs_set_regions (t, sys_target->regions); } static int damon_sysfs_add_targets(struct damon_ctx *ctx,
@@ -1458,13 +1443,11 @@ static int damon_sysfs_commit_input(void *data) test_ctx =damon_new_ctx (); err =damon_commit_ctx (test_ctx, param_ctx);if (err) { -damon_sysfs_destroy_targets (test_ctx);damon_destroy_ctx (test_ctx); goto out; } err =damon_commit_ctx (kdamond->damon_ctx, param_ctx); out: -damon_sysfs_destroy_targets (param_ctx);damon_destroy_ctx (param_ctx);return err; }
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 12/14 mm/damon/core: destroy targets when kdamond_fn() finish
In reply to SeongJae ParkWhen kdamond_fn() completes, the targets are kept. Those are kept to let callers do additional cleanups if they need. There are no such additional cleanups though. DAMON sysfs interface deallocates those in before_terminate() callback, to reduce unnecessary memory usage, for [f]vaddr use case. Just destroy the targets for every case in the core layer. This saves more memory and simplifies the logic.
--- mm/damon/core.c | 1 + 1 file changed, 1 insertion(+)
mm/damon/core.c +1 -0
@@ -2617,6 +2617,7 @@ static int kdamond_fn(void *data) running_exclusive_ctxs = false;mutex_unlock (&damon_lock); +damon_destroy_targets (ctx);return 0 ; }
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 13/14 mm/damon/sysfs: remove damon_sysfs_before_terminate()
In reply to SeongJae ParkDAMON core layer does target cleanup on its own. Remove duplicated and unnecessarily selective cleanup attempts in DAMON sysfs interface.
--- mm/damon/sysfs.c | 12 ------------ 1 file changed, 12 deletions(-)
mm/damon/sysfs.c +0 -12
@@ -1361,17 +1361,6 @@ static int damon_sysfs_add_targets(struct damon_ctx *ctx,return 0 ; } -static void damon_sysfs_before_terminate (struct damon_ctx *ctx) -{ -struct damon_target *t, *next; - -if (!damon_target_has_pid (ctx)) -return ; - -damon_for_each_target_safe (t, next, ctx) -damon_destroy_target (t, ctx); -} -/* * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files. * @data: The kobject wrapper that associated to the kdamond thread.
@@ -1516,7 +1505,6 @@ static struct damon_ctx *damon_sysfs_build_ctx(return ERR_PTR (err); } - ctx->callback.before_terminate = damon_sysfs_before_terminate;return ctx; }
Signature · 1 line
2.39.5
SeongJae Park
maintainerRFC PATCH 14/14 mm/damon/core: remove damon_callback
In reply to SeongJae ParkAll damon_callback usages are replicated by damon_call() and damos_walk(). Remove damon_callback.
--- include/linux/damon.h | 31 +------------------------------ mm/damon/core.c | 26 +++++++------------------- 2 files changed, 8 insertions(+), 49 deletions(-)
include/linux/damon.h +1 -30
@@ -628,34 +628,6 @@ struct damon_operations {void (*cleanup)(struct damon_ctx *context); }; -/** -* struct damon_callback - Monitoring events notification callbacks. -* -* @after_wmarks_check: Called after each schemes' watermarks check. -* @after_aggregation: Called after each aggregation. -* @before_terminate: Called before terminating the monitoring. -* -* The monitoring thread (&damon_ctx.kdamond) calls @before_terminate just -* before finishing the monitoring. -* -* The monitoring thread calls @after_wmarks_check after each DAMON-based -* operation schemes' watermarks check. If users need to make changes to the -* attributes of the monitoring context while it's deactivated due to the -* watermarks, this is the good place to do. -* -* The monitoring thread calls @after_aggregation for each of the aggregation -* intervals. Therefore, users can safely access the monitoring results -* without additional protection. For the reason, users are recommended to use -* these callback for the accesses to the results. -* -* If any callback returns non-zero, monitoring stops. -*/ -struct damon_callback { -int (*after_wmarks_check)(struct damon_ctx *context); -int (*after_aggregation)(struct damon_ctx *context); -void (*before_terminate)(struct damon_ctx *context); -}; -/* * struct damon_call_control - Control damon_call(). *
@@ -726,7 +698,7 @@ struct damon_intervals_goal {* ``mmap()`` calls from the application, in case of virtual memory monitoring) * and applies the changes for each @ops_update_interval. All time intervals * are in micro-seconds. Please refer to &struct damon_operations and &struct -* damon_callback for more detail. +* damon_call_control for more detail. */ struct damon_attrs {unsigned long sample_interval;
@@ -816,7 +788,6 @@ struct damon_ctx {struct mutex kdamond_lock;struct damon_operations ops; -struct damon_callback callback;struct list_head adaptive_targets;struct list_head schemes;
mm/damon/core.c +7 -19
@@ -680,9 +680,7 @@ static bool damon_valid_intervals_goal(struct damon_attrs *attrs)* @attrs: monitoring attributes * * This function should be called while the kdamond is not running, an access -* check results aggregation is not ongoing (e.g., from &struct -* damon_callback->after_aggregation or &struct -* damon_callback->after_wmarks_check callbacks), or from damon_call(). +* check results aggregation is not ongoing (e.g., from damon_call(). * * Every time interval is in micro-seconds. *
@@ -778,7 +776,7 @@ static void damos_commit_quota_goal(* DAMON contexts, instead of manual in-place updates. * * This function should be called from parameters-update safe context, like -* DAMON callbacks. +* damon_call(). */ int damos_commit_quota_goals (struct damos_quota *dst,struct damos_quota *src) {
@@ -1137,7 +1135,7 @@ static int damon_commit_targets(* in-place updates. * * This function should be called from parameters-update safe context, like -* DAMON callbacks. +* damon_call(). */ int damon_commit_ctx (struct damon_ctx *dst,struct damon_ctx *src) {
@@ -2444,9 +2442,6 @@ static int kdamond_wait_activation(struct damon_ctx *ctx)kdamond_usleep (min_wait_time); -if (ctx->callback.after_wmarks_check && - ctx->callback.after_wmarks_check (ctx)) -break ;kdamond_call (ctx, false);damos_walk_cancel (ctx); }
@@ -2503,10 +2498,9 @@ static int kdamond_fn(void *data)while (!kdamond_need_stop (ctx)) {/* * ctx->attrs and ctx->next_{aggregation,ops_update}_sis could -* be changed from after_wmarks_check() or after_aggregation() -* callbacks. Read the values here, and use those for this -* iteration. That is, damon_set_attrs() updated new values -* are respected from next iteration. +* be changed from kdamond_call(). Read the values here, and +* use those for this iteration. That is, damon_set_attrs() +* updated new values are respected from next iteration. */ unsigned long next_aggregation_sis = ctx->next_aggregation_sis;unsigned long next_ops_update_sis = ctx->next_ops_update_sis;
@@ -2524,14 +2518,10 @@ static int kdamond_fn(void *data)if (ctx->ops.check_accesses) max_nr_accesses = ctx->ops.check_accesses (ctx); -if (ctx->passed_sample_intervals >= next_aggregation_sis) { +if (ctx->passed_sample_intervals >= next_aggregation_sis)kdamond_merge_regions (ctx, max_nr_accesses /10 , sz_limit); -if (ctx->callback.after_aggregation && - ctx->callback.after_aggregation (ctx)) -break ; - }/* * do kdamond_call() and kdamond_apply_schemes() after
@@ -2597,8 +2587,6 @@ static int kdamond_fn(void *data)damon_destroy_region (r, t); } -if (ctx->callback.before_terminate) - ctx->callback.before_terminate (ctx);if (ctx->ops.cleanup) ctx->ops.cleanup (ctx);kfree (ctx->regions_score_histogram);
Signature · 1 line
2.39.5