Skip to content

Commit 2eeb0d4

Browse files
joe-lawrencepmladek
authored andcommitted
selftests/livepatch: Don't clear dmesg when running tests
Inspired by commit f131d9e ("selftests/lkdtm: Don't clear dmesg when running tests"), keep a reference dmesg copy when beginning each test. This way check_result() can compare against the initial copy rather than relying upon an empty log. Signed-off-by: Joe Lawrence <[email protected]> Reviewed-by: Kamalesh Babulal <[email protected]> Reviewed-by: Yannick Cote <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Acked-by: Miroslav Benes <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 270f780 commit 2eeb0d4

File tree

7 files changed

+66
-81
lines changed

7 files changed

+66
-81
lines changed

tools/testing/selftests/livepatch/README

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This is a small set of sanity tests for the kernel livepatching.
66

77
The test suite loads and unloads several test kernel modules to verify
88
livepatch behavior. Debug information is logged to the kernel's message
9-
buffer and parsed for expected messages. (Note: the tests will clear
10-
the message buffer between individual tests.)
9+
buffer and parsed for expected messages. (Note: the tests will compare
10+
the message buffer for only the duration of each individual test.)
1111

1212

1313
Config
@@ -35,9 +35,9 @@ Adding tests
3535
------------
3636

3737
See the common functions.sh file for the existing collection of utility
38-
functions, most importantly setup_config() and check_result(). The
39-
latter function greps the kernel's ring buffer for "livepatch:" and
40-
"test_klp" strings, so tests be sure to include one of those strings for
41-
result comparison. Other utility functions include general module
42-
loading and livepatch loading helpers (waiting for patch transitions,
43-
sysfs entries, etc.)
38+
functions, most importantly setup_config(), start_test() and
39+
check_result(). The latter function greps the kernel's ring buffer for
40+
"livepatch:" and "test_klp" strings, so tests be sure to include one of
41+
those strings for result comparison. Other utility functions include
42+
general module loading and livepatch loading helpers (waiting for patch
43+
transitions, sysfs entries, etc.)

tools/testing/selftests/livepatch/functions.sh

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ function die() {
4141
exit 1
4242
}
4343

