cpu-on-off-test.sh 5.4 KB

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