From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
Viktor Malik <vmalik@redhat.com>
Subject: [PATCH RFCv2 bpf-next 1/4] bpf: Add support for kprobe multi wrapper attach
Date: Wed, 28 Feb 2024 10:02:39 +0100 [thread overview]
Message-ID: <20240228090242.4040210-2-jolsa@kernel.org> (raw)
In-Reply-To: <20240228090242.4040210-1-jolsa@kernel.org>
Adding support to attach bpf program for entry and return probe
of the same function. This is common usecase and at the moment
it requires to create two kprobe multi links.
Adding new attr.link_create.kprobe_multi.flags value that instructs
kernel to attach link program to both entry and exit probe.
It's possible to control execution of the bpf program on return
probe simply by returning zero or non zero from the entry bpf
program execution to execute or not respectively the bpf program
on return probe.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
include/uapi/linux/bpf.h | 3 ++-
kernel/trace/bpf_trace.c | 24 ++++++++++++++++++------
tools/include/uapi/linux/bpf.h | 3 ++-
3 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index d2e6c5fcec01..a430855c5bcd 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
enum {
- BPF_F_KPROBE_MULTI_RETURN = (1U << 0)
+ BPF_F_KPROBE_MULTI_RETURN = (1U << 0),
+ BPF_F_KPROBE_MULTI_WRAPPER = (1U << 1),
};
/* link_create.uprobe_multi.flags used in LINK_CREATE command for
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 241ddf5e3895..726a8c71f0da 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2587,6 +2587,7 @@ struct bpf_kprobe_multi_link {
u32 mods_cnt;
struct module **mods;
u32 flags;
+ bool is_wrapper;
};
struct bpf_kprobe_multi_run_ctx {
@@ -2826,10 +2827,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
void *data)
{
struct bpf_kprobe_multi_link *link;
+ int err;
link = container_of(fp, struct bpf_kprobe_multi_link, fp);
- kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
- return 0;
+ err = kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
+ return link->is_wrapper ? err : 0;
}
static void
@@ -2967,6 +2969,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
void __user *uaddrs;
u64 *cookies = NULL;
void __user *usyms;
+ bool is_wrapper;
int err;
/* no support for 32bit archs yet */
@@ -2977,9 +2980,12 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
return -EINVAL;
flags = attr->link_create.kprobe_multi.flags;
- if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
+ if (flags & ~(BPF_F_KPROBE_MULTI_RETURN|
+ BPF_F_KPROBE_MULTI_WRAPPER))
return -EINVAL;
+ is_wrapper = flags & BPF_F_KPROBE_MULTI_WRAPPER;
+
uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
if (!!uaddrs == !!usyms)
@@ -3054,15 +3060,21 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
if (err)
goto error;
- if (flags & BPF_F_KPROBE_MULTI_RETURN)
- link->fp.exit_handler = kprobe_multi_link_exit_handler;
- else
+ if (is_wrapper) {
link->fp.entry_handler = kprobe_multi_link_handler;
+ link->fp.exit_handler = kprobe_multi_link_exit_handler;
+ } else {
+ if (flags & BPF_F_KPROBE_MULTI_RETURN)
+ link->fp.exit_handler = kprobe_multi_link_exit_handler;
+ else
+ link->fp.entry_handler = kprobe_multi_link_handler;
+ }
link->addrs = addrs;
link->cookies = cookies;
link->cnt = cnt;
link->flags = flags;
+ link->is_wrapper = is_wrapper;
if (cookies) {
/*
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index d2e6c5fcec01..a430855c5bcd 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
*/
enum {
- BPF_F_KPROBE_MULTI_RETURN = (1U << 0)
+ BPF_F_KPROBE_MULTI_RETURN = (1U << 0),
+ BPF_F_KPROBE_MULTI_WRAPPER = (1U << 1),
};
/* link_create.uprobe_multi.flags used in LINK_CREATE command for
--
2.43.2
next prev parent reply other threads:[~2024-02-28 9:02 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-28 9:02 [PATCH RFCv2 bpf-next 0/4] bpf: Introduce kprobe multi wrapper attach Jiri Olsa
2024-02-28 9:02 ` Jiri Olsa [this message]
2024-02-29 1:23 ` [PATCH RFCv2 bpf-next 1/4] bpf: Add support for " Andrii Nakryiko
2024-02-29 10:20 ` Jiri Olsa
2024-02-28 9:02 ` [PATCH RFCv2 bpf-next 2/4] bpf: Add bpf_kprobe_multi_is_return kfunc Jiri Olsa
2024-02-29 1:23 ` Andrii Nakryiko
2024-02-29 10:16 ` Jiri Olsa
2024-03-01 18:01 ` Andrii Nakryiko
2024-03-04 8:28 ` Jiri Olsa
2024-02-28 9:02 ` [PATCH RFCv2 bpf-next 3/4] libbpf: Add support for kprobe multi wrapper attach Jiri Olsa
2024-02-29 1:23 ` Andrii Nakryiko
2024-02-29 10:24 ` Jiri Olsa
2024-02-28 9:02 ` [PATCH RFCv2 bpf-next 4/4] selftests/bpf: Add kprobe multi wrapper test Jiri Olsa
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=20240228090242.4040210-2-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=mhiramat@kernel.org \
--cc=sdf@google.com \
--cc=songliubraving@fb.com \
--cc=vmalik@redhat.com \
--cc=yhs@fb.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