* [PATCH v2] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
@ 2026-10-29 7:56 Xiaolei Wang
2026-10-29 14:10 ` SJ Park
0 siblings, 1 reply; 3+ messages in thread
From: Xiaolei Wang @ 2026-10-29 7:56 UTC (permalink / raw)
To: sj, akpm, david, ljs, error27, xiaoleiwangxiaolei
Cc: damon, linux-mm, linux-kernel, Liam R. Howlett, Michal Hocko,
Mike Rapoport, Suren Baghdasaryan, Vlastimil Babka
From: Wang Xiaolei <xiaoleiwangxiaolei@gmail.com>
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.
Fixes: b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Signed-off-by: Wang Xiaolei <xiaoleiwangxiaolei@gmail.com>
---
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/20261029015725.17378-1-xiaoleiwangxiaolei@gmail.com/
mm/damon/tests/sysfs-kunit.h | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/mm/damon/tests/sysfs-kunit.h b/mm/damon/tests/sysfs-kunit.h
index 138a4b8d14e7..16373ba751db 100644
--- a/mm/damon/tests/sysfs-kunit.h
+++ b/mm/damon/tests/sysfs-kunit.h
@@ -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));
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
2026-10-29 7:56 [PATCH v2] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test Xiaolei Wang
@ 2026-10-29 14:10 ` SJ Park
2026-10-29 15:18 ` Dan Carpenter
0 siblings, 1 reply; 3+ messages in thread
From: SJ Park @ 2026-10-29 14:10 UTC (permalink / raw)
To: Xiaolei Wang
Cc: SJ Park, akpm, david, ljs, error27, damon, linux-mm,
linux-kernel, Liam R. Howlett, Michal Hocko, Mike Rapoport,
Suren Baghdasaryan, Vlastimil Babka, stable
Hello Xiaolei,
On Thu, 29 Oct 2026 15:56:09 +0800 Xiaolei Wang <xiaoleiwangxiaolei@gmail.com> wrote:
> From: Wang Xiaolei <xiaoleiwangxiaolei@gmail.com>
>
> 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.
Makes sense. Thank you for finding and fixing this.
>
> 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@.
Cc: <stable@vger.kernel.org> # 6.7.x
> Cc: Liam R. Howlett <liam@infradead.org>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Suren Baghdasaryan <surenb@google.com>
> Cc: Vlastimil Babka <vbabka@kernel.org>
> Signed-off-by: Wang Xiaolei <xiaoleiwangxiaolei@gmail.com>
Other than Cc: stable@,
Reviewed-by: SJ Park <sj@kernel.org>
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
[...]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test
2026-10-29 14:10 ` SJ Park
@ 2026-10-29 15:18 ` Dan Carpenter
0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2026-10-29 15:18 UTC (permalink / raw)
To: SJ Park
Cc: Xiaolei Wang, akpm, david, ljs, damon, linux-mm, linux-kernel,
Liam R. Howlett, Michal Hocko, Mike Rapoport, Suren Baghdasaryan,
Vlastimil Babka, stable
On Thu, Oct 29, 2026 at 07:10:40AM -0700, SJ Park wrote:
> Hello Xiaolei,
>
> On Thu, 29 Oct 2026 15:56:09 +0800 Xiaolei Wang <xiaoleiwangxiaolei@gmail.com> wrote:
>
> > From: Wang Xiaolei <xiaoleiwangxiaolei@gmail.com>
> >
> > 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.
>
> Makes sense. Thank you for finding and fixing this.
>
> >
> > 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-29 15:18 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-29 7:56 [PATCH v2] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test Xiaolei Wang
2026-10-29 14:10 ` SJ Park
2026-10-29 15:18 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®