Skip to contentSkip to conversation

mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms

RFC PATCH In review SJ Park 2 participants 11 messages Aug 28

Subsystems (4)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

cover mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms

DAMOS quota goals commit unit test is mistakenly not testing a test case that was designed to test. DAMOS quota goals and DAMON sysfs refresh_ms file have bugs that can produce non critical but still unexpected behaviors. Fix the bugs.

Patch 1 fixes the DAMOS quota goals commit unit test to cover a mistakenly uncovered case. Patches 2 and 3 fix the bugs in DAMOS PSI goal initialization and eligible_mem_bp online commit, respectively. Patch 4 fixes the bug in DAMON sysfs refresh_ms file handling.

Changes from RFC v1.1
- RFC v1.1: https://lore.kernel.org/[email protected]
- Fix quota goals commit unit test that was mistakenly missing a test case.
- Initialize last_psi_total as uninitialized at the beginning.
- Correct kunit test for changed last_psi_total initialization behavior. Changes from RFC
- RFC: https://lore.kernel.org/[email protected]
(Mistakenly missed RFC tag)
- Fix build error.
- Add eligible_mem_bp nid commit fix.

SJ Park (4):
mm/damon/tests/core-kunit: test committing psi goal to psi goal mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit mm/damon/sysfs: set next refresh jiffies per sysfs context

 mm/damon/core.c             | 13 ++++++++++++-
 mm/damon/sysfs.c            | 11 +++++------
 mm/damon/tests/core-kunit.h | 16 ++++++++++------
 3 files changed, 27 insertions(+), 13 deletions(-)

base-commit: 90e1725f175e19e191adc6e1b0f0317feeddd268

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH 1/4 mm/damon/tests/core-kunit: test committing psi goal to psi goal

damon_test_commit_quota_goal_for() is set to test committing a new psi goal on an existing psi goal. However, damon_test_commit_quota_goal() is mistakenly not covering the test case. Add the test case.

Fixes 99f89debafc5 ("mm/damon/tests/core-kunit: add damos_commit_quota_goal() test")
Cc <[email protected]> # 6.19.x
---
 mm/damon/tests/core-kunit.h | 7 +++++++
 1 file changed, 7 insertions(+)
mm/damon/tests/core-kunit.h +7 -0
index 4a536d41cdb2d..b643f9a83f14a 100644
@@ -787,6 +787,13 @@ static void damos_test_commit_quota_goal(struct kunit *test)
 		.last_psi_total = 456,
 	};
 
