2 # SPDX-License-Identifier: GPL-2.0
10 if [ $UID != 0 ]; then
11 echo $msg must be run as root >&2
15 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
17 if [ ! -d "$SYSFS" ]; then
18 echo $msg sysfs is not mounted >&2
22 if ! ls $SYSFS/devices/system/memory/memory* > /dev/null 2>&1; then
23 echo $msg memory hotplug is not supported >&2
27 if ! grep -q 1 $SYSFS/devices/system/memory/memory*/removable; then
28 echo $msg no hot-pluggable memory >&2
34 # list all hot-pluggable memory
40 for memory in $SYSFS/devices/system/memory/memory*; do
41 if grep -q 1 $memory/removable &&
42 grep -q $state $memory/state; then
43 echo ${memory##/*/memory}
48 hotpluggable_offline_memory()
50 hotpluggable_memory offline
53 hotpluggable_online_memory()
55 hotpluggable_memory online
60 grep -q online $SYSFS/devices/system/memory/memory$1/state
65 grep -q offline $SYSFS/devices/system/memory/memory$1/state
70 echo online > $SYSFS/devices/system/memory/memory$1/state
75 echo offline > $SYSFS/devices/system/memory/memory$1/state
78 online_memory_expect_success()
82 if ! online_memory $memory; then
83 echo $FUNCNAME $memory: unexpected fail >&2
85 elif ! memory_is_online $memory; then
86 echo $FUNCNAME $memory: unexpected offline >&2
92 online_memory_expect_fail()
96 if online_memory $memory 2> /dev/null; then
97 echo $FUNCNAME $memory: unexpected success >&2
99 elif ! memory_is_offline $memory; then
100 echo $FUNCNAME $memory: unexpected online >&2
106 offline_memory_expect_success()
110 if ! offline_memory $memory; then
111 echo $FUNCNAME $memory: unexpected fail >&2
113 elif ! memory_is_offline $memory; then
114 echo $FUNCNAME $memory: unexpected offline >&2
120 offline_memory_expect_fail()
124 if offline_memory $memory 2> /dev/null; then
125 echo $FUNCNAME $memory: unexpected success >&2
127 elif ! memory_is_online $memory; then
128 echo $FUNCNAME $memory: unexpected offline >&2
139 while getopts e:hp:r: opt; do
145 echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"
153 if [ "$ratio" -gt 100 ] || [ "$ratio" -lt 0 ]; then
154 echo "The percentage should be an integer within 0~100 range"
161 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
162 echo "error code must be -4095 <= errno < 0" >&2
168 echo "Test scope: $ratio% hotplug memory"
171 # Online all hot-pluggable memory
173 hotpluggable_num=`hotpluggable_offline_memory | wc -l`
174 echo -e "\t online all hot-pluggable memory in offline state:"
175 if [ "$hotpluggable_num" -gt 0 ]; then
176 for memory in `hotpluggable_offline_memory`; do
177 echo "offline->online memory$memory"
178 if ! online_memory_expect_success $memory; then
183 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
187 # Offline $ratio percent of hot-pluggable memory
189 hotpluggable_num=`hotpluggable_online_memory | wc -l`
190 target=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc`
191 echo -e "\t offline $ratio% hot-pluggable memory in online state"
192 echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):"
193 for memory in `hotpluggable_online_memory`; do
194 if [ "$target" -gt 0 ]; then
195 echo "online->offline memory$memory"
196 if offline_memory_expect_success $memory; then
197 target=$(($target - 1))
201 if [ "$target" -gt 0 ]; then
203 echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?"
207 # Online all hot-pluggable memory again
209 hotpluggable_num=`hotpluggable_offline_memory | wc -l`
210 echo -e "\t online all hot-pluggable memory in offline state:"
211 if [ "$hotpluggable_num" -gt 0 ]; then
212 for memory in `hotpluggable_offline_memory`; do
213 echo "offline->online memory$memory"
214 if ! online_memory_expect_success $memory; then
219 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
223 # Test with memory notifier error injection
226 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
227 NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory
231 msg="skip extra tests:"
233 /sbin/modprobe -q -r memory-notifier-error-inject
234 /sbin/modprobe -q memory-notifier-error-inject priority=$priority
236 if [ ! -d "$DEBUGFS" ]; then
237 echo $msg debugfs is not mounted >&2
241 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
242 echo $msg memory-notifier-error-inject module is not available >&2
247 echo -e "\t Test with memory notifier error injection"
251 # Offline $ratio percent of hot-pluggable memory
253 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
254 for memory in `hotpluggable_online_memory`; do
255 if [ $((RANDOM % 100)) -lt $ratio ]; then
256 offline_memory_expect_success $memory
261 # Test memory hot-add error handling (offline => online)
263 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
264 for memory in `hotpluggable_offline_memory`; do
265 online_memory_expect_fail $memory
269 # Online all hot-pluggable memory
271 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
272 for memory in `hotpluggable_offline_memory`; do
273 online_memory_expect_success $memory
277 # Test memory hot-remove error handling (online => offline)
279 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
280 for memory in `hotpluggable_online_memory`; do
281 offline_memory_expect_fail $memory
284 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
285 /sbin/modprobe -q -r memory-notifier-error-inject