Skip to contentSkip to conversation

bpf: Remove config check to enable bpf support for branch records

PATCH Superseded Kajol Jain 2 participants 3 messages Nov 23, 2021 – Nov 24, 2021

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kajol Jain

author
#0 lore raw

PATCH v3 1/1 bpf: Remove config check to enable bpf support for branch records

Branch data available to bpf programs can be very useful to get stack traces out of userspace application.

Commit fff7b64355ea ("bpf: Add bpf_read_branch_records() helper") added bpf support to capture branch records in x86. Enable this feature for other architectures as well by removing check specific to x86.

Incase any architecture doesn't support branch records, bpf_read_branch_records still have appropriate checks and it will return error number -EINVAL in that scenario. But based on documentation there in include/uapi/linux/bpf.h file, incase of unsupported archs, this function should return -ENOENT. Hence update the appropriate checks to return -ENOENT instead.

Selftest 'perf_branches' result on power9 machine which has branch stacks support.

Before this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:FAIL
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:FAIL
Summary: 0/1 PASSED, 0 SKIPPED, 1 FAILED

After this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:OK
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:OK
Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED

Selftest 'perf_branches' result on power9 machine which doesn't have branch stack report.

After this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:SKIP
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:OK
Summary: 1/1 PASSED, 1 SKIPPED, 0 FAILED

Fixes fff7b64355eac ("bpf: Add bpf_read_branch_records() helper")

---

