1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4SYSFS=
5# Kselftest framework requirement - SKIP code is 4.
6ksft_skip=4
7retval=0
8
9prerequisite()
10{
11	msg="skip all tests:"
12
13	if [ $UID != 0 ]; then
14		echo $msg must be run as root >&2
15		exit $ksft_skip
16	fi
17
18	taskset -p 01 $$
19
20	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
21
22	if [ ! -d "$SYSFS" ]; then
23		echo $msg sysfs is not mounted >&2
24		exit $ksft_skip
25	fi
26
27	if ! ls $SYSFS/devices/system/cpu/cpu* > /dev/null 2>&1; then
28		echo $msg cpu hotplug is not supported >&2
29		exit $ksft_skip
30	fi
31
32	echo "CPU online/offline summary:"
33	online_cpus=`cat $SYSFS/devices/system/cpu/online`
34	online_max=${online_cpus##*-}
35
36	if [[ "$online_cpus" = "$online_max" ]]; then
37		echo "$msg: since there is only one cpu: $online_cpus"
38		exit $ksft_skip
39	fi
40
41	present_cpus=`cat $SYSFS/devices/system/cpu/present`
42	present_max=${present_cpus##*-}
43	echo "present_cpus = $present_cpus present_max = $present_max"
44
45	echo -e "\t Cpus in online state: $online_cpus"
46
47	offline_cpus=`cat $SYSFS/devices/system/cpu/offline`
48	if [[ "a$offline_cpus" = "a" ]]; then
49		offline_cpus=0
50	else
51		offline_max=${offline_cpus##*-}
52	fi
53	echo -e "\t Cpus in offline state: $offline_cpus"
54}
55
56#
57# list all hot-pluggable CPUs
58#
59hotpluggable_cpus()
60{
61	local state=${1:-.\*}
62
63	for cpu in $SYSFS/devices/system/cpu/cpu*; do
64		if [ -f $cpu/online ] && grep -q $state $cpu/online; then
65			echo ${cpu##/*/cpu}
66		fi
67	done
68}
69
70hotplaggable_offline_cpus()
71{
72	hotpluggable_cpus 0
73}
74
75hotpluggable_online_cpus()
76{
77	hotpluggable_cpus 1
78}
79
80cpu_is_online()
81{
82	grep -q 1 $SYSFS/devices/system/cpu/cpu$1/online
83}
84
85cpu_is_offline()
86{
87	grep -q 0 $SYSFS/devices/system/cpu/cpu$1/online
88}
89
90online_cpu()
91{
92	echo 1 > $SYSFS/devices/system/cpu/cpu$1/online
93}
94
95offline_cpu()
96{
97	echo 0 > $SYSFS/devices/system/cpu/cpu$1/online
98}
99
100online_cpu_expect_success()
101{
102	local cpu=$1
103
104	if ! online_cpu $cpu; then
105		echo $FUNCNAME $cpu: unexpected fail >&2
106		retval=1
107	elif ! cpu_is_online $cpu; then
108		echo $FUNCNAME $cpu: unexpected offline >&2
109		retval=1
110	fi
111}
112
113online_cpu_expect_fail()
114{
115	local cpu=$1
116
117	if online_cpu $cpu 2> /dev/null; then
118		echo $FUNCNAME $cpu: unexpected success >&2
119		retval=1
120	elif ! cpu_is_offline $cpu; then
121		echo $FUNCNAME $cpu: unexpected online >&2
122		retval=1
123	fi
124}
125
126offline_cpu_expect_success()
127{
128	local cpu=$1
129
130	if ! offline_cpu $cpu; then
131		echo $FUNCNAME $cpu: unexpected fail >&2
132		retval=1
133	elif ! cpu_is_offline $cpu; then
134		echo $FUNCNAME $cpu: unexpected offline >&2
135		retval=1
136	fi
137}
138
139offline_cpu_expect_fail()
140{
141	local cpu=$1
142
143	if offline_cpu $cpu 2> /dev/null; then
144		echo $FUNCNAME $cpu: unexpected success >&2
145		retval=1
146	elif ! cpu_is_online $cpu; then
147		echo $FUNCNAME $cpu: unexpected offline >&2
148		retval=1
149	fi
150}
151
152allcpus=0
153online_cpus=0
154online_max=0
155offline_cpus=0
156offline_max=0
157present_cpus=0
158present_max=0
159
160while getopts ah opt; do
161	case $opt in
162	a)
163		allcpus=1
164		;;
165	h)
166		echo "Usage $0 [ -a ]"
167		echo -e "\t default offline one cpu"
168		echo -e "\t run with -a option to offline all cpus"
169		exit
170		;;
171	esac
172done
173
174prerequisite
175
176#
177# Safe test (default) - offline and online one cpu
178#
179if [ $allcpus -eq 0 ]; then
180	echo "Limited scope test: one hotplug cpu"
181	echo -e "\t (leaves cpu in the original state):"
182	echo -e "\t online to offline to online: cpu $online_max"
183	offline_cpu_expect_success $online_max
184	online_cpu_expect_success $online_max
185
186	if [[ $offline_cpus -gt 0 ]]; then
187		echo -e "\t online to offline to online: cpu $present_max"
188		online_cpu_expect_success $present_max
189		offline_cpu_expect_success $present_max
190		online_cpu $present_max
191	fi
192	exit $retval
193else
194	echo "Full scope test: all hotplug cpus"
195	echo -e "\t online all offline cpus"
196	echo -e "\t offline all online cpus"
197	echo -e "\t online all offline cpus"
198fi
199
200#
201# Online all hot-pluggable CPUs
202#
203for cpu in `hotplaggable_offline_cpus`; do
204	online_cpu_expect_success $cpu
205done
206
207#
208# Offline all hot-pluggable CPUs
209#
210for cpu in `hotpluggable_online_cpus`; do
211	offline_cpu_expect_success $cpu
212done
213
214#
215# Online all hot-pluggable CPUs again
216#
217for cpu in `hotplaggable_offline_cpus`; do
218	online_cpu_expect_success $cpu
219done
220
221exit $retval
222