[PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test

From
Dylan Hatch <hidden>
Date
2026-11-17 04:53:07
Also in
linux-kselftest, lkml
Reply to
Dylan Hatch, 2026-11-17
Add a livepatch kselftest (test-cond-resched.sh) and helper module
(test_klp_cond_resched) to verify that a livepatch transition completes
while CPU-bound kthreads spin in a cpu_relax() +
cond_resched_tasks_rcu_qs() loop on other online CPUs.

After commit 676e8cf70cb0 ("sched,livepatch: Untangle cond_resched() and
live-patching"), klp_sched_try_switch() was moved from cond_resched()
into __schedule() and gated on TASK_FREEZABLE. A sole runnable kthread
spinning in a cond_resched() loop never gets TIF_NEED_RESCHED set and
does not have TASK_FREEZABLE set, causing the livepatch transition to
stall.

Note that because _cond_resched() is a no-op under CONFIG_PREEMPTION=y,
a sole runnable spinning kthread enters __schedule(SM_PREEMPT) via
preempt_schedule_irq() with an in-kernel interrupt pt_regs frame on the
stack. Passing this test without another runnable task on the CPU
therefore requires arch_stack_walk_reliable() to support unwinding
across in-kernel exception frames (such as x86_64 with
CONFIG_UNWINDER_ORC=y); on architectures or configs where
arch_stack_walk_reliable() currently rejects in-kernel exception frames
(arm64, powerpc, s390, or x86 with CONFIG_FRAME_POINTER=y), this test
exposes the lack of exception-boundary reliable unwinding.

Signed-off-by: Dylan Hatch <redacted>
---
 tools/testing/selftests/livepatch/Makefile    |  3 +-
 .../selftests/livepatch/test-cond-resched.sh  | 73 ++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |  1 +
 .../test_modules/test_klp_cond_resched.c      | 94 +++++++++++++++++++
 4 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/livepatch/test-cond-resched.sh
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c
diff --git a/tools/testing/selftests/livepatch/Makefile b/tools/testing/selftests/livepatch/Makefile
index a080eb54a215d..14741d8da4209 100644
--- a/tools/testing/selftests/livepatch/Makefile
+++ b/tools/testing/selftests/livepatch/Makefile
@@ -11,7 +11,8 @@ TEST_PROGS := \
 	test-ftrace.sh \
 	test-sysfs.sh \
 	test-syscall.sh \
-	test-kprobe.sh
+	test-kprobe.sh \
+	test-cond-resched.sh
 
 TEST_FILES := settings
 
diff --git a/tools/testing/selftests/livepatch/test-cond-resched.sh b/tools/testing/selftests/livepatch/test-cond-resched.sh
new file mode 100755
index 0000000000000..4a7c013c863db
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test-cond-resched.sh
@@ -0,0 +1,73 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. $(dirname $0)/functions.sh
+
+MOD_LIVEPATCH=test_klp_livepatch
+MOD_COND_RESCHED=test_klp_cond_resched
+
+setup_config
+
+if [[ $(nproc) -lt 3 ]]; then
+	skip "test-cond-resched requires at least 3 online CPUs"
+fi
+
+function cleanup_cond_resched() {
+	# Stop the spinning kthread first so any stalled livepatch transition
+	# can complete before unloading the livepatch module.
+	rmmod $MOD_COND_RESCHED 2>/dev/null || true
+	if [[ -e "$SYSFS_KLP_DIR/$MOD_LIVEPATCH" ]]; then
+		echo 0 > "$SYSFS_KLP_DIR/$MOD_LIVEPATCH/enabled" 2>/dev/null || true
+		loop_until '[[ ! -e "$SYSFS_KLP_DIR/$MOD_LIVEPATCH" ]]' || true
+	fi
+	rmmod $MOD_LIVEPATCH 2>/dev/null || true
+	cleanup
+}
+trap cleanup_cond_resched EXIT INT TERM HUP
+
+# - start CPU-bound kthreads spinning in a cpu_relax() +
+#   cond_resched_tasks_rcu_qs() loop on other online CPUs
+# - load a livepatch and verify the patching transition completes
+# - disable and unload the livepatch and verify the unpatching transition
+#   completes
+# - unload the cond_resched kthread module
+
+start_test "kthread spinning in cond_resched() transition"
+
+load_mod $MOD_COND_RESCHED
+
+load_lp $MOD_LIVEPATCH
+
+if [[ "$(cat /proc/cmdline)" != "$MOD_LIVEPATCH: this has been live patched" ]] ; then
+	echo -e "FAIL\n\n"
+	die "livepatch kselftest(s) failed"
+fi
+
+disable_lp $MOD_LIVEPATCH
+unload_lp $MOD_LIVEPATCH
+
+if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
+	echo -e "FAIL\n\n"
+	die "livepatch kselftest(s) failed"
+fi
+
+unload_mod $MOD_COND_RESCHED
+
+check_result "% insmod test_modules/$MOD_COND_RESCHED.ko
+$MOD_COND_RESCHED: cond_resched_fn: spinning in cond_resched()
+% insmod test_modules/$MOD_LIVEPATCH.ko
+livepatch: enabling patch '$MOD_LIVEPATCH'
+livepatch: '$MOD_LIVEPATCH': initializing patching transition
+livepatch: '$MOD_LIVEPATCH': starting patching transition
+livepatch: '$MOD_LIVEPATCH': completing patching transition
+livepatch: '$MOD_LIVEPATCH': patching complete
+% echo 0 > $SYSFS_KLP_DIR/$MOD_LIVEPATCH/enabled
+livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
+livepatch: '$MOD_LIVEPATCH': starting unpatching transition
+livepatch: '$MOD_LIVEPATCH': completing unpatching transition
+livepatch: '$MOD_LIVEPATCH': unpatching complete
+% rmmod $MOD_LIVEPATCH
+% rmmod $MOD_COND_RESCHED
+$MOD_COND_RESCHED: cond_resched_fn: stopping"
+
+exit 0
diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile
index a13d398585dcb..1502ff9070b88 100644
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile
@@ -6,6 +6,7 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_callbacks_demo.o \
 	test_klp_callbacks_demo2.o \
 	test_klp_callbacks_mod.o \
+	test_klp_cond_resched.o \
 	test_klp_kprobe.o \
 	test_klp_livepatch.o \
 	test_klp_mod_patch.o \
diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c b/tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c
new file mode 100644
index 0000000000000..ae1d1e3aab106
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * test_klp_cond_resched.c - Support module for testing livepatch
+ * transitions while a CPU-bound kthread spins in cond_resched().
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/completion.h>
+#include <linux/cpumask.h>
+#include <linux/kernel.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/processor.h>
+#include <linux/rcupdate.h>
+#include <linux/sched.h>
+#include <linux/smp.h>
+
+#define NUM_COND_RESCHED_TASKS 2
+
+static struct task_struct *cond_resched_tsks[NUM_COND_RESCHED_TASKS];
+static atomic_t cond_resched_ready = ATOMIC_INIT(NUM_COND_RESCHED_TASKS);
+static DECLARE_COMPLETION(cond_resched_started);
+
+static int cond_resched_fn(void *data)
+{
+	if (atomic_dec_and_test(&cond_resched_ready)) {
+		pr_info("%s: spinning in cond_resched()\n", __func__);
+		complete(&cond_resched_started);
+	}
+	while (!kthread_should_stop()) {
+		cpu_relax();
+		cond_resched_tasks_rcu_qs();
+	}
+	return 0;
+}
+
+static void stop_cond_resched_tasks(void)
+{
+	int i;
+
+	for (i = 0; i < NUM_COND_RESCHED_TASKS; i++) {
+		if (cond_resched_tsks[i]) {
+			kthread_stop(cond_resched_tsks[i]);
+			cond_resched_tsks[i] = NULL;
+		}
+	}
+}
+
+static int test_klp_cond_resched_init(void)
+{
+	int cur_cpu, cpu, i = 0;
+	int target_cpus[NUM_COND_RESCHED_TASKS];
+
+	cur_cpu = get_cpu();
+	for_each_online_cpu(cpu) {
+		if (cpu != cur_cpu)
+			target_cpus[i++ % NUM_COND_RESCHED_TASKS] = cpu;
+	}
+	put_cpu();
+
+	if (i < NUM_COND_RESCHED_TASKS)
+		return -EINVAL;
+
+	for (i = 0; i < NUM_COND_RESCHED_TASKS; i++) {
+		struct task_struct *tsk;
+
+		tsk = kthread_create_on_cpu(cond_resched_fn, NULL,
+					    target_cpus[i],
+					    "klp_cond_resched/%u");
+		if (IS_ERR(tsk)) {
+			stop_cond_resched_tasks();
+			return PTR_ERR(tsk);
+		}
+		cond_resched_tsks[i] = tsk;
+	}
+
+	for (i = 0; i < NUM_COND_RESCHED_TASKS; i++)
+		wake_up_process(cond_resched_tsks[i]);
+
+	wait_for_completion(&cond_resched_started);
+	return 0;
+}
+
+static void test_klp_cond_resched_exit(void)
+{
+	stop_cond_resched_tasks();
+	pr_info("cond_resched_fn: stopping\n");
+}
+
+module_init(test_klp_cond_resched_init);
+module_exit(test_klp_cond_resched_exit);
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Livepatch test: cond_resched() kthread transition");
-- 
2.56.0.679.gc527c65306-goog
Thread (4 messages) 4 messages, 2 authors, last reply 2d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help