Re: [PATCH v2 0/2] sched, livepatch: Fix transition stall for CPU-bound tasks

From: Dylan Hatch

Date: Wed Dec 02 2026 - 21:12:09 EST


Hi Peter,

On Thu, Nov 26, 2026 at 6:41 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Thu, Nov 26, 2026 at 03:29:19PM +0100, Peter Zijlstra wrote:
> > On Wed, Nov 25, 2026 at 04:18:58AM +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 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).
> >
> > So I didn't get a reply on my v1 comment, and I still have the same
> > quesiton; how isn't any of this a problem for the freezer?

Sorry about that, I was too hasty in preparing v2 and missed your comment.

> >
> > The intent was to make sure that a patch problem is a freezer problem
> > and we fix both together.
> >
> > Both cases above would also delay freezing, no?
> >
> > It seems I have overlooked try_to_freezer() /
> > kthread_freezable_should_stop(). Those should also do the patching
> > thing. And then its a matter of adding either of those to whatever loops
> > you think are lacking.

I think the freezer and livepatch needs/behavior don't align in a
couple of ways:

- PF_NOFREEZE kthreads are skipped by the freezer, but still must be
livepatched. With your suggested freezer-based patch, the selftest in
patch 2 still fails.

- A kthread holding a lock cannot be frozen (can't call
try_to_freeze()), but it can still be livepatched. Since the livepatch
consistency model enforces only that the patched functions aren't
being executed, this seems like an unnecessary restriction.

>
> Perhaps something like so?

I think this patch is useful as a complement to the existing patch 1.
Since it switches freezable kthreads without an IRQ pt_regs frame, it
may aleviate the unwind issue on non-ORC arches. Would it make sense
to incorporate this (in addition to the klp_send_signals() logic) in a
v3?

>
> ---
> diff --git a/include/linux/freezer.h b/include/linux/freezer.h
> index 0a8c6c4d1a82..479e35b4f2cf 100644
> --- a/include/linux/freezer.h
> +++ b/include/linux/freezer.h
> @@ -9,6 +9,7 @@
> #include <linux/wait.h>
> #include <linux/atomic.h>
> #include <linux/jump_label.h>
> +#include <linux/livepatch_sched.h>
>
> #ifdef CONFIG_FREEZER
> DECLARE_STATIC_KEY_FALSE(freezer_active);
> @@ -56,6 +57,7 @@ extern void thaw_process(struct task_struct *p);
> static inline bool try_to_freeze(void)
> {
> might_sleep();
> + klp_try_to_freeze();
> if (likely(!freezing(current)))
> return false;
> if (!(current->flags & PF_NOFREEZE))
> @@ -87,7 +89,11 @@ static inline void thaw_processes(void) {}
> static inline void thaw_kernel_threads(void) {}
> static inline void thaw_process(struct task_struct *p) {}
>
> -static inline bool try_to_freeze(void) { return false; }
> +static inline bool try_to_freeze(void)
> +{
> + klp_try_to_freeze();
> + return false;
> +}
>
> static inline void set_freezable(void) {}
>
> diff --git a/include/linux/livepatch_sched.h b/include/linux/livepatch_sched.h
> index 065c185f2763..8b9acffa3b64 100644
> --- a/include/linux/livepatch_sched.h
> +++ b/include/linux/livepatch_sched.h
> @@ -18,8 +18,18 @@ static __always_inline void klp_sched_try_switch(struct task_struct *curr)
> __klp_sched_try_switch();
> }
>
> +static __always_inline void klp_try_to_freeze(void)
> +{
> + if (static_branch_unlikely(&klp_sched_try_switch_key) &&
> + !(current->flags & PF_NOFREEZE)) {
> + guard(preempt)();
> + __klp_sched_try_switch();
> + }
> +}
> +
> #else /* !CONFIG_LIVEPATCH */
> static inline void klp_sched_try_switch(struct task_struct *curr) {}
> +static inline void klp_try_to_freeze(void) {}
> #endif /* CONFIG_LIVEPATCH */
>
> #endif /* _LINUX_LIVEPATCH_SCHED_H_ */
> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index fdb8fb07f40b..87a13421da7a 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -198,6 +198,8 @@ bool kthread_freezable_should_stop(bool *was_frozen)
>
> might_sleep();
>
> + klp_try_to_freeze();
> +
> if (unlikely(freezing(current)))
> frozen = __refrigerator(true);
>

Thanks,
Dylan