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.
8 RETRY_INTERVAL
=".1" # seconds
10 # Kselftest framework requirement - SKIP code is 4
13 # log(msg) - write message to kernel log
14 # msg - insightful words
19 # skip(msg) - testing can't proceed
30 if [ $uid -ne 0 ]; then
31 echo "skip all tests: must be run as root" >&2
36 # die(msg) - game over, man
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
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
() {
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
() {
90 eval "$cmd" && return 0
91 [[ $
((i
++)) -eq $MAX_RETRIES ]] && return 1
96 function assert_mod
() {
99 modprobe
--dry-run "$mod" &>/dev
/null
102 function is_livepatch_mod
() {
105 if [[ $
(modinfo
"$mod" |
awk '/^livepatch:/{print $NF}') == "Y" ]]; then
112 function __load_mod
() {
113 local mod
="$1"; shift
115 local msg
="% modprobe $mod $*"
117 ret
=$
(modprobe
"$mod" "$@" 2>&1)
118 if [[ "$ret" != "" ]]; then
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
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
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
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 $*"
184 ret
=$
(modprobe
"$mod" "$@" 2>&1)
185 if [[ "$ret" == "" ]]; then
186 die
"$mod unexpectedly loaded"
191 # unload_mod(modname) - unload a kernel module
192 # modname - module name to unload
193 function unload_mod
() {
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)"
201 ret
=$
(rmmod
"$mod" 2>&1)
202 if [[ "$ret" != "" ]]; then
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
() {
217 # disable_lp(modname) - disable a livepatch
218 # modname - module name to unload
219 function disable_lp
() {
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
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
{
252 result
=$
(dmesg |
grep -v 'tainting' |
grep -e 'livepatch:' -e 'test_klp' |
sed 's/^\[[ 0-9.]*\] //')
254 if [[ "$expect" == "$result" ]] ; then
257 echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
258 die
"livepatch kselftest(s) failed"