Live Patching
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yafang Shao" <laoar.shao@gmail.com>,
	jpoimboe@kernel.org, mbenes@suse.cz, pmladek@suse.com,
	joe.lawrence@redhat.com, song@kernel.org
Cc: live-patching@vger.kernel.org
Subject: Re: [PATCH] livepatch: Avoid blocking tasklist_lock too long
Date: Tue, 17 Nov 2026 13:29:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-218879@kernel.org> (raw)
In-Reply-To: <20261117130848.65142-1-laoar.shao@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] livepatch: race between klp_try_complete_transition() and
  klp_copy_process()
--

commit ceb01fc48f77409ab4a5313629520f441760c90c
Author: Yafang Shao <laoar.shao@gmail.com>

livepatch: Avoid blocking tasklist_lock too long

This commit splits the task iteration in klp_try_complete_transition() into
a first pass under rcu_read_lock() and a second pass under tasklist_lock to
reduce tasklist_lock hold times during livepatch transitions.

Link: https://github.com/redis/redis [0]
Link: https://lore.kernel.org/all/20250227024733.16989-1-laoar.shao@gmail.com/ [1]
Link: https://lore.kernel.org/all/Z8CRXxfI24E9_IHC@pathway.suse.cz/ [2]
Link: https://lore.kernel.org/all/20250213013603.i6uxtjvc3qxlsqwc@jpoimboe/ [3]

> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index 2351a19ac2a9e..e058d21d84b75 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -444,7 +444,27 @@ void klp_try_complete_transition(void)
>  	 *
>  	 * Usually this will transition most (or all) of the tasks on a system
>  	 * unless the patch includes changes to a very common function.
> +	 *
> +	 * To avoid holding the tasklist_lock for too long, we iterate over the
> +	 * tasks in two passes. In the first pass, we use the lightweight RCU
> +	 * read lock to iterate through most tasks. This pass will cover the
> +	 * majority of tasks, except for some newly forked tasks which may be
> +	 * added into the tasklist concurrently. In the second pass, we acquire
> +	 * the tasklist_lock to handle the tasks missed in the first pass.
>  	 */
> +
> +	/* First pass: Iterate over tasks using the lightweight RCU read lock. */
> +	rcu_read_lock();
> +	for_each_process_thread(g, task) {
> +		/* klp_check_stack() requires preemption to be disabled */
> +		if (task == current)
> +			continue;
> +
> +		klp_try_switch_task(task);
> +	}
> +	rcu_read_unlock();

[Severity: Medium]
Can this RCU pass race with klp_copy_process() on CONFIG_PREEMPT_RT kernels
and leave a newly forked child with TIF_PATCH_PENDING still set when the
transition completes?

klp_copy_process() relies on tasklist_lock to serialize against transition
updates while copying TIF_PATCH_PENDING and patch_state from current to
child:

kernel/livepatch/transition.c:klp_copy_process() {
	if (test_tsk_thread_flag(current, TIF_PATCH_PENDING))
		set_tsk_thread_flag(child, TIF_PATCH_PENDING);
	else
		clear_tsk_thread_flag(child, TIF_PATCH_PENDING);

	child->patch_state = current->patch_state;
}

On CONFIG_PREEMPT_RT, write_lock_irq(&tasklist_lock) in copy_process() is a
preemptible rtmutex lock, and unwinders such as x86_64 ORC unwind reliably
across in-kernel preemption frames:

CPU0 (parent in fork)             CPU1 (klp_try_complete_transition)
copy_process()
  write_lock_irq(&tasklist_lock)
  klp_copy_process(child)
    set_tsk_thread_flag(child,
      TIF_PATCH_PENDING)
    <preempted>
                                  /* First pass under rcu_read_lock() */
                                  klp_try_switch_task(parent)
                                    ...
                                    clear_tsk_thread_flag(parent,
                                      TIF_PATCH_PENDING)
                                    parent->patch_state = klp_target_state
    child->patch_state =
      current->patch_state
  write_unlock_irq(&tasklist_lock)
                                  /* Second pass under tasklist_lock */
                                  klp_try_switch_task(child)
                                    if (task->patch_state == klp_target_state)
                                      return true;
                                  klp_complete_transition()
                                    WARN_ON_ONCE(test_tsk_thread_flag(child,
                                      TIF_PATCH_PENDING))  <-- stale flag

Because child->patch_state already matches klp_target_state, the second pass
under tasklist_lock returns early in klp_try_switch_task() without clearing
TIF_PATCH_PENDING on child.  klp_complete_transition() then triggers
WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)) and leaves the
flag set on the child task.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117130848.65142-1-laoar.shao@gmail.com?part=1

  reply	other threads:[~2026-11-17 13:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 13:08 [PATCH] livepatch: Avoid blocking tasklist_lock too long Yafang Shao
2026-11-17 13:29 ` sashiko-bot [this message]
2026-11-17 14:26   ` Yafang Shao

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-218879@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=laoar.shao@gmail.com \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=song@kernel.org \
    /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