mm/damon: validate min_region_size to be power of 2
Subsystems (2)
PATCH 1/1 mm/damon: validate min_region_size to be power of 2
Problem
=======damon_commit_ctx() checks if 'min_region_sz' is a power of 2. But if it is not, kdamond will terminate unexpectedly due to the user's invalid input.
Solution
========
Add the same check to damon_{lru_sort, reclaim}_apply_parameters(), but return -EINVAL immediately. This is to prevent user's invalid input causing kdamond to terminate unexpectedly.
--- mm/damon/lru_sort.c | 3 +++ mm/damon/reclaim.c | 3 +++ 2 files changed, 6 insertions(+)
mm/damon/lru_sort.c +3 -0
@@ -294,6 +294,9 @@ static int damon_lru_sort_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +if (!damon_lru_sort_mon_attrs.sample_interval) { err = -EINVAL ; goto out;
mm/damon/reclaim.c +3 -0
@@ -204,6 +204,9 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;
Signature · 1 line
2.53.0
Re: (sashiko review) [PATCH] mm/damon: validate min_region_size to be power of 2
Forwarding Sashiko review for doing discussions via mails.
# review url: https://sashiko.dev/#/patchset/[email protected] # start of sashiko.dev inline review commit 883916ab04d9fc6b9074dcd36200cd14402cc8fb Author: Liew Rui Yan <[email protected]> mm/damon: validate min_region_size to be power of 2
This patch adds checks to ensure that min_region_sz is a power of 2 in damon_lru_sort_apply_parameters() and damon_reclaim_apply_parameters(). If the value is not a power of 2, it returns -EINVAL immediately to prevent kdamond from terminating unexpectedly.
mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 299
Quoted 11 lines
mm/damon/lru_sort.c +3
-0@@ -294,6 +294,9 @@ static int damon_lru_sort_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +
Does this code leak the allocated param_ctx?
Looking earlier in damon_lru_sort_apply_parameters(), param_ctx is allocated via damon_modules_new_paddr_ctx_target(). The existing error paths below this check use a goto out; to ensure damon_destroy_ctx(param_ctx) is called.
By returning directly here, does it skip freeing param_ctx?
Quoted 3 lines
mm/damon/lru_sort.c +0
-0if (!damon_lru_sort_mon_attrs.sample_interval) { err = -EINVAL ; goto out;
[ ... ]
mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 209
Quoted 11 lines
mm/damon/reclaim.c +3
-0@@ -204,6 +204,9 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +
Can this result in a similar leak in the reclaim path?
Like the previous file, param_ctx is allocated earlier in this function and there is an out label that handles cleaning up the context. Could this be changed to set err = -EINVAL; and goto out; instead of returning directly?
mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 212
Quoted 3 lines
mm/damon/reclaim.c +0
-0if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;
# end of sashiko.dev inline review # review url: https://sashiko.dev/#/patchset/[email protected] # # hkml [1] generated a draft of this mail. It can be regenerated # using below command: # # hkml patch sashiko_dev --for_forwarding \ # [email protected] # # [1] https://github.com/sjp38/hackermail
Re: (sashiko review) [PATCH] mm/damon: validate min_region_size to be power of 2
Quoted 15 lines
mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 299
Quoted 12 lines
[...]
mm/damon/lru_sort.c +3
-0@@ -294,6 +294,9 @@ static int damon_lru_sort_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +Does this code leak the allocated
param_ctx?
Quoted 6 lines
Looking earlier in
damon_lru_sort_apply_parameters(),param_ctxis allocated viadamon_modules_new_paddr_ctx_target(). The existing error paths below this check use a goto out; to ensuredamon_destroy_ctx(param_ctx)is called.By returning directly here, does it skip freeing
param_ctx?
You are absolutely right. I missed the cleanup path here.
I should have used 'err = -EINVAL; goto out;' to ensure 'param_ctx' is properly freed, similar to the existing error handling below.
Quoted 20 lines
Quoted 3 lines
mm/damon/lru_sort.c +0
-0if (!damon_lru_sort_mon_attrs.sample_interval) { err = -EINVAL ; goto out;[ ... ]
mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 209
Quoted 11 lines
mm/damon/reclaim.c +3
-0@@ -204,6 +204,9 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); +if (!is_power_of_2 (param_ctx->min_region_sz)) +return -EINVAL ; +Can this result in a similar leak in the reclaim path?
Quoted 3 lines
Like the previous file,
param_ctxis allocated earlier in this function and there is an out label that handles cleaning up the context. Could this be changed to set err =-EINVAL; and goto out; instead of returning directly?
I will fix this in the next version. Thank you for the catch!
Quoted 6 lines
mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 212
Quoted 4 lines
mm/damon/reclaim.c +0
-0if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;
Quoted 10 lines
# end of sashiko.dev inline review # review url: https://sashiko.dev/#/patchset/[email protected] # # hkml [1] generated a draft of this mail. It can be regenerated # using below command: # # hkml patch sashiko_dev --for_forwarding \ # [email protected] # # [1] https://github.com/sjp38/hackermail
Best regards,
Rui Yan
SeongJae Park
maintainerQuoted 11 lines · On Tue, 31 Mar 2026 15:32:31 +0800 Liew Rui Yan <[email protected]>…
Problem
=======damon_commit_ctx()checks if 'min_region_sz' is a power of 2. But if it is not, kdamond will terminate unexpectedly due to the user's invalid input.Solution
========
Add the same check to damon_{lru_sort, reclaim}_apply_parameters(), but return-EINVALimmediately. This is to prevent user's invalid input causing kdamond to terminate unexpectedly.
Seems sashiko found an issue, and you are preparing the next revision, right? Assuming so, I will skip code review of this version.
Quoted 3 lines
Signed-off-by Liew Rui Yan <[email protected]>---
Weren't there previous versions of this patch? Could you please add changelog here, from the next version?
Thanks,
SJ
[...]
Quoted 5 lines · On Tue, 31 Mar 2026 18:01:56 -0700 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Tue, 31 Mar 2026 15:32:31 +0800 Liew Rui Yan <[email protected]>…
[...]
Seems sashiko found an issue, and you are preparing the next revision, right? Assuming so, I will skip code review of this version.
Yes, I'm preparing the next revision.
Quoted 6 lines
Quoted 3 lines
Signed-off-by Liew Rui Yan <[email protected]>---
Weren't there previous versions of this patch? Could you please add changelog here, from the next version?
Thank you for reminding me. I will add a changelog at the comment area, and add this to the commit messages.
This patch is a follow-up to the discussion in [1], where we agreed that the validation should be done on min_region_sz rather than addr_unit.
[1] https://lore.kernel.org/[email protected]
Best regards,
Rui Yan
SeongJae Park
maintainerQuoted 17 lines · On Wed, 1 Apr 2026 16:40:45 +0800 Liew Rui Yan <[email protected]>…
Quoted 5 lines · On Tue, 31 Mar 2026 18:01:56 -0700 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Tue, 31 Mar 2026 15:32:31 +0800 Liew Rui Yan <[email protected]>…
[...]
Seems sashiko found an issue, and you are preparing the next revision, right? Assuming so, I will skip code review of this version.
Yes, I'm preparing the next revision.
Quoted 6 lines
Quoted 3 lines
Signed-off-by Liew Rui Yan <[email protected]>---
Weren't there previous versions of this patch? Could you please add changelog here, from the next version?
Quoted 8 lines
Thank you for reminding me. I will add a changelog at the comment area, and add this to the commit messages.
This patch is a follow-up to the discussion in [1], where we agreed that the validation should be done on
min_region_szrather thanaddr_unit.
Sounds good. I appreciate your continued grateful contributions to DAMON, Liew.
Thanks,
SJ
[...]