From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andriin@fb.com>
Cc: netdev@vger.kernel.org, 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>, "Daniel Xu" <dxu@dxuuu.xyz>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Jesper Brouer" <jbrouer@redhat.com>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
"Viktor Malik" <vmalik@redhat.com>
Subject: [PATCHv2 RFC bpf-next 5/7] libbpf: Add support to load and attach ftrace probe
Date: Tue, 13 Apr 2021 14:15:14 +0200 [thread overview]
Message-ID: <20210413121516.1467989-6-jolsa@kernel.org> (raw)
In-Reply-To: <20210413121516.1467989-1-jolsa@kernel.org>
Adding support to load and attach ftrace probe.
Adding new section type 'fentry.ftrace', that identifies
ftrace probe and assigns BPF_TRACE_FTRACE_ENTRY to prog's
expected_attach_type.
The attach function creates bpf_functions object and
makes an ftrace link with the program.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/lib/bpf/bpf.c | 12 +++++++
tools/lib/bpf/bpf.h | 5 ++-
tools/lib/bpf/libbpf.c | 74 ++++++++++++++++++++++++++++++++++++++++
tools/lib/bpf/libbpf.map | 1 +
4 files changed, 91 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index bba48ff4c5c0..b3195ac3e32e 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -643,6 +643,7 @@ int bpf_link_create(int prog_fd, int target_fd,
attr.link_create.target_fd = target_fd;
attr.link_create.attach_type = attach_type;
attr.link_create.flags = OPTS_GET(opts, flags, 0);
+ attr.link_create.funcs_fd = OPTS_GET(opts, funcs_fd, 0);
if (iter_info_len) {
attr.link_create.iter_info =
@@ -971,3 +972,14 @@ int bpf_prog_bind_map(int prog_fd, int map_fd,
return sys_bpf(BPF_PROG_BIND_MAP, &attr, sizeof(attr));
}
+
+int bpf_functions_add(int fd, int btf_id)
+{
+ union bpf_attr attr;
+
+ memset(&attr, 0, sizeof(attr));
+ attr.functions_add.fd = fd;
+ attr.functions_add.btf_id = btf_id;
+
+ return sys_bpf(BPF_FUNCTIONS_ADD, &attr, sizeof(attr));
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 875dde20d56e..f677fe06262b 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -175,8 +175,9 @@ struct bpf_link_create_opts {
union bpf_iter_link_info *iter_info;
__u32 iter_info_len;
__u32 target_btf_id;
+ __u32 funcs_fd;
};
-#define bpf_link_create_opts__last_field target_btf_id
+#define bpf_link_create_opts__last_field funcs_fd
LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
enum bpf_attach_type attach_type,
@@ -278,6 +279,8 @@ struct bpf_test_run_opts {
LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
struct bpf_test_run_opts *opts);
+LIBBPF_API int bpf_functions_add(int fd, int btf_id);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ed5586cce227..b3cb43990524 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8838,6 +8838,10 @@ static const struct bpf_sec_def section_defs[] = {
.expected_attach_type = BPF_TRACE_ITER,
.is_attach_btf = true,
.attach_fn = attach_iter),
+ SEC_DEF("fentry.ftrace/", TRACING,
+ .expected_attach_type = BPF_TRACE_FTRACE_ENTRY,
+ .is_attach_btf = true,
+ .attach_fn = attach_trace),
BPF_EAPROG_SEC("xdp_devmap/", BPF_PROG_TYPE_XDP,
BPF_XDP_DEVMAP),
BPF_EAPROG_SEC("xdp_cpumap/", BPF_PROG_TYPE_XDP,
@@ -9125,6 +9129,7 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
#define BTF_TRACE_PREFIX "btf_trace_"
#define BTF_LSM_PREFIX "bpf_lsm_"
#define BTF_ITER_PREFIX "bpf_iter_"
+#define BTF_FTRACE_PROBE "bpf_ftrace_probe"
#define BTF_MAX_NAME_SIZE 128
static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
@@ -9158,6 +9163,9 @@ static inline int find_attach_btf_id(struct btf *btf, const char *name,
else if (attach_type == BPF_TRACE_ITER)
err = find_btf_by_prefix_kind(btf, BTF_ITER_PREFIX, name,
BTF_KIND_FUNC);
+ else if (attach_type == BPF_TRACE_FTRACE_ENTRY)
+ err = btf__find_by_name_kind(btf, BTF_FTRACE_PROBE,
+ BTF_KIND_FUNC);
else
err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
@@ -10191,8 +10199,74 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
return (struct bpf_link *)link;
}
+static struct bpf_link *bpf_program__attach_ftrace(struct bpf_program *prog)
+{
+ char *pattern = prog->sec_name + prog->sec_def->len;
+ DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
+ int prog_fd, link_fd, cnt, err, i;
+ enum bpf_attach_type attach_type;
+ struct bpf_link *link = NULL;
+ __s32 *ids = NULL;
+ int funcs_fd = -1;
+
+ prog_fd = bpf_program__fd(prog);
+ if (prog_fd < 0) {
+ pr_warn("prog '%s': can't attach before loaded\n", prog->name);
+ return ERR_PTR(-EINVAL);
+ }
+
+ err = bpf_object__load_vmlinux_btf(prog->obj, true);
+ if (err)
+ return ERR_PTR(err);
+
+ cnt = btf__find_by_pattern_kind(prog->obj->btf_vmlinux, pattern,
+ BTF_KIND_FUNC, &ids);
+ if (cnt <= 0)
+ return ERR_PTR(-EINVAL);
+
+ for (i = 0; i < cnt; i++) {
+ err = bpf_functions_add(funcs_fd, ids[i]);
+ if (err < 0) {
+ pr_warn("prog '%s': can't attach function BTF ID %d\n",
+ prog->name, ids[i]);
+ goto out_err;
+ }
+ if (funcs_fd == -1)
+ funcs_fd = err;
+ }
+
+ link = calloc(1, sizeof(*link));
+ if (!link) {
+ err = -ENOMEM;
+ goto out_err;
+ }
+ link->detach = &bpf_link__detach_fd;
+
+ opts.funcs_fd = funcs_fd;
+
+ attach_type = bpf_program__get_expected_attach_type(prog);
+ link_fd = bpf_link_create(prog_fd, 0, attach_type, &opts);
+ if (link_fd < 0) {
+ err = -errno;
+ goto out_err;
+ }
+ link->fd = link_fd;
+ free(ids);
+ return link;
+
+out_err:
+ if (funcs_fd != -1)
+ close(funcs_fd);
+ free(link);
+ free(ids);
+ return ERR_PTR(err);
+}
+
struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)
{
+ if (prog->expected_attach_type == BPF_TRACE_FTRACE_ENTRY)
+ return bpf_program__attach_ftrace(prog);
+
return bpf_program__attach_btf_id(prog);
}
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index b9b29baf1df8..69cbe54125e3 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -355,6 +355,7 @@ LIBBPF_0.4.0 {
global:
btf__add_float;
btf__add_type;
+ bpf_functions_add;
bpf_linker__add_file;
bpf_linker__finalize;
bpf_linker__free;
--
2.30.2
next prev parent reply other threads:[~2021-04-13 12:16 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-13 12:15 [PATCHv2 RFC bpf-next 0/7] bpf: Add support for ftrace probe Jiri Olsa
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 1/7] bpf: Move bpf_prog_start/end functions to generic place Jiri Olsa
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 2/7] bpf: Add bpf_functions object Jiri Olsa
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 3/7] bpf: Add support to attach program to ftrace probe Jiri Olsa
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 4/7] libbpf: Add btf__find_by_pattern_kind function Jiri Olsa
2021-04-13 12:15 ` Jiri Olsa [this message]
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 6/7] selftests/bpf: Add ftrace probe to fentry test Jiri Olsa
2021-04-13 12:15 ` [PATCHv2 RFC bpf-next 7/7] selftests/bpf: Add ftrace probe test Jiri Olsa
2021-04-14 1:04 ` [PATCHv2 RFC bpf-next 0/7] bpf: Add support for ftrace probe Andrii Nakryiko
2021-04-14 12:19 ` Jiri Olsa
2021-04-14 22:46 ` Andrii Nakryiko
2021-04-15 14:00 ` Jiri Olsa
2021-04-15 15:10 ` Steven Rostedt
2021-04-15 17:39 ` Jiri Olsa
2021-04-15 18:18 ` Steven Rostedt
2021-04-15 18:21 ` Steven Rostedt
2021-04-15 21:49 ` Jiri Olsa
2021-04-15 23:30 ` Steven Rostedt
2021-04-19 20:51 ` Jiri Olsa
2021-04-19 22:00 ` Steven Rostedt
2021-04-15 18:31 ` Yonghong Song
2021-04-15 20:45 ` Andrii Nakryiko
2021-04-15 21:00 ` Steven Rostedt
2021-04-16 15:03 ` Masami Hiramatsu
2021-04-16 16:48 ` Steven Rostedt
2021-04-19 14:29 ` Masami Hiramatsu
2021-04-20 12:51 ` Jiri Olsa
2021-04-20 15:33 ` Alexei Starovoitov
2021-04-20 16:33 ` Steven Rostedt
2021-04-20 16:52 ` Jiri Olsa
2021-04-20 23:38 ` Alexei Starovoitov
2021-04-21 13:40 ` Jiri Olsa
2021-04-21 14:05 ` Steven Rostedt
2021-04-21 18:52 ` Andrii Nakryiko
2021-04-21 19:18 ` Jiri Olsa
2021-04-22 14:24 ` Steven Rostedt
2021-04-21 21:37 ` Jiri Olsa
2021-04-22 1:17 ` Steven Rostedt
2021-04-20 4:51 ` Andrii Nakryiko
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=20210413121516.1467989-6-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dxu@dxuuu.xyz \
--cc=jbrouer@redhat.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=netdev@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=songliubraving@fb.com \
--cc=toke@redhat.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;
as well as URLs for NNTP newsgroup(s).