From: Kui-Feng Lee <kuifeng@fb.com>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
<andrii@kernel.org>
Cc: Kui-Feng Lee <kuifeng@fb.com>
Subject: [PATCH bpf-next 5/5] bpf: Implement bpf_get_attach_cookie() for tracing programs.
Date: Wed, 26 Jan 2022 13:48:09 -0800 [thread overview]
Message-ID: <20220126214809.3868787-6-kuifeng@fb.com> (raw)
In-Reply-To: <20220126214809.3868787-1-kuifeng@fb.com>
Implement bpf_get_attach_cookie() for fentry, fexit, and fmod_ret
tracing programs. The cookie is from the bpf_prog from the program ID
of the current BPF program.
Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
---
kernel/trace/bpf_trace.c | 22 +++++++
.../selftests/bpf/prog_tests/bpf_cookie.c | 57 +++++++++++++++++++
.../selftests/bpf/progs/test_bpf_cookie.c | 24 ++++++++
3 files changed, 103 insertions(+)
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index bf2c9d11ad05..7b1f4c8a10de 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1084,6 +1084,26 @@ static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
.arg1_type = ARG_PTR_TO_CTX,
};
+BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
+{
+ const struct bpf_prog *prog;
+ int off = get_trampo_var_off(ctx, BPF_TRAMP_F_PROG_ID);
+
+ if (off < 0)
+ return 0;
+
+ prog = (const struct bpf_prog *)((u64 *)ctx)[-off];
+
+ return prog->aux->cookie;
+}
+
+static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
+ .func = bpf_get_attach_cookie_tracing,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+};
+
BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
{
#ifndef CONFIG_X86
@@ -1706,6 +1726,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
case BPF_FUNC_get_func_arg_cnt:
return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
+ case BPF_FUNC_get_attach_cookie:
+ return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
default:
fn = raw_tp_prog_func_proto(func_id, prog);
if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 5eea3c3a40fe..41a73c0a3a8e 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -231,6 +231,61 @@ static void pe_subtest(struct test_bpf_cookie *skel)
bpf_link__destroy(link);
}
+static void tracing_subtest(struct test_bpf_cookie *skel)
+{
+ __u64 cookie;
+ __u32 duration = 0, retval;
+ int prog_fd;
+ int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1;
+
+ skel->bss->fentry_res = 0;
+ skel->bss->fexit_res = 0;
+
+ cookie = 0x100000;
+ prog_fd = bpf_program__fd(skel->progs.fentry_test1);
+ if (!ASSERT_GE(prog_fd, 0, "fentry.prog_fd"))
+ return;
+ fentry_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fentry_fd, 0, "fentry.open"))
+ return;
+
+ cookie = 0x200000;
+ prog_fd = bpf_program__fd(skel->progs.fexit_test1);
+ if (!ASSERT_GE(prog_fd, 0, "fexit.prog_fd"))
+ goto cleanup;
+ fexit_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fexit_fd, 0, "fexit.open"))
+ goto cleanup;
+
+ cookie = 0x300000;
+ prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
+ if (!ASSERT_GE(prog_fd, 0, "fmod_ret.prog_fd"))
+ goto cleanup;
+ fmod_ret_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fmod_ret_fd, 0, "fmod_ret.opoen"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.fentry_test1);
+ bpf_prog_test_run(prog_fd, 1, NULL, 0,
+ NULL, NULL, &retval, &duration);
+
+ prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
+ bpf_prog_test_run(prog_fd, 1, NULL, 0,
+ NULL, NULL, &retval, &duration);
+
+ ASSERT_EQ(skel->bss->fentry_res, 0x100000, "fentry_res");
+ ASSERT_EQ(skel->bss->fexit_res, 0x200000, "fexit_res");
+ ASSERT_EQ(skel->bss->fmod_ret_res, 0x300000, "fmod_ret_res");
+
+cleanup:
+ if (fentry_fd >= 0)
+ close(fentry_fd);
+ if (fexit_fd >= 0)
+ close(fexit_fd);
+ if (fmod_ret_fd >= 0)
+ close(fmod_ret_fd);
+}
+
void test_bpf_cookie(void)
{
struct test_bpf_cookie *skel;
@@ -249,6 +304,8 @@ void test_bpf_cookie(void)
tp_subtest(skel);
if (test__start_subtest("perf_event"))
pe_subtest(skel);
+ if (test__start_subtest("tracing"))
+ tracing_subtest(skel);
test_bpf_cookie__destroy(skel);
}
diff --git a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
index 2d3a7710e2ce..a9f83f46e7b7 100644
--- a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
+++ b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
@@ -14,6 +14,9 @@ int uprobe_res;
int uretprobe_res;
int tp_res;
int pe_res;
+int fentry_res;
+int fexit_res;
+int fmod_ret_res;
static void update(void *ctx, int *res)
{
@@ -82,4 +85,25 @@ int handle_pe(struct pt_regs *ctx)
return 0;
}
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(fentry_test1, int a)
+{
+ update(ctx, &fentry_res);
+ return 0;
+}
+
+SEC("fexit/bpf_fentry_test1")
+int BPF_PROG(fexit_test1, int a, int ret)
+{
+ update(ctx, &fexit_res);
+ return 0;
+}
+
+SEC("fmod_ret/bpf_modify_return_test")
+int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
+{
+ update(ctx, &fmod_ret_res);
+ return 1234;
+}
+
char _license[] SEC("license") = "GPL";
--
2.30.2
next prev parent reply other threads:[~2022-01-26 21:48 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-26 21:48 [PATCH bpf-next 0/5] Attach a cookie to a tracing program Kui-Feng Lee
2022-01-26 21:48 ` [PATCH bpf-next 1/5] bpf: Add a flags value on trampoline frames Kui-Feng Lee
2022-01-26 21:48 ` [PATCH bpf-next 2/5] bpf: Detect if a program needs its program ID Kui-Feng Lee
2022-01-26 21:48 ` [PATCH bpf-next 3/5] bpf, x86: Store program ID to trampoline frames Kui-Feng Lee
2022-01-26 21:48 ` [PATCH bpf-next 4/5] bpf: Attach a cookie to a BPF program Kui-Feng Lee
2022-02-01 6:46 ` Andrii Nakryiko
2022-02-01 20:17 ` Alexei Starovoitov
2022-02-02 1:24 ` Andrii Nakryiko
2022-01-26 21:48 ` Kui-Feng Lee [this message]
2022-01-26 23:38 ` [PATCH bpf-next 5/5] bpf: Implement bpf_get_attach_cookie() for tracing programs kernel test robot
2022-01-26 23:38 ` kernel test robot
2022-01-27 5:17 ` [PATCH bpf-next 0/5] Attach a cookie to a tracing program Alexei Starovoitov
2022-01-31 16:56 ` Jiri Olsa
2022-02-01 6:45 ` Andrii Nakryiko
2022-02-01 17:37 ` Kui-Feng Lee
2022-02-02 1:06 ` Andrii Nakryiko
2022-02-01 19:32 ` Alexei Starovoitov
2022-02-02 1:15 ` 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=20220126214809.3868787-6-kuifeng@fb.com \
--to=kuifeng@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
/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.