Re: [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition

From: Yafang Shao

Date: Tue Nov 24 2026 - 01:47:47 EST


On Tue, Nov 24, 2026 at 12:57 PM Ravindran, Binoy <binoy@xxxxxx> wrote:
>
>
> Hi Yafang,
>
> Thanks for the pointers to the earlier thread and to Josh's
> suggestion.
>
> > I did not take it for the same reason Sashiko raised in issue 2: the
> > RCU exit list narrows the race window but does not close it.
> > I could not see how klp_synchronize_transition(), as you describe
> > below, completely resolves it. Could you please explain that in
> > detail?
>
> The exit list alone does not close it, and v2 does not rely on it
> alone. The exit list covers the part of the exit path that can sleep.
> The part it misses cannot sleep or be preempted, and
> klp_synchronize_transition() waits it out. In detail:
>
> 1. Where the gap is. An exiting task is on an exit list from
> exit_tasks_rcu_start() to exit_tasks_rcu_finish(). exit_notify()
> runs inside that range, and the task is unhashed no earlier than
> exit_notify(). So until exit_tasks_rcu_finish() the task is on the
> task list, an exit list, or both. After exit_tasks_rcu_finish() it
> may be on neither, if it has already been unhashed. That is the
> gap in issue 2.
>
> 2. What the task can do in the gap. do_exit() calls preempt_disable()
> before exit_rcu() and exit_tasks_rcu_finish(). After that the task
> runs only lockdep_free_task() and do_task_dead(), whose __schedule()
> is its last. From preempt_disable() on, the task stays on its CPU:
> it cannot sleep, be preempted, or migrate. So once a task is in the
> gap, the next context switch on its CPU is the task's final one.
>
> 3. What klp_synchronize_transition() guarantees. It calls
> schedule_on_each_cpu() and returns only after a work item has run
> on every online CPU, so every CPU has switched context since the
> call began. Any task that was in the gap when the call began has
> reached its final __schedule() and will never run again.
>
> 4. Where v2 calls it.
>
> a. In klp_init_transition(), after patch_state has been set on
> every task (the task list and the exit lists, walked under
> tasklist_lock) and before func->transition is set. A task that
> the walk did not visit had left the exit lists before or during
> the walk, so it was in the gap when the sync began, and it is
> dead before any function enters the transition. So when
> func->transition is set, every task that can still run has a
> valid patch_state.
>
> b. In klp_try_complete_transition(), before it calls
> klp_complete_transition(). A task in the gap can still be
> running code reached through the old function, and the switch
> check cannot see it. The sync makes it dead before
> func->transition is cleared and before the old patch can be
> freed. A task that leaves the exit lists after this sync was
> already counted as switched, so it runs the new code.
>
> So there is no point at which a task that can still run patched code
> is invisible to the transition: before exit_tasks_rcu_finish() it is
> on a list the transition walks, and after it, the sync waits for it to
> die.

Thanks for the detailed explanation.
It makes sense to me.

>
> One limit: the exit lists exist only with CONFIG_TASKS_RCU. Without
> it, the helpers are empty and only the final non-preemptible stretch
> is covered.

Is it worth adding yet another separate list for the klp transition?
If not, I think we should at least make the CONFIG_TASKS_RCU dependency
explicit:

--- a/kernel/livepatch/Kconfig
+++ b/kernel/livepatch/Kconfig
@@ -12,6 +12,7 @@ config LIVEPATCH
depends on KALLSYMS_ALL
depends on HAVE_LIVEPATCH
depends on !TRIM_UNUSED_KSYMS
+ select TASKS_RCU
help
Say Y here if you want to support kernel live patching.
This option has no runtime impact until a kernel "patch"

--
Regards
Yafang