1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4
5# Shell functions for the rest of the scripts.
6
7MAX_RETRIES=600
8RETRY_INTERVAL=".1"	# seconds
9
10# Kselftest framework requirement - SKIP code is 4
11ksft_skip=4
12
13# log(msg) - write message to kernel log
14#	msg - insightful words
15function log() {
16	echo "$1" > /dev/kmsg
17}
18
19# skip(msg) - testing can't proceed
20#	msg - explanation
21function skip() {
22	log "SKIP: $1"
23	echo "SKIP: $1" >&2
24	exit $ksft_skip
25}
26
27# root test
28function 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
33	fi
34}
35
36# die(msg) - game over, man
37#	msg - dying words
38function die() {
39	log "ERROR: $1"
40	echo "ERROR: $1" >&2
41	exit 1
42}
43
44function 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)
48}
49
50function pop_config() {
51	if [[ -n "$DYNAMIC_DEBUG" ]]; then
52		echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
53	fi
54	if [[ -n "$FTRACE_ENABLED" ]]; then
55		sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
56	fi
57}
58
59function 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
64}
65
66function set_ftrace_enabled() {
67	result=$(sysctl kernel.ftrace_enabled="$1" 2>&1 | paste --serial --delimiters=' ')
68	echo "livepatch: $result" > /dev/kmsg
69}
70
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.
75function 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
81}
82
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
86function 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
94}
95
96function assert_mod() {
97	local mod="$1"
98
99	modprobe --dry-run "$mod" &>/dev/null
100}
101
102function is_livepatch_mod() {
103	local mod="$1"
104
105	if [[ $(modinfo "$mod" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then
106		return 0
107	fi
108
109	return 1
110}
111
112function __load_mod() {
113	local mod="$1"; shift
114
115	local msg="% modprobe $mod $*"
116	log "${msg%% }"
117	ret=$(modprobe "$mod" "$@" 2>&1)
118	if [[ "$ret" != "" ]]; then
119		die "$ret"
120	fi
121
122	# Wait for module in sysfs ...
123	loop_until '[[ -e "/sys/module/$mod" ]]' ||
124		die "failed to load module $mod"
125}
126
127
128# load_mod(modname, params) - load a kernel module
129#	modname - module name to load
130#	params  - module parameters to pass to modprobe
131function load_mod() {
132	local mod="$1"; shift
133
134	assert_mod "$mod" ||
135		skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
136
137	is_livepatch_mod "$mod" &&
138		die "use load_lp() to load the livepatch module $mod"
139
140	__load_mod "$mod" "$@"
141}
142
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
147function load_lp_nowait() {
148	local mod="$1"; shift
149
150	assert_mod "$mod" ||
151		skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
152
153	is_livepatch_mod "$mod" ||
154		die "module $mod is not a livepatch"
155
156	__load_mod "$mod" "$@"
157
158	# Wait for livepatch in sysfs ...
159	loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
160		die "failed to load module $mod (sysfs)"
161}
162
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
166function load_lp() {
167	local mod="$1"; shift
168
169	load_lp_nowait "$mod" "$@"
170
171	# Wait until the transition finishes ...
172	loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
173		die "failed to complete transition"
174}
175
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
179function load_failing_mod() {
180	local mod="$1"; shift
181
182	local msg="% modprobe $mod $*"
183	log "${msg%% }"
184	ret=$(modprobe "$mod" "$@" 2>&1)
185	if [[ "$ret" == "" ]]; then
186		die "$mod unexpectedly loaded"
187	fi
188	log "$ret"
189}
190
191# unload_mod(modname) - unload a kernel module
192#	modname - module name to unload
193function unload_mod() {
194	local mod="$1"
195
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)"
199
200	log "% rmmod $mod"
201	ret=$(rmmod "$mod" 2>&1)
202	if [[ "$ret" != "" ]]; then
203		die "$ret"
204	fi
205
206	# Wait for module in sysfs ...
207	loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
208		die "failed to unload module $mod (/sys/module)"
209}
210
211# unload_lp(modname) - unload a kernel module with a livepatch
212#	modname - module name to unload
213function unload_lp() {
214	unload_mod "$1"
215}
216
217# disable_lp(modname) - disable a livepatch
218#	modname - module name to unload
219function disable_lp() {
220	local mod="$1"
221
222	log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
223	echo 0 > /sys/kernel/livepatch/"$mod"/enabled
224
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"
229}
230
231# set_pre_patch_ret(modname, pre_patch_ret)
232#	modname - module name to set
233#	pre_patch_ret - new pre_patch_ret value
234function set_pre_patch_ret {
235	local mod="$1"; shift
236	local ret="$1"
237
238	log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
239	echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
240
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"
244}
245
246# check_result() - verify dmesg output
247#	TODO - better filter, out of order msgs, etc?
248function check_result {
249	local expect="$*"
250	local result
251
252	result=$(dmesg | grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | sed 's/^\[[ 0-9.]*\] //')
253
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"
259	fi
260}
261