Support for BPF_ST instruction in LLVM C compiler
Subsystems (6)
Eduard Zingerman
maintainercover Support for BPF_ST instruction in LLVM C compiler
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg + off) = src_regBPF_ST : *(size *) (dst_reg + off) = imm32
Currently LLVM BPF back-end does not emit BPF_ST instruction and does not allow one to be specified as inline assembly.
Recently I've been exploring ways to port some of the verifier test cases from tools/testing/selftests/bpf/verifier/*.c to use inline assembly and machinery provided in tools/testing/selftests/bpf/test_loader.c (which should hopefully simplify tests maintenance).
The BPF_ST instruction is popular in these tests: used in 52 of 94 files.
While it is possible to adjust LLVM to only support BPF_ST for inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emit BPF_ST instructions (source code is available here [1]). The changes include:
- update to verifier.c:check_stack_write_*() functions to track constant values spilled to stack via BPF_ST instruction in a same way stack spills of known registers by BPF_STX are tracked;
- updates to verifier.c:convert_ctx_access() and it's callbacks to handle BPF_ST instruction in a way similar to BPF_STX;
- some test adjustments and a few new tests.
With the above changes applied and LLVM from [1] all test_verifier, test_maps, test_progs and test_progs-no_alu32 test cases are passing.
When built using the LLVM version from [1] BPF programs generated for selftests and Cilium programs (taken from [2]) see certain reduction in size, e.g. below are total numbers of instructions for files with over 5K instructions:
File Insns Insns Insns Diff
w/o with diff pct
BPF_ST BPF_ST
cilium/bpf_host.o 44620 43774 -846 -1.90%
cilium/bpf_lxc.o 36842 36060 -782 -2.12%
cilium/bpf_overlay.o 23557 23186 -371 -1.57%
cilium/bpf_xdp.o 26397 25931 -466 -1.77%
selftests/core_kern.bpf.o 12359 12359 0 0.00%
selftests/linked_list_fail.bpf.o 5501 5302 -199 -3.62%
selftests/profiler1.bpf.o 17828 17709 -119 -0.67%
selftests/pyperf100.bpf.o 49793 49268 -525 -1.05%
selftests/pyperf180.bpf.o 88738 87813 -925 -1.04%
selftests/pyperf50.bpf.o 25388 25113 -275 -1.08%
selftests/pyperf600.bpf.o 78330 78300 -30 -0.04%
selftests/pyperf_global.bpf.o 5244 5188 -56 -1.07%
selftests/pyperf_subprogs.bpf.o 5262 5192 -70 -1.33%
selftests/strobemeta.bpf.o 17154 16065 -1089 -6.35%
selftests/test_verif_scale2.bpf.o 11337 11337 0 0.00%(Instructions are counted by counting the number of instruction lines
in disassembly).
Is community interested in this work?
Are there any omissions in my changes to the verifier?
Known issue:
There are two programs (one Cilium, one selftest) that exhibit anomalous increase in verifier processing time with this patch-set:
File Program Insns (A) Insns (B) Insns (DIFF) ------------------- ----------------------------- --------- --------- -------------- bpf_host.o tail_ipv6_host_policy_ingress 1576 2403 +827 (+52.47%) map_kptr.bpf.o test_map_kptr 400 475 +75 (+18.75%) ------------------- ----------------------------- --------- --------- --------------
These are under investigation.
Thanks,
Eduard
[1] https://reviews.llvm.org/D140804
[2] [email protected]:anakryiko/cilium.git
Eduard Zingerman (5):
bpf: more precise stack write reasoning for BPF_ST instruction selftests/bpf: check if verifier tracks constants spilled by
BPF_ST_MEM
bpf: allow ctx writes using BPF_ST_MEM instruction selftests/bpf: test if pointer type is tracked for BPF_ST_MEM selftests/bpf: don't match exact insn index in expected error message
kernel/bpf/cgroup.c | 49 +++++--- kernel/bpf/verifier.c | 102 +++++++++------- net/core/filter.c | 72 ++++++------ .../selftests/bpf/prog_tests/log_fixup.c | 2 +- .../selftests/bpf/prog_tests/spin_lock.c | 8 +- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++ tools/testing/selftests/bpf/verifier/ctx.c | 11 -- tools/testing/selftests/bpf/verifier/unpriv.c | 23 ++++ 9 files changed, 249 insertions(+), 157 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/bpf_st_mem.c
Signature · 1 line
2.39.0
Eduard Zingerman
maintainerRFC 1/5 bpf: more precise stack write reasoning for BPF_ST instruction
For aligned stack writes using BPF_ST instruction track stored values in a same way BPF_STX is handled, e.g. make sure that the following commands produce similar verifier knowledge:
fp[-8] = 42; r1 = 42;
fp[-8] = r1;This covers two cases:
- non-null values written to stack are stored as spill of fake registers;
- null values written to stack are stored as STACK_ZERO marks.
Previously both cases above used STACK_MISC marks instead.
Some verifier test cases relied on the old logic to obtain STACK_MISC marks for some stack values. These test cases are updated in the same commit to avoid failures during bisect.
--- kernel/bpf/verifier.c | 23 +++- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- 2 files changed, 84 insertions(+), 49 deletions(-)
kernel/bpf/verifier.c +20 -3
@@ -3257,6 +3257,11 @@ static void save_register_state(struct bpf_func_state *state,scrub_spilled_slot (&state->stack[spi].slot_type[i -1 ]); } +static bool is_bpf_st_mem (struct bpf_insn *insn) +{ +return BPF_CLASS (insn->code) ==BPF_ST &&BPF_MODE (insn->code) ==BPF_MEM ; +} +/* check_stack_{read,write}_fixed_off functions track spill/fill of registers, * stack boundary and alignment are checked in check_mem_access() */
@@ -3268,8 +3273,9 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env, {struct bpf_func_state *cur;/* state of the current function */ int i, slot = -off -1 , spi = slot /BPF_REG_SIZE , err; -u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; +struct bpf_insn *insn = &env->prog->insnsi[insn_idx];struct bpf_reg_state *reg =NULL ; +u32 dst_reg = insn->dst_reg; err =grow_stack_state (state,round_up (slot +1 ,BPF_REG_SIZE ));if (err)
@@ -3316,6 +3322,14 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,return err; }save_register_state (state, spi, reg, size); + }else if (!reg && !(off %BPF_REG_SIZE ) &&is_bpf_st_mem (insn) && + insn->imm !=0 && env->bpf_capable) { +struct bpf_reg_state fake_reg = {}; + +__mark_reg_known (&fake_reg, (u32 )insn->imm); + fake_reg.type =SCALAR_VALUE ; + fake_reg.parent = state->stack[spi].spilled_ptr.parent; +save_register_state (state, spi, &fake_reg, size); }else if (reg &&is_spillable_regtype (reg->type)) {/* register containing pointer is being spilled into stack */ if (size !=BPF_REG_SIZE ) {
@@ -3350,7 +3364,8 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env, state->stack[spi].spilled_ptr.live |=REG_LIVE_WRITTEN ;/* when we zero initialize stack slots mark them as such */ -if (reg &®ister_is_null (reg)) { +if ((reg &®ister_is_null (reg)) || + (!reg &&is_bpf_st_mem (insn) && insn->imm ==0 )) {/* backtracking doesn't work for STACK_ZERO yet. */ err =mark_chain_precision (env, value_regno);if (err)
@@ -3395,6 +3410,7 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,int min_off, max_off;int i, err;struct bpf_reg_state *ptr_reg =NULL , *value_reg =NULL ; +struct bpf_insn *insn = &env->prog->insnsi[insn_idx];bool writing_zero = false;/* set if the fact that we're writing a zero is used to let any * stack slots remain STACK_ZERO
@@ -3407,7 +3423,8 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env, max_off = ptr_reg->smax_value + off + size;if (value_regno >=0 ) value_reg = &cur->regs[value_regno]; -if (value_reg &®ister_is_null (value_reg)) +if ((value_reg &®ister_is_null (value_reg)) || + (!value_reg &&is_bpf_st_mem (insn) && insn->imm ==0 )) writing_zero = true; err =grow_stack_state (state,round_up (-min_off,BPF_REG_SIZE ));
tools/testing/selftests/bpf/verifier/bounds_mix_sign_unsign.c +64 -46
371 lines not on this page · raw message
Signature · 1 line
2.39.0
Eduard Zingerman
maintainerRFC 2/5 selftests/bpf: check if verifier tracks constants spilled by BPF_ST_MEM
In reply to Eduard ZingermanCheck that verifier tracks the value of 'imm' spilled to stack by BPF_ST_MEM instruction. Cover the following cases:
- write of non-zero constant to stack;
- write of a zero constant to stack.
--- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tools/testing/selftests/bpf/verifier/bpf_st_mem.c
tools/testing/selftests/bpf/verifier/bpf_st_mem.c new file +29 -0
@@ -0,0 +1,29 @@ +{ + "BPF_ST_MEM stack imm non-zero", + .insns = { +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,42 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_10 ,-8 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_0 ,-42 ), +/* if value is tracked correctly R0 is zero */ + BPF_EXIT_INSN(), + }, + .result =ACCEPT , +}, +{ +"BPF_ST_MEM stack imm zero" , + .insns = { +/* mark stack 0000 0000 */ +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), +/* read and sum a few bytes */ +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_10 ,-8 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_0 ,BPF_REG_1 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_10 ,-4 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_0 ,BPF_REG_1 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_10 ,-1 ), +BPF_ALU64_REG (BPF_ADD ,BPF_REG_0 ,BPF_REG_1 ), +/* if value is tracked correctly R0 is zero */ + BPF_EXIT_INSN(), + }, + .result = ACCEPT, +},
Signature · 1 line
2.39.0
Eduard Zingerman
maintainerRFC 3/5 bpf: allow ctx writes using BPF_ST_MEM instruction
In reply to Eduard ZingermanLift verifier restriction to use BPF_ST_MEM instructions to write to context data structures. This requires the following changes:
- verifier.c:do_check() for BPF_ST updated to:
- no longer forbid writes to registers of type PTR_TO_CTX;
- track dst_reg type in the env->insn_aux_data[...].ptr_type field (same way it is done for BPF_STX and BPF_LDX instructions).
- verifier.c:convert_ctx_access() and various callbacks invoked by it are updated to handle BPF_ST instruction alongside BPF_STX.
--- kernel/bpf/cgroup.c | 49 ++++++++------ kernel/bpf/verifier.c | 79 +++++++++++----------- net/core/filter.c | 72 ++++++++++---------- tools/testing/selftests/bpf/verifier/ctx.c | 11 --- 4 files changed, 108 insertions(+), 103 deletions(-)
kernel/bpf/cgroup.c +30 -19
@@ -2223,10 +2223,12 @@ static u32 sysctl_convert_ctx_access(enum bpf_access_type type, BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos), treg, si->dst_reg, offsetof(struct bpf_sysctl_kern, ppos)); - *insn++ =BPF_STX_MEM ( -BPF_SIZEOF (u32), treg, si->src_reg, + *insn++ =BPF_RAW_INSN ( +BPF_CLASS (si->code) |BPF_MEM |BPF_SIZEOF (u32), + treg, si->src_reg,bpf_ctx_narrow_access_offset ( -0 ,sizeof (u32),sizeof (loff_t))); +0 ,sizeof (u32),sizeof (loff_t)), + si->imm); *insn++ =BPF_LDX_MEM (BPF_DW , treg, si->dst_reg, offsetof(struct bpf_sysctl_kern , tmp_reg));
@@ -2376,10 +2378,17 @@ static bool cg_sockopt_is_valid_access(int off, int size,return true; } -#define CG_SOCKOPT_ACCESS_FIELD (T ,F ) \ - T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \ - si->dst_reg, si->src_reg, \ - offsetof(struct bpf_sockopt_kern, F)) +#define CG_SOCKOPT_READ_FIELD (F ) \ + BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \ + si->dst_reg, si->src_reg, \ + offsetof(struct bpf_sockopt_kern, F)) + +#define CG_SOCKOPT_WRITE_FIELD (F ) \ + BPF_RAW_INSN((BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F) | \ + BPF_MEM | BPF_CLASS(si->code)), \ + si->dst_reg, si->src_reg, \ + offsetof(struct bpf_sockopt_kern, F), \ + si->imm) static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type, const struct bpf_insn *si,
@@ -2391,25 +2400,25 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,switch (si->off) {case offsetof(struct bpf_sockopt , sk): - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , sk); + *insn++ =CG_SOCKOPT_READ_FIELD (sk);break ;case offsetof(struct bpf_sockopt , level):if (type ==BPF_WRITE ) - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_STX_MEM , level); + *insn++ =CG_SOCKOPT_WRITE_FIELD (level);else - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , level); + *insn++ =CG_SOCKOPT_READ_FIELD (level);break ;case offsetof(struct bpf_sockopt , optname):if (type ==BPF_WRITE ) - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_STX_MEM , optname); + *insn++ =CG_SOCKOPT_WRITE_FIELD (optname);else - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , optname); + *insn++ =CG_SOCKOPT_READ_FIELD (optname);break ;case offsetof(struct bpf_sockopt , optlen):if (type ==BPF_WRITE ) - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_STX_MEM , optlen); + *insn++ =CG_SOCKOPT_WRITE_FIELD (optlen);else - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , optlen); + *insn++ =CG_SOCKOPT_READ_FIELD (optlen);break ;case offsetof(struct bpf_sockopt , retval):BUILD_BUG_ON (offsetof(struct bpf_cg_run_ctx , run_ctx) !=0 );
@@ -2429,9 +2438,11 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type, *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx), treg, treg, offsetof(struct task_struct , bpf_ctx)); - *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval), - treg, si->src_reg, - offsetof(struct bpf_cg_run_ctx , retval)); + *insn++ =BPF_RAW_INSN (BPF_CLASS (si->code) |BPF_MEM | + BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval), + treg, si->src_reg, + offsetof(struct bpf_cg_run_ctx , retval), + si->imm); *insn++ =BPF_LDX_MEM (BPF_DW , treg, si->dst_reg, offsetof(struct bpf_sockopt_kern , tmp_reg)); }else {
@@ -2447,10 +2458,10 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type, }break ;case offsetof(struct bpf_sockopt , optval): - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , optval); + *insn++ =CG_SOCKOPT_READ_FIELD (optval);break ;case offsetof(struct bpf_sockopt , optval_end): - *insn++ =CG_SOCKOPT_ACCESS_FIELD (BPF_LDX_MEM , optval_end); + *insn++ =CG_SOCKOPT_READ_FIELD (optval_end);break ; }
kernel/bpf/verifier.c +40 -39
123 lines not on this page · raw message
net/core/filter.c +38 -34
161 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/ctx.c +0 -11
@@ -1,14 +1,3 @@ -{ - "context stores via ST", - .insns = { -BPF_MOV64_IMM (BPF_REG_0 ,0 ), -BPF_ST_MEM (BPF_DW ,BPF_REG_1 , offsetof(struct __sk_buff , mark),0 ), - BPF_EXIT_INSN(), - }, - .errstr ="BPF_ST stores into R1 ctx is not allowed" , - .result = REJECT, - .prog_type = BPF_PROG_TYPE_SCHED_CLS, -}, { "context stores via BPF_ATOMIC", .insns = {
Signature · 1 line
2.39.0
Eduard Zingerman
maintainerRFC 4/5 selftests/bpf: test if pointer type is tracked for BPF_ST_MEM
In reply to Eduard ZingermanCheck that verifier tracks pointer types for BPF_ST_MEM instructions and reports error if pointer types do not match for different execution branches.
--- tools/testing/selftests/bpf/verifier/unpriv.c | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/testing/selftests/bpf/verifier/unpriv.c +23 -0
@@ -239,6 +239,29 @@ .errstr = "same insn cannot be used with different pointers", .prog_type = BPF_PROG_TYPE_SCHED_CLS, }, +{ +/* Same as above, but use BPF_ST_MEM to save 42 +* instead of BPF_STX_MEM. +*/ + "unpriv: spill/fill of different pointers st", + .insns = { +BPF_ALU64_REG (BPF_MOV ,BPF_REG_6 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_6 ,-8 ), +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_1 ,0 ,3 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-16 ), +BPF_STX_MEM (BPF_DW ,BPF_REG_6 ,BPF_REG_2 ,0 ), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_1 ,0 ,1 ), +BPF_STX_MEM (BPF_DW ,BPF_REG_6 ,BPF_REG_1 ,0 ), +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_6 ,0 ), +BPF_ST_MEM (BPF_W ,BPF_REG_1 , offsetof(struct __sk_buff , mark),42 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .result =REJECT , + .errstr = "same insn cannot be used with different pointers", + .prog_type = BPF_PROG_TYPE_SCHED_CLS, +}, {"unpriv: spill/fill of different pointers stx - ctx and sock" , .insns = {
Signature · 1 line
2.39.0
Eduard Zingerman
maintainerRFC 5/5 selftests/bpf: don't match exact insn index in expected error message
In reply to Eduard ZingermanDepending on the behavior of the C compiler statements like below could be translated as 1 or 2 instructions:
C: int credit = 0;
BPF: *(u32 *)(r10 -4) = 0
- or -
r1 = 0
*(u32 *)(r10 -4) = r1This commit relaxes expected error messages for a few tests to avoid matching exact instruction number.
--- tools/testing/selftests/bpf/prog_tests/log_fixup.c | 2 +- tools/testing/selftests/bpf/prog_tests/spin_lock.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/log_fixup.c +1 -1
@@ -123,7 +123,7 @@ static void missing_map(void)ASSERT_FALSE (bpf_map__autocreate (skel->maps.missing_map),"missing_map_autocreate" );ASSERT_HAS_SUBSTR (log_buf, -"8: <invalid BPF map reference>\n" +": <invalid BPF map reference>\n" "BPF map 'missing_map' is referenced but wasn't created\n" ,"log_buf" );
tools/testing/selftests/bpf/prog_tests/spin_lock.c +4 -4
@@ -19,12 +19,12 @@ static struct {"; R1_w=map_value(off=0,ks=4,vs=4,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mapval_preserve", -"8: (bf) r1 = r0 ; R0_w=map_value(id=1,off=0,ks=4,vs=8,imm=0) " -"R1_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)\n9: (85) call bpf_this_cpu_ptr#154\n" +": (bf) r1 = r0 ; R0_w=map_value(id=1,off=0,ks=4,vs=8,imm=0) " +"R1_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)\n8: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_innermapval_preserve", -"13: (bf) r1 = r0 ; R0=map_value(id=2,off=0,ks=4,vs=8,imm=0) " -"R1_w=map_value(id=2,off=0,ks=4,vs=8,imm=0)\n14: (85) call bpf_this_cpu_ptr#154\n" +": (bf) r1 = r0 ; R0=map_value(id=2,off=0,ks=4,vs=8,imm=0) " +"R1_w=map_value(id=2,off=0,ks=4,vs=8,imm=0)\n13: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" }, { "lock_id_mismatch_kptr_global", "bpf_spin_unlock of different lock" },
Signature · 1 line
2.39.0
Quoted 15 lines · Eduard Zingerman
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.
Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).
Would we gate LLVM's emitting of BPF_ST for C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.
So I feel like the biggest benefit is to be able to use this instruction in embedded assembly, to make writing and maintaining tests easier.
Quoted 83 lines
The changes include:
- update to verifier.c:check_stack_write_*() functions to track constant values spilled to stack viaBPF_STinstruction in a same way stack spills of known registers byBPF_STXare tracked;
- updates to verifier.c:convert_ctx_access() and it's callbacks to handleBPF_STinstruction in a way similar toBPF_STX;
- some test adjustments and a few new tests.With the above changes applied and LLVM from [1] all
test_verifier,test_maps,test_progsand test_progs-no_alu32 test cases are passing.When built using the LLVM version from [1] BPF programs generated for selftests and Cilium programs (taken from [2]) see certain reduction in size, e.g. below are total numbers of instructions for files with over 5K instructions:
File Insns Insns Insns Diff w/o with diff pct BPF_ST BPF_ST cilium/bpf_host.o 44620 43774 -846 -1.90% cilium/bpf_lxc.o 36842 36060 -782 -2.12% cilium/bpf_overlay.o 23557 23186 -371 -1.57% cilium/bpf_xdp.o 26397 25931 -466 -1.77% selftests/core_kern.bpf.o 12359 12359 0 0.00% selftests/linked_list_fail.bpf.o 5501 5302 -199 -3.62% selftests/profiler1.bpf.o 17828 17709 -119 -0.67% selftests/pyperf100.bpf.o 49793 49268 -525 -1.05% selftests/pyperf180.bpf.o 88738 87813 -925 -1.04% selftests/pyperf50.bpf.o 25388 25113 -275 -1.08% selftests/pyperf600.bpf.o 78330 78300 -30 -0.04% selftests/pyperf_global.bpf.o 5244 5188 -56 -1.07% selftests/pyperf_subprogs.bpf.o 5262 5192 -70 -1.33% selftests/strobemeta.bpf.o 17154 16065 -1089 -6.35% selftests/test_verif_scale2.bpf.o 11337 11337 0 0.00%(Instructions are counted by counting the number of instruction lines
in disassembly).Is community interested in this work?
Are there any omissions in my changes to the verifier?Known issue:
There are two programs (one Cilium, one selftest) that exhibit anomalous increase in verifier processing time with this patch-set:
File Program Insns (A) Insns (B) Insns (DIFF) ------------------- ----------------------------- --------- --------- -------------- bpf_host.o tail_ipv6_host_policy_ingress 1576 2403 +827 (+52.47%) map_kptr.bpf.o test_map_kptr 400 475 +75 (+18.75%) ------------------- ----------------------------- --------- --------- --------------These are under investigation.
Thanks,
Eduard[1] https://reviews.llvm.org/D140804
[2] [email protected]:anakryiko/cilium.gitEduard Zingerman (5):
bpf: more precise stack write reasoning forBPF_STinstruction selftests/bpf: check if verifier tracks constants spilled by
BPF_ST_MEM
bpf: allow ctx writes usingBPF_ST_MEMinstruction selftests/bpf: test if pointer type is tracked forBPF_ST_MEMselftests/bpf: don't match exact insn index in expected error messagekernel/bpf/cgroup.c | 49 +++++--- kernel/bpf/verifier.c | 102 +++++++++------- net/core/filter.c | 72 ++++++------ .../selftests/bpf/prog_tests/log_fixup.c | 2 +- .../selftests/bpf/prog_tests/spin_lock.c | 8 +- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++ tools/testing/selftests/bpf/verifier/ctx.c | 11 -- tools/testing/selftests/bpf/verifier/unpriv.c | 23 ++++ 9 files changed, 249 insertions(+), 157 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/bpf_st_mem.c--
2.39.0
Quoted 22 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).
Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.
Hmm, GCC happily generates BPF_ST instructions:
$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s -
$ cat foo.s
.file "<stdin>"
.text
.align 3
.global foo
.type foo, @function
foo:
lddw %r0,v
stw [%r0+0],666
exit
.size foo, .-foo
.global v
.type v, @object
.lcomm v,4,4
.ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Quoted 87 lines
So I feel like the biggest benefit is to be able to use this instruction in embedded assembly, to make writing and maintaining tests easier.
Quoted 83 lines
The changes include:
- update to verifier.c:check_stack_write_*() functions to track constant values spilled to stack viaBPF_STinstruction in a same way stack spills of known registers byBPF_STXare tracked;
- updates to verifier.c:convert_ctx_access() and it's callbacks to handleBPF_STinstruction in a way similar toBPF_STX;
- some test adjustments and a few new tests.With the above changes applied and LLVM from [1] all
test_verifier,test_maps,test_progsand test_progs-no_alu32 test cases are passing.When built using the LLVM version from [1] BPF programs generated for selftests and Cilium programs (taken from [2]) see certain reduction in size, e.g. below are total numbers of instructions for files with over 5K instructions:
File Insns Insns Insns Diff w/o with diff pct BPF_ST BPF_ST cilium/bpf_host.o 44620 43774 -846 -1.90% cilium/bpf_lxc.o 36842 36060 -782 -2.12% cilium/bpf_overlay.o 23557 23186 -371 -1.57% cilium/bpf_xdp.o 26397 25931 -466 -1.77% selftests/core_kern.bpf.o 12359 12359 0 0.00% selftests/linked_list_fail.bpf.o 5501 5302 -199 -3.62% selftests/profiler1.bpf.o 17828 17709 -119 -0.67% selftests/pyperf100.bpf.o 49793 49268 -525 -1.05% selftests/pyperf180.bpf.o 88738 87813 -925 -1.04% selftests/pyperf50.bpf.o 25388 25113 -275 -1.08% selftests/pyperf600.bpf.o 78330 78300 -30 -0.04% selftests/pyperf_global.bpf.o 5244 5188 -56 -1.07% selftests/pyperf_subprogs.bpf.o 5262 5192 -70 -1.33% selftests/strobemeta.bpf.o 17154 16065 -1089 -6.35% selftests/test_verif_scale2.bpf.o 11337 11337 0 0.00%(Instructions are counted by counting the number of instruction lines
in disassembly).Is community interested in this work?
Are there any omissions in my changes to the verifier?Known issue:
There are two programs (one Cilium, one selftest) that exhibit anomalous increase in verifier processing time with this patch-set:
File Program Insns (A) Insns (B) Insns (DIFF) ------------------- ----------------------------- --------- --------- -------------- bpf_host.o tail_ipv6_host_policy_ingress 1576 2403 +827 (+52.47%) map_kptr.bpf.o test_map_kptr 400 475 +75 (+18.75%) ------------------- ----------------------------- --------- --------- --------------These are under investigation.
Thanks,
Eduard[1] https://reviews.llvm.org/D140804
[2] [email protected]:anakryiko/cilium.gitEduard Zingerman (5):
bpf: more precise stack write reasoning forBPF_STinstruction selftests/bpf: check if verifier tracks constants spilled by
BPF_ST_MEM
bpf: allow ctx writes usingBPF_ST_MEMinstruction selftests/bpf: test if pointer type is tracked forBPF_ST_MEMselftests/bpf: don't match exact insn index in expected error messagekernel/bpf/cgroup.c | 49 +++++--- kernel/bpf/verifier.c | 102 +++++++++------- net/core/filter.c | 72 ++++++------ .../selftests/bpf/prog_tests/log_fixup.c | 2 +- .../selftests/bpf/prog_tests/spin_lock.c | 8 +- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++ tools/testing/selftests/bpf/verifier/ctx.c | 11 -- tools/testing/selftests/bpf/verifier/unpriv.c | 23 ++++ 9 files changed, 249 insertions(+), 157 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/bpf_st_mem.c--
2.39.0
Eduard Zingerman
maintainerQuoted 47 lines · Jose E. Marchesi
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"
Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when BPF_ST instruction is allowed for selection:
- patch #1 in this patchset: track values of constants written to stack using BPF_ST. Currently these are tracked imprecisely, unlike the writes using BPF_STX, e.g.:
fp[-8] = 42; currently verifier assumes that fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].
r1 = 42; verifier tracks r1=42 after this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.
So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts verifier.c:convert_ctx_access() to operate on BPF_ST alongside BPF_STX.
Context parameters for some BPF programs types are "fake" data structures. The verifier matches all BPF_STX and BPF_LDX instructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).
So the patch extends this logic to also handle BPF_ST.
Quoted 88 lines
Quoted 88 lines
So I feel like the biggest benefit is to be able to use this instruction in embedded assembly, to make writing and maintaining tests easier.
Quoted 83 lines
The changes include:
- update to verifier.c:check_stack_write_*() functions to track constant values spilled to stack viaBPF_STinstruction in a same way stack spills of known registers byBPF_STXare tracked;
- updates to verifier.c:convert_ctx_access() and it's callbacks to handleBPF_STinstruction in a way similar toBPF_STX;
- some test adjustments and a few new tests.With the above changes applied and LLVM from [1] all
test_verifier,test_maps,test_progsand test_progs-no_alu32 test cases are passing.When built using the LLVM version from [1] BPF programs generated for selftests and Cilium programs (taken from [2]) see certain reduction in size, e.g. below are total numbers of instructions for files with over 5K instructions:
File Insns Insns Insns Diff
w/o with diff pct
BPF_STBPF_STcilium/bpf_host.o 44620 43774 -846 -1.90% cilium/bpf_lxc.o 36842 36060 -782 -2.12% cilium/bpf_overlay.o 23557 23186 -371 -1.57% cilium/bpf_xdp.o 26397 25931 -466 -1.77% selftests/core_kern.bpf.o 12359 12359 0 0.00% selftests/linked_list_fail.bpf.o 5501 5302 -199 -3.62% selftests/profiler1.bpf.o 17828 17709 -119 -0.67% selftests/pyperf100.bpf.o 49793 49268 -525 -1.05% selftests/pyperf180.bpf.o 88738 87813 -925 -1.04% selftests/pyperf50.bpf.o 25388 25113 -275 -1.08% selftests/pyperf600.bpf.o 78330 78300 -30 -0.04% selftests/pyperf_global.bpf.o 5244 5188 -56 -1.07% selftests/pyperf_subprogs.bpf.o 5262 5192 -70 -1.33% selftests/strobemeta.bpf.o 17154 16065 -1089 -6.35% selftests/test_verif_scale2.bpf.o 11337 11337 0 0.00%(Instructions are counted by counting the number of instruction lines in disassembly).
Is community interested in this work?
Are there any omissions in my changes to the verifier?Known issue:
There are two programs (one Cilium, one selftest) that exhibit anomalous increase in verifier processing time with this patch-set:
File Program Insns (A) Insns (B) Insns (DIFF) ------------------- ----------------------------- --------- --------- -------------- bpf_host.o tail_ipv6_host_policy_ingress 1576 2403 +827 (+52.47%) map_kptr.bpf.o test_map_kptr 400 475 +75 (+18.75%) ------------------- ----------------------------- --------- --------- --------------These are under investigation.
Thanks,
Eduard[1] https://reviews.llvm.org/D140804
[2] [email protected]:anakryiko/cilium.gitEduard Zingerman (5):
bpf: more precise stack write reasoning forBPF_STinstruction selftests/bpf: check if verifier tracks constants spilled byBPF_ST_MEM
bpf: allow ctx writes usingBPF_ST_MEMinstruction selftests/bpf: test if pointer type is tracked forBPF_ST_MEMselftests/bpf: don't match exact insn index in expected error messagekernel/bpf/cgroup.c | 49 +++++--- kernel/bpf/verifier.c | 102 +++++++++------- net/core/filter.c | 72 ++++++------ .../selftests/bpf/prog_tests/log_fixup.c | 2 +- .../selftests/bpf/prog_tests/spin_lock.c | 8 +- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++ tools/testing/selftests/bpf/verifier/ctx.c | 11 -- tools/testing/selftests/bpf/verifier/unpriv.c | 23 ++++ 9 files changed, 249 insertions(+), 157 deletions(-)create mode 100644
tools/testing/selftests/bpf/verifier/bpf_st_mem.c--
2.39.0
Quoted 49 lines · Jose E. Marchesi
Quoted 47 lines
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"
Quoted 8 lines
Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
We are still working on other issues in GCC; we are not yet in par with clang when it comes to build/run the BPF selftests. So I guess we didn't reach that particular breaking point yet ;)
Quoted 2 lines
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:
So, if the current state of affairs is that the kernel rejects BPF_ST instructions, we shouldn't be generating them from GCC.
I like Andrii's idea of introducing a -mcpu=v4 and condition generation of BPF_ST from the compiler to it. GCC is defaulting to -mcpu=v3 atm.
Yonghong, WDYT? Would that be acceptable for clang as well?
Quoted 115 lines
- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:fp[-8] = 42; currently verifier assumes that fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].r1 = 42; verifier tracks r1=42 after this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts verifier.c:convert_ctx_access() to operate on
BPF_STalongsideBPF_STX.Context parameters for some BPF programs types are "fake" data structures. The verifier matches all
BPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).So the patch extends this logic to also handle
BPF_ST.Quoted 88 lines
Quoted 88 lines
So I feel like the biggest benefit is to be able to use this instruction in embedded assembly, to make writing and maintaining tests easier.
Quoted 83 lines
The changes include:
- update to verifier.c:check_stack_write_*() functions to track constant values spilled to stack viaBPF_STinstruction in a same way stack spills of known registers byBPF_STXare tracked;
- updates to verifier.c:convert_ctx_access() and it's callbacks to handleBPF_STinstruction in a way similar toBPF_STX;
- some test adjustments and a few new tests.With the above changes applied and LLVM from [1] all
test_verifier,test_maps,test_progsand test_progs-no_alu32 test cases are passing.When built using the LLVM version from [1] BPF programs generated for selftests and Cilium programs (taken from [2]) see certain reduction in size, e.g. below are total numbers of instructions for files with over 5K instructions:
File Insns Insns Insns Diff
w/o with diff pct
BPF_STBPF_STcilium/bpf_host.o 44620 43774 -846 -1.90% cilium/bpf_lxc.o 36842 36060 -782 -2.12% cilium/bpf_overlay.o 23557 23186 -371 -1.57% cilium/bpf_xdp.o 26397 25931 -466 -1.77% selftests/core_kern.bpf.o 12359 12359 0 0.00% selftests/linked_list_fail.bpf.o 5501 5302 -199 -3.62% selftests/profiler1.bpf.o 17828 17709 -119 -0.67% selftests/pyperf100.bpf.o 49793 49268 -525 -1.05% selftests/pyperf180.bpf.o 88738 87813 -925 -1.04% selftests/pyperf50.bpf.o 25388 25113 -275 -1.08% selftests/pyperf600.bpf.o 78330 78300 -30 -0.04% selftests/pyperf_global.bpf.o 5244 5188 -56 -1.07% selftests/pyperf_subprogs.bpf.o 5262 5192 -70 -1.33% selftests/strobemeta.bpf.o 17154 16065 -1089 -6.35% selftests/test_verif_scale2.bpf.o 11337 11337 0 0.00%(Instructions are counted by counting the number of instruction lines in disassembly).
Is community interested in this work?
Are there any omissions in my changes to the verifier?Known issue:
There are two programs (one Cilium, one selftest) that exhibit anomalous increase in verifier processing time with this patch-set:
File Program Insns (A) Insns (B) Insns (DIFF) ------------------- ----------------------------- --------- --------- -------------- bpf_host.o tail_ipv6_host_policy_ingress 1576 2403 +827 (+52.47%) map_kptr.bpf.o test_map_kptr 400 475 +75 (+18.75%) ------------------- ----------------------------- --------- --------- --------------These are under investigation.
Thanks,
Eduard[1] https://reviews.llvm.org/D140804
[2] [email protected]:anakryiko/cilium.gitEduard Zingerman (5):
bpf: more precise stack write reasoning forBPF_STinstruction selftests/bpf: check if verifier tracks constants spilled byBPF_ST_MEM
bpf: allow ctx writes usingBPF_ST_MEMinstruction selftests/bpf: test if pointer type is tracked forBPF_ST_MEMselftests/bpf: don't match exact insn index in expected error messagekernel/bpf/cgroup.c | 49 +++++--- kernel/bpf/verifier.c | 102 +++++++++------- net/core/filter.c | 72 ++++++------ .../selftests/bpf/prog_tests/log_fixup.c | 2 +- .../selftests/bpf/prog_tests/spin_lock.c | 8 +- .../bpf/verifier/bounds_mix_sign_unsign.c | 110 ++++++++++-------- .../selftests/bpf/verifier/bpf_st_mem.c | 29 +++++ tools/testing/selftests/bpf/verifier/ctx.c | 11 -- tools/testing/selftests/bpf/verifier/unpriv.c | 23 ++++ 9 files changed, 249 insertions(+), 157 deletions(-)create mode 100644
tools/testing/selftests/bpf/verifier/bpf_st_mem.c--
2.39.0
Quoted 84 lines · Eduard Zingerman
Quoted 47 lines · Jose E. Marchesi
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:fp[-8] = 42; currently verifier assumes that fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].r1 = 42; verifier tracks r1=42 after this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts verifier.c:convert_ctx_access() to operate on
BPF_STalongsideBPF_STX.Context parameters for some BPF programs types are "fake" data structures. The verifier matches all
BPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).
Quoted 1 line
So the patch extends this logic to also handle
BPF_ST.
The patch 3 is necessary to land before llvm starts generating 'st' for ctx access. That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ?
Regarind -mcpu=v4.
I think we need to add all of our upcoming instructions as a single flag. Otherwise we'll have -mcpu=v5,v6,v7 and full combinations of them.
-mcpu=v4 could mean:
- ST
- sign extending loads
- sign extend a register
- 32-bit JA
- proper bswap insns: bswap16, bswap32, bswap64
The sign and 32-bit JA we've discussed earlier.
The bswap was on my wish list forever.
The existing TO_LE, TO_BE insns are really odd from compiler pov. The compiler should translate bswap IR op into proper bswap insn just like it does on all cpus.
Maybe add SDIV to -mcpu=v4 as well?
Quoted 104 lines · Alexei Starovoitov
Quoted 84 lines · Eduard Zingerman
Quoted 47 lines · Jose E. Marchesi
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:fp[-8] = 42; currently verifier assumes that fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].r1 = 42; verifier tracks r1=42 after this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts verifier.c:convert_ctx_access() to operate on
BPF_STalongsideBPF_STX.Context parameters for some BPF programs types are "fake" data structures. The verifier matches all
BPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).Quoted 1 line
So the patch extends this logic to also handle
BPF_ST.The patch 3 is necessary to land before llvm starts generating 'st' for ctx access. That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ?Regarind -mcpu=v4.
I think we need to add all of our upcoming instructions as a single flag. Otherwise we'll have -mcpu=v5,v6,v7 and full combinations of them.-mcpu=v4 could mean:
- ST
- sign extending loads
- sign extend a register
- 32-bit JA
- proper bswap insns: bswap16, bswap32, bswap64
Quoted 7 lines
The sign and 32-bit JA we've discussed earlier.
The bswap was on my wish list forever.
The existingTO_LE,TO_BEinsns are really odd from compiler pov. The compiler should translate bswap IR op into proper bswap insn just like it does on all cpus.Maybe add SDIV to -mcpu=v4 as well?
Right, we should add these insns in llvm17 with -mcpu=v4, so we can keep the number of cpu generations minimum.
Quoted 109 lines · Alexei Starovoitov
Quoted 98 lines · Eduard Zingerman
Quoted 71 lines · Jose E. Marchesi
Quoted 47 lines
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:
fp[-8] = 42; currently verifier assumes that
fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].
r1 = 42; verifier tracks r1=42 after
this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.Quoted 13 lines
So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts
verifier.c:convert_ctx_access()
to operate onBPF_STalongsideBPF_STX.
Context parameters for some BPF programs types are "fake"
data
structures. The verifier matches allBPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).
So the patch extends this logic to also handleBPF_ST.The patch 3 is necessary to land before llvm starts generating 'st' for ctx access.
That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ? Regarind -mcpu=v4.
I think we need to add all of our upcoming instructions as a single flag. Otherwise we'll have -mcpu=v5,v6,v7 and full combinations of them. -mcpu=v4 could mean:
- ST
- sign extending loads
- sign extend a registerQuoted 8 lines
- 32-bit JA
- proper bswap insns: bswap16, bswap32, bswap64
The sign and 32-bit JA we've discussed earlier.
The bswap was on my wish list forever.
The existingTO_LE,TO_BEinsns are really odd from compiler pov. The compiler should translate bswap IR op into proper bswap insn just like it does on all cpus.
Maybe add SDIV to -mcpu=v4 as well?
Quoted 2 lines
Right, we should add these insns in llvm17 with -mcpu=v4, so we can keep the number of cpu generations minimum.
How do you plan to encode the sign-extend load instructions?
I guess a possibility would be to use one of the available op-mode for load instructions that are currently marked as reserved. For example:
IMM = 0b000 ABS = 0b001 IND = 0b010 MEM = 0b011
SEM = 0b100 <- new
Then we would have the following code fields for sign-extending LDX instructions:
op-mode:SEM op-size:{W,H,B,DW} op-class:LDX
Eduard Zingerman
maintainerQuoted 86 lines · Jose E. Marchesi
Quoted 86 lines · Alexei Starovoitov
Quoted 85 lines · Eduard Zingerman
Quoted 84 lines · Jose E. Marchesi
Quoted 47 lines
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:
fp[-8] = 42; currently verifier assumes that
fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].
r1 = 42; verifier tracks r1=42 after
this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts verifier.c:convert_ctx_access()
to operate onBPF_STalongsideBPF_STX.
Context parameters for some BPF programs types are "fake" data
structures. The verifier matches allBPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant offsets inside such structures).
So the patch extends this logic to also handleBPF_ST.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
The patch 3 is necessary to land before llvm starts generating 'st' for ctx access.
That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ?
I should have added an example to the summary. There are a few test_prog tests that fail w/o this patch, namely atomic_bounds, dynptr, for_each, xdp_noinline, xdp_synproxy.
Here is how atomic_bounds looks:
SEC("fentry/bpf_fentry_test1")
int BPF_PROG(sub, int x)
{
int a = 0;
int b = __sync_fetch_and_add(&a, 1);
/* b is certainly 0 here. Can the verifier tell? */
while (b)
continue;
return 0;
} Compiled w/o BPF_ST Compiled with BPF_ST
<sub>: <sub>:
w1 = 0x0
*(u32 *)(r10 - 0x4) = r1 *(u32 *)(r10 - 0x4) = 0x0
w1 = 0x1 w1 = 0x1
lock *(u32 *)(r10 - 0x4) += r1 lock *(u32 *)(r10 - 0x4) += r1
if w1 == 0x0 goto +0x1 <LBB0_2> if w1 == 0x0 goto +0x1 <LBB0_2> <LBB0_1>: <LBB0_1>:
goto -0x1 <LBB0_1> goto -0x1 <LBB0_1>
<LBB0_2>: <LBB0_2>:
w0 = 0x0 w0 = 0x0
exit exitWhen compiled with BPF_ST and verified w/o the patch #1 verification log looks as follows:
0: R1=ctx(off=0,imm=0) R10=fp0
0: (62) *(u32 *)(r10 -4) = 0 ; R10=fp0 fp-8=mmmm????
1: (b4) w1 = 1 ; R1_w=1
2: (c3) r1 = atomic_fetch_add((u32 *)(r10 -4), r1) ; R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) R10=fp0 fp-8=mmmm????
3: (16) if w1 == 0x0 goto pc+1 ; R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff))
4: (05) goto pc-1
4: (05) goto pc-1
4: (05) goto pc-1
4: (05) goto pc-1
infinite loop detected at insn 4
When compiled w/o BPF_ST and verified w/o the patch #1 verification log looks as follows:
func#0 @0
reg type unsupported for arg#0 function sub#5
0: R1=ctx(off=0,imm=0) R10=fp0
0: (b4) w1 = 0 ; R1_w=0
1: (63) *(u32 *)(r10 -4) = r1
last_idx 1 first_idx 0
regs=2 stack=0 before 0: (b4) w1 = 0
2: R1_w=0 R10=fp0 fp-8=0000????
2: (b4) w1 = 1 ; R1_w=1
; int b = __sync_fetch_and_add(&a, 1);
3: (c3) r1 = atomic_fetch_add((u32 *)(r10 -4), r1) ; R1_w=P0 R10=fp0 fp-8=mmmm????
4: (16) if w1 == 0x0 goto pc+1
6: R1_w=P0
6: (b4) w0 = 0 ; R0_w=0
7: (95) exit
processed 7 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
The difference comes from the way zero write to r10-4 is processed, with BPF_ST it is tracked as fp-8=mmmm???? after write, without BPF_ST it is tracked as fp-8=0000???? after write.
Which is caused by the way check_stack_write_fixed_off() is written. For the register spills it either saves the complete register state or STACK_ZERO if register is known to be zero. However, for the BPF_ST it just saves STACK_MISC. Hence, the patch #1.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Regarind -mcpu=v4.
I think we need to add all of our upcoming instructions as a single flag. Otherwise we'll have -mcpu=v5,v6,v7 and full combinations of them. -mcpu=v4 could mean:
- ST
- sign extending loads
- sign extend a register
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
- 32-bit JA
- proper bswap insns: bswap16, bswap32, bswap64
The sign and 32-bit JA we've discussed earlier.
The bswap was on my wish list forever.
The existingTO_LE,TO_BEinsns are really odd from compiler pov. The compiler should translate bswap IR op into proper bswap insn just like it does on all cpus.
Maybe add SDIV to -mcpu=v4 as well?
There is also BPF_JSET "PC += off if dst & src" which is not currently emitted by LLVM backend as far as I can tell.
Quoted 13 lines
Quoted 3 lines
Right, we should add these insns in llvm17 with -mcpu=v4, so we can keep the number of cpu generations minimum.
How do you plan to encode the sign-extend load instructions?
I guess a possibility would be to use one of the available op-mode for load instructions that are currently marked as reserved. For example:
IMM = 0b000 ABS = 0b001 IND = 0b010 MEM = 0b011
Quoted 6 lines
SEM = 0b100 <- new
Then we would have the following code fields for sign-extending LDX instructions:
op-mode:SEM op-size:{W,H,B,DW} op-class:LDX
I second the Jose's question about encoding for the new instructions.
Quoted 1 line
- sign extend a register
E.g. like this:
BPF_SEXT = 0xb0
opcode: BPF_SEXT | BPF_X | BPF_ALU
imm32: {8,16,32} // to be consistent with BPF_END insn
or BPF_{B,H,W} // to be consistent with LDX/STX
Sign extend 8,16,32-bit value from src to 64-bit dst register:
src = sext{8,16,32}(dst)
Quoted 1 line
The sign and 32-bit JA we've discussed earlier.
Could you please share a link for this discussion?
Quoted 1 line
- proper bswap insns: bswap16, bswap32, bswap64
Could you please extrapolate on this.
Current instructions operate on a single register, e.g.:
dst_reg = htole16(dst_reg).
It looks like this could be reflected in x86 assembly as a single instruction using either bswap or xchg instructions (see [1] and [2]). x86/net/bpf_jit_comp.c:1266 can probably be adjusted to use xchg for 16-bit case.
For ARM rev16, rev32, rev64 allow to use the same register for source and destination ([3]).
[1] https://www.felixcloutier.com/x86/bswap
[2] https://www.felixcloutier.com/x86/xchg
[3] https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/REV16--Reverse-bytes-in-16-bit-halfwords-?lang=en
https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/REV32--Reverse-bytes-in-32-bit-words-?lang=en https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/REV64--Reverse-Bytes--an-alias-of-REV-?lang=en
Quoted 75 lines · Eduard Zingerman
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
So the patch extends this logic to also handle
BPF_ST.
The patch 3 is necessary to land before llvm starts generating 'st' for ctx access.
That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ?I should have added an example to the summary. There are a few
test_progtests that fail w/o this patch, namelyatomic_bounds, dynptr,for_each,xdp_noinline,xdp_synproxy.Here is how
atomic_boundslooks:SEC("fentry/bpf_fentry_test1") int BPF_PROG(sub, int x) { int a = 0; int b = __sync_fetch_and_add(&a, 1); /* b is certainly 0 here. Can the verifier tell? */ while (b) continue; return 0; }Compiled w/o
BPF_STCompiled withBPF_ST<sub>: <sub>: w1 = 0x0 *(u32 *)(r10 - 0x4) = r1 *(u32 *)(r10 - 0x4) = 0x0 w1 = 0x1 w1 = 0x1 lock *(u32 *)(r10 - 0x4) += r1 lock *(u32 *)(r10 - 0x4) += r1 if w1 == 0x0 goto +0x1 <LBB0_2> if w1 == 0x0 goto +0x1 <LBB0_2> <LBB0_1>: <LBB0_1>: goto -0x1 <LBB0_1> goto -0x1 <LBB0_1> <LBB0_2>: <LBB0_2>: w0 = 0x0 w0 = 0x0 exit exitWhen compiled with
BPF_STand verified w/o the patch #1 verification log looks as follows:0: R1=ctx(off=0,imm=0) R10=fp0 0: (62) *(u32 *)(r10 -4) = 0 ; R10=fp0 fp-8=mmmm???? 1: (b4) w1 = 1 ; R1_w=1 2: (c3) r1 = atomic_fetch_add((u32 *)(r10 -4), r1) ; R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) R10=fp0 fp-8=mmmm???? 3: (16) if w1 == 0x0 goto pc+1 ; R1_w=scalar(umax=4294967295,var_off=(0x0; 0xffffffff)) 4: (05) goto pc-1 4: (05) goto pc-1 4: (05) goto pc-1 4: (05) goto pc-1 infinite loop detected at insn 4When compiled w/o
BPF_STand verified w/o the patch #1 verification log looks as follows:func#0 @0 reg type unsupported for arg#0 function sub#5 0: R1=ctx(off=0,imm=0) R10=fp0 0: (b4) w1 = 0 ; R1_w=0 1: (63) *(u32 *)(r10 -4) = r1 last_idx 1 first_idx 0 regs=2 stack=0 before 0: (b4) w1 = 0 2: R1_w=0 R10=fp0 fp-8=0000???? 2: (b4) w1 = 1 ; R1_w=1 ; int b = __sync_fetch_and_add(&a, 1); 3: (c3) r1 = atomic_fetch_add((u32 *)(r10 -4), r1) ; R1_w=P0 R10=fp0 fp-8=mmmm???? 4: (16) if w1 == 0x0 goto pc+1 6: R1_w=P0 6: (b4) w0 = 0 ; R0_w=0 7: (95) exit processed 7 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
Quoted 8 lines
The difference comes from the way zero write to
r10-4is processed, withBPF_STit is tracked asfp-8=mmmm????after write, withoutBPF_STit is tracked asfp-8=0000???? afterwrite.Which is caused by the way
check_stack_write_fixed_off()is written. For the register spills it either saves the complete register state orSTACK_ZEROif register is known to be zero. However, for theBPF_STit just savesSTACK_MISC. Hence, the patch #1.
Thank you for explaining.
Though llvm changes are not ready, let's land the required kernel changes now.
Please craft asm test cases for the issues you've seen with BPF_ST.
Quoted 123 lines · Jose E. Marchesi
Quoted 109 lines · Alexei Starovoitov
Quoted 98 lines · Eduard Zingerman
Quoted 71 lines · Jose E. Marchesi
Quoted 47 lines
Quoted 21 lines · Eduard Zingerman
Quoted 15 lines
BPF has two documented (non-atomic) memory store instructions:
BPF_STX: *(size *) (dst_reg+ off) =src_regBPF_ST: *(size *) (dst_reg+ off) = imm32Currently LLVM BPF back-end does not emit
BPF_STinstruction and does not allow one to be specified as inline assembly.Recently I've been exploring ways to port some of the verifier test cases from
tools/testing/selftests/bpf/verifier/*.cto use inline assembly and machinery provided intools/testing/selftests/bpf/test_loader.c(which should hopefully simplify tests maintenance).
TheBPF_STinstruction is popular in these tests: used in 52 of 94 files.Quoted 4 lines
While it is possible to adjust LLVM to only support
BPF_STfor inline assembly blocks it seems a bit wasteful. This patch-set contains a set of changes to verifier necessary in case when LLVM is allowed to freely emitBPF_STinstructions (source code is available here [1]).Quoted 6 lines
Would we gate LLVM's emitting of
BPF_STfor C code behind some new cpu=v4? What is the benefit for compiler to start automatically emit such instructions? Such thinking about logistics, if there isn't much benefit, as BPF application owner I wouldn't bother enabling this behavior risking regressions on old kernels that don't have these changes.Hmm, GCC happily generates
BPF_STinstructions:$ echo 'int v; void foo () { v = 666; }' | bpf-unknown-none-gcc -O2 -xc -S -o foo.s - $ cat foo.s .file "<stdin>" .text .align 3 .global foo .type foo, @function foo: lddw %r0,v stw [%r0+0],666 exit .size foo, .-foo .global v .type v, @object .lcomm v,4,4 .ident "GCC: (GNU) 12.0.0 20211206 (experimental)"Quoted 5 lines
Been doing that since October 2019, I think before the cpu versioning mechanism was got in place?
We weren't aware this was problematic. Does the verifier reject such instructions?
Interesting, do BPF selftests generated by GCC pass the same way they do if generated by clang?
I had to do the following changes to the verifier to make the selftests pass when
BPF_STinstruction is allowed for selection:- patch #1 in this patchset: track values of constants written to stack using
BPF_ST. Currently these are tracked imprecisely, unlike the writes usingBPF_STX, e.g.:
fp[-8] = 42; currently verifier assumes that
fp[-8]=mmmmmmmm
after such instruction, where m stands for "misc", just a note that something is written at fp[-8].
r1 = 42; verifier tracks r1=42 after
this instruction.
fp[-8] = r1; verifier tracks fp[-8]=42 after this instruction.Quoted 13 lines
So the patch makes both cases equivalent.
- patch #3 in this patchset: adjusts
verifier.c:convert_ctx_access()
to operate onBPF_STalongsideBPF_STX.
Context parameters for some BPF programs types are "fake"
data
structures. The verifier matches allBPF_STXandBPF_LDXinstructions that operate on pointers to such contexts and rewrites these instructions. It might change an offset or add another layer of indirection, etc. E.g. see filter.c:bpf_convert_ctx_access(). (This also implies that verifier forbids writes to non-constant
offsets inside such structures).
So the patch extends this logic to also handleBPF_ST.The patch 3 is necessary to land before llvm starts generating 'st' for ctx access.
That's clear, but I'm missing why patch 1 is necessary.
Sure, it's making the verifier understand scalar spills with 'st' and makes 'st' equivalent to 'stx', but I'm missing why it's necessary. What kind of programs fail to be verified when llvm starts generating 'st' ? Regarind -mcpu=v4.
I think we need to add all of our upcoming instructions as a single flag. Otherwise we'll have -mcpu=v5,v6,v7 and full combinations of them. -mcpu=v4 could mean:
- ST
- sign extending loads
- sign extend a registerQuoted 8 lines
- 32-bit JA
- proper bswap insns: bswap16, bswap32, bswap64
The sign and 32-bit JA we've discussed earlier.
The bswap was on my wish list forever.
The existingTO_LE,TO_BEinsns are really odd from compiler pov. The compiler should translate bswap IR op into proper bswap insn just like it does on all cpus.
Maybe add SDIV to -mcpu=v4 as well?Quoted 2 lines
Right, we should add these insns in llvm17 with -mcpu=v4, so we can keep the number of cpu generations minimum.
How do you plan to encode the sign-extend load instructions?
I guess a possibility would be to use one of the available op-mode for load instructions that are currently marked as reserved. For example:
IMM = 0b000 ABS = 0b001 IND = 0b010 MEM = 0b011 SEM = 0b100 <- new
Quoted 4 lines
Then we would have the following code fields for sign-extending LDX instructions:
op-mode:SEM op-size:{W,H,B,DW} op-class:LDX
Right, this is what I plan to do as well. See my incomplete llvm prototype here:
https://reviews.llvm.org/D133464