Splitting the code emitting part out of x86_call_depth_emit_accounting into separate bpf_call_depth_emit_accounting function, that is defined in bpf_jit_comp.c object. There's no functionality change, but this change will ease up following code emitting patch. Also moving MAX_PATCH_LEN macro in common header, so we do not need to define it for the 3rd time. Signed-off-by: Jiri Olsa --- arch/x86/include/asm/alternative.h | 4 ++-- arch/x86/include/asm/text-patching.h | 2 ++ arch/x86/kernel/alternative.c | 2 -- arch/x86/kernel/callthunks.c | 7 +------ arch/x86/net/bpf_jit_comp.c | 20 ++++++++++++++++---- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index 08af86ef090a..2ef493741186 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -83,7 +83,7 @@ extern void callthunks_patch_builtin_calls(void); extern void callthunks_patch_module_calls(struct callthunk_sites *sites, struct module *mod); extern void *callthunks_translate_call_dest(void *dest); -extern int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip); +extern int x86_call_depth_emit_accounting(u8 *insn_buff, void *func, void *ip); #else static __always_inline void callthunks_patch_builtin_calls(void) {} static __always_inline void @@ -93,7 +93,7 @@ static __always_inline void *callthunks_translate_call_dest(void *dest) { return dest; } -static __always_inline int x86_call_depth_emit_accounting(u8 **pprog, +static __always_inline int x86_call_depth_emit_accounting(u8 *insn_buff, void *func, void *ip) { return 0; diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index f2d142a0a862..a0a7c778b0a2 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -13,6 +13,8 @@ */ #define TEXT_POKE_MAX_OPCODE_SIZE 5 +#define MAX_PATCH_LEN (255-1) + extern void text_poke_early(void *addr, const void *opcode, size_t len); extern void text_poke_apply_relocation(u8 *buf, const u8 * const instr, size_t instrlen, u8 *repl, size_t repl_len); diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 91b1cdd16569..5f9989c8559e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -18,8 +18,6 @@ int __read_mostly alternatives_patched; EXPORT_SYMBOL_GPL(alternatives_patched); -#define MAX_PATCH_LEN (255-1) - #define DA_ALL (~0) #define DA_ALT 0x01 #define DA_RET 0x02 diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index e37728f70322..15b221d06901 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -23,8 +23,6 @@ static int __initdata_or_module debug_callthunks; -#define MAX_PATCH_LEN (255-1) - #define prdbg(fmt, args...) \ do { \ if (debug_callthunks) \ @@ -298,10 +296,9 @@ static bool is_callthunk(void *addr) return !bcmp(pad, insn_buff, tmpl_size); } -int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip) +int x86_call_depth_emit_accounting(u8 *insn_buff, void *func, void *ip) { unsigned int tmpl_size = SKL_TMPL_SIZE; - u8 insn_buff[MAX_PATCH_LEN]; if (!thunks_initialized) return 0; @@ -313,8 +310,6 @@ int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip) memcpy(insn_buff, skl_call_thunk_template, tmpl_size); text_poke_apply_relocation(insn_buff, ip, tmpl_size, skl_call_thunk_template, tmpl_size); - memcpy(*pprog, insn_buff, tmpl_size); - *pprog += tmpl_size; return tmpl_size; } #endif diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 48429fae0641..5922bb4728aa 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -149,6 +149,18 @@ static int bpf_size_to_x86_bytes(int bpf_size) return 0; } +static int bpf_call_depth_emit_accounting(u8 **pprog, void *func, void *ip) +{ + u8 insn_buff[MAX_PATCH_LEN]; + int size; + + size = x86_call_depth_emit_accounting(insn_buff, func, ip); + memcpy(*pprog, insn_buff, size); + + *pprog += size; + return size; +} + /* * List of x86 cond jumps opcodes (. + s8) * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32) @@ -608,7 +620,7 @@ static int emit_call(u8 **pprog, void *func, void *ip) static int emit_rsb_call(u8 **pprog, void *func, void *ip) { OPTIMIZER_HIDE_VAR(func); - ip += x86_call_depth_emit_accounting(pprog, func, ip); + ip += bpf_call_depth_emit_accounting(pprog, func, ip); return emit_patch(pprog, func, ip, 0xE8); } @@ -1653,7 +1665,7 @@ static int emit_spectre_bhb_barrier(u8 **pprog, u8 *ip, ip += 2; func = (u8 *)clear_bhb_loop; - ip += x86_call_depth_emit_accounting(&prog, func, ip); + ip += bpf_call_depth_emit_accounting(&prog, func, ip); if (emit_call(&prog, func, ip)) return -EINVAL; @@ -2661,7 +2673,7 @@ st: insn_off = insn->off; push_r9(&prog); ip += 2; } - ip += x86_call_depth_emit_accounting(&prog, func, ip); + ip += bpf_call_depth_emit_accounting(&prog, func, ip); if (emit_call(&prog, func, ip)) return -EINVAL; if (priv_frame_ptr) @@ -3598,7 +3610,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im * Direct-call fentry stub, as such it needs accounting for the * __fentry__ call. */ - x86_call_depth_emit_accounting(&prog, NULL, image); + bpf_call_depth_emit_accounting(&prog, NULL, image); } EMIT1(0x55); /* push rbp */ EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */ -- 2.54.0