Live Patching
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Binoy Ravindran" <binoy@vt.edu>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Neeraj Upadhyay" <neeraj.upadhyay@kernel.org>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Josh Triplett" <josh@joshtriplett.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
	"Lai Jiangshan" <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang@linux.dev>,
	"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@kernel.org>,
	"Jiri Kosina" <jikos@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>
Cc: live-patching@vger.kernel.org,
	"Yafang Shao" <laoar.shao@gmail.com>,
	"Jaidev Shastri" <jaidevshastri@vt.edu>
Subject: Re: [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition
Date: Sat, 05 Dec 2026 18:27:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-251790@kernel.org> (raw)
In-Reply-To: <20261205-klp-series-v3-v5-4-3d60c8ea9316@vt.edu>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] livepatch: klp_wait_exiting_tasks() checks per-patch forced
  flag
- [High] livepatch: AB-BA deadlock between klp_mutex and
  tasks_rcu_exit_srcu
- [High] livepatch: unprivileged PID namespace can pin
  tasks_rcu_exit_srcu indefinitely
- [Medium] livepatch: unbacked-off 10 ms exit polling keeps
  klp_sched_try_switch active
--

commit fe2e50e0784ef2725574105892343b959dcc0320
Author: Binoy Ravindran <binoy@vt.edu>

livepatch: Wait for exiting tasks around a transition

This commit waits for tasks in the do_exit() SRCU exit window before
starting and before completing a livepatch transition, using a killable
wait in klp_init_transition() and a polled wait in
klp_try_complete_transition().

Link: https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/

> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index 33095e888e10c..fb033483d02dd 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -426,6 +426,36 @@ static void klp_send_signals(void)
>  	read_unlock(&tasklist_lock);
>  }
>  
> +/*
> + * Tasks in the exit window, between exit_tasks_rcu_start() and
> + * exit_tasks_rcu_finish(), are no longer on the task list, so the stack
> + * checks above do not see them.  They may still run code that the cleanup
> + * is about to remove.  Once every other task has switched, start an SRCU
> + * grace period for the window and poll it.  Tasks do not switch back, so
> + * the retries only poll, every 10 ms, without walking the tasks again.
> + * klp_mutex is not held across the wait, a task stuck in the window delays
> + * the transition like any other task that cannot switch, and a forced
> + * transition does not wait for it.
> + *
> + * Return true if the transition must wait; it has then been rescheduled.
> + */
> +static unsigned long klp_exit_cookie;
> +static bool klp_exit_cookie_valid;
> +
> +static bool klp_wait_exiting_tasks(void)
> +{
> +	if (klp_transition_patch->forced)
> +		return false;

[Severity: High]
Does checking klp_transition_patch->forced work for forced enabling or
replace transitions?

klp_force_transition() only sets klp_transition_patch->forced when
klp_target_state == KLP_TRANSITION_UNPATCHED.  For KLP_TRANSITION_PATCHED
transitions it stays false, so forcing an enable or replace transition
still loops here every 10 ms forever if a task is stuck in the exit window.

Conversely, klp_reverse_transition() leaves klp_transition_patch->forced
set.  Reversing a replace patch to unpatching, forcing it, and reversing
back skips the wait while prior patches remain unforced, letting
klp_free_patch_finish() cause a use-after-free on exiting tasks.

Using a separate transition-scoped flag set in klp_force_transition() and
cleared in klp_init_transition() and klp_reverse_transition() would avoid
both problems.

> +	if (!klp_exit_cookie_valid) {
> +		klp_exit_cookie = start_poll_synchronize_rcu_tasks_exit();
> +		klp_exit_cookie_valid = true;
> +	}
> +	if (poll_state_synchronize_rcu_tasks_exit(klp_exit_cookie))
> +		return false;
> +	schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(10));
> +	return true;
> +}

[Severity: Medium]
Should this polling back off instead of firing every 10 ms indefinitely?

If a task lingers in the exit window, rescheduling klp_transition_work
every unrounded 10 ms wakes a CPU and acquires klp_mutex 100 times per
second, preventing tickless idle and deep C-states (unlike the existing
round_jiffies_relative(HZ) retry).

In addition, klp_resched_disable() is not called until exiting_done, which
keeps klp_sched_try_switch_key enabled in __schedule() across all CPUs for
the entire wait even though all live and idle tasks have already switched.

[ ... ]

> @@ -551,12 +606,52 @@ void klp_start_transition(void)
>  	klp_signals_cnt = 0;
>  }
>  
> +/*
> + * Wait for the tasks in the exit window to leave it.  Return -EINTR if the
> + * current task gets a fatal signal first.
> + */
> +static int klp_wait_exiting_tasks_killable(void)
> +{
> +	unsigned long cookie = start_poll_synchronize_rcu_tasks_exit();
> +
> +	while (!poll_state_synchronize_rcu_tasks_exit(cookie)) {
> +		if (fatal_signal_pending(current))
> +			return -EINTR;
> +		schedule_timeout_killable(msecs_to_jiffies(10));
> +	}
> +	return 0;
> +}

[Severity: High]
Can an unprivileged user pin tasks_rcu_exit_srcu here indefinitely?

With unshare(CLONE_NEWUSER | CLONE_NEWPID), a parent can fork PID 1 and
PID 2 into the child namespace, let PID 1 exit, and never reap PID 2.
do_exit() calls exit_tasks_rcu_start() before zap_pid_ns_processes(), so
PID 1 blocks in TASK_IDLE while holding the tasks_rcu_exit_srcu read lock.

Any host livepatch enable or disable then hangs here holding klp_mutex,
blocking all module loads and unloads in klp_module_coming() and
klp_module_going() until killed, and any active transition stalls
indefinitely in klp_wait_exiting_tasks().

[ ... ]

> @@ -597,6 +692,28 @@ void klp_init_transition(struct klp_patch *patch, int state)
>  		task->patch_state = initial_state;
>  	}
>  
> +	/*
> +	 * Tasks in the exit window were not visited above, so their patch_state
> +	 * is stale.  Once func->transition is set, klp_ftrace_handler() would
> +	 * warn about them, or run the wrong version of a function for them.
> +	 * Wait for them to leave the window.  Tasks that enter it later were on
> +	 * the task list above.  The wait can be long, or never end if a task is
> +	 * stuck in the window, so the caller can interrupt it with a fatal
> +	 * signal.
> +	 *
> +	 * Tasks that already left the window run with preemption disabled until
> +	 * they finally stop, and may still call functions such as __schedule().
> +	 * Make sure that they stopped running as well.  synchronize_rcu() is not
> +	 * enough: __schedule() reports the RCU quiescent state before
> +	 * switch_to().
> +	 */
> +	if (klp_wait_exiting_tasks_killable()) {
> +		klp_undo_init_transition();
> +		return -EINTR;
> +	}

[Severity: High]
Can waiting for tasks_rcu_exit_srcu here under klp_mutex cause an ABBA
deadlock?

Task A (root ns)      Task I (child init)    Task B (child ns)
----------------      -------------------    -----------------
lock(&klp_mutex)
                      exit_tasks_rcu_start()
                      zap_pid_ns_processes()
                        SIGKILL -> Task B
                                             stack_order_show()
                                               lock(&klp_mutex)
                                                 <-- uninterruptible
                        wait for Task B
klp_wait_exiting_tasks_killable()
  <-- waits for tasks_rcu_exit_srcu

 *** DEADLOCK ***

Task B blocks uninterruptibly in stack_order_show() (world-readable sysfs)
or klp_module_coming()/klp_module_going() and ignores Task I's SIGKILL.
Task A is outside the child namespace so Task I never signals it; all three
tasks deadlock and module loading stalls on klp_mutex until Task A is killed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-klp-series-v3-v5-0-3d60c8ea9316@vt.edu?part=4

      reply	other threads:[~2026-12-05 18:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-251790@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=binoy@vt.edu \
    --cc=boqun@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=frederic@kernel.org \
    --cc=jaidevshastri@vt.edu \
    --cc=jiangshanlai@gmail.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=jpoimboe@kernel.org \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=laoar.shao@gmail.com \
    --cc=live-patching@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mbenes@suse.cz \
    --cc=mgorman@suse.de \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=qiang.zhang@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=urezki@gmail.com \
    --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