mm/damon: fix watermark state, kunit pid leak and selftest fake pass
Subsystems (4)
SJ Park
maintainercover mm/damon: fix watermark state, kunit pid leak and selftest fake pass
Three hotfixes for core, kunit and selftest. Fix kdamond deactivation from missed watermark reset with patch 1. Fix pid leak in kunit test with patch 2. Fix a selftest wrongly passing when it should skip, with patch 3.
Note for reviewers. This series is a collection of fixes that were initially developed by different people. Changelog is in each patch.
Aveline Noir (1):
mm/damon/core: reset watermark wait state on each iteration
SJ Park (1):
selftests/damon/sysfs_memcg_path_leak: define ksft_skip
Wang Xiaolei (1):
mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets
test
mm/damon/core.c | 5 +++-- mm/damon/tests/sysfs-kunit.h | 9 +++++++++ tools/testing/selftests/damon/sysfs_memcg_path_leak.sh | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-)
base-commit: 395206c0e287bdd981918e033a2b1621ea6a3b9a
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 1/3 mm/damon/core: reset watermark wait state on each iteration
From: Aveline Noir <[email protected]>
Found while reviewing the DAMON source code that if an online commit removes all schemes while kdamond is waiting for watermark activation, the scheme loop becomes empty but min_wait_time keeps the previous positive value. kdamond then keeps sleeping instead of resuming monitoring without schemes.
The way to wake up the kdamond again without DAMON is bit complicated. Install a scheme having watermarks that can activate the kdamond again, and remove the scheme again. Doable but cumbersome and complicated.
Initialize min_wait_time and init_wait_time on each loop iteration so the wait is calculated from the current schemes and an empty scheme list resumes monitoring.
---
Changes from v1
- v1: https://lore.kernel.org/179277129124.2.16132047926074873101.damon-empty-schemes@gmail.com
- Collect R-b: from SJ.
- Add how to work around the problem.
- Update Fixes: and add Cc: stable@.
- Rebase to latest mm-new.
mm/damon/core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/damon/core.c +3 -2
@@ -3976,10 +3976,11 @@ static int kdamond_wait_activation(struct damon_ctx *ctx) {struct damos *s;unsigned long wait_time; -unsigned long min_wait_time =0 ; -bool init_wait_time = false;while (!kdamond_need_stop (ctx)) { +unsigned long min_wait_time =0 ; +bool init_wait_time = false; +damon_for_each_scheme (s, ctx) { wait_time =damos_wmark_wait_us (s);if (!init_wait_time || wait_time < min_wait_time) {
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH mm-hotfixes-unstable 1/3] mm/damon/core: reset watermark wait state on each iteration
Sashiko has reviewed this patch and found no issues.
Quoted 16 lines
From: Aveline Noir <[email protected]>
Found while reviewing the DAMON source code that if an online commit removes all schemes while kdamond is waiting for watermark activation, the scheme loop becomes empty but
min_wait_timekeeps the previous positive value. kdamond then keeps sleeping instead of resuming monitoring without schemes.The way to wake up the kdamond again without DAMON is bit complicated. Install a scheme having watermarks that can activate the kdamond again, and remove the scheme again. Doable but cumbersome and complicated.
Initialize
min_wait_timeandinit_wait_timeon each loop iteration so the wait is calculated from the current schemes and an empty scheme list resumes monitoring.
Quoted 6 lines
Fixes da87878010e5 ("mm/damon/sysfs: support online inputs update")Cc <[email protected]> # 5.19.xAssisted-by LLMSigned-off-by Aveline Noir <[email protected]>Reviewed-by SJ Park <[email protected]>Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerPATCH 2/3 mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
In reply to SJ ParkFrom: Wang Xiaolei <[email protected]>
damon_sysfs_test_add_targets() creates its monitoring context with damon_new_ctx(), which leaves ctx->ops zero-initialized. Since DAMON_OPS_VADDR is zero, damon_target_has_pid() returns true for the context, and damon_sysfs_add_targets() therefore calls damon_set_target_pid() for each target it adds. That function grabs a reference to the target's struct pid via find_get_pid().
The test then tears the context down with damon_destroy_ctx(), which releases such a reference from ctx->ops.cleanup_target(). For the vaddr operations set that callback is damon_va_cleanup_target(), and it is the only place where the reference is put. The test never selects an operations set, though, so cleanup_target is NULL and both references -- one for each of the two damon_sysfs_add_targets() calls -- are leaked on every run of the test. Running the test with CONFIG_DEBUG_KMEMLEAK enabled reports the leaked struct pid objects as unreferenced.
Fix this by selecting the vaddr operations set for the context before adding the targets, mirroring what the sysfs interface does. damon_select_ops() fails if the vaddr operations set is not registered, in which case the test cannot exercise the pid handling and skips itself.
---
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Add Cc: stable@.
- Collect R-b: from SJ.
- Rebase to latest mm-new.
mm/damon/tests/sysfs-kunit.h | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/damon/tests/sysfs-kunit.h +9 -0
@@ -79,6 +79,15 @@ static void damon_sysfs_test_add_targets(struct kunit *test)kunit_skip (test,"ctx alloc fail" ); } +if (damon_select_ops (ctx,DAMON_OPS_VADDR )) { +damon_destroy_ctx (ctx); +kfree (sysfs_targets->targets_arr); +kfree (sysfs_targets); +kfree (sysfs_target->regions); +kfree (sysfs_target); +kunit_skip (test,"vaddr ops not registered" ); + } +damon_sysfs_add_targets (ctx, sysfs_targets);KUNIT_EXPECT_EQ (test,1u ,nr_damon_targets (ctx));
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH mm-hotfixes-unstable 2/3] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines
From: Wang Xiaolei <[email protected]>
damon_sysfs_test_add_targets()creates its monitoring context withdamon_new_ctx(), which leavesctx->opszero-initialized. SinceDAMON_OPS_VADDRis zero,damon_target_has_pid()returns true for the context, anddamon_sysfs_add_targets()therefore callsdamon_set_target_pid()for each target it adds. That function grabs a reference to the target's struct pid viafind_get_pid().The test then tears the context down with
damon_destroy_ctx(), which releases such a reference fromctx->ops.cleanup_target(). For the vaddr operations set that callback isdamon_va_cleanup_target(), and it is the only place where the reference is put. The test never selects an operations set, though, socleanup_targetis NULL and both references -- one for each of the twodamon_sysfs_add_targets()calls -- are leaked on
Quoted 6 lines
[ ... ]Cc Vlastimil Babka <[email protected]>Cc "Liam R. Howlett" <[email protected]>Signed-off-by Wang Xiaolei <[email protected]>Reviewed-by SJ Park <[email protected]>Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SJ Park
maintainerPATCH 3/3 selftests/damon/sysfs_memcg_path_leak: define ksft_skip
In reply to SJ Parksysfs_memcg_path_leak test uses $ksft_skip without defining it. As a result, the test is marked as passed while it was skipped, like below.
$ ./sysfs_memcg_path_leak.sh
Run as root
$ echo $?
0Users who run the test could therefore wrongly believe the test was run and passed while it was skipped.
Define the value at the beginning of the test. After this patch, the exit code reflects the skip.
$ ./sysfs_memcg_path_leak.sh
Run as root
$ echo $?
4---
Changes from RFC
- RFC: https://lore.kernel.org/[email protected]
- Drop RFC tag.
- Rebase to latest mm-new.
tools/testing/selftests/damon/sysfs_memcg_path_leak.sh | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/damon/sysfs_memcg_path_leak.sh +3 -0
@@ -1,6 +1,9 @@ #!/bin/bash # SPDX-License-Identifier: GPL-2.0 +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + if [ $EUID -ne 0 ] then echo "Run as root"
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH mm-hotfixes-unstable 3/3] selftests/damon/sysfs_memcg_path_leak: define ksft_skip
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines
sysfs_memcg_path_leaktest uses $ksft_skip without defining it. As a result, the test is marked as passed while it was skipped, like below.$ ./sysfs_memcg_path_leak.sh Run as root $ echo $? 0Users who run the test could therefore wrongly believe the test was run and passed while it was skipped.
Define the value at the beginning of the test. After this patch, the exit code reflects the skip.
$ ./sysfs_memcg_path_leak.sh Run as root $ echo $? 4
Quoted 6 lines
Fixes dfa3cf0bc018 ("selftets/damon: add a test for memcg_path leak")Cc <[email protected]> # 6.17.xReviewed-by Aveline Noir <[email protected]>Tested-by Aveline Noir <[email protected]>Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3