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 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
44 offline_max
=${offline_cpus##*-}
46 echo -e "\t Cpus in offline state: $offline_cpus"
50 # list all hot-pluggable CPUs
56 for cpu
in $SYSFS/devices
/system
/cpu
/cpu
*; do
57 if [ -f $cpu/online
] && grep -q $state $cpu/online
; then
63 hotplaggable_offline_cpus
()
68 hotpluggable_online_cpus
()
75 grep -q 1 $SYSFS/devices
/system
/cpu
/cpu
$1/online
80 grep -q 0 $SYSFS/devices
/system
/cpu
/cpu
$1/online
85 echo 1 > $SYSFS/devices
/system
/cpu
/cpu
$1/online
90 echo 0 > $SYSFS/devices
/system
/cpu
/cpu
$1/online
93 online_cpu_expect_success
()
97 if ! online_cpu
$cpu; then
98 echo $FUNCNAME $cpu: unexpected fail
>&2
100 elif ! cpu_is_online
$cpu; then
101 echo $FUNCNAME $cpu: unexpected offline
>&2
106 online_cpu_expect_fail
()
110 if online_cpu
$cpu 2> /dev
/null
; then
111 echo $FUNCNAME $cpu: unexpected success
>&2
113 elif ! cpu_is_offline
$cpu; then
114 echo $FUNCNAME $cpu: unexpected online
>&2
119 offline_cpu_expect_success
()
123 if ! offline_cpu
$cpu; then
124 echo $FUNCNAME $cpu: unexpected fail
>&2
126 elif ! cpu_is_offline
$cpu; then
127 echo $FUNCNAME $cpu: unexpected offline
>&2
132 offline_cpu_expect_fail
()
136 if offline_cpu
$cpu 2> /dev
/null
; then
137 echo $FUNCNAME $cpu: unexpected success
>&2
139 elif ! cpu_is_online
$cpu; then
140 echo $FUNCNAME $cpu: unexpected offline
>&2
153 while getopts e
:ahp
: opt
; do
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"
173 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
174 echo "error code must be -4095 <= errno < 0" >&2
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
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
211 # Offline all hot-pluggable CPUs
213 for cpu
in `hotpluggable_online_cpus`; do
214 offline_cpu_expect_success
$cpu
218 # Online all hot-pluggable CPUs again
220 for cpu
in `hotplaggable_offline_cpus`; do
221 online_cpu_expect_success
$cpu
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
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
243 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
244 echo $msg cpu-notifier-error-inject module is not available
>&2
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
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
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
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
283 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions
/CPU_DOWN_PREPARE
/error
284 /sbin
/modprobe
-q -r cpu-notifier-error-inject