2 # SPDX-License-Identifier: GPL-2.0
10 if [ $UID != 0 ]; then
11 echo $msg must be run as root >&2
17 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
19 if [ ! -d "$SYSFS" ]; then
20 echo $msg sysfs is not mounted >&2
24 if ! ls $SYSFS/devices/system/cpu/cpu* > /dev/null 2>&1; then
25 echo $msg cpu hotplug is not supported >&2
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"
38 present_cpus=`cat $SYSFS/devices/system/cpu/present`
39 present_max=${present_cpus##*-}
40 echo "present_cpus = $present_cpus present_max = $present_max"
42 echo -e "\t Cpus in online state: $online_cpus"
44 offline_cpus=`cat $SYSFS/devices/system/cpu/offline`
45 if [[ "a$offline_cpus" = "a" ]]; then
48 offline_max=${offline_cpus##*-}
50 echo -e "\t Cpus in offline state: $offline_cpus"
54 # list all hot-pluggable CPUs
60 for cpu in $SYSFS/devices/system/cpu/cpu*; do
61 if [ -f $cpu/online ] && grep -q $state $cpu/online; then
67 hotplaggable_offline_cpus()
72 hotpluggable_online_cpus()
79 grep -q 1 $SYSFS/devices/system/cpu/cpu$1/online
84 grep -q 0 $SYSFS/devices/system/cpu/cpu$1/online
89 echo 1 > $SYSFS/devices/system/cpu/cpu$1/online
94 echo 0 > $SYSFS/devices/system/cpu/cpu$1/online
97 online_cpu_expect_success()
101 if ! online_cpu $cpu; then
102 echo $FUNCNAME $cpu: unexpected fail >&2
104 elif ! cpu_is_online $cpu; then
105 echo $FUNCNAME $cpu: unexpected offline >&2
110 online_cpu_expect_fail()
114 if online_cpu $cpu 2> /dev/null; then
115 echo $FUNCNAME $cpu: unexpected success >&2
117 elif ! cpu_is_offline $cpu; then
118 echo $FUNCNAME $cpu: unexpected online >&2
123 offline_cpu_expect_success()
127 if ! offline_cpu $cpu; then
128 echo $FUNCNAME $cpu: unexpected fail >&2
130 elif ! cpu_is_offline $cpu; then
131 echo $FUNCNAME $cpu: unexpected offline >&2
136 offline_cpu_expect_fail()
140 if offline_cpu $cpu 2> /dev/null; then
141 echo $FUNCNAME $cpu: unexpected success >&2
143 elif ! cpu_is_online $cpu; then
144 echo $FUNCNAME $cpu: unexpected offline >&2
159 while getopts e:ahp: opt; do
168 echo "Usage $0 [ -a ] [ -e errno ] [ -p notifier-priority ]"
169 echo -e "\t default offline one cpu"
170 echo -e "\t run with -a option to offline all cpus"
179 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
180 echo "error code must be -4095 <= errno < 0" >&2
187 # Safe test (default) - offline and online one cpu
189 if [ $allcpus -eq 0 ]; then
190 echo "Limited scope test: one hotplug cpu"
191 echo -e "\t (leaves cpu in the original state):"
192 echo -e "\t online to offline to online: cpu $online_max"
193 offline_cpu_expect_success $online_max
194 online_cpu_expect_success $online_max
196 if [[ $offline_cpus -gt 0 ]]; then
197 echo -e "\t offline to online to offline: cpu $present_max"
198 online_cpu_expect_success $present_max
199 offline_cpu_expect_success $present_max
200 online_cpu $present_max
204 echo "Full scope test: all hotplug cpus"
205 echo -e "\t online all offline cpus"
206 echo -e "\t offline all online cpus"
207 echo -e "\t online all offline cpus"
211 # Online all hot-pluggable CPUs
213 for cpu in `hotplaggable_offline_cpus`; do
214 online_cpu_expect_success $cpu
218 # Offline all hot-pluggable CPUs
220 for cpu in `hotpluggable_online_cpus`; do
221 offline_cpu_expect_success $cpu
225 # Online all hot-pluggable CPUs again
227 for cpu in `hotplaggable_offline_cpus`; do
228 online_cpu_expect_success $cpu
232 # Test with cpu notifier error injection
235 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
236 NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/cpu
240 msg="skip extra tests:"
242 /sbin/modprobe -q -r cpu-notifier-error-inject
243 /sbin/modprobe -q cpu-notifier-error-inject priority=$priority
245 if [ ! -d "$DEBUGFS" ]; then
246 echo $msg debugfs is not mounted >&2
250 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
251 echo $msg cpu-notifier-error-inject module is not available >&2
259 # Offline all hot-pluggable CPUs
261 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
262 for cpu in `hotpluggable_online_cpus`; do
263 offline_cpu_expect_success $cpu
267 # Test CPU hot-add error handling (offline => online)
269 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_UP_PREPARE/error
270 for cpu in `hotplaggable_offline_cpus`; do
271 online_cpu_expect_fail $cpu
275 # Online all hot-pluggable CPUs
277 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_UP_PREPARE/error
278 for cpu in `hotplaggable_offline_cpus`; do
279 online_cpu_expect_success $cpu
283 # Test CPU hot-remove error handling (online => offline)
285 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
286 for cpu in `hotpluggable_online_cpus`; do
287 offline_cpu_expect_fail $cpu
290 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/CPU_DOWN_PREPARE/error
291 /sbin/modprobe -q -r cpu-notifier-error-inject