The memory hotplug selftest relies on user-supplied parameters and on the removable sysfs attribute when selecting memory blocks to exercise. The attribute does not guarantee that a block can actually be offlined, which makes the test unreliable when blocks contain unmovable allocations. Convert the test to KTAP output and select a bounded set of blocks by actually attempting to offline them. Limit each offlining attempt to 10 seconds, online selected blocks into ZONE_MOVABLE, and skip the test when no suitable block is found. Exercise notifier error injection when the module is available. Preserve each selected block's initial online state and restore it at the end of the test or when the test exits early. Limit sysfs discovery to numeric memory block directories. Register the script as a regular kselftest program and make run_full_test an alias for the standard runner. Example test results on arm64 vs x86: x86: TAP version 13 # Trying to offline up to 4 memory block(s): # 1 is already offline # Offlining 102 succeeded # Offlining 105 succeeded # Offlining 106 succeeded 1..7 # Test scope: 4 memory block(s) ok 1 Online memory to ZONE_MOVABLE ok 2 Offline memory ok 3 Offline memory with error injection ok 4 Offline memory ok 5 Reonline memory with error injection ok 6 Reonline memory with error injection to ZONE_MOVABLE ok 7 Restore memory state # Totals: pass:7 fail:0 xfail:0 xpass:0 skip:0 error:0 arm64 (no suitable memory blocks found): TAP version 13 # Trying to offline up to 4 memory block(s): # Selected memory blocks: 1..0 # SKIP unable to offline any memory The x86 run passes all seven tests, including notifier error injection and memory-state restoration. The arm64 run skips because no memory block could be selected; it does not exercise the seven test cases. Reported-by: Annam Sai Manisha Signed-off-by: Rama Krishna Katta --- .../testing/selftests/memory-hotplug/Makefile | 9 +- .../memory-hotplug/mem-on-off-test.sh | 436 +++++++----------- 2 files changed, 182 insertions(+), 263 deletions(-) diff --git a/tools/testing/selftests/memory-hotplug/Makefile b/tools/testing/selftests/memory-hotplug/Makefile index e0a625e34f40..1cda8845977d 100644 --- a/tools/testing/selftests/memory-hotplug/Makefile +++ b/tools/testing/selftests/memory-hotplug/Makefile @@ -1,11 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 -all: - -include ../lib.mk TEST_PROGS := mem-on-off-test.sh -run_full_test: - @/bin/bash ./mem-on-off-test.sh -r 10 && echo "memory-hotplug selftests: [PASS]" || echo "memory-hotplug selftests: [FAIL]" +include ../lib.mk -clean: +.PHONY: run_full_test +run_full_test: run_tests diff --git a/tools/testing/selftests/memory-hotplug/mem-on-off-test.sh b/tools/testing/selftests/memory-hotplug/mem-on-off-test.sh index 611be86eaf3d..78e802587772 100755 --- a/tools/testing/selftests/memory-hotplug/mem-on-off-test.sh +++ b/tools/testing/selftests/memory-hotplug/mem-on-off-test.sh @@ -1,312 +1,234 @@ #!/bin/bash # SPDX-License-Identifier: GPL-2.0 +# +KSELFTEST_KTAP_HELPERS="$(dirname "$(realpath "$0")")/../kselftest/ktap_helpers.sh" +source "$KSELFTEST_KTAP_HELPERS" SYSFS= +DEBUGFS= +NOTIFIER_ERR_INJECT_DIR= +ERR_INJECT_ERRNO=-12 +ERR_INJECT_PRIORITY=0 +MAX_BLOCKS=4 -# Kselftest framework requirement - SKIP code is 4. -ksft_skip=4 +declare -A INITIAL_STATE +test_memory=() -prerequisite() +prepare_error_inject() { - msg="skip all tests:" - - if [ $UID != 0 ]; then - echo $msg must be run as root >&2 - exit $ksft_skip - fi - - SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'` - - if [ ! -d "$SYSFS" ]; then - echo $msg sysfs is not mounted >&2 - exit $ksft_skip + DEBUGFS=$(mount -t debugfs | head -1 | awk '{ print $3 }') + if [ ! -d "$DEBUGFS" ]; then + ktap_print_msg "debugfs is not mounted" + return fi - - if ! ls $SYSFS/devices/system/memory/memory* > /dev/null 2>&1; then - echo $msg memory hotplug is not supported >&2 - exit $ksft_skip + if findmnt -rn -T "$DEBUGFS" -o VFS-OPTIONS | grep -qw ro; then + ktap_print_msg "debugfs is mounted read-only" + return fi - if ! grep -q 1 $SYSFS/devices/system/memory/memory*/removable; then - echo $msg no hot-pluggable memory >&2 - exit $ksft_skip + /sbin/modprobe -q -r memory-notifier-error-inject + /sbin/modprobe -q memory-notifier-error-inject priority=$ERR_INJECT_PRIORITY + NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory + if [ ! -d "$NOTIFIER_ERR_INJECT_DIR" ]; then + ktap_print_msg "memory-notifier-error-inject module is not available" + NOTIFIER_ERR_INJECT_DIR= fi } -# -# list all hot-pluggable memory -# -hotpluggable_memory() -{ - local state=${1:-.\*} - - for memory in $SYSFS/devices/system/memory/memory*; do - if grep -q 1 $memory/removable && - grep -q $state $memory/state; then - echo ${memory##/*/memory} - fi - done -} - -hotpluggable_offline_memory() -{ - hotpluggable_memory offline -} - -hotpluggable_online_memory() +prerequisite() { - hotpluggable_memory online + if [ "$UID" -ne 0 ]; then + ktap_skip_all "must be run as root" + exit "$KSFT_SKIP" + fi + SYSFS=$(mount -t sysfs | head -1 | awk '{ print $3 }') + if [ ! -d "$SYSFS" ]; then + ktap_skip_all "sysfs is not mounted" + exit "$KSFT_SKIP" + fi + if ! compgen -G "$SYSFS/devices/system/memory/memory[0-9]*" >/dev/null; then + ktap_skip_all "memory hotplug is not supported" + exit "$KSFT_SKIP" + fi + if ! grep -q 1 "$SYSFS"/devices/system/memory/memory[0-9]*/removable; then + ktap_skip_all "no hot-pluggable memory" + exit "$KSFT_SKIP" + fi + prepare_error_inject } memory_is_online() { - grep -q online $SYSFS/devices/system/memory/memory$1/state + grep -qx online "$SYSFS/devices/system/memory/memory$1/state" } memory_is_offline() { - grep -q offline $SYSFS/devices/system/memory/memory$1/state + grep -qx offline "$SYSFS/devices/system/memory/memory$1/state" } online_memory() { - echo online > $SYSFS/devices/system/memory/memory$1/state + echo "online_$2" > "$SYSFS/devices/system/memory/memory$1/state" } offline_memory() { - echo offline > $SYSFS/devices/system/memory/memory$1/state + timeout --foreground 10s sh -c 'echo offline > "$1"' sh \ + "$SYSFS/devices/system/memory/memory$1/state" } -online_memory_expect_success() +online_memory_blocks() { - local memory=$1 - - if ! online_memory $memory; then - echo $FUNCNAME $memory: unexpected fail >&2 - return 1 - elif ! memory_is_online $memory; then - echo $FUNCNAME $memory: unexpected offline >&2 - return 1 - fi - return 0 -} - -online_memory_expect_fail() -{ - local memory=$1 - - if online_memory $memory 2> /dev/null; then - echo $FUNCNAME $memory: unexpected success >&2 - return 1 - elif ! memory_is_offline $memory; then - echo $FUNCNAME $memory: unexpected online >&2 - return 1 - fi - return 0 -} - -offline_memory_expect_success() -{ - local memory=$1 - - if ! offline_memory $memory; then - echo $FUNCNAME $memory: unexpected fail >&2 - return 1 - elif ! memory_is_offline $memory; then - echo $FUNCNAME $memory: unexpected offline >&2 - return 1 - fi - return 0 -} - -offline_memory_expect_fail() -{ - local memory=$1 - - if offline_memory $memory 2> /dev/null; then - echo $FUNCNAME $memory: unexpected success >&2 - return 1 - elif ! memory_is_online $memory; then - echo $FUNCNAME $memory: unexpected offline >&2 - return 1 - fi - return 0 + local actual_ret block + local expected_ret=$3 + local -n blocks=$1 + local online_type=$2 + local ret=0 + + for block in "${blocks[@]}"; do + online_memory "$block" "$online_type" 2>/dev/null + actual_ret=$? + if [ "$actual_ret" -ne "$expected_ret" ]; then + ktap_print_msg \ + "Onlining memory block $block as $online_type" \ + "returned $actual_ret, expected $expected_ret" + ret=1 + elif [ "$expected_ret" -eq 0 ] && ! memory_is_online "$block"; then + ktap_print_msg "Memory block $block remained offline" + ret=1 + elif [ "$expected_ret" -ne 0 ] && ! memory_is_offline "$block"; then + ktap_print_msg "Memory block $block was onlined unexpectedly" + ret=1 + fi + done + return $ret } -online_all_offline_memory() +offline_memory_blocks() { - for memory in `hotpluggable_offline_memory`; do - if ! online_memory_expect_success $memory; then - retval=1 + local actual_ret block + local expected_ret=$2 + local -n blocks=$1 + local ret=0 + + for block in "${blocks[@]}"; do + offline_memory "$block" 2>/dev/null + actual_ret=$? + if [ "$actual_ret" -ne "$expected_ret" ]; then + ktap_print_msg \ + "Offlining memory block $block returned $actual_ret," \ + "expected $expected_ret" + ret=1 + elif [ "$expected_ret" -eq 0 ] && ! memory_is_offline "$block"; then + ktap_print_msg "Memory block $block remained online" + ret=1 + elif [ "$expected_ret" -ne 0 ] && ! memory_is_online "$block"; then + ktap_print_msg "Memory block $block was offlined unexpectedly" + ret=1 fi done + return $ret } -error=-12 -priority=0 -# Run with default of ratio=2 for Kselftest run -ratio=2 -retval=0 +find_test_memory_blocks() +{ + local -a candidates + local cur_block path + local -n blocks=$1 + local target -while getopts e:hp:r: opt; do - case $opt in - e) - error=$OPTARG - ;; - h) - echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]" - exit - ;; - p) - priority=$OPTARG - ;; - r) - ratio=$OPTARG - if [ "$ratio" -gt 100 ] || [ "$ratio" -lt 0 ]; then - echo "The percentage should be an integer within 0~100 range" - exit 1 + for path in "$SYSFS"/devices/system/memory/memory[0-9]*; do + candidates+=("${path##*/memory}") + done + target=$(((${#candidates[@]} + 1) / 2)) + [ "$target" -gt "$MAX_BLOCKS" ] && target=$MAX_BLOCKS + blocks=() + ktap_print_msg "Trying to offline up to $target memory block(s):" + + for cur_block in "${candidates[@]}"; do + [ "${#blocks[@]}" -ge "$target" ] && break + if memory_is_offline "$cur_block"; then + ktap_print_msg "$cur_block is already offline" + INITIAL_STATE[$cur_block]=offline + blocks+=("$cur_block") fi - ;; - esac -done - -if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then - echo "error code must be -4095 <= errno < 0" >&2 - exit 1 -fi - -prerequisite - -echo "Test scope: $ratio% hotplug memory" - -# -# Online all hot-pluggable memory -# -hotpluggable_num=`hotpluggable_offline_memory | wc -l` -echo -e "\t online all hot-pluggable memory in offline state:" -if [ "$hotpluggable_num" -gt 0 ]; then - for memory in `hotpluggable_offline_memory`; do - echo "offline->online memory$memory" - if ! online_memory_expect_success $memory; then - retval=1 + done + for cur_block in "${candidates[@]}"; do + [ "${#blocks[@]}" -ge "$target" ] && break + memory_is_offline "$cur_block" && continue + memory_is_online "$cur_block" || continue + if offline_memory_blocks cur_block 0 &>/dev/null; then + ktap_print_msg "Offlining $cur_block succeeded" + INITIAL_STATE[$cur_block]=online + blocks+=("$cur_block") fi done -else - echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state" -fi + [ "${#blocks[@]}" -gt 0 ] +} -# -# Offline $ratio percent of hot-pluggable memory -# -hotpluggable_num=`hotpluggable_online_memory | wc -l` -target=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc` -echo -e "\t offline $ratio% hot-pluggable memory in online state" -echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):" -for memory in `hotpluggable_online_memory`; do - if [ "$target" -gt 0 ]; then - echo "online->offline memory$memory" - if offline_memory_expect_success $memory &>/dev/null; then - target=$(($target - 1)) - echo "-> Success" +restore_memory_blocks() +{ + local block ret=0 + for block in "${test_memory[@]}"; do + if [ "${INITIAL_STATE[$block]}" = offline ]; then + memory_is_offline "$block" || offline_memory "$block" || ret=1 else - echo "-> Failure" - fi - fi -done -if [ "$target" -gt 0 ]; then - retval=1 - echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?" -fi - -# -# Online all hot-pluggable memory again -# -hotpluggable_num=`hotpluggable_offline_memory | wc -l` -echo -e "\t online all hot-pluggable memory in offline state:" -if [ "$hotpluggable_num" -gt 0 ]; then - for memory in `hotpluggable_offline_memory`; do - echo "offline->online memory$memory" - if ! online_memory_expect_success $memory; then - retval=1 + memory_is_online "$block" || online_memory "$block" kernel || ret=1 fi done -else - echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state" -fi - -# -# Test with memory notifier error injection -# - -DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'` -NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory + return $ret +} -prerequisite_extra() +cleanup() { - msg="skip extra tests:" - - /sbin/modprobe -q -r memory-notifier-error-inject - /sbin/modprobe -q memory-notifier-error-inject priority=$priority - - if [ ! -d "$DEBUGFS" ]; then - echo $msg debugfs is not mounted >&2 - exit $retval - fi - - if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then - echo $msg memory-notifier-error-inject module is not available >&2 - exit $retval + if [ -n "$NOTIFIER_ERR_INJECT_DIR" ]; then + echo 0 > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error" 2>/dev/null + echo 0 > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error" 2>/dev/null + /sbin/modprobe -q -r memory-notifier-error-inject fi + restore_memory_blocks >/dev/null 2>&1 } -echo -e "\t Test with memory notifier error injection" -prerequisite_extra - -# -# Offline $ratio percent of hot-pluggable memory -# -echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error -for memory in `hotpluggable_online_memory`; do - if [ $((RANDOM % 100)) -lt $ratio ]; then - offline_memory_expect_success $memory &>/dev/null - fi -done - -# -# Test memory hot-add error handling (offline => online) -# -echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error -for memory in `hotpluggable_offline_memory`; do - if ! online_memory_expect_fail $memory; then - retval=1 - fi -done - -# -# Online all hot-pluggable memory -# -echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error -online_all_offline_memory - -# -# Test memory hot-remove error handling (online => offline) -# -echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error -for memory in `hotpluggable_online_memory`; do - if [ $((RANDOM % 100)) -lt $ratio ]; then - if ! offline_memory_expect_fail $memory; then - retval=1 - fi - fi -done - -echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error -/sbin/modprobe -q -r memory-notifier-error-inject +ktap_print_header +prerequisite +find_test_memory_blocks test_memory +if [ "${#test_memory[@]}" -eq 0 ]; then + ktap_skip_all "unable to offline any memory" + exit "$KSFT_SKIP" +fi -# -# Restore memory before exit -# -online_all_offline_memory +trap cleanup EXIT +if [ -n "$NOTIFIER_ERR_INJECT_DIR" ]; then + ktap_set_plan 7 +else + ktap_set_plan 3 +fi +ktap_print_msg "Test scope: ${#test_memory[@]} memory block(s)" + +ktap_test_result "Online memory to ZONE_MOVABLE" \ + online_memory_blocks test_memory movable 0 +ktap_test_result "Offline memory" \ + offline_memory_blocks test_memory 0 + +if [ -n "$NOTIFIER_ERR_INJECT_DIR" ]; then + online_memory_blocks test_memory movable 0 + echo $ERR_INJECT_ERRNO > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error" + ktap_test_result "Offline memory with error injection" \ + offline_memory_blocks test_memory 1 + echo 0 > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error" + ktap_test_result "Offline memory" \ + offline_memory_blocks test_memory 0 + echo $ERR_INJECT_ERRNO > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error" + ktap_test_result "Reonline memory with error injection" \ + online_memory_blocks test_memory kernel 1 + ktap_test_result "Reonline memory with error injection to ZONE_MOVABLE" \ + online_memory_blocks test_memory movable 1 + echo 0 > "$NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error" + /sbin/modprobe -q -r memory-notifier-error-inject + NOTIFIER_ERR_INJECT_DIR= +fi -exit $retval +ktap_test_result "Restore memory state" restore_memory_blocks +trap - EXIT +ktap_finished -- 2.34.1