Tested this patch changes on power9 machine using selftest 'perf branches' which is added in commit 67306f84ca78 ("selftests/bpf:
Add bpf_read_branch_records()")

Changelog:
v2 -> v3
- Change the return error number for bpf_read_branch_records function from -EINVAL to -ENOENT for appropriate checks as suggested by Daniel Borkmann.

- Link to the v2 patch: https://lkml.org/lkml/2021/11/18/510

v1 -> v2
- Inorder to add bpf support to capture branch record in powerpc, rather then adding config for powerpc, entirely remove config check from bpf_read_branch_records function as suggested by Peter Zijlstra

- Link to the v1 patch: https://lkml.org/lkml/2021/11/14/434

 kernel/trace/bpf_trace.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
kernel/trace/bpf_trace.c +2 -6
index 7396488793ff..b94a00f92759 100644
@@ -1402,18 +1402,15 @@ static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
 	   void *, buf, u32, size, u64, flags)
 {
-#ifndef CONFIG_X86
-	return -ENOENT;
-#else
 	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
 	struct perf_branch_stack *br_stack = ctx->data->br_stack;
 	u32 to_copy;
 
 	if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
-		return -EINVAL;
+		return -ENOENT;
 
 	if (unlikely(!br_stack))
-		return -EINVAL;
+		return -ENOENT;
 
 	if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
 		return br_stack->nr * br_entry_size;
@@ -1425,7 +1422,6 @@ BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
 	memcpy(buf, br_stack->entries, to_copy);
 
 	return to_copy;
-#endif
 }
 
 static const struct bpf_func_proto bpf_read_branch_records_proto = {
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 77 lines · Kajol Jain

Branch data available to bpf programs can be very useful to get stack traces out of userspace application.

Commit fff7b64355ea ("bpf: Add bpf_read_branch_records() helper") added bpf support to capture branch records in x86. Enable this feature for other architectures as well by removing check specific to x86.

Incase any architecture doesn't support branch records, bpf_read_branch_records still have appropriate checks and it will return error number -EINVAL in that scenario. But based on documentation there in include/uapi/linux/bpf.h file, incase of unsupported archs, this function should return -ENOENT. Hence update the appropriate checks to return -ENOENT instead.

Selftest 'perf_branches' result on power9 machine which has branch stacks support.

Before this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:FAIL
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:FAIL
Summary: 0/1 PASSED, 0 SKIPPED, 1 FAILED

After this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:OK
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:OK
Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED

Selftest 'perf_branches' result on power9 machine which doesn't have branch stack report.

After this patch changes:
[command]# ./test_progs -t perf_branches
#88/1 perf_branches/perf_branches_hw:SKIP
#88/2 perf_branches/perf_branches_no_hw:OK
#88 perf_branches:OK
Summary: 1/1 PASSED, 1 SKIPPED, 0 FAILED

Fixes fff7b64355eac ("bpf: Add bpf_read_branch_records() helper")

---

Tested this patch changes on power9 machine using selftest 'perf branches' which is added in commit 67306f84ca78 ("selftests/bpf:
Add bpf_read_branch_records()")

Changelog:
v2 -> v3
- Change the return error number for bpf_read_branch_records function from -EINVAL to -ENOENT for appropriate checks as suggested by Daniel Borkmann.

- Link to the v2 patch: https://lkml.org/lkml/2021/11/18/510

v1 -> v2
- Inorder to add bpf support to capture branch record in powerpc, rather then adding config for powerpc, entirely remove config check from bpf_read_branch_records function as suggested by Peter Zijlstra

- Link to the v1 patch: https://lkml.org/lkml/2021/11/14/434

  kernel/trace/bpf_trace.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
kernel/trace/bpf_trace.c +0 -0
index 7396488793ff..b94a00f92759 100644
@@ -1402,18 +1402,15 @@ static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
  	   void *, buf, u32, size, u64, flags)
  {

kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { · line 1410

Quoted 10 lines
kernel/trace/bpf_trace.c +1 -4
-#ifndef CONFIG_X86
-	return -ENOENT;
-#else
  	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  	struct perf_branch_stack *br_stack = ctx->data->br_stack;
  	u32 to_copy;
 
  	if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
-		return -EINVAL;
+		return -ENOENT;

What's the rationale for also changing the above? Invalid/unsupported flags should still return -EINVAL as they did before ...

kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { · line 1413

Quoted 3 lines
kernel/trace/bpf_trace.c +1 -1
  	if (unlikely(!br_stack))
-		return -EINVAL;
+		return -ENOENT;

... meaning only this one here was necessary.

Quoted 11 lines
kernel/trace/bpf_trace.c +0 -1
  	if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
  		return br_stack->nr * br_entry_size;
@@ -1425,7 +1422,6 @@ BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
  	memcpy(buf, br_stack->entries, to_copy);
 
  	return to_copy;
-#endif
  }
 
  static const struct bpf_func_proto bpf_read_branch_records_proto = {
author
#2 lore raw
Quoted 85 lines · Daniel Borkmann
Quoted 84 lines · Kajol Jain

Branch data available to bpf programs can be very useful to get stack traces out of userspace application.

Commit fff7b64355ea ("bpf: Add bpf_read_branch_records() helper") added bpf support to capture branch records in x86. Enable this feature for other architectures as well by removing check specific to x86.

Incase any architecture doesn't support branch records, bpf_read_branch_records still have appropriate checks and it will return error number -EINVAL in that scenario. But based on documentation there in include/uapi/linux/bpf.h file, incase of unsupported archs, this function should return -ENOENT. Hence update the appropriate checks to return -ENOENT instead.

Selftest 'perf_branches' result on power9 machine which has branch stacks support.

Before this patch changes:
[command]# ./test_progs -t perf_branches
  #88/1 perf_branches/perf_branches_hw:FAIL
  #88/2 perf_branches/perf_branches_no_hw:OK
  #88 perf_branches:FAIL
Summary: 0/1 PASSED, 0 SKIPPED, 1 FAILED

After this patch changes:
[command]# ./test_progs -t perf_branches
  #88/1 perf_branches/perf_branches_hw:OK
  #88/2 perf_branches/perf_branches_no_hw:OK
  #88 perf_branches:OK
Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED

Selftest 'perf_branches' result on power9 machine which doesn't have branch stack report.

After this patch changes:
[command]# ./test_progs -t perf_branches
  #88/1 perf_branches/perf_branches_hw:SKIP
  #88/2 perf_branches/perf_branches_no_hw:OK
  #88 perf_branches:OK
Summary: 1/1 PASSED, 1 SKIPPED, 0 FAILED

Fixes fff7b64355eac ("bpf: Add bpf_read_branch_records() helper")

---

Tested this patch changes on power9 machine using selftest 'perf branches' which is added in commit 67306f84ca78 ("selftests/bpf:
Add bpf_read_branch_records()")

Changelog:
v2 -> v3
- Change the return error number for bpf_read_branch_records function from -EINVAL to -ENOENT for appropriate checks as suggested by Daniel Borkmann.

- Link to the v2 patch: https://lkml.org/lkml/2021/11/18/510

v1 -> v2
- Inorder to add bpf support to capture branch record in powerpc, rather then adding config for powerpc, entirely remove config check from bpf_read_branch_records function as suggested by Peter Zijlstra

- Link to the v1 patch: https://lkml.org/lkml/2021/11/14/434

  kernel/trace/bpf_trace.c | 8 ++------

  1 file changed, 2 insertions(+), 6 deletions(-)
kernel/trace/bpf_trace.c +0 -0
index 7396488793ff..b94a00f92759 100644
@@ -1402,18 +1402,15 @@ static const struct bpf_func_proto
 bpf_perf_prog_read_value_proto = {

  BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
         void *, buf, u32, size, u64, flags)
  {

kernel/trace/bpf_trace.c +0 -3
-#ifndef CONFIG_X86
-    return -ENOENT;
-#else

      static const u32 br_entry_size = sizeof(struct perf_branch_entry); struct perf_branch_stack *br_stack = ctx->data->br_stack;

Quoted 8 lines
Quoted 4 lines

      u32 to_copy;
        if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) -        return -EINVAL;
+        return -ENOENT;

What's the rationale for also changing the above? Invalid/unsupported flags should
still return -EINVAL as they did before ...

Thanks for pointing it, I will make this change in the next version.

Thanks,
Kajol Jain

Quoted 18 lines
Quoted 4 lines

      if (unlikely(!br_stack))
-        return -EINVAL;
+        return -ENOENT;

... meaning only this one here was necessary.

Quoted 10 lines

      if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
          return br_stack->nr * br_entry_size;

kernel/trace/bpf_trace.c +0 -1
@@ -1425,7 +1422,6 @@ BPF_CALL_4(bpf_read_branch_records, struct
 bpf_perf_event_data_kern *, ctx,
       memcpy(buf, br_stack->entries, to_copy);
         return to_copy;
-#endif
   }
     static const struct bpf_func_proto bpf_read_branch_records_proto = {