Skip to contentSkip to conversation

mm/damon/core: reject empty access pattern intervals on scheme commit

PATCH In review Xiaolei Wang (Kaihong) 4 participants 7 messages Nov 3 – Nov 5

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

PATCH 1/1 mm/damon/core: reject empty access pattern intervals on scheme commit

DAMOS schemes accept access pattern bounds with min > max (e.g. min_sz_region > max_sz_region). No input path validates the bounds:
damon_new_scheme() stores the pattern as is, and the sysfs ul_range stores are plain kstrtoul().

At runtime __damos_valid_target() evaluates "min <= x <= max" for each of the three pattern dimensions. A reversed interval is an empty set, so every region fails the check and the scheme silently never matches
anything: nr_tried stays zero forever while the kdamond keeps running normally.

With such a reversed interval, damo starts normally (rc=0, kdamond state=on), but nr_tried and nr_applied stay zero forever and the target RSS does not drop.

The same holds for the watermarks thresholds, which must be monotonic (low <= mid <= high).

Validate the access pattern and watermarks at the beginning of damos_commit(), through which both new schemes (damo start) and online updates (damo tune) pass.

Fixes 1f366e421c8f ("mm/damon/core: implement DAMON-based Operation Schemes (DAMOS)")
Cc Liam R. Howlett <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
---
 mm/damon/core.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
mm/damon/core.c +19 -0
index eb1fcdabc007..865627bda240 100644
@@ -1566,10 +1566,29 @@ static struct damos *damon_nth_scheme(int n, struct damon_ctx *ctx)
 	return NULL;
 }
 
+static int damos_validate_scheme(struct damos *s)
+{
+	struct damos_access_pattern *p = &s->pattern;
+	struct damos_watermarks *wmarks = &s->wmarks;
+
+	if (p->min_sz_region > p->max_sz_region ||
+	    p->min_nr_accesses > p->max_nr_accesses ||
+	    p->min_age_region > p->max_age_region)
+		return -EINVAL;
+	if (wmarks->metric != DAMOS_WMARK_NONE &&
+	    (wmarks->low > wmarks->mid || wmarks->mid > wmarks->high))
+		return -EINVAL;
+	return 0;
+}
+
 static int damos_commit(struct damos *dst, struct damos *src)
 {
 	int err;
 
+	err = damos_validate_scheme(src);
+	if (err)
+		return err;
+
 	dst->pattern = src->pattern;
 	dst->action = src->action;
 	dst->apply_interval_us = src->apply_interval_us;
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm/damon: damos_validate_scheme() breaks DAMON sysfs.py selftest --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/damon: damos_validate_scheme() breaks DAMON sysfs.py selftest --

commit f1effb4d8401c0508f7cb04b14b9b55d0d6abd17
Author: Xiaolei Wang (Kaihong) <[email protected]>

mm/damon/core: reject empty access pattern intervals on scheme commit

This commit introduces damos_validate_scheme() and calls it at the start of damos_commit() to reject DAMOS schemes with reversed access pattern bounds (min > max) or non-monotonic watermark thresholds with -EINVAL.

Quoted 7 lines
mm/damon/core.c +0 -0
index d36823f4d0928..9b2b2a63a2576 100644
@@ -1561,10 +1561,29 @@ static struct damos *damon_nth_scheme(int n, struct damon_ctx *ctx)
 	return NULL;
 }

mm/damon/core.c static struct damos *damon_nth_scheme(int n, struct damon_ctx *ctx) · line 1572

Quoted 10 lines
mm/damon/core.c +9 -0
 
+static int damos_validate_scheme(struct damos *s)
+{
+	struct damos_access_pattern *p = &s->pattern;
+	struct damos_watermarks *wmarks = &s->wmarks;
+
+	if (p->min_sz_region > p->max_sz_region ||
+	    p->min_nr_accesses > p->max_nr_accesses ||
+	    p->min_age_region > p->max_age_region)
+		return -EINVAL;

[Severity: Medium]
Does this validation break the existing DAMON sysfs kselftest in tools/testing/selftests/damon/sysfs.py?

In tools/testing/selftests/damon/sysfs.py, main() constructs a non-default scheme for testing online parameter commitment with min_sz_region = 4096 and max_sz_region = 1024:

tools/testing/selftests/damon/sysfs.py:main()

            schemes=[_damon_sysfs.Damos(
                action='pageout',
                access_pattern=_damon_sysfs.DamosAccessPattern(
                    size=[4096, 2**10],
                    nr_accesses=[3, 317],
                    age=[5,71]),

When main() subsequently calls kdamonds.kdamonds[0].commit() and assert_ctxs_committed(kdamonds), writing commit to the kdamond state sysfs file invokes:

state_store()
-> damon_sysfs_handle_cmd()

    -> damon_sysfs_commit_input()
      -> damon_commit_ctx()
        -> __damon_commit_ctx()
          -> damon_commit_schemes()
            -> damos_commit()
              -> damos_validate_scheme()

Because p->min_sz_region (4096) > p->max_sz_region (1024), damos_validate_scheme() returns -EINVAL, causing kd.commit() in assert_ctxs_committed() to fail and exit with status 1:

tools/testing/selftests/damon/sysfs.py:assert_ctxs_committed()
if kd in kdamonds_paused_for_dump:
err = kd.commit()
if err is not None:
print('pause fail (%s)' % err) kdamonds.stop()
exit(1)

Should tools/testing/selftests/damon/sysfs.py also be updated to use a non-empty size range (for example, size=[4096, 2**20]) so the selftest continues to pass?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Xiaolei Wang (Kaihong)

Hi Xiaolei,

Quoted 5 lines · On Tue, 03 Nov 2026 18:54:18 +0800 Xiaolei Wang (Kaihong) wrote:

At runtime __damos_valid_target() evaluates "min <= x <= max" for each of the three pattern dimensions. A reversed interval is an empty set, so every region fails the check and the scheme silently never matches
anything: nr_tried stays zero forever while the kdamond keeps running normally.

My understanding is that DAMON just does what the user asked for here, so this looks more like an input validation improvement than a bug fix. Since previously accepted input would now fail with -EINVAL, maybe drop the Fixes: and Cc: stable tags, and mention the behavior change in the changelog?

Quoted 2 lines

The same holds for the watermarks thresholds, which must be monotonic (low <= mid <= high).

Not for all cases, I think. With mid > high (and low <= high), the scheme still works; mid just has no effect. The subject would also need to mention watermarks.

mm/damon/core.c static struct damos *damon_nth_scheme(int n, struct damon_ctx *ctx) · line 1577

Quoted 4 lines
mm/damon/core.c +4 -0
+	if (p->min_sz_region > p->max_sz_region ||
+	    p->min_nr_accesses > p->max_nr_accesses ||
+	    p->min_age_region > p->max_age_region)
+		return -EINVAL;

As Sashiko pointed out, this would break the DAMON sysfs.py selftest, which uses size=[4096, 2**10]. Maybe update it together, and also add a kunit case and a note in the DAMON usage document?

Thanks,
Aveline Noir

maintainer
#3 lore raw
In reply to Xiaolei Wang (Kaihong)
Quoted 15 lines · On Tue, 3 Nov 2026 18:54:18 +0800 "Xiaolei Wang (Kaihong)"…

DAMOS schemes accept access pattern bounds with min > max (e.g. min_sz_region > max_sz_region). No input path validates the bounds:
damon_new_scheme() stores the pattern as is, and the sysfs ul_range stores are plain kstrtoul().

At runtime __damos_valid_target() evaluates "min <= x <= max" for each of the three pattern dimensions. A reversed interval is an empty set, so every region fails the check and the scheme silently never matches
anything: nr_tried stays zero forever while the kdamond keeps running normally.

With such a reversed interval, damo starts normally (rc=0, kdamond state=on), but nr_tried and nr_applied stay zero forever and the target RSS does not drop.

Quoted 6 lines

The same holds for the watermarks thresholds, which must be monotonic (low <= mid <= high).

Validate the access pattern and watermarks at the beginning of damos_commit(), through which both new schemes (damo start) and online updates (damo tune) pass.

The documentation is not explicitly mentioning about the inputs (min > max and non-monotonic watermarks). But I was thinking it is not really difficult to infer this behavior based on the existing document.

If my understanding of the documentation is not wrong, I find no problem at accepting the inputs and just behave as documented. It might look silly, but who knows the users have a creative reason to do that? As long as it is not causing a harm, just supporting it is better for both users and code maintainability, in my humble opinion.

Pleae let me know if I am missing something.

Thanks,
SJ

[...]

#4 lore raw

Hi SeongJae and Aveline

 Thank you both for the careful review.

I appreciate the clarification that DAMON simply executes what

 the user configured, and that this is more of an input validation

improvement than a bug fix. Perhaps I was too aggressive in framing

it as a bug.

thanks

xiaolei

Quoted 40 lines · SJ Park
Quoted 15 lines · On Tue, 3 Nov 2026 18:54:18 +0800 "Xiaolei Wang (Kaihong)"…

DAMOS schemes accept access pattern bounds with min > max (e.g. min_sz_region > max_sz_region). No input path validates the bounds:
damon_new_scheme() stores the pattern as is, and the sysfs ul_range stores are plain kstrtoul().

At runtime __damos_valid_target() evaluates "min <= x <= max" for each of the three pattern dimensions. A reversed interval is an empty set, so every region fails the check and the scheme silently never matches
anything: nr_tried stays zero forever while the kdamond keeps running normally.

With such a reversed interval, damo starts normally (rc=0, kdamond state=on), but nr_tried and nr_applied stay zero forever and the target RSS does not drop.

Quoted 6 lines

The same holds for the watermarks thresholds, which must be monotonic (low <= mid <= high).

Validate the access pattern and watermarks at the beginning of damos_commit(), through which both new schemes (damo start) and online updates (damo tune) pass.

The documentation is not explicitly mentioning about the inputs (min > max and non-monotonic watermarks). But I was thinking it is not really difficult to infer this behavior based on the existing document.

If my understanding of the documentation is not wrong, I find no problem at accepting the inputs and just behave as documented. It might look silly, but who knows the users have a creative reason to do that? As long as it is not causing a harm, just supporting it is better for both users and code maintainability, in my humble opinion.

Pleae let me know if I am missing something.

Thanks,
SJ

[...]

maintainer
#5 lore raw
Quoted 11 lines · On Wed, 4 Nov 2026 09:17:24 +0800 Wang Xiaolei <[email protected]>…

Hi SeongJae and Aveline

 Thank you both for the careful review.

I appreciate the clarification that DAMON simply executes what

 the user configured, and that this is more of an input validation

improvement than a bug fix. Perhaps I was too aggressive in framing

it as a bug.

Thank you for clarifying. But my point is that this change might be just unnecessary behavior change rather than an improvement. What do you think?

Thanks,
SJ

[...]

#6 lore raw
Quoted 8 lines · SJ Park
Quoted 6 lines · On Wed, 4 Nov 2026 09:17:24 +0800 Wang Xiaolei <[email protected]>…

Hi SeongJae and Aveline

 Thank you both for the careful review.

I appreciate the clarification that DAMON simply executes what

Quoted 7 lines
Quoted 5 lines

 the user configured, and that this is more of an input validation

improvement than a bug fix. Perhaps I was too aggressive in framing

it as a bug.

Thank you for clarifying. But my point is that this change might be just unnecessary behavior change rather than an improvement. What do you think?

Hi SeongJae

Thank you again for the clear explanation.

I agree with you . My earlier framing was wrong;

I won't push this patch further. Let me focus on the

other items that are genuine bugs instead.

thanks

xiaolei

Quoted 6 lines

Thanks,
SJ

[...]