Skip to contentSkip to conversation

mm/damon: reject zero sampling interval

PATCH In review Karthikeyan KS 4 participants 9 messages Sep 17 – Sep 18

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

PATCH 1/1 mm/damon: reject zero sampling interval

damon_set_attrs() already rejects inverted intervals (sample_interval > aggr_interval) and invalid region counts, but it accepts sample_interval == 0.

A zero sampling interval is not a valid monitoring period. Sysfs "on" and "commit" both install attrs via damon_set_attrs(), so kdamond then calls kdamond_usleep(0) and busy-spins.

Return -EINVAL if sample_interval is zero.

---

Hello,

I recently started reading DAMON and just found this one. This change only tightens damon_set_attrs() validation, in the same place as the existing sample > aggr and min_nr_regions checks. It is not a crash, UAF, or race.

sample_us=0 is accepted today, so kdamond_usleep(0) busy-spins (about one core). Sysfs "on" and "commit" both go through damon_set_attrs(); one check covers both.

Checked on v7.3-rc3 (238650ef6c7c):

unpatched: echo on with sample_us=0 succeeds; kdamond uses ~97% of
one core. echo commit of 0 onto a running kdamond also succeeds and it starts spinning.

patched: both on and commit return -EINVAL. Valid sample_us=5000
still starts. After a rejected commit the same pid stays on and does not spin.

aggr_us=0 with a positive sample_us is already -EINVAL from the ordering check.

Thanks,

 mm/damon/core.c             |  2 ++
 mm/damon/tests/core-kunit.h | 13 +++++++++++++
 2 files changed, 15 insertions(+)
mm/damon/core.c +2 -0
index 644daf5a1656..376c3e2cbe00 100644
@@ -1060,6 +1060,8 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
 		return -EINVAL;
 	if (attrs->min_nr_regions > attrs->max_nr_regions)
 		return -EINVAL;
+	if (!attrs->sample_interval)
+		return -EINVAL;
 	if (attrs->sample_interval > attrs->aggr_interval)
 		return -EINVAL;
 
mm/damon/tests/core-kunit.h +13 -0
index 4a536d41cdb2..ec8ef71956c8 100644
@@ -658,6 +658,19 @@ static void damon_test_set_attrs(struct kunit *test)
 	invalid_attrs.aggr_interval = 4999;
 	KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
 
+	invalid_attrs = valid_attrs;
+	invalid_attrs.sample_interval = 0;
+	KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
+	invalid_attrs = valid_attrs;
+	invalid_attrs.aggr_interval = 0;
+	KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
+	invalid_attrs = valid_attrs;
+	invalid_attrs.sample_interval = 0;
+	invalid_attrs.aggr_interval = 0;
+	KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
 	damon_destroy_ctx(c);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] mm: obsolete zero-interval fallbacks left in core.c --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] mm: obsolete zero-interval fallbacks left in core.c
--

commit 3fc9c7e0ec1f59dfad336e3842553089ded8b3b8
Author: Karthikeyan KS <[email protected]>

mm/damon: reject zero sampling interval

damon_set_attrs() already rejects inverted intervals, but it accepts sample_interval == 0. A zero sampling interval is not a valid monitoring period and causes busy-spinning in kdamond. Return -EINVAL if sample_interval is zero.

mm/damon/core.c int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs) · line 1139

Quoted 10 lines
mm/damon/core.c +2 -0
index 444c97c184027..7faa92106cf1d 100644
@@ -1135,6 +1135,8 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
 		return -EINVAL;
 	if (attrs->min_nr_regions > attrs->max_nr_regions)
 		return -EINVAL;
+	if (!attrs->sample_interval)
+		return -EINVAL;

[Severity: Low]
Since attrs->sample_interval and attrs->aggr_interval are now explicitly rejected if zero, do we still need the zero-interval fallbacks scattered throughout core.c?

For example, earlier in damon_set_attrs() we have:

	unsigned long sample_interval = attrs->sample_interval ?
		attrs->sample_interval : 1;

And similarly in kdamond_init_ctx() and kdamond_fn():

	sample_interval = ctx->attrs.sample_interval ?
		ctx->attrs.sample_interval : 1;

There is also a defensive check in damon_update_monitoring_results() that looks obsolete now:

	if (!old_attrs->sample_interval || !old_attrs->aggr_interval ||
			!new_attrs->sample_interval ||
			!new_attrs->aggr_interval)
		return;

Should we clean up these zero-interval fallbacks since they appear to be structurally unreachable after this change?

Quoted 2 lines
mm/damon/core.c +0 -0
 	if (attrs->sample_interval > attrs->aggr_interval)
 		return -EINVAL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Karthikeyan KS

Hi,

Quoted 10 lines · Karthikeyan KS

damon_set_attrs() already rejects inverted intervals (sample_interval > aggr_interval) and invalid region counts, but it accepts sample_interval == 0.

A zero sampling interval is not a valid monitoring period. Sysfs "on" and "commit" both install attrs via damon_set_attrs(), so kdamond then calls kdamond_usleep(0) and busy-spins.

Return -EINVAL if sample_interval is zero.

I think SJ already commented on a similar patch. please refer to [1] If this happens a lot, maybe should we document or add a comment about this?

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

Quoted 71 lines

---

Hello,

I recently started reading DAMON and just found this one. This change only tightens damon_set_attrs() validation, in the same place as the existing sample > aggr and min_nr_regions checks. It is not a crash, UAF, or race.

sample_us=0 is accepted today, so kdamond_usleep(0) busy-spins (about one core). Sysfs "on" and "commit" both go through damon_set_attrs(); one check covers both.

Checked on v7.3-rc3 (238650ef6c7c):

unpatched: echo on with sample_us=0 succeeds; kdamond uses ~97% of
one core. echo commit of 0 onto a running kdamond also succeeds and it starts spinning.

patched: both on and commit return -EINVAL. Valid sample_us=5000
still starts. After a rejected commit the same pid stays on and does not spin.

aggr_us=0 with a positive sample_us is already -EINVAL from the ordering check.

Thanks,

 mm/damon/core.c             |  2 ++
 mm/damon/tests/core-kunit.h | 13 +++++++++++++
 2 files changed, 15 insertions(+)
mm/damon/core.c +2 -0
index 644daf5a1656..376c3e2cbe00 100644
@@ -1060,6 +1060,8 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
                return -EINVAL;
        if (attrs->min_nr_regions > attrs->max_nr_regions)
                return -EINVAL;
+       if (!attrs->sample_interval)
+               return -EINVAL;
        if (attrs->sample_interval > attrs->aggr_interval)
                return -EINVAL;
 
mm/damon/tests/core-kunit.h +13 -1
index 4a536d41cdb2..ec8ef71956c8 100644
@@ -658,6 +658,19 @@ static void damon_test_set_attrs(struct kunit *test)
        invalid_attrs.aggr_interval = 4999;
        KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
 
+       invalid_attrs = valid_attrs;
+       invalid_attrs.sample_interval = 0;
+       KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
+       invalid_attrs = valid_attrs;
+       invalid_attrs.aggr_interval = 0;
+       KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
+       invalid_attrs = valid_attrs;
+       invalid_attrs.sample_interval = 0;
+       invalid_attrs.aggr_interval = 0;
+       KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &invalid_attrs), -EINVAL);
+
        damon_destroy_ctx(c);
 }
 
--
Quoted 2 lines

2.34.1

Best regards,
Sang-Heon Jeon

#3 lore raw

Hi Sang-Heon, Hi SJ,

Quoted 3 lines

I think SJ already commented on a similar patch. please refer to [1]

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

Thanks, missed that thread. Same objection applies to mine.

Quoted 2 lines

Can't we keep supporting zero sample interval and fix the warning or the real bug instead?

I measured a few floors for kdamond_usleep():

  floor    kdamond CPU (3s)   vs sample_us=5000
  1us      84%                28x
  100us    42%                14x
  1000us   12%                ~3x
  5000us   ~2%                1x (matches control)

5000us is damon_new_ctx()'s own default, and at that floor the spin is just gone.

Would you rather we just document sample_us=0 as accepted/expected behavior, or clamp it to that default inside damon_set_attrs()?

Thanks,
Karthikeyan KS

maintainer
#4 lore raw

Hi Karthikeyan and Sang-Heon,

Quoted 5 lines · On Thu, 17 Sep 2026 18:00:13 +0000 Karthikeyan KS…

Hi Sang-Heon, Hi SJ,

Quoted 3 lines

I think SJ already commented on a similar patch. please refer to [1]

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

Thank you for pointing this out, Sang-Heon!

Quoted 14 lines

Thanks, missed that thread. Same objection applies to mine.

Quoted 2 lines

Can't we keep supporting zero sample interval and fix the warning or the real bug instead?

