From: Sailesh Nandanavanam Add a KUnit test to verify that damos_walk() rejects new requests when walk_control_obsolete is set. Commit 33c3f6c2b48c ("mm/damon/core: fix damos_walk() vs kdamond_fn() exit race") introduced walk_control_obsolete to prevent a race condition where new requests could be registered during kdamond shutdown and never handled. This test simulates the shutdown condition by setting walk_control_obsolete and verifies that damos_walk() returns -ECANCELED immediately. This validates the invariant introduced by the fix and helps prevent regressions. Link: https://patch.msgid.link/20260612062337.2459-1-saileshnandanavanam@gmail.com Suggested-by: SJ Park Signed-off-by: Sailesh Nandanavanam Reviewed-by: SJ Park Signed-off-by: SJ Park --- Changes from v3 - v3: https://lore.kernel.org/20260612062337.2459-1-saileshnandanavanam@gmail.com - Collect R-b: from SJ. - Rebase to latest mm-new. Changes since v2 - v2: https://lore.kernel.org/20260524100258.36819-1-saileshnandanavanam@gmail.com - Dropped the userspace selftest approach entirely. SJ tested v2 100 times on a kernel with the fix reverted and it always passed, confirming the microsecond-wide race window cannot be reliably hit from userspace due to syscall overhead. - Added a KUnit test for damos_walk() + walk_control_obsolete instead, as suggested by SJ. This directly sets the obsolete flag and verifies damos_walk() returns -ECANCELED immediately, without timing dependency. Changes since v1 - v1: https://lore.kernel.org/20260524091812.35283-1-saileshnandanavanam@gmail.com - Addressed sashiko bot review comments (execute bit, threading, dynamic sysfs path, OSError handling) - superseded by the v3 approach above. mm/damon/tests/core-kunit.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h index fcf7c7fadb5fe..c5f5124c3d1f4 100644 --- a/mm/damon/tests/core-kunit.h +++ b/mm/damon/tests/core-kunit.h @@ -1456,6 +1456,33 @@ static void damon_test_is_last_region(struct kunit *test) damon_free_target(t); } +/* + * Verify that damos_walk() rejects new requests when + * walk_control_obsolete is set. + * + * This tests the invariant introduced by: + * commit 33c3f6c2b48c ("mm/damon/core: fix damos_walk() vs kdamond_fn() exit race") + */ +static void damon_test_walk_control_obsolete(struct kunit *test) +{ + struct damon_ctx *ctx; + struct damos_walk_control control = {}; + int ret; + + ctx = damon_new_ctx(); + if (!ctx) + kunit_skip(test, "ctx alloc fail"); + + /* Simulate shutdown phase */ + ctx->walk_control_obsolete = true; + + ret = damos_walk(ctx, &control); + + KUNIT_EXPECT_EQ(test, ret, -ECANCELED); + + damon_destroy_ctx(ctx); +} + static struct kunit_case damon_test_cases[] = { KUNIT_CASE(damon_test_target), KUNIT_CASE(damon_test_regions), @@ -1485,6 +1512,7 @@ static struct kunit_case damon_test_cases[] = { KUNIT_CASE(damon_test_set_filters_default_reject), KUNIT_CASE(damon_test_apply_min_nr_regions), KUNIT_CASE(damon_test_is_last_region), + KUNIT_CASE(damon_test_walk_control_obsolete), {}, }; -- 2.47.3