From nobody Sat Sep 26 11:46:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EC872F49F6; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308854; cv=none; b=mhIb4XS7gq49WmfSElOZbBbml0xCqBdFXItkrX/Me77gnCB7v3sIUopeF2KQVoyURfFwkWhKxGKrROq2tI30s9MJQrYt7nMrztIlD8PfI9/GEQTH0bpM+PC1x897mgYEs+E4F19zv9m+IfLmxcLyaHTogVsdHo21mvpF4GJ6Lro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308854; c=relaxed/simple; bh=mblgMYWNs4C+qYuiFnh28aLwABP3tVSSD3iGEowOVSc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GG2Rh+o8nw8qXMay9lNXXm3RFOBGnDKUnXUs5M43TyN8ngZA6JioMZ+qlYZAqbNctiE4Ez880VIXeNPRwzn1vDws4p7VfIM/dk8wFxC7/YgSH6NdWb5wlLYqQHAdX1yV98ZCPKBBtF2m/u3Wd0r7O/eVoM3mpctLNfrwZX/ayRs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EBp/q+jh; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EBp/q+jh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBDDD1F00A3D; Wed, 2 Sep 2026 00:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788308853; bh=4Aq7mCS1n7SM9+gwhZmdlAUpokje7QH5p7CwEaALh/c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EBp/q+jhGmoX23PF/KR8yCsXY3rmPIUMsqyxq4V7hhR2ZmeEMYnHNawW7n+XfpQig B1G9yahk9AAoxz0k8lFwwlUpi380TZXoouESL8l8Il9BLlUTCRTtJf2W/jn5dVUywp 9eroAfIphosA0O9NWWvBEzwx++PuM+POU6kUJqq8pFOT191rb2OXS7JgYdnz6VWmwn Vyt2pKdhGytPl+2a/LsRz8Ch+GAtbWwtHxrCTo3xzwdwYa8PkzBcpoY7ZtlUko1wSc 52cWJM4A6MwNm+AYEKRCq2QYH2Ld46OUbmBLFGBcAh4i0SU6D8CwzCGHyC4+CdSwNY RLaCZ0mXnFshg== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, Brendan Higgins , David Gow , damon@lists.linux.dev, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/4] mm/damon/tests/core-kunit: test committing psi goal to psi goal Date: Tue, 1 Sep 2026 17:27:20 -0700 Message-ID: <20260902002725.108635-2-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260902002725.108635-1-sj@kernel.org> References: <20260902002725.108635-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_goa= l() test") Cc: # 6.19.x Signed-off-by: SJ Park Reviewed-by: Kunwu Chan Reviewed-by: Lian Wang Tested-by: Lian Wang --- 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 =3D 456, }; =20 + damos_test_commit_quota_goal_for(test, &dst, + &(struct damos_quota_goal) { + .metric =3D DAMOS_QUOTA_SOME_MEM_PSI_US, + .target_value =3D 234, + .current_value =3D 345, + .last_psi_total =3D 567, + }); damos_test_commit_quota_goal_for(test, &dst, &(struct damos_quota_goal){ .metric =3D DAMOS_QUOTA_USER_INPUT, --=20 2.47.3 From nobody Sat Sep 26 11:46:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9072A2F5324; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308854; cv=none; b=lSo6MXGPwwG6Znmmb7yiEk9Wthb5ZiosyBHQmVs0fvoU4KjzfGSOamN4H5h3VbKMJb9A5a72V9A0D1j582btj4MMZBR43oVv9oKMGgJ3pPwRabNftkTYIcLLz4bjaf/ZMei2tPhmNalJ3S90kf2mcu48mvy0WmMD6jubt34UPQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308854; c=relaxed/simple; bh=s7pVSJOEltTV/WyNYgejAsjWg5izNrR7PpH6dlVvvbA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MDcjH77dJ2JWTHdEINfWQbT2Wgk9HY6zwdna2jCrzLKVz1VsQ9Ligj6Bsq/7SEjFbT2exEj+K6j1MK1TwzlDxyzzf7ZoXy8LXVxX8UuCLUVAiUupjGFHKHefwM3fqg0reGc0PPoQ/9DdaayZQ961EjH6dvfT4ZN3FjgHMPAHdzQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JfbAthgR; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JfbAthgR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A6761F00A3E; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788308853; bh=SfHgIbwjiDjLtKHgMa5uADWtbVJn1DArTTqrj0I3xWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=JfbAthgRBHPFv6T+0PEu95zWODpJg5fDB1H2k4FMhtqoOLLiMJ4WncB991oUR7IWf PoEHUuFwqVCyxHyCL43BD+vOyEpBvs28GJTuudj+0ff3DSz0+Dgu0iSVUMwCy548zs cJLGko83qIa5PJGO1P43gFkkaWELE0yLE3f/ahXTI7H0q5aktc2lqymNmsMj3sUQAa pda74jc/AR36HHSFZbxGYMpD0BsZdmOVaz7k/ukUl9MuCMk5AbBdPpMMjoqC5M4iTd oYpbZcwwvb3q+cyDnq78rT+bjfGcIGO1tgnkphsdm5oDM8p62PWmXdMR1uu8BQk1Ud 2FeKTaWhFO7/w== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/4] mm/damon/core: handle uninitialized damos_quota_goal->last_psi_total Date: Tue, 1 Sep 2026 17:27:21 -0700 Message-ID: <20260902002725.108635-3-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260902002725.108635-1-sj@kernel.org> References: <20260902002725.108635-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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: # 6.9.x Signed-off-by: SJ Park Reviewed-by: Kunwu Chan Reviewed-by: Lian Wang Tested-by: Lian Wang --- 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 =3D metric; goal->target_value =3D target_value; + if (metric =3D=3D DAMOS_QUOTA_SOME_MEM_PSI_US) + goal->last_psi_total =3D 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 =3D U64_MAX; + break; case DAMOS_QUOTA_NODE_MEM_USED_BP: case DAMOS_QUOTA_NODE_MEM_FREE_BP: dst->nid =3D src->nid; @@ -1150,7 +1155,6 @@ static void damos_commit_quota_goal( dst->target_value =3D src->target_value; if (dst->metric =3D=3D DAMOS_QUOTA_USER_INPUT) dst->current_value =3D src->current_value; - /* keep last_psi_total as is, since it will be updated in next cycle */ damos_commit_quota_goal_union(dst, src); } =20 @@ -3039,7 +3043,12 @@ static void damos_set_quota_goal_current_value(struc= t damon_ctx *c, break; case DAMOS_QUOTA_SOME_MEM_PSI_US: now_psi_total =3D damos_get_some_mem_psi_total(); - goal->current_value =3D now_psi_total - goal->last_psi_total; + /* uninitialized last_psi_total; make no effect this round */ + if (goal->last_psi_total =3D=3D U64_MAX) + goal->current_value =3D goal->target_value; + else + goal->current_value =3D now_psi_total - + goal->last_psi_total; goal->last_psi_total =3D 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 k= unit *test, struct damos_quota_goal *dst, struct damos_quota_goal *src) { - u64 dst_last_psi_total =3D 0; - - if (dst->metric =3D=3D DAMOS_QUOTA_SOME_MEM_PSI_US) - dst_last_psi_total =3D dst->last_psi_total; damos_commit_quota_goal(dst, src); =20 KUNIT_EXPECT_EQ(test, dst->metric, src->metric); KUNIT_EXPECT_EQ(test, dst->target_value, src->target_value); if (src->metric =3D=3D DAMOS_QUOTA_USER_INPUT) KUNIT_EXPECT_EQ(test, dst->current_value, src->current_value); - if (dst_last_psi_total && src->metric =3D=3D 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); --=20 2.47.3 From nobody Sat Sep 26 11:46:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAFD72D8DBB; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308855; cv=none; b=cRVmV/lvpk6BlvmfhzvxIIJ85zXXnzf2tM8L8Lz21Vk9+fl2Vs3airD2I+VoO2tAfh5MeTCUtrTP4NOBTr84UDRVGXyowyjdoTwXVciufboGBD455BIYfULebEpP6NV+GMRMNXezV7/hW9y5rsBgqbsqKkA2VTc7uL1lFiyrAaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308855; c=relaxed/simple; bh=nM5ntWNruZ80VccSW5rjD3fIf32vH4ps/z73q2oxrgg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JIx/l2fXypS5hwscfJySCRDSRLO6zYCKgstmoBs15bxwUcs62LeRe4GYJDqqbANJc1KV/ahcAl10U8g2fiIjrg/jxoHqJkcKoU2kmPVr86/C43VFs7lhBAaSCpDh7HfkvkRVOPi9NXWiqlY24qnw7sw742MIRx0dGF55/vqbs2U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QacSCw1g; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QacSCw1g" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D5061F00A3F; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788308853; bh=KgVH3jXroigXUbQaVhPgGVZ8NdHiYPKQ6daDtsHhDVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QacSCw1gsI5N0HGKudxjhUx3D/hC8VbYXVePH9gDtXWWcKeurZ+Zf11T80nVvNKOM 7SK0Nbuhxra1HuTtRAazt2utA3ruJC9trhXfLzMIv329BicGcqudFTXUPS397jimdd xWK6HEcOMwsSRz8kabDpie08JwrvcbqVZAp7WUhpp2TjGJzCOnse8ufokprHt4QR57 EOeaEsL0cW496EETLYaOQePpD/0aVH+LRRIDxN3B842siNm8T8PT/VY210obNOS2hy a5wt5cs4S698A5jpWb7apPXmLDPiZLM/D/0qdKvbGGOoAVb6QZWNqyUsPPt3aG1b6W u6YcZcyESewvA== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, Ravi Jonnalagadda , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 3/4] mm/damon/core: copy nid for eligible_mem_bp damos quota goal commit Date: Tue, 1 Sep 2026 17:27:22 -0700 Message-ID: <20260902002725.108635-4-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260902002725.108635-1-sj@kernel.org> References: <20260902002725.108635-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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: # 7.2.x Signed-off-by: SJ Park Reviewed-by: Kunwu Chan Reviewed-by: Lian Wang Tested-by: Lian Wang --- 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 =3D src->nid; dst->memcg_id =3D src->memcg_id; break; + case DAMOS_QUOTA_NODE_ELIGIBLE_MEM_BP: + dst->nid =3D src->nid; + break; default: break; } --=20 2.47.3 From nobody Sat Sep 26 11:46:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A82B30C14A; Wed, 2 Sep 2026 00:27:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308855; cv=none; b=HpJbKNKarpgvpErepFZKFOP5oje543EQOe0R01QLqTJ80Nv9fk5MARU+9zwstFdy0ZfoRTh5kDGJHGKdSOMsfFMBohZBwJDhc6SrbsRc5alWjl1fj/Lw4HOZEL//fU7nuTQ9pRNRqsu6eY7rCU5fiUfPs0yMaGy3V37SXN+/uD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788308855; c=relaxed/simple; bh=ddJls6Z/ngnt9vZmX1K+spUcI8CLqjuVk9ZoDNPKseY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lF3EuASeVYQGZ47W95+v308uHU2jS4AsYa8M4E0gyL6DUmMrzs9lmvZ1E2Y39e0FRrHMVE1utbkoqM7kSb11/oUhyj7nvw7js3qStFDCOLDaA2/+4mwxCaZMjLqWhONRUpK7bLZWXEjQl/IgVwNERwVT+Bl7A86YiRlD0fEot9w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bqyf63ol; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bqyf63ol" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E916E1F00A3A; Wed, 2 Sep 2026 00:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788308854; bh=dPYnDusm6ZlAINNm2dD6FV62eCx2pxNkpvN0aJrj8uI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bqyf63oliUzCdLSCa2ShHyBHxtNTT/sxl0PpKRYUg0wgdCm+1RksqmlFUexq4GXUF sX0oaDNLD8cyUfONL3G62kOpS1QRAB4Y8xSj/ZLMh9X6K8b7PoPpgoSKWB9OkjNmfh 2p+nSviFi+R1qQBPfHzy9bMl7t+S+BWcahgF8O6yxb6s9mEsUp0ORv7HpriEBrIAzM MTmyd7Dx/zbTHGFecODtxBiKRaW4xovLK0r7jjzLjV5cmaOvtEZ60ZaNgFIF882pu9 9W/gOC7hyqT9D58C/CiPhF2EWdYh99cCrBuKoBu+Jv2pTwLqw8uCgmaTu62x2LO9TN NkUfnM38685mA== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, Quanmin Yan , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 4/4] mm/damon/sysfs: set next refresh jiffies per sysfs context Date: Tue, 1 Sep 2026 17:27:23 -0700 Message-ID: <20260902002725.108635-5-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260902002725.108635-1-sj@kernel.org> References: <20260902002725.108635-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 globa= l variable") Cc: # 6.18.x Signed-off-by: SJ Park Reviewed-by: Kunwu Chan Reviewed-by: Lian Wang Tested-by: Lian Wang --- 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; }; =20 static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void) @@ -2213,17 +2214,15 @@ static struct damon_ctx *damon_sysfs_build_ctx( return ctx; } =20 -static unsigned long damon_sysfs_next_update_jiffies; - static int damon_sysfs_repeat_call_fn(void *data) { struct damon_sysfs_kdamond *sysfs_kdamond =3D data; =20 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 =3D jiffies + + sysfs_kdamond->next_refresh_jiffies =3D jiffies + msecs_to_jiffies(sysfs_kdamond->refresh_ms); =20 if (!mutex_trylock(&damon_sysfs_lock)) @@ -2271,8 +2270,8 @@ static int damon_sysfs_turn_damon_on(struct damon_sys= fs_kdamond *kdamond) } kdamond->damon_ctx =3D ctx; =20 - damon_sysfs_next_update_jiffies =3D - jiffies + msecs_to_jiffies(kdamond->refresh_ms); + kdamond->next_refresh_jiffies =3D jiffies + + msecs_to_jiffies(kdamond->refresh_ms); =20 repeat_call_control->fn =3D damon_sysfs_repeat_call_fn; repeat_call_control->data =3D kdamond; --=20 2.47.3