Skip to contentSkip to conversation

mm/damon: validate min_region_size to be power of 2

PATCH Applied Liew Rui Yan 3 participants 8 messages May 1 – May 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover mm/damon: validate min_region_size to be power of 2

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_LRU_SORT or DAMON_RECLAIM, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Solution
========
Add an early validation in damon_lru_sort_apply_parameters() and damon_reclaim_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

Patch 1 fixes the issue for DAMON_LRU_SORT.
Patch 2 fixes the issue for DAMON_RECLAIM.

Changes from v4
(https://lore.kernel.org/[email protected])
- Clarify this is only a minor user experience improvement.
- Remove Fixes: and Cc: stable tags.
Changes from v3
(https://lore.kernel.org/[email protected])
- Improve commit message: clarify "unexpected termination".
- Add detailed User Impact with reason why kdamond cannot be restarted. Changes from v2
(https://lore.kernel.org/[email protected])
- Split the patch into two per-module patches.
- Add Fixes: and Cc: stable tags.
- Elaborate user impact and reproduction steps.
Changes from v1
(https://lore.kernel.org/[email protected])
- Fix memory leak issue.
Changes from first attempt
(https://lore.kernel.org/[email protected])
- Renamed the subject.
- Validate min_region_sz rather than addr_unit.

Liew Rui Yan (2):
mm/damon/lru_sort: validate min_region_size to be power of 2 mm/damon/reclaim: validate min_region_size to be power of 2

 mm/damon/lru_sort.c | 5 +++++
 mm/damon/reclaim.c  | 5 +++++
 2 files changed, 10 insertions(+)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 1/2 mm/damon/lru_sort: validate min_region_size to be power of 2

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_LRU_SORT, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Reproduction
============
1. Enable DAMON_LRU_SORT
2. Set addr_unit=3
3. Commit inputs via 'commit_inputs'
4. Observe kdamond termination

Solution
========
Add an early validation in damon_lru_sort_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

---
 mm/damon/lru_sort.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/damon/lru_sort.c +5 -0
index 554559d72976..3fd176ef9d9c 100644
@@ -294,6 +294,11 @@ 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)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	if (!damon_lru_sort_mon_attrs.sample_interval) {
 		err = -EINVAL;
 		goto out;
Signature · 1 line
2.53.0
#5 lore raw

PATCH v5 2/2 mm/damon/reclaim: validate min_region_size to be power of 2

In reply to Liew Rui Yan

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_RECLAIM, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Reproduction
============
1. Enable DAMON_RECLAIM
2. Set addr_unit=3
3. Commit inputs via 'commit_inputs'
4. Observe kdamond termination

Solution
========
Add an early validation in damon_reclaim_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

---
 mm/damon/reclaim.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/damon/reclaim.c +5 -0
index 86da14778658..2747eef5919d 100644
@@ -204,6 +204,11 @@ 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)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	if (!damon_reclaim_mon_attrs.aggr_interval) {
 		err = -EINVAL;
 		goto out;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v5 1/2] mm/damon/lru_sort: validate min_region_size to be power of 2

In reply to Liew Rui Yan
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition exists between the validation of…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition exists between the validation of damon_lru_sort_mon_attrs.sample_interval and its subsequent use, leading to a division-by-zero kernel panic. --

commit 27c4642b5b420a4c1731c9385273b591e05b5b4c
Author: Liew Rui Yan <[email protected]>

mm/damon/lru_sort: validate min_region_size to be power of 2

This commit adds early validation for the min_region_sz parameter to ensure it is a power of 2. It prevents kdamond from incorrectly terminating with the maybe_corrupted flag when a user simply sets an invalid addr_unit.

Quoted 5 lines
mm/damon/lru_sort.c +0 -0
index 2eb559d913b62..eca88ed941b32 100644

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 294

Quoted 9 lines
mm/damon/lru_sort.c +5 -0
@@ -286,6 +286,11 @@ 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)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	if (!damon_lru_sort_mon_attrs.sample_interval) {

This isn't a regression introduced by this patch, but can a time-of-check to time-of-use (TOCTOU) race condition happen here?

Because damon_lru_sort_mon_attrs is exposed as a module parameter, it can be modified concurrently. If a user concurrently writes 0 to sample_interval immediately after this check passes, the local attrs struct would copy the new zero value:

    attrs = damon_lru_sort_mon_attrs;

Later in the function, damon_max_nr_accesses(&attrs) evaluates attrs.aggr_interval / attrs.sample_interval. Since sample_interval would be 0, could this trigger a divide-by-zero exception?

Quoted 2 lines
mm/damon/lru_sort.c +0 -0
 		err = -EINVAL;
 		goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH v5 1/2] mm/damon/lru_sort: validate min_region_size to be power of 2

Quoted 21 lines · On Fri, 01 May 2026 02:16:43 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition exists between the validation of damon_lru_sort_mon_attrs.sample_interval and its subsequent use, leading to a division-by-zero kernel panic. --

commit 27c4642b5b420a4c1731c9385273b591e05b5b4c
Author: Liew Rui Yan <[email protected]>

mm/damon/lru_sort: validate min_region_size to be power of 2

This commit adds early validation for the min_region_sz parameter to ensure it is a power of 2. It prevents kdamond from incorrectly terminating with the maybe_corrupted flag when a user simply sets an invalid addr_unit.

Quoted 8 lines
mm/damon/lru_sort.c +0 -0
index 2eb559d913b62..eca88ed941b32 100644
@@ -286,6 +286,11 @@ 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);
 
Quoted 8 lines

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 302

Quoted 6 lines
mm/damon/lru_sort.c +5 -0
+	if (!is_power_of_2(param_ctx->min_region_sz)) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	if (!damon_lru_sort_mon_attrs.sample_interval) {

This isn't a regression introduced by this patch,

So no blocker for this patch.

Quoted 8 lines

but can a time-of-check
to time-of-use (TOCTOU) race condition happen here?

Because damon_lru_sort_mon_attrs is exposed as a module parameter, it can be modified concurrently. If a user concurrently writes 0 to sample_interval immediately after this check passes, the local attrs struct would copy the new zero value:

Quoted 5 lines
    attrs = damon_lru_sort_mon_attrs;

Later in the function, damon_max_nr_accesses(&attrs) evaluates attrs.aggr_interval / attrs.sample_interval. Since sample_interval would be 0, could this trigger a divide-by-zero exception?

This cannot happen, as previously explained [1] to a similar question.

Seems Sashiko is not using mm-new as the baseline of DAMON patches, and therefore does not know the fact we now doing this in synchronous manner.

[1] https://lore.kernel.org/[email protected]

Thanks,
SJ

[...]

maintainer Reviewed-by on v5 1/2
#4 lore raw

Re: [PATCH v5 1/2] mm/damon/lru_sort: validate min_region_size to be power of 2

In reply to Liew Rui Yan
Quoted 19 lines · On Fri, 1 May 2026 09:37:49 +0800 Liew Rui Yan <[email protected]>…

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_LRU_SORT, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Reproduction
============
1. Enable DAMON_LRU_SORT
2. Set addr_unit=3
3. Commit inputs via 'commit_inputs'
4. Observe kdamond termination

Quoted 7 lines

Solution
========
Add an early validation in damon_lru_sort_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

Thanks,
SJ

[...]

maintainer Reviewed-by on v5 2/2
#6 lore raw

Re: [PATCH v5 2/2] mm/damon/reclaim: validate min_region_size to be power of 2

In reply to Liew Rui Yan
Quoted 19 lines · On Fri, 1 May 2026 09:37:50 +0800 Liew Rui Yan <[email protected]>…

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_RECLAIM, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Reproduction
============
1. Enable DAMON_RECLAIM
2. Set addr_unit=3
3. Commit inputs via 'commit_inputs'
4. Observe kdamond termination

Quoted 5 lines

Solution
========
Add an early validation in damon_reclaim_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

Looks good to me.

Quoted 2 lines

Thanks,
SJ

[...]

maintainer
#7 lore raw
In reply to Liew Rui Yan

Hi Andrew,

Quoted 12 lines · On Fri, 1 May 2026 09:37:48 +0800 Liew Rui Yan <[email protected]>…

Problem
=======
When a user sets an invalid 'addr_unit' (e.g., 3) via DAMON_LRU_SORT or DAMON_RECLAIM, 'min_region_sz' becomes a non-power-of-2 value. While damon_commit_ctx() correctly detects this and returns -EINVAL, it sets the 'maybe_corrupted' flag during this process.

This flag causes the running kdamond to terminate. While the termination is a safety measure, it is suboptimal in this case because the error is just a simple invalid input from the user, which shouldn't neccessitate stopping the kdamond.

Quoted 6 lines

Solution
========
Add an early validation in damon_lru_sort_apply_parameters() and damon_reclaim_apply_parameters() to check 'min_region_sz' before any state change occurs. If it is non-power-of-2, return -EINVAL immediately, preventing 'maybe_corrupted' from being set.

I think this series is good to go in my opinion. Could you please add this series into mm.git?

Thanks,
SJ

[...]