Skip to content

Commit 0b7623b

Browse files
sjp38akpm00
authored andcommitted
selftests/damon: test removed scheme sysfs dir access bug
A DAMON sysfs user could start DAMON with a scheme, remove the sysfs directory for the scheme, and then ask stats or schemes tried regions update. The related logic were not aware of the already removed directory situation, so it was able to results in invalid memory accesses. The fix has made with commit 8468b48 ("mm/damon/sysfs-schemes: skip stats update if the scheme directory is removed"), though. Add a selftest to prevent such kinds of bugs from being introduced again. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: SeongJae Park <[email protected]> Cc: Shuah Khan <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c8c7016 commit 0b7623b

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

tools/testing/selftests/damon/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TEST_PROGS = debugfs_attrs.sh debugfs_schemes.sh debugfs_target_ids.sh
88
TEST_PROGS += debugfs_empty_targets.sh debugfs_huge_count_read_write.sh
99
TEST_PROGS += debugfs_duplicate_context_creation.sh
1010
TEST_PROGS += debugfs_rm_non_contexts.sh
11-
TEST_PROGS += sysfs.sh
11+
TEST_PROGS += sysfs.sh sysfs_update_removed_scheme_dir.sh
1212
TEST_PROGS += reclaim.sh lru_sort.sh
1313

1414
include ../lib.mk
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
# Kselftest framework requirement - SKIP code is 4.
5+
ksft_skip=4
6+
7+
if [ $EUID -ne 0 ]
8+
then
9+
echo "Run as root"
10+
exit $ksft_skip
11+
fi
12+
13+
damon_sysfs="/sys/kernel/mm/damon/admin"
14+
if [ ! -d "$damon_sysfs" ]
15+
then
16+
echo "damon sysfs not found"
17+
exit $ksft_skip
18+
fi
19+
20+
# clear log
21+
dmesg -C
22+
23+
# start DAMON with a scheme
24+
echo 1 > "$damon_sysfs/kdamonds/nr_kdamonds"
25+
echo 1 > "$damon_sysfs/kdamonds/0/contexts/nr_contexts"
26+
echo "vaddr" > "$damon_sysfs/kdamonds/0/contexts/0/operations"
27+
echo 1 > "$damon_sysfs/kdamonds/0/contexts/0/targets/nr_targets"
28+
echo $$ > "$damon_sysfs/kdamonds/0/contexts/0/targets/0/pid_target"
29+
echo 1 > "$damon_sysfs/kdamonds/0/contexts/0/schemes/nr_schemes"
30+
scheme_dir="$damon_sysfs/kdamonds/0/contexts/0/schemes/0"
31+
echo 4096000 > "$scheme_dir/access_pattern/sz/max"
32+
echo 20 > "$scheme_dir/access_pattern/nr_accesses/max"
33+
echo 1024 > "$scheme_dir/access_pattern/age/max"
34+
echo "on" > "$damon_sysfs/kdamonds/0/state"
35+
sleep 0.3
36+
37+
# remove scheme sysfs dir
38+
echo 0 > "$damon_sysfs/kdamonds/0/contexts/0/schemes/nr_schemes"
39+
40+
# try to update stat of already removed scheme sysfs dir
41+
echo "update_schemes_stats" > "$damon_sysfs/kdamonds/0/state"
42+
if dmesg | grep -q BUG
43+
then
44+
echo "update_schemes_stats triggers a kernel bug"
45+
dmesg
46+
exit 1
47+
fi
48+
49+
# try to update tried regions of already removed scheme sysfs dir
50+
echo "update_schemes_tried_regions" > "$damon_sysfs/kdamonds/0/state"
51+
if dmesg | grep -q BUG
52+
then
53+
echo "update_schemes_tried_regions triggers a kernel bug"
54+
dmesg
55+
exit 1
56+
fi
57+
58+
echo "off" > "$damon_sysfs/kdamonds/0/state"

0 commit comments

Comments
 (0)