Skip to content

Commit fff7b64

Browse files
danobiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_read_branch_records() helper
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 2f14b2d commit fff7b64

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

Diff for: include/uapi/linux/bpf.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,25 @@ union bpf_attr {
28922892
* Obtain the 64bit jiffies
28932893
* Return
28942894
* The 64 bit jiffies
2895+
*
2896+
* int bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags)
2897+
* Description
2898+
* For an eBPF program attached to a perf event, retrieve the
2899+
* branch records (struct perf_branch_entry) associated to *ctx*
2900+
* and store it in the buffer pointed by *buf* up to size
2901+
* *size* bytes.
2902+
* Return
2903+
* On success, number of bytes written to *buf*. On error, a
2904+
* negative value.
2905+
*
2906+
* The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to
2907+
* instead return the number of bytes required to store all the
2908+
* branch entries. If this flag is set, *buf* may be NULL.
2909+
*
2910+
* **-EINVAL** if arguments invalid or **size** not a multiple
2911+
* of sizeof(struct perf_branch_entry).
2912+
*
2913+
* **-ENOENT** if architecture does not support branch records.
28952914
*/
28962915
#define __BPF_FUNC_MAPPER(FN) \
28972916
FN(unspec), \
@@ -3012,7 +3031,8 @@ union bpf_attr {
30123031
FN(probe_read_kernel_str), \
30133032
FN(tcp_send_ack), \
30143033
FN(send_signal_thread), \
3015-
FN(jiffies64),
3034+
FN(jiffies64), \
3035+
FN(read_branch_records),
30163036

30173037
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30183038
* function eBPF program intends to call
@@ -3091,6 +3111,9 @@ enum bpf_func_id {
30913111
/* BPF_FUNC_sk_storage_get flags */
30923112
#define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0)
30933113

3114+
/* BPF_FUNC_read_branch_records flags. */
3115+
#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0)
3116+
30943117
/* Mode for BPF_FUNC_skb_adjust_room helper. */
30953118
enum bpf_adj_room_mode {
30963119
BPF_ADJ_ROOM_NET,

Diff for: kernel/trace/bpf_trace.c

+41
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,45 @@ static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
10281028
.arg3_type = ARG_CONST_SIZE,
10291029
};
10301030

1031+
BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1032+
void *, buf, u32, size, u64, flags)
1033+
{
1034+
#ifndef CONFIG_X86
1035+
return -ENOENT;
1036+
#else
1037+
static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1038+
struct perf_branch_stack *br_stack = ctx->data->br_stack;
1039+
u32 to_copy;
1040+
1041+
if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1042+
return -EINVAL;
1043+
1044+
if (unlikely(!br_stack))
1045+
return -EINVAL;
1046+
1047+
if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1048+
return br_stack->nr * br_entry_size;
1049+
1050+
if (!buf || (size % br_entry_size != 0))
1051+
return -EINVAL;
1052+
1053+
to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1054+
memcpy(buf, br_stack->entries, to_copy);
1055+
1056+
return to_copy;
1057+
#endif
1058+
}
1059+
1060+
static const struct bpf_func_proto bpf_read_branch_records_proto = {
1061+
.func = bpf_read_branch_records,
1062+
.gpl_only = true,
1063+
.ret_type = RET_INTEGER,
1064+
.arg1_type = ARG_PTR_TO_CTX,
1065+
.arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1066+
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
1067+
.arg4_type = ARG_ANYTHING,
1068+
};
1069+
10311070
static const struct bpf_func_proto *
10321071
pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10331072
{
@@ -1040,6 +1079,8 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10401079
return &bpf_get_stack_proto_tp;
10411080
case BPF_FUNC_perf_prog_read_value:
10421081
return &bpf_perf_prog_read_value_proto;
1082+
case BPF_FUNC_read_branch_records:
1083+
return &bpf_read_branch_records_proto;
10431084
default:
10441085
return tracing_func_proto(func_id, prog);
10451086
}

0 commit comments

Comments
 (0)