mem-on-off-test.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. online_all_offline_memory()
  113. {
  114. for memory in `hotpluggable_offline_memory`; do
  115. if ! online_memory_expect_success $memory; then
  116. retval=1
  117. fi
  118. done
  119. }
  120. error=-12
  121. priority=0
  122. # Run with default of ratio=2 for Kselftest run
  123. ratio=2
  124. retval=0
  125. while getopts e:hp:r: opt; do
  126. case $opt in
  127. e)
  128. error=$OPTARG
  129. ;;
  130. h)
  131. echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"
  132. exit
  133. ;;
  134. p)
  135. priority=$OPTARG
  136. ;;
  137. r)
  138. ratio=$OPTARG
  139. if [ "$ratio" -gt 100 ] || [ "$ratio" -lt 0 ]; then
  140. echo "The percentage should be an integer within 0~100 range"
  141. exit 1
  142. fi
  143. ;;
  144. esac
  145. done
  146. if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
  147. echo "error code must be -4095 <= errno < 0" >&2
  148. exit 1
  149. fi
  150. prerequisite
  151. echo "Test scope: $ratio% hotplug memory"
  152. #
  153. # Online all hot-pluggable memory
  154. #
  155. hotpluggable_num=`hotpluggable_offline_memory | wc -l`
  156. echo -e "\t online all hot-pluggable memory in offline state:"
  157. if [ "$hotpluggable_num" -gt 0 ]; then
  158. for memory in `hotpluggable_offline_memory`; do
  159. echo "offline->online memory$memory"
  160. if ! online_memory_expect_success $memory; then
  161. retval=1
  162. fi
  163. done
  164. else
  165. echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
  166. fi
  167. #
  168. # Offline $ratio percent of hot-pluggable memory
  169. #
  170. hotpluggable_num=`hotpluggable_online_memory | wc -l`
  171. target=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc`
  172. echo -e "\t offline $ratio% hot-pluggable memory in online state"
  173. echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):"
  174. for memory in `hotpluggable_online_memory`; do
  175. if [ "$target" -gt 0 ]; then
  176. echo "online->offline memory$memory"
  177. if offline_memory_expect_success $memory &>/dev/null; then
  178. target=$(($target - 1))
  179. echo "-> Success"
  180. else
  181. echo "-> Failure"
  182. fi
  183. fi
  184. done
  185. if [ "$target" -gt 0 ]; then
  186. retval=1
  187. echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?"
  188. fi
  189. #
  190. # Online all hot-pluggable memory again
  191. #
  192. hotpluggable_num=`hotpluggable_offline_memory | wc -l`
  193. echo -e "\t online all hot-pluggable memory in offline state:"
  194. if [ "$hotpluggable_num" -gt 0 ]; then
  195. for memory in `hotpluggable_offline_memory`; do
  196. echo "offline->online memory$memory"
  197. if ! online_memory_expect_success $memory; then
  198. retval=1
  199. fi
  200. done
  201. else
  202. echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
  203. fi
  204. #
  205. # Test with memory notifier error injection
  206. #
  207. DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
  208. NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory
  209. prerequisite_extra()
  210. {
  211. msg="skip extra tests:"
  212. /sbin/modprobe -q -r memory-notifier-error-inject
  213. /sbin/modprobe -q memory-notifier-error-inject priority=$priority
  214. if [ ! -d "$DEBUGFS" ]; then
  215. echo $msg debugfs is not mounted >&2
  216. exit $retval
  217. fi
  218. if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
  219. echo $msg memory-notifier-error-inject module is not available >&2
  220. exit $retval
  221. fi
  222. }
  223. echo -e "\t Test with memory notifier error injection"
  224. prerequisite_extra
  225. #
  226. # Offline $ratio percent of hot-pluggable memory
  227. #
  228. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  229. for memory in `hotpluggable_online_memory`; do
  230. if [ $((RANDOM % 100)) -lt $ratio ]; then
  231. offline_memory_expect_success $memory &>/dev/null
  232. fi
  233. done
  234. #
  235. # Test memory hot-add error handling (offline => online)
  236. #
  237. echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
  238. for memory in `hotpluggable_offline_memory`; do
  239. if ! online_memory_expect_fail $memory; then
  240. retval=1
  241. fi
  242. done
  243. #
  244. # Online all hot-pluggable memory
  245. #
  246. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
  247. online_all_offline_memory
  248. #
  249. # Test memory hot-remove error handling (online => offline)
  250. #
  251. echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  252. for memory in `hotpluggable_online_memory`; do
  253. if [ $((RANDOM % 100)) -lt $ratio ]; then
  254. if ! offline_memory_expect_fail $memory; then
  255. retval=1
  256. fi
  257. fi
  258. done
  259. echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
  260. /sbin/modprobe -q -r memory-notifier-error-inject
  261. #
  262. # Restore memory before exit
  263. #
  264. online_all_offline_memory
  265. exit $retval