sched, livepatch: Fix transition stall for CPU-bound tasks
Subsystems (3)
v2 cover sched, livepatch: Fix transition stall for CPU-bound tasks
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/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://github.com/dylanbhatch/linux/tree/stall-v2
---
Changes since v1:
https://lore.kernel.org/all/[email protected]/
- (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
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v2 1/2 sched, livepatch: Fix transition stall for CPU-bound tasks
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.
--- include/linux/livepatch_sched.h | 2 +- include/linux/sched.h | 1 + kernel/livepatch/transition.c | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-)
include/linux/livepatch_sched.h +1 -1
@@ -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 (); }
include/linux/sched.h +1 -0
@@ -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) ({ \
kernel/livepatch/transition.c +15 -4
@@ -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); }
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v2 2/2 selftests/livepatch: Add cond_resched() transition tests
In reply to Dylan HatchAdd 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.
--- 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
tools/testing/selftests/livepatch/Makefile +2 -1
@@ -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
tools/testing/selftests/livepatch/test-cond-resched.sh new file +204 -0
204 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/Makefile +1 -0
@@ -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 \
tools/testing/selftests/livepatch/test_modules/test_klp_cond_resched.c new file +141 -0
141 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] livepatch: klp_sched_try_switch() transitions task inside klp_ftrace_handler() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] livepatch: klp_sched_try_switch() transitions task inside klp_ftrace_handler()
--
commit 838d28a5585cbc1f5e4b8cf8405e19da19b1f60d
Author: Dylan Hatch <[email protected]>
sched, livepatch: Fix transition stall for CPU-bound tasks
This commit removes the TASK_FREEZABLE check from klp_sched_try_switch() and updates klp_send_signals() to wake PF_USER_WORKER threads and call resched_cpu() on running tasks with pending livepatch transitions.
include/linux/livepatch_sched.h DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key); · line 19
Quoted 12 lines
include/linux/livepatch_sched.h +1
-1@@ -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 (); }
[Severity: High]
Can removing the TASK_FREEZABLE check switch a task while it is preempted at the end of klp_ftrace_handler(), after the handler has already redirected ftrace_regs to the previous patch state's function?
During an unpatch transition (or a stacked/replace patch transition), a task with current->patch_state == KLP_TRANSITION_PATCHED calling a patched function can be preempted before ftrace_caller transfers control to the selected target:
ftrace_caller
klp_ftrace_handler()
ftrace_test_recursion_trylock() /* disables preemption */
ftrace_regs_set_instruction_pointer(fregs, func->new_func) ftrace_test_recursion_unlock()
preempt_enable_notrace()
preempt_schedule_notrace()
__schedule(SM_PREEMPT)
klp_sched_try_switch()
klp_check_stack() /* sees only func->old_func */
klp_update_patch_state() /* sets KLP_TRANSITION_UNPATCHED */
/* returns to func->new_func while task is KLP_TRANSITION_UNPATCHED */
ftrace_regs_set_instruction_pointer() only updates the saved IP inside ftrace_regs; the return address slot unwound by arch_stack_walk_reliable() still holds func->old_func + MCOUNT_INSN_SIZE until klp_ftrace_handler() returns to ftrace_caller. When klp_target_state is KLP_TRANSITION_UNPATCHED (or when patching over a stacked patch), klp_check_stack_func() checks the stack against func->new_func (or prev->new_func), so klp_check_stack() succeeds and transitions the task.
When __schedule() returns, ftrace_caller jumps into func->new_func even though the task's patch_state is now KLP_TRANSITION_UNPATCHED, breaking per-task consistency. If __schedule() context-switches the task out after klp_sched_try_switch() marks it unpatched, klp_complete_transition() and klp_synchronize_transition() can also finish unpatching and allow the livepatch module to be unloaded before the task is scheduled back in and jumps to the freed func->new_func address.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1