Skip to contentSkip to conversation

mm/damon/core: reject zero sample_interval

PATCH In review [email protected] 2 participants 3 messages Jul 22 – Jul 23

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

PATCH 1/1 mm/damon/core: reject zero sample_interval

From: liyouhong <[email protected]>

damon_set_attrs() allows sample_interval == 0. Sysfs likewise accepts sample_us=0. kdamond_fn() then calls kdamond_usleep(0) every loop iteration, so kdamond busy-spins and can peg a CPU.

With a zero sampling interval the access-rate accounting also diverges:
nr_accesses_bp no longer matches nr_accesses * 10000, and kdamond_reset_aggregated() hits:

WARNING: invalid nr_accesses_bp at reset: ...

Reject sample_interval == 0 in damon_set_attrs(), which covers sysfs commit and other callers. damon_lru_sort already refused a zero sample_interval; align the core validation with that.

Reproduce:
echo 0 > .../intervals/sample_us
echo on > .../kdamonds/0/state

  # kdamond ~100% CPU; may WARN in kdamond_reset_aggregated()
Fixes 4472edf63d66 ("mm/damon/core: use number of passed access sampling as a timer")
Assisted-by Cursor Grok 4.5

---

mm/damon/core.c +5 -2
index 7e4b9affc5b0..a6d54ec8c4be 100644
@@ -911,6 +911,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;
 
@@ -3770,8 +3772,9 @@ void damon_update_region_access_rate(struct damon_region *r, bool accessed,
 	unsigned int len_window = 1;
 
 	/*
-	 * sample_interval can be zero, but cannot be larger than
-	 * aggr_interval, owing to validation of damon_set_attrs().
+	 * sample_interval cannot be zero or larger than aggr_interval,
+	 * owing to validation of damon_set_attrs().  Keep the zero check
+	 * below as defense in depth.
 	 */
 	if (attrs->sample_interval)
 		len_window = damon_max_nr_accesses(attrs);
maintainer
#1 lore raw

Hello liyouhong,

Thank you for sharing this patch!

Quoted 11 lines · On Wed, 22 Jul 2026 17:43:04 +0800 [email protected] wrote:

From: liyouhong <[email protected]>

damon_set_attrs() allows sample_interval == 0. Sysfs likewise accepts sample_us=0. kdamond_fn() then calls kdamond_usleep(0) every loop iteration, so kdamond busy-spins and can peg a CPU.

With a zero sampling interval the access-rate accounting also diverges:
nr_accesses_bp no longer matches nr_accesses * 10000, and kdamond_reset_aggregated() hits:

WARNING: invalid nr_accesses_bp at reset: ...

The warning has removed by nr_accesses_bp optimization patch series, which is currently queued for 7.3-rc1. So the warning will not be triggered on near future mainline.

Quoted 4 lines

Reject sample_interval == 0 in damon_set_attrs(), which covers sysfs commit and other callers. damon_lru_sort already refused a zero sample_interval; align the core validation with that.

This is a sort of behavioral change. I also feel this adds another corner case that makes my head complicated. There are a few logic written under the assumption of zero intervals. Having inconsistencies between allowed input and handling logic will make it difficult to maintain in long term. Can't we keep supporting zero sample interval and fix the warning or the real bug instead?

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

Thanks,
SJ

[...]

author
#2 lore raw
Quoted 27 lines · SJ Park

Hello liyouhong,

Thank you for sharing this patch!

Quoted 11 lines · On Wed, 22 Jul 2026 17:43:04 +0800 [email protected] wrote:

From: liyouhong <[email protected]>

damon_set_attrs() allows sample_interval == 0. Sysfs likewise accepts sample_us=0. kdamond_fn() then calls kdamond_usleep(0) every loop iteration, so kdamond busy-spins and can peg a CPU.

With a zero sampling interval the access-rate accounting also diverges:
nr_accesses_bp no longer matches nr_accesses * 10000, and kdamond_reset_aggregated() hits:

WARNING: invalid nr_accesses_bp at reset: ...

The warning has removed by nr_accesses_bp optimization patch series, which is currently queued for 7.3-rc1. So the warning will not be triggered on near future mainline.

Quoted 4 lines

Reject sample_interval == 0 in damon_set_attrs(), which covers sysfs commit and other callers. damon_lru_sort already refused a zero sample_interval; align the core validation with that.

Quoted 6 lines

This is a sort of behavioral change. I also feel this adds another corner case that makes my head complicated. There are a few logic written under the assumption of zero intervals. Having inconsistencies between allowed input and handling logic will make it difficult to maintain in long term. Can't we keep supporting zero sample interval and fix the warning or the real bug instead?

Hi SJ,

Thanks for the explanation. I agree rejecting zero sample_interval is the wrong approach, especially since the warning will be gone with your queued series.

Thanks,
liyouhong