[PATCH] mm/damon/sysfs: make next_update_jiffies per-kdamond

From: Karthikeyan KS

Date: Sat Oct 17 2026 - 02:32:08 EST


damon_sysfs_next_update_jiffies was a single static variable shared by
all kdamonds, so with two or more kdamonds using refresh_ms their
threads race on it in damon_sysfs_repeat_call_fn().

Signed-off-by: Karthikeyan KS <karthiproffesional@xxxxxxxxx>
---
Testing notes for review:

- Found and confirmed with KCSAN.
- Repro setup: QEMU (x86_64, 2GB RAM, multiple vCPUs), two paddr
kdamonds, both refresh_ms non-zero. kdamond.0 watching [0, 1GB),
kdamond.1 watching [1GB, 2GB).
- On the unpatched kernel, KCSAN kept reporting the race on
damon_sysfs_next_update_jiffies, again and again.
- Ran the same setup after the fix, KCSAN still on, nothing reported.
- Tried the same with vaddr kdamonds too, could not repro there.

mm/damon/sysfs.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 160292f077dd..872b89f58bd1 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1789,6 +1789,7 @@ struct damon_sysfs_kdamond {
struct damon_sysfs_contexts *contexts;
struct damon_ctx *damon_ctx;
unsigned int refresh_ms;
+ unsigned long damon_sysfs_next_update_jiffies;
};

static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void)
@@ -2215,18 +2216,16 @@ static struct damon_ctx *damon_sysfs_build_ctx(
return ctx;
}

-/* Tracks when the next periodic re-commit via repeat_call_fn() is due. */
-static unsigned long damon_sysfs_next_update_jiffies;
-
static int damon_sysfs_repeat_call_fn(void *data)
{
struct damon_sysfs_kdamond *sysfs_kdamond = data;

if (!sysfs_kdamond->refresh_ms)
return 0;
- if (time_before(jiffies, damon_sysfs_next_update_jiffies))
+ if (time_before(jiffies,
+ sysfs_kdamond->damon_sysfs_next_update_jiffies))
return 0;
- damon_sysfs_next_update_jiffies = jiffies +
+ sysfs_kdamond->damon_sysfs_next_update_jiffies = jiffies +
msecs_to_jiffies(sysfs_kdamond->refresh_ms);

if (!mutex_trylock(&damon_sysfs_lock))
@@ -2274,7 +2273,7 @@ static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
}
kdamond->damon_ctx = ctx;

- damon_sysfs_next_update_jiffies =
+ kdamond->damon_sysfs_next_update_jiffies =
jiffies + msecs_to_jiffies(kdamond->refresh_ms);

repeat_call_control->fn = damon_sysfs_repeat_call_fn;
--
2.43.0