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