treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / livepatch / functions.sh
blob2aab9791791d4b0afdeced0516f4164bd7cb093c
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
5 # Shell functions for the rest of the scripts.
7 MAX_RETRIES=600
8 RETRY_INTERVAL=".1" # seconds
10 # Kselftest framework requirement - SKIP code is 4
11 ksft_skip=4
13 # log(msg) - write message to kernel log
14 # msg - insightful words
15 function log() {
16 echo "$1" > /dev/kmsg
19 # skip(msg) - testing can't proceed
20 # msg - explanation
21 function skip() {
22 log "SKIP: $1"
23 echo "SKIP: $1" >&2
24 exit $ksft_skip
27 # root test
28 function is_root() {
29 uid=$(id -u)
30 if [ $uid -ne 0 ]; then
31 echo "skip all tests: must be run as root" >&2
32 exit $ksft_skip
36 # die(msg) - game over, man
37 # msg - dying words
38 function die() {
39 log "ERROR: $1"
40 echo "ERROR: $1" >&2
41 exit 1
44 function push_config() {
45 DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
46 awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
47 FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
50 function pop_config() {
51 if [[ -n "$DYNAMIC_DEBUG" ]]; then
52 echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
54 if [[ -n "$FTRACE_ENABLED" ]]; then
55 sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
59 function set_dynamic_debug() {
60 cat <<-EOF > /sys/kernel/debug/dynamic_debug/control
61 file kernel/livepatch/* +p
62 func klp_try_switch_task -p
63 EOF
66 function set_ftrace_enabled() {
67 result=$(sysctl kernel.ftrace_enabled="$1" 2>&1 | paste --serial --delimiters=' ')
68 echo "livepatch: $result" > /dev/kmsg
71 # setup_config - save the current config and set a script exit trap that
72 # restores the original config. Setup the dynamic debug
73 # for verbose livepatching output and turn on
74 # the ftrace_enabled sysctl.
75 function setup_config() {
76 is_root
77 push_config
78 set_dynamic_debug
79 set_ftrace_enabled 1
80 trap pop_config EXIT INT TERM HUP
83 # loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
84 # sleep $RETRY_INTERVAL between attempts
85 # cmd - command and its arguments to run
86 function loop_until() {
87 local cmd="$*"
88 local i=0
89 while true; do
90 eval "$cmd" && return 0
91 [[ $((i++)) -eq $MAX_RETRIES ]] && return 1
92 sleep $RETRY_INTERVAL
93 done
96 function assert_mod() {
97 local mod="$1"
99 modprobe --dry-run "$mod" &>/dev/null
102 function is_livepatch_mod() {
103 local mod="$1"
105 if [[ $(modinfo "$mod" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then
106 return 0
109 return 1
112 function __load_mod() {
113 local mod="$1"; shift
115 local msg="% modprobe $mod $*"
116 log "${msg%% }"
117 ret=$(modprobe "$mod" "$@" 2>&1)
118 if [[ "$ret" != "" ]]; then
119 die "$ret"
122 # Wait for module in sysfs ...
123 loop_until '[[ -e "/sys/module/$mod" ]]' ||
124 die "failed to load module $mod"
128 # load_mod(modname, params) - load a kernel module
129 # modname - module name to load
130 # params - module parameters to pass to modprobe
131 function load_mod() {
132 local mod="$1"; shift
134 assert_mod "$mod" ||
135 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
137 is_livepatch_mod "$mod" &&
138 die "use load_lp() to load the livepatch module $mod"
140 __load_mod "$mod" "$@"
143 # load_lp_nowait(modname, params) - load a kernel module with a livepatch
144 # but do not wait on until the transition finishes
145 # modname - module name to load
146 # params - module parameters to pass to modprobe
147 function load_lp_nowait() {
148 local mod="$1"; shift
150 assert_mod "$mod" ||
151 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
153 is_livepatch_mod "$mod" ||
154 die "module $mod is not a livepatch"
156 __load_mod "$mod" "$@"
158 # Wait for livepatch in sysfs ...
159 loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
160 die "failed to load module $mod (sysfs)"
163 # load_lp(modname, params) - load a kernel module with a livepatch
164 # modname - module name to load
165 # params - module parameters to pass to modprobe
166 function load_lp() {
167 local mod="$1"; shift
169 load_lp_nowait "$mod" "$@"
171 # Wait until the transition finishes ...
172 loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
173 die "failed to complete transition"
176 # load_failing_mod(modname, params) - load a kernel module, expect to fail
177 # modname - module name to load
178 # params - module parameters to pass to modprobe
179 function load_failing_mod() {
180 local mod="$1"; shift
182 local msg="% modprobe $mod $*"
183 log "${msg%% }"
184 ret=$(modprobe "$mod" "$@" 2>&1)
185 if [[ "$ret" == "" ]]; then
186 die "$mod unexpectedly loaded"
188 log "$ret"
191 # unload_mod(modname) - unload a kernel module
192 # modname - module name to unload
193 function unload_mod() {
194 local mod="$1"
196 # Wait for module reference count to clear ...
197 loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' ||
198 die "failed to unload module $mod (refcnt)"
200 log "% rmmod $mod"
201 ret=$(rmmod "$mod" 2>&1)
202 if [[ "$ret" != "" ]]; then
203 die "$ret"
206 # Wait for module in sysfs ...
207 loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
208 die "failed to unload module $mod (/sys/module)"
211 # unload_lp(modname) - unload a kernel module with a livepatch
212 # modname - module name to unload
213 function unload_lp() {
214 unload_mod "$1"
217 # disable_lp(modname) - disable a livepatch
218 # modname - module name to unload
219 function disable_lp() {
220 local mod="$1"
222 log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
223 echo 0 > /sys/kernel/livepatch/"$mod"/enabled
225 # Wait until the transition finishes and the livepatch gets
226 # removed from sysfs...
227 loop_until '[[ ! -e "/sys/kernel/livepatch/$mod" ]]' ||
228 die "failed to disable livepatch $mod"
231 # set_pre_patch_ret(modname, pre_patch_ret)
232 # modname - module name to set
233 # pre_patch_ret - new pre_patch_ret value
234 function set_pre_patch_ret {
235 local mod="$1"; shift
236 local ret="$1"
238 log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
239 echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
241 # Wait for sysfs value to hold ...
242 loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' ||
243 die "failed to set pre_patch_ret parameter for $mod module"
246 # check_result() - verify dmesg output
247 # TODO - better filter, out of order msgs, etc?
248 function check_result {
249 local expect="$*"
250 local result
252 result=$(dmesg | grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | sed 's/^\[[ 0-9.]*\] //')
254 if [[ "$expect" == "$result" ]] ; then
255 echo "ok"
256 else
257 echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
258 die "livepatch kselftest(s) failed"