Skip to contentSkip to conversation

mm/damon: fix watermark state, kunit pid leak and selftest fake pass

PATCH Applied SJ Park 2 participants 7 messages Nov 2

Subsystems (4)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

cover 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
author maintainer
#1 lore raw

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

Fixes da87878010e5 ("mm/damon/sysfs: support online inputs update")
Cc <[email protected]> # 5.19.x
Assisted-by LLM

---
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
index 73b571b429cb3..7441cdea57dc5 100644
@@ -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
author maintainer
#3 lore raw

PATCH 2/3 mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test

In reply to SJ Park

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.

Fixes b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")
Cc <[email protected]> # 6.7.x
Cc Michal Hocko <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc "Liam R. Howlett" <[email protected]>

---
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
index 138a4b8d14e73..16373ba751dbc 100644
@@ -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
author maintainer
#5 lore raw

PATCH 3/3 selftests/damon/sysfs_memcg_path_leak: define ksft_skip

In reply to SJ Park

sysfs_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 $?
    0

Users 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
Fixes dfa3cf0bc018 ("selftets/damon: add a test for memcg_path leak")
Cc <[email protected]> # 6.17.x

---
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
index 34c37129c49fe..d407dddb879ec 100755
@@ -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
#6 lore raw

Re: [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_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 $?
    0

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#2 lore raw

Re: [PATCH mm-hotfixes-unstable 1/3] mm/damon/core: reset watermark wait state on each iteration

In reply to SJ Park
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_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.

Quoted 6 lines
Fixes da87878010e5 ("mm/damon/sysfs: support online inputs update")
Cc <[email protected]> # 5.19.x
Assisted-by LLM

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH mm-hotfixes-unstable 2/3] mm/damon/tests/sysfs-kunit: fix leaked pid reference in add_targets test

In reply to SJ Park
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 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

Quoted 6 lines
[ ... ]
Cc Vlastimil Babka <[email protected]>
Cc "Liam R. Howlett" <[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