^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # Shell functions for the rest of the scripts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) MAX_RETRIES=600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) RETRY_INTERVAL=".1" # seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # Kselftest framework requirement - SKIP code is 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ksft_skip=4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # log(msg) - write message to kernel log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # msg - insightful words
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) function log() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) echo "$1" > /dev/kmsg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # skip(msg) - testing can't proceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # msg - explanation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) function skip() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) log "SKIP: $1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) echo "SKIP: $1" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) exit $ksft_skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # root test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) function is_root() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) uid=$(id -u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if [ $uid -ne 0 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) echo "skip all tests: must be run as root" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) exit $ksft_skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # die(msg) - game over, man
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) # msg - dying words
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) function die() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) log "ERROR: $1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) echo "ERROR: $1" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # save existing dmesg so we can detect new content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) function save_dmesg() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) SAVED_DMESG=$(mktemp --tmpdir -t klp-dmesg-XXXXXX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) dmesg > "$SAVED_DMESG"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # cleanup temporary dmesg file from save_dmesg()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) function cleanup_dmesg_file() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) rm -f "$SAVED_DMESG"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) function push_config() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) function pop_config() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if [[ -n "$DYNAMIC_DEBUG" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if [[ -n "$FTRACE_ENABLED" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) function set_dynamic_debug() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) cat <<-EOF > /sys/kernel/debug/dynamic_debug/control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) file kernel/livepatch/* +p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) func klp_try_switch_task -p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) function set_ftrace_enabled() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) result=$(sysctl -q kernel.ftrace_enabled="$1" 2>&1 && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sysctl kernel.ftrace_enabled 2>&1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) echo "livepatch: $result" > /dev/kmsg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) function cleanup() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pop_config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cleanup_dmesg_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # setup_config - save the current config and set a script exit trap that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) # restores the original config. Setup the dynamic debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # for verbose livepatching output and turn on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # the ftrace_enabled sysctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) function setup_config() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) is_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) push_config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) set_dynamic_debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) set_ftrace_enabled 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) trap cleanup EXIT INT TERM HUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) # loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) # sleep $RETRY_INTERVAL between attempts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) # cmd - command and its arguments to run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) function loop_until() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) local cmd="$*"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) local i=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) while true; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) eval "$cmd" && return 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) [[ $((i++)) -eq $MAX_RETRIES ]] && return 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sleep $RETRY_INTERVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) function assert_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) local mod="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) modprobe --dry-run "$mod" &>/dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) function is_livepatch_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) local mod="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if [[ $(modinfo "$mod" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) function __load_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) local msg="% modprobe $mod $*"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) log "${msg%% }"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ret=$(modprobe "$mod" "$@" 2>&1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if [[ "$ret" != "" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) die "$ret"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) # Wait for module in sysfs ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) loop_until '[[ -e "/sys/module/$mod" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) die "failed to load module $mod"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) # load_mod(modname, params) - load a kernel module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) # modname - module name to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) # params - module parameters to pass to modprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) function load_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) assert_mod "$mod" ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) is_livepatch_mod "$mod" &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) die "use load_lp() to load the livepatch module $mod"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) __load_mod "$mod" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) # load_lp_nowait(modname, params) - load a kernel module with a livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) # but do not wait on until the transition finishes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) # modname - module name to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) # params - module parameters to pass to modprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) function load_lp_nowait() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) assert_mod "$mod" ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) is_livepatch_mod "$mod" ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) die "module $mod is not a livepatch"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) __load_mod "$mod" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) # Wait for livepatch in sysfs ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) die "failed to load module $mod (sysfs)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) # load_lp(modname, params) - load a kernel module with a livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) # modname - module name to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) # params - module parameters to pass to modprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) function load_lp() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) load_lp_nowait "$mod" "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) # Wait until the transition finishes ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) die "failed to complete transition"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) # load_failing_mod(modname, params) - load a kernel module, expect to fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) # modname - module name to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) # params - module parameters to pass to modprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) function load_failing_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) local msg="% modprobe $mod $*"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) log "${msg%% }"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret=$(modprobe "$mod" "$@" 2>&1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if [[ "$ret" == "" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) die "$mod unexpectedly loaded"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) log "$ret"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) # unload_mod(modname) - unload a kernel module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) # modname - module name to unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) function unload_mod() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) local mod="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) # Wait for module reference count to clear ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) die "failed to unload module $mod (refcnt)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) log "% rmmod $mod"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret=$(rmmod "$mod" 2>&1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if [[ "$ret" != "" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) die "$ret"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) # Wait for module in sysfs ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) die "failed to unload module $mod (/sys/module)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) # unload_lp(modname) - unload a kernel module with a livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) # modname - module name to unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) function unload_lp() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unload_mod "$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) # disable_lp(modname) - disable a livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) # modname - module name to unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) function disable_lp() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) local mod="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) echo 0 > /sys/kernel/livepatch/"$mod"/enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) # Wait until the transition finishes and the livepatch gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) # removed from sysfs...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) loop_until '[[ ! -e "/sys/kernel/livepatch/$mod" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) die "failed to disable livepatch $mod"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) # set_pre_patch_ret(modname, pre_patch_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) # modname - module name to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) # pre_patch_ret - new pre_patch_ret value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) function set_pre_patch_ret {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) local mod="$1"; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) local ret="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) # Wait for sysfs value to hold ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) die "failed to set pre_patch_ret parameter for $mod module"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) function start_test {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) local test="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) save_dmesg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) echo -n "TEST: $test ... "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) log "===== TEST: $test ====="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) # check_result() - verify dmesg output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) # TODO - better filter, out of order msgs, etc?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) function check_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) local expect="$*"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) local result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) # Note: when comparing dmesg output, the kernel log timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) # help differentiate repeated testing runs. Remove them with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) # post-comparison sed filter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) result=$(dmesg | comm --nocheck-order -13 "$SAVED_DMESG" - | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) grep -e 'livepatch:' -e 'test_klp' | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) grep -v '\(tainting\|taints\) kernel' | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) sed 's/^\[[ 0-9.]*\] //')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if [[ "$expect" == "$result" ]] ; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) echo "ok"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) die "livepatch kselftest(s) failed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) cleanup_dmesg_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }