Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/livepatch: Don't clear dmesg when running tests

Inspired by commit f131d9edc29d ("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 <joe.lawrence@redhat.com>
Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Reviewed-by: Yannick Cote <ycote@redhat.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Acked-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20200618181040.21132-2-joe.lawrence@redhat.com

authored by

Joe Lawrence and committed by
Petr Mladek
2eeb0d45 270f7806

+66 -81
+8 -8
tools/testing/selftests/livepatch/README
··· 6 6 7 7 The test suite loads and unloads several test kernel modules to verify 8 8 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.) 11 11 12 12 13 13 Config ··· 35 35 ------------ 36 36 37 37 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.)
+33 -2
tools/testing/selftests/livepatch/functions.sh
··· 41 41 exit 1 42 42 } 43 43 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 + 44 55 function push_config() { 45 56 DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \ 46 57 awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}') ··· 79 68 echo "livepatch: $result" > /dev/kmsg 80 69 } 81 70 71 + function cleanup() { 72 + pop_config 73 + cleanup_dmesg_file 74 + } 75 + 82 76 # setup_config - save the current config and set a script exit trap that 83 77 # restores the original config. Setup the dynamic debug 84 78 # for verbose livepatching output and turn on ··· 93 77 push_config 94 78 set_dynamic_debug 95 79 set_ftrace_enabled 1 96 - trap pop_config EXIT INT TERM HUP 80 + trap cleanup EXIT INT TERM HUP 97 81 } 98 82 99 83 # loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES, ··· 259 243 die "failed to set pre_patch_ret parameter for $mod module" 260 244 } 261 245 246 + function start_test { 247 + local test="$1" 248 + 249 + save_dmesg 250 + echo -n "TEST: $test ... " 251 + } 252 + 262 253 # check_result() - verify dmesg output 263 254 # TODO - better filter, out of order msgs, etc? 264 255 function check_result { 265 256 local expect="$*" 266 257 local result 267 258 268 - result=$(dmesg | grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | sed 's/^\[[ 0-9.]*\] //') 259 + # Note: when comparing dmesg output, the kernel log timestamps 260 + # help differentiate repeated testing runs. Remove them with a 261 + # post-comparison sed filter. 262 + 263 + result=$(dmesg | diff --changed-group-format='%>' --unchanged-group-format='' "$SAVED_DMESG" - | \ 264 + grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | \ 265 + sed 's/^\[[ 0-9.]*\] //') 269 266 270 267 if [[ "$expect" == "$result" ]] ; then 271 268 echo "ok" ··· 286 257 echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n" 287 258 die "livepatch kselftest(s) failed" 288 259 fi 260 + 261 + cleanup_dmesg_file 289 262 }
+11 -44
tools/testing/selftests/livepatch/test-callbacks.sh
··· 12 12 setup_config 13 13 14 14 15 - # TEST: target module before livepatch 16 - # 17 15 # Test a combination of loading a kernel module and a livepatch that 18 16 # patches a function in the first module. Load the target module 19 17 # before the livepatch module. Unload them in the same order. ··· 26 28 # unpatching transition starts. klp_objects are reverted, post-patch 27 29 # callbacks execute and the transition completes. 28 30 29 - echo -n "TEST: target module before livepatch ... " 30 - dmesg -C 31 + start_test "target module before livepatch" 31 32 32 33 load_mod $MOD_TARGET 33 34 load_lp $MOD_LIVEPATCH ··· 60 63 $MOD_TARGET: ${MOD_TARGET}_exit" 61 64 62 65 63 - # TEST: module_coming notifier 64 - # 65 66 # This test is similar to the previous test, but (un)load the livepatch 66 67 # module before the target kernel module. This tests the livepatch 67 68 # core's module_coming handler. ··· 73 78 # - On livepatch disable, all currently loaded klp_objects' (vmlinux and 74 79 # $MOD_TARGET) pre/post-unpatch callbacks are executed. 75 80 76 - echo -n "TEST: module_coming notifier ... " 77 - dmesg -C 81 + start_test "module_coming notifier" 78 82 79 83 load_lp $MOD_LIVEPATCH 80 84 load_mod $MOD_TARGET ··· 108 114 $MOD_TARGET: ${MOD_TARGET}_exit" 109 115 110 116 111 - # TEST: module_going notifier 112 - # 113 117 # Test loading the livepatch after a targeted kernel module, then unload 114 118 # the kernel module before disabling the livepatch. This tests the 115 119 # livepatch core's module_going handler. ··· 121 129 # - When the livepatch is disabled, pre and post-unpatch callbacks are 122 130 # run for the remaining klp_object, vmlinux. 123 131 124 - echo -n "TEST: module_going notifier ... " 125 - dmesg -C 132 + start_test "module_going notifier" 126 133 127 134 load_mod $MOD_TARGET 128 135 load_lp $MOD_LIVEPATCH ··· 156 165 % rmmod $MOD_LIVEPATCH" 157 166 158 167 159 - # TEST: module_coming and module_going notifiers 160 - # 161 168 # This test is similar to the previous test, however the livepatch is 162 169 # loaded first. This tests the livepatch core's module_coming and 163 170 # module_going handlers. ··· 169 180 # from the $MOD_TARGET klp_object. As such, only pre and 170 181 # post-unpatch callbacks are executed when this occurs. 171 182 172 - echo -n "TEST: module_coming and module_going notifiers ... " 173 - dmesg -C 183 + start_test "module_coming and module_going notifiers" 174 184 175 185 load_lp $MOD_LIVEPATCH 176 186 load_mod $MOD_TARGET ··· 205 217 % rmmod $MOD_LIVEPATCH" 206 218 207 219 208 - # TEST: target module not present 209 - # 210 220 # A simple test of loading a livepatch without one of its patch target 211 221 # klp_objects ever loaded ($MOD_TARGET). 212 222 # ··· 213 227 # - As expected, only pre/post-(un)patch handlers are executed for 214 228 # vmlinux. 215 229 216 - echo -n "TEST: target module not present ... " 217 - dmesg -C 230 + start_test "target module not present" 218 231 219 232 load_lp $MOD_LIVEPATCH 220 233 disable_lp $MOD_LIVEPATCH ··· 237 252 % rmmod $MOD_LIVEPATCH" 238 253 239 254 240 - # TEST: pre-patch callback -ENODEV 241 - # 242 255 # Test a scenario where a vmlinux pre-patch callback returns a non-zero 243 256 # status (ie, failure). 244 257 # ··· 248 265 # The result is that the insmod command refuses to load the livepatch 249 266 # module. 250 267 251 - echo -n "TEST: pre-patch callback -ENODEV ... " 252 - dmesg -C 268 + start_test "pre-patch callback -ENODEV" 253 269 254 270 load_mod $MOD_TARGET 255 271 load_failing_mod $MOD_LIVEPATCH pre_patch_ret=-19 ··· 270 288 $MOD_TARGET: ${MOD_TARGET}_exit" 271 289 272 290 273 - # TEST: module_coming + pre-patch callback -ENODEV 274 - # 275 291 # Similar to the previous test, setup a livepatch such that its vmlinux 276 292 # pre-patch callback returns success. However, when a targeted kernel 277 293 # module is later loaded, have the livepatch return a failing status ··· 287 307 # 288 308 # - Pre/post-unpatch callbacks are run for the vmlinux klp_object. 289 309 290 - echo -n "TEST: module_coming + pre-patch callback -ENODEV ... " 291 - dmesg -C 310 + start_test "module_coming + pre-patch callback -ENODEV" 292 311 293 312 load_lp $MOD_LIVEPATCH 294 313 set_pre_patch_ret $MOD_LIVEPATCH -19 ··· 320 341 % rmmod $MOD_LIVEPATCH" 321 342 322 343 323 - # TEST: multiple target modules 324 - # 325 344 # Test loading multiple targeted kernel modules. This test-case is 326 345 # mainly for comparing with the next test-case. 327 346 # ··· 330 353 # module. Post-patch callbacks are executed and the transition 331 354 # completes quickly. 332 355 333 - echo -n "TEST: multiple target modules ... " 334 - dmesg -C 356 + start_test "multiple target modules" 335 357 336 358 load_mod $MOD_TARGET_BUSY block_transition=N 337 359 load_lp $MOD_LIVEPATCH ··· 378 402 $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" 379 403 380 404 381 - # TEST: busy target module 382 - # 383 405 # A similar test as the previous one, but force the "busy" kernel module 384 406 # to block the livepatch transition. 385 407 # ··· 405 431 # klp_object's post-patch callbacks executed, the remaining 406 432 # klp_object's pre-unpatch callbacks are skipped. 407 433 408 - echo -n "TEST: busy target module ... " 409 - dmesg -C 434 + start_test "busy target module" 410 435 411 436 load_mod $MOD_TARGET_BUSY block_transition=Y 412 437 load_lp_nowait $MOD_LIVEPATCH ··· 451 478 $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" 452 479 453 480 454 - # TEST: multiple livepatches 455 - # 456 481 # Test loading multiple livepatches. This test-case is mainly for comparing 457 482 # with the next test-case. 458 483 # ··· 458 487 # execute as each patch progresses through its (un)patching 459 488 # transition. 460 489 461 - echo -n "TEST: multiple livepatches ... " 462 - dmesg -C 490 + start_test "multiple livepatches" 463 491 464 492 load_lp $MOD_LIVEPATCH 465 493 load_lp $MOD_LIVEPATCH2 ··· 501 531 % rmmod $MOD_LIVEPATCH" 502 532 503 533 504 - # TEST: atomic replace 505 - # 506 534 # Load multiple livepatches, but the second as an 'atomic-replace' 507 535 # patch. When the latter loads, the original livepatch should be 508 536 # disabled and *none* of its pre/post-unpatch callbacks executed. On ··· 515 547 # - Once the atomic replace module is loaded, only its pre and post 516 548 # unpatch callbacks are executed. 517 549 518 - echo -n "TEST: atomic replace ... " 519 - dmesg -C 550 + start_test "atomic replace" 520 551 521 552 load_lp $MOD_LIVEPATCH 522 553 load_lp $MOD_LIVEPATCH2 replace=1
+1 -3
tools/testing/selftests/livepatch/test-ftrace.sh
··· 9 9 setup_config 10 10 11 11 12 - # TEST: livepatch interaction with ftrace_enabled sysctl 13 12 # - turn ftrace_enabled OFF and verify livepatches can't load 14 13 # - turn ftrace_enabled ON and verify livepatch can load 15 14 # - verify that ftrace_enabled can't be turned OFF while a livepatch is loaded 16 15 17 - echo -n "TEST: livepatch interaction with ftrace_enabled sysctl ... " 18 - dmesg -C 16 + start_test "livepatch interaction with ftrace_enabled sysctl" 19 17 20 18 set_ftrace_enabled 0 21 19 load_failing_mod $MOD_LIVEPATCH
+3 -9
tools/testing/selftests/livepatch/test-livepatch.sh
··· 10 10 setup_config 11 11 12 12 13 - # TEST: basic function patching 14 13 # - load a livepatch that modifies the output from /proc/cmdline and 15 14 # verify correct behavior 16 15 # - unload the livepatch and make sure the patch was removed 17 16 18 - echo -n "TEST: basic function patching ... " 19 - dmesg -C 17 + start_test "basic function patching" 20 18 21 19 load_lp $MOD_LIVEPATCH 22 20 ··· 45 47 % rmmod $MOD_LIVEPATCH" 46 48 47 49 48 - # TEST: multiple livepatches 49 50 # - load a livepatch that modifies the output from /proc/cmdline and 50 51 # verify correct behavior 51 52 # - load another livepatch and verify that both livepatches are active 52 53 # - unload the second livepatch and verify that the first is still active 53 54 # - unload the first livepatch and verify none are active 54 55 55 - echo -n "TEST: multiple livepatches ... " 56 - dmesg -C 56 + start_test "multiple livepatches" 57 57 58 58 load_lp $MOD_LIVEPATCH 59 59 ··· 105 109 % rmmod $MOD_LIVEPATCH" 106 110 107 111 108 - # TEST: atomic replace livepatch 109 112 # - load a livepatch that modifies the output from /proc/cmdline and 110 113 # verify correct behavior 111 114 # - load an atomic replace livepatch and verify that only the second is active ··· 112 117 # is still active 113 118 # - remove the atomic replace livepatch and verify that none are active 114 119 115 - echo -n "TEST: atomic replace livepatch ... " 116 - dmesg -C 120 + start_test "atomic replace livepatch" 117 121 118 122 load_lp $MOD_LIVEPATCH 119 123
+1 -3
tools/testing/selftests/livepatch/test-shadow-vars.sh
··· 9 9 setup_config 10 10 11 11 12 - # TEST: basic shadow variable API 13 12 # - load a module that exercises the shadow variable API 14 13 15 - echo -n "TEST: basic shadow variable API ... " 16 - dmesg -C 14 + start_test "basic shadow variable API" 17 15 18 16 load_mod $MOD_TEST 19 17 unload_mod $MOD_TEST
+9 -12
tools/testing/selftests/livepatch/test-state.sh
··· 10 10 11 11 setup_config 12 12 13 - # TEST: Loading and removing a module that modifies the system state 14 13 15 - echo -n "TEST: system state modification ... " 16 - dmesg -C 14 + # Load and remove a module that modifies the system state 15 + 16 + start_test "system state modification" 17 17 18 18 load_lp $MOD_LIVEPATCH 19 19 disable_lp $MOD_LIVEPATCH ··· 41 41 % rmmod $MOD_LIVEPATCH" 42 42 43 43 44 - # TEST: Take over system state change by a cumulative patch 44 + # Take over system state change by a cumulative patch 45 45 46 - echo -n "TEST: taking over system state modification ... " 47 - dmesg -C 46 + start_test "taking over system state modification" 48 47 49 48 load_lp $MOD_LIVEPATCH 50 49 load_lp $MOD_LIVEPATCH2 ··· 84 85 % rmmod $MOD_LIVEPATCH2" 85 86 86 87 87 - # TEST: Take over system state change by a cumulative patch 88 + # Take over system state change by a cumulative patch 88 89 89 - echo -n "TEST: compatible cumulative livepatches ... " 90 - dmesg -C 90 + start_test "compatible cumulative livepatches" 91 91 92 92 load_lp $MOD_LIVEPATCH2 93 93 load_lp $MOD_LIVEPATCH3 ··· 140 142 % rmmod $MOD_LIVEPATCH3" 141 143 142 144 143 - # TEST: Failure caused by incompatible cumulative livepatches 145 + # Failure caused by incompatible cumulative livepatches 144 146 145 - echo -n "TEST: incompatible cumulative livepatches ... " 146 - dmesg -C 147 + start_test "incompatible cumulative livepatches" 147 148 148 149 load_lp $MOD_LIVEPATCH2 149 150 load_failing_mod $MOD_LIVEPATCH