Re: [PATCH v4 2/3] livepatch: Include exiting tasks in the transition

From: Ravindran, Binoy

Date: Fri Nov 27 2026 - 14:30:25 EST



Hi Petr,

Thank you for the careful review.

> I would use [1] to reference the discussion, see below.
> ...
> It might be better to mention here the mail where the discussion
> about livepatching do_exit() started.

Will do, with the paragraph breaks you suggest.

> Instead, I would write something like: "skip tasks which are still
> accessible via the for_each_process_thread() walk, so that each
> task is visited only once."

Agreed, sorry for the confusing term. But see the next point: v5 will
not need the check at all.

> BTW: Would it hurt to process the same task twice?

No. Each callback is idempotent. Setting the same patch_state twice,
or setting or clearing TIF_PATCH_PENDING twice, changes nothing. A
second klp_update_patch_state() finds the flag already cleared, and
klp_try_switch_task() returns early for a task that has already
switched. So v5 drops the pid_alive() check, together with the
dependence on tasklist_lock and __unhash_process() that you found
tricky.

Coverage does not depend on it. A task is added to the exit list in
exit_tasks_rcu_start(), before exit_notify() removes it from the task
list. So a walk of the task list followed by a walk of the exit lists
misses no task. A task that leaves the exit list before the second
walk is covered by the wait.

> Assisted-by: Claude:claude-opus-5-5
>
> This is not needed.

OK, I will drop it.

> So, rename:
>
> + klp_exiting_init -> klp_exit_task_init_transition
> + klp_exiting_start -> klp_exit_task_start_transition
> + klp_exiting_complete -> klp_exit_task_complete_transition

Will do, with klp_exit_task_reverse_transition and
klp_exit_task_force_transition for the other two.

> I would be more precise. I suggest something like:

Thanks, I will use your text, with the synchronize_rcu() note in a
separate paragraph.

> BTW: Do we need it in all situations, for example, when the 1st
> livepatch gets enabled? In this case, nothing is removed.

No. It is needed only when code can go away afterwards: when the
target state is KLP_UNPATCHED, or when the patch replaces others. I
can call it only in those cases, with a comment that says why. Or I
can keep it unconditional for simplicity, if you prefer.

> Hmm, I would move this up, above the comment: "/* Done! Now cleanup
> the data structures. */".
> ...
> Or is there any reason why it is done here?

No reason. I will move it.

> Why exactly do we need the synchronization here, please?

It is about the check in klp_ftrace_handler(), as you say. Once
func->transition is set, the handler reads current->patch_state and
warns if it is KLP_TRANSITION_IDLE. A task that has already left the
exit lists is not visited by klp_init_transition(), so its state is
still IDLE. If it then calls one of the functions you listed, it hits
that warning. The wait makes sure all such tasks are gone before
func->transition is set.

> But wait! If this is the problem then we might need to call
> klp_synchronize_transition() in each stage which calls
> rcu_tasks_visit_exiting().

I think the other stages are already covered. klp_start_transition()
only sets TIF_PATCH_PENDING, which the handler does not read.
klp_reverse_transition() already calls klp_synchronize_transition().
klp_complete_transition() clears func->transition before it sets
patch_state to IDLE, and the handler reads patch_state only while
func->transition is set. klp_force_transition() gives up the
consistency check by design. I will spell this out in the comment.

> PS: It is an amazing work for a newcomer.

Thank you, that is very kind.

Thanks,
Binoy