I measured a few floors for kdamond_usleep():

  floor    kdamond CPU (3s)   vs sample_us=5000
  1us      84%                28x
  100us    42%                14x
  1000us   12%                ~3x
  5000us   ~2%                1x (matches control)
Quoted 5 lines

5000us is damon_new_ctx()'s own default, and at that floor the spin is just gone.

Would you rather we just document sample_us=0 as accepted/expected behavior, or clamp it to that default inside damon_set_attrs()?

I'd prefer adding a comment saying zero sampling interval is accepted. I think damon_attr kernel-doc comment is a good place for adding that.

Thanks,
SJ

[...]

#5 lore raw

PATCH v2 1/1 mm/damon: document that a zero sample_interval is accepted

damon_set_attrs() accepts sample_interval == 0. This was reported as a bug in v1 of this patch (rejecting it in damon_set_attrs()). A similar patch was already declined for the same reason: a zero interval is intentionally supported [1].

Document the behavior instead of changing it.

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

---

Hi SJ,

Quoted 3 lines

I'd prefer adding a comment saying zero sampling interval is accepted. I think damon_attr kernel-doc comment is a good place for adding that.

Adding comments instead of code changes. Let me know if you'd rather the wording live somewhere else, or be phrased differently.

Changes from v1:
- Dropped the damon_set_attrs() rejection and the KUnit cases that tested it.
- Added a kernel-doc note on struct damon_attrs's @sample_interval instead.

Thanks,
Karthikeyan KS

 include/linux/damon.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/damon.h +2 -1
index 0c8b7ddef9ab..1a7ef416ec46 100644
@@ -787,7 +787,8 @@ struct damon_probe {
 /**
  * struct damon_attrs - Monitoring attributes for accuracy/overhead control.
  *
- * @sample_interval:		The time between access samplings.
+ * @sample_interval:		The time between access samplings.  Zero is
+ *				accepted.
  * @aggr_interval:		The time between monitor results aggregations.
  * @ops_update_interval:	The time between monitoring operations updates.
  * @intervals_goal:		Intervals auto-tuning goal.
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2] mm/damon: document that a zero sample_interval is accepted

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

damon_set_attrs() accepts sample_interval == 0. This was reported as a bug in v1 of this patch (rejecting it in damon_set_attrs()). A similar patch was already declined for the same reason: a zero interval is intentionally supported [1].

Document the behavior instead of changing it.

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

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v2 1/1
#7 lore raw

Re: [PATCH v2] mm/damon: document that a zero sample_interval is accepted

In reply to Karthikeyan KS

Hello Karthikeyan,

From the next time, please don't post a new version of a patch as a reply to the thread. Post it as a new thread instead. Also please add a links to the previous revisions in the changelog [1].

Quoted 8 lines · On Fri, 18 Sep 2026 13:13:01 +0000 Karthikeyan KS…

damon_set_attrs() accepts sample_interval == 0. This was reported as a bug in v1 of this patch (rejecting it in damon_set_attrs()). A similar patch was already declined for the same reason: a zero interval is intentionally supported [1].

Document the behavior instead of changing it.

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

The patch itself looks good to me.

Quoted 2 lines

This patch is applied to damon/next [2] tree. If this patch is not added to mm.git in short term (~1 week?), I will ask mm.git maintainer (Andrew Morton) to pick this. So, no action from your side is needed for now. If it seems I also forgot doing that or you cannot wait for my action, please feel free to directly ask that to Andrew.

Quoted 8 lines

---

Hi SJ,

Quoted 3 lines

I'd prefer adding a comment saying zero sampling interval is accepted. I think damon_attr kernel-doc comment is a good place for adding that.

Quoted 8 lines

Adding comments instead of code changes. Let me know if you'd rather the wording live somewhere else, or be phrased differently.

Changes from v1:
- Dropped the damon_set_attrs() rejection and the KUnit cases that tested it.
- Added a kernel-doc note on struct damon_attrs's @sample_interval instead.

Thank you for accepting my humble suggestion.

[1] https://docs.kernel.org/process/submitting-patches.html#commentary
[2] https://origin.kernel.org/doc/html/latest/mm/damon/maintainer-profile.html#scm-trees

Thanks,
SJ

[...]

#8 lore raw

Re: [PATCH v2] mm/damon: document that a zero sample_interval is accepted

Hi SJ, hi Sang-Heon,

Thanks -- appreciate the review, and the pointer to the July thread. Will post as a new thread with a link to the previous version next time.

Thanks,
Karthikeyan KS