Skip to content

Commit 547840b

Browse files
joe-lawrencepmladek
authored andcommitted
selftests/livepatch: simplify test-klp-callbacks busy target tests
The test-klp-callbacks script includes a few tests which rely on kernel task timings that may not always execute as expected under system load. These may generate out of sequence kernel log messages that result in test failure. Instead of using sleep timing windows to orchestrate these tests, add a block_transition module parameter to communicate the test purpose and utilize flush_queue() to serialize the test module's task output. Signed-off-by: Joe Lawrence <[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 9fb4c52 commit 547840b

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

lib/livepatch/test_klp_callbacks_busy.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,53 @@
55

66
#include <linux/module.h>
77
#include <linux/kernel.h>
8+
#include <linux/sched.h>
89
#include <linux/workqueue.h>
910
#include <linux/delay.h>
1011

11-
static int sleep_secs;
12-
module_param(sleep_secs, int, 0644);
13-
MODULE_PARM_DESC(sleep_secs, "sleep_secs (default=0)");
12+
/* load/run-time control from sysfs writer */
13+
static bool block_transition;
14+
module_param(block_transition, bool, 0644);
15+
MODULE_PARM_DESC(block_transition, "block_transition (default=false)");
1416

1517
static void busymod_work_func(struct work_struct *work);
16-
static DECLARE_DELAYED_WORK(work, busymod_work_func);
18+
static DECLARE_WORK(work, busymod_work_func);
1719

1820
static void busymod_work_func(struct work_struct *work)
1921
{
20-
pr_info("%s, sleeping %d seconds ...\n", __func__, sleep_secs);
21-
msleep(sleep_secs * 1000);
22+
pr_info("%s enter\n", __func__);
23+
24+
while (READ_ONCE(block_transition)) {
25+
/*
26+
* Busy-wait until the sysfs writer has acknowledged a
27+
* blocked transition and clears the flag.
28+
*/
29+
msleep(20);
30+
}
31+
2232
pr_info("%s exit\n", __func__);
2333
}
2434

2535
static int test_klp_callbacks_busy_init(void)
2636
{
2737
pr_info("%s\n", __func__);
28-
schedule_delayed_work(&work,
29-
msecs_to_jiffies(1000 * 0));
38+
schedule_work(&work);
39+
40+
if (!block_transition) {
41+
/*
42+
* Serialize output: print all messages from the work
43+
* function before returning from init().
44+
*/
45+
flush_work(&work);
46+
}
47+
3048
return 0;
3149
}
3250

3351
static void test_klp_callbacks_busy_exit(void)
3452
{
35-
cancel_delayed_work_sync(&work);
53+
WRITE_ONCE(block_transition, false);
54+
flush_work(&work);
3655
pr_info("%s\n", __func__);
3756
}
3857

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,17 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
356356
echo -n "TEST: multiple target modules ... "
357357
dmesg -C
358358

359-
load_mod $MOD_TARGET_BUSY sleep_secs=0
360-
# give $MOD_TARGET_BUSY::busymod_work_func() a chance to run
361-
sleep 5
359+
load_mod $MOD_TARGET_BUSY block_transition=N
362360
load_lp $MOD_LIVEPATCH
363361
load_mod $MOD_TARGET
364362
unload_mod $MOD_TARGET
365363
disable_lp $MOD_LIVEPATCH
366364
unload_lp $MOD_LIVEPATCH
367365
unload_mod $MOD_TARGET_BUSY
368366

369-
check_result "% modprobe $MOD_TARGET_BUSY sleep_secs=0
367+
check_result "% modprobe $MOD_TARGET_BUSY block_transition=N
370368
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_init
371-
$MOD_TARGET_BUSY: busymod_work_func, sleeping 0 seconds ...
369+
$MOD_TARGET_BUSY: busymod_work_func enter
372370
$MOD_TARGET_BUSY: busymod_work_func exit
373371
% modprobe $MOD_LIVEPATCH
374372
livepatch: enabling patch '$MOD_LIVEPATCH'
@@ -404,11 +402,10 @@ livepatch: '$MOD_LIVEPATCH': unpatching complete
404402
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
405403

406404

407-
408405
# TEST: busy target module
409406
#
410407
# A similar test as the previous one, but force the "busy" kernel module
411-
# to do longer work.
408+
# to block the livepatch transition.
412409
#
413410
# The livepatching core will refuse to patch a task that is currently
414411
# executing a to-be-patched function -- the consistency model stalls the
@@ -417,8 +414,7 @@ $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
417414
# function for a long time. Meanwhile, load and unload other target
418415
# kernel modules while the livepatch transition is in progress.
419416
#
420-
# - Load the "busy" kernel module, this time make it do 10 seconds worth
421-
# of work.
417+
# - Load the "busy" kernel module, this time make its work function loop
422418
#
423419
# - Meanwhile, the livepatch is loaded. Notice that the patch
424420
# transition does not complete as the targeted "busy" module is
@@ -438,20 +434,23 @@ $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
438434
echo -n "TEST: busy target module ... "
439435
dmesg -C
440436

441-
load_mod $MOD_TARGET_BUSY sleep_secs=10
437+
load_mod $MOD_TARGET_BUSY block_transition=Y
442438
load_lp_nowait $MOD_LIVEPATCH
443-
# Don't wait for transition, load $MOD_TARGET while the transition
444-
# is still stalled in $MOD_TARGET_BUSY::busymod_work_func()
445-
sleep 5
439+
440+
# Wait until the livepatch reports in-transition state, i.e. that it's
441+
# stalled on $MOD_TARGET_BUSY::busymod_work_func()
442+
loop_until 'grep -q '^1$' /sys/kernel/livepatch/$MOD_LIVEPATCH/transition' ||
443+
die "failed to stall transition"
444+
446445
load_mod $MOD_TARGET
447446
unload_mod $MOD_TARGET
448447
disable_lp $MOD_LIVEPATCH
449448
unload_lp $MOD_LIVEPATCH
450449
unload_mod $MOD_TARGET_BUSY
451450

452-
check_result "% modprobe $MOD_TARGET_BUSY sleep_secs=10
451+
check_result "% modprobe $MOD_TARGET_BUSY block_transition=Y
453452
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_init
454-
$MOD_TARGET_BUSY: busymod_work_func, sleeping 10 seconds ...
453+
$MOD_TARGET_BUSY: busymod_work_func enter
455454
% modprobe $MOD_LIVEPATCH
456455
livepatch: enabling patch '$MOD_LIVEPATCH'
457456
livepatch: '$MOD_LIVEPATCH': initializing patching transition

0 commit comments

Comments
 (0)