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