Re: [PATCH v7 bpf-next RESEND 1/2] bpf: Add bpf_read_branch_records() helper

From: Andrii Nakryiko
Date: Tue Feb 11 2020 - 14:23:16 EST


On Mon, Feb 10, 2020 at 12:09 PM Daniel Xu <dxu@xxxxxxxxx> wrote:
>
> Branch records are a CPU feature that can be configured to record
> certain branches that are taken during code execution. This data is
> particularly interesting for profile guided optimizations. perf has had
> branch record support for a while but the data collection can be a bit
> coarse grained.
>
> We (Facebook) have seen in experiments that associating metadata with
> branch records can improve results (after postprocessing). We generally
> use bpf_probe_read_*() to get metadata out of userspace. That's why bpf
> support for branch records is useful.
>
> Aside from this particular use case, having branch data available to bpf
> progs can be useful to get stack traces out of userspace applications
> that omit frame pointers.
>
> Signed-off-by: Daniel Xu <dxu@xxxxxxxxx>
> ---

LGTM, one typo in description of the helper. bpf-next is still closed,
btw, but should hopefully open soon.

Acked-by: Andrii Nakryiko <andriin@xxxxxx>

> include/uapi/linux/bpf.h | 25 +++++++++++++++++++++++-
> kernel/trace/bpf_trace.c | 41 ++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 65 insertions(+), 1 deletion(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index f1d74a2bd234..3004470b7269 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -2892,6 +2892,25 @@ union bpf_attr {
> * Obtain the 64bit jiffies
> * Return
> * The 64 bit jiffies
> + *
> + * int bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags)
> + * Description
> + * For an eBPF program attached to a perf event, retrieve the
> + * branch records (struct perf_branch_entry) associated to *ctx*
> + * and store it in the buffer pointed by *buf* up to size
> + * *buf_size* bytes.
> + * Return
> + * On success, number of bytes written to *buf*. On error, a
> + * negative value.
> + *
> + * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to
> + * instead return the number of bytes required to store all the
> + * branch entries. If this flag is set, *buf* may be NULL.
> + *
> + * **-EINVAL** if arguments invalid or **buf_size** not a multiple

buf_size -> size

> + * of sizeof(struct perf_branch_entry).
> + *
> + * **-ENOENT** if architecture does not support branch records.
> */

[...]