1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Basc test for cpuset v1 interfaces write/read
5#
6
7skip_test() {
8	echo "$1"
9	echo "Test SKIPPED"
10	exit 4 # ksft_skip
11}
12
13write_test() {
14	dir=$1
15	interface=$2
16	value=$3
17	original=$(cat $dir/$interface)
18	echo "testing $interface $value"
19	echo $value > $dir/$interface
20	new=$(cat $dir/$interface)
21	[[ $value -ne $(cat $dir/$interface) ]] && {
22		echo "$interface write $value failed: new:$new"
23		exit 1
24	}
25}
26
27[[ $(id -u) -eq 0 ]] || skip_test "Test must be run as root!"
28
29# Find cpuset v1 mount point
30CPUSET=$(mount -t cgroup | grep cpuset | head -1 | awk '{print $3}')
31[[ -n "$CPUSET" ]] || skip_test "cpuset v1 mount point not found!"
32
33#
34# Create a test cpuset, read write test
35#
36TDIR=test$$
37[[ -d $CPUSET/$TDIR ]] || mkdir $CPUSET/$TDIR
38
39ITF_MATRIX=(
40	#interface			value		expect 	root_only
41	'cpuset.cpus			0-1		0-1	0'
42	'cpuset.mem_exclusive		1		1	0'
43	'cpuset.mem_exclusive		0		0	0'
44	'cpuset.mem_hardwall		1		1	0'
45	'cpuset.mem_hardwall		0		0	0'
46	'cpuset.memory_migrate		1		1	0'
47	'cpuset.memory_migrate		0		0	0'
48	'cpuset.memory_spread_page	1		1	0'
49	'cpuset.memory_spread_page	0		0	0'
50	'cpuset.memory_spread_slab	1		1	0'
51	'cpuset.memory_spread_slab	0		0	0'
52	'cpuset.mems			0		0	0'
53	'cpuset.sched_load_balance	1		1	0'
54	'cpuset.sched_load_balance	0		0	0'
55	'cpuset.sched_relax_domain_level	2	2	0'
56	'cpuset.memory_pressure_enabled	1		1	1'
57	'cpuset.memory_pressure_enabled	0		0	1'
58)
59
60run_test()
61{
62	cnt="${ITF_MATRIX[@]}"
63	for i in "${ITF_MATRIX[@]}" ; do
64		args=($i)
65		root_only=${args[3]}
66		[[ $root_only -eq 1 ]] && {
67			write_test "$CPUSET" "${args[0]}" "${args[1]}" "${args[2]}"
68			continue
69		}
70		write_test "$CPUSET/$TDIR" "${args[0]}" "${args[1]}" "${args[2]}"
71	done
72}
73
74run_test
75rmdir $CPUSET/$TDIR
76echo "Test PASSED"
77exit 0
78