From: Dylan Hatch <dylanbhatch@google.com>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>,
Joe Lawrence <joe.lawrence@redhat.com>,
Song Liu <song@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Shuah Khan <shuah@kernel.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Thomas Gleixner <tglx@kernel.org>,
live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org,
Dylan Hatch <dylanbhatch@google.com>
Subject: [PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test
Date: Tue, 17 Nov 2026 04:52:54 +0000 [thread overview]
Message-ID: <20261117045254.1048298-3-dylanbhatch@google.com> (raw)
In-Reply-To: <20261117045254.1048298-1-dylanbhatch@google.com>
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 <dylanbhatch@google.com>
---
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
prev parent reply other threads:[~2026-11-17 4:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 4:52 [PATCH 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks Dylan Hatch
2026-11-17 4:52 ` [PATCH 1/2] " Dylan Hatch
2026-11-17 4:52 ` Dylan Hatch [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117045254.1048298-3-dylanbhatch@google.com \
--to=dylanbhatch@google.com \
--cc=bigeasy@linutronix.de \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tglx@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®