mem-on-off-test.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
  14. if [ ! -d "$SYSFS" ]; then
  15. echo $msg sysfs is not mounted >&2
  16. exit $ksft_skip
  17. fi
  18. if ! ls $SYSFS/devices/system/memory/memory* > /dev/null 2>&1; then
  19. echo $msg memory hotplug is not supported >&2
  20. exit $ksft_skip
  21. fi
  22. if ! grep -q 1 $SYSFS/devices/system/memory/memory*/removable; then
  23. echo $msg no hot-pluggable memory >&2
  24. exit $ksft_skip
  25. fi
  26. }
  27. #
  28. # list all hot-pluggable memory
  29. #
  30. hotpluggable_memory()
  31. {
  32. local state=${1:-.\*}
  33. for memory in $SYSFS/devices/system/memory/memory*; do
  34. if grep -q 1 $memory/removable &&
  35. grep -q $state $memory/state; then
  36. echo ${memory##/*/memory}
  37. fi
  38. done
  39. }
  40. hotpluggable_offline_memory()
  41. {
  42. hotpluggable_memory offline
  43. }
  44. hotpluggable_online_memory()
  45. {
  46. hotpluggable_memory online
  47. }
  48. memory_is_online()
  49. {
  50. grep -q online $SYSFS/devices/system/memory/memory$1/state
  51. }
  52. memory_is_offline()
  53. {
  54. grep -q offline $SYSFS/devices/system/memory/memory$1/state
  55. }
  56. online_memory()
  57. {
  58. echo online > $SYSFS/devices/system/memory/memory$1/state
  59. }
  60. offline_memory()
  61. {
  62. echo offline > $SYSFS/devices/system/memory/memory$1/state
  63. }
  64. online_memory_expect_success()
  65. {
  66. local memory=$1
  67. if ! online_memory $memory; then
  68. echo $FUNCNAME $memory: unexpected fail >&2
  69. return 1
  70. elif ! memory_is_online $memory; then
  71. echo $FUNCNAME $memory: unexpected offline >&2
  72. return 1
  73. fi
  74. return 0
  75. }
  76. online_memory_expect_fail()
  77. {
  78. local memory=$1
  79. if online_memory $memory 2> /dev/null; then
  80. echo $FUNCNAME $memory: unexpected success >&2
  81. return 1
  82. elif ! memory_is_offline $memory; then
  83. echo $FUNCNAME $memory: unexpected online >&2
  84. return 1
  85. fi
  86. return 0
  87. }
  88. offline_memory_expect_success()
  89. {
  90. local memory=$1
  91. if ! offline_memory $memory; then
  92. echo $FUNCNAME $memory: unexpected fail >&2
  93. return 1
  94. elif ! memory_is_offline $memory; then
  95. echo $FUNCNAME $memory: unexpected offline >&2
  96. return 1
  97. fi
  98. return 0
  99. }
  100. offline_memory_expect_fail()
  101. {
  102. local memory=$1
  103. if offline_memory $memory 2> /dev/null; then
  104. echo $FUNCNAME $memory: unexpected success >&2
  105. return 1
  106. elif ! memory_is_online $memory; then
  107. echo $FUNCNAME $memory: unexpected offline >&2
  108. return 1
  109. fi
  110. return 0
  111. }
  112. error=-12
  113. priority=0
  114. # Run with default of ratio=2 for Kselftest run
  115. ratio=2
  116. retval=0
  117. while getopts e:hp:r: opt; do
  118. case $opt in
  119. e)
  120. error=$OPTARG
  121. ;;
  122. h)
  123. echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"
  124. exit
  125. ;;
  126. p)
  127. priority=$OPTARG
  128. ;;
  129. r)
  130. ratio=$OPTARG
  131. if [ "$ratio" -gt 100 ] || [ "$ratio" -lt 0 ]; then
  132. echo "The percentage should be an integer within 0~100 range"
  133. exit 1
  134. fi
  135. ;;
  136. esac
  137. done
  138. if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
  139. echo "error code must be -4095 <= errno < 0" >&2
  140. exit 1
  141. fi
  142. prerequisite
  143. echo "Test scope: $ratio% hotplug memory"
  144. #
  145. # Online all hot-pluggable memory
  146. #
  147. hotpluggable_num=`hotpluggable_offline_memory | wc -l`
  148. echo -e "\t online all hot-pluggable memory in offline state:"
  149. if [ "$hotpluggable_num" -gt 0 ]; then
  150. for memory in `hotpluggable_offline_memory`; do
  151. echo "offline->online memory$memory"
  152. if ! online_memory_expect_success $memory; then
  153. retval=1
  154. fi
  155. done
  156. else
  157. echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
  158. fi
  159. #
  160. # Offline $ratio percent of hot-pluggable memory
  161. #
  162. hotpluggable_num=`hotpluggable_online_memory | wc -l`
  163. target=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc`
  164. echo -e "\t offline $ratio% hot-pluggable memory in online state"
  165. echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):"
  166. for memory in `hotpluggable_online_memory`; do
  167. if [ "$target" -gt 0 ]; then
  168. echo "online->offline memory$memory"
  169. if offline_memory_expect_success $memory; then
  170. target=$(($target - 1))
  171. fi
  172. fi
  173. done
  174. if [ "$target" -gt 0 ]; then
  175. retval=1
  176. echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?"
  177. fi
  178. #
  179. # Online all hot-pluggable memory again
  180. #
  181. hotpluggable_num=`hotpluggable_offline_memory | wc -l`
  182. echo -e "\t online all hot-pluggable memory in offline state:"
  183. if [ "$hotpluggable_num" -gt 0 ]; then
  184. for memory in `hotpluggable_offline_memory`; do
  185. echo "offline->online memory$memory"
  186. if ! online_memory_expect_success $memory; then
  187. retval=1
  188. fi
  189. done
  190. else
  191. echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
  192. fi
  193. #
  194. # Test with memory notifier error injection
  195. #
  196. DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
  197. NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory
  198. prerequisite_extra()
  199. {
  200. msg="skip extra tests:"
  201. /sbin/modprobe -q -r memory-notifier-error-inject
  202. /sbin/modprobe -q memory-notifier-error-inject priority=$priority
  203. if [ ! -d "$DEBUGFS" ]; then
  204. echo $msg debugfs is not mounted >&2
  205. exit $retval
  206. fi
  207. if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
  208. echo $msg memory-notifier-error-inject module is not available >&2
  209. exit $retval
  210. fi
  211. }
  212. echo -e "\t Test with memory notifier error injection"
  213. prerequisite_extra
  214. #
  215. # Offline $ratio percent of hot-pluggable memory
  216. #
  217. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  218. for memory in `hotpluggable_online_memory`; do
  219. if [ $((RANDOM % 100)) -lt $ratio ]; then
  220. offline_memory_expect_success $memory
  221. fi
  222. done
  223. #
  224. # Test memory hot-add error handling (offline => online)
  225. #
  226. echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
  227. for memory in `hotpluggable_offline_memory`; do
  228. online_memory_expect_fail $memory
  229. done
  230. #
  231. # Online all hot-pluggable memory
  232. #
  233. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
  234. for memory in `hotpluggable_offline_memory`; do
  235. online_memory_expect_success $memory
  236. done
  237. #
  238. # Test memory hot-remove error handling (online => offline)
  239. #
  240. echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  241. for memory in `hotpluggable_online_memory`; do
  242. offline_memory_expect_fail $memory
  243. done
  244. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  245. /sbin/modprobe -q -r memory-notifier-error-inject
  246. exit $retval