* [PATCH 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks
@ 2026-11-17 4:52 Dylan Hatch
2026-11-17 4:52 ` [PATCH 1/2] " Dylan Hatch
2026-11-17 4:52 ` [PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test Dylan Hatch
0 siblings, 2 replies; 4+ messages in thread
From: Dylan Hatch @ 2026-11-17 4:52 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 or
PF_USER_WORKER threads (such as vhost-<pid>) spin in a kernel loop with
cond_resched():
1. A sole runnable task on a CPU (cfs_rq->nr_queued == 1) never has
TIF_NEED_RESCHED set during sched_tick(), and wake_up_state(task,
TASK_INTERRUPTIBLE) in klp_send_signals() is a no-op for a
TASK_RUNNING task. Because _cond_resched() is a no-op under
CONFIG_PREEMPTION=y, the task only enters __schedule(SM_PREEMPT) on
return from interrupts via irqentry_exit_cond_resched() when
TIF_NEED_RESCHED is set.
2. 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.
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 calling resched_curr(rq) in sched_tick() when
klp_sched_need_resched() is true for the current task.
Patch 2 adds a livepatch kselftest (test-cond-resched.sh) that spawns
kthreads spinning in a cpu_relax() + cond_resched_tasks_rcu_qs() loop
and verifies that livepatch transitions complete without stalling.
Note on reliable stacktraces across exception boundaries:
Because _cond_resched() is a no-op under CONFIG_PREEMPTION=y, a sole
runnable spinning kthread 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].
This series is available on github: [3].
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
Dylan Hatch (2):
sched, livepatch: Fix transition stall for CPU-bound tasks
selftests/livepatch: Add cond_resched() kthread transition test
include/linux/livepatch_sched.h | 13 ++-
kernel/sched/core.c | 3 +-
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 +++++++++++++++++++
6 files changed, 183 insertions(+), 4 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.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 1/2] sched, livepatch: Fix transition stall for CPU-bound tasks 2026-11-17 4:52 [PATCH 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks Dylan Hatch @ 2026-11-17 4:52 ` Dylan Hatch 2026-11-21 21:55 ` Josh Poimboeuf 2026-11-17 4:52 ` [PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test Dylan Hatch 1 sibling, 1 reply; 4+ messages in thread From: Dylan Hatch @ 2026-11-17 4:52 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 a CPU-bound kthread or PF_USER_WORKER thread (such as vhost-<pid>) spins in a kernel loop with cond_resched() for two reasons: 1. A sole runnable task on a CPU (cfs_rq->nr_queued == 1) never has TIF_NEED_RESCHED set during sched_tick(), and wake_up_state(task, TASK_INTERRUPTIBLE) in klp_send_signals() is a no-op for a TASK_RUNNING kthread. Because _cond_resched() is a no-op under CONFIG_PREEMPTION=y, the task only enters __schedule(SM_PREEMPT) on return from interrupts via irqentry_exit_cond_resched() when TIF_NEED_RESCHED is set. 2. 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(). Fix both issues by: - Removing the TASK_FREEZABLE check from klp_sched_try_switch() and checking TIF_PATCH_PENDING inline via klp_sched_need_resched(). - Calling resched_curr(rq) in sched_tick() when klp_sched_need_resched() is true for the current task, ensuring CPU-bound tasks with a pending livepatch enter __schedule() at the next timer tick. 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") Signed-off-by: Dylan Hatch <dylanbhatch@google.com> --- include/linux/livepatch_sched.h | 13 +++++++++++-- kernel/sched/core.c | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/linux/livepatch_sched.h b/include/linux/livepatch_sched.h index 065c185f27638..748b6012e2e06 100644 --- a/include/linux/livepatch_sched.h +++ b/include/linux/livepatch_sched.h @@ -11,15 +11,24 @@ void __klp_sched_try_switch(void); DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key); +static __always_inline bool klp_sched_need_resched(struct task_struct *curr) +{ + return static_branch_unlikely(&klp_sched_try_switch_key) && + unlikely(test_tsk_thread_flag(curr, TIF_PATCH_PENDING)); +} + 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) + if (klp_sched_need_resched(curr)) __klp_sched_try_switch(); } #else /* !CONFIG_LIVEPATCH */ static inline void klp_sched_try_switch(struct task_struct *curr) {} +static inline bool klp_sched_need_resched(struct task_struct *curr) +{ + return false; +} #endif /* CONFIG_LIVEPATCH */ #endif /* _LINUX_LIVEPATCH_SCHED_H_ */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 04b62c16fa8ca..fcea9cea3edf1 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -5854,7 +5854,8 @@ void sched_tick(void) hw_pressure = arch_scale_hw_pressure(cpu_of(rq)); update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure); - if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) + if ((dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) || + klp_sched_need_resched(curr)) resched_curr(rq); donor->sched_class->task_tick(rq, donor, 0); -- 2.56.0.679.gc527c65306-goog ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] sched, livepatch: Fix transition stall for CPU-bound tasks 2026-11-17 4:52 ` [PATCH 1/2] " Dylan Hatch @ 2026-11-21 21:55 ` Josh Poimboeuf 0 siblings, 0 replies; 4+ messages in thread From: Josh Poimboeuf @ 2026-11-21 21:55 UTC (permalink / raw) To: Dylan Hatch Cc: Miroslav Benes, Petr Mladek, Joe Lawrence, Song Liu, Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot, Shuah Khan, Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak, Sebastian Andrzej Siewior, Thomas Gleixner, live-patching, linux-kselftest, linux-kernel On Tue, Nov 17, 2026 at 04:52:53AM +0000, Dylan Hatch wrote: > 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 a CPU-bound kthread or > PF_USER_WORKER thread (such as vhost-<pid>) spins in a kernel loop with > cond_resched() for two reasons: > > 1. A sole runnable task on a CPU (cfs_rq->nr_queued == 1) never has > TIF_NEED_RESCHED set during sched_tick(), and wake_up_state(task, > TASK_INTERRUPTIBLE) in klp_send_signals() is a no-op for a > TASK_RUNNING kthread. Because _cond_resched() is a no-op under > CONFIG_PREEMPTION=y, the task only enters __schedule(SM_PREEMPT) on > return from interrupts via irqentry_exit_cond_resched() when > TIF_NEED_RESCHED is set. > 2. 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(). > > Fix both issues by: > - Removing the TASK_FREEZABLE check from klp_sched_try_switch() and > checking TIF_PATCH_PENDING inline via klp_sched_need_resched(). > - Calling resched_curr(rq) in sched_tick() when klp_sched_need_resched() > is true for the current task, ensuring CPU-bound tasks with a pending > livepatch enter __schedule() at the next timer tick. > > 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") Oof, so Peter's patch broke all non-ORC for this case :-( Not sure what we can do about that other than get kernel sframe/eh_frame working for exception boundary unwinding, as you mention. > Signed-off-by: Dylan Hatch <dylanbhatch@google.com> > --- > include/linux/livepatch_sched.h | 13 +++++++++++-- > kernel/sched/core.c | 3 ++- > 2 files changed, 13 insertions(+), 3 deletions(-) > > diff --git a/include/linux/livepatch_sched.h b/include/linux/livepatch_sched.h > index 065c185f27638..748b6012e2e06 100644 > --- a/include/linux/livepatch_sched.h > +++ b/include/linux/livepatch_sched.h > @@ -11,15 +11,24 @@ void __klp_sched_try_switch(void); > > DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key); > > +static __always_inline bool klp_sched_need_resched(struct task_struct *curr) > +{ > + return static_branch_unlikely(&klp_sched_try_switch_key) && > + unlikely(test_tsk_thread_flag(curr, TIF_PATCH_PENDING)); > +} > + > 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) > + if (klp_sched_need_resched(curr)) > __klp_sched_try_switch(); > } > > #else /* !CONFIG_LIVEPATCH */ > static inline void klp_sched_try_switch(struct task_struct *curr) {} > +static inline bool klp_sched_need_resched(struct task_struct *curr) > +{ > + return false; > +} > #endif /* CONFIG_LIVEPATCH */ > > #endif /* _LINUX_LIVEPATCH_SCHED_H_ */ > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 04b62c16fa8ca..fcea9cea3edf1 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -5854,7 +5854,8 @@ void sched_tick(void) > hw_pressure = arch_scale_hw_pressure(cpu_of(rq)); > update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure); > > - if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) > + if ((dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) || > + klp_sched_need_resched(curr)) > resched_curr(rq); Hm, so for unpatched tasks this forces a full preemption and stack walk on every scheduler tick? Instead can we just kick the thread in the existing klp_send_signals() escalation path like below (untested)? diff --git a/include/linux/livepatch_sched.h b/include/linux/livepatch_sched.h index 065c185f27638..77161f5725dcc 100644 --- a/include/linux/livepatch_sched.h +++ b/include/linux/livepatch_sched.h @@ -13,8 +13,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) + if (static_branch_unlikely(&klp_sched_try_switch_key)) __klp_sched_try_switch(); } diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index e058d21d84b75..1ccbae2051c72 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -402,12 +402,22 @@ 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_state(task, TASK_INTERRUPTIBLE); + if (wake_up_state(task, TASK_INTERRUPTIBLE)) + continue; + + /* + * If it's running, force it through __schedule() so + * it can switch itself in klp_sched_try_switch(). + */ + if (task_curr(task)) { + set_tsk_need_resched(task); + kick_process(task); + } } else { /* * Send fake signal to all non-kthread tasks which are ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test 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 1 sibling, 0 replies; 4+ messages in thread From: Dylan Hatch @ 2026-11-17 4:52 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 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-21 21:55 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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-21 21:55 ` Josh Poimboeuf 2026-11-17 4:52 ` [PATCH 2/2] selftests/livepatch: Add cond_resched() kthread transition test Dylan Hatch
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®