Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / cpu-hotplug / cpu-on-off-test.sh
blobf3a8933c12755f9593c1207a9d13d3d6a0d7999d
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 SYSFS=
6 prerequisite()
8 msg="skip all tests:"
10 if [ $UID != 0 ]; then
11 echo $msg must be run as root >&2
12 exit 0
15 taskset -p 01 $$
17 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
19 if [ ! -d "$SYSFS" ]; then
20 echo $msg sysfs is not mounted >&2
21 exit 0
24 if ! ls $SYSFS/devices/system/cpu/cpu* > /dev/null 2>&1; then
25 echo $msg cpu hotplug is not supported >&2
26 exit 0
29 echo "CPU online/offline summary:"
30 online_cpus=`cat $SYSFS/devices/system/cpu/online`
31 online_max=${online_cpus##*-}
33 if [[ "$online_cpus" = "$online_max" ]]; then
34 echo "$msg: since there is only one cpu: $online_cpus"
35 exit 0
38 echo -e "\t Cpus in online state: $online_cpus"
40 offline_cpus=`cat $SYSFS/devices/system/cpu/offline`
41 if [[ "a$offline_cpus" = "a" ]]; then
42 offline_cpus=0
43 else
44 offline_max=${offline_cpus##*-}
46 echo -e "\t Cpus in offline state: $offline_cpus"
50 # list all hot-pluggable CPUs
52 hotpluggable_cpus()
54 local state=${1:-.\*}
56 for cpu in $SYSFS/devices/system/cpu/cpu*; do
57 if [ -f $cpu/online ] && grep -q $state $cpu/online; then
58 echo ${cpu##/*/cpu}
60 done
63 hotplaggable_offline_cpus()
65 hotpluggable_cpus 0
68 hotpluggable_online_cpus()
70 hotpluggable_cpus 1
73 cpu_is_online()
75 grep -q 1 $SYSFS/devices/system/cpu/cpu$1/online
78 cpu_is_offline()
80 grep -q 0 $SYSFS/devices/system/cpu/cpu$1/online
83 online_cpu()
85 echo 1 > $SYSFS/devices/system/cpu/cpu$1/online
88 offline_cpu()
90 echo 0 > $SYSFS/devices/system/cpu/cpu$1/online
93 online_cpu_expect_success()
95 local cpu=$1
97 if ! online_cpu $cpu; then
98 echo $FUNCNAME $cpu: unexpected fail >&2
99 exit 1
100 elif ! cpu_is_online $cpu; then
101 echo $FUNCNAME $cpu: unexpected offline >&2
102 exit 1
106 online_cpu_expect_fail()
108 local cpu=$1
110 if online_cpu $cpu 2> /dev/null; then
111 echo $FUNCNAME $cpu: unexpected success >&2
112 exit 1
113 elif ! cpu_is_offline $cpu; then
114 echo $FUNCNAME $cpu: unexpected online >&2
115 exit 1
119 offline_cpu_expect_success()
121 local cpu=$1
123 if ! offline_cpu $cpu; then
124 echo $FUNCNAME $cpu: unexpected fail >&2
125 exit 1
126 elif ! cpu_is_offline $cpu; then
127 echo $FUNCNAME $cpu: unexpected offline >&2
128 exit 1
132 offline_cpu_expect_fail()
134 local cpu=$1
136 if offline_cpu $cpu 2> /dev/null; then
137 echo $FUNCNAME $cpu: unexpected success >&2
138 exit 1
139 elif ! cpu_is_online $cpu; then
140 echo $FUNCNAME $cpu: unexpected offline >&2
141 exit 1
145 error=-12
146 allcpus=0
147 priority=0
148 online_cpus=0
149 online_max=0
150 offline_cpus=0
151 offline_max=0
153 while getopts e:ahp: opt; do
154 case $opt in
156 error=$OPTARG
159 allcpus=1
162 echo "Usage $0 [ -a ] [ -e errno ] [ -p notifier-priority ]"
163 echo -e "\t default offline one cpu"
164 echo -e "\t run with -a option to offline all cpus"
165 exit
168 priority=$OPTARG
170 esac
171 done
173 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
174 echo "error code must be -4095 <= errno < 0" >&2
175 exit 1
178 prerequisite
181 # Safe test (default) - offline and online one cpu
183 if [ $allcpus -eq 0 ]; then
184 echo "Limited scope test: one hotplug cpu"
185 echo -e "\t (leaves cpu in the original state):"
186 echo -e "\t online to offline to online: cpu $online_max"
187 offline_cpu_expect_success $online_max
188 online_cpu_expect_success $online_max
190 if [[ $offline_cpus -gt 0 ]]; then
191 echo -e "\t offline to online to offline: cpu $offline_max"
192 online_cpu_expect_success $offline_max
193 offline_cpu_expect_success $offline_max
195 exit 0
196 else
197 echo "Full scope test: all hotplug cpus"
198 echo -e "\t online all offline cpus"
199 echo -e "\t offline all online cpus"
200 echo -e "\t online all offline cpus"
204 # Online all hot-pluggable CPUs
206 for cpu in `hotplaggable_offline_cpus`; do
207 online_cpu_expect_success $cpu
208 done
211 # Offline all hot-pluggable CPUs
213 for cpu in `hotpluggable_online_cpus`; do
214 offline_cpu_expect_success $cpu
215 done
218 # Online all hot-pluggable CPUs again
220 for cpu in `hotplaggable_offline_cpus`; do
221 online_cpu_expect_success $cpu
222 done
225 # Test with cpu notifier error injection
228 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
229 NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/cpu
231 prerequisite_extra()
233 msg="skip extra tests:"
235 /sbin/modprobe -q -r cpu-notifier-error-inject
236 /sbin/modprobe -q cpu-notifier-error-inject priority=$priority
238 if [ ! -d "$DEBUGFS" ]; then
239 echo $msg debugfs is not mounted >&2
240 exit 0
243 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
244 echo $msg cpu-notifier-error-inject module is not available >&2
245 exit 0
249 prerequisite_extra
252 # Offline all hot-pluggable CPUs
254 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
255 for cpu in `hotpluggable_online_cpus`; do
256 offline_cpu_expect_success $cpu
257 done
260 # Test CPU hot-add error handling (offline => online)
262 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_UP_PREPARE/error
263 for cpu in `hotplaggable_offline_cpus`; do
264 online_cpu_expect_fail $cpu
265 done
268 # Online all hot-pluggable CPUs
270 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_UP_PREPARE/error
271 for cpu in `hotplaggable_offline_cpus`; do
272 online_cpu_expect_success $cpu
273 done
276 # Test CPU hot-remove error handling (online => offline)
278 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
279 for cpu in `hotpluggable_online_cpus`; do
280 offline_cpu_expect_fail $cpu
281 done
283 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
284 /sbin/modprobe -q -r cpu-notifier-error-inject