313 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4SYSFS=5 6# Kselftest framework requirement - SKIP code is 4.7ksft_skip=48 9prerequisite()10{11 msg="skip all tests:"12 13 if [ $UID != 0 ]; then14 echo $msg must be run as root >&215 exit $ksft_skip16 fi17 18 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`19 20 if [ ! -d "$SYSFS" ]; then21 echo $msg sysfs is not mounted >&222 exit $ksft_skip23 fi24 25 if ! ls $SYSFS/devices/system/memory/memory* > /dev/null 2>&1; then26 echo $msg memory hotplug is not supported >&227 exit $ksft_skip28 fi29 30 if ! grep -q 1 $SYSFS/devices/system/memory/memory*/removable; then31 echo $msg no hot-pluggable memory >&232 exit $ksft_skip33 fi34}35 36#37# list all hot-pluggable memory38#39hotpluggable_memory()40{41 local state=${1:-.\*}42 43 for memory in $SYSFS/devices/system/memory/memory*; do44 if grep -q 1 $memory/removable &&45 grep -q $state $memory/state; then46 echo ${memory##/*/memory}47 fi48 done49}50 51hotpluggable_offline_memory()52{53 hotpluggable_memory offline54}55 56hotpluggable_online_memory()57{58 hotpluggable_memory online59}60 61memory_is_online()62{63 grep -q online $SYSFS/devices/system/memory/memory$1/state64}65 66memory_is_offline()67{68 grep -q offline $SYSFS/devices/system/memory/memory$1/state69}70 71online_memory()72{73 echo online > $SYSFS/devices/system/memory/memory$1/state74}75 76offline_memory()77{78 echo offline > $SYSFS/devices/system/memory/memory$1/state79}80 81online_memory_expect_success()82{83 local memory=$184 85 if ! online_memory $memory; then86 echo $FUNCNAME $memory: unexpected fail >&287 return 188 elif ! memory_is_online $memory; then89 echo $FUNCNAME $memory: unexpected offline >&290 return 191 fi92 return 093}94 95online_memory_expect_fail()96{97 local memory=$198 99 if online_memory $memory 2> /dev/null; then100 echo $FUNCNAME $memory: unexpected success >&2101 return 1102 elif ! memory_is_offline $memory; then103 echo $FUNCNAME $memory: unexpected online >&2104 return 1105 fi106 return 0107}108 109offline_memory_expect_success()110{111 local memory=$1112 113 if ! offline_memory $memory; then114 echo $FUNCNAME $memory: unexpected fail >&2115 return 1116 elif ! memory_is_offline $memory; then117 echo $FUNCNAME $memory: unexpected offline >&2118 return 1119 fi120 return 0121}122 123offline_memory_expect_fail()124{125 local memory=$1126 127 if offline_memory $memory 2> /dev/null; then128 echo $FUNCNAME $memory: unexpected success >&2129 return 1130 elif ! memory_is_online $memory; then131 echo $FUNCNAME $memory: unexpected offline >&2132 return 1133 fi134 return 0135}136 137online_all_offline_memory()138{139 for memory in `hotpluggable_offline_memory`; do140 if ! online_memory_expect_success $memory; then141 retval=1142 fi143 done144}145 146error=-12147priority=0148# Run with default of ratio=2 for Kselftest run149ratio=2150retval=0151 152while getopts e:hp:r: opt; do153 case $opt in154 e)155 error=$OPTARG156 ;;157 h)158 echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"159 exit160 ;;161 p)162 priority=$OPTARG163 ;;164 r)165 ratio=$OPTARG166 if [ "$ratio" -gt 100 ] || [ "$ratio" -lt 0 ]; then167 echo "The percentage should be an integer within 0~100 range"168 exit 1169 fi170 ;;171 esac172done173 174if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then175 echo "error code must be -4095 <= errno < 0" >&2176 exit 1177fi178 179prerequisite180 181echo "Test scope: $ratio% hotplug memory"182 183#184# Online all hot-pluggable memory185#186hotpluggable_num=`hotpluggable_offline_memory | wc -l`187echo -e "\t online all hot-pluggable memory in offline state:"188if [ "$hotpluggable_num" -gt 0 ]; then189 for memory in `hotpluggable_offline_memory`; do190 echo "offline->online memory$memory"191 if ! online_memory_expect_success $memory; then192 retval=1193 fi194 done195else196 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"197fi198 199#200# Offline $ratio percent of hot-pluggable memory201#202hotpluggable_num=`hotpluggable_online_memory | wc -l`203target=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc`204echo -e "\t offline $ratio% hot-pluggable memory in online state"205echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):"206for memory in `hotpluggable_online_memory`; do207 if [ "$target" -gt 0 ]; then208 echo "online->offline memory$memory"209 if offline_memory_expect_success $memory &>/dev/null; then210 target=$(($target - 1))211 echo "-> Success"212 else213 echo "-> Failure"214 fi215 fi216done217if [ "$target" -gt 0 ]; then218 retval=1219 echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?"220fi221 222#223# Online all hot-pluggable memory again224#225hotpluggable_num=`hotpluggable_offline_memory | wc -l`226echo -e "\t online all hot-pluggable memory in offline state:"227if [ "$hotpluggable_num" -gt 0 ]; then228 for memory in `hotpluggable_offline_memory`; do229 echo "offline->online memory$memory"230 if ! online_memory_expect_success $memory; then231 retval=1232 fi233 done234else235 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"236fi237 238#239# Test with memory notifier error injection240#241 242DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`243NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory244 245prerequisite_extra()246{247 msg="skip extra tests:"248 249 /sbin/modprobe -q -r memory-notifier-error-inject250 /sbin/modprobe -q memory-notifier-error-inject priority=$priority251 252 if [ ! -d "$DEBUGFS" ]; then253 echo $msg debugfs is not mounted >&2254 exit $retval255 fi256 257 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then258 echo $msg memory-notifier-error-inject module is not available >&2259 exit $retval260 fi261}262 263echo -e "\t Test with memory notifier error injection"264prerequisite_extra265 266#267# Offline $ratio percent of hot-pluggable memory268#269echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error270for memory in `hotpluggable_online_memory`; do271 if [ $((RANDOM % 100)) -lt $ratio ]; then272 offline_memory_expect_success $memory &>/dev/null273 fi274done275 276#277# Test memory hot-add error handling (offline => online)278#279echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error280for memory in `hotpluggable_offline_memory`; do281 if ! online_memory_expect_fail $memory; then282 retval=1283 fi284done285 286#287# Online all hot-pluggable memory288#289echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error290online_all_offline_memory291 292#293# Test memory hot-remove error handling (online => offline)294#295echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error296for memory in `hotpluggable_online_memory`; do297 if [ $((RANDOM % 100)) -lt $ratio ]; then298 if ! offline_memory_expect_fail $memory; then299 retval=1300 fi301 fi302done303 304echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error305/sbin/modprobe -q -r memory-notifier-error-inject306 307#308# Restore memory before exit309#310online_all_offline_memory311 312exit $retval313