44+
# save existing dmesg so we can detect new content
45+
function 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()
51+
function cleanup_dmesg_file() {
52+
rm -f "$SAVED_DMESG"
53+
}
54+
4455
function push_config() {
4556
DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
4657
awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
@@ -68,6 +79,11 @@ function set_ftrace_enabled() {
6879
echo "livepatch: $result" > /dev/kmsg
6980
}
7081

82+
function cleanup() {
83+
pop_config
84+
cleanup_dmesg_file
85+
}
86+
7187
# setup_config - save the current config and set a script exit trap that
7288
# restores the original config. Setup the dynamic debug
7389
# for verbose livepatching output and turn on
@@ -77,7 +93,7 @@ function setup_config() {
7793
push_config
7894
set_dynamic_debug
7995
set_ftrace_enabled 1
80-
trap pop_config EXIT INT TERM HUP
96+
trap cleanup EXIT INT TERM HUP
8197
}
8298

8399
# loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
@@ -243,18 +259,33 @@ function set_pre_patch_ret {
243259
die "failed to set pre_patch_ret parameter for $mod module"
244260
}
245261

262+
function start_test {
263+
local test="$1"
264+
265+
save_dmesg
266+
echo -n "TEST: $test ... "
267+
}
268+
246269
# check_result() - verify dmesg output
247270
# TODO - better filter, out of order msgs, etc?
248271
function check_result {
249272
local expect="$*"
250273
local result
251274

252-
result=$(dmesg | grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | sed 's/^\[[ 0-9.]*\] //')
275+
# Note: when comparing dmesg output, the kernel log timestamps
276+
# help differentiate repeated testing runs. Remove them with a
277+
# post-comparison sed filter.
278+
279+
result=$(dmesg | diff --changed-group-format='%>' --unchanged-group-format='' "$SAVED_DMESG" - | \
280+
grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | \
281+
sed 's/^\[[ 0-9.]*\] //')
253282

254283
if [[ "$expect" == "$result" ]] ; then
255284
echo "ok"
256285
else
257286
echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
258287
die "livepatch kselftest(s) failed"
259288
fi
289+
290+
cleanup_dmesg_file
260291
}

tools/testing/selftests/livepatch/test-callbacks.sh

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ MOD_TARGET_BUSY=test_klp_callbacks_busy
1212
setup_config
1313

1414

15-
# TEST: target module before livepatch
16-
#
1715
# Test a combination of loading a kernel module and a livepatch that
1816
# patches a function in the first module. Load the target module
1917
# before the livepatch module. Unload them in the same order.
@@ -28,8 +26,7 @@ setup_config
2826
# unpatching transition starts. klp_objects are reverted, post-patch
2927
# callbacks execute and the transition completes.
3028

31-
echo -n "TEST: target module before livepatch ... "
32-
dmesg -C
29+
start_test "target module before livepatch"
3330

3431
load_mod $MOD_TARGET
3532
load_lp $MOD_LIVEPATCH
@@ -63,8 +60,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
6360
$MOD_TARGET: ${MOD_TARGET}_exit"
6461

6562

66-
# TEST: module_coming notifier
67-
#
6863
# This test is similar to the previous test, but (un)load the livepatch
6964
# module before the target kernel module. This tests the livepatch
7065
# core's module_coming handler.
@@ -78,8 +73,7 @@ $MOD_TARGET: ${MOD_TARGET}_exit"
7873
# - On livepatch disable, all currently loaded klp_objects' (vmlinux and
7974
# $MOD_TARGET) pre/post-unpatch callbacks are executed.
8075

81-
echo -n "TEST: module_coming notifier ... "
82-
dmesg -C
76+
start_test "module_coming notifier"
8377

8478
load_lp $MOD_LIVEPATCH
8579
load_mod $MOD_TARGET
@@ -114,8 +108,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
114108
$MOD_TARGET: ${MOD_TARGET}_exit"
115109

116110

117-
# TEST: module_going notifier
118-
#
119111
# Test loading the livepatch after a targeted kernel module, then unload
120112
# the kernel module before disabling the livepatch. This tests the
121113
# livepatch core's module_going handler.
@@ -129,8 +121,7 @@ $MOD_TARGET: ${MOD_TARGET}_exit"
129121
# - When the livepatch is disabled, pre and post-unpatch callbacks are
130122
# run for the remaining klp_object, vmlinux.
131123

132-
echo -n "TEST: module_going notifier ... "
133-
dmesg -C
124+
start_test "module_going notifier"
134125

135126
load_mod $MOD_TARGET
136127
load_lp $MOD_LIVEPATCH
@@ -165,8 +156,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
165156
% rmmod $MOD_LIVEPATCH"
166157

167158

168-
# TEST: module_coming and module_going notifiers
169-
#
170159
# This test is similar to the previous test, however the livepatch is
171160
# loaded first. This tests the livepatch core's module_coming and
172161
# module_going handlers.
@@ -180,8 +169,7 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
180169
# from the $MOD_TARGET klp_object. As such, only pre and
181170
# post-unpatch callbacks are executed when this occurs.
182171

183-
echo -n "TEST: module_coming and module_going notifiers ... "
184-
dmesg -C
172+
start_test "module_coming and module_going notifiers"
185173

186174
load_lp $MOD_LIVEPATCH
187175
load_mod $MOD_TARGET
@@ -217,8 +205,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
217205
% rmmod $MOD_LIVEPATCH"
218206

219207

220-
# TEST: target module not present
221-
#
222208
# A simple test of loading a livepatch without one of its patch target
223209
# klp_objects ever loaded ($MOD_TARGET).
224210
#
@@ -227,8 +213,7 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
227213
# - As expected, only pre/post-(un)patch handlers are executed for
228214
# vmlinux.
229215

230-
echo -n "TEST: target module not present ... "
231-
dmesg -C
216+
start_test "target module not present"
232217

233218
load_lp $MOD_LIVEPATCH
234219
disable_lp $MOD_LIVEPATCH
@@ -252,8 +237,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
252237
% rmmod $MOD_LIVEPATCH"
253238

254239

255-
# TEST: pre-patch callback -ENODEV
256-
#
257240
# Test a scenario where a vmlinux pre-patch callback returns a non-zero
258241
# status (ie, failure).
259242
#
@@ -265,8 +248,7 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
265248
# The result is that the insmod command refuses to load the livepatch
266249
# module.
267250

268-
echo -n "TEST: pre-patch callback -ENODEV ... "
269-
dmesg -C
251+
start_test "pre-patch callback -ENODEV"
270252

271253
load_mod $MOD_TARGET
272254
load_failing_mod $MOD_LIVEPATCH pre_patch_ret=-19
@@ -288,8 +270,6 @@ modprobe: ERROR: could not insert '$MOD_LIVEPATCH': No such device
288270
$MOD_TARGET: ${MOD_TARGET}_exit"
289271

290272

291-
# TEST: module_coming + pre-patch callback -ENODEV
292-
#
293273
# Similar to the previous test, setup a livepatch such that its vmlinux
294274
# pre-patch callback returns success. However, when a targeted kernel
295275
# module is later loaded, have the livepatch return a failing status
@@ -307,8 +287,7 @@ $MOD_TARGET: ${MOD_TARGET}_exit"
307287
#
308288
# - Pre/post-unpatch callbacks are run for the vmlinux klp_object.
309289

310-
echo -n "TEST: module_coming + pre-patch callback -ENODEV ... "
311-
dmesg -C
290+
start_test "module_coming + pre-patch callback -ENODEV"
312291

313292
load_lp $MOD_LIVEPATCH
314293
set_pre_patch_ret $MOD_LIVEPATCH -19
@@ -341,8 +320,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
341320
% rmmod $MOD_LIVEPATCH"
342321

343322

344-
# TEST: multiple target modules
345-
#
346323
# Test loading multiple targeted kernel modules. This test-case is
347324
# mainly for comparing with the next test-case.
348325
#
@@ -353,8 +330,7 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
353330
# module. Post-patch callbacks are executed and the transition
354331
# completes quickly.
355332

356-
echo -n "TEST: multiple target modules ... "
357-
dmesg -C
333+
start_test "multiple target modules"
358334

359335
load_mod $MOD_TARGET_BUSY block_transition=N
360336
load_lp $MOD_LIVEPATCH
@@ -402,8 +378,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
402378
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
403379

404380

405-
# TEST: busy target module
406-
#
407381
# A similar test as the previous one, but force the "busy" kernel module
408382
# to block the livepatch transition.
409383
#
@@ -431,8 +405,7 @@ $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
431405
# klp_object's post-patch callbacks executed, the remaining
432406
# klp_object's pre-unpatch callbacks are skipped.
433407

434-
echo -n "TEST: busy target module ... "
435-
dmesg -C
408+
start_test "busy target module"
436409

437410
load_mod $MOD_TARGET_BUSY block_transition=Y
438411
load_lp_nowait $MOD_LIVEPATCH
@@ -478,17 +451,14 @@ $MOD_TARGET_BUSY: busymod_work_func exit
478451
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
479452

480453

481-
# TEST: multiple livepatches
482-
#
483454
# Test loading multiple livepatches. This test-case is mainly for comparing
484455
# with the next test-case.
485456
#
486457
# - Load and unload two livepatches, pre and post (un)patch callbacks
487458
# execute as each patch progresses through its (un)patching
488459
# transition.
489460

490-
echo -n "TEST: multiple livepatches ... "
491-
dmesg -C
461+
start_test "multiple livepatches"
492462

493463
load_lp $MOD_LIVEPATCH
494464
load_lp $MOD_LIVEPATCH2
@@ -531,8 +501,6 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
531501
% rmmod $MOD_LIVEPATCH"
532502

533503

534-
# TEST: atomic replace
535-
#
536504
# Load multiple livepatches, but the second as an 'atomic-replace'
537505
# patch. When the latter loads, the original livepatch should be
538506
# disabled and *none* of its pre/post-unpatch callbacks executed. On
@@ -547,8 +515,7 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
547515
# - Once the atomic replace module is loaded, only its pre and post
548516
# unpatch callbacks are executed.
549517

550-
echo -n "TEST: atomic replace ... "
551-
dmesg -C
518+
start_test "atomic replace"
552519

553520
load_lp $MOD_LIVEPATCH
554521
load_lp $MOD_LIVEPATCH2 replace=1

tools/testing/selftests/livepatch/test-ftrace.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ MOD_LIVEPATCH=test_klp_livepatch
99
setup_config
1010

1111

12-
# TEST: livepatch interaction with ftrace_enabled sysctl
1312
# - turn ftrace_enabled OFF and verify livepatches can't load
1413
# - turn ftrace_enabled ON and verify livepatch can load
1514
# - verify that ftrace_enabled can't be turned OFF while a livepatch is loaded
1615

17-
echo -n "TEST: livepatch interaction with ftrace_enabled sysctl ... "
18-
dmesg -C
16+
start_test "livepatch interaction with ftrace_enabled sysctl"
1917

2018
set_ftrace_enabled 0
2119
load_failing_mod $MOD_LIVEPATCH

tools/testing/selftests/livepatch/test-livepatch.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ MOD_REPLACE=test_klp_atomic_replace
1010
setup_config
1111

1212

13-
# TEST: basic function patching
1413
# - load a livepatch that modifies the output from /proc/cmdline and
1514
# verify correct behavior
1615
# - unload the livepatch and make sure the patch was removed
1716

18-
echo -n "TEST: basic function patching ... "
19-
dmesg -C
17+
start_test "basic function patching"
2018

2119
load_lp $MOD_LIVEPATCH
2220

@@ -47,15 +45,13 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
4745
% rmmod $MOD_LIVEPATCH"
4846

4947

50-
# TEST: multiple livepatches
5148
# - load a livepatch that modifies the output from /proc/cmdline and
5249
# verify correct behavior
5350
# - load another livepatch and verify that both livepatches are active
5451
# - unload the second livepatch and verify that the first is still active
5552
# - unload the first livepatch and verify none are active
5653

57-
echo -n "TEST: multiple livepatches ... "
58-
dmesg -C
54+
start_test "multiple livepatches"
5955

6056
load_lp $MOD_LIVEPATCH
6157

@@ -109,16 +105,14 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
109105
% rmmod $MOD_LIVEPATCH"
110106

111107

112-
# TEST: atomic replace livepatch
113108
# - load a livepatch that modifies the output from /proc/cmdline and
114109
# verify correct behavior
115110
# - load an atomic replace livepatch and verify that only the second is active
116111
# - remove the first livepatch and verify that the atomic replace livepatch
117112
# is still active
118113
# - remove the atomic replace livepatch and verify that none are active
119114

120-
echo -n "TEST: atomic replace livepatch ... "
121-
dmesg -C
115+
start_test "atomic replace livepatch"
122116

123117
load_lp $MOD_LIVEPATCH
124118

tools/testing/selftests/livepatch/test-shadow-vars.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ MOD_TEST=test_klp_shadow_vars
99
setup_config
1010

1111

12-
# TEST: basic shadow variable API
1312
# - load a module that exercises the shadow variable API
1413

15-
echo -n "TEST: basic shadow variable API ... "
16-
dmesg -C
14+
start_test "basic shadow variable API"
1715

1816
load_mod $MOD_TEST
1917
unload_mod $MOD_TEST

0 commit comments

Comments
 (0)