[RFC PATCH v1.3 03/11] mm/damon/tests/core-kunit: test damon_rand()

From: SeongJae Park

Date: Thu Jun 25 2026 - 20:17:37 EST


Commit 9012c4e647df ("mm/damon: replace damon_rand() with a per-ctx
lockless PRNG") optimized DAMON for better performance. Add a kunit
test for ensuring the bounds of the output.

Signed-off-by: SeongJae Park <sj@xxxxxxxxxx>
---
mm/damon/tests/core-kunit.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 1cfb8c176b873..282670b0fa908 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -1460,6 +1460,20 @@ static void damon_test_is_last_region(struct kunit *test)
damon_free_target(t);
}

+static void damon_test_rand(struct kunit *test)
+{
+ struct damon_ctx ctx;
+ int i;
+
+ prandom_seed_state(&ctx.rnd_state, get_random_u64());
+ for (i = 0; i < 10000; i++) {
+ unsigned long rnd = damon_rand(&ctx, 0, 10);
+
+ KUNIT_EXPECT_GE(test, rnd, 0);
+ KUNIT_EXPECT_LE(test, rnd, 9);
+ }
+}
+
static struct kunit_case damon_test_cases[] = {
KUNIT_CASE(damon_test_target),
KUNIT_CASE(damon_test_regions),
@@ -1489,6 +1503,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_rand),
{},
};

--
2.47.3