From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Song Liu <songliubraving@fb.com>
Cc: bpf <bpf@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Peter Ziljstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Kajol Jain <kjain@linux.ibm.com>,
Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v3 bpf-next 2/3] bpf: introduce helper bpf_get_branch_snapshot
Date: Mon, 30 Aug 2021 15:14:55 -0700 [thread overview]
Message-ID: <CAEf4BzYuUTndYGb7-4q9=8s4PQUbTK+VHy=u9vOBqabZ08Yy-w@mail.gmail.com> (raw)
In-Reply-To: <20210830214106.4142056-3-songliubraving@fb.com>
On Mon, Aug 30, 2021 at 2:42 PM Song Liu <songliubraving@fb.com> wrote:
>
> Introduce bpf_get_branch_snapshot(), which allows tracing pogram to get
> branch trace from hardware (e.g. Intel LBR). To use the feature, the
> user need to create perf_event with proper branch_record filtering
> on each cpu, and then calls bpf_get_branch_snapshot in the bpf function.
> On Intel CPUs, VLBR event (raw event 0x1b00) can be use for this.
>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
> include/linux/bpf.h | 2 ++
> include/linux/filter.h | 3 ++-
> include/uapi/linux/bpf.h | 16 +++++++++++++
> kernel/bpf/trampoline.c | 13 ++++++++++
> kernel/bpf/verifier.c | 12 ++++++++++
> kernel/trace/bpf_trace.c | 43 ++++++++++++++++++++++++++++++++++
> tools/include/uapi/linux/bpf.h | 16 +++++++++++++
> 7 files changed, 104 insertions(+), 1 deletion(-)
>
[...]
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 206c221453cfa..72e8b49da0bf9 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -6446,6 +6446,18 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
> env->prog->call_get_func_ip = true;
> }
>
> + if (func_id == BPF_FUNC_get_branch_snapshot) {
> + if (env->prog->aux->sleepable) {
> + verbose(env, "sleepable progs cannot call get_branch_snapshot\n");
> + return -ENOTSUPP;
> + }
> + if (!IS_ENABLED(CONFIG_PERF_EVENTS)) {
> + verbose(env, "func %s#%d not supported without CONFIG_PERF_EVENTS\n",
> + func_id_name(func_id), func_id);
> + return -ENOTSUPP;
> + }
> + env->prog->call_get_branch = true;
> + }
> if (changes_data)
> clear_all_pkt_pointers(env);
> return 0;
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 8e2eb950aa829..a01f26b7877e6 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1017,6 +1017,33 @@ static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
> .arg1_type = ARG_PTR_TO_CTX,
> };
>
> +BPF_CALL_2(bpf_get_branch_snapshot, void *, buf, u32, size)
I bet we'll need u64 flags over time, let's add it right now. It's
similar to bpf_read_branch_records().
> +{
> +#ifdef CONFIG_PERF_EVENTS
> + u32 max_size;
> +
> + if (this_cpu_ptr(&bpf_perf_branch_snapshot)->nr == 0)
> + return -EOPNOTSUPP;
> +
> + max_size = this_cpu_ptr(&bpf_perf_branch_snapshot)->nr *
> + sizeof(struct perf_branch_entry);
> + memcpy(buf, this_cpu_ptr(&bpf_perf_branch_snapshot)->entries,
> + min_t(u32, size, max_size));
> +
Check bpf_read_branch_records() implementation and it's argument
validation logic. Let's keep them consistent (e.g., it enforces that
size is a multiple of sizeof(struct perf_branch_entry)). Another
difference is that bpf_read_branch_records() returns number of bytes
filled, not number of records. That's consistent with accepting size
as number of bytes. Let's stick to this convention then, so bytes
everywhere.
> + return this_cpu_ptr(&bpf_perf_branch_snapshot)->nr;
> +#else
> + return -EOPNOTSUPP;
> +#endif
> +}
> +
> +static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
> + .func = bpf_get_branch_snapshot,
> + .gpl_only = true,
> + .ret_type = RET_INTEGER,
> + .arg1_type = ARG_PTR_TO_UNINIT_MEM,
> + .arg2_type = ARG_CONST_SIZE_OR_ZERO,
> +};
> +
[...]
next prev parent reply other threads:[~2021-08-30 22:15 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-30 21:41 [PATCH v3 bpf-next 0/3] bpf: introduce bpf_get_branch_snapshot Song Liu
2021-08-30 21:41 ` [PATCH v3 bpf-next 1/3] perf: enable branch record for software events Song Liu
2021-08-30 22:05 ` Andrii Nakryiko
2021-08-31 15:24 ` Peter Zijlstra
2021-08-31 16:12 ` Song Liu
2021-08-30 21:41 ` [PATCH v3 bpf-next 2/3] bpf: introduce helper bpf_get_branch_snapshot Song Liu
2021-08-30 22:14 ` Andrii Nakryiko [this message]
2021-08-31 11:16 ` kernel test robot
2021-08-31 15:32 ` Peter Zijlstra
2021-08-31 16:41 ` Song Liu
2021-08-31 21:24 ` Song Liu
2021-08-31 21:37 ` Alexei Starovoitov
2021-08-30 21:41 ` [PATCH v3 bpf-next 3/3] selftests/bpf: add test for bpf_get_branch_snapshot Song Liu
2021-08-30 22:28 ` 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='CAEf4BzYuUTndYGb7-4q9=8s4PQUbTK+VHy=u9vOBqabZ08Yy-w@mail.gmail.com' \
--to=andrii.nakryiko@gmail.com \
--cc=acme@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@fb.com \
--cc=kjain@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=songliubraving@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).