[PATCH] mm/damon/core: reject zero sample_interval

[email protected]
Newsgroups dev.linux.lists.damon
Message-ID <[email protected]>
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
Signed-off-by: liyouhong <[email protected]>
---
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 7e4b9affc5b0..a6d54ec8c4be 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -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);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.