Live Patching
 help / color / mirror / Atom feed
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 1/2] sched, livepatch: Fix transition stall for CPU-bound tasks
Date: Wed, 25 Nov 2026 04:18:59 +0000	[thread overview]
Message-ID: <20261125041900.3243569-2-dylanbhatch@google.com> (raw)
In-Reply-To: <20261125041900.3243569-1-dylanbhatch@google.com>

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


  reply	other threads:[~2026-11-25  4:19 UTC|newest]

Thread overview: 4+ 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 ` Dylan Hatch [this message]
2026-11-25  4:46   ` [PATCH v2 1/2] " sashiko-bot
2026-11-25  4:19 ` [PATCH v2 2/2] selftests/livepatch: Add cond_resched() transition tests Dylan Hatch

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-2-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox