mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
Subsystems (3)
PATCH v2 1/1 mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
From: 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 in v2:
- Fix ctx leak in the vaddr-not-registered error path (reported by Sashiko AI review)
- Link to v1:
https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
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.53.0
[email protected]
AI reviewSashiko 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 Michal Hocko <[email protected]>Cc Mike Rapoport <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Vlastimil Babka <[email protected]>Signed-off-by Wang Xiaolei <[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
Hello Xiaolei,
Quoted 18 lines · On Thu, 29 Oct 2026 15:56:09 +0800 Xiaolei Wang <[email protected]>…
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 every run of the test. Running the test withCONFIG_DEBUG_KMEMLEAKenabled reports the leaked struct pid objects as unreferenced.
Quoted 5 lines
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.
Makes sense. Thank you for finding and fixing this.
Quoted 2 lines
Fixes b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")
The impact is leak. I think we should add Cc: stable@.
Quoted 6 lines
Cc Liam R. Howlett <[email protected]>Cc Michal Hocko <[email protected]>Cc Mike Rapoport <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Vlastimil Babka <[email protected]>Signed-off-by Wang Xiaolei <[email protected]>
Other than Cc: stable@,
Assuming you agree to Cc stable@, I applied this patch to damon/next [1] tree after adding the Cc. I will repost it next Monday, as an mm.git merge request. So, no action from your side is needed for now.
[1] https://origin.kernel.org/doc/html/latest/mm/damon/maintainer-profile.html#scm-trees
Thanks,
SJ
[...]
Quoted 28 lines · SJ Park
Hello Xiaolei,
Quoted 18 lines · On Thu, 29 Oct 2026 15:56:09 +0800 Xiaolei Wang <[email protected]>…
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 every run of the test. Running the test withCONFIG_DEBUG_KMEMLEAKenabled reports the leaked struct pid objects as unreferenced.Quoted 5 lines
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.
Quoted 6 lines
Makes sense. Thank you for finding and fixing this.
Quoted 2 lines
Fixes b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")The impact is leak. I think we should add Cc: stable@.
I wouldn't bother Cc stable for unit tests. It's not something which affects production or real life at all.
I used to be a be more involved in testing and the rule was that you pulled the tests from the latest kernel and ran them against the stable kernels. It feels like that can't make a lot of sense for unit testing so maybe for unit testing we didn't do that. I can't remember...
regards,
dan carpenter
SJ Park
maintainerQuoted 32 lines · On Thu, 29 Oct 2026 18:18:05 +0300 Dan Carpenter <[email protected]> wrote:
Quoted 31 lines · SJ Park
Hello Xiaolei,
Quoted 18 lines · On Thu, 29 Oct 2026 15:56:09 +0800 Xiaolei Wang <[email protected]>…
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 every run of the test. Running the test withCONFIG_DEBUG_KMEMLEAKenabled reports the leaked struct pid objects as unreferenced.Quoted 5 lines
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.Makes sense. Thank you for finding and fixing this.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Fixes b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")The impact is leak. I think we should add Cc: stable@.
I wouldn't bother Cc stable for unit tests. It's not something which affects production or real life at all.
Thank you for your input, Dan!
I have no strong opinion. But we got CVEs [1] for this class of unit test code in the past. I'd like to keep it consistent to the past for now.
[1] https://lore.kernel.org/2024102930-CVE-2024-50068-2e3c@gregkh/
Thanks,
SJ
[...]