Live Patching
 help / color / mirror / Atom feed
* [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition
@ 2026-12-05 18:08 Binoy Ravindran via B4 Relay
  2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 UTC (permalink / raw)
  To: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
	Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
	Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
	Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
	Song Liu, Ingo Molnar, Jiri Kosina, Ingo Molnar, Peter Zijlstra,
	Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
	Mel Gorman, Valentin Schneider, K Prateek Nayak
  Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
	Binoy Ravindran

After exit_notify(), a self-reaping task is unhashed from the task list
but keeps running the rest of do_exit(). The livepatch transition walks
the task list, so it can complete while such a task still runs code from
a patch module that atomic replace is about to free. Yafang Shao
reported this race in [1] and discussed it there with Petr Mladek, and
Josh Poimboeuf suggested building on the exit tracking of
exit_tasks_rcu_start() and exit_tasks_rcu_finish() in [2]. A task that
was already in this exit window when a transition starts is also never
initialized, so if it then calls a function in transition,
klp_ftrace_handler() warns or picks the wrong version of the function.
That second problem predates the RCU-tasks exit lists.

v5 follows Paul McKenney's suggestion in the v4 thread. Patch 1 makes
the exit window, from exit_tasks_rcu_start() to exit_tasks_rcu_finish(),
an SRCU read-side critical section, as it was before the exit lists, and
adds blocking and polled waits for it. Patch 4 uses them:

  - klp_init_transition() waits for the window before func->transition
    is set, so that no task in it runs a patched function uninitialized.
    The wait is killable: on a fatal signal, klp_init_transition()
    undoes its setup and returns -EINTR, and enabling or disabling the
    patch fails.

  - klp_try_complete_transition() starts a polled grace period once
    every other task has switched. Tasks do not switch back, so the
    retries only poll it, every 10 ms, without walking the tasks again.
    klp_mutex is not held across this wait, and a forced transition
    does not wait.

As in v4, klp_synchronize_transition() waits out the tasks past
exit_tasks_rcu_finish(), which run with preemption disabled until their
final switch, in both places. The exit-list walks of v4 are gone, and so
is the walker interface on the RCU side. Patch 2 makes
klp_synchronize_transition() retry if schedule_on_each_cpu() cannot
allocate its work items. Patch 3, suggested by Yafang, makes LIVEPATCH
select TASKS_RCU, which every livepatch kernel already builds.

The SRCU wait covers every task in the exit window, whether or not it
runs patched code, so a slow exit delays the transition by its length.
The wait at init holds klp_mutex, and a task stuck in the window could
make it last indefinitely. It could also deadlock: a task that loads a
livepatch inside a PID namespace whose init is exiting waits for that
init, which waits in zap_pid_ns_processes() for the task. Because the
wait is killable, the loading task can be killed, and in the namespace
case the exiting init sends it SIGKILL. The wait at completion does not
hold klp_mutex, and a forced transition does not wait.

Tested on v7.4-rc2 under KVM, with two livepatches that replace each
other in a loop and a debug-only sleep after exit_notify(). On x86-64,
with klp-build:

  - do_exit() replaced, with no sleep and with sleeps of 20 ms and 2 s:
    30 runs and 420 replace cycles without a fault. Without the series,
    6 of 6 runs crashed with a 2 s sleep.
  - A function called after the sleep, inside the exit window, replaced
    instead: no livepatch warning in 24 runs. Without the series, all 24
    warned.
  - With lockdep, PROVE_RCU and DEBUG_ATOMIC_SLEEP: 10 runs and 180
    replace cycles with no reports.
  - With CPUs going offline and online throughout: 12 runs and 120
    replace cycles without a fault.
  - 12 runs that force a transition all finished.
  - With exiting tasks sleeping 15 s in the window, insmod was killed
    5 s into the wait at init. In 6 of 6 runs it exited within 0.03 s
    and left nothing loaded, and a later insmod enabled the patch.
    Without the killable wait, insmod exited only after about 10 s, and
    the patch was enabled anyway.

On arm64, with kpatch-build, do_exit() replaced with no sleep and with
sleeps of 20 ms, 200 ms and 2 s: 42 runs and 540 replace cycles without
a fault.

Each commit builds without warnings with and without CONFIG_LIVEPATCH,
and with W=1 for the files it touches.

Transition time, from "starting patching transition" to "patching
complete", as p90 / p99, for a livepatch that does not touch the exit
path, with 36 runs per exit sleep (18 for no sleep):

  no sleep:  exit-list walk 1.68 / 1.79 s,  v5 1.63 / 1.78 s
  500 ms:    exit-list walk 1.73 / 1.84 s,  v5 2.11 / 2.31 s
  1 s:       exit-list walk 1.75 / 1.91 s,  v5 2.69 / 2.83 s
  2 s:       exit-list walk 1.84 / 2.03 s,  v5 3.70 / 3.85 s

A reproducer and the logs are available on request.

checkpatch.pl in v7.4-rc2 reports the Assisted-by: trailer as
non-standard. The trailer follows
Documentation/process/coding-assistants.rst.

[1] https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/
[2] https://lore.kernel.org/live-patching/20250124010848.pdcxokfsk2ngrowl@jpoimboe/

---
Changes in v5:
- Replace the exit-list walks with an SRCU read side over the exit
  window and waits for it (Paul McKenney). This replaces
  "rcu-tasks: Add helpers to visit tasks on the exit lists" with
  "rcu-tasks: Make the exit window an SRCU read-side critical section",
  and "livepatch: Include exiting tasks in the transition" with
  "livepatch: Wait for exiting tasks around a transition".
- Wait for the exit window in klp_init_transition() too, which also
  fixes the stale patch_state of tasks that were in the window when the
  transition started. Make that wait killable.
- Add "livepatch: Retry klp_synchronize_transition() if it fails".
- Point [1] and the Link: tag of patch 4 at the original crash report.
- Link to v4: https://patch.msgid.link/20261126-klp-series-v3-v4-0-2ad73e9c9254@vt.edu

To: "Paul E. McKenney" <paulmck@kernel.org>
To: Frederic Weisbecker <frederic@kernel.org>
To: Neeraj Upadhyay <neeraj.upadhyay@kernel.org>
To: Joel Fernandes <joelagnelf@nvidia.com>
To: Josh Triplett <josh@joshtriplett.org>
To: Boqun Feng <boqun@kernel.org>
To: Uladzislau Rezki <urezki@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Lai Jiangshan <jiangshanlai@gmail.com>
To: Zqiang <qiang.zhang@linux.dev>
To: Josh Poimboeuf <jpoimboe@kernel.org>
To: Miroslav Benes <mbenes@suse.cz>
To: Petr Mladek <pmladek@suse.com>
To: Joe Lawrence <joe.lawrence@redhat.com>
To: Song Liu <song@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
To: Jiri Kosina <jikos@kernel.org>
To: Ingo Molnar <mingo@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>
To: Juri Lelli <juri.lelli@redhat.com>
To: Vincent Guittot <vincent.guittot@linaro.org>
To: Dietmar Eggemann <dietmar.eggemann@arm.com>
To: Ben Segall <bsegall@google.com>
To: Mel Gorman <mgorman@suse.de>
To: Valentin Schneider <vschneid@redhat.com>
To: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: live-patching@vger.kernel.org
Cc: Yafang Shao <laoar.shao@gmail.com>
Cc: Jaidev Shastri <jaidevshastri@vt.edu>

---
Binoy Ravindran (4):
      rcu-tasks: Make the exit window an SRCU read-side critical section
      livepatch: Retry klp_synchronize_transition() if it fails
      livepatch: Select TASKS_RCU
      livepatch: Wait for exiting tasks around a transition

 include/linux/rcupdate.h      |   6 ++
 include/linux/sched.h         |   1 +
 kernel/livepatch/Kconfig      |   1 +
 kernel/livepatch/core.c       |   9 ++-
 kernel/livepatch/transition.c | 131 +++++++++++++++++++++++++++++++++++++++++-
 kernel/livepatch/transition.h |   2 +-
 kernel/rcu/tasks.h            |  28 +++++++++
 7 files changed, 173 insertions(+), 5 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261123-klp-series-v3-d52796c0590f

Best regards,
--  
Binoy Ravindran <binoy@vt.edu>



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-05 18:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
2026-12-05 18:28   ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails Binoy Ravindran via B4 Relay
2026-12-05 18:22   ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 3/4] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Binoy Ravindran via B4 Relay
2026-12-05 18:27   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox