powerpc/bpf: use BPF prog pack allocator
Subsystems (3)
Hari Bathini
maintainerv4 cover powerpc/bpf: use BPF prog pack allocator
Most BPF programs are small, but they consume a page each. For systems with busy traffic and many BPF programs, this may also add significant pressure on instruction TLB. High iTLB pressure usually slows down the whole system causing visible performance degradation for production workloads.
bpf_prog_pack, a customized allocator that packs multiple bpf programs into preallocated memory chunks, was proposed [1] to address it. This series extends this support on powerpc.
Patches 1 & 2 add the arch specific functions needed to support this feature. Patch 3 enables the support for powerpc and ensures cleanup is handled gracefully. Patch 4 introduces patch_instructions() that optimizes some calls while patching more than one instruction. Patch 5 leverages this new function to improve time taken for JIT'ing BPF programs.
Note that the first 3 patches are sufficient to enable the support for bpf_prog_pack on powerpc. Patches 4 & 5 are to improve the JIT compilation time of BPF programs on powerpc.
Changes in v4:
* Updated bpf_patch_instructions() definition in patch 1/5 so that it doesn't have to be updated again in patch 2/5.
* Addressed Christophe's comment on bpf_arch_text_invalidate() return value in patch 2/5.
Changes in v3:
* Fixed segfault issue observed on ppc32 due to inaccurate offset calculation for branching.
* Tried to minimize the performance impact for patch_instruction() with the introduction of patch_instructions().
* Corrected uses of u32* vs ppc_instr_t.
* Moved the change that introduces patch_instructions() to after enabling bpf_prog_pack support.
* Added few comments to improve code readability.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Hari Bathini (5):
powerpc/bpf: implement bpf_arch_text_copy
powerpc/bpf: implement bpf_arch_text_invalidate for bpf_prog_pack powerpc/bpf: use bpf_jit_binary_pack_[alloc|finalize|free] powerpc/code-patching: introduce patch_instructions() powerpc/bpf: use patch_instructions()
arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 94 ++++++++++++--- arch/powerpc/net/bpf_jit.h | 12 +- arch/powerpc/net/bpf_jit_comp.c | 144 ++++++++++++++++++----- arch/powerpc/net/bpf_jit_comp32.c | 13 +- arch/powerpc/net/bpf_jit_comp64.c | 10 +- 6 files changed, 211 insertions(+), 63 deletions(-)
Signature · 1 line
2.41.0
Hari Bathini
maintainerPATCH v4 1/5 powerpc/bpf: implement bpf_arch_text_copy
bpf_arch_text_copy is used to dump JITed binary to RX page, allowing multiple BPF programs to share the same page. Use patch_instruction() to implement it.
--- arch/powerpc/net/bpf_jit_comp.c | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-)
arch/powerpc/net/bpf_jit_comp.c +40 -1
@@ -13,9 +13,12 @@#include <linux/netdevice.h> #include <linux/filter.h> #include <linux/if_vlan.h> -#include <asm/kprobes.h> +#include <linux/memory.h> #include <linux/bpf.h> +#include <asm/kprobes.h> +#include <asm/code-patching.h> +#include "bpf_jit.h" static void bpf_jit_fill_ill_insns (void *area,unsigned int size)
@@ -23,6 +26,28 @@ static void bpf_jit_fill_ill_insns(void *area, unsigned int size)memset32 (area,BREAKPOINT_INSTRUCTION , size /4 ); } +/* +* Patch 'len' bytes of instructions from opcode to addr, one instruction +* at a time. Returns addr on success. ERR_PTR(-EINVAL), otherwise. +*/ +static void *bpf_patch_instructions (void *addr,void *opcode,size_t len,bool fill_insn) +{ +while (len >0 ) { +ppc_inst_t insn =ppc_inst_read (opcode); +int ilen =ppc_inst_len (insn); + +if (patch_instruction (addr, insn)) +return ERR_PTR (-EINVAL ); + + len -= ilen; + addr = addr + ilen; +if (!fill_insn) + opcode = opcode + ilen; + } + +return addr; +} +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx,int tmp_reg,long exit_addr) {if (!exit_addr ||is_offset_in_branch_range (exit_addr - (ctx->idx *4 ))) {
@@ -274,3 +299,17 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct code ctx->exentry_idx++;return 0 ; } + +void *bpf_arch_text_copy (void *dst,void *src,size_t len) +{ +void *ret; + +if (WARN_ON_ONCE (core_kernel_text ((unsigned long )dst))) +return ERR_PTR (-EINVAL ); + +mutex_lock (&text_mutex); + ret =bpf_patch_instructions (dst, src, len, false); +mutex_unlock (&text_mutex); + +return ret; +}
Signature · 1 line
2.41.0
Song Liu
reviewerRe: [PATCH v4 1/5] powerpc/bpf: implement bpf_arch_text_copy
Quoted 34 lines · Hari Bathini
bpf_arch_text_copyis used to dump JITed binary to RX page, allowing multiple BPF programs to share the same page. Usepatch_instruction()to implement it.Signed-off-by Hari Bathini <[email protected]>--- arch/powerpc/net/bpf_jit_comp.c | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-)arch/powerpc/net/bpf_jit_comp.c +6
-1@@ -13,9 +13,12 @@#include <linux/netdevice.h> #include <linux/filter.h> #include <linux/if_vlan.h> -#include <asm/kprobes.h> +#include <linux/memory.h> #include <linux/bpf.h> +#include <asm/kprobes.h> +#include <asm/code-patching.h> +#include "bpf_jit.h" static void bpf_jit_fill_ill_insns (void *area,unsigned int size)@@ -23,6 +26,28 @@ static void bpf_jit_fill_ill_insns(void *area, unsigned int size)memset32 (area,BREAKPOINT_INSTRUCTION , size /4 ); } +/* +* Patch 'len' bytes of instructions from opcode to addr, one instruction
arch/powerpc/net/bpf_jit_comp.c static void bpf_jit_fill_ill_insns(void *area, unsigned int size) · line 40
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp.c +10
-0+* at a time. Returns addr on success. ERR_PTR(-EINVAL), otherwise. +*/ +static void *bpf_patch_instructions (void *addr,void *opcode,size_t len,bool fill_insn) +{ +while (len >0 ) { +ppc_inst_t insn =ppc_inst_read (opcode); +int ilen =ppc_inst_len (insn); + +if (patch_instruction (addr, insn)) +return ERR_PTR (-EINVAL );
Is there any reason we have to do this one instruction at a time? I believe Christophe Leroy pointed out the same in an earlier version?
Thanks,
Song
Quoted 31 lines
arch/powerpc/net/bpf_jit_comp.c +24
-0+ + len -= ilen; + addr = addr + ilen; +if (!fill_insn) + opcode = opcode + ilen; + } + +return addr; +} +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx,int tmp_reg,long exit_addr) {if (!exit_addr ||is_offset_in_branch_range (exit_addr - (ctx->idx *4 ))) {@@ -274,3 +299,17 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct code ctx->exentry_idx++;return 0 ; } + +void *bpf_arch_text_copy (void *dst,void *src,size_t len) +{ +void *ret; + +if (WARN_ON_ONCE (core_kernel_text ((unsigned long )dst))) +return ERR_PTR (-EINVAL ); + +mutex_lock (&text_mutex); + ret =bpf_patch_instructions (dst, src, len, false); +mutex_unlock (&text_mutex); + +return ret; +}
Hari Bathini
maintainerPATCH v4 2/5 powerpc/bpf: implement bpf_arch_text_invalidate for bpf_prog_pack
In reply to Hari BathiniImplement bpf_arch_text_invalidate and use it to fill unused part of the bpf_prog_pack with trap instructions when a BPF program is freed.
--- arch/powerpc/net/bpf_jit_comp.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/powerpc/net/bpf_jit_comp.c +15 -0
@@ -313,3 +313,18 @@ void *bpf_arch_text_copy(void *dst, void *src, size_t len)return ret; } + +int bpf_arch_text_invalidate (void *dst,size_t len) +{ +u32 insn =BREAKPOINT_INSTRUCTION ; +void *ret; + +if (WARN_ON_ONCE (core_kernel_text ((unsigned long )dst))) +return -EINVAL ; + +mutex_lock (&text_mutex); + ret =bpf_patch_instructions (dst, &insn, len, true); +mutex_unlock (&text_mutex); + +return IS_ERR (ret) ?PTR_ERR (ret) :0 ; +}
Signature · 1 line
2.41.0
Hari Bathini
maintainerPATCH v4 3/5 powerpc/bpf: use bpf_jit_binary_pack_[alloc|finalize|free]
In reply to Hari BathiniUse bpf_jit_binary_pack_alloc in powerpc jit. The jit engine first writes the program to the rw buffer. When the jit is done, the program is copied to the final location with bpf_jit_binary_pack_finalize. With multiple jit_subprogs, bpf_jit_free is called on some subprograms that haven't got bpf_jit_binary_pack_finalize() yet. Implement custom bpf_jit_free() like in commit 1d5f82d9dd47 ("bpf, x86: fix freeing of not-finalized bpf_prog_pack") to call bpf_jit_binary_pack_finalize(), if necessary. While here, correct the misnomer powerpc64_jit_data to powerpc_jit_data as it is meant for both ppc32 and ppc64.
--- arch/powerpc/net/bpf_jit.h | 12 ++-- arch/powerpc/net/bpf_jit_comp.c | 110 ++++++++++++++++++++++-------- arch/powerpc/net/bpf_jit_comp32.c | 13 ++-- arch/powerpc/net/bpf_jit_comp64.c | 10 +-- 4 files changed, 98 insertions(+), 47 deletions(-)
arch/powerpc/net/bpf_jit.h +5 -7
@@ -36,9 +36,6 @@EMIT (PPC_RAW_BRANCH (offset)); \ }while (0 ) -/* bl (unconditional 'branch' with link) */ -#define PPC_BL (dest) EMIT(PPC_RAW_BL((dest) - (unsigned long)(image + ctx->idx))) - /* "cond" here covers BO:BI fields. */#define PPC_BCC_SHORT (cond, dest) \ do { \
@@ -169,16 +166,17 @@ static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i) }void bpf_jit_init_reg_mapping (struct codegen_context *ctx); -int bpf_jit_emit_func_call_rel (u32 *image,struct codegen_context *ctx,u64 func); -int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,struct codegen_context *ctx, +int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func); +int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,u32 *fimage,struct codegen_context *ctx,u32 *addrs,int pass,bool extra_pass);void bpf_jit_build_prologue (u32 *image,struct codegen_context *ctx);void bpf_jit_build_epilogue (u32 *image,struct codegen_context *ctx);void bpf_jit_realloc_regs (struct codegen_context *ctx);int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx,int tmp_reg,long exit_addr); -int bpf_add_extable_entry (struct bpf_prog *fp,u32 *image,int pass,struct codegen_context *ctx, -int insn_idx,int jmp_off,int dst_reg); +int bpf_add_extable_entry (struct bpf_prog *fp,u32 *image,u32 *fimage,int pass, +struct codegen_context *ctx,int insn_idx, +int jmp_off,int dst_reg);#endif
arch/powerpc/net/bpf_jit_comp.c +81 -29
220 lines not on this page · raw message
arch/powerpc/net/bpf_jit_comp32.c +7 -6
@@ -200,12 +200,13 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_BLR ()); } -int bpf_jit_emit_func_call_rel (u32 *image,struct codegen_context *ctx,u64 func) +/* Relative offset needs to be calculated based on final image location */ +int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) { -s32 rel = (s32 )func - (s32)(image + ctx->idx); +s32 rel = (s32 )func - (s32)(fimage + ctx->idx);if (image && rel <0x2000000 && rel >=-0x2000000 ) { -PPC_BL (func); +EMIT (PPC_RAW_BL (rel)); }else {/* Load function address into r0 */ EMIT (PPC_RAW_LIS (_R0,IMM_H (func)));
@@ -278,7 +279,7 @@ static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 o }/* Assemble the body code between the prologue & epilogue */ -int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,struct codegen_context *ctx, +int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,u32 *fimage,struct codegen_context *ctx,u32 *addrs,int pass,bool extra_pass) {const struct bpf_insn *insn = fp->insnsi;
@@ -997,7 +998,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * jmp_off +=4 ; } - ret =bpf_add_extable_entry (fp, image, pass, ctx, insn_idx, + ret =bpf_add_extable_entry (fp, image, fimage, pass, ctx, insn_idx, jmp_off, dst_reg);if (ret)return ret;
@@ -1053,7 +1054,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *EMIT (PPC_RAW_STW (bpf_to_ppc (BPF_REG_5 ), _R1,12 )); } - ret =bpf_jit_emit_func_call_rel (image, ctx, func_addr); + ret =bpf_jit_emit_func_call_rel (image, fimage, ctx, func_addr);if (ret)return ret;
arch/powerpc/net/bpf_jit_comp64.c +5 -5
@@ -240,7 +240,7 @@ static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, ureturn 0 ; } -int bpf_jit_emit_func_call_rel (u32 *image,struct codegen_context *ctx,u64 func) +int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) {unsigned int i, ctx_idx = ctx->idx;
@@ -361,7 +361,7 @@ asm ( );/* Assemble the body code between the prologue & epilogue */ -int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,struct codegen_context *ctx, +int bpf_jit_build_body (struct bpf_prog *fp,u32 *image,u32 *fimage,struct codegen_context *ctx,u32 *addrs,int pass,bool extra_pass) {enum stf_barrier_type stf_barrier =stf_barrier_type_get ();
@@ -940,8 +940,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * addrs[++i] = ctx->idx *4 ;if (BPF_MODE (code) ==BPF_PROBE_MEM ) { - ret =bpf_add_extable_entry (fp, image, pass, ctx, ctx->idx -1 , -4 , dst_reg); + ret =bpf_add_extable_entry (fp, image, fimage, pass, ctx, + ctx->idx -1 ,4 , dst_reg);if (ret)return ret; }
@@ -995,7 +995,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *if (func_addr_fixed) ret =bpf_jit_emit_func_call_hlp (image, ctx, func_addr);else - ret =bpf_jit_emit_func_call_rel (image, ctx, func_addr); + ret =bpf_jit_emit_func_call_rel (image, fimage, ctx, func_addr);if (ret)return ret;
Signature · 1 line
2.41.0
Song Liu
reviewerRe: [PATCH v4 3/5] powerpc/bpf: use bpf_jit_binary_pack_[alloc|finalize|free]
Quoted 10 lines · Hari Bathini
Use
bpf_jit_binary_pack_allocin powerpc jit. The jit engine first writes the program to the rw buffer. When the jit is done, the program is copied to the final location withbpf_jit_binary_pack_finalize. With multiplejit_subprogs,bpf_jit_freeis called on some subprograms that haven't gotbpf_jit_binary_pack_finalize()yet. Implement custombpf_jit_free()like in commit1d5f82d9dd47("bpf, x86: fix freeing of not-finalizedbpf_prog_pack") to callbpf_jit_binary_pack_finalize(), if necessary. While here, correct the misnomerpowerpc64_jit_datatopowerpc_jit_dataas it is meant for both ppc32 and ppc64.
I would personally prefer to put the rename to a separate patch.
Quoted 8 lines
Signed-off-by Hari Bathini <[email protected]>--- arch/powerpc/net/bpf_jit.h | 12 ++-- arch/powerpc/net/bpf_jit_comp.c | 110 ++++++++++++++++++++++-------- arch/powerpc/net/bpf_jit_comp32.c | 13 ++-- arch/powerpc/net/bpf_jit_comp64.c | 10 +-- 4 files changed, 98 insertions(+), 47 deletions(-)
[...]
Quoted 5 lines
arch/powerpc/net/bpf_jit_comp.c +0
-1@@ -220,17 +237,19 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)#ifdef CONFIG_PPC64_ELF_ABI_V1 /* Function descriptor nastiness: Address + TOC */ - ((u64 *)image)[0 ] = (u64 )code_base;
arch/powerpc/net/bpf_jit_comp.c struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) · line 231
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp.c +2
-2+ ((u64 *)image)[0 ] = (u64 )fcode_base; ((u64 *)image)[1 ] = local_paca->kernel_toc;#endif - fp->bpf_func = (void *)image; + fp->bpf_func = (void *)fimage; fp->jited =1 ; fp->jited_len = proglen +FUNCTION_DESCR_SIZE ; -bpf_flush_icache (bpf_hdr, (u8 *)bpf_hdr + bpf_hdr->size);
I guess we don't need bpf_flush_icache() any more? So can we remove it from arch/powerpc/net/bpf_jit.h?
Thanks,
Song
Quoted 21 lines
arch/powerpc/net/bpf_jit_comp.c +7
-3if (!fp->is_func || extra_pass) { -bpf_jit_binary_lock_ro (bpf_hdr); +if (bpf_jit_binary_pack_finalize (fp, fhdr, hdr)) { + fp = org_fp; + goto out_addrs; + }bpf_prog_fill_jited_linfo (fp, addrs); out_addrs:kfree (addrs);@@ -240,8 +259,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) jit_data->addrs = addrs; jit_data->ctx = cgctx; jit_data->proglen = proglen; - jit_data->image = image; - jit_data->header = bpf_hdr; + jit_data->fimage = fimage; + jit_data->fhdr = fhdr; + jit_data->hdr = hdr; } out:
[...]
Hari Bathini
maintainerPATCH v4 4/5 powerpc/code-patching: introduce patch_instructions()
In reply to Hari Bathinipatch_instruction() entails setting up pte, patching the instruction, clearing the pte and flushing the tlb. If multiple instructions need to be patched, every instruction would have to go through the above drill unnecessarily. Instead, introduce function patch_instructions() that sets up the pte, clears the pte and flushes the tlb only once per page range of instructions to be patched. This adds a slight overhead to patch_instruction() call while improving the patching time for scenarios where more than one instruction needs to be patched.
--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 94 ++++++++++++++++++++---- 2 files changed, 80 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/code-patching.h +1 -0
@@ -74,6 +74,7 @@ int create_cond_branch(ppc_inst_t *instr, const u32 *addr,int patch_branch (u32 *addr,unsigned long target,int flags);int patch_instruction (u32 *addr,ppc_inst_t instr);int raw_patch_instruction (u32 *addr,ppc_inst_t instr); +int patch_instructions (void *addr,void *code,size_t len,bool fill_insn);static inline unsigned long patch_site_addr (s32 *site) {
arch/powerpc/lib/code-patching.c +79 -15
148 lines not on this page · raw message
Signature · 1 line
2.41.0
Song Liu
reviewerRe: [PATCH v4 4/5] powerpc/code-patching: introduce patch_instructions()
Quoted 11 lines · Hari Bathini
patch_instruction()entails setting up pte, patching the instruction, clearing the pte and flushing the tlb. If multiple instructions need to be patched, every instruction would have to go through the above drill unnecessarily. Instead, introduce functionpatch_instructions()that sets up the pte, clears the pte and flushes the tlb only once per page range of instructions to be patched. This adds a slight overhead topatch_instruction()call while improving the patching time for scenarios where more than one instruction needs to be patched.Signed-off-by Hari Bathini <[email protected]>
I didn't see this one when I reviewed 1/5. Please ignore that comment.
[...]
arch/powerpc/lib/code-patching.c static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) · line 318
Quoted 9 lines
arch/powerpc/lib/code-patching.c +4
-1@@ -307,11 +312,22 @@ static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) orig_mm =start_using_temp_mm (patching_mm); - err =__patch_instruction (addr, instr, patch_addr); +while (len >0 ) { + instr =ppc_inst_read (code); + ilen =ppc_inst_len (instr); + err =__patch_instruction (addr, instr, patch_addr);
It appears we are still repeating a lot of work here. For example, with fill_insn == true, we don't need to repeat ppc_inst_read().
Can we do this with a memcpy or memset like functions?
Quoted 4 lines
arch/powerpc/lib/code-patching.c +4
-0+/* hwsync performed by __patch_instruction (sync) if successful */ +if (err) { +mb ();/* sync */ +break ;
arch/powerpc/lib/code-patching.c static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) · line 329
Quoted 10 lines
arch/powerpc/lib/code-patching.c +6
-3+ } -/* hwsync performed by __patch_instruction (sync) if successful */ -if (err) -mb ();/* sync */ + len -= ilen; + patch_addr = patch_addr + ilen; + addr = (void *)addr + ilen; +if (!fill_insn) + code = code + ilen;
It took me a while to figure out what "fill_insn" means. Maybe call it "repeat_input" or something?
Thanks,
Song
Quoted 8 lines
arch/powerpc/lib/code-patching.c +1
-0+ }/* context synchronisation performed by __patch_instruction (isync or exception) */ stop_using_temp_mm (patching_mm, orig_mm);@@ -328,16 +344,21 @@ static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr)return err; }
Re: [PATCH v4 4/5] powerpc/code-patching: introduce patch_instructions()
Le 26/09/2023 à 00:50, Song Liu a écrit :
Quoted 11 lines · Hari Bathini
Quoted 10 lines
patch_instruction()entails setting up pte, patching the instruction, clearing the pte and flushing the tlb. If multiple instructions need to be patched, every instruction would have to go through the above drill unnecessarily. Instead, introduce functionpatch_instructions()that sets up the pte, clears the pte and flushes the tlb only once per page range of instructions to be patched. This adds a slight overhead topatch_instruction()call while improving the patching time for scenarios where more than one instruction needs to be patched.
Quoted 3 lines
Quoted 1 line
Signed-off-by Hari Bathini <[email protected]>I didn't see this one when I reviewed 1/5. Please ignore that comment.
If I remember correctry, patch 1 introduces a huge performance degradation, which gets then improved with this patch.
As I said before, I'd expect patch 4 to go first then get bpf_arch_text_copy() be implemented with patch_instructions() directly.
Christophe
Quoted 47 lines
[...]
arch/powerpc/lib/code-patching.c static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) · line 318
Quoted 9 lines
arch/powerpc/lib/code-patching.c +4
-1@@ -307,11 +312,22 @@ static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) orig_mm =start_using_temp_mm (patching_mm); - err =__patch_instruction (addr, instr, patch_addr); +while (len >0 ) { + instr =ppc_inst_read (code); + ilen =ppc_inst_len (instr); + err =__patch_instruction (addr, instr, patch_addr);It appears we are still repeating a lot of work here. For example, with
fill_insn== true, we don't need to repeatppc_inst_read().Can we do this with a memcpy or memset like functions?
Quoted 4 lines
arch/powerpc/lib/code-patching.c +4
-0+/* hwsync performed by __patch_instruction (sync) if successful */ +if (err) { +mb ();/* sync */ +break ;arch/powerpc/lib/code-patching.c static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr) · line 329
Quoted 10 lines
arch/powerpc/lib/code-patching.c +6
-3+ } -/* hwsync performed by __patch_instruction (sync) if successful */ -if (err) -mb ();/* sync */ + len -= ilen; + patch_addr = patch_addr + ilen; + addr = (void *)addr + ilen; +if (!fill_insn) + code = code + ilen;It took me a while to figure out what "
fill_insn" means. Maybe call it "repeat_input" or something?Thanks,
SongQuoted 8 lines
arch/powerpc/lib/code-patching.c +1
-0+ }/* context synchronisation performed by __patch_instruction (isync or exception) */ stop_using_temp_mm (patching_mm, orig_mm);@@ -328,16 +344,21 @@ static int __do_patch_instruction_mm(u32 *addr, ppc_inst_t instr)return err; }
Hari Bathini
maintainerRe: [PATCH v4 4/5] powerpc/code-patching: introduce patch_instructions()
Quoted 16 lines · Christophe Leroy
Le 26/09/2023 à 00:50, Song Liu a écrit :
Quoted 13 lines · Hari Bathini
Quoted 11 lines
patch_instruction()entails setting up pte, patching the instruction, clearing the pte and flushing the tlb. If multiple instructions need to be patched, every instruction would have to go through the above drill unnecessarily. Instead, introduce functionpatch_instructions()that sets up the pte, clears the pte and flushes the tlb only once per page range of instructions to be patched. This adds a slight overhead topatch_instruction()call while improving the patching time for scenarios where more than one instruction needs to be patched.Signed-off-by Hari Bathini <[email protected]>
Quoted 8 lines
Quoted 1 line
I didn't see this one when I reviewed 1/5. Please ignore that comment.
If I remember correctry, patch 1 introduces a huge performance degradation, which gets then improved with this patch.
As I said before, I'd expect patch 4 to go first then get
bpf_arch_text_copy()be implemented withpatch_instructions()directly.
Thanks for the reviews, Christophe & Song.
Posted v5 based on your inputs.
- Hari
Hari Bathini
maintainerPATCH v4 5/5 powerpc/bpf: use patch_instructions()
In reply to Hari BathiniUse the newly introduced patch_instructions() that handles patching multiple instructions with one call. This improves speed of exectution for JIT'ing bpf programs.
Without this patch (on a POWER9 lpar):
# time modprobe test_bpf real 2m59.681s user 0m0.000s sys 1m44.160s #
With this patch (on a POWER9 lpar):
# time modprobe test_bpf real 0m5.013s user 0m0.000s sys 0m4.216s #
--- arch/powerpc/net/bpf_jit_comp.c | 34 ++++++--------------------------- 1 file changed, 6 insertions(+), 28 deletions(-)
arch/powerpc/net/bpf_jit_comp.c +6 -28
@@ -26,28 +26,6 @@ static void bpf_jit_fill_ill_insns(void *area, unsigned int size)memset32 (area,BREAKPOINT_INSTRUCTION , size /4 ); } -/* -* Patch 'len' bytes of instructions from opcode to addr, one instruction -* at a time. Returns addr on success. ERR_PTR(-EINVAL), otherwise. -*/ -static void *bpf_patch_instructions (void *addr,void *opcode,size_t len,bool fill_insn) -{ -while (len >0 ) { -ppc_inst_t insn =ppc_inst_read (opcode); -int ilen =ppc_inst_len (insn); - -if (patch_instruction (addr, insn)) -return ERR_PTR (-EINVAL ); - - len -= ilen; - addr = addr + ilen; -if (!fill_insn) - opcode = opcode + ilen; - } - -return addr; -} -int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx,int tmp_reg,long exit_addr) {if (!exit_addr ||is_offset_in_branch_range (exit_addr - (ctx->idx *4 ))) {
@@ -330,31 +308,31 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int passvoid *bpf_arch_text_copy (void *dst,void *src,size_t len) { -void *ret; +int err;if (WARN_ON_ONCE (core_kernel_text ((unsigned long )dst)))return ERR_PTR (-EINVAL );mutex_lock (&text_mutex); - ret =bpf_patch_instructions (dst, src, len, false); + err =patch_instructions (dst, src, len, false);mutex_unlock (&text_mutex); -return ret; +return err ?ERR_PTR (err) : dst; }int bpf_arch_text_invalidate (void *dst,size_t len) {u32 insn =BREAKPOINT_INSTRUCTION ; -void *ret; +int ret;if (WARN_ON_ONCE (core_kernel_text ((unsigned long )dst)))return -EINVAL ;mutex_lock (&text_mutex); - ret =bpf_patch_instructions (dst, &insn, len, true); + ret =patch_instructions (dst, &insn, len, true);mutex_unlock (&text_mutex); -return IS_ERR (ret) ?PTR_ERR (ret) :0 ; +return ret; }void bpf_jit_free (struct bpf_prog *fp)
Signature · 1 line
2.41.0
Hari Bathini
maintainerPing!
Comments, please..
Quoted 56 lines · Hari Bathini
Most BPF programs are small, but they consume a page each. For systems with busy traffic and many BPF programs, this may also add significant pressure on instruction TLB. High iTLB pressure usually slows down the whole system causing visible performance degradation for production workloads.
bpf_prog_pack, a customized allocator that packs multiple bpf programs into preallocated memory chunks, was proposed [1] to address it. This series extends this support on powerpc.Patches 1 & 2 add the arch specific functions needed to support this feature. Patch 3 enables the support for powerpc and ensures cleanup is handled gracefully. Patch 4 introduces
patch_instructions()that optimizes some calls while patching more than one instruction. Patch 5 leverages this new function to improve time taken for JIT'ing BPF programs.Note that the first 3 patches are sufficient to enable the support for
bpf_prog_packon powerpc. Patches 4 & 5 are to improve the JIT compilation time of BPF programs on powerpc.Changes in v4:
* Updatedbpf_patch_instructions()definition in patch 1/5 so that it doesn't have to be updated again in patch 2/5.
* Addressed Christophe's comment onbpf_arch_text_invalidate()return value in patch 2/5.Changes in v3:
* Fixed segfault issue observed on ppc32 due to inaccurate offset calculation for branching.
* Tried to minimize the performance impact forpatch_instruction()with the introduction ofpatch_instructions().
* Corrected uses of u32* vsppc_instr_t.
* Moved the change that introducespatch_instructions()to after enablingbpf_prog_packsupport.
* Added few comments to improve code readability.[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Hari Bathini (5):
powerpc/bpf: implementbpf_arch_text_copy
powerpc/bpf: implementbpf_arch_text_invalidateforbpf_prog_packpowerpc/bpf: use bpf_jit_binary_pack_[alloc|finalize|free] powerpc/code-patching: introducepatch_instructions()powerpc/bpf: usepatch_instructions()arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 94 ++++++++++++--- arch/powerpc/net/bpf_jit.h | 12 +- arch/powerpc/net/bpf_jit_comp.c | 144 ++++++++++++++++++----- arch/powerpc/net/bpf_jit_comp32.c | 13 +- arch/powerpc/net/bpf_jit_comp64.c | 10 +- 6 files changed, 211 insertions(+), 63 deletions(-)