Skip to content

Commit f1fea72

Browse files
joe-lawrenceshuahkh
authored andcommitted
selftests/livepatch: fix and refactor new dmesg message code
The livepatching kselftests rely on comparing expected vs. observed dmesg output. After each test, new dmesg entries are determined by the 'comm' utility comparing a saved, pre-test copy of dmesg to post-test dmesg output. Alexander reports that the 'comm --nocheck-order -13' invocation used by the tests can be confused when dmesg entry timestamps vary in magnitude (ie, "[ 98.820331]" vs. "[ 100.031067]"), in which case, additional messages are reported as new. The unexpected entries then spoil the test results. Instead of relying on 'comm' or 'diff' to determine new testing dmesg entries, refactor the code: - pre-test : log a unique canary dmesg entry - test : run tests, log messages - post-test : filter dmesg starting from pre-test message Reported-by: Alexander Gordeev <[email protected]> Closes: https://lore.kernel.org/live-patching/[email protected]/ Signed-off-by: Joe Lawrence <[email protected]> Acked-by: Alexander Gordeev <[email protected]> Tested-by: Marcos Paulo de Souza <[email protected]> Reviewed-by: Marcos Paulo de Souza <[email protected]> Acked-by: Miroslav Benes <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent d53271c commit f1fea72

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

tools/testing/selftests/livepatch/functions.sh

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ function die() {
4242
exit 1
4343
}
4444

45-
# save existing dmesg so we can detect new content
46-
function save_dmesg() {
47-
SAVED_DMESG=$(mktemp --tmpdir -t klp-dmesg-XXXXXX)
48-
dmesg > "$SAVED_DMESG"
49-
}
50-
51-
# cleanup temporary dmesg file from save_dmesg()
52-
function cleanup_dmesg_file() {
53-
rm -f "$SAVED_DMESG"
54-
}
55-
5645
function push_config() {
5746
DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
5847
awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
@@ -99,7 +88,6 @@ function set_ftrace_enabled() {
9988

10089
function cleanup() {
10190
pop_config
102-
cleanup_dmesg_file
10391
}
10492

10593
# setup_config - save the current config and set a script exit trap that
@@ -280,7 +268,15 @@ function set_pre_patch_ret {
280268
function start_test {
281269
local test="$1"
282270

283-
save_dmesg
271+
# Dump something unique into the dmesg log, then stash the entry
272+
# in LAST_DMESG. The check_result() function will use it to
273+
# find new kernel messages since the test started.
274+
local last_dmesg_msg="livepatch kselftest timestamp: $(date --rfc-3339=ns)"
275+
log "$last_dmesg_msg"
276+
loop_until 'dmesg | grep -q "$last_dmesg_msg"' ||
277+
die "buffer busy? can't find canary dmesg message: $last_dmesg_msg"
278+
LAST_DMESG=$(dmesg | grep "$last_dmesg_msg")
279+
284280
echo -n "TEST: $test ... "
285281
log "===== TEST: $test ====="
286282
}
@@ -291,23 +287,24 @@ function check_result {
291287
local expect="$*"
292288
local result
293289

294-
# Note: when comparing dmesg output, the kernel log timestamps
295-
# help differentiate repeated testing runs. Remove them with a
296-
# post-comparison sed filter.
297-
298-
result=$(dmesg | comm --nocheck-order -13 "$SAVED_DMESG" - | \
290+
# Test results include any new dmesg entry since LAST_DMESG, then:
291+
# - include lines matching keywords
292+
# - exclude lines matching keywords
293+
# - filter out dmesg timestamp prefixes
294+
result=$(dmesg | awk -v last_dmesg="$LAST_DMESG" 'p; $0 == last_dmesg { p=1 }' | \
299295
grep -e 'livepatch:' -e 'test_klp' | \
300296
grep -v '\(tainting\|taints\) kernel' | \
301297
sed 's/^\[[ 0-9.]*\] //')
302298

303299
if [[ "$expect" == "$result" ]] ; then
304300
echo "ok"
301+
elif [[ "$result" == "" ]] ; then
302+
echo -e "not ok\n\nbuffer overrun? can't find canary dmesg entry: $LAST_DMESG\n"
303+
die "livepatch kselftest(s) failed"
305304
else
306305
echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
307306
die "livepatch kselftest(s) failed"
308307
fi
309-
310-
cleanup_dmesg_file
311308
}
312309

313310
# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs

0 commit comments

Comments
 (0)