DAMON development mailing list
 help / color / mirror / Atom feed
* [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms
@ 2026-09-02  0:27 SJ Park
  2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  0:27 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SJ Park, stable, Brendan Higgins, David Gow, Quanmin Yan,
	Ravi Jonnalagadda, damon, kunit-dev, linux-kernel,
	linux-kselftest, linux-mm

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.3
- RFC v1.3: https://lore.kernel.org/20260828183649.71192-1-sj@kernel.org
- Drop RFC tag.
- Rebase to latest mm-new.
Changes from RFC v1.2
- RFC v1.2: https://lore.kernel.org/20260828014659.121058-1-sj@kernel.org
- Fix last_psi_total underflow.
Changes from RFC v1.1
- RFC v1.1: https://lore.kernel.org/20260827134429.123023-1-sj@kernel.org
- 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/20260827042920.93580-1-sj@kernel.org
  (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             | 16 ++++++++++++++--
 mm/damon/sysfs.c            | 11 +++++------
 mm/damon/tests/core-kunit.h | 16 ++++++++++------
 3 files changed, 29 insertions(+), 14 deletions(-)


base-commit: ee125db0b1f24a2f6b04e3143ffe1a14324176fe
-- 
2.47.3

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
@ 2026-09-02  0:27 ` SJ Park
  2026-09-02  0:34   ` sashiko-bot
  2026-09-03  8:33   ` Kunwu Chan
  2026-09-02  0:27 ` [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total SJ Park
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  0:27 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SJ Park, stable, Brendan Higgins, David Gow, damon, kunit-dev,
	linux-kernel, linux-kselftest, linux-mm

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: <stable@vger.kernel.org> # 6.19.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 7071ec277b007..f1e11548c771b 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -793,6 +793,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,
-- 
2.47.3

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
  2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
@ 2026-09-02  0:27 ` SJ Park
  2026-09-02  0:34   ` sashiko-bot
  2026-09-03  9:23   ` Kunwu Chan
  2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  0:27 UTC (permalink / raw)
  To: Andrew Morton; +Cc: SJ Park, stable, damon, linux-kernel, linux-mm

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/20260718005316.89585-1-sj@kernel.org

Fixes: 2dbb60f789cb ("mm/damon/core: implement PSI metric DAMOS quota goal")
Cc: <stable@vger.kernel.org> # 6.9.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/core.c             | 13 +++++++++++--
 mm/damon/tests/core-kunit.h |  9 +++------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index 0df785e72438f..20748b0a71026 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -636,6 +636,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;
 }
@@ -1129,6 +1131,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;
@@ -1150,7 +1155,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);
 }
 
@@ -3039,7 +3043,12 @@ 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();
-		goal->current_value = now_psi_total - goal->last_psi_total;
+		/* uninitialized last_psi_total; make no effect this round */
+		if (goal->last_psi_total == U64_MAX)
+			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:
diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index f1e11548c771b..af26b3d60957b 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -757,19 +757,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);
-- 
2.47.3

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
  2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
  2026-09-02  0:27 ` [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total SJ Park
@ 2026-09-02  0:27 ` SJ Park
  2026-09-02  0:39   ` sashiko-bot
                     ` (2 more replies)
  2026-09-02  0:27 ` [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context SJ Park
                   ` (2 subsequent siblings)
  5 siblings, 3 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  0:27 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SJ Park, stable, Ravi Jonnalagadda, damon, linux-kernel, linux-mm

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/20260827045035.94611-1-sj@kernel.org

Fixes: 9138e27a3bc3 ("mm/damon: add node_eligible_mem_bp goal metric")
Cc: <stable@vger.kernel.org> # 7.2.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index 20748b0a71026..53c3f97e3f126 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -1143,6 +1143,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;
 	}
-- 
2.47.3

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
                   ` (2 preceding siblings ...)
  2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
@ 2026-09-02  0:27 ` SJ Park
  2026-09-02  0:39   ` sashiko-bot
  2026-09-03 10:01   ` Kunwu Chan
  2026-09-02  1:16 ` [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
  2026-09-03  3:13 ` Lian Wang
  5 siblings, 2 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  0:27 UTC (permalink / raw)
  To: Andrew Morton; +Cc: SJ Park, stable, Quanmin Yan, damon, linux-kernel, linux-mm

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: <stable@vger.kernel.org> # 6.18.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 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 3c81b4c91ac0d..dcb739ce0a729 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1788,6 +1788,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)
@@ -2213,17 +2214,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))
@@ -2271,8 +2270,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;
-- 
2.47.3

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal
  2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
@ 2026-09-02  0:34   ` sashiko-bot
  2026-09-03  8:33   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-09-02  0:34 UTC (permalink / raw)
  To: SJ Park; +Cc: damon

> 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: <stable@vger.kernel.org> # 6.19.x
> Signed-off-by: SJ Park <sj@kernel.org>

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902002725.108635-1-sj@kernel.org?part=1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total
  2026-09-02  0:27 ` [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total SJ Park
@ 2026-09-02  0:34   ` sashiko-bot
  2026-09-03  9:23   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-09-02  0:34 UTC (permalink / raw)
  To: SJ Park; +Cc: damon

> 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.
> 
> [ ... ]
> [1] https://lore.kernel.org/20260718005316.89585-1-sj@kernel.org
> 
> Fixes: 2dbb60f789cb ("mm/damon/core: implement PSI metric DAMOS quota goal")
> Cc: <stable@vger.kernel.org> # 6.9.x
> Signed-off-by: SJ Park <sj@kernel.org>

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902002725.108635-1-sj@kernel.org?part=2


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context
  2026-09-02  0:27 ` [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context SJ Park
@ 2026-09-02  0:39   ` sashiko-bot
  2026-09-03 10:01   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-09-02  0:39 UTC (permalink / raw)
  To: SJ Park; +Cc: damon

> 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: <stable@vger.kernel.org> # 6.18.x
> Signed-off-by: SJ Park <sj@kernel.org>

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902002725.108635-1-sj@kernel.org?part=4


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
@ 2026-09-02  0:39   ` sashiko-bot
  2026-09-02  1:13     ` SJ Park
  2026-09-02  1:14   ` SJ Park
  2026-09-03  8:45   ` Kunwu Chan
  2 siblings, 1 reply; 19+ messages in thread
From: sashiko-bot @ 2026-09-02  0:39 UTC (permalink / raw)
  To: SJ Park; +Cc: damon

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Inconsistent state handling in damon_commit_ctx() on memory allocation failure.
--

commit c0fdc35d2fb6726e726ba0e857811cbaefc34918
Author: SJ Park <sj@kernel.org>

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

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.

> damos_commit_quota_goal_union() is not updating the ->nid union field
> when the goal metric is DAMOS_QUITA_NODE_ELIGIBLE_MEM_BP.

This isn't a bug, but there's a typo (DAMOS_QUITA_NODE_ELIGIBLE_MEM_BP) in
the commit message.

> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 647ac77f573bb..a499d7866c507 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -1143,6 +1143,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;
>  	}

[Severity: High]
This isn't a bug introduced by this patch, but is a pre-existing logic
flaw in the core DAMON commit mechanism on memory allocation failure.

In mm/damon/core.c:damon_commit_ctx():

	err = __damon_commit_ctx(test_ctx, src);
	if (err)
		goto out;
	err = __damon_commit_ctx(dst, src);

If the second call to __damon_commit_ctx() fails with -ENOMEM, won't it
leave the active dst context corrupted?

For example, an OOM condition can occur here in
mm/damon/core.c:damon_commit_schemes():

		new_scheme = damon_new_scheme(&src_scheme->pattern,
				src_scheme->action,
				src_scheme->apply_interval_us,
				&src_scheme->quota, &src_scheme->wmarks,
				NUMA_NO_NODE);
		if (!new_scheme)
			return -ENOMEM;

A failure here exits the commit process without rolling back prior scheme
updates, leaving the active dst context partially updated.

The kernel handles this by setting dst->maybe_corrupted = true, which
forces the kdamond monitoring thread to terminate gracefully but permanently.

Could this be triggered by a user-initiated online parameter update
encountering an OOM condition during the second __damon_commit_ctx()
invocation?

Does this violate the atomic nature of damon_commit_ctx() (which uses
test_ctx precisely to avoid partial updates on the active context) and
cause an unintended loss of the running context?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902002725.108635-1-sj@kernel.org?part=3

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-02  0:39   ` sashiko-bot
@ 2026-09-02  1:13     ` SJ Park
  0 siblings, 0 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  1:13 UTC (permalink / raw)
  To: sashiko-bot; +Cc: SJ Park, damon

On Wed, 02 Sep 2026 00:39:50 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] Inconsistent state handling in damon_commit_ctx() on memory allocation failure.
> --
> 
> commit c0fdc35d2fb6726e726ba0e857811cbaefc34918
> Author: SJ Park <sj@kernel.org>
> 
> mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
> 
> 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.
> 
> > damos_commit_quota_goal_union() is not updating the ->nid union field
> > when the goal metric is DAMOS_QUITA_NODE_ELIGIBLE_MEM_BP.
> 
> This isn't a bug, but there's a typo (DAMOS_QUITA_NODE_ELIGIBLE_MEM_BP) in
> the commit message.

Good catch.  I will ask Andrew if he could fix this while picking this.

> 
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index 647ac77f573bb..a499d7866c507 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -1143,6 +1143,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;
> >  	}
> 
> [Severity: High]
> This isn't a bug introduced by this patch, but is a pre-existing logic
> flaw in the core DAMON commit mechanism on memory allocation failure.
> 
> In mm/damon/core.c:damon_commit_ctx():
> 
> 	err = __damon_commit_ctx(test_ctx, src);
> 	if (err)
> 		goto out;
> 	err = __damon_commit_ctx(dst, src);
> 
> If the second call to __damon_commit_ctx() fails with -ENOMEM, won't it
> leave the active dst context corrupted?
> 
> For example, an OOM condition can occur here in
> mm/damon/core.c:damon_commit_schemes():
> 
> 		new_scheme = damon_new_scheme(&src_scheme->pattern,
> 				src_scheme->action,
> 				src_scheme->apply_interval_us,
> 				&src_scheme->quota, &src_scheme->wmarks,
> 				NUMA_NO_NODE);
> 		if (!new_scheme)
> 			return -ENOMEM;
> 
> A failure here exits the commit process without rolling back prior scheme
> updates, leaving the active dst context partially updated.
> 
> The kernel handles this by setting dst->maybe_corrupted = true, which
> forces the kdamond monitoring thread to terminate gracefully but permanently.
> 
> Could this be triggered by a user-initiated online parameter update
> encountering an OOM condition during the second __damon_commit_ctx()
> invocation?
> 
> Does this violate the atomic nature of damon_commit_ctx() (which uses
> test_ctx precisely to avoid partial updates on the active context) and
> cause an unintended loss of the running context?

Such user would need to have sysfs write permission first.  With the
permission, the user could do whatever, even worse than stopping DAMON.

So no issue in my opinion.

> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260902002725.108635-1-sj@kernel.org?part=3


Thanks,
SJ

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
  2026-09-02  0:39   ` sashiko-bot
@ 2026-09-02  1:14   ` SJ Park
  2026-09-03  8:45   ` Kunwu Chan
  2 siblings, 0 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  1:14 UTC (permalink / raw)
  To: SJ Park
  Cc: Andrew Morton, stable, Ravi Jonnalagadda, damon, linux-kernel,
	linux-mm

On Tue,  1 Sep 2026 17:27:22 -0700 SJ Park <sj@kernel.org> wrote:

> 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

Sashiko found the above typo.  It needs 's/QUITA/QUOTA/'.  Andrew, could you
please do that when you pick this patch?  Please feel free to let me know if
you prefer repositing.


Thanks,
SJ

[...]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
                   ` (3 preceding siblings ...)
  2026-09-02  0:27 ` [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context SJ Park
@ 2026-09-02  1:16 ` SJ Park
  2026-09-03  3:13 ` Lian Wang
  5 siblings, 0 replies; 19+ messages in thread
From: SJ Park @ 2026-09-02  1:16 UTC (permalink / raw)
  To: SJ Park
  Cc: Andrew Morton, stable, Brendan Higgins, David Gow, Quanmin Yan,
	Ravi Jonnalagadda, damon, kunit-dev, linux-kernel,
	linux-kselftest, linux-mm

On Tue,  1 Sep 2026 17:27:19 -0700 SJ Park <sj@kernel.org> wrote:

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

Sashiko found no blocker for this series.  Sashiko sent findings to damon@
mailing list [1], and I replied to all the comments.  Please read those for
details.

Sashiko found a typo in the commit message of patch 3, though.  It should
's/QUITA/QUOTA/'.  I asked Andrew if he could fix it from his side.

[1] https://lore.kernel.org/damon/


Thanks,
SJ

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms
  2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
                   ` (4 preceding siblings ...)
  2026-09-02  1:16 ` [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
@ 2026-09-03  3:13 ` Lian Wang
  2026-09-03 13:51   ` SJ Park
  5 siblings, 1 reply; 19+ messages in thread
From: Lian Wang @ 2026-09-03  3:13 UTC (permalink / raw)
  To: SJ Park
  Cc: Andrew Morton, stable, Brendan Higgins, David Gow, Quanmin Yan,
	Ravi Jonnalagadda, damon, kunit-dev, linux-kernel,
	linux-kselftest, linux-mm

Hi SJ,

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

I reviewed and tested the integrated four-patch series in mm-new:

  a20e38846efb..72dc8fdaae62

The integrated commits had stable patch-ids matching the posted patches.
DAMON KUnit passed 42/42 tests.  I also ran test-only eligible_mem_bp nid and
two-kdamond refresh-isolation regression cases; they passed 42/42 and 43/43
tests, respectively.  Both test-only changes were then removed and the
worktree returned clean at the unchanged series tip.

These were functional UML KUnit tests.  The fixes look good to me.

For the series:

Reviewed-by: Lian Wang <lianux.mm@gmail.com>
Tested-by: Lian Wang <lianux.mm@gmail.com>

Thanks,
Lian

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal
  2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
  2026-09-02  0:34   ` sashiko-bot
@ 2026-09-03  8:33   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: Kunwu Chan @ 2026-09-03  8:33 UTC (permalink / raw)
  To: SJ Park
  Cc: Kunwu Chan, Andrew Morton, stable, Brendan Higgins, David Gow,
	damon, kunit-dev, linux-kernel, linux-kselftest, linux-mm,
	Kunwu Chan

On Tue,  1 Sep 2026 17:27:20 -0700 SJ Park <sj@kernel.org> wrote:

> 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: <stable@vger.kernel.org> # 6.19.x
> Signed-off-by: SJ Park <sj@kernel.org>
> ---
>  mm/damon/tests/core-kunit.h | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
> index 7071ec277b007..f1e11548c771b 100644
> --- a/mm/damon/tests/core-kunit.h
> +++ b/mm/damon/tests/core-kunit.h
> @@ -793,6 +793,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,
> -- 
> 2.47.3
> 

Thanks SJ. The added case correctly exercises the PSI -> PSI commit 
path before the existing tests change the destination metric.

Reviewed-by: Kunwu Chan <kunwu.chan@gmail.com>

Thanks,
Kunwu


Sent using hkml (https://github.com/sjp38/hackermail)

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
  2026-09-02  0:39   ` sashiko-bot
  2026-09-02  1:14   ` SJ Park
@ 2026-09-03  8:45   ` Kunwu Chan
  2026-09-03 13:49     ` SJ Park
  2 siblings, 1 reply; 19+ messages in thread
From: Kunwu Chan @ 2026-09-03  8:45 UTC (permalink / raw)
  To: SJ Park
  Cc: Kunwu Chan, Andrew Morton, stable, Ravi Jonnalagadda, damon,
	linux-kernel, linux-mm, Kunwu Chan

On Tue,  1 Sep 2026 17:27:22 -0700 SJ Park <sj@kernel.org> wrote:

> 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/20260827045035.94611-1-sj@kernel.org

Hi SJ,

The fix is consistent with the other node-based quota metrics. I also 
checked that damos_get_node_eligible_mem_bp() directly uses goal->nid 
when calculating the current value.

One minor typo: the [1] has an extra k (kkernel should be kernel).

Reviewed-by: Kunwu Chan <kunwu.chan@gmail.com>

Thanks,
Kunwu

> 
> Fixes: 9138e27a3bc3 ("mm/damon: add node_eligible_mem_bp goal metric")
> Cc: <stable@vger.kernel.org> # 7.2.x
> Signed-off-by: SJ Park <sj@kernel.org>
> ---
>  mm/damon/core.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 20748b0a71026..53c3f97e3f126 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -1143,6 +1143,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;
>  	}
> -- 
> 2.47.3
> 

Sent using hkml (https://github.com/sjp38/hackermail)

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total
  2026-09-02  0:27 ` [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total SJ Park
  2026-09-02  0:34   ` sashiko-bot
@ 2026-09-03  9:23   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: Kunwu Chan @ 2026-09-03  9:23 UTC (permalink / raw)
  To: SJ Park
  Cc: Kunwu Chan, Andrew Morton, stable, damon, linux-kernel, linux-mm,
	Kunwu Chan

On Tue,  1 Sep 2026 17:27:21 -0700 SJ Park <sj@kernel.org> wrote:

> 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/20260718005316.89585-1-sj@kernel.org
> 
> Fixes: 2dbb60f789cb ("mm/damon/core: implement PSI metric DAMOS quota goal")
> Cc: <stable@vger.kernel.org> # 6.9.x
> Signed-off-by: SJ Park <sj@kernel.org>
> ---
>  mm/damon/core.c             | 13 +++++++++++--
>  mm/damon/tests/core-kunit.h |  9 +++------
>  2 files changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 0df785e72438f..20748b0a71026 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -636,6 +636,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;
>  }
> @@ -1129,6 +1131,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;
> @@ -1150,7 +1155,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);
>  }
>  
> @@ -3039,7 +3043,12 @@ 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();
> -		goal->current_value = now_psi_total - goal->last_psi_total;
> +		/* uninitialized last_psi_total; make no effect this round */
> +		if (goal->last_psi_total == U64_MAX)
> +			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:
> diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
> index f1e11548c771b..af26b3d60957b 100644
> --- a/mm/damon/tests/core-kunit.h
> +++ b/mm/damon/tests/core-kunit.h
> @@ -757,19 +757,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;

The U64_MAX sentinel approach looks correct to me.

In the first feedback iteration, setting `current_value` to `target_value`
gives the PSI goal a score of 10000, so the uninitialized PSI delta does
not affect the quota score. The current PSI total is then recorded,
allowing subsequent iterations to calculate the delta normally.

I also checked the commit path: the PSI goal is reset to U64_MAX when
committed, which intentionally skips one tuning round when replacing an
existing PSI goal.

Reviewed-by: Kunwu Chan <kunwu.chan@gmail.com>

Thanks,
Kunwu

>  	case DAMOS_QUOTA_NODE_MEM_USED_BP:
>  	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
>  		KUNIT_EXPECT_EQ(test, dst->nid, src->nid);
> -- 
> 2.47.3
> 

Sent using hkml (https://github.com/sjp38/hackermail)

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context
  2026-09-02  0:27 ` [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context SJ Park
  2026-09-02  0:39   ` sashiko-bot
@ 2026-09-03 10:01   ` Kunwu Chan
  1 sibling, 0 replies; 19+ messages in thread
From: Kunwu Chan @ 2026-09-03 10:01 UTC (permalink / raw)
  To: SJ Park
  Cc: Kunwu Chan, Andrew Morton, stable, Quanmin Yan, damon,
	linux-kernel, linux-mm, Kunwu Chan

On Tue,  1 Sep 2026 17:27:23 -0700 SJ Park <sj@kernel.org> wrote:

> 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: <stable@vger.kernel.org> # 6.18.x
> Signed-off-by: SJ Park <sj@kernel.org>
> ---
>  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 3c81b4c91ac0d..dcb739ce0a729 100644
> --- a/mm/damon/sysfs.c
> +++ b/mm/damon/sysfs.c
> @@ -1788,6 +1788,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)
> @@ -2213,17 +2214,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))
> @@ -2271,8 +2270,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);

The fix looks correct to me.

Moving next_refresh_jiffies into struct damon_sysfs_kdamond gives 
each kdamond ownership of its refresh schedule, so refresh activity 
from one kdamond can no longer interfere with another.

I also checked the initialization path: the new field is zero-initialized 
by kzalloc_obj(), and damon_sysfs_turn_damon_on() sets it to 
jiffies + refresh_ms before registering the callback via damon_call().

Reviewed-by: Kunwu Chan <kunwu.chan@gmail.com>

Thanks,
Kunwu

>  
>  	repeat_call_control->fn = damon_sysfs_repeat_call_fn;
>  	repeat_call_control->data = kdamond;
> -- 
> 2.47.3
> 

Sent using hkml (https://github.com/sjp38/hackermail)

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit
  2026-09-03  8:45   ` Kunwu Chan
@ 2026-09-03 13:49     ` SJ Park
  0 siblings, 0 replies; 19+ messages in thread
From: SJ Park @ 2026-09-03 13:49 UTC (permalink / raw)
  To: Kunwu Chan
  Cc: SJ Park, Kunwu Chan, Andrew Morton, stable, Ravi Jonnalagadda,
	damon, linux-kernel, linux-mm

On Thu,  3 Sep 2026 16:45:45 +0800 Kunwu Chan <kunwu.chan@gmail.com> wrote:

> On Tue,  1 Sep 2026 17:27:22 -0700 SJ Park <sj@kernel.org> wrote:
[...]
> > This issue was discovered [1] by Sashiko.
> > 
> > [1] https://lore.kkernel.org/20260827045035.94611-1-sj@kernel.org
> 
> Hi SJ,
> 
> The fix is consistent with the other node-based quota metrics. I also 
> checked that damos_get_node_eligible_mem_bp() directly uses goal->nid 
> when calculating the current value.
> 
> One minor typo: the [1] has an extra k (kkernel should be kernel).

Nice catch, thank you Kunwu!  Andrew, could you please fix this when you pick
this series up?  Let me know if you prefer reposting the series with the fix.

> 
> Reviewed-by: Kunwu Chan <kunwu.chan@gmail.com>

Appreciate all the reviews, Kunwu!


Thanks,
SJ

[...]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms
  2026-09-03  3:13 ` Lian Wang
@ 2026-09-03 13:51   ` SJ Park
  0 siblings, 0 replies; 19+ messages in thread
From: SJ Park @ 2026-09-03 13:51 UTC (permalink / raw)
  To: Lian Wang
  Cc: SJ Park, Andrew Morton, stable, Brendan Higgins, David Gow,
	Quanmin Yan, Ravi Jonnalagadda, damon, kunit-dev, linux-kernel,
	linux-kselftest, linux-mm

On Thu,  3 Sep 2026 11:13:28 +0800 Lian Wang <lianux.mm@gmail.com> wrote:

> Hi SJ,
> 
> > 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.
> 
> I reviewed and tested the integrated four-patch series in mm-new:
> 
>   a20e38846efb..72dc8fdaae62
> 
> The integrated commits had stable patch-ids matching the posted patches.
> DAMON KUnit passed 42/42 tests.  I also ran test-only eligible_mem_bp nid and
> two-kdamond refresh-isolation regression cases; they passed 42/42 and 43/43
> tests, respectively.  Both test-only changes were then removed and the
> worktree returned clean at the unchanged series tip.
> 
> These were functional UML KUnit tests.  The fixes look good to me.
> 
> For the series:
> 
> Reviewed-by: Lian Wang <lianux.mm@gmail.com>
> Tested-by: Lian Wang <lianux.mm@gmail.com>

Thank you for your review and test, Lian!


Thanks,
SJ

[...]

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-09-03 13:51 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-02  0:27 [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
2026-09-02  0:27 ` [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal SJ Park
2026-09-02  0:34   ` sashiko-bot
2026-09-03  8:33   ` Kunwu Chan
2026-09-02  0:27 ` [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total SJ Park
2026-09-02  0:34   ` sashiko-bot
2026-09-03  9:23   ` Kunwu Chan
2026-09-02  0:27 ` [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit SJ Park
2026-09-02  0:39   ` sashiko-bot
2026-09-02  1:13     ` SJ Park
2026-09-02  1:14   ` SJ Park
2026-09-03  8:45   ` Kunwu Chan
2026-09-03 13:49     ` SJ Park
2026-09-02  0:27 ` [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context SJ Park
2026-09-02  0:39   ` sashiko-bot
2026-09-03 10:01   ` Kunwu Chan
2026-09-02  1:16 ` [PATCH 0/4] mm/damon: fix misc bugs in kunit, quota goals and sysfs refresh_ms SJ Park
2026-09-03  3:13 ` Lian Wang
2026-09-03 13:51   ` SJ Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox