Skip to content

Commit b8ee557

Browse files
sjp38akpm00
authored andcommitted
mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()
damon_sysfs_set_targets() had a bug that can result in unexpected memory usage and monitoring overhead increase. The bug has fixed by a previous commit. Add a unit test for avoiding a similar bug of future. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: SeongJae Park <[email protected]> Cc: Brendan Higgins <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 62f76a7 commit b8ee557

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

mm/damon/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ config DAMON_SYSFS
5959
This builds the sysfs interface for DAMON. The user space can use
6060
the interface for arbitrary data access monitoring.
6161

62+
config DAMON_SYSFS_KUNIT_TEST
63+
bool "Test for damon debugfs interface" if !KUNIT_ALL_TESTS
64+
depends on DAMON_SYSFS && KUNIT=y
65+
default KUNIT_ALL_TESTS
66+
help
67+
This builds the DAMON sysfs interface Kunit test suite.
68+
69+
For more information on KUnit and unit tests in general, please refer
70+
to the KUnit documentation.
71+
72+
If unsure, say N.
73+
6274
config DAMON_DBGFS
6375
bool "DAMON debugfs interface (DEPRECATED!)"
6476
depends on DAMON_VADDR && DAMON_PADDR && DEBUG_FS

mm/damon/sysfs-test.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Data Access Monitor Unit Tests
4+
*
5+
* Author: SeongJae Park <[email protected]>
6+
*/
7+
8+
#ifdef CONFIG_DAMON_SYSFS_KUNIT_TEST
9+
10+
#ifndef _DAMON_SYSFS_TEST_H
11+
#define _DAMON_SYSFS_TEST_H
12+
13+
#include <kunit/test.h>
14+
15+
static unsigned int nr_damon_targets(struct damon_ctx *ctx)
16+
{
17+
struct damon_target *t;
18+
unsigned int nr_targets = 0;
19+
20+
damon_for_each_target(t, ctx)
21+
nr_targets++;
22+
23+
return nr_targets;
24+
}
25+
26+
static int __damon_sysfs_test_get_any_pid(int min, int max)
27+
{
28+
struct pid *pid;
29+
int i;
30+
31+
for (i = min; i <= max; i++) {
32+
pid = find_get_pid(i);
33+
if (pid) {
34+
put_pid(pid);
35+
return i;
36+
}
37+
}
38+
return -1;
39+
}
40+
41+
static void damon_sysfs_test_set_targets(struct kunit *test)
42+
{
43+
struct damon_sysfs_targets *sysfs_targets;
44+
struct damon_sysfs_target *sysfs_target;
45+
struct damon_ctx *ctx;
46+
47+
sysfs_targets = damon_sysfs_targets_alloc();
48+
sysfs_targets->nr = 1;
49+
sysfs_targets->targets_arr = kmalloc_array(1,
50+
sizeof(*sysfs_targets->targets_arr), GFP_KERNEL);
51+
52+
sysfs_target = damon_sysfs_target_alloc();
53+
sysfs_target->pid = __damon_sysfs_test_get_any_pid(12, 100);
54+
sysfs_target->regions = damon_sysfs_regions_alloc();
55+
sysfs_targets->targets_arr[0] = sysfs_target;
56+
57+
ctx = damon_new_ctx();
58+
59+
damon_sysfs_set_targets(ctx, sysfs_targets);
60+
KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx));
61+
62+
sysfs_target->pid = __damon_sysfs_test_get_any_pid(
63+
sysfs_target->pid + 1, 200);
64+
damon_sysfs_set_targets(ctx, sysfs_targets);
65+
KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx));
66+
67+
damon_destroy_ctx(ctx);
68+
kfree(sysfs_targets->targets_arr);
69+
kfree(sysfs_targets);
70+
kfree(sysfs_target);
71+
}
72+
73+
static struct kunit_case damon_sysfs_test_cases[] = {
74+
KUNIT_CASE(damon_sysfs_test_set_targets),
75+
{},
76+
};
77+
78+
static struct kunit_suite damon_sysfs_test_suite = {
79+
.name = "damon-sysfs",
80+
.test_cases = damon_sysfs_test_cases,
81+
};
82+
kunit_test_suite(damon_sysfs_test_suite);
83+
84+
#endif /* _DAMON_SYSFS_TEST_H */
85+
86+
#endif /* CONFIG_DAMON_SYSFS_KUNIT_TEST */

mm/damon/sysfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,3 +1836,5 @@ static int __init damon_sysfs_init(void)
18361836
return err;
18371837
}
18381838
subsys_initcall(damon_sysfs_init);
1839+
1840+
#include "sysfs-test.h"

0 commit comments

Comments
 (0)