* [PATCH v2 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks
@ 2026-11-25 4:18 Dylan Hatch
2026-11-25 4:18 ` [PATCH v2 1/2] " Dylan Hatch
2026-11-25 4:19 ` [PATCH v2 2/2] selftests/livepatch: Add cond_resched() transition tests Dylan Hatch
0 siblings, 2 replies; 3+ messages in thread
From: Dylan Hatch @ 2026-11-25 4:18 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Peter Zijlstra, Juri Lelli,
Vincent Guittot, Shuah Khan
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Sebastian Andrzej Siewior,
Thomas Gleixner, live-patching, linux-kselftest, linux-kernel,
Dylan Hatch
Commit 676e8cf70cb0 ("sched,livepatch: Untangle cond_resched() and
live-patching") moved klp_sched_try_switch() from _cond_resched() into
__schedule() and gated it on READ_ONCE(curr->__state) & TASK_FREEZABLE.
This causes livepatch transitions to stall when CPU-bound kthreads,
PF_USER_WORKER threads (such as vhost-<pid>), or user tasks in long
kernel loops spin with cond_resched():
1. Even when __schedule() is entered via preemption or an unfreezable
sleep (such as PF_NOFREEZE kthreads or PF_USER_WORKER threads
sleeping in TASK_INTERRUPTIBLE), curr->__state does not have
TASK_FREEZABLE set, so klp_sched_try_switch() skips the task.
2. A sole runnable task spinning in the kernel never gets
TIF_NEED_RESCHED set during sched_tick(). In klp_send_signals(),
wake_up_state(task, TASK_INTERRUPTIBLE) is a no-op for a
TASK_RUNNING task, and set_notify_signal() sets TIF_NOTIFY_SIGNAL
without TIF_NEED_RESCHED, so the resulting reschedule IPI returns to
kernel mode without entering __schedule().
Patch 1 fixes both issues by dropping the TASK_FREEZABLE check from
klp_sched_try_switch() (relying on stack_trace_save_tsk_reliable() in
klp_check_stack()) and kicking still-running unmigrated tasks via
resched_cpu() in klp_send_signals() (as well as waking interruptibly
sleeping PF_USER_WORKER tasks).
Patch 2 adds a livepatch kselftest (test-cond-resched.sh) exercising the
klp_send_signals() reschedule path for both CPU-bound kthreads and user
tasks spinning in kernel mode.
Note on reliable stacktraces across exception boundaries:
Because _cond_resched() is a no-op under CONFIG_PREEMPTION=y, a sole
runnable spinning task enters __schedule(SM_PREEMPT) from
preempt_schedule_irq() with an in-kernel interrupt pt_regs frame on the
stack. Switching the task in klp_sched_try_switch() therefore requires
arch_stack_walk_reliable() to support unwinding across in-kernel
exception frames. This works on x86_64 with CONFIG_UNWINDER_ORC=y, where
test-cond-resched.sh passes with this series.
On architectures or configurations where arch_stack_walk_reliable()
currently rejects in-kernel exception frames (arm64, powerpc, s390, or
x86 with CONFIG_FRAME_POINTER=y), test-cond-resched.sh exposes this
limitation and will fail until exception-boundary unwinding is
supported. On arm64, the test passes when combined with the proposed
kernel SFrame unwinder series [1] or equivalent .eh_frame-derived
unwind tables [2].
These patches are available on github [3], with an additional commit
containing extra tracepoint validation.
Ref:
[1]: https://lore.kernel.org/all/20261117020648.323464-1-dylanbhatch@google.com/
[2]: https://lore.kernel.org/all/20261117043504.1021063-1-dylanbhatch@google.com/
[3]: https://github.com/dylanbhatch/linux/tree/stall-v2
---
Changes since v1:
https://lore.kernel.org/all/20261117045254.1048298-1-dylanbhatch@google.com/
- (Josh) Drop the sched_tick() hook and instead force still-running
tasks through __schedule() from klp_send_signals(), avoiding per-tick
preemption and reliable stack-walk overhead on unpatched tasks.
- Use resched_cpu() rather than set_tsk_need_resched() + kick_process()
when kicking remote tasks in klp_send_signals(), and apply it to all
still-running tasks (including user tasks spinning in kernel mode).
- Update test-cond-resched.sh to deterministically exercise the
klp_send_signals() reschedule path for both kthreads and user tasks
spinning in kernel mode.
- (Sashiko) Bounds-check target_cpu against nr_cpu_ids before calling
cpu_online() in test_klp_cond_resched.
Dylan Hatch (2):
sched, livepatch: Fix transition stall for CPU-bound tasks
selftests/livepatch: Add cond_resched() transition tests
include/linux/livepatch_sched.h | 2 +-
include/linux/sched.h | 1 +
kernel/livepatch/transition.c | 19 +-
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 ++++++++++++
7 files changed, 365 insertions(+), 6 deletions(-)
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
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] sched, livepatch: Fix transition stall for CPU-bound tasks
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 ` Dylan Hatch
2026-11-25 4:19 ` [PATCH v2 2/2] selftests/livepatch: Add cond_resched() transition tests Dylan Hatch
1 sibling, 0 replies; 3+ messages in thread
From: Dylan Hatch @ 2026-11-25 4:18 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Peter Zijlstra, Juri Lelli,
Vincent Guittot, Shuah Khan
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Sebastian Andrzej Siewior,
Thomas Gleixner, live-patching, linux-kselftest, linux-kernel,
Dylan Hatch
Commit 676e8cf70cb0 ("sched,livepatch: Untangle cond_resched() and
live-patching") moved klp_sched_try_switch() from cond_resched() into
__schedule() and gated it on READ_ONCE(curr->__state) & TASK_FREEZABLE.
This causes livepatch transitions to stall when CPU-bound kthreads,
PF_USER_WORKER threads (such as vhost-<pid>), or user tasks in long
kernel loops spin with cond_resched() for two reasons:
1. Even when __schedule() is entered via preemption or an unfreezable
sleep (such as PF_NOFREEZE kthreads or PF_USER_WORKER threads
sleeping in TASK_INTERRUPTIBLE), curr->__state does not have
TASK_FREEZABLE set, so klp_sched_try_switch() skips the task.
Livepatch safety in __schedule() is already guaranteed by
stack_trace_save_tsk_reliable() inside klp_check_stack().
2. A sole runnable task spinning in the kernel never gets
TIF_NEED_RESCHED set during sched_tick(). In klp_send_signals(),
wake_up_state(task, TASK_INTERRUPTIBLE) is a no-op for a
TASK_RUNNING task, and set_notify_signal() sets TIF_NOTIFY_SIGNAL
without TIF_NEED_RESCHED, so the resulting reschedule IPI returns to
kernel mode without entering __schedule().
Fix both issues by:
- Removing the TASK_FREEZABLE check from klp_sched_try_switch() and
checking TIF_PATCH_PENDING inline.
- Waking interruptibly sleeping PF_USER_WORKER tasks alongside
PF_KTHREAD tasks in klp_send_signals(), and calling resched_cpu() on
any still-running task's CPU so it is forced through __schedule() and
switches in klp_sched_try_switch().
Note that when a CPU-bound task is the sole runnable task on its CPU,
entering __schedule(SM_PREEMPT) from preempt_schedule_irq() leaves an
in-kernel interrupt pt_regs frame on the stack during
klp_sched_try_switch(). Completing the transition in that context
requires arch_stack_walk_reliable() to support unwinding across
in-kernel exception frames (as CONFIG_UNWINDER_ORC=y does on x86_64);
configs and architectures whose reliable unwinder rejects in-kernel
exception frames (x86 with CONFIG_FRAME_POINTER=y, arm64, powerpc, and
s390) still require another runnable task to context-switch the spinning
task out until exception-boundary reliable unwinding (e.g. via SFrame)
is available.
Fixes: 676e8cf70cb0 ("sched,livepatch: Untangle cond_resched() and live-patching")
Suggested-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Dylan Hatch <dylanbhatch@google.com>
---
include/linux/livepatch_sched.h | 2 +-
include/linux/sched.h | 1 +
kernel/livepatch/transition.c | 19 +++++++++++++++----
3 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/include/linux/livepatch_sched.h b/include/linux/livepatch_sched.h
index 065c185f27638..533b754f10da4 100644
--- a/include/linux/livepatch_sched.h
+++ b/include/linux/livepatch_sched.h
@@ -14,7 +14,7 @@ DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
static __always_inline void klp_sched_try_switch(struct task_struct *curr)
{
if (static_branch_unlikely(&klp_sched_try_switch_key) &&
- READ_ONCE(curr->__state) & TASK_FREEZABLE)
+ unlikely(test_tsk_thread_flag(curr, TIF_PATCH_PENDING)))
__klp_sched_try_switch();
}
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 1c93da75226e3..121f2b3ed69b0 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2035,6 +2035,7 @@ extern int wake_up_process(struct task_struct *tsk);
extern void wake_up_new_task(struct task_struct *tsk);
extern void kick_process(struct task_struct *tsk);
+extern void resched_cpu(int cpu);
extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
#define set_task_comm(tsk, from) ({ \
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 2351a19ac2a9e..f626b8a07323b 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -402,12 +402,13 @@ static void klp_send_signals(void)
* Meanwhile the task could migrate itself and the action
* would be meaningless. It is not serious though.
*/
- if (task->flags & PF_KTHREAD) {
+ if (task->flags & (PF_KTHREAD | PF_USER_WORKER)) {
/*
- * Wake up a kthread which sleeps interruptedly and
- * still has not been migrated.
+ * Wake up a kthread or user worker which sleeps
+ * interruptibly and still has not been migrated.
*/
- wake_up_state(task, TASK_INTERRUPTIBLE);
+ if (wake_up_state(task, TASK_INTERRUPTIBLE))
+ continue;
} else {
/*
* Send fake signal to all non-kthread tasks which are
@@ -415,6 +416,16 @@ static void klp_send_signals(void)
*/
set_notify_signal(task);
}
+
+ /*
+ * If the task is currently running (e.g. CPU-bound in the
+ * kernel), force its CPU through __schedule() so it can switch
+ * itself in klp_sched_try_switch().
+ */
+ scoped_guard(preempt) {
+ if (task_curr(task))
+ resched_cpu(task_cpu(task));
+ }
}
read_unlock(&tasklist_lock);
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] selftests/livepatch: Add cond_resched() transition tests
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:19 ` Dylan Hatch
1 sibling, 0 replies; 3+ messages in thread
From: Dylan Hatch @ 2026-11-25 4:19 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Peter Zijlstra, Juri Lelli,
Vincent Guittot, Shuah Khan
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Sebastian Andrzej Siewior,
Thomas Gleixner, live-patching, linux-kselftest, linux-kernel,
Dylan Hatch
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 4:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:19 ` [PATCH v2 2/2] selftests/livepatch: Add cond_resched() transition tests Dylan Hatch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox