From: Peter Zijlstra <peterz@infradead.org>
To: Andrea Parri <parri.andrea@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>,
Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
Petr Mladek <pmladek@suse.com>,
Joe Lawrence <joe.lawrence@redhat.com>,
Song Liu <song@kernel.org>,
live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] livepatch: Fix races between stack check and module load/unload
Date: Mon, 12 Oct 2026 16:49:33 +0200 [thread overview]
Message-ID: <20261012144933.GW2009045@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20261012140546.154730-1-parri.andrea@gmail.com>
On Mon, Oct 12, 2026 at 04:05:45PM +0200, Andrea Parri wrote:
> diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
> index 3f54a017bbf69..d01ec7a6256ab 100644
> --- a/kernel/livepatch/patch.c
> +++ b/kernel/livepatch/patch.c
> @@ -22,15 +22,19 @@
>
> static LIST_HEAD(klp_ops);
>
> +/*
> + * Caller must hold klp_mutex or rcu_read_lock(): klp_check_stack_func() walks
> + * klp_ops from the scheduler without klp_mutex.
> + */
> struct klp_ops *klp_find_ops(void *old_func)
> {
> struct klp_ops *ops;
> struct klp_func *func;
>
> - list_for_each_entry(ops, &klp_ops, node) {
> - func = list_first_entry(&ops->func_stack, struct klp_func,
> - stack_node);
> - if (func->old_func == old_func)
> + list_for_each_entry_rcu(ops, &klp_ops, node, lockdep_is_held(&klp_mutex)) {
> + func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
> + stack_node);
> + if (func && READ_ONCE(func->old_func) == old_func)
> return ops;
> }
>
> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index 2351a19ac2a9e..e9aa16baf1743 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -8,6 +8,7 @@
> #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>
> #include <linux/cpu.h>
> +#include <linux/rculist.h>
> #include <linux/stacktrace.h>
> #include <linux/static_call.h>
> #include "core.h"
> @@ -201,12 +202,16 @@ void klp_update_patch_state(struct task_struct *task)
> /*
> * Determine whether the given stack trace includes any references to a
> * to-be-patched or to-be-unpatched function.
> + *
> + * Must be called under rcu_read_lock(), see klp_check_stack().
> */
> static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
> unsigned int nr_entries)
> {
> unsigned long func_addr, func_size, address;
> + struct klp_func *prev;
> struct klp_ops *ops;
> + void *old_func;
> int i;
>
> if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
As per always, I much prefer things like lockdep_assert_held() and
__must_hold() over comments.
next prev parent reply other threads:[~2026-10-12 14:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-12 14:05 Andrea Parri
2026-10-12 14:49 ` Peter Zijlstra [this message]
2026-10-12 15:07 ` Andrea Parri
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=20261012144933.GW2009045@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=parri.andrea@gmail.com \
--cc=pmladek@suse.com \
--cc=song@kernel.org \
--cc=stable@vger.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
all inboxes | Powered by JetHome®