From: Jiri Olsa <olsajiri@gmail.com>
To: Pu Lehui <pulehui@huaweicloud.com>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jann Horn <jannh@google.com>, Pu Lehui <pulehui@huawei.com>
Subject: Re: [PATCH bpf-next] bpf: Move out synchronize_rcu_tasks_trace from mutex CS
Date: Fri, 3 Jan 2025 16:22:45 +0100 [thread overview]
Message-ID: <Z3gAxZTJTYngLnYi@krava> (raw)
In-Reply-To: <20241231033509.349277-1-pulehui@huaweicloud.com>
On Tue, Dec 31, 2024 at 03:35:09AM +0000, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
>
> Commit ef1b808e3b7c ("bpf: Fix UAF via mismatching bpf_prog/attachment
> RCU flavors") resolved a possible UAF issue in uprobes that attach
> non-sleepable bpf prog by explicitly waiting for a tasks-trace-RCU grace
> period. But, in the current implementation, synchronize_rcu_tasks_trace
> is included within the mutex critical section, which increases the
> length of the critical section and may affect performance. So let's move
> out synchronize_rcu_tasks_trace from mutex CS.
>
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> ---
> kernel/trace/bpf_trace.c | 18 +++++++++++-------
> 1 file changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 48db147c6c7d..30ef8a6f5ca2 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -2245,12 +2245,15 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
> {
> struct bpf_prog_array *old_array;
> struct bpf_prog_array *new_array;
> + struct bpf_prog *prog;
> int ret;
>
> mutex_lock(&bpf_event_mutex);
>
> - if (!event->prog)
> - goto unlock;
> + if (!event->prog) {
> + mutex_unlock(&bpf_event_mutex);
> + return;
> + }
>
> old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
> if (!old_array)
> @@ -2265,6 +2268,11 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
> }
>
> put:
> + prog = event->prog;
> + event->prog = NULL;
> +
> + mutex_unlock(&bpf_event_mutex);
> +
> /*
> * It could be that the bpf_prog is not sleepable (and will be freed
> * via normal RCU), but is called from a point that supports sleepable
> @@ -2272,11 +2280,7 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
> */
> synchronize_rcu_tasks_trace();
>
> - bpf_prog_put(event->prog);
> - event->prog = NULL;
> -
> -unlock:
> - mutex_unlock(&bpf_event_mutex);
> + bpf_prog_put(prog);
> }
>
> int perf_event_query_prog_array(struct perf_event *event, void __user *info)
> --
> 2.34.1
>
would something like below be simpler? (not tested)
jirka
---
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 973104f861e9..a4c0efa3a26e 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2246,6 +2246,7 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
{
struct bpf_prog_array *old_array;
struct bpf_prog_array *new_array;
+ struct bpf_prog *prog = NULL;
int ret;
mutex_lock(&bpf_event_mutex);
@@ -2266,18 +2267,22 @@ void perf_event_detach_bpf_prog(struct perf_event *event)
}
put:
- /*
- * It could be that the bpf_prog is not sleepable (and will be freed
- * via normal RCU), but is called from a point that supports sleepable
- * programs and uses tasks-trace-RCU.
- */
- synchronize_rcu_tasks_trace();
-
- bpf_prog_put(event->prog);
+ prog = event->prog;
event->prog = NULL;
unlock:
mutex_unlock(&bpf_event_mutex);
+
+ if (prog) {
+ /*
+ * It could be that the bpf_prog is not sleepable (and will be freed
+ * via normal RCU), but is called from a point that supports sleepable
+ * programs and uses tasks-trace-RCU.
+ */
+ synchronize_rcu_tasks_trace();
+
+ bpf_prog_put(prog);
+ }
}
int perf_event_query_prog_array(struct perf_event *event, void __user *info)
next prev parent reply other threads:[~2025-01-03 15:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-31 3:35 [PATCH bpf-next] bpf: Move out synchronize_rcu_tasks_trace from mutex CS Pu Lehui
2025-01-03 14:38 ` Jann Horn
2025-01-03 15:22 ` Jiri Olsa [this message]
2025-01-04 1:30 ` Pu Lehui
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=Z3gAxZTJTYngLnYi@krava \
--to=olsajiri@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=jannh@google.com \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=pulehui@huawei.com \
--cc=pulehui@huaweicloud.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.