LoongArch: Add support for BPF Arena
Subsystems (3)
Hengqi Chen
reviewercover LoongArch: Add support for BPF Arena
This patch set aims to add support for BPF Arena in LoongArch.
Patch 1~4 are some fixes and prep works.
Patch 5 use BPF prog pack allocator for BPF progs
Patch 6~7 adds support for BPF Arena
With this set, the following test cases pass without regressions:
$ ./test_progs -a arena_htab \
-a arena_list \
-a compute_live_registers \
-a kfunc_call \
-a libbpf_probe_map_types \
-a lookup_key \
-a verifier_arena \
-a verifier_arena_large \
-a verifier_ldsxHengqi Chen (7):
LoongArch: BPF: Move sign_extend() upper
LoongArch: BPF: Rename local variable sign_extend to sign_ext
LoongArch: BPF: Sign extend kfunc call arguments
LoongArch: BPF: Zero-extend bpf_tail_call() index
LoongArch: BPF: Use BPF prog pack allocator
LoongArch: BPF: Implement PROBE_MEM32 pseudo instructions
LoongArch: BPF: Implement bpf_addr_space_cast instruction
arch/loongarch/net/bpf_jit.c | 305 +++++++++++++++++++++++++---------- arch/loongarch/net/bpf_jit.h | 4 +- stxSxLQw | 0 3 files changed, 227 insertions(+), 82 deletions(-) create mode 100644 stxSxLQw
--
2.43.5
Hengqi Chen
reviewerPATCH 1/7 LoongArch: BPF: Move sign_extend() upper
Move sign_extend() upper in bpf_jit.c so that it can be used in build_insn() without forward declaration. No functional change intended.
--- arch/loongarch/net/bpf_jit.c | 62 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 31 deletions(-)
arch/loongarch/net/bpf_jit.c +31 -31
@@ -437,6 +437,37 @@ static bool is_signed_bpf_cond(u8 cond) cond ==BPF_JSGE || cond ==BPF_JSLE ; } +/* +* Sign-extend the register if necessary +*/ +static void sign_extend (struct jit_ctx *ctx,int rd,int rj,u8 size,bool sign) +{ +/* The ABI requires unsigned char/short to be zero-extended */ +if (!sign && (size ==1 || size ==2 )) { +if (rd != rj) +move_reg (ctx, rd, rj); +return ; + } + +switch (size) { +case 1 : +emit_insn (ctx, extwb, rd, rj); +break ; +case 2 : +emit_insn (ctx, extwh, rd, rj); +break ; +case 4 : +emit_insn (ctx, addiw, rd, rj,0 ); +break ; +case 8 : +if (rd != rj) +move_reg (ctx, rd, rj); +break ; +default : +pr_warn ("bpf_jit: invalid size %d for sign_extend\n" , size); + } +} +#define BPF_FIXUP_REG_MASK GENMASK(31, 27)#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
@@ -1451,37 +1482,6 @@ void arch_free_bpf_trampoline(void *image, unsigned int size)bpf_prog_pack_free (image, size); } -/* -* Sign-extend the register if necessary -*/ -static void sign_extend (struct jit_ctx *ctx,int rd,int rj,u8 size,bool sign) -{ -/* The ABI requires unsigned char/short to be zero-extended */ -if (!sign && (size ==1 || size ==2 )) { -if (rd != rj) -move_reg (ctx, rd, rj); -return ; - } - -switch (size) { -case 1 : -emit_insn (ctx, extwb, rd, rj); -break ; -case 2 : -emit_insn (ctx, extwh, rd, rj); -break ; -case 4 : -emit_insn (ctx, addiw, rd, rj,0 ); -break ; -case 8 : -if (rd != rj) -move_reg (ctx, rd, rj); -break ; -default : -pr_warn ("bpf_jit: invalid size %d for sign_extend\n" , size); - } -} -static int __arch_prepare_bpf_trampoline (struct jit_ctx *ctx,struct bpf_tramp_image *im,const struct btf_func_model *m,struct bpf_tramp_links *tlinks,void *func_addr,u32 flags)
Signature · 1 line
2.43.5
Tiezhu Yang
maintainerRe: [PATCH 1/7] LoongArch: BPF: Move sign_extend() upper
Quoted 37 lines · Hengqi Chen
Move
sign_extend()upper inbpf_jit.cso that it can be used inbuild_insn()without forward declaration. No functional change intended.Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 62 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 31 deletions(-)arch/loongarch/net/bpf_jit.c +20
-0@@ -437,6 +437,37 @@ static bool is_signed_bpf_cond(u8 cond) cond ==BPF_JSGE || cond ==BPF_JSLE ; } +/* +* Sign-extend the register if necessary +*/ +static void sign_extend (struct jit_ctx *ctx,int rd,int rj,u8 size,bool sign) +{ +/* The ABI requires unsigned char/short to be zero-extended */ +if (!sign && (size ==1 || size ==2 )) { +if (rd != rj) +move_reg (ctx, rd, rj); +return ; + } + +switch (size) { +case 1 : +emit_insn (ctx, extwb, rd, rj); +break ; +case 2 : +emit_insn (ctx, extwh, rd, rj); +break ; +case 4 :
arch/loongarch/net/bpf_jit.c static bool is_signed_bpf_cond(u8 cond) · line 469
Quoted 10 lines
arch/loongarch/net/bpf_jit.c +10
-0+emit_insn (ctx, addiw, rd, rj,0 ); +break ; +case 8 : +if (rd != rj) +move_reg (ctx, rd, rj); +break ; +default : +pr_warn ("bpf_jit: invalid size %d for sign_extend\n" , size); + } +}
Is it better to move this function to arch/loongarch/net/bpf_jit.h? Just put it next to emit_zext_32() and emit_sext_32() because they are similar helper functions.
In my opinion, the function name is not proper because it contains not
only sign-extension but also zero-extension, maybe we can rename it to emit_extension(), then the patch #2 can be dropped.
Thanks,
Tiezhu
Hengqi Chen
reviewerRe: [PATCH 1/7] LoongArch: BPF: Move sign_extend() upper
Quoted 54 lines · Tiezhu Yang
Quoted 37 lines · Hengqi Chen
Move
sign_extend()upper inbpf_jit.cso that it can be used inbuild_insn()without forward declaration. No functional change intended.Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 62 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 31 deletions(-)arch/loongarch/net/bpf_jit.c +20
-0@@ -437,6 +437,37 @@ static bool is_signed_bpf_cond(u8 cond) cond ==BPF_JSGE || cond ==BPF_JSLE ; } +/* +* Sign-extend the register if necessary +*/ +static void sign_extend (struct jit_ctx *ctx,int rd,int rj,u8 size,bool sign) +{ +/* The ABI requires unsigned char/short to be zero-extended */ +if (!sign && (size ==1 || size ==2 )) { +if (rd != rj) +move_reg (ctx, rd, rj); +return ; + } + +switch (size) { +case 1 : +emit_insn (ctx, extwb, rd, rj); +break ; +case 2 : +emit_insn (ctx, extwh, rd, rj); +break ; +case 4 :arch/loongarch/net/bpf_jit.c static bool is_signed_bpf_cond(u8 cond) · line 469
Quoted 10 lines
arch/loongarch/net/bpf_jit.c +10
-0+emit_insn (ctx, addiw, rd, rj,0 ); +break ; +case 8 : +if (rd != rj) +move_reg (ctx, rd, rj); +break ; +default : +pr_warn ("bpf_jit: invalid size %d for sign_extend\n" , size); + } +}Is it better to move this function to
arch/loongarch/net/bpf_jit.h? Just put it next toemit_zext_32()andemit_sext_32()because they are similar helper functions.
Quoted 4 lines
In my opinion, the function name is not proper because it contains not only sign-extension but also zero-extension, maybe we can rename it to
emit_extension(), then the patch #2 can be dropped.
Will add a new helper in bpf_jit.h since the existing sign_extend() is not generic.
Will also drop patch 2. Thanks.
Quoted 3 lines
Thanks,
Tiezhu
Hengqi Chen
reviewerPATCH 2/7 LoongArch: BPF: Rename local variable sign_extend to sign_ext
In reply to Hengqi ChenRename the local variable sign_extend in build_insn() to sign_ext to avoid naming conflict with sign_extend(). sign_extend() will be used in upcoming change in build_insn().
--- arch/loongarch/net/bpf_jit.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
arch/loongarch/net/bpf_jit.c +9 -9
@@ -535,7 +535,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext {u8 tm =-1 ;u64 func_addr; -bool func_addr_fixed, sign_extend; +bool func_addr_fixed, sign_ext;int i = insn - ctx->prog->insnsi;int ret, jmp_offset, tcc_ptr_off;const u8 code = insn->code;
@@ -1035,18 +1035,18 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extcase BPF_LDX |BPF_PROBE_MEMSX |BPF_B :case BPF_LDX |BPF_PROBE_MEMSX |BPF_H :case BPF_LDX |BPF_PROBE_MEMSX |BPF_W : - sign_extend =BPF_MODE (insn->code) ==BPF_MEMSX || -BPF_MODE (insn->code) ==BPF_PROBE_MEMSX ; + sign_ext =BPF_MODE (code) ==BPF_MEMSX || +BPF_MODE (code) ==BPF_PROBE_MEMSX ;switch (BPF_SIZE (code)) {case BPF_B :if (is_signed_imm12 (off)) { -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldb, dst, src, off);else emit_insn (ctx, ldbu, dst, src, off); }else {move_imm (ctx, t1, off, is32); -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldxb, dst, src, t1);else emit_insn (ctx, ldxbu, dst, src, t1);
@@ -1054,13 +1054,13 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extbreak ;case BPF_H :if (is_signed_imm12 (off)) { -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldh, dst, src, off);else emit_insn (ctx, ldhu, dst, src, off); }else {move_imm (ctx, t1, off, is32); -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldxh, dst, src, t1);else emit_insn (ctx, ldxhu, dst, src, t1);
@@ -1068,13 +1068,13 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extbreak ;case BPF_W :if (is_signed_imm12 (off)) { -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldw, dst, src, off);else emit_insn (ctx, ldwu, dst, src, off); }else {move_imm (ctx, t1, off, is32); -if (sign_extend) +if (sign_ext)emit_insn (ctx, ldxw, dst, src, t1);else emit_insn (ctx, ldxwu, dst, src, t1);
Signature · 1 line
2.43.5
Hengqi Chen
reviewerPATCH 3/7 LoongArch: BPF: Sign extend kfunc call arguments
In reply to Hengqi ChenThe kfunc calls are native calls so they should follow LoongArch calling conventions. Sign extend its arguments properly to avoid kernel panic.
--- arch/loongarch/net/bpf_jit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/loongarch/net/bpf_jit.c +16 -0
@@ -983,6 +983,22 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extemit_insn (ctx, ldd,REG_TCC ,LOONGARCH_GPR_SP , tcc_ptr_off); } +if (insn->src_reg ==BPF_PSEUDO_KFUNC_CALL ) { +const struct btf_func_model *fm; +int idx; + + fm =bpf_jit_find_kfunc_model (ctx->prog, insn); +if (!fm) +return -EINVAL ; + +for (idx =0 ; idx < fm->nr_args; idx++) { +u8 reg = regmap[BPF_REG_1 + idx]; + +sign_extend (ctx, reg, reg, fm->arg_size[idx], + fm->arg_flags[idx] &BTF_FMODEL_SIGNED_ARG ); + } + } +move_addr (ctx, t1, func_addr);emit_insn (ctx, jirl,LOONGARCH_GPR_RA , t1,0 );
Signature · 1 line
2.43.5
Tiezhu Yang
maintainerRe: [PATCH 3/7] LoongArch: BPF: Sign extend kfunc call arguments
Quoted 26 lines · Hengqi Chen
The kfunc calls are native calls so they should follow LoongArch calling conventions. Sign extend its arguments properly to avoid kernel panic.
Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/loongarch/net/bpf_jit.c +8
-0@@ -983,6 +983,22 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extemit_insn (ctx, ldd,REG_TCC ,LOONGARCH_GPR_SP , tcc_ptr_off); } +if (insn->src_reg ==BPF_PSEUDO_KFUNC_CALL ) { +const struct btf_func_model *fm; +int idx; + + fm =bpf_jit_find_kfunc_model (ctx->prog, insn); +if (!fm) +return -EINVAL ; +
arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 1003
Quoted 10 lines
arch/loongarch/net/bpf_jit.c +8
-0+for (idx =0 ; idx < fm->nr_args; idx++) { +u8 reg = regmap[BPF_REG_1 + idx]; + +sign_extend (ctx, reg, reg, fm->arg_size[idx], + fm->arg_flags[idx] &BTF_FMODEL_SIGNED_ARG ); + } + } +move_addr (ctx, t1, func_addr);emit_insn (ctx, jirl,LOONGARCH_GPR_RA , t1,0 );
Without this patch, there are some failed test cases? With this patch, the failed test cases can pass?
If so, please add the related test cases in the commit message, then we can test this single patch with the corresponding test cases and the git log can show what problem this patch solves clearly.
Please add the related test cases in the commit message for the patch #4 - #7 too.
Thanks,
Tiezhu
Hengqi Chen
reviewerRe: [PATCH 3/7] LoongArch: BPF: Sign extend kfunc call arguments
Quoted 42 lines · Tiezhu Yang
Quoted 26 lines · Hengqi Chen
The kfunc calls are native calls so they should follow LoongArch calling conventions. Sign extend its arguments properly to avoid kernel panic.
Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)arch/loongarch/net/bpf_jit.c +8
-0@@ -983,6 +983,22 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extemit_insn (ctx, ldd,REG_TCC ,LOONGARCH_GPR_SP , tcc_ptr_off); } +if (insn->src_reg ==BPF_PSEUDO_KFUNC_CALL ) { +const struct btf_func_model *fm; +int idx; + + fm =bpf_jit_find_kfunc_model (ctx->prog, insn); +if (!fm) +return -EINVAL ; +arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 1003
Quoted 10 lines
arch/loongarch/net/bpf_jit.c +8
-0+for (idx =0 ; idx < fm->nr_args; idx++) { +u8 reg = regmap[BPF_REG_1 + idx]; + +sign_extend (ctx, reg, reg, fm->arg_size[idx], + fm->arg_flags[idx] &BTF_FMODEL_SIGNED_ARG ); + } + } +move_addr (ctx, t1, func_addr);emit_insn (ctx, jirl,LOONGARCH_GPR_RA , t1,0 );Without this patch, there are some failed test cases? With this patch, the failed test cases can pass?
Quoted 8 lines
If so, please add the related test cases in the commit message, then we can test this single patch with the corresponding test cases and the git log can show what problem this patch solves clearly.
Please add the related test cases in the commit message for the patch #4 - #7 too.
Will detail the test instructions in cover letter, it's complex in some way.
Quoted 3 lines
Thanks,
Tiezhu
Hengqi Chen
reviewerPATCH 4/7 LoongArch: BPF: Zero-extend bpf_tail_call() index
In reply to Hengqi ChenThe bpf_tail_call() index should be treated as u32 value. Let's zero-extend it to avoid calling wrong BPF progs. See similar fixes for x86 ([0]) and arm64 ([1]) for more details.
[0]: https://github.com/torvalds/linux/commit/90caccdd8cc0215705f18b92771b449b01e2474a
[1]: https://github.com/torvalds/linux/commit/16338a9b3ac30740d49f5dfed81bac0ffa53b9c7
--- arch/loongarch/net/bpf_jit.c | 2 ++ 1 file changed, 2 insertions(+)
arch/loongarch/net/bpf_jit.c +2 -0
@@ -280,6 +280,8 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx, int insn)* goto out; */ tc_ninsn = insn ? ctx->offset[insn+1 ] - ctx->offset[insn] : ctx->offset[0 ]; +emit_zext_32 (ctx, a2, true); + off = offsetof(struct bpf_array, map.max_entries);emit_insn (ctx, ldwu, t1, a1, off);/* bgeu $a2, $t1, jmp_offset */
Signature · 1 line
2.43.5
Hengqi Chen
reviewerPATCH 5/7 LoongArch: BPF: Use BPF prog pack allocator
In reply to Hengqi ChenUse bpf_jit_binary_pack_alloc() for JIT binaries. The BPF prog pack allocator creates a pair of RW and RX buffers. The JIT writes the program into the RW buffer. When the JIT is done, the program is copied to the final RX buffer with bpf_jit_binary_pack_finalize.
--- arch/loongarch/net/bpf_jit.c | 123 ++++++++++++++++++++++++----------- arch/loongarch/net/bpf_jit.h | 2 +- stxSxLQw | 0 3 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 stxSxLQw
arch/loongarch/net/bpf_jit.c +86 -37
@@ -490,11 +490,11 @@ static int add_exception_handler(const struct bpf_insn *insn, struct jit_ctx *ctx, int dst_reg) { -unsigned long pc; -off_t offset;struct exception_table_entry *ex; +off_t ins_offset, fixup_offset; +unsigned long pc; -if (!ctx->image || !ctx->prog->aux->extable) +if (!ctx->image || !ctx->ro_image || !ctx->prog->aux->extable)return 0 ; if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
@@ -505,14 +505,18 @@ static int add_exception_handler(const struct bpf_insn *insn,return -EINVAL ; ex = &ctx->prog->aux->extable[ctx->num_exentries]; - pc = (unsigned long )&ctx->image[ctx->idx -1 ]; + pc = (unsigned long )&ctx->ro_image[ctx->idx -1 ]; - offset = pc - (long )&ex->insn; -if (WARN_ON_ONCE (offset >=0 || offset <INT_MIN )) +/* +* This is the relative offset of the instruction that may fault from +* the exception table itself. This will be written to the exception +* table and if this instruction faults, the destination register will +* be set to '0' and the execution will jump to the next instruction. +*/ + ins_offset = pc - (long )&ex->insn; +if (WARN_ON_ONCE (ins_offset >=0 || ins_offset <INT_MIN ))return -ERANGE ; - ex->insn = offset; -/* * Since the extable follows the program, the fixup offset is always * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
@@ -520,13 +524,25 @@ static int add_exception_handler(const struct bpf_insn *insn,* bits. We don't need to worry about buildtime or runtime sort * modifying the upper bits because the table is already sorted, and * isn't part of the main exception table. +* +* The fixup_offset is set to the next instruction from the instruction +* that may fault. The execution will jump to this after handling the +* fault. */ - offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); -if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , offset)) + fixup_offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); +if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , fixup_offset))return -ERANGE ; +/* +* The offsets above have been calculated using the RO buffer but we +* need to use the R/W buffer for writes. +* switch ex to rw buffer for writing. +*/ + ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image); + ex->insn = ins_offset; + ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , fixup_offset) | +FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ex->type =EX_TYPE_BPF ; - ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , offset) |FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ctx->num_exentries++;
@@ -1809,11 +1825,12 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) {bool tmp_blinded = false, extra_pass = false; -u8 *image_ptr; +u8 *image_ptr, *ro_image_ptr;int image_size, prog_size, extable_size;struct jit_ctx ctx;struct jit_data *jit_data;struct bpf_binary_header *header; +struct bpf_binary_header *ro_header;struct bpf_prog *tmp, *orig_prog = prog;/*
@@ -1848,8 +1865,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) }if (jit_data->ctx.offset) { ctx = jit_data->ctx; - image_ptr = jit_data->image; + ro_image_ptr = (void *)ctx.ro_image; + ro_header = jit_data->ro_header; header = jit_data->header; + image_ptr = (void *)header + ((void *)ro_image_ptr - (void *)ro_header); extra_pass = true; prog_size =sizeof (u32) * ctx.idx; goto skip_init_ctx;
@@ -1883,17 +1902,26 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) prog_size =sizeof (u32) * ctx.idx; image_size = prog_size + extable_size;/* Now we know the size of the structure to make */ - header =bpf_jit_binary_alloc (image_size, &image_ptr, -sizeof (u32), jit_fill_hole); -if (header ==NULL ) { + ro_header =bpf_jit_binary_pack_alloc (image_size, &ro_image_ptr, +sizeof (u32), &header, &image_ptr, + jit_fill_hole); +if (!ro_header) { prog = orig_prog; goto out_offset; }/* 2. Now, the actual pass to generate final JIT code */ +/* +* Use the image(RW) for writing the JITed instructions. But also save +* the ro_image(RX) for calculating the offsets in the image. The RW +* image will be later copied to the RX image from where the program +* will run. The bpf_jit_binary_pack_finalize() will do this copy in the +* final step. +*/ ctx.image = (union loongarch_instruction *)image_ptr; + ctx.ro_image = (union loongarch_instruction *)ro_image_ptr;if (extable_size) - prog->aux->extable = (void *)image_ptr + prog_size; + prog->aux->extable = (void *)ro_image_ptr + prog_size; skip_init_ctx: ctx.idx =0 ;
@@ -1901,48 +1929,47 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)build_prologue (&ctx);if (build_body (&ctx, extra_pass)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }build_epilogue (&ctx);/* 3. Extra pass to validate JITed code */ if (validate_ctx (&ctx)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }/* And we're done */ if (bpf_jit_enable >1 )bpf_jit_dump (prog->len, prog_size,2 , ctx.image); -/* Update the icache */ -flush_icache_range ((unsigned long )header, (unsigned long )(ctx.image + ctx.idx)); -if (!prog->is_func || extra_pass) { -int err; -if (extra_pass && ctx.idx != jit_data->ctx.idx) {pr_err_once ("multi-func JIT bug %d != %d\n" , ctx.idx, jit_data->ctx.idx); goto out_free; } - err =bpf_jit_binary_lock_ro (header); -if (err) { -pr_err_once ("bpf_jit_binary_lock_ro() returned %d\n" , - err); +if (WARN_ON (bpf_jit_binary_pack_finalize (ro_header, header))) { +/* ro_header has been freed */ + ro_header =NULL ; + prog = orig_prog; goto out_free; } +/* +* The instructions have now been copied to the ROX region from +* where they will execute. Now the data cache has to be cleaned to +* the PoU and the I-cache has to be invalidated for the VAs. +*/ +bpf_flush_icache (ro_header, ctx.ro_image + ctx.idx); }else { jit_data->ctx = ctx; - jit_data->image = image_ptr; jit_data->header = header; + jit_data->ro_header = ro_header; } prog->jited =1 ; prog->jited_len = prog_size; - prog->bpf_func = (void *)ctx.image; + prog->bpf_func = (void *)ctx.ro_image;if (!prog->is_func || extra_pass) {int i;
@@ -1962,17 +1989,39 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)if (tmp_blinded)bpf_jit_prog_release_other (prog, prog == orig_prog ? tmp : orig_prog); -return prog; out_free: -bpf_jit_binary_free (header); - prog->bpf_func =NULL ; - prog->jited =0 ; - prog->jited_len =0 ; +if (header) { +bpf_arch_text_copy (&ro_header->size, &header->size,sizeof (header->size)); +bpf_jit_binary_pack_free (ro_header, header); + } goto out_offset; } +void bpf_jit_free (struct bpf_prog *prog) +{ +if (prog->jited) { +struct jit_data *jit_data = prog->aux->jit_data; +struct bpf_binary_header *hdr; + +/* +* If we fail the final pass of JIT (from jit_subprogs), +* the program may not be finalized yet. Call finalize here +* before freeing it. +*/ +if (jit_data) { +bpf_jit_binary_pack_finalize (jit_data->ro_header, jit_data->header); +kfree (jit_data); + } + hdr =bpf_jit_binary_pack_hdr (prog); +bpf_jit_binary_pack_free (hdr,NULL ); +WARN_ON_ONCE (!bpf_prog_kallsyms_verify_off (prog)); + } + +bpf_prog_unlock_free (prog); +} +bool bpf_jit_bypass_spec_v1 (void ) {return true;
arch/loongarch/net/bpf_jit.h +1 -1
@@ -24,7 +24,7 @@ struct jit_ctx {struct jit_data {struct bpf_binary_header *header; -u8 *image; +struct bpf_binary_header *ro_header;struct jit_ctx ctx; };
stxSxLQw new file +0 -0
Signature · 1 line
2.43.5
Huacai Chen
maintainerRe: [PATCH 5/7] LoongArch: BPF: Use BPF prog pack allocator
Quoted 7 lines · Hengqi Chen
Use
bpf_jit_binary_pack_alloc()for JIT binaries. The BPF prog pack allocator creates a pair of RW and RX buffers. The JIT writes the program into the RW buffer. When the JIT is done, the program is copied to the final RX buffer withbpf_jit_binary_pack_finalize.
Quoted 7 lines
Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 123 ++++++++++++++++++++++++----------- arch/loongarch/net/bpf_jit.h | 2 +- stxSxLQw | 0 3 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 stxSxLQw
???
Quoted 257 lines
arch/loongarch/net/bpf_jit.c +86
-37@@ -490,11 +490,11 @@ static int add_exception_handler(const struct bpf_insn *insn, struct jit_ctx *ctx, int dst_reg) { -unsigned long pc; -off_t offset;struct exception_table_entry *ex; +off_t ins_offset, fixup_offset; +unsigned long pc; -if (!ctx->image || !ctx->prog->aux->extable) +if (!ctx->image || !ctx->ro_image || !ctx->prog->aux->extable)return 0 ; if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&@@ -505,14 +505,18 @@ static int add_exception_handler(const struct bpf_insn *insn,return -EINVAL ; ex = &ctx->prog->aux->extable[ctx->num_exentries]; - pc = (unsigned long )&ctx->image[ctx->idx -1 ]; + pc = (unsigned long )&ctx->ro_image[ctx->idx -1 ]; - offset = pc - (long )&ex->insn; -if (WARN_ON_ONCE (offset >=0 || offset <INT_MIN )) +/* +* This is the relative offset of the instruction that may fault from +* the exception table itself. This will be written to the exception +* table and if this instruction faults, the destination register will +* be set to '0' and the execution will jump to the next instruction. +*/ + ins_offset = pc - (long )&ex->insn; +if (WARN_ON_ONCE (ins_offset >=0 || ins_offset <INT_MIN ))return -ERANGE ; - ex->insn = offset; -/* * Since the extable follows the program, the fixup offset is always * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value @@ -520,13 +524,25 @@ static int add_exception_handler(const struct bpf_insn *insn,* bits. We don't need to worry about buildtime or runtime sort * modifying the upper bits because the table is already sorted, and * isn't part of the main exception table. +* +* The fixup_offset is set to the next instruction from the instruction +* that may fault. The execution will jump to this after handling the +* fault. */ - offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); -if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , offset)) + fixup_offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); +if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , fixup_offset))return -ERANGE ; +/* +* The offsets above have been calculated using the RO buffer but we +* need to use the R/W buffer for writes. +* switch ex to rw buffer for writing. +*/ + ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image); + ex->insn = ins_offset; + ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , fixup_offset) | +FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ex->type =EX_TYPE_BPF ; - ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , offset) |FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ctx->num_exentries++;@@ -1809,11 +1825,12 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) {bool tmp_blinded = false, extra_pass = false; -u8 *image_ptr; +u8 *image_ptr, *ro_image_ptr;int image_size, prog_size, extable_size;struct jit_ctx ctx;struct jit_data *jit_data;struct bpf_binary_header *header; +struct bpf_binary_header *ro_header;struct bpf_prog *tmp, *orig_prog = prog;/* @@ -1848,8 +1865,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) }if (jit_data->ctx.offset) { ctx = jit_data->ctx; - image_ptr = jit_data->image; + ro_image_ptr = (void *)ctx.ro_image; + ro_header = jit_data->ro_header; header = jit_data->header; + image_ptr = (void *)header + ((void *)ro_image_ptr - (void *)ro_header); extra_pass = true; prog_size =sizeof (u32) * ctx.idx; goto skip_init_ctx;@@ -1883,17 +1902,26 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) prog_size =sizeof (u32) * ctx.idx; image_size = prog_size + extable_size;/* Now we know the size of the structure to make */ - header =bpf_jit_binary_alloc (image_size, &image_ptr, -sizeof (u32), jit_fill_hole); -if (header ==NULL ) { + ro_header =bpf_jit_binary_pack_alloc (image_size, &ro_image_ptr, +sizeof (u32), &header, &image_ptr, + jit_fill_hole); +if (!ro_header) { prog = orig_prog; goto out_offset; }/* 2. Now, the actual pass to generate final JIT code */ +/* +* Use the image(RW) for writing the JITed instructions. But also save +* the ro_image(RX) for calculating the offsets in the image. The RW +* image will be later copied to the RX image from where the program +* will run. The bpf_jit_binary_pack_finalize() will do this copy in the +* final step. +*/ ctx.image = (union loongarch_instruction *)image_ptr; + ctx.ro_image = (union loongarch_instruction *)ro_image_ptr;if (extable_size) - prog->aux->extable = (void *)image_ptr + prog_size; + prog->aux->extable = (void *)ro_image_ptr + prog_size; skip_init_ctx: ctx.idx =0 ;@@ -1901,48 +1929,47 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)build_prologue (&ctx);if (build_body (&ctx, extra_pass)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }build_epilogue (&ctx);/* 3. Extra pass to validate JITed code */ if (validate_ctx (&ctx)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }/* And we're done */ if (bpf_jit_enable >1 )bpf_jit_dump (prog->len, prog_size,2 , ctx.image); -/* Update the icache */ -flush_icache_range ((unsigned long )header, (unsigned long )(ctx.image + ctx.idx)); -if (!prog->is_func || extra_pass) { -int err; -if (extra_pass && ctx.idx != jit_data->ctx.idx) {pr_err_once ("multi-func JIT bug %d != %d\n" , ctx.idx, jit_data->ctx.idx); goto out_free; } - err =bpf_jit_binary_lock_ro (header); -if (err) { -pr_err_once ("bpf_jit_binary_lock_ro() returned %d\n" , - err); +if (WARN_ON (bpf_jit_binary_pack_finalize (ro_header, header))) { +/* ro_header has been freed */ + ro_header =NULL ; + prog = orig_prog; goto out_free; } +/* +* The instructions have now been copied to the ROX region from +* where they will execute. Now the data cache has to be cleaned to +* the PoU and the I-cache has to be invalidated for the VAs. +*/ +bpf_flush_icache (ro_header, ctx.ro_image + ctx.idx); }else { jit_data->ctx = ctx; - jit_data->image = image_ptr; jit_data->header = header; + jit_data->ro_header = ro_header; } prog->jited =1 ; prog->jited_len = prog_size; - prog->bpf_func = (void *)ctx.image; + prog->bpf_func = (void *)ctx.ro_image;if (!prog->is_func || extra_pass) {int i;@@ -1962,17 +1989,39 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)if (tmp_blinded)bpf_jit_prog_release_other (prog, prog == orig_prog ? tmp : orig_prog); -return prog; out_free: -bpf_jit_binary_free (header); - prog->bpf_func =NULL ; - prog->jited =0 ; - prog->jited_len =0 ; +if (header) { +bpf_arch_text_copy (&ro_header->size, &header->size,sizeof (header->size)); +bpf_jit_binary_pack_free (ro_header, header); + } goto out_offset; } +void bpf_jit_free (struct bpf_prog *prog) +{ +if (prog->jited) { +struct jit_data *jit_data = prog->aux->jit_data; +struct bpf_binary_header *hdr; + +/* +* If we fail the final pass of JIT (from jit_subprogs), +* the program may not be finalized yet. Call finalize here +* before freeing it. +*/ +if (jit_data) { +bpf_jit_binary_pack_finalize (jit_data->ro_header, jit_data->header); +kfree (jit_data); + } + hdr =bpf_jit_binary_pack_hdr (prog); +bpf_jit_binary_pack_free (hdr,NULL ); +WARN_ON_ONCE (!bpf_prog_kallsyms_verify_off (prog)); + } + +bpf_prog_unlock_free (prog); +} +bool bpf_jit_bypass_spec_v1 (void ) {return true;arch/loongarch/net/bpf_jit.h +1
-1@@ -24,7 +24,7 @@ struct jit_ctx {struct jit_data {struct bpf_binary_header *header; -u8 *image; +struct bpf_binary_header *ro_header;struct jit_ctx ctx; };stxSxLQw new file +0
-1--2.43.5
Hengqi Chen
reviewerRe: [PATCH 5/7] LoongArch: BPF: Use BPF prog pack allocator
Quoted 9 lines · Huacai Chen
Quoted 7 lines · Hengqi Chen
Use
bpf_jit_binary_pack_alloc()for JIT binaries. The BPF prog pack allocator creates a pair of RW and RX buffers. The JIT writes the program into the RW buffer. When the JIT is done, the program is copied to the final RX buffer withbpf_jit_binary_pack_finalize.
Quoted 8 lines
Quoted 7 lines
Signed-off-by Hengqi Chen <[email protected]>--- arch/loongarch/net/bpf_jit.c | 123 ++++++++++++++++++++++++----------- arch/loongarch/net/bpf_jit.h | 2 +- stxSxLQw | 0 3 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 stxSxLQw???
Sorry, this slipped in probably due to disk full and leftover during my local build.
Will remove and resend.
Quoted 258 lines
Quoted 258 lines
arch/loongarch/net/bpf_jit.c +86
-37@@ -490,11 +490,11 @@ static int add_exception_handler(const struct bpf_insn *insn, struct jit_ctx *ctx, int dst_reg) { -unsigned long pc; -off_t offset;struct exception_table_entry *ex; +off_t ins_offset, fixup_offset; +unsigned long pc; -if (!ctx->image || !ctx->prog->aux->extable) +if (!ctx->image || !ctx->ro_image || !ctx->prog->aux->extable)return 0 ; if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&@@ -505,14 +505,18 @@ static int add_exception_handler(const struct bpf_insn *insn,return -EINVAL ; ex = &ctx->prog->aux->extable[ctx->num_exentries]; - pc = (unsigned long )&ctx->image[ctx->idx -1 ]; + pc = (unsigned long )&ctx->ro_image[ctx->idx -1 ]; - offset = pc - (long )&ex->insn; -if (WARN_ON_ONCE (offset >=0 || offset <INT_MIN )) +/* +* This is the relative offset of the instruction that may fault from +* the exception table itself. This will be written to the exception +* table and if this instruction faults, the destination register will +* be set to '0' and the execution will jump to the next instruction. +*/ + ins_offset = pc - (long )&ex->insn; +if (WARN_ON_ONCE (ins_offset >=0 || ins_offset <INT_MIN ))return -ERANGE ; - ex->insn = offset; -/* * Since the extable follows the program, the fixup offset is always * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value @@ -520,13 +524,25 @@ static int add_exception_handler(const struct bpf_insn *insn,* bits. We don't need to worry about buildtime or runtime sort * modifying the upper bits because the table is already sorted, and * isn't part of the main exception table. +* +* The fixup_offset is set to the next instruction from the instruction +* that may fault. The execution will jump to this after handling the +* fault. */ - offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); -if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , offset)) + fixup_offset = (long )&ex->fixup - (pc +LOONGARCH_INSN_SIZE ); +if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , fixup_offset))return -ERANGE ; +/* +* The offsets above have been calculated using the RO buffer but we +* need to use the R/W buffer for writes. +* switch ex to rw buffer for writing. +*/ + ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image); + ex->insn = ins_offset; + ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , fixup_offset) | +FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ex->type =EX_TYPE_BPF ; - ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , offset) |FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ctx->num_exentries++;@@ -1809,11 +1825,12 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) {bool tmp_blinded = false, extra_pass = false; -u8 *image_ptr; +u8 *image_ptr, *ro_image_ptr;int image_size, prog_size, extable_size;struct jit_ctx ctx;struct jit_data *jit_data;struct bpf_binary_header *header; +struct bpf_binary_header *ro_header;struct bpf_prog *tmp, *orig_prog = prog;/* @@ -1848,8 +1865,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) }if (jit_data->ctx.offset) { ctx = jit_data->ctx; - image_ptr = jit_data->image; + ro_image_ptr = (void *)ctx.ro_image; + ro_header = jit_data->ro_header; header = jit_data->header; + image_ptr = (void *)header + ((void *)ro_image_ptr - (void *)ro_header); extra_pass = true; prog_size =sizeof (u32) * ctx.idx; goto skip_init_ctx;@@ -1883,17 +1902,26 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) prog_size =sizeof (u32) * ctx.idx; image_size = prog_size + extable_size;/* Now we know the size of the structure to make */ - header =bpf_jit_binary_alloc (image_size, &image_ptr, -sizeof (u32), jit_fill_hole); -if (header ==NULL ) { + ro_header =bpf_jit_binary_pack_alloc (image_size, &ro_image_ptr, +sizeof (u32), &header, &image_ptr, + jit_fill_hole); +if (!ro_header) { prog = orig_prog; goto out_offset; }/* 2. Now, the actual pass to generate final JIT code */ +/* +* Use the image(RW) for writing the JITed instructions. But also save +* the ro_image(RX) for calculating the offsets in the image. The RW +* image will be later copied to the RX image from where the program +* will run. The bpf_jit_binary_pack_finalize() will do this copy in the +* final step. +*/ ctx.image = (union loongarch_instruction *)image_ptr; + ctx.ro_image = (union loongarch_instruction *)ro_image_ptr;if (extable_size) - prog->aux->extable = (void *)image_ptr + prog_size; + prog->aux->extable = (void *)ro_image_ptr + prog_size; skip_init_ctx: ctx.idx =0 ;@@ -1901,48 +1929,47 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)build_prologue (&ctx);if (build_body (&ctx, extra_pass)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }build_epilogue (&ctx);/* 3. Extra pass to validate JITed code */ if (validate_ctx (&ctx)) { -bpf_jit_binary_free (header); prog = orig_prog; - goto out_offset; + goto out_free; }/* And we're done */ if (bpf_jit_enable >1 )bpf_jit_dump (prog->len, prog_size,2 , ctx.image); -/* Update the icache */ -flush_icache_range ((unsigned long )header, (unsigned long )(ctx.image + ctx.idx)); -if (!prog->is_func || extra_pass) { -int err; -if (extra_pass && ctx.idx != jit_data->ctx.idx) {pr_err_once ("multi-func JIT bug %d != %d\n" , ctx.idx, jit_data->ctx.idx); goto out_free; } - err =bpf_jit_binary_lock_ro (header); -if (err) { -pr_err_once ("bpf_jit_binary_lock_ro() returned %d\n" , - err); +if (WARN_ON (bpf_jit_binary_pack_finalize (ro_header, header))) { +/* ro_header has been freed */ + ro_header =NULL ; + prog = orig_prog; goto out_free; } +/* +* The instructions have now been copied to the ROX region from +* where they will execute. Now the data cache has to be cleaned to +* the PoU and the I-cache has to be invalidated for the VAs. +*/ +bpf_flush_icache (ro_header, ctx.ro_image + ctx.idx); }else { jit_data->ctx = ctx; - jit_data->image = image_ptr; jit_data->header = header; + jit_data->ro_header = ro_header; } prog->jited =1 ; prog->jited_len = prog_size; - prog->bpf_func = (void *)ctx.image; + prog->bpf_func = (void *)ctx.ro_image;if (!prog->is_func || extra_pass) {int i;@@ -1962,17 +1989,39 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)if (tmp_blinded)bpf_jit_prog_release_other (prog, prog == orig_prog ? tmp : orig_prog); -return prog; out_free: -bpf_jit_binary_free (header); - prog->bpf_func =NULL ; - prog->jited =0 ; - prog->jited_len =0 ; +if (header) { +bpf_arch_text_copy (&ro_header->size, &header->size,sizeof (header->size)); +bpf_jit_binary_pack_free (ro_header, header); + } goto out_offset; } +void bpf_jit_free (struct bpf_prog *prog) +{ +if (prog->jited) { +struct jit_data *jit_data = prog->aux->jit_data; +struct bpf_binary_header *hdr; + +/* +* If we fail the final pass of JIT (from jit_subprogs), +* the program may not be finalized yet. Call finalize here +* before freeing it. +*/ +if (jit_data) { +bpf_jit_binary_pack_finalize (jit_data->ro_header, jit_data->header); +kfree (jit_data); + } + hdr =bpf_jit_binary_pack_hdr (prog); +bpf_jit_binary_pack_free (hdr,NULL ); +WARN_ON_ONCE (!bpf_prog_kallsyms_verify_off (prog)); + } + +bpf_prog_unlock_free (prog); +} +bool bpf_jit_bypass_spec_v1 (void ) {return true;arch/loongarch/net/bpf_jit.h +1
-1@@ -24,7 +24,7 @@ struct jit_ctx {struct jit_data {struct bpf_binary_header *header; -u8 *image; +struct bpf_binary_header *ro_header;struct jit_ctx ctx; };stxSxLQw new file +0
-1--2.43.5
Hengqi Chen
reviewerPATCH 6/7 LoongArch: BPF: Implement PROBE_MEM32 pseudo instructions
In reply to Hengqi ChenAdd support for {LDX,STX,ST} | PROBE_MEM32 | {B,H,W,DW} instructions. They are similar to PROBE_MEM instructions with the following differences:
* PROBE_MEM32 supports store.
* PROBE_MEM32 relies on the verifier to clear upper 32-bit of the src/dst register
* PROBE_MEM32 adds 64-bit kern_vm_start address (which is stored in S6 in the prologue). Due to bpf_arena constructions such S6 + reg + off16 access is guaranteed to be within arena virtual range, so no address check at run-time.
* S6 is a free callee-saved register, so it is used to store arena_vm_start
* PROBE_MEM32 allows STX and ST. If they fault the store is a nop. When LDX faults the destination register is zeroed.
To support these on LoongArch, we employ the t2/t3 registers to store intermediate results of reg_arena + src/dst reg and use the t2/t3 registers as the new src/dst reg. This allows us to reuse most of the existing code.
--- arch/loongarch/net/bpf_jit.c | 69 +++++++++++++++++++++++++++++++++--- arch/loongarch/net/bpf_jit.h | 1 + 2 files changed, 66 insertions(+), 4 deletions(-)
arch/loongarch/net/bpf_jit.c +65 -4
@@ -17,6 +17,7 @@#define LOONGARCH_BPF_FENTRY_NBYTES (LOONGARCH_LONG_JUMP_NINSNS * 4)#define REG_TCC LOONGARCH_GPR_A6 +#define REG_ARENA LOONGARCH_GPR_S6/* For storing arena_vm_start */ #define BPF_TAIL_CALL_CNT_PTR_STACK_OFF (stack) (round_up(stack, 16) - 80)static const int regmap[] = {
@@ -136,6 +137,9 @@ static void build_prologue(struct jit_ctx *ctx)/* To store tcc and tcc_ptr */ stack_adjust +=sizeof (long ) *2 ; +if (ctx->arena_vm_start) + stack_adjust +=8 ; + stack_adjust =round_up (stack_adjust,16 ); stack_adjust += bpf_stack_adjust;
@@ -177,6 +181,11 @@ static void build_prologue(struct jit_ctx *ctx) store_offset -=sizeof (long );emit_insn (ctx, std,LOONGARCH_GPR_S5 ,LOONGARCH_GPR_SP , store_offset); +if (ctx->arena_vm_start) { + store_offset -=sizeof (long ); +emit_insn (ctx, std,REG_ARENA ,LOONGARCH_GPR_SP , store_offset); + } +prepare_bpf_tail_call_cnt (ctx, &store_offset);emit_insn (ctx, addid,LOONGARCH_GPR_FP ,LOONGARCH_GPR_SP , stack_adjust);
@@ -185,6 +194,9 @@ static void build_prologue(struct jit_ctx *ctx)emit_insn (ctx, addid, regmap[BPF_REG_FP ],LOONGARCH_GPR_SP , bpf_stack_adjust); ctx->stack_size = stack_adjust; + +if (ctx->arena_vm_start) +move_imm (ctx,REG_ARENA , ctx->arena_vm_start, false); }static void __build_epilogue (struct jit_ctx *ctx,bool is_tail_call)
@@ -216,6 +228,11 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call) load_offset -=sizeof (long );emit_insn (ctx, ldd,LOONGARCH_GPR_S5 ,LOONGARCH_GPR_SP , load_offset); +if (ctx->arena_vm_start) { + load_offset -=sizeof (long ); +emit_insn (ctx, ldd,REG_ARENA ,LOONGARCH_GPR_SP , load_offset); + } +/* * When push into the stack, follow the order of tcc then tcc_ptr. * When pop from the stack, first pop tcc_ptr then followed by tcc.
@@ -472,6 +489,7 @@ static void sign_extend(struct jit_ctx *ctx, int rd, int rj, u8 size, bool sign)#define BPF_FIXUP_REG_MASK GENMASK(31, 27)#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0) +#define REG_DONT_CLEAR_MARKER 0bool ex_handler_bpf (const struct exception_table_entry *ex,struct pt_regs *regs)
@@ -479,7 +497,8 @@ bool ex_handler_bpf(const struct exception_table_entry *ex,int dst_reg =FIELD_GET (BPF_FIXUP_REG_MASK , ex->fixup);off_t offset =FIELD_GET (BPF_FIXUP_OFFSET_MASK , ex->fixup); - regs->regs[dst_reg] =0 ; +if (dst_reg !=REG_DONT_CLEAR_MARKER ) + regs->regs[dst_reg] =0 ; regs->csr_era = (unsigned long )&ex->fixup - offset;return true;
@@ -498,7 +517,8 @@ static int add_exception_handler(const struct bpf_insn *insn,return 0 ;if (BPF_MODE (insn->code) !=BPF_PROBE_MEM && -BPF_MODE (insn->code) !=BPF_PROBE_MEMSX ) +BPF_MODE (insn->code) !=BPF_PROBE_MEMSX && +BPF_MODE (insn->code) !=BPF_PROBE_MEM32 )return 0 ;if (WARN_ON_ONCE (ctx->num_exentries >= ctx->prog->aux->num_exentries))
@@ -560,8 +580,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extconst u8 cond =BPF_OP (code);const u8 t1 =LOONGARCH_GPR_T1 ;const u8 t2 =LOONGARCH_GPR_T2 ; -const u8 src = regmap[insn->src_reg]; -const u8 dst = regmap[insn->dst_reg]; +const u8 t3 =LOONGARCH_GPR_T3 ; +u8 src = regmap[insn->src_reg]; +u8 dst = regmap[insn->dst_reg];const s16 off = insn->off;const s32 imm = insn->imm;const bool is32 =BPF_CLASS (insn->code) ==BPF_ALU ||BPF_CLASS (insn->code) ==BPF_JMP32 ;
@@ -1069,8 +1090,19 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extcase BPF_LDX |BPF_PROBE_MEMSX |BPF_B :case BPF_LDX |BPF_PROBE_MEMSX |BPF_H :case BPF_LDX |BPF_PROBE_MEMSX |BPF_W : +/* LDX | PROBE_MEM32: dst = *(unsigned size *)(src + REG_ARENA + off) */ +case BPF_LDX |BPF_PROBE_MEM32 |BPF_B : +case BPF_LDX |BPF_PROBE_MEM32 |BPF_H : +case BPF_LDX |BPF_PROBE_MEM32 |BPF_W : +case BPF_LDX |BPF_PROBE_MEM32 |BPF_DW : sign_ext =BPF_MODE (code) ==BPF_MEMSX ||BPF_MODE (code) ==BPF_PROBE_MEMSX ; + +if (BPF_MODE (code) ==BPF_PROBE_MEM32 ) { +emit_insn (ctx, addd, t2, src,REG_ARENA ); + src = t2; + } +switch (BPF_SIZE (code)) {case BPF_B :if (is_signed_imm12 (off)) {
@@ -1130,6 +1162,16 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extcase BPF_ST |BPF_MEM |BPF_H :case BPF_ST |BPF_MEM |BPF_W :case BPF_ST |BPF_MEM |BPF_DW : +/* ST | PROBE_MEM32: *(size *)(dst + REG_ARENA + off) = imm */ +case BPF_ST |BPF_PROBE_MEM32 |BPF_B : +case BPF_ST |BPF_PROBE_MEM32 |BPF_H : +case BPF_ST |BPF_PROBE_MEM32 |BPF_W : +case BPF_ST |BPF_PROBE_MEM32 |BPF_DW : +if (BPF_MODE (code) ==BPF_PROBE_MEM32 ) { +emit_insn (ctx, addd, t3, dst,REG_ARENA ); + dst = t3; + } +switch (BPF_SIZE (code)) {case BPF_B :move_imm (ctx, t1, imm, is32);
@@ -1172,6 +1214,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext }break ; } + + ret =add_exception_handler (insn, ctx,REG_DONT_CLEAR_MARKER ); +if (ret) +return ret;break ;/* *(size *)(dst + off) = src */
@@ -1179,6 +1225,16 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extcase BPF_STX |BPF_MEM |BPF_H :case BPF_STX |BPF_MEM |BPF_W :case BPF_STX |BPF_MEM |BPF_DW : +/* STX | PROBE_MEM32: *(size *)(dst + REG_ARENA + off) = src */ +case BPF_STX |BPF_PROBE_MEM32 |BPF_B : +case BPF_STX |BPF_PROBE_MEM32 |BPF_H : +case BPF_STX |BPF_PROBE_MEM32 |BPF_W : +case BPF_STX |BPF_PROBE_MEM32 |BPF_DW : +if (BPF_MODE (code) ==BPF_PROBE_MEM32 ) { +emit_insn (ctx, addd, t2, dst,REG_ARENA ); + dst = t2; + } +switch (BPF_SIZE (code)) {case BPF_B :if (is_signed_imm12 (off)) {
@@ -1217,6 +1273,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext }break ; } + + ret =add_exception_handler (insn, ctx,REG_DONT_CLEAR_MARKER ); +if (ret) +return ret;break ;case BPF_STX |BPF_ATOMIC |BPF_W :
@@ -1876,6 +1936,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)memset (&ctx,0 ,sizeof (ctx)); ctx.prog = prog; + ctx.arena_vm_start =bpf_arena_get_kern_vm_start (prog->aux->arena); ctx.offset =kvcalloc (prog->len +1 ,sizeof (u32),GFP_KERNEL );if (ctx.offset ==NULL ) {
arch/loongarch/net/bpf_jit.h +1 -0
@@ -20,6 +20,7 @@ struct jit_ctx {union loongarch_instruction *image;union loongarch_instruction *ro_image;u32 stack_size; +u64 arena_vm_start; };struct jit_data {
Signature · 1 line
2.43.5
Hengqi Chen
reviewerPATCH 7/7 LoongArch: BPF: Implement bpf_addr_space_cast instruction
In reply to Hengqi ChenLLVM generates bpf_addr_space_cast instruction while translating pointers between native (zero) address space and __attribute__((address_space(N))). The addr_space=0 is reserved as bpf_arena address space.
rY = addr_space_cast(rX, 0, 1) is processed by the verifier and converted to normal 32-bit move: wX = wY
rY = addr_space_cast(rX, 1, 0) has to be converted by JIT.
--- arch/loongarch/net/bpf_jit.c | 15 +++++++++++++++ arch/loongarch/net/bpf_jit.h | 1 + 2 files changed, 16 insertions(+)
arch/loongarch/net/bpf_jit.c +15 -0
@@ -591,6 +591,15 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext/* dst = src */ case BPF_ALU |BPF_MOV |BPF_X :case BPF_ALU64 |BPF_MOV |BPF_X : +if (insn_is_cast_user (insn)) { +move_reg (ctx, t1, src); +emit_zext_32 (ctx, t1, true); +move_imm (ctx, dst, (ctx->user_vm_start >>32 ) <<32 , false); +emit_insn (ctx, beq, t1,LOONGARCH_GPR_ZERO ,1 ); +emit_insn (ctx, or, t1, dst, t1); +move_reg (ctx, dst, t1); +break ; + }switch (off) {case 0 :move_reg (ctx, dst, src);
@@ -1937,6 +1946,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)memset (&ctx,0 ,sizeof (ctx)); ctx.prog = prog; ctx.arena_vm_start =bpf_arena_get_kern_vm_start (prog->aux->arena); + ctx.user_vm_start =bpf_arena_get_user_vm_start (prog->aux->arena); ctx.offset =kvcalloc (prog->len +1 ,sizeof (u32),GFP_KERNEL );if (ctx.offset ==NULL ) {
@@ -2098,3 +2108,8 @@ bool bpf_jit_supports_subprog_tailcalls(void) {return true; } + +bool bpf_jit_supports_arena (void ) +{ +return true; +}
arch/loongarch/net/bpf_jit.h +1 -0
@@ -21,6 +21,7 @@ struct jit_ctx {union loongarch_instruction *ro_image;u32 stack_size;u64 arena_vm_start; +u64 user_vm_start; };struct jit_data {
Signature · 1 line
2.43.5