Skip to content

Commit 856c02d

Browse files
liu-song-6Alexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Introduce helper bpf_get_branch_snapshot
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c22ac2a commit 856c02d

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,6 +4877,27 @@ union bpf_attr {
48774877
* Get the struct pt_regs associated with **task**.
48784878
* Return
48794879
* A pointer to struct pt_regs.
4880+
*
4881+
* long bpf_get_branch_snapshot(void *entries, u32 size, u64 flags)
4882+
* Description
4883+
* Get branch trace from hardware engines like Intel LBR. The
4884+
* hardware engine is stopped shortly after the helper is
4885+
* called. Therefore, the user need to filter branch entries
4886+
* based on the actual use case. To capture branch trace
4887+
* before the trigger point of the BPF program, the helper
4888+
* should be called at the beginning of the BPF program.
4889+
*
4890+
* The data is stored as struct perf_branch_entry into output
4891+
* buffer *entries*. *size* is the size of *entries* in bytes.
4892+
* *flags* is reserved for now and must be zero.
4893+
*
4894+
* Return
4895+
* On success, number of bytes written to *buf*. On error, a
4896+
* negative value.
4897+
*
4898+
* **-EINVAL** if *flags* is not zero.
4899+
*
4900+
* **-ENOENT** if architecture does not support branch records.
48804901
*/
48814902
#define __BPF_FUNC_MAPPER(FN) \
48824903
FN(unspec), \
@@ -5055,6 +5076,7 @@ union bpf_attr {
50555076
FN(get_func_ip), \
50565077
FN(get_attach_cookie), \
50575078
FN(task_pt_regs), \
5079+
FN(get_branch_snapshot), \
50585080
/* */
50595081

50605082
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/trampoline.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/rcupdate_trace.h>
1111
#include <linux/rcupdate_wait.h>
1212
#include <linux/module.h>
13+
#include <linux/static_call.h>
1314

1415
/* dummy _ops. The verifier will operate on target program's ops. */
1516
const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -526,7 +527,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
526527
}
527528

528529
#define NO_START_TIME 1
529-
static u64 notrace bpf_prog_start_time(void)
530+
static __always_inline u64 notrace bpf_prog_start_time(void)
530531
{
531532
u64 start = NO_START_TIME;
532533

kernel/trace/bpf_trace.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,34 @@ static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
10171017
.arg1_type = ARG_PTR_TO_CTX,
10181018
};
10191019

1020+
BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1021+
{
1022+
#ifndef CONFIG_X86
1023+
return -ENOENT;
1024+
#else
1025+
static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1026+
u32 entry_cnt = size / br_entry_size;
1027+
1028+
entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1029+
1030+
if (unlikely(flags))
1031+
return -EINVAL;
1032+
1033+
if (!entry_cnt)
1034+
return -ENOENT;
1035+
1036+
return entry_cnt * br_entry_size;
1037+
#endif
1038+
}
1039+
1040+
static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1041+
.func = bpf_get_branch_snapshot,
1042+
.gpl_only = true,
1043+
.ret_type = RET_INTEGER,
1044+
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
1045+
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
1046+
};
1047+
10201048
static const struct bpf_func_proto *
10211049
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10221050
{
@@ -1132,6 +1160,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11321160
return &bpf_snprintf_proto;
11331161
case BPF_FUNC_get_func_ip:
11341162
return &bpf_get_func_ip_proto_tracing;
1163+
case BPF_FUNC_get_branch_snapshot:
1164+
return &bpf_get_branch_snapshot_proto;
11351165
default:
11361166
return bpf_base_func_proto(func_id);
11371167
}

tools/include/uapi/linux/bpf.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,6 +4877,27 @@ union bpf_attr {
48774877
* Get the struct pt_regs associated with **task**.
48784878
* Return
48794879
* A pointer to struct pt_regs.
4880+
*
4881+
* long bpf_get_branch_snapshot(void *entries, u32 size, u64 flags)
4882+
* Description
4883+
* Get branch trace from hardware engines like Intel LBR. The
4884+
* hardware engine is stopped shortly after the helper is
4885+
* called. Therefore, the user need to filter branch entries
4886+
* based on the actual use case. To capture branch trace
4887+
* before the trigger point of the BPF program, the helper
4888+
* should be called at the beginning of the BPF program.
4889+
*
4890+
* The data is stored as struct perf_branch_entry into output
4891+
* buffer *entries*. *size* is the size of *entries* in bytes.
4892+
* *flags* is reserved for now and must be zero.
4893+
*
4894+
* Return
4895+
* On success, number of bytes written to *buf*. On error, a
4896+
* negative value.
4897+
*
4898+
* **-EINVAL** if *flags* is not zero.
4899+
*
4900+
* **-ENOENT** if architecture does not support branch records.
48804901
*/
48814902
#define __BPF_FUNC_MAPPER(FN) \
48824903
FN(unspec), \
@@ -5055,6 +5076,7 @@ union bpf_attr {
50555076
FN(get_func_ip), \
50565077
FN(get_attach_cookie), \
50575078
FN(task_pt_regs), \
5079+
FN(get_branch_snapshot), \
50585080
/* */
50595081

50605082
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)