Linux Audio

Check our new training course

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