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 v2 2/2] selftests/livepatch: Add cond_resched() transition tests
Date: Wed, 25 Nov 2026 04:19:00 +0000 [thread overview]
Message-ID: <20261125041900.3243569-3-dylanbhatch@google.com> (raw)
In-Reply-To: <20261125041900.3243569-1-dylanbhatch@google.com>
Add a livepatch kselftest (test-cond-resched.sh) and helper module
(test_klp_cond_resched) to verify that livepatch transitions complete
when sole-runnable CPU-bound tasks spin in kernel cpu_relax() +
cond_resched_tasks_rcu_qs() loops and are kicked via klp_send_signals():
1. './test-cond-resched.sh kthread' pins a SCHED_FIFO kthread spinning
in cond_resched_tasks_rcu_qs() on an isolated target CPU (while
enabling the RV sched:opid monitor via tracefs when available) and
verifies that klp_send_signals() reschedules and transitions it.
2. './test-cond-resched.sh usertask' spins a normal userspace task
(!(PF_KTHREAD | PF_USER_WORKER)) inside a kernel cond_resched() loop
via /proc/test_klp_cond_resched without checking signal_pending(),
verifying that klp_send_signals() forces it through __schedule() as
well.
Note that because _cond_resched() is a no-op under CONFIG_PREEMPTION=y,
a sole runnable spinning task enters __schedule(SM_PREEMPT) via
preempt_schedule_irq() with an in-kernel interrupt pt_regs frame on the
stack. Passing these tests 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), these tests
expose 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 | 204 ++++++++++++++++++
.../selftests/livepatch/test_modules/Makefile | 1 +
.../test_modules/test_klp_cond_resched.c | 141 ++++++++++++
4 files changed, 348 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..800a783df94e0
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test-cond-resched.sh
@@ -0,0 +1,204 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. $(dirname $0)/functions.sh
+
+MOD_LIVEPATCH=test_klp_livepatch
+MOD_RESCHED=test_klp_cond_resched
+SUBTEST="${1:-all}"
+
+case "$SUBTEST" in
+ all|kthread|usertask) ;;
+ *)
+ echo "Usage: $0 [all|kthread|usertask]" >&2
+ exit 1
+ ;;
+esac
+
+setup_config
+
+if [[ $(nproc) -lt 2 ]]; then
+ skip "test-cond-resched requires at least 2 online CPUs"
+fi
+
+# Pin the test runner (and a userspace busy-loop so global timers such as
+# klp_transition_work stay on CPU 0) to CPU 0, and disable RT bandwidth
+# throttling and the DL fair_server on TARGET_CPU so our SCHED_FIFO spinner
+# remains the sole runnable task until klp_send_signals() fires at
+# SIGNALS_TIMEOUT (15s).
+TARGET_CPU=$(($(nproc) - 1))
+FAIR_RUNTIME_FILE="$SYSFS_DEBUG_DIR/sched/fair_server/cpu$TARGET_CPU/runtime"
+if [[ ! -w "$FAIR_RUNTIME_FILE" ]]; then
+ skip "fair_server debugfs control ($FAIR_RUNTIME_FILE) not available"
+fi
+
+taskset -cp 0 $$ >/dev/null
+
+ORIG_RT_RUNTIME=$(sysctl -n kernel.sched_rt_runtime_us)
+ORIG_FAIR_RUNTIME=$(cat "$FAIR_RUNTIME_FILE")
+RV_DIR="$SYSFS_TRACING_DIR/rv"
+ENABLED_OPID=0
+USER_SPIN_PID=""
+BG_BUSY_PID=""
+
+function cleanup_cond_resched() {
+ if [[ -e /proc/test_klp_cond_resched ]]; then
+ echo 1 > /proc/test_klp_cond_resched 2>/dev/null || true
+ fi
+ if [[ -n "$USER_SPIN_PID" ]]; then
+ wait "$USER_SPIN_PID" 2>/dev/null || true
+ fi
+ if [[ -n "$BG_BUSY_PID" ]]; then
+ kill "$BG_BUSY_PID" 2>/dev/null || true
+ wait "$BG_BUSY_PID" 2>/dev/null || true
+ fi
+ rmmod $MOD_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
+ if [[ "$ENABLED_OPID" -eq 1 ]]; then
+ echo "!sched:opid" > "$RV_DIR/enabled_monitors" 2>/dev/null || true
+ echo nop > "$RV_DIR/monitors/sched/opid/reactors" 2>/dev/null || true
+ fi
+ echo "$ORIG_FAIR_RUNTIME" > "$FAIR_RUNTIME_FILE" 2>/dev/null || true
+ sysctl -q kernel.sched_rt_runtime_us="$ORIG_RT_RUNTIME" 2>/dev/null || true
+ cleanup
+}
+trap cleanup_cond_resched EXIT INT TERM HUP
+
+sysctl -q kernel.sched_rt_runtime_us=-1
+echo 0 > "$FAIR_RUNTIME_FILE"
+
+taskset -c 0 bash -c 'while :; do :; done' &
+BG_BUSY_PID=$!
+
+if [[ -f "$RV_DIR/available_monitors" ]] &&
+ grep -q '^sched:opid$' "$RV_DIR/available_monitors"; then
+ echo printk > "$RV_DIR/monitors/sched/opid/reactors" 2>/dev/null || true
+ if ! grep -q '^sched:opid$' "$RV_DIR/enabled_monitors"; then
+ echo sched:opid > "$RV_DIR/enabled_monitors" && ENABLED_OPID=1
+ fi
+fi
+
+function check_rv_opid() {
+ local opid_err
+
+ opid_err=$(dmesg -r | awk -v last="$LAST_DMESG" 'p { print } $0 == last { p = 1 }' |
+ grep 'rv: monitor opid does not allow' || true)
+ if [[ -n "$opid_err" ]]; then
+ echo -e "\n$opid_err" >&2
+ return 1
+ fi
+ return 0
+}
+
+function user_spin_started() {
+ dmesg -r | awk -v last="$LAST_DMESG" 'p { print } $0 == last { p = 1 }' |
+ grep -q "$MOD_RESCHED: user_spin_read: spinning"
+}
+
+# Test 1: Verify a sole-runnable CPU-bound kthread spinning in cond_resched()
+# transitions cleanly via klp_send_signals() -> __schedule().
+if [[ "$SUBTEST" == "all" || "$SUBTEST" == "kthread" ]]; then
+ start_test "CPU-bound kthread spinning in cond_resched() transition"
+
+ load_mod $MOD_RESCHED target_cpu=$TARGET_CPU spawn_kthread=1
+ 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_RESCHED
+
+ OPID_RC=0
+ check_rv_opid || OPID_RC=1
+
+ check_result "% insmod test_modules/$MOD_RESCHED.ko target_cpu=$TARGET_CPU spawn_kthread=1
+$MOD_RESCHED: cond_resched_kthread_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: signaling remaining tasks
+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_RESCHED
+$MOD_RESCHED: stopped"
+
+ if [[ "$OPID_RC" -ne 0 ]]; then
+ die "RV sched:opid violation detected during klp_send_signals()"
+ fi
+fi
+
+# Test 2: Verify a normal userspace task (!(PF_KTHREAD | PF_USER_WORKER))
+# spinning inside a kernel cond_resched() loop (without checking
+# signal_pending()) transitions cleanly via klp_send_signals() -> __schedule().
+if [[ "$SUBTEST" == "all" || "$SUBTEST" == "usertask" ]]; then
+ start_test "CPU-bound user task spinning in kernel cond_resched() transition"
+
+ load_mod $MOD_RESCHED target_cpu=$TARGET_CPU
+
+ cat /proc/test_klp_cond_resched >/dev/null &
+ USER_SPIN_PID=$!
+ loop_until 'user_spin_started' ||
+ die "user task failed to enter kernel spin"
+
+ load_lp $MOD_LIVEPATCH
+
+ wait "$USER_SPIN_PID"
+ USER_SPIN_PID=""
+
+ 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
+ unload_mod $MOD_RESCHED
+
+ OPID_RC=0
+ check_rv_opid || OPID_RC=1
+
+ check_result "% insmod test_modules/$MOD_RESCHED.ko target_cpu=$TARGET_CPU
+$MOD_RESCHED: user_spin_read: 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: signaling remaining tasks
+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_RESCHED
+$MOD_RESCHED: stopped"
+
+ if [[ "$OPID_RC" -ne 0 ]]; then
+ die "RV sched:opid violation detected during klp_send_signals()"
+ fi
+fi
+
+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..19ff9dffd96bc
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * test_klp_cond_resched.c - Helper module for testing livepatch transitions
+ * while CPU-bound kthreads or user tasks spin in kernel cond_resched() loops.
+ */
+
+#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/proc_fs.h>
+#include <linux/processor.h>
+#include <linux/rcupdate.h>
+#include <linux/sched.h>
+#include <linux/smp.h>
+
+static int target_cpu = -1;
+module_param(target_cpu, int, 0444);
+MODULE_PARM_DESC(target_cpu, "CPU to pin spinner task to");
+
+static bool spawn_kthread;
+module_param(spawn_kthread, bool, 0444);
+MODULE_PARM_DESC(spawn_kthread, "Spawn a SCHED_FIFO kthread spinner on load");
+
+static struct task_struct *cond_resched_tsk;
+static struct proc_dir_entry *proc_entry;
+static DECLARE_COMPLETION(spinner_started);
+static bool stop_spin;
+
+static void spin_until_patched(void)
+{
+ bool saw_pending = false;
+
+ while (!READ_ONCE(stop_spin)) {
+ if ((current->flags & PF_KTHREAD) && kthread_should_stop())
+ break;
+ if (test_thread_flag(TIF_PATCH_PENDING))
+ saw_pending = true;
+ else if (saw_pending)
+ break;
+ cpu_relax();
+ cond_resched_tasks_rcu_qs();
+ }
+}
+
+static int cond_resched_kthread_fn(void *data)
+{
+ sched_set_fifo_low(current);
+ pr_info("%s: spinning in cond_resched()\n", __func__);
+ complete(&spinner_started);
+
+ spin_until_patched();
+
+ sched_set_normal(current, 0);
+ while (!kthread_should_stop())
+ schedule_timeout_interruptible(HZ);
+
+ return 0;
+}
+
+static ssize_t user_spin_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ int ret;
+
+ if (*ppos > 0)
+ return 0;
+
+ ret = set_cpus_allowed_ptr(current, cpumask_of(target_cpu));
+ if (ret)
+ return ret;
+
+ sched_set_fifo_low(current);
+ WRITE_ONCE(stop_spin, false);
+ pr_info("%s: spinning in cond_resched()\n", __func__);
+
+ spin_until_patched();
+
+ sched_set_normal(current, 0);
+ *ppos = 1;
+ return 0;
+}
+
+static ssize_t user_spin_write(struct file *file, const char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ WRITE_ONCE(stop_spin, true);
+ return count;
+}
+
+static const struct proc_ops user_spin_ops = {
+ .proc_read = user_spin_read,
+ .proc_write = user_spin_write,
+};
+
+static int __init test_klp_cond_resched_init(void)
+{
+ if (target_cpu < 0)
+ target_cpu = cpumask_last(cpu_online_mask);
+ if ((unsigned int)target_cpu >= nr_cpu_ids || !cpu_online(target_cpu))
+ return -EINVAL;
+
+ proc_entry = proc_create("test_klp_cond_resched", 0600, NULL,
+ &user_spin_ops);
+ if (!proc_entry)
+ return -ENOMEM;
+
+ if (spawn_kthread) {
+ cond_resched_tsk = kthread_create_on_cpu(cond_resched_kthread_fn,
+ NULL, target_cpu,
+ "klp_cond_resched/%u");
+ if (IS_ERR(cond_resched_tsk)) {
+ int ret = PTR_ERR(cond_resched_tsk);
+
+ cond_resched_tsk = NULL;
+ proc_remove(proc_entry);
+ return ret;
+ }
+ wake_up_process(cond_resched_tsk);
+ wait_for_completion(&spinner_started);
+ }
+
+ return 0;
+}
+
+static void __exit test_klp_cond_resched_exit(void)
+{
+ WRITE_ONCE(stop_spin, true);
+ if (cond_resched_tsk)
+ kthread_stop(cond_resched_tsk);
+ proc_remove(proc_entry);
+ pr_info("stopped\n");
+}
+
+module_init(test_klp_cond_resched_init);
+module_exit(test_klp_cond_resched_exit);
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Livepatch test: CPU-bound cond_resched() transition");
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-25 4:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 4:18 [PATCH v2 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks Dylan Hatch
2026-11-25 4:18 ` [PATCH v2 1/2] " Dylan Hatch
2026-11-25 4:46 ` sashiko-bot
2026-11-25 4:19 ` Dylan Hatch [this message]
2026-11-25 9:50 ` [PATCH v2 0/2] " Peter Zijlstra
2026-11-26 14:29 ` Peter Zijlstra
2026-11-26 14:41 ` Peter Zijlstra
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=20261125041900.3243569-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.