mm/damon: modified damon_call_control from static to kmalloc
Subsystems (2)
PATCH 1/1 mm/damon: modified damon_call_control from static to kmalloc
The damon_call_control structure is used by all damon modules. In mm/damon/sysfs.c, it is dynamically allocated with kmalloc(), and in the remaining modules, it is statically coded. There are many daemon modules and they are designed to be used only when needed. It is more efficient to use kmalloc dynamically whenever needed as in damon/sysfs.c. Using damon_call_control dynamically and consistently has the advantage of eliminating the dealloc_on_cancel member variable condition and reducing code size.
--- include/linux/damon.h | 2 -- mm/damon/core.c | 2 +- mm/damon/lru_sort.c | 20 ++++++++++++-------- mm/damon/reclaim.c | 20 ++++++++++++-------- mm/damon/stat.c | 18 +++++++++++------- mm/damon/sysfs.c | 1 - samples/damon/prcl.c | 17 ++++++++++------- samples/damon/wsse.c | 18 +++++++++++------- 8 files changed, 57 insertions(+), 41 deletions(-)
include/linux/damon.h +0 -2
@@ -636,7 +636,6 @@ struct damon_operations {* @data: Data that will be passed to @fn. * @repeat: Repeat invocations. * @return_code: Return code from @fn invocation. -* @dealloc_on_cancel: De-allocate when canceled. * * Control damon_call(), which requests specific kdamond to invoke a given * function. Refer to damon_call() for more details.
@@ -646,7 +645,6 @@ struct damon_call_control {void *data;bool repeat;int return_code; -bool dealloc_on_cancel;/* private: internal use only */ /* informs if the kdamond finished handling of the request */ struct completion completion;
mm/damon/core.c +1 -1
@@ -2519,7 +2519,7 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)mutex_unlock (&ctx->call_controls_lock);if (!control->repeat) {complete (&control->completion); - }else if (control->canceled && control->dealloc_on_cancel) { + }else if (control->canceled) {kfree (control);continue ; }else {
mm/damon/lru_sort.c +12 -8
@@ -279,14 +279,10 @@ static int damon_lru_sort_damon_call_fn(void *arg)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; +struct damon_call_control *call_control;if (!on) { err =damon_stop (&ctx,1 );
@@ -302,8 +298,18 @@ static int damon_lru_sort_turn(bool on) err =damon_start (&ctx,1 , true);if (err)return err; + kdamond_pid = ctx->kdamond->pid; -return damon_call (ctx, &call_control); + + call_control =kmalloc (sizeof (*call_control),GFP_KERNEL ); +if (!call_control) +return -ENOMEM ; + + call_control->fn = damon_lru_sort_damon_call_fn; + call_control->repeat = true; + call_control->data = ctx; + +return damon_call (ctx, call_control); } static int damon_lru_sort_addr_unit_store(const char *val,
@@ -378,8 +384,6 @@ static int __init damon_lru_sort_init(void)if (err) goto out; - call_control.data = ctx; -/* 'enabled' has set before this function, probably via command line */ if (enabled) err =damon_lru_sort_turn (true);
mm/damon/reclaim.c +12 -8
@@ -283,14 +283,10 @@ static int damon_reclaim_damon_call_fn(void *arg)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; +struct damon_call_control *call_control;if (!on) { err =damon_stop (&ctx,1 );
@@ -306,8 +302,18 @@ static int damon_reclaim_turn(bool on) err =damon_start (&ctx,1 , true);if (err)return err; + kdamond_pid = ctx->kdamond->pid; -return damon_call (ctx, &call_control); + + call_control =kmalloc (sizeof (*call_control),GFP_KERNEL ); +if (!call_control) +return -ENOMEM ; + + call_control->fn = damon_reclaim_damon_call_fn; + call_control->repeat = true; + call_control->data = ctx; + +return damon_call (ctx, call_control); } static int damon_reclaim_addr_unit_store(const char *val,
@@ -382,8 +388,6 @@ static int __init damon_reclaim_init(void)if (err) goto out; - call_control.data = ctx; -/* 'enabled' has set before this function, probably via command line */ if (enabled) err =damon_reclaim_turn (true);
mm/damon/stat.c +11 -7
@@ -196,14 +196,10 @@ static struct damon_ctx *damon_stat_build_ctx(void)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; +struct damon_call_control *call_control; damon_stat_context =damon_stat_build_ctx ();if (!damon_stat_context)
@@ -213,8 +209,16 @@ static int damon_stat_start(void)return err; damon_stat_last_refresh_jiffies = jiffies; - call_control.data = damon_stat_context; -return damon_call (damon_stat_context, &call_control); + + call_control =kmalloc (sizeof (*call_control),GFP_KERNEL ); +if (!call_control) +return -ENOMEM ; + + call_control->fn = damon_stat_damon_call_fn; + call_control->repeat = true; + call_control->data = damon_stat_context; + +return damon_call (damon_stat_context, call_control); }static void damon_stat_stop (void )
mm/damon/sysfs.c +0 -1
@@ -1614,7 +1614,6 @@ static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond) repeat_call_control->fn = damon_sysfs_repeat_call_fn; repeat_call_control->data = kdamond; repeat_call_control->repeat = true; - repeat_call_control->dealloc_on_cancel = true;damon_call (ctx, repeat_call_control);return err; }
samples/damon/prcl.c +10 -7
@@ -52,15 +52,11 @@ static int damon_sample_prcl_repeat_call_fn(void *data)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; +struct damon_call_control *call_control;int err;pr_info ("start\n" );
@@ -109,8 +105,15 @@ static int damon_sample_prcl_start(void)if (err)return err; - repeat_call_control.data = ctx; -return damon_call (ctx, &repeat_call_control); + call_control =kmalloc (sizeof (*call_control),GFP_KERNEL ); +if (!call_control) +return -ENOMEM ; + + call_control->fn = damon_sample_prcl_repeat_call_fn; + call_control->repeat = true; + call_control->data = ctx; + +return damon_call (ctx, call_control); }static void damon_sample_prcl_stop (void )
samples/damon/wsse.c +11 -7
@@ -53,14 +53,10 @@ static int damon_sample_wsse_repeat_call_fn(void *data)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; +struct damon_call_control *call_control;int err;pr_info ("start\n" );
@@ -89,8 +85,16 @@ static int damon_sample_wsse_start(void) err =damon_start (&ctx,1 , true);if (err)return err; - repeat_call_control.data = ctx; -return damon_call (ctx, &repeat_call_control); + + call_control =kmalloc (sizeof (*call_control),GFP_KERNEL ); +if (!call_control) +return -ENOMEM ; + + call_control->fn = damon_sample_wsse_repeat_call_fn; + call_control->repeat = true; + call_control->data = ctx; + +return damon_call (ctx, call_control); }static void damon_sample_wsse_stop (void )
--
2.43.0
SeongJae Park
maintainerQuoted 2 lines · On Sun, 7 Dec 2025 07:47:23 +0900 JaeJoon Jung <[email protected]> wrote:
The
damon_call_controlstructure is used by all damon modules. Inmm/damon/sysfs.c, it is dynamically allocated withkmalloc(),
It is only for refresh_ms handling damon_call_control. damon_sysfs_damon_call() doesn't use kmalloc().
Quoted 2 lines
and in
the remaining modules, it is statically coded.
I try not to use dynamic allocation when possible and *reasonable*, since doing so eliminates the chances of bugs from dynamic allocation handling mistakes.
Quoted 3 lines
There are many daemon
modules and they are designed to be used only when needed. It is more efficient to use kmalloc dynamically whenever needed as indamon/sysfs.c.
I agree that is more efficient in terms of kernel size. Nevertheless, I don't think it is a significant improvement, since it is only single data structure per the DAMON module. The total number of DAMON modules is only five. Two among those are samples.
Quoted 3 lines
Using
damon_call_controldynamically and consistently has the advantage of eliminating thedealloc_on_cancelmember variable condition and reducing code size.
Disadvantage of this change is that it is adding four more dynamic allocations that we need to cautiously maintain. It is not significantly increasing the maintenance overhead. But, the advantage (kernel size reduction) also seems only modest, if I'm not wrong.
So I'm sadly have to say I don't think this change is really needed.
Please let me know if I'm missing something, though.
Thanks,
SJ
[...]
Hello, SeongJae,
Thank you for your detailed feedback.
My patch has the advantage of removing the use of the damon_call_control.dealloc_on_cancel variable. Other than that, I agree with you pointed out. It's not a patch that significantly improves functionality, so I'm fine with doing whatever you decide.
Thanks,
JaeJoon
Quoted 42 lines · SeongJae Park
Quoted 2 lines · On Sun, 7 Dec 2025 07:47:23 +0900 JaeJoon Jung <[email protected]> wrote:
The
damon_call_controlstructure is used by all damon modules. Inmm/damon/sysfs.c, it is dynamically allocated withkmalloc(),It is only for
refresh_mshandlingdamon_call_control.damon_sysfs_damon_call()doesn't usekmalloc().Quoted 2 lines
and in
the remaining modules, it is statically coded.I try not to use dynamic allocation when possible and *reasonable*, since doing so eliminates the chances of bugs from dynamic allocation handling mistakes.
Quoted 3 lines
There are many daemon
modules and they are designed to be used only when needed. It is more efficient to use kmalloc dynamically whenever needed as indamon/sysfs.c.I agree that is more efficient in terms of kernel size. Nevertheless, I don't think it is a significant improvement, since it is only single data structure per the DAMON module. The total number of DAMON modules is only five. Two among those are samples.
Quoted 3 lines
Using
damon_call_controldynamically and consistently has the advantage of eliminating thedealloc_on_cancelmember variable condition and reducing code size.Disadvantage of this change is that it is adding four more dynamic allocations that we need to cautiously maintain. It is not significantly increasing the maintenance overhead. But, the advantage (kernel size reduction) also seems only modest, if I'm not wrong.
So I'm sadly have to say I don't think this change is really needed.
Please let me know if I'm missing something, though.
Thanks,
SJ[...]
SeongJae Park
maintainerQuoted 5 lines · On Tue, 9 Dec 2025 21:20:42 +0900 JaeJoon Jung <[email protected]> wrote:
Hello, SeongJae,
Thank you for your detailed feedback.
My patch has the advantage of removing the use of the damon_call_control.dealloc_on_cancel variable.
Thank you for keeping this conversation for enlightening me, Jaejoon.
Could you please elaborate why you think the removal is an advantage? I understand reducing code is good in general. But that's only if the code is unnecessary. dealloc_on_cancel is there for real use case, so I don't clearly see why it is an advantage.
Meanwhile I find the feature might look complicated, or not well documented. Specifically, dealloc_on_cancel should be set on only dynamic-allocated damon_call_control object, but that is not well documented on the kernel-doc comments. Are you saying removing it is an advantage because it makes reading code easier? If that's the case, how about improving the documentation?
Btw, please consider not doing "top posting" [1].
[1] https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying
Thanks,
SJ
[...]
Quoted 9 lines · SeongJae Park
Quoted 5 lines · On Tue, 9 Dec 2025 21:20:42 +0900 JaeJoon Jung <[email protected]> wrote:
Hello, SeongJae,
Thank you for your detailed feedback.
My patch has the advantage of removing the use of the damon_call_control.dealloc_on_cancel variable.
Quoted 6 lines
Thank you for keeping this conversation for enlightening me, Jaejoon.
Could you please elaborate why you think the removal is an advantage? I understand reducing code is good in general. But that's only if the code is unnecessary.
dealloc_on_cancelis there for real use case, so I don't clearly see why it is an advantage.
I suggested removing the dealloc_on_cancel condition from the patch. When using the damon_call_control structure, if you only use kmalloc()/kfree() when canceled without dealloc_on_cancel. However, currently, in other modules, since it is static struct damon_call_control { ... }, kfree() is not allowed, so dealloc_on_cancel condition is additionally required.
Because kmalloc()/kfree() and static are mixed together, dealloc_on_cancel condition is required.
As you pointed out in the previous email, kmalloc()/kfree() has the burden of memory allocation and deallocation, and static has the disadvantage of increasing
the code size. Both have their pros and cons.
Among these, I proposed unifying them into kmalloc()/kfree() methods and removing the dealloc_on_cancel
Quoted 7 lines
Meanwhile I find the feature might look complicated, or not well documented. Specifically,
dealloc_on_cancelshould be set on only dynamic-allocateddamon_call_controlobject, but that is not well documented on the kernel-doc comments. Are you saying removing it is an advantage because it makes reading code easier? If that's the case, how about improving the documentation?
dealloc_on_cancel is only used in mm/damon/sysfs.c. Here, since kmalloc the damon_call_control structure, the dealloc_on_cancel=true condition is required. If you want to keep the current code as is and only change the comment, it would be clearer to comment it out like this:
De-allocate when canceled.
--> To perform kfree() if allocated with kmalloc() when canceled.
Quoted 9 lines
Btw, please consider not doing "top posting" [1].
[1] https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying
Thanks,
SJ[...]
SeongJae Park
maintainerQuoted 3 lines · On Thu, 11 Dec 2025 12:29:59 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 2 lines · SeongJae Park
On Tue, 9 Dec 2025 21:20:42 +0900 JaeJoon Jung <[email protected]> wrote:
[...]
Quoted 5 lines
I suggested removing the
dealloc_on_cancelcondition from the patch. When using thedamon_call_controlstructure, if you only use kmalloc()/kfree() when canceled withoutdealloc_on_cancel. However, currently, in other modules, since it is static structdamon_call_control{ ... },kfree()is not allowed, sodealloc_on_cancelcondition is additionally required.
Quoted 8 lines
Because kmalloc()/kfree() and static are mixed together,
dealloc_on_cancelcondition is required.
As you pointed out in the previous email, kmalloc()/kfree() has the burden of memory allocation and deallocation, and static has the disadvantage of increasing
the code size. Both have their pros and cons.
Among these, I proposed unifying them into kmalloc()/kfree() methods and removing thedealloc_on_cancel
Thank you for kindly clarifying this, JeaJoon!
Quoted 9 lines
Quoted 8 lines
Meanwhile I find the feature might look complicated, or not well documented. Specifically,
dealloc_on_cancelshould be set on only dynamic-allocateddamon_call_controlobject, but that is not well documented on the kernel-doc comments. Are you saying removing it is an advantage because it makes reading code easier? If that's the case, how about improving the documentation?
Quoted 7 lines
dealloc_on_cancelis only used inmm/damon/sysfs.c. Here, since kmalloc thedamon_call_controlstructure, the dealloc_on_cancel=true condition is required. If you want to keep the current code as is and only change the comment, it would be clearer to comment it out like this:De-allocate when canceled.
--> To performkfree()if allocated withkmalloc()when canceled.
Sounds good. Would you mind sending a patch for that?
Thanks,
SJ
[...]
Quoted 37 lines · SeongJae Park
Quoted 3 lines · On Thu, 11 Dec 2025 12:29:59 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 2 lines · SeongJae Park
On Tue, 9 Dec 2025 21:20:42 +0900 JaeJoon Jung <[email protected]> wrote:
[...]
Quoted 5 lines
I suggested removing the
dealloc_on_cancelcondition from the patch. When using thedamon_call_controlstructure, if you only use kmalloc()/kfree() when canceled withoutdealloc_on_cancel. However, currently, in other modules, since it is static structdamon_call_control{ ... },kfree()is not allowed, sodealloc_on_cancelcondition is additionally required.Quoted 8 lines
Because kmalloc()/kfree() and static are mixed together,
dealloc_on_cancelcondition is required.
As you pointed out in the previous email, kmalloc()/kfree() has the burden of memory allocation and deallocation, and static has the disadvantage of increasing
the code size. Both have their pros and cons.
Among these, I proposed unifying them into kmalloc()/kfree() methods and removing thedealloc_on_cancelThank you for kindly clarifying this, JeaJoon!
Quoted 14 lines
Quoted 8 lines
Meanwhile I find the feature might look complicated, or not well documented. Specifically,
dealloc_on_cancelshould be set on only dynamic-allocateddamon_call_controlobject, but that is not well documented on the kernel-doc comments. Are you saying removing it is an advantage because it makes reading code easier? If that's the case, how about improving the documentation?
dealloc_on_cancelis only used inmm/damon/sysfs.c. Here, since kmalloc thedamon_call_controlstructure, the dealloc_on_cancel=true condition is required. If you want to keep the current code as is and only change the comment, it would be clearer to comment it out like this:
Quoted 4 lines
Quoted 2 lines
De-allocate when canceled.
--> To performkfree()if allocated withkmalloc()when canceled.Sounds good. Would you mind sending a patch for that?
Would you like me to send you the patch with only the comments corrected?
Thanks,
JaeJoon
Quoted 6 lines
Thanks,
SJ[...]
SeongJae Park
maintainerQuoted 1 line · On Thu, 11 Dec 2025 16:38:46 +0900 JaeJoon Jung <[email protected]> wrote:
On Thu, 11 Dec 2025 at 15:04, SeongJae Park <[email protected]> wrote:
[...]
Quoted 3 lines
Quoted 1 line
Sounds good. Would you mind sending a patch for that?
Would you like me to send you the patch with only the comments corrected?
I was just asking your preferrence, and seems you have no strong one. I will do that on my own unless you mind.
Thanks,
SJ
[...]
Quoted 11 lines · SeongJae Park
Quoted 1 line · On Thu, 11 Dec 2025 16:38:46 +0900 JaeJoon Jung <[email protected]> wrote:
On Thu, 11 Dec 2025 at 15:04, SeongJae Park <[email protected]> wrote:
[...]
Quoted 3 lines
Quoted 1 line
Sounds good. Would you mind sending a patch for that?
Would you like me to send you the patch with only the comments corrected?
I was just asking your preferrence, and seems you have no strong one. I will do that on my own unless you mind.
I have been argued the following so far:
"I proposed unifying them into kmalloc()/kfree() methods and removing the dealloc_on_cancel."
I apologize for not being able to respond promptly and smoothly this week due to a busy my external lecture schedule.
Thanks,
JaeJoon
Quoted 6 lines
Thanks,
SJ[...]
SeongJae Park
maintainerQuoted 8 lines · On Thu, 11 Dec 2025 17:13:09 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 7 lines · SeongJae Park
Quoted 1 line · On Thu, 11 Dec 2025 16:38:46 +0900 JaeJoon Jung <[email protected]> wrote:
On Thu, 11 Dec 2025 at 15:04, SeongJae Park <[email protected]> wrote:
[...]
Quoted 2 lines
Quoted 1 line
Sounds good. Would you mind sending a patch for that?
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Would you like me to send you the patch with only the comments corrected?
I was just asking your preferrence, and seems you have no strong one. I will do that on my own unless you mind.
I have been argued the following so far:
"I proposed unifying them into kmalloc()/kfree() methods and removing thedealloc_on_cancel."
Based on your previous email [1], I understand you argue this patch is better to be merged as-is, though you agree it has the pros and the cons. And therefore you are letting me to make the final decision.
I was implicitly saying I show the cons more than the pros, so don't want to merge this as is. Sorry if it was too implicit. Hope now my opinion is clearly reached out to you.
I also suggested making the comment better, and you agreed it but I understand you are not interested in making that change. I will do it.
So, all is well and no more action from your side is required.
Quoted 2 lines
I apologize for not being able to respond promptly and smoothly this week due to a busy my external lecture schedule.
No worry, take your time.
[1] https://lore.kernel.org/CAHOvCC6bJLf=LuBpm90n5poz8Sqhz9wjXtk5ypbPwkvhb1ptag@mail.gmail.com
Thanks,
SJ
[...]
Quoted 16 lines · SeongJae Park
Quoted 13 lines · On Thu, 11 Dec 2025 17:13:09 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 11 lines · SeongJae Park
Quoted 1 line · On Thu, 11 Dec 2025 16:38:46 +0900 JaeJoon Jung <[email protected]> wrote:
On Thu, 11 Dec 2025 at 15:04, SeongJae Park <[email protected]> wrote:
[...]
Quoted 3 lines
Quoted 1 line
Sounds good. Would you mind sending a patch for that?
Would you like me to send you the patch with only the comments corrected?
I was just asking your preferrence, and seems you have no strong one. I will do that on my own unless you mind.
Quoted 7 lines
Quoted 3 lines
I have been argued the following so far:
"I proposed unifying them into kmalloc()/kfree() methods and removing thedealloc_on_cancel."Based on your previous email [1], I understand you argue this patch is better to be merged as-is, though you agree it has the pros and the cons. And therefore you are letting me to make the final decision.
There are pros and cons to both, so I agree with your opinion.
Quoted 7 lines
I was implicitly saying I show the cons more than the pros, so don't want to merge this as is. Sorry if it was too implicit. Hope now my opinion is clearly reached out to you.
I also suggested making the comment better, and you agreed it but I understand you are not interested in making that change. I will do it.
I can also send you a patch for the comment.
But, I've been looking at the documents you've been working on lately. It would be nice if the documentation could continue to be improved on your end, so that there is no confusion.
Quoted 7 lines
So, all is well and no more action from your side is required.
Quoted 2 lines
I apologize for not being able to respond promptly and smoothly this week due to a busy my external lecture schedule.
No worry, take your time.
Thanks,
JaeJoon
Quoted 8 lines
[1] https://lore.kernel.org/CAHOvCC6bJLf=LuBpm90n5poz8Sqhz9wjXtk5ypbPwkvhb1ptag@mail.gmail.com
Thanks,
SJ[...]