Skip to contentSkip to conversation

mm/damon/sysfs: make next_update_jiffies per-kdamond

PATCH In review Karthikeyan KS 2 participants 4 messages Oct 17

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

PATCH 1/1 mm/damon/sysfs: make next_update_jiffies per-kdamond

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().

---
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(-)
mm/damon/sysfs.c +5 -6
index 160292f077dd..872b89f58bd1 100644
@@ -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;
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi Karthikeyan,

Quoted 3 lines · On Sat, 17 Oct 2026 06:31:52 +0000 Karthikeyan KS…

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().

Thank you for this patch. However, I think the same issue is already fixed by another patch [1]. Could you please check?

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

Thanks,
SJ

[...]

#2 lore raw

Hi SJ,

Yes, my patch is redundant. I should have checked your damon/next tree before sending it. Will do that before filing anything going forward.

Karthikeyan