bpf: Make the verifier recognize llvm register allocation patterns.
Subsystems (6)
v2 cover bpf: Make the verifier recognize llvm register allocation patterns.
From: Alexei Starovoitov <[email protected]>
v1->v2:
- fixed 32-bit mov issue spotted by John.
- allowed r2=r1; r3=r2; sequence as suggested by John.
- added comments, acks, more tests.
Make two verifier improvements:
- The llvm register allocator may use two different registers representing the same virtual register. Teach the verifier to recognize that.
- Track bounded scalar spill/fill.
The profiler[123] test in patch 3 will fail to load without patches 1 and 2. The profiler[23] test may fail to load on older llvm due to speculative code motion nd instruction combining optimizations that are fixed in
https://reviews.llvm.org/D85570
Alexei Starovoitov (3):
bpf: Propagate scalar ranges through register assignments. selftests/bpf: Add profiler test
selftests/bpf: Asm tests for the verifier regalloc tracking.
Yonghong Song (1):
bpf: Track spill/fill of bounded scalars.
kernel/bpf/verifier.c | 66 +- tools/testing/selftests/bpf/README.rst | 38 + .../testing/selftests/bpf/prog_tests/align.c | 16 +- .../selftests/bpf/prog_tests/test_profiler.c | 72 ++ tools/testing/selftests/bpf/progs/profiler.h | 177 ++++ .../selftests/bpf/progs/profiler.inc.h | 969 ++++++++++++++++++ tools/testing/selftests/bpf/progs/profiler1.c | 6 + tools/testing/selftests/bpf/progs/profiler2.c | 6 + tools/testing/selftests/bpf/progs/profiler3.c | 6 + .../bpf/verifier/direct_packet_access.c | 2 +- .../testing/selftests/bpf/verifier/regalloc.c | 243 +++++ 11 files changed, 1591 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_profiler.c create mode 100644 tools/testing/selftests/bpf/progs/profiler.h create mode 100644 tools/testing/selftests/bpf/progs/profiler.inc.h create mode 100644 tools/testing/selftests/bpf/progs/profiler1.c create mode 100644 tools/testing/selftests/bpf/progs/profiler2.c create mode 100644 tools/testing/selftests/bpf/progs/profiler3.c create mode 100644 tools/testing/selftests/bpf/verifier/regalloc.c
Signature · 1 line
2.23.0
PATCH v2 1/4 bpf: Propagate scalar ranges through register assignments.
From: Alexei Starovoitov <[email protected]>
The llvm register allocator may use two different registers representing the same virtual register. In such case the following pattern can be observed:
1047: (bf) r9 = r6
1048: (a5) if r6 < 0x1000 goto pc+1
1050: ...
1051: (a5) if r9 < 0x2 goto pc+66
1052: ...
1053: (bf) r2 = r9 /* r2 needs to have upper and lower bounds */
This is normal behavior of greedy register allocator. The slides 137+ explain why regalloc introduces such register copy:
http://llvm.org/devmtg/2018-04/slides/Yatsina-LLVM%20Greedy%20Register%20Allocator.pdf
There is no way to tell llvm 'not to do this'.
Hence the verifier has to recognize such patterns.
In order to track this information without backtracking allocate ID for scalars in a similar way as it's done for find_good_pkt_pointers().
When the verifier encounters r9 = r6 assignment it will assign the same ID to both registers. Later if either register range is narrowed via conditional jump propagate the register state into the other register.
Clear register ID in adjust_reg_min_max_vals() for any alu instruction. The register ID is ignored for scalars in regsafe() and doesn't affect state pruning. mark_reg_unknown() clears the ID. It's used to process call, endian and other instructions. Hence ID is explicitly cleared only in adjust_reg_min_max_vals() and in 32-bit mov.
--- kernel/bpf/verifier.c | 50 +++++++++++++++++++ .../testing/selftests/bpf/prog_tests/align.c | 16 +++--- .../bpf/verifier/direct_packet_access.c | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +50 -0
@@ -6436,6 +6436,11 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, src_reg =NULL ;if (dst_reg->type !=SCALAR_VALUE ) ptr_reg = dst_reg; +else +/* Make sure ID is cleared otherwise dst_reg min/max could be +* incorrectly propagated into other registers by find_equal_scalars() +*/ + dst_reg->id =0 ;if (BPF_SRC (insn->code) ==BPF_X ) { src_reg = ®s[insn->src_reg];if (src_reg->type !=SCALAR_VALUE ) {
@@ -6569,6 +6574,12 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)/* case: R1 = R2 * copy register state to dest reg */ +if (src_reg->type ==SCALAR_VALUE && !src_reg->id) +/* Assign src and dst registers the same ID +* that will be used by find_equal_scalars() +* to propagate min/max range. +*/ + src_reg->id = ++env->id_gen; *dst_reg = *src_reg; dst_reg->live |=REG_LIVE_WRITTEN ; dst_reg->subreg_def =DEF_NOT_SUBREG ;
@@ -6581,6 +6592,11 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)return -EACCES ; }else if (src_reg->type ==SCALAR_VALUE ) { *dst_reg = *src_reg; +/* Make sure ID is cleared otherwise +* dst_reg min/max could be incorrectly +* propagated into src_reg by find_equal_scalars() +*/ + dst_reg->id =0 ; dst_reg->live |=REG_LIVE_WRITTEN ; dst_reg->subreg_def = env->insn_idx +1 ; }else {
@@ -7369,6 +7385,30 @@ static bool try_match_pkt_pointers(const struct bpf_insn *insn,return true; } +static void find_equal_scalars (struct bpf_verifier_state *vstate, +struct bpf_reg_state *known_reg) +{ +struct bpf_func_state *state; +struct bpf_reg_state *reg; +int i, j; + +for (i =0 ; i <= vstate->curframe; i++) { + state = vstate->frame[i]; +for (j =0 ; j <MAX_BPF_REG ; j++) { + reg = &state->regs[j]; +if (reg->type ==SCALAR_VALUE && reg->id == known_reg->id) + *reg = *known_reg; + } + +bpf_for_each_spilled_reg (j, state, reg) { +if (!reg) +continue ; +if (reg->type ==SCALAR_VALUE && reg->id == known_reg->id) + *reg = *known_reg; + } + } +} +static int check_cond_jmp_op (struct bpf_verifier_env *env,struct bpf_insn *insn,int *insn_idx) {
@@ -7497,6 +7537,11 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,reg_combine_min_max (&other_branch_regs[insn->src_reg], &other_branch_regs[insn->dst_reg], src_reg, dst_reg, opcode); +if (src_reg->id) { +find_equal_scalars (this_branch, src_reg); +find_equal_scalars (other_branch, &other_branch_regs[insn->src_reg]); + } + } }else if (dst_reg->type ==SCALAR_VALUE ) { reg_set_min_max(&other_branch_regs[insn->dst_reg],
@@ -7504,6 +7549,11 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, opcode, is_jmp32); } +if (dst_reg->type ==SCALAR_VALUE && dst_reg->id) { +find_equal_scalars (this_branch, dst_reg); +find_equal_scalars (other_branch, &other_branch_regs[insn->dst_reg]); + } +/* detect if R == 0 where R is returned from bpf_map_lookup_elem(). * NOTE: these optimizations below are related with pointer comparison * which will never be JMP32.
tools/testing/selftests/bpf/prog_tests/align.c +8 -8
@@ -195,13 +195,13 @@ static struct bpf_align_test tests[] = { .prog_type = BPF_PROG_TYPE_SCHED_CLS, .matches = { {7, "R3_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, - {8, "R4_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, + {8, "R4_w=inv(id=1,umax_value=255,var_off=(0x0; 0xff))"}, {9, "R4_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, - {10, "R4_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, + {10, "R4_w=inv(id=1,umax_value=255,var_off=(0x0; 0xff))"}, {11, "R4_w=inv(id=0,umax_value=510,var_off=(0x0; 0x1fe))"}, - {12, "R4_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, + {12, "R4_w=inv(id=1,umax_value=255,var_off=(0x0; 0xff))"}, {13, "R4_w=inv(id=0,umax_value=1020,var_off=(0x0; 0x3fc))"}, - {14, "R4_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff))"}, + {14, "R4_w=inv(id=1,umax_value=255,var_off=(0x0; 0xff))"}, {15, "R4_w=inv(id=0,umax_value=2040,var_off=(0x0; 0x7f8))"}, {16, "R4_w=inv(id=0,umax_value=4080,var_off=(0x0; 0xff0))"}, },
@@ -518,7 +518,7 @@ static struct bpf_align_test tests[] = {* the total offset is 4-byte aligned and meets the * load's requirements. */ - {20, "R5=pkt(id=1,off=0,r=4,umin_value=2,umax_value=1034,var_off=(0x2; 0x7fc)"}, + {20, "R5=pkt(id=2,off=0,r=4,umin_value=2,umax_value=1034,var_off=(0x2; 0x7fc)"}, }, },
@@ -561,18 +561,18 @@ static struct bpf_align_test tests[] = {/* Adding 14 makes R6 be (4n+2) */ {11, "R6_w=inv(id=0,umin_value=14,umax_value=74,var_off=(0x2; 0x7c))"},/* Subtracting from packet pointer overflows ubounds */ - {13, "R5_w=pkt(id=1,off=0,r=8,umin_value=18446744073709551542,umax_value=18446744073709551602,var_off=(0xffffffffffffff82; 0x7c)"}, + {13, "R5_w=pkt(id=2,off=0,r=8,umin_value=18446744073709551542,umax_value=18446744073709551602,var_off=(0xffffffffffffff82; 0x7c)"},/* New unknown value in R7 is (4n), >= 76 */ {15, "R7_w=inv(id=0,umin_value=76,umax_value=1096,var_off=(0x0; 0x7fc))"},/* Adding it to packet pointer gives nice bounds again */ - {16, "R5_w=pkt(id=2,off=0,r=0,umin_value=2,umax_value=1082,var_off=(0x2; 0xfffffffc)"}, + {16, "R5_w=pkt(id=3,off=0,r=0,umin_value=2,umax_value=1082,var_off=(0x2; 0xfffffffc)"},/* At the time the word size load is performed from R5, * its total fixed offset is NET_IP_ALIGN + reg->off (0) * which is 2. Then the variable offset is (4n+2), so * the total offset is 4-byte aligned and meets the * load's requirements. */ - {20, "R5=pkt(id=2,off=0,r=4,umin_value=2,umax_value=1082,var_off=(0x2; 0xfffffffc)"}, + {20, "R5=pkt(id=3,off=0,r=4,umin_value=2,umax_value=1082,var_off=(0x2; 0xfffffffc)"}, }, }, };
tools/testing/selftests/bpf/verifier/direct_packet_access.c +1 -1
@@ -529,7 +529,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "invalid access to packet, off=0 size=8, R5(id=1,off=0,r=0)", + .errstr = "invalid access to packet, off=0 size=8, R5(id=2,off=0,r=0)", .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, }, {
Signature · 1 line
2.23.0
RE: [PATCH v2 bpf-next 1/4] bpf: Propagate scalar ranges through register assignments.
Quoted 30 lines · Alexei Starovoitov
From: Alexei Starovoitov <[email protected]>
The llvm register allocator may use two different registers representing the same virtual register. In such case the following pattern can be observed:
1047: (bf) r9 = r6
1048: (a5) if r6 <0x1000goto pc+1
1050: ...
1051: (a5) if r9 <0x2goto pc+66
1052: ...
1053: (bf) r2 = r9 /* r2 needs to have upper and lower bounds */This is normal behavior of greedy register allocator. The slides 137+ explain why regalloc introduces such register copy:
http://llvm.org/devmtg/2018-04/slides/Yatsina-LLVM%20Greedy%20Register%20Allocator.pdf
There is no way to tell llvm 'not to do this'.
Hence the verifier has to recognize such patterns.In order to track this information without backtracking allocate ID for scalars in a similar way as it's done for
find_good_pkt_pointers().When the verifier encounters r9 = r6 assignment it will assign the same ID to both registers. Later if either register range is narrowed via conditional jump propagate the register state into the other register.
Clear register ID in
adjust_reg_min_max_vals()for any alu instruction. The register ID is ignored for scalars inregsafe()and doesn't affect state pruning.mark_reg_unknown()clears the ID. It's used to process call, endian and other instructions. Hence ID is explicitly cleared only inadjust_reg_min_max_vals()and in 32-bit mov.
Quoted 8 lines
Signed-off-by Alexei Starovoitov <[email protected]>Acked-by Andrii Nakryiko <[email protected]>--- kernel/bpf/verifier.c | 50 +++++++++++++++++++ .../testing/selftests/bpf/prog_tests/align.c | 16 +++--- .../bpf/verifier/direct_packet_access.c | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-)
I also walked through the stack read case and I don't see any issues there either. We will run check_mem_access on the ld with src_reg->type set to PTR_TO_STACK. So we run through check_stack_read() with a value_regno set to the dst_reg (>=0). The case I was thinking of was if we had a size != BPF_REG_SIZE. But, this case is handled by marking the dst reg unknown and never copying over the stack[spi]. If size == BPF_REG_SIZE its a full read and we also ensure all stype[] are STACK_SPILL. Then everything looks safe to do state->regs[value_reno] = *reg e.g. assign register state from stack complete with reg.id.
Looking the other way at stack writes. We only allow SCALAR type bounds to be saved on the stack if they are constant and size = BPF_REG_SIZE so no problems there.
LGTM!
PATCH v2 2/4 bpf: Track spill/fill of bounded scalars.
In reply to Alexei StarovoitovFrom: Yonghong Song <[email protected]>
Under register pressure the llvm may spill registers with bounds into the stack. The verifier has to track them through spill/fill otherwise many kinds of bound errors will be seen. The spill/fill of induction variables was already happening. This patch extends this logic from tracking spill/fill of a constant into any bounded register. There is no need to track spill/fill of unbounded, since no new information will be retrieved from the stack during register fill.
Though extra stack difference could cause state pruning to be less effective, no adverse affects were seen from this patch on selftests and on cilium programs.
--- kernel/bpf/verifier.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +15 -1
@@ -2227,6 +2227,20 @@ static bool register_is_const(struct bpf_reg_state *reg)return reg->type ==SCALAR_VALUE &&tnum_is_const (reg->var_off); } +static bool __is_scalar_unbounded (struct bpf_reg_state *reg) +{ +return tnum_is_unknown (reg->var_off) && + reg->smin_value ==S64_MIN && reg->smax_value ==S64_MAX && + reg->umin_value ==0 && reg->umax_value ==U64_MAX && + reg->s32_min_value ==S32_MIN && reg->s32_max_value ==S32_MAX && + reg->u32_min_value ==0 && reg->u32_max_value ==U32_MAX ; +} + +static bool register_is_bounded (struct bpf_reg_state *reg) +{ +return reg->type ==SCALAR_VALUE && !__is_scalar_unbounded (reg); +} +static bool __is_pointer_value (bool allow_ptr_leaks,const struct bpf_reg_state *reg) {
@@ -2278,7 +2292,7 @@ static int check_stack_write(struct bpf_verifier_env *env,if (value_regno >=0 ) reg = &cur->regs[value_regno]; -if (reg && size ==BPF_REG_SIZE &®ister_is_const (reg) && +if (reg && size ==BPF_REG_SIZE &®ister_is_bounded (reg) && !register_is_null (reg) && env->bpf_capable) {if (dst_reg !=BPF_REG_FP ) {/* The backtracking logic can only recognize explicit
Signature · 1 line
2.23.0
RE: [PATCH v2 bpf-next 2/4] bpf: Track spill/fill of bounded scalars.
Quoted 12 lines · Alexei Starovoitov
From: Yonghong Song <[email protected]>
Under register pressure the llvm may spill registers with bounds into the stack. The verifier has to track them through spill/fill otherwise many kinds of bound errors will be seen. The spill/fill of induction variables was already happening. This patch extends this logic from tracking spill/fill of a constant into any bounded register. There is no need to track spill/fill of unbounded, since no new information will be retrieved from the stack during register fill.
Though extra stack difference could cause state pruning to be less effective, no adverse affects were seen from this patch on selftests and on cilium programs.
Quoted 6 lines
Signed-off-by Yonghong Song <[email protected]>Signed-off-by Alexei Starovoitov <[email protected]>Acked-by Andrii Nakryiko <[email protected]>--- kernel/bpf/verifier.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
LGTM and will be useful.
PATCH v2 3/4 selftests/bpf: Add profiler test
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
The main purpose of the profiler test to check different llvm generation patterns to make sure the verifier can load these large programs.
Note that profiler.inc.h test doesn't follow strict kernel coding style. The code was formatted in the kernel style, but variable declarations are kept as-is to preserve original llvm IR pattern.
profiler1.c should pass with older and newer llvm
profiler[23].c may fail on older llvm that don't have:
https://reviews.llvm.org/D85570
because llvm may do speculative code motion optimization that will generate code like this:
// r9 is a pointer to map_value
// r7 is a scalar
17: bf 96 00 00 00 00 00 00 r6 = r9 18: 0f 76 00 00 00 00 00 00 r6 += r7 19: a5 07 01 00 01 01 00 00 if r7 < 257 goto +1 20: bf 96 00 00 00 00 00 00 r6 = r9
// r6 is used here
The verifier will reject such code with the error:
"math between map_value pointer and register with unbounded min value is not allowed" At insn 18 the r7 is indeed unbounded. The later insn 19 checks the bounds and the insn 20 undoes map_value addition. It is currently impossible for the verifier to understand such speculative pointer arithmetic. Hence llvm D85570 addresses it on the compiler side.
--- tools/testing/selftests/bpf/README.rst | 38 + .../selftests/bpf/prog_tests/test_profiler.c | 72 ++ tools/testing/selftests/bpf/progs/profiler.h | 177 ++++ .../selftests/bpf/progs/profiler.inc.h | 969 ++++++++++++++++++ tools/testing/selftests/bpf/progs/profiler1.c | 6 + tools/testing/selftests/bpf/progs/profiler2.c | 6 + tools/testing/selftests/bpf/progs/profiler3.c | 6 + 7 files changed, 1274 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_profiler.c create mode 100644 tools/testing/selftests/bpf/progs/profiler.h create mode 100644 tools/testing/selftests/bpf/progs/profiler.inc.h create mode 100644 tools/testing/selftests/bpf/progs/profiler1.c create mode 100644 tools/testing/selftests/bpf/progs/profiler2.c create mode 100644 tools/testing/selftests/bpf/progs/profiler3.c
tools/testing/selftests/bpf/README.rst +38 -0
@@ -7,6 +7,44 @@ General instructions on running selftests can be found in Additional information about selftest failures are documented here. +profiler[23] test failures with clang/llvm <12.0.0 +================================================== + +With clang/llvm <12.0.0, the profiler[23] test may fail. +The symptom looks like + +.. code-block:: c + + // r9 is a pointer to map_value + // r7 is a scalar + 17: bf 96 00 00 00 00 00 00 r6 = r9 + 18: 0f 76 00 00 00 00 00 00 r6 += r7 + math between map_value pointer and register with unbounded min value is not allowed + + // the instructions below will not be seen in the verifier log + 19: a5 07 01 00 01 01 00 00 if r7 < 257 goto +1 + 20: bf 96 00 00 00 00 00 00 r6 = r9 + // r6 is used here + +The verifier will reject such code with above error. +At insn 18 the r7 is indeed unbounded. The later insn 19 checks the bounds and +the insn 20 undoes map_value addition. It is currently impossible for the +verifier to understand such speculative pointer arithmetic. +Hence + https://reviews.llvm.org/D85570 +addresses it on the compiler side. It was committed on llvm 12. + +The corresponding C code +.. code-block:: c + + for (int i = 0; i < MAX_CGROUPS_PATH_DEPTH; i++) { + filepart_length = bpf_probe_read_str(payload, ...); + if (filepart_length <= MAX_PATH) { + barrier_var(filepart_length); // workaround + payload += filepart_length; + } + } + bpf_iter test failures with clang/llvm 10.0.0 =============================================
tools/testing/selftests/bpf/prog_tests/test_profiler.c new file +72 -0
72 lines not on this page · raw message
tools/testing/selftests/bpf/progs/profiler.h new file +177 -0
177 lines not on this page · raw message
tools/testing/selftests/bpf/progs/profiler.inc.h new file +969 -0
969 lines not on this page · raw message
tools/testing/selftests/bpf/progs/profiler1.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#define barrier_var (var) asm volatile("" : "=r"(var) : "0"(var)) +#define UNROLL +#define INLINE __always_inline +#include "profiler.inc.h"
tools/testing/selftests/bpf/progs/profiler2.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#define barrier_var (var)/**/ +/* undef #define UNROLL */ +#define INLINE /**/ +#include "profiler.inc.h"
tools/testing/selftests/bpf/progs/profiler3.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#define barrier_var (var)/**/ +#define UNROLL +#define INLINE __noinline +#include "profiler.inc.h"
Signature · 1 line
2.23.0
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 5 lines · Alexei Starovoitov
From: Alexei Starovoitov <[email protected]>
The main purpose of the profiler test to check different llvm generation patterns to make sure the verifier can load these large programs.
Quoted 8 lines
Note that
profiler.inc.htest doesn't follow strict kernel coding style. The code was formatted in the kernel style, but variable declarations are kept as-is to preserve original llvm IR pattern.
profiler1.cshould pass with older and newer llvmprofiler[23].c may fail on older llvm that don't have:
https://reviews.llvm.org/D85570
Not sure but the below equivalent URL may be more intuitive:
https://reviews.llvm.org/D85570
Quoted 11 lines
because llvm may do speculative code motion optimization that will generate code like this:
// r9 is a pointer to
map_value
// r7 is a scalar17: bf 96 00 00 00 00 00 00 r6 = r9 18: 0f 76 00 00 00 00 00 00 r6 += r7 19: a5 07 01 00 01 01 00 00 if r7 < 257 goto +1 20: bf 96 00 00 00 00 00 00 r6 = r9// r6 is used here
Quoted 8 lines
The verifier will reject such code with the error:
"math betweenmap_valuepointer and register with unbounded min value is not allowed" At insn 18 the r7 is indeed unbounded. The later insn 19 checks the bounds and the insn 20 undoesmap_valueaddition. It is currently impossible for the verifier to understand such speculative pointer arithmetic. Hence llvm D85570 addresses it on the compiler side.Signed-off-by Alexei Starovoitov <[email protected]>
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 6 lines · Yonghong Song
Quoted 3 lines
profiler[23].c may fail on older llvm that don't have:
https://reviews.llvm.org/D85570Not sure but the below equivalent URL may be more intuitive:
https://reviews.llvm.org/D85570
It's the same URL :)
See it unmangled here:
https://patchwork.kernel.org/patch/11824813/#23675319
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yonghong Song
Quoted 3 lines
profiler[23].c may fail on older llvm that don't have:
https://reviews.llvm.org/D85570Not sure but the below equivalent URL may be more intuitive:
https://reviews.llvm.org/D85570It's the same URL :)
See it unmangled here:
https://patchwork.kernel.org/patch/11824813/#23675319
WoW, I never pay attention to this. Thanks for pointing it out.
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
In reply to Alexei StarovoitovOn Thu, Oct 08, 2020 at 06:12:39PM -0700, Alexei Starovoitov wrote:
SNIP
Quoted 24 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +24
-0+ +#ifdef UNROLL +#pragma unroll +#endif +for (int i =0 ; i <MAX_CGROUPS_PATH_DEPTH ; i++) { + filepart_length = +bpf_probe_read_str (payload,MAX_PATH ,BPF_CORE_READ (cgroup_node, name)); +if (!cgroup_node) +return payload; +if (cgroup_node == cgroup_root_node) + *root_pos = payload - payload_start; +if (filepart_length <=MAX_PATH ) { +barrier_var (filepart_length); + payload += filepart_length; + } + cgroup_node =BPF_CORE_READ (cgroup_node, parent); + } +return payload; +} + +static ino_t get_inode_from_kernfs (struct kernfs_node * node) +{ +struct kernfs_node___52 * node52 = (void *)node; +
tools/testing/selftests/bpf/progs/profiler.inc.h line 246
Quoted 10 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +10
-0+if (bpf_core_field_exists (node52->id.ino)) { +barrier_var (node52); +return BPF_CORE_READ (node52, id.ino); + }else { +barrier_var (node); +return (u64 )BPF_CORE_READ (node, id); + } +} + +int pids_cgrp_id =1 ;
hi,
I'm getting compilation failure with this:
CLNG-LLC [test_maps] profiler2.o
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:246:5: error: redefinition of 'pids_cgrp_id' as different kind of symbol
int pids_cgrp_id = 1;
^
/home/jolsa/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:14531:2: note: previous definition is here
pids_cgrp_id = 11,
^
1 error generated.
/opt/clang/bin/llc: error: /opt/clang/bin/llc: <stdin>:1:1: error: expected top-level entity
BPF obj compilation failed
^
make: *** [Makefile:396: /home/jolsa/linux-qemu/tools/testing/selftests/bpf/profiler2.o] Error 1
jirka
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 44 lines · Jiri Olsa
On Thu, Oct 08, 2020 at 06:12:39PM -0700, Alexei Starovoitov wrote:
SNIP
Quoted 24 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +24
-0+ +#ifdef UNROLL +#pragma unroll +#endif +for (int i =0 ; i <MAX_CGROUPS_PATH_DEPTH ; i++) { + filepart_length = +bpf_probe_read_str (payload,MAX_PATH ,BPF_CORE_READ (cgroup_node, name)); +if (!cgroup_node) +return payload; +if (cgroup_node == cgroup_root_node) + *root_pos = payload - payload_start; +if (filepart_length <=MAX_PATH ) { +barrier_var (filepart_length); + payload += filepart_length; + } + cgroup_node =BPF_CORE_READ (cgroup_node, parent); + } +return payload; +} + +static ino_t get_inode_from_kernfs (struct kernfs_node * node) +{ +struct kernfs_node___52 * node52 = (void *)node; +tools/testing/selftests/bpf/progs/profiler.inc.h line 246
Quoted 10 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +10
-0+if (bpf_core_field_exists (node52->id.ino)) { +barrier_var (node52); +return BPF_CORE_READ (node52, id.ino); + }else { +barrier_var (node); +return (u64 )BPF_CORE_READ (node, id); + } +} + +int pids_cgrp_id =1 ;hi,
I'm getting compilation failure with this:
Quoted 7 lines
CLNG-LLC [
test_maps] profiler2.o
In file included fromprogs/profiler2.c:6:
progs/profiler.inc.h:246:5: error: redefinition of 'pids_cgrp_id' as different kind of symbol
intpids_cgrp_id= 1;
^
/home/jolsa/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:14531:2: note: previous definition is here
pids_cgrp_id= 11,
Interesting.
You probably have CONFIG_CGROUP_PIDS in your .config?
I don't and bpf CI doesn't have it either, so this issue wasn't spotted earlier.
I can hard code 11, of course, but
but maybe Andrii has a cool way to use co-re to deal with this? I think
"extern bool CONFIG_CGROUP_PIDS __kconfig"
won't work.
A good opportunity to try to use bpf_core_enum_value_exists() ?
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 53 lines · Alexei Starovoitov
Quoted 44 lines · Jiri Olsa
On Thu, Oct 08, 2020 at 06:12:39PM -0700, Alexei Starovoitov wrote:
SNIP
Quoted 24 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +24
-0+ +#ifdef UNROLL +#pragma unroll +#endif +for (int i =0 ; i <MAX_CGROUPS_PATH_DEPTH ; i++) { + filepart_length = +bpf_probe_read_str (payload,MAX_PATH ,BPF_CORE_READ (cgroup_node, name)); +if (!cgroup_node) +return payload; +if (cgroup_node == cgroup_root_node) + *root_pos = payload - payload_start; +if (filepart_length <=MAX_PATH ) { +barrier_var (filepart_length); + payload += filepart_length; + } + cgroup_node =BPF_CORE_READ (cgroup_node, parent); + } +return payload; +} + +static ino_t get_inode_from_kernfs (struct kernfs_node * node) +{ +struct kernfs_node___52 * node52 = (void *)node; +tools/testing/selftests/bpf/progs/profiler.inc.h line 246
Quoted 10 lines
tools/testing/selftests/bpf/progs/profiler.inc.h new file +10
-0+if (bpf_core_field_exists (node52->id.ino)) { +barrier_var (node52); +return BPF_CORE_READ (node52, id.ino); + }else { +barrier_var (node); +return (u64 )BPF_CORE_READ (node, id); + } +} + +int pids_cgrp_id =1 ;hi,
I'm getting compilation failure with this:Quoted 7 lines
CLNG-LLC [
test_maps] profiler2.o
In file included fromprogs/profiler2.c:6:
progs/profiler.inc.h:246:5: error: redefinition of 'pids_cgrp_id' as different kind of symbol
intpids_cgrp_id= 1;
^
/home/jolsa/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:14531:2: note: previous definition is here
pids_cgrp_id= 11,
Quoted 2 lines
Interesting.
You probably haveCONFIG_CGROUP_PIDSin your .config?
yes
jirka
Quoted 9 lines
I don't and bpf CI doesn't have it either, so this issue wasn't spotted earlier.
I can hard code 11, of course, but
but maybe Andrii has a cool way to use co-re to deal with this? I think
"extern boolCONFIG_CGROUP_PIDS__kconfig"
won't work.
A good opportunity to try to usebpf_core_enum_value_exists()?
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
In reply to Alexei StarovoitovQuoted 54 lines · Alexei Starovoitov
Quoted 44 lines · Jiri Olsa
On Thu, Oct 08, 2020 at 06:12:39PM -0700, Alexei Starovoitov wrote:
SNIP
Quoted 24 lines
(quoted diff) +24
-0+ +#ifdef UNROLL +#pragma unroll +#endif + for (int i = 0; i < MAX_CGROUPS_PATH_DEPTH; i++) { + filepart_length = + bpf_probe_read_str(payload, MAX_PATH, BPF_CORE_READ(cgroup_node, name)); + if (!cgroup_node) + return payload; + if (cgroup_node == cgroup_root_node) + *root_pos = payload - payload_start; + if (filepart_length <= MAX_PATH) { + barrier_var(filepart_length); + payload += filepart_length; + } + cgroup_node = BPF_CORE_READ(cgroup_node, parent); + } + return payload; +} + +static ino_t get_inode_from_kernfs(struct kernfs_node* node) +{ + struct kernfs_node___52* node52 = (void*)node; +Review
Quoted 10 lines
(quoted diff) +10
-0+ if (bpf_core_field_exists(node52->id.ino)) { + barrier_var(node52); + return BPF_CORE_READ(node52, id.ino); + } else { + barrier_var(node); + return (u64)BPF_CORE_READ(node, id); + } +} + +int pids_cgrp_id = 1;hi,
I'm getting compilation failure with this:Quoted 7 lines
CLNG-LLC [
test_maps] profiler2.o
In file included fromprogs/profiler2.c:6:
progs/profiler.inc.h:246:5: error: redefinition of 'pids_cgrp_id' as different kind of symbol
intpids_cgrp_id= 1;
^
/home/jolsa/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:14531:2: note: previous definition is here
pids_cgrp_id= 11,
Quoted 6 lines
Interesting.
You probably haveCONFIG_CGROUP_PIDSin your .config?
I don't and bpf CI doesn't have it either, so this issue wasn't spotted earlier.I can hard code 11, of course, but
but maybe Andrii has a cool way to use co-re to deal with this?
Cool or not, but we do have a way to deal with it. :)
Quoted 4 lines
I think
"extern boolCONFIG_CGROUP_PIDS__kconfig"
won't work.
A good opportunity to try to usebpf_core_enum_value_exists()?
There are fews parts here.
First, we can't rely that vmlinux.h has pids_cgrp_id enum defined, so we need to define our own. But in such a way that it doesn't collide with enum cgroup_subsys_id in vmlinux.h. ___suffix for the rescue here:
enum cgroup_subsys_id___local {
pids_cgrp_id___local = 1, /* anything but zero */
};
If value is zero, built-in will complain. Need to check with Yonghong on why that is. For all things CO-RE, ___local suffix is going to be ignored.
Second, detecting if the kernel even has this pids_cgrp_id defined. That could be done with __kconfig as you mentioned, actually (but need __weak as well). But one can also use bpf_core_enum_value_exists as well. So it's either:
extern bool CONFIG_CGROUP_PIDS __kconfig __weak;
...
if (ENABLE_CGROUP_V1_RESOLVER && CONFIG_CGROUP_PIDS) {
...
}
Or a bit more verbose way with relos:
if (ENABLE_CGROUP_V1_RESOLVER &&
bpf_core_enum_value_exists(enum cgroup_subsys_id___local,pids_cgrp_id___local)) {
...
}
Third, actually getting the value of enum. This one would be impossible without CO-RE reloc, but that's exactly what bpf_core_enum_value() exists for:
int cgrp_id = bpf_core_enum_value(enum cgroup_subsys_id___local, pids_cgrp_id___local);
I'd go with Kconfig + bpf_core_enum_value(), as it's shorter and nicer. This compiles and works with my Kconfig, but I haven't checked with CONFIG_CGROUP_PIDS defined.
(quoted diff) +0 -0
b/tools/testing/selftests/bpf/progs/profiler.inc.h index 00578311a423..79b8d2860a5c 100644
tools/testing/selftests/bpf/progs/profiler.inc.h +0 -0
@@ -243,7 +243,11 @@ static ino_t get_inode_from_kernfs(struct
kernfs_node* node)
}
}
-int pids_cgrp_id = 1;
+extern bool CONFIG_CGROUP_PIDS __kconfig __weak;
+
+enum cgroup_subsys_id___local {
+ pids_cgrp_id___local = 1, /* anything but zero */
+};
static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
struct task_struct* task,
@@ -253,7 +257,9 @@ static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
BPF_CORE_READ(task, nsproxy, cgroup_ns, root_cset,dfl_cgrp, kn);
struct kernfs_node* proc_kernfs = BPF_CORE_READ(task, cgroups,dfl_cgrp, kn);
- if (ENABLE_CGROUP_V1_RESOLVER) {
+ if (ENABLE_CGROUP_V1_RESOLVER && CONFIG_CGROUP_PIDS) {
+ int cgrp_id = bpf_core_enum_value(enumcgroup_subsys_id___local, pids_cgrp_id___local);
+
#ifdef UNROLL
#pragma unroll
#endif
@@ -262,7 +268,7 @@ static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
BPF_CORE_READ(task, cgroups, subsys[i]);
if (subsys != NULL) {
int subsys_id = BPF_CORE_READ(subsys, ss, id);- if (subsys_id == pids_cgrp_id) {
+ if (subsys_id == cgrp_id) {
proc_kernfs =BPF_CORE_READ(subsys, cgroup, kn);
root_kernfs =
BPF_CORE_READ(subsys, ss, root, kf_root, kn);
break;Song Liu
Tested-byRe: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 3 lines · Andrii Nakryiko
[...]
Quoted 4 lines
I'd go with
Kconfig+bpf_core_enum_value(), as it's shorter and nicer. This compiles and works with myKconfig, but I haven't checked withCONFIG_CGROUP_PIDSdefined.
Tested with CONFIG_CGROUP_PIDS, it looks good.
Quoted 48 lines
tools/testing/selftests/bpf/progs/profiler.inc.h +5
-1@@ -243,7 +243,11 @@ static ino_t get_inode_from_kernfs(struct kernfs_node* node) } } -int pids_cgrp_id =1 ; +extern bool CONFIG_CGROUP_PIDS __kconfig __weak; + +enum cgroup_subsys_id___local { + pids_cgrp_id___local =1 ,/* anything but zero */ +}; static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,struct task_struct* task,
tools/testing/selftests/bpf/progs/profiler.inc.h +2
-1@@ -253,7 +257,9 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,nsproxy ,cgroup_ns ,root_cset ,dfl_cgrp ,kn );struct kernfs_node * proc_kernfs =BPF_CORE_READ (task, cgroups, dfl_cgrp, kn); -if (ENABLE_CGROUP_V1_RESOLVER ) { +if (ENABLE_CGROUP_V1_RESOLVER &&CONFIG_CGROUP_PIDS ) { + int cgrp_id = bpf_core_enum_value(enum
cgroup_subsys_id___local,pids_cgrp_id___local);
+
#ifdef UNROLL
#pragma unroll
#endiftools/testing/selftests/bpf/progs/profiler.inc.h +1
-1@@ -262,7 +268,7 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,cgroups ,subsys [i]);if (subsys !=NULL ) {int subsys_id =BPF_CORE_READ (subsys, ss, id); -if (subsys_id == pids_cgrp_id) { +if (subsys_id == cgrp_id) { proc_kernfs =BPF_CORE_READ (subsys, cgroup, kn);
root_kernfs=BPF_CORE_READ(subsys, ss, root, kf_root, kn); break;
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 7 lines · Song Liu
Quoted 3 lines · Andrii Nakryiko
[...]
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
I'd go with
Kconfig+bpf_core_enum_value(), as it's shorter and nicer. This compiles and works with myKconfig, but I haven't checked withCONFIG_CGROUP_PIDSdefined.Tested with
CONFIG_CGROUP_PIDS, it looks good.Tested-by Song Liu <[email protected]>
hi,
I still need to apply my workaround to compile tests, so I wonder this fell through cracks
thanks,
jirka
Quoted 50 lines
Quoted 49 lines
tools/testing/selftests/bpf/progs/profiler.inc.h +5
-1@@ -243,7 +243,11 @@ static ino_t get_inode_from_kernfs(struct kernfs_node* node) } } -int pids_cgrp_id =1 ; +extern bool CONFIG_CGROUP_PIDS __kconfig __weak; + +enum cgroup_subsys_id___local { + pids_cgrp_id___local =1 ,/* anything but zero */ +}; static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,struct task_struct* task,
tools/testing/selftests/bpf/progs/profiler.inc.h +2
-1@@ -253,7 +257,9 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,nsproxy ,cgroup_ns ,root_cset ,dfl_cgrp ,kn );struct kernfs_node * proc_kernfs =BPF_CORE_READ (task, cgroups, dfl_cgrp, kn); -if (ENABLE_CGROUP_V1_RESOLVER ) { +if (ENABLE_CGROUP_V1_RESOLVER &&CONFIG_CGROUP_PIDS ) { + int cgrp_id = bpf_core_enum_value(enum
cgroup_subsys_id___local,pids_cgrp_id___local);
+
#ifdef UNROLL
#pragma unroll
#endiftools/testing/selftests/bpf/progs/profiler.inc.h +1
-1@@ -262,7 +268,7 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,cgroups ,subsys [i]);if (subsys !=NULL ) {int subsys_id =BPF_CORE_READ (subsys, ss, id); -if (subsys_id == pids_cgrp_id) { +if (subsys_id == cgrp_id) { proc_kernfs =BPF_CORE_READ (subsys, cgroup, kn);
root_kernfs=BPF_CORE_READ(subsys, ss, root, kf_root, kn); break;
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 13 lines · Jiri Olsa
Quoted 11 lines · Song Liu
Quoted 3 lines · Andrii Nakryiko
[...]
Quoted 4 lines
I'd go with
Kconfig+bpf_core_enum_value(), as it's shorter and nicer. This compiles and works with myKconfig, but I haven't checked withCONFIG_CGROUP_PIDSdefined.
Quoted 7 lines
Quoted 3 lines
Tested with
CONFIG_CGROUP_PIDS, it looks good.Tested-by Song Liu <[email protected]>hi,
I still need to apply my workaround to compile tests, so I wonder this fell through cracks
The fix was already applied ([0]). Do you still see issues?
[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Quoted 55 lines
thanks,
jirkaQuoted 50 lines
Quoted 49 lines
tools/testing/selftests/bpf/progs/profiler.inc.h +5
-1@@ -243,7 +243,11 @@ static ino_t get_inode_from_kernfs(struct kernfs_node* node) } } -int pids_cgrp_id =1 ; +extern bool CONFIG_CGROUP_PIDS __kconfig __weak; + +enum cgroup_subsys_id___local { + pids_cgrp_id___local =1 ,/* anything but zero */ +}; static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,struct task_struct* task,
tools/testing/selftests/bpf/progs/profiler.inc.h +2
-1@@ -253,7 +257,9 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,nsproxy ,cgroup_ns ,root_cset ,dfl_cgrp ,kn );struct kernfs_node * proc_kernfs =BPF_CORE_READ (task, cgroups, dfl_cgrp, kn); -if (ENABLE_CGROUP_V1_RESOLVER ) { +if (ENABLE_CGROUP_V1_RESOLVER &&CONFIG_CGROUP_PIDS ) { + int cgrp_id = bpf_core_enum_value(enum
cgroup_subsys_id___local,pids_cgrp_id___local);
+
#ifdef UNROLL
#pragma unroll
#endiftools/testing/selftests/bpf/progs/profiler.inc.h +1
-1@@ -262,7 +268,7 @@ static INLINE void* populate_cgroup_info(structcgroup_data_t * cgroup_data,BPF_CORE_READ (task ,cgroups ,subsys [i]);if (subsys !=NULL ) {int subsys_id =BPF_CORE_READ (subsys, ss, id); -if (subsys_id == pids_cgrp_id) { +if (subsys_id == cgrp_id) { proc_kernfs =BPF_CORE_READ (subsys, cgroup, kn);
root_kernfs=BPF_CORE_READ(subsys, ss, root, kf_root, kn); break;
Re: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
Quoted 18 lines · Andrii Nakryiko
Quoted 17 lines · Jiri Olsa
Quoted 14 lines · Song Liu
Quoted 3 lines · Andrii Nakryiko
[...]
Quoted 4 lines
I'd go with
Kconfig+bpf_core_enum_value(), as it's shorter and nicer. This compiles and works with myKconfig, but I haven't checked withCONFIG_CGROUP_PIDSdefined.Tested with
CONFIG_CGROUP_PIDS, it looks good.Tested-by Song Liu <[email protected]>
Quoted 8 lines
Quoted 3 lines
hi,
I still need to apply my workaround to compile tests, so I wonder this fell through cracksThe fix was already applied ([0]). Do you still see issues?
[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
nope, it's working now, I was on the wrong branch, sry for noise
thanks,
jirka
Song Liu
reviewerRe: [PATCH v2 bpf-next 3/4] selftests/bpf: Add profiler test
In reply to Jiri OlsaQuoted 44 lines · Jiri Olsa
On Thu, Oct 08, 2020 at 06:12:39PM -0700, Alexei Starovoitov wrote:
SNIP
Quoted 24 lines
(quoted diff) +24
-0+ +#ifdef UNROLL +#pragma unroll +#endif + for (int i = 0; i < MAX_CGROUPS_PATH_DEPTH; i++) { + filepart_length = + bpf_probe_read_str(payload, MAX_PATH, BPF_CORE_READ(cgroup_node, name)); + if (!cgroup_node) + return payload; + if (cgroup_node == cgroup_root_node) + *root_pos = payload - payload_start; + if (filepart_length <= MAX_PATH) { + barrier_var(filepart_length); + payload += filepart_length; + } + cgroup_node = BPF_CORE_READ(cgroup_node, parent); + } + return payload; +} + +static ino_t get_inode_from_kernfs(struct kernfs_node* node) +{ + struct kernfs_node___52* node52 = (void*)node; +Review
Quoted 10 lines
(quoted diff) +10
-0+ if (bpf_core_field_exists(node52->id.ino)) { + barrier_var(node52); + return BPF_CORE_READ(node52, id.ino); + } else { + barrier_var(node); + return (u64)BPF_CORE_READ(node, id); + } +} + +int pids_cgrp_id = 1;hi,
I'm getting compilation failure with this:
Quoted 13 lines
CLNG-LLC [
test_maps] profiler2.o
In file included fromprogs/profiler2.c:6:
progs/profiler.inc.h:246:5: error: redefinition of 'pids_cgrp_id' as different kind of symbol
intpids_cgrp_id= 1;
^
/home/jolsa/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:14531:2: note: previous definition is here
pids_cgrp_id= 11,
^
1 error generated.
/opt/clang/bin/llc: error:/opt/clang/bin/llc: <stdin>:1:1: error: expected top-level entity
BPF obj compilation failed
^
make: *** [Makefile:396:/home/jolsa/linux-qemu/tools/testing/selftests/bpf/profiler2.o] Error 1
I got the same error with some .config. We should fix it with something below.
I will send official patch soon.
Thanks,
Song
(quoted diff) +0 -0
w/tools/testing/selftests/bpf/progs/profiler.inc.h index 00578311a4233..54f594efccbca 100644
tools/testing/selftests/bpf/progs/profiler.inc.h +1 -1
@@ -243,7 +243,7 @@ static ino_t get_inode_from_kernfs(struct kernfs_node* node) } } -int pids_cgrp_id =1 ; +int pids_cgrp_id_ =1 ; static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data, struct task_struct* task,
@@ -262,7 +262,7 @@ static INLINE void* populate_cgroup_info(struct
cgroup_data_t* cgroup_data,
BPF_CORE_READ(task, cgroups, subsys[i]);
if (subsys != NULL) {
int subsys_id = BPF_CORE_READ(subsys, ss, id);- if (subsys_id == pids_cgrp_id) {
+ if (subsys_id == pids_cgrp_id_) {
proc_kernfs =BPF_CORE_READ(subsys, cgroup, kn);
root_kernfs =
BPF_CORE_READ(subsys, ss, root, kf_root, kn);
break;PATCH v2 4/4 selftests/bpf: Asm tests for the verifier regalloc tracking.
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
Add asm tests for register allocator tracking logic.
--- .../testing/selftests/bpf/verifier/regalloc.c | 243 ++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 tools/testing/selftests/bpf/verifier/regalloc.c
tools/testing/selftests/bpf/verifier/regalloc.c new file +243 -0
@@ -0,0 +1,243 @@ +{ + "regalloc basic", + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,8 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,20 ,4 ), +BPF_JMP_IMM (BPF_JSLT ,BPF_REG_2 ,0 ,3 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_0 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_2 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc negative" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,8 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,24 ,4 ), +BPF_JMP_IMM (BPF_JSLT ,BPF_REG_2 ,0 ,3 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_0 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_2 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = REJECT, + .errstr = "invalid access to map value, value_size=48 off=48 size=1", + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc src_reg mark" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,9 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,20 ,5 ), +BPF_MOV64_IMM (BPF_REG_3 ,0 ), +BPF_JMP_REG (BPF_JSGE ,BPF_REG_3 ,BPF_REG_2 ,3 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_0 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_2 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc src_reg negative" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,9 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,22 ,5 ), +BPF_MOV64_IMM (BPF_REG_3 ,0 ), +BPF_JMP_REG (BPF_JSGE ,BPF_REG_3 ,BPF_REG_2 ,3 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_0 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_2 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = REJECT, + .errstr = "invalid access to map value, value_size=48 off=44 size=8", + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc and spill" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,11 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,20 ,7 ), +/* r0 has upper bound that should propagate into r2 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_10 ,BPF_REG_2 ,-8 ),/* spill r2 */ +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_MOV64_IMM (BPF_REG_2 ,0 ),/* clear r0 and r2 */ +BPF_LDX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_10 ,-8 ),/* fill r3 */ +BPF_JMP_REG (BPF_JSGE ,BPF_REG_0 ,BPF_REG_3 ,2 ), +/* r3 has lower and upper bounds */ +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_3 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc and spill negative" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,11 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,48 ,7 ), +/* r0 has upper bound that should propagate into r2 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_10 ,BPF_REG_2 ,-8 ),/* spill r2 */ +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_MOV64_IMM (BPF_REG_2 ,0 ),/* clear r0 and r2 */ +BPF_LDX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_10 ,-8 ),/* fill r3 */ +BPF_JMP_REG (BPF_JSGE ,BPF_REG_0 ,BPF_REG_3 ,2 ), +/* r3 has lower and upper bounds */ +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_3 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = REJECT, + .errstr = "invalid access to map value, value_size=48 off=48 size=8", + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc three regs" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,10 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_4 ,BPF_REG_2 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_0 ,12 ,5 ), +BPF_JMP_IMM (BPF_JSLT ,BPF_REG_2 ,0 ,4 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_0 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_2 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_4 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc after call" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,10 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_8 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_9 ,BPF_REG_0 ), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,1 ,0 ,6 ), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_8 ,20 ,4 ), +BPF_JMP_IMM (BPF_JSLT ,BPF_REG_9 ,0 ,3 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_8 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_7 ,BPF_REG_9 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_7 ,0 ), +BPF_EXIT_INSN (), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +}, +{ +"regalloc in callee" , + .insns = { +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ), +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,6 ), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_get_prandom_u32), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_7 ), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,1 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_JMP_IMM (BPF_JSGT ,BPF_REG_1 ,20 ,5 ), +BPF_JMP_IMM (BPF_JSLT ,BPF_REG_2 ,0 ,4 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_3 ,BPF_REG_1 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_3 ,BPF_REG_2 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_3 ,0 ), +BPF_EXIT_INSN (), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_48b = { 4 }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACEPOINT, +},
Signature · 1 line
2.23.0
RE: [PATCH v2 bpf-next 4/4] selftests/bpf: Asm tests for the verifier regalloc tracking.
Quoted 11 lines · Alexei Starovoitov
From: Alexei Starovoitov <[email protected]>
Add asm tests for register allocator tracking logic.
Signed-off-by Alexei Starovoitov <[email protected]>Acked-by Andrii Nakryiko <[email protected]>--- .../testing/selftests/bpf/verifier/regalloc.c | 243 ++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 tools/testing/selftests/bpf/verifier/regalloc.c
I'm writing some extra tests now for a few things so I'll probably also add some to track the bounds for non-const through stack spill today or monday.
This series was applied to bpf/bpf-next.git (refs/heads/master):
Hello:
This series was applied to bpf/bpf-next.git (refs/heads/master):
Quoted 8 lines · On Thu, 8 Oct 2020 18:12:36 -0700 you wrote:
From: Alexei Starovoitov <[email protected]>
v1->v2:
- fixed 32-bit mov issue spotted by John.
- allowed r2=r1; r3=r2; sequence as suggested by John.
- added comments, acks, more tests.[...]
Here is the summary with links:
- [v2,bpf-next,1/4] bpf: Propagate scalar ranges through register assignments.
https://git.kernel.org/bpf/bpf-next/c/75748837b7e5 - [v2,bpf-next,2/4] bpf: Track spill/fill of bounded scalars. https://git.kernel.org/bpf/bpf-next/c/5689d49b71ad - [v2,bpf-next,3/4] selftests/bpf: Add profiler test https://git.kernel.org/bpf/bpf-next/c/03d4d13fab3f - [v2,bpf-next,4/4] selftests/bpf: Asm tests for the verifier regalloc tracking. https://git.kernel.org/bpf/bpf-next/c/54fada41e8a1
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html