Add bpf_read_branch_records() helper
Subsystems (6)
v8 cover 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.
Changes in v8:
- Use globals instead of perf buffer
- Call test_perf_branches__detach() before destroying skeleton
- Fix typo in docs
Changes in v7:
- Const-ify and static-ify local var
- Documentation formatting
Changes in v6:
- Move #ifdef a little to avoid unused variable warnings on !x86
- Test negative condition in selftest (-EINVAL on improperly configured perf event)
- Skip positive condition selftest on setups that don't support branch records
Changes in v5:
- Rename bpf_perf_prog_read_branches() -> bpf_read_branch_records()
- Rename BPF_F_GET_BR_SIZE -> BPF_F_GET_BRANCH_RECORDS_SIZE
- Squash tools/ bpf.h sync into selftest commit
Changes in v4:
- Add BPF_F_GET_BR_SIZE flag
- Return -ENOENT on unsupported architectures
- Only accept initialized memory in helper
- Check buffer size is multiple of sizeof(struct perf_branch_entry)
- Use bpf skeleton in selftest
- Add commit messages
- Spelling and formatting
Changes in v3:
- Document filling unused buffer with zero
- Formatting fixes
- Rebase
Changes in v2:
- Change to a bpf helper instead of context access
- Avoid mentioning Intel specific things
Daniel Xu (2):
bpf: Add bpf_read_branch_records() helper selftests/bpf: add bpf_read_branch_records() selftest
include/uapi/linux/bpf.h | 25 ++- kernel/trace/bpf_trace.c | 41 +++++ tools/include/uapi/linux/bpf.h | 25 ++- .../selftests/bpf/prog_tests/perf_branches.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_branches.c | 50 ++++++ 5 files changed, 308 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_branches.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_branches.c
Signature · 1 line
2.21.1
PATCH v8 1/2 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.
--- include/uapi/linux/bpf.h | 25 +++++++++++++++++++++++- kernel/trace/bpf_trace.c | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +24 -1
@@ -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 +* *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 **size** not a multiple +* of sizeof(struct perf_branch_entry). +* +* **-ENOENT** if architecture does not support branch records. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3012,7 +3031,8 @@ union bpf_attr {FN (probe_read_kernel_str), \FN (tcp_send_ack), \FN (send_signal_thread), \ -FN (jiffies64), +FN (jiffies64), \ +FN (read_branch_records),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3091,6 +3111,9 @@ enum bpf_func_id {/* BPF_FUNC_sk_storage_get flags */ #define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0) +/* BPF_FUNC_read_branch_records flags. */ +#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0) +/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET ,
kernel/trace/bpf_trace.c +41 -0
53 lines not on this page · raw message
Signature · 1 line
2.21.1
PATCH v8 2/2 selftests/bpf: add bpf_read_branch_records() selftest
In reply to Daniel XuAdd a selftest to test:
* default bpf_read_branch_records() behavior
* BPF_F_GET_BRANCH_RECORDS_SIZE flag behavior
* error path on non branch record perf events
* using helper to write to stack
* using helper to write to global
On host with hardware counter support:
# ./test_progs -t perf_branches
#27/1 perf_branches_hw:OK
#27/2 perf_branches_no_hw:OK
#27 perf_branches:OK
Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILEDOn host without hardware counter support (VM):
# ./test_progs -t perf_branches
#27/1 perf_branches_hw:OK
#27/2 perf_branches_no_hw:OK
#27 perf_branches:OK
Summary: 1/2 PASSED, 1 SKIPPED, 0 FAILEDAlso sync tools/include/uapi/linux/bpf.h.
--- tools/include/uapi/linux/bpf.h | 25 ++- .../selftests/bpf/prog_tests/perf_branches.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_branches.c | 50 ++++++ 3 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_branches.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_branches.c
tools/include/uapi/linux/bpf.h +24 -1
@@ -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 +* *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 **size** not a multiple +* of sizeof(struct perf_branch_entry). +* +* **-ENOENT** if architecture does not support branch records. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3012,7 +3031,8 @@ union bpf_attr {FN (probe_read_kernel_str), \FN (tcp_send_ack), \FN (send_signal_thread), \ -FN (jiffies64), +FN (jiffies64), \ +FN (read_branch_records),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3091,6 +3111,9 @@ enum bpf_func_id {/* BPF_FUNC_sk_storage_get flags */ #define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0) +/* BPF_FUNC_read_branch_records flags. */ +#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0) +/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET ,
tools/testing/selftests/bpf/prog_tests/perf_branches.c new file +169 -0
169 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_perf_branches.c new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.21.1
Re: [PATCH v8 bpf-next 2/2] selftests/bpf: add bpf_read_branch_records() selftest
Quoted 25 lines · Daniel Xu
Add a selftest to test:
* default
bpf_read_branch_records()behavior
*BPF_F_GET_BRANCH_RECORDS_SIZEflag behavior
* error path on non branch record perf events
* using helper to write to stack
* using helper to write to globalOn host with hardware counter support:
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILEDOn host without hardware counter support (VM):
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 1 SKIPPED, 0 FAILED
Quoted 4 lines
Also sync
tools/include/uapi/linux/bpf.h.Signed-off-by Daniel Xu <[email protected]>---
LGTM.
Quoted 7 lines
tools/include/uapi/linux/bpf.h | 25 ++- .../selftests/bpf/prog_tests/perf_branches.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_branches.c | 50 ++++++ 3 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_branches.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_branches.c
[...]
Re: [PATCH v8 bpf-next 2/2] selftests/bpf: add bpf_read_branch_records() selftest
In reply to Daniel XuQuoted 17 lines · Daniel Xu
Add a selftest to test:
* default
bpf_read_branch_records()behavior
*BPF_F_GET_BRANCH_RECORDS_SIZEflag behavior
* error path on non branch record perf events
* using helper to write to stack
* using helper to write to globalOn host with hardware counter support:
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
Quoted 7 lines
On host without hardware counter support (VM):
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 1 SKIPPED, 0 FAILED
That's not what I see:
./test_progs -t perf_branches
test_perf_branches_hw:FAIL:perf_event_open err -1
#27/1 perf_branches_hw:FAIL
#27/2 perf_branches_no_hw:OK
#27 perf_branches:FAIL
Summary: 0/1 PASSED, 0 SKIPPED, 2 FAILED
I remember previous version used to work, but something changed.
Re: [PATCH v8 bpf-next 2/2] selftests/bpf: add bpf_read_branch_records() selftest
Quoted 35 lines · Alexei Starovoitov
Quoted 17 lines · Daniel Xu
Add a selftest to test:
* default
bpf_read_branch_records()behavior
*BPF_F_GET_BRANCH_RECORDS_SIZEflag behavior
* error path on non branch record perf events
* using helper to write to stack
* using helper to write to globalOn host with hardware counter support:
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILEDQuoted 7 lines
On host without hardware counter support (VM):
# ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 1 SKIPPED, 0 FAILEDThat's not what I see:
./test_progs -tperf_branches
test_perf_branches_hw:FAIL:perf_event_open err -1
#27/1 perf_branches_hw:FAIL
#27/2 perf_branches_no_hw:OK
#27 perf_branches:FAIL
Summary: 0/1 PASSED, 0 SKIPPED, 2 FAILED
Quoted 1 line
I remember previous version used to work, but something changed.
Looks like the error code has changed. I've added
if (errno == ENOENT || errno == EOPNOTSUPP) and applied.
Please test your patches more carefully next time.