From: sashiko-bot@kernel.org
To: "Zhu, Yi" <chuyee@google.com>,
"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>,
"Wilson Felipe Pereira" <wfelipe@google.com>,
"Dylan Hatch" <dylanbhatch@google.com>
Cc: live-patching@vger.kernel.org
Subject: Re: [PATCH 2/2] livepatch: check task stacks against a sorted range table
Date: Fri, 04 Dec 2026 00:59:18 +0000 [thread overview]
Message-ID: <sashiko-outbox-248863@kernel.org> (raw)
In-Reply-To: <4a186e29014b74d0bf480a70cb7e41b543758939.1796344045.git.chuyee@google.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] livepatch: missing smp_wmb() after klp_update_stack_ranges()
- [Medium] livepatch: unnecessary klp_update_stack_ranges() on module
events
- [Medium] livepatch: PROVE_RCU warning from rcu_dereference_sched() on
PREEMPT_RT
--
commit 0663b7e5bf136d2c344658ea79f62f99071f953d
Author: Zhu, Yi <chuyee@google.com>
livepatch: check task stacks against a sorted range table
This commit builds an RCU-published sorted table of patched function
address ranges so klp_check_stack() can look up stack entries with
bsearch() instead of scanning every patched function for every task.
> diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
> index b83715272e8c3..355ba21705544 100644
> --- a/kernel/livepatch/core.c
> +++ b/kernel/livepatch/core.c
> @@ -1366,6 +1366,10 @@ int klp_module_coming(struct module *mod)
> }
> }
>
> + /* The transition patch may now patch functions in this module. */
> + if (klp_transition_patch)
> + klp_update_stack_ranges();
> +
> mutex_unlock(&klp_mutex);
>
> return 0;
> @@ -1380,6 +1384,8 @@ int klp_module_coming(struct module *mod)
> mod->klp_alive = false;
> obj->mod = NULL;
> klp_cleanup_module_patches_limited(mod, patch);
> + if (klp_transition_patch)
> + klp_update_stack_ranges();
> mutex_unlock(&klp_mutex);
>
> return ret;
> @@ -1401,6 +1407,10 @@ void klp_module_going(struct module *mod)
>
> klp_cleanup_module_patches_limited(mod, NULL);
>
> + /* Drop this module's functions from the transition range table. */
> + if (klp_transition_patch)
> + klp_update_stack_ranges();
> +
> mutex_unlock(&klp_mutex);
> }
[Severity: Medium]
Should klp_update_stack_ranges() only run when klp_transition_patch
actually patched or unpatched an object for mod?
Right now any unrelated module load or unload during an active
transition allocates, rebuilds, and sorts the entire range table under
klp_mutex. The call on the err: path in klp_module_coming() is also
redundant, since klp_stack_ranges was never updated before the failure
and klp_cleanup_module_patches_limited() restores the previous state.
If kvmalloc() fails transiently during one of these unnecessary
rebuilds, klp_update_stack_ranges() overwrites the valid table with
NULL, falling back to the O(N * D) stack check for the rest of the
transition. Tracking whether klp_transition_patch touched mod and
dropping the err: call avoids both.
> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index ef70ff582da18..fc35e09eff692 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
[ ... ]
> @@ -266,6 +420,11 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
> return -EINVAL;
> nr_entries = ret;
>
> + ranges = rcu_dereference_sched(klp_stack_ranges);
> + if (ranges)
> + return klp_check_stack_ranges(ranges, entries, nr_entries,
> + oldname);
> +
> klp_for_each_object(klp_transition_patch, obj) {
[Severity: Medium]
Does rcu_dereference_sched(klp_stack_ranges) trigger a CONFIG_PROVE_RCU
warning on CONFIG_PREEMPT_RT?
When klp_try_complete_transition() checks task == current, it holds
klp_mutex and read_lock(&tasklist_lock) and calls klp_check_stack()
directly without disabling preemption. On PREEMPT_RT, rt_read_lock()
takes rcu_read_lock() and migrate_disable() while leaving preemption
enabled, so rcu_read_lock_sched_held() returns false and fires a
suspicious RCU usage splat.
Using rcu_dereference_sched_check(klp_stack_ranges,
lockdep_is_held(&klp_mutex)), as klp_find_ops() does, avoids the
warning.
[ ... ]
> @@ -517,6 +676,13 @@ void klp_start_transition(void)
> klp_transition_patch->mod->name,
> klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
>
> + /*
> + * Build the range table for klp_check_stack() before any task can be
> + * marked TIF_PATCH_PENDING and check its stack. This also covers a
> + * reversed transition, whose target state just changed.
> + */
> + klp_update_stack_ranges();
> +
> /*
> * Mark all normal tasks as needing a patch state update. They'll
> * switch either in klp_try_complete_transition() or as they exit the
[Severity: Medium]
Is an smp_wmb() missing between klp_update_stack_ranges() and setting
TIF_PATCH_PENDING?
During klp_reverse_transition(), klp_sched_try_switch_key is already
enabled, and its existing smp_wmb() runs before klp_start_transition().
Inside klp_update_stack_ranges(), rcu_replace_pointer() only uses
smp_store_release(), which does not order the klp_stack_ranges store
before the subsequent unordered set_tsk_thread_flag(...,
TIF_PATCH_PENDING) writes.
CPU0 CPU1
klp_reverse_transition()
klp_target_state = ...
smp_wmb()
klp_start_transition()
klp_update_stack_ranges()
rcu_replace_pointer(...)
set_tsk_thread_flag(...)
__klp_sched_try_switch()
klp_patch_pending(current) -> true
smp_rmb()
klp_try_switch_task(current)
klp_check_stack()
rcu_dereference_sched(...)
<-- sees stale range table
On a weakly-ordered architecture such as powerpc or arm64, CPU1 can
observe TIF_PATCH_PENDING and the flipped klp_target_state with the
pre-reversal range table. When reversing to unpatch, that checks
old_func instead of new_func, allowing a task to switch while a patched
function is still on its stack and potentially return into freed
livepatch module text.
Adding smp_wmb() after klp_update_stack_ranges() pairs with the
smp_rmb() in __klp_sched_try_switch().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796344045.git.chuyee@google.com?part=2
prev parent reply other threads:[~2026-12-04 0:59 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 0:35 [PATCH 0/2] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 0:35 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 0:51 ` sashiko-bot
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 0:59 ` 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-248863@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=chuyee@google.com \
--cc=dylanbhatch@google.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=pmladek@suse.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=song@kernel.org \
--cc=wfelipe@google.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