+	damos_test_commit_quota_goal_for(test, &dst,
+			&(struct damos_quota_goal) {
+			.metric = DAMOS_QUOTA_SOME_MEM_PSI_US,
+			.target_value = 234,
+			.current_value = 345,
+			.last_psi_total = 567,
+			});
 	damos_test_commit_quota_goal_for(test, &dst,
 			&(struct damos_quota_goal){
 			.metric = DAMOS_QUOTA_USER_INPUT,
Signature · 1 line
2.47.3
#2 lore raw

Re: [RFC PATCH v1.2 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal

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

damon_test_commit_quota_goal_for() is set to test committing a new psi goal on an existing psi goal. However, damon_test_commit_quota_goal() is mistakenly not covering the test case. Add the test case.

Fixes 99f89debafc5 ("mm/damon/tests/core-kunit: add damos_commit_quota_goal() test")
Cc <[email protected]> # 6.19.x

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
author maintainer
#3 lore raw

RFC PATCH 2/4 mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total

In reply to SJ Park

When DAMOS_QUOTA_SOME_MEM_PSI_US metric damos quota goal is set, the PSI delta for the feedback loop is calculated using damos_quota_goal->last_psi_total. However, it is initialized only after the first feedback loop. The first iteration of the loop uses the uninitialized value. As a result, the feedback loop can change the effective quota in an unexpected way at the first iteration.

The user impact of the issue is not big, because the issue impacts only the first iteration of the feedback loop. The feedback loop also has an internal cap of the quota adjustment. The wrong adjustment will soon be corrected over a few iterations. For this reason, doing no initialization at commit time was intentional. It is also explicitly commented. That said, nobody likes behaviors that are unexpected or difficult to be expected.

Check last_psi_total initialization and skip the tuning round when it is not initialized. For this, initialize last_psi_total with U64_MAX in the goal creation and the goal commit time. U64_MAX means the field is not initialized. The tuning round shows the value and adjusts it to guarantee the current quota is maintained for the round, and last_psi_total is correctly initialized on the next round.

Before this change, committing a new PSI goal on an existing PSI goal with goal-only DAMON sysfs command (commit_schemes_quota_goals) just worked. After this change, the tuning round right after the commit will be unnecessarily skipped, because last_psi_total is unconditionally marked as not initialized in the damos_commit_quota_goal_union(). This is an intended tradeoff for simplicity. Skipping just one round of tuning is no problem. Meanwhile it makes both the code and the behavior simple to understand.

Also update the quota goal commit unit test for changed last_psi_total setup behavior.

The issue was discovered [1] by Sashiko.

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

Fixes 2dbb60f789cb ("mm/damon/core: implement PSI metric DAMOS quota goal")
Cc <[email protected]> # 6.9.x
---
 mm/damon/core.c             | 10 +++++++++-
 mm/damon/tests/core-kunit.h |  9 +++------
 2 files changed, 12 insertions(+), 7 deletions(-)
mm/damon/core.c +9 -1
index f8dddbff74a77..f7e66898aeb84 100644
@@ -635,6 +635,8 @@ struct damos_quota_goal *damos_new_quota_goal(
 		return NULL;
 	goal->metric = metric;
 	goal->target_value = target_value;
+	if (metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
+		goal->last_psi_total = U64_MAX;
 	INIT_LIST_HEAD(&goal->list);
 	return goal;
 }
@@ -1122,6 +1124,9 @@ static void damos_commit_quota_goal_union(
 		struct damos_quota_goal *dst, struct damos_quota_goal *src)
 {
 	switch (dst->metric) {
+	case DAMOS_QUOTA_SOME_MEM_PSI_US:
+		dst->last_psi_total = U64_MAX;
+		break;
 	case DAMOS_QUOTA_NODE_MEM_USED_BP:
 	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
 		dst->nid = src->nid;
@@ -1143,7 +1148,6 @@ static void damos_commit_quota_goal(
 	dst->target_value = src->target_value;
 	if (dst->metric == DAMOS_QUOTA_USER_INPUT)
 		dst->current_value = src->current_value;
-	/* keep last_psi_total as is, since it will be updated in next cycle */
 	damos_commit_quota_goal_union(dst, src);
 }
 
@@ -3032,6 +3036,10 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
 		break;
 	case DAMOS_QUOTA_SOME_MEM_PSI_US:
 		now_psi_total = damos_get_some_mem_psi_total();
+		/* uninitialized last_psi_total; make no effect this round */
+		if (goal->last_psi_total == U64_MAX)
+			goal->last_psi_total = now_psi_total +
+				goal->target_value;
 		goal->current_value = now_psi_total - goal->last_psi_total;
 		goal->last_psi_total = now_psi_total;
 		break;
mm/damon/tests/core-kunit.h +3 -6
index b643f9a83f14a..65443aba03300 100644
@@ -751,19 +751,16 @@ static void damos_test_commit_quota_goal_for(struct kunit *test,
 		struct damos_quota_goal *dst,
 		struct damos_quota_goal *src)
 {
-	u64 dst_last_psi_total = 0;
-
-	if (dst->metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
-		dst_last_psi_total = dst->last_psi_total;
 	damos_commit_quota_goal(dst, src);
 
 	KUNIT_EXPECT_EQ(test, dst->metric, src->metric);
 	KUNIT_EXPECT_EQ(test, dst->target_value, src->target_value);
 	if (src->metric == DAMOS_QUOTA_USER_INPUT)
 		KUNIT_EXPECT_EQ(test, dst->current_value, src->current_value);
-	if (dst_last_psi_total && src->metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
-		KUNIT_EXPECT_EQ(test, dst->last_psi_total, dst_last_psi_total);
 	switch (dst->metric) {
+	case DAMOS_QUOTA_SOME_MEM_PSI_US:
+		KUNIT_EXPECT_EQ(test, dst->last_psi_total, U64_MAX);
+		break;
 	case DAMOS_QUOTA_NODE_MEM_USED_BP:
 	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
 		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
Signature · 1 line
2.47.3
#4 lore raw

Re: [RFC PATCH v1.2 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Integer underflow when initializing last_psi_total causes severe quota throttling instead of…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Integer underflow when initializing last_psi_total causes severe quota throttling instead of maintaining the quota. --

commit 216061cc4d3a0304d1961ab1798b65cbf748aa6c
Author: SJ Park <[email protected]>

mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total

This commit handles an uninitialized last_psi_total for PSI metric DAMOS quota goals by initializing it to U64_MAX. During the first tuning round, it detects this state and attempts to set last_psi_total such that the first iteration has no unexpected effect on the quota.

Quoted 4 lines
mm/damon/core.c +0 -0
index 644daf5a16560..b0c8e22ab5404 100644

[ ... ]

mm/damon/core.c static void damos_set_quota_goal_current_value(struct damon_ctx *c, · line 3029

Quoted 9 lines
mm/damon/core.c +4 -0
@@ -3018,6 +3022,10 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
 		break;
 	case DAMOS_QUOTA_SOME_MEM_PSI_US:
 		now_psi_total = damos_get_some_mem_psi_total();
+		/* uninitialized last_psi_total; make no effect this round */
+		if (goal->last_psi_total == U64_MAX)
+			goal->last_psi_total = now_psi_total +
+				goal->target_value;
 		goal->current_value = now_psi_total - goal->last_psi_total;

[Severity: High]
Does this arithmetic result in an integer underflow on the first tuning round?

If goal->last_psi_total is set to (now_psi_total + goal->target_value), the calculation on the very next line becomes:

goal->current_value = now_psi_total - (now_psi_total + goal->target_value)

This evaluates to -goal->target_value. Since current_value is an unsigned long, this underflows to an astronomically large value. Will the feedback loop interpret this as a massive over-achievement and drastically throttle the quota to its minimum, causing the exact opposite of the intended "no effect this round" behavior?

Quoted 2 lines
mm/damon/core.c +0 -0
 		goal->last_psi_total = now_psi_total;
 		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

Re: [RFC PATCH v1.2 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total

Quoted 38 lines · On Fri, 28 Aug 2026 01:55:23 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Integer underflow when initializing last_psi_total causes severe quota throttling instead of maintaining the quota. --

commit 216061cc4d3a0304d1961ab1798b65cbf748aa6c
Author: SJ Park <[email protected]>

mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total

This commit handles an uninitialized last_psi_total for PSI metric DAMOS quota goals by initializing it to U64_MAX. During the first tuning round, it detects this state and attempts to set last_psi_total such that the first iteration has no unexpected effect on the quota.

Quoted 4 lines
mm/damon/core.c +0 -0
index 644daf5a16560..b0c8e22ab5404 100644

[ ... ]

mm/damon/core.c static void damos_set_quota_goal_current_value(struct damon_ctx *c, · line 3029

Quoted 9 lines
mm/damon/core.c +4 -0
@@ -3018,6 +3022,10 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
 		break;
 	case DAMOS_QUOTA_SOME_MEM_PSI_US:
 		now_psi_total = damos_get_some_mem_psi_total();
+		/* uninitialized last_psi_total; make no effect this round */
+		if (goal->last_psi_total == U64_MAX)
+			goal->last_psi_total = now_psi_total +
+				goal->target_value;
 		goal->current_value = now_psi_total - goal->last_psi_total;

[Severity: High]
Does this arithmetic result in an integer underflow on the first tuning round?

If goal->last_psi_total is set to (now_psi_total + goal->target_value), the calculation on the very next line becomes:

Quoted 7 lines

goal->current_value = now_psi_total - (now_psi_total + goal->target_value)

This evaluates to -goal->target_value. Since current_value is an unsigned long, this underflows to an astronomically large value. Will the feedback loop interpret this as a massive over-achievement and drastically throttle the quota to its minimum, causing the exact opposite of the intended "no effect this round" behavior?

Argh, correct. I will fix this like below.

'''

mm/damon/core.c +4 -3
@@ -3394,9 +3394,10 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
                now_psi_total = damos_get_some_mem_psi_total();
                /* uninitialized last_psi_total; make no effect this round */
                if (goal->last_psi_total == U64_MAX)
-                       goal->last_psi_total = now_psi_total +
-                               goal->target_value;
-               goal->current_value = now_psi_total - goal->last_psi_total;
+                       goal->current_value = goal->target_value;
+               else
+                       goal->current_value = now_psi_total -
+                               goal->last_psi_total;
                goal->last_psi_total = now_psi_total;
                break;
        case DAMOS_QUOTA_NODE_MEM_USED_BP:

'''

Quoted 6 lines
Quoted 3 lines
mm/damon/core.c +0 -0
 		goal->last_psi_total = now_psi_total;
 		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

Thanks,
SJ

author maintainer
#6 lore raw

RFC PATCH 3/4 mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit

In reply to SJ Park

damos_commit_quota_goal_union() is not updating the ->nid union field when the goal metric is DAMOS_QUITA_NODE_ELIGIBLE_MEM_BP. Hence, if a DAMOS quota goal of the type is online committed in a way that it will reuse other quota goal's memory space, the new goal will work with a garbage nid value. As a result, the DAMOS scheme can show unexpected aggressiveness. Do the update.

The user impact is not catastrophic. No leak or crash happens. Doing the quota goal online commit that can reproduce the issue is expected to be not common. This issue was not found by real users but the AI review. That said, the issue can reliably be reproduced.

This issue was discovered [1] by Sashiko.

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

Fixes 9138e27a3bc3 ("mm/damon: add node_eligible_mem_bp goal metric")
Cc <[email protected]> # 7.2.x
---
 mm/damon/core.c | 3 +++
 1 file changed, 3 insertions(+)
mm/damon/core.c +3 -0
index f7e66898aeb84..ce649553d768d 100644
@@ -1136,6 +1136,9 @@ static void damos_commit_quota_goal_union(
 		dst->nid = src->nid;
 		dst->memcg_id = src->memcg_id;
 		break;
+	case DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP:
+		dst->nid = src->nid;
+		break;
 	default:
 		break;
 	}
Signature · 1 line
2.47.3
#7 lore raw

Re: [RFC PATCH v1.2 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The KUnit tests in `mm/damon/tests/core-kunit.h` were not updated to verify the bug fix for the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The KUnit tests in mm/damon/tests/core-kunit.h were not updated to verify the bug fix for the DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP metric. --

commit 2e3746fc6a44e14dd4bda23054c960b1e043e013
Author: SJ Park <[email protected]>

mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit

damos_commit_quota_goal_union() does not update the nid union field when the goal metric is DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP. This commit adds the missing update to ensure it does not use a garbage nid value.

mm/damon/core.c static void damos_commit_quota_goal_union( · line 1141

Quoted 11 lines
mm/damon/core.c +3 -0
index b0c8e22ab5404..086d2ac57259f 100644
@@ -1136,6 +1136,9 @@ static void damos_commit_quota_goal_union(
 		dst->nid = src->nid;
 		dst->memcg_id = src->memcg_id;
 		break;
+	case DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP:
+		dst->nid = src->nid;
+		break;

[Severity: Medium]
Since this case was added here, do we also need to update the corresponding KUnit tests in mm/damon/tests/core-kunit.h?

Currently, damos_test_commit_quota_goal_for() is missing the case for DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP:

mm/damon/tests/core-kunit.h:damos_test_commit_quota_goal_for() {
    ...
	switch (dst->metric) {
	case DAMOS_QUOTA_SOME_MEM_PSI_US:
		KUNIT_EXPECT_EQ(test, dst->last_psi_total, U64_MAX);
		break;
	case DAMOS_QUOTA_NODE_MEM_USED_BP:
	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
		break;
	case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
	case DAMOS_QUOTA_NODE_MEMCG_FREE_BP:
		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
		KUNIT_EXPECT_EQ(test, dst->memcg_id, src->memcg_id);
		break;
	default:
		break;
	}
}

Should a test case for DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP also be added to damos_test_commit_quota_goal() to ensure the fix is validated?

Quoted 3 lines
mm/damon/core.c +0 -0
 	default:
 		break;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#8 lore raw

Re: [RFC PATCH v1.2 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit

Quoted 54 lines · On Fri, 28 Aug 2026 01:56:34 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The KUnit tests in mm/damon/tests/core-kunit.h were not updated to verify the bug fix for the DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP metric. --

commit 2e3746fc6a44e14dd4bda23054c960b1e043e013
Author: SJ Park <[email protected]>

mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit

damos_commit_quota_goal_union() does not update the nid union field when the goal metric is DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP. This commit adds the missing update to ensure it does not use a garbage nid value.

mm/damon/core.c static void damos_commit_quota_goal_union( · line 1141

Quoted 11 lines
mm/damon/core.c +3 -0
index b0c8e22ab5404..086d2ac57259f 100644
@@ -1136,6 +1136,9 @@ static void damos_commit_quota_goal_union(
 		dst->nid = src->nid;
 		dst->memcg_id = src->memcg_id;
 		break;
+	case DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP:
+		dst->nid = src->nid;
+		break;

[Severity: Medium]
Since this case was added here, do we also need to update the corresponding KUnit tests in mm/damon/tests/core-kunit.h?

Currently, damos_test_commit_quota_goal_for() is missing the case for DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP:

mm/damon/tests/core-kunit.h:damos_test_commit_quota_goal_for() {
    ...
	switch (dst->metric) {
	case DAMOS_QUOTA_SOME_MEM_PSI_US:
		KUNIT_EXPECT_EQ(test, dst->last_psi_total, U64_MAX);
		break;
	case DAMOS_QUOTA_NODE_MEM_USED_BP:
	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
		break;
	case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
	case DAMOS_QUOTA_NODE_MEMCG_FREE_BP:
		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
		KUNIT_EXPECT_EQ(test, dst->memcg_id, src->memcg_id);
		break;
	default:
		break;
	}
}
Quoted 2 lines

Should a test case for DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP also be added to damos_test_commit_quota_goal() to ensure the fix is validated?

I'm working on this in a stealth mode ;) Will post as a separate series, since it is definitely not a fix.

Quoted 7 lines
Quoted 4 lines
mm/damon/core.c +0 -0
 	default:
 		break;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

Thanks,
SJ

author maintainer
#9 lore raw

RFC PATCH 4/4 mm/damon/sysfs: set next refresh jiffies per sysfs context

In reply to SJ Park

When 'refresh_ms' is set, DAMON sysfs interface periodically updates auto-tuned parameters and DAMOS stats. The timestamp for the next refresh is initialized when a DAMON context starts, and updated in its damon_call() callback function. That is, each DAMON context updates it. However, the timestamp is a global variable that is shared with all the contexts. When there are multiple DAMON contexts having different refresh_ms, the update frequency will be changed, depending on the order of the contexts. When there are multiple kdamonds, it will be even more chaotic. Fix the problem by having the timestamp per each context.

The user impact is not very critical. It does not leak, corrupt or crash. The update will not be faster or slower than the lowest and largest refresh_ms values of the contexts, respectively. The user can also manually ask the updates on demand using kdamond state commands. That said, clearly this is a bug and can easily be reproduced.

Fixes 9fd7bb5083d1 ("mm/damon/sysfs: change next_update_jiffies to a global variable")
Cc <[email protected]> # 6.18.x
---
 mm/damon/sysfs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
mm/damon/sysfs.c +5 -6
index e3858ffab4b22..f05b256c90ee7 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 next_refresh_jiffies;
 };
 
 static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void)
@@ -2215,17 +2216,15 @@ static struct damon_ctx *damon_sysfs_build_ctx(
 	return ctx;
 }
 
-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->next_refresh_jiffies))
 		return 0;
-	damon_sysfs_next_update_jiffies = jiffies +
+	sysfs_kdamond->next_refresh_jiffies = jiffies +
 		msecs_to_jiffies(sysfs_kdamond->refresh_ms);
 
 	if (!mutex_trylock(&damon_sysfs_lock))
@@ -2273,8 +2272,8 @@ static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
 	}
 	kdamond->damon_ctx = ctx;
 
-	damon_sysfs_next_update_jiffies =
-		jiffies + msecs_to_jiffies(kdamond->refresh_ms);
+	kdamond->next_refresh_jiffies = jiffies +
+		msecs_to_jiffies(kdamond->refresh_ms);
 
 	repeat_call_control->fn = damon_sysfs_repeat_call_fn;
 	repeat_call_control->data = kdamond;
Signature · 1 line
2.47.3
#10 lore raw

Re: [RFC PATCH v1.2 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context

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

When 'refresh_ms' is set, DAMON sysfs interface periodically updates auto-tuned parameters and DAMOS stats. The timestamp for the next refresh is initialized when a DAMON context starts, and updated in its damon_call() callback function. That is, each DAMON context updates it. However, the timestamp is a global variable that is shared with all the contexts. When there are multiple DAMON contexts having different refresh_ms, the update frequency will be changed, depending on the order of the contexts. When there are multiple kdamonds, it will be even more chaotic. Fix the problem by having the timestamp per each context.

The user impact is not very critical. It does not leak, corrupt or crash. The update will not be faster or slower than the lowest and largest refresh_ms values of the contexts, respectively. The user can also manually ask the updates on demand using kdamond state commands. That said, clearly this is a bug and can easily be reproduced.

Quoted 3 lines
Fixes 9fd7bb5083d1 ("mm/damon/sysfs: change next_update_jiffies to a global variable")
Cc <[email protected]> # 6.18.x

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=4