powerpc: Core ftrace rework, support for ftrace direct and bpf trampolines
Hari Bathini
maintainerv5 cover powerpc: Core ftrace rework, support for ftrace direct and bpf trampolines
This is v5 of the series posted here:
https://lore.kernel.org/all/[email protected]/
This series reworks core ftrace support on powerpc to have the function profiling sequence moved out of line. This enables us to have a single nop at kernel function entry virtually eliminating effect of the function tracer when it is not enabled. The function profile sequence is moved out of line and is allocated at two separate places depending on a new config option.
For 64-bit powerpc, the function profiling sequence is also updated to include an additional instruction 'mtlr r0' after the usual two-instruction sequence to fix link stack imbalance (return address predictor) when ftrace is enabled. This showed an improvement of ~10% in null_syscall benchmark (NR_LOOPS=10000000) on a Power 10 system with ftrace enabled.
Finally, support for ftrace direct calls is added based on support for DYNAMIC_FTRACE_WITH_CALL_OPS. BPF Trampoline support is added atop this.
Support for ftrace direct calls is added for 32-bit powerpc. There is some code to enable bpf trampolines for 32-bit powerpc, but it is not complete and will need to be pursued separately.
Patches 1 to 10 are independent of this series and can go in separately though. Rest of the patches depend on the series from Benjamin Gray adding support for patch_uint() and patch_ulong():
https://lore.kernel.org/all/[email protected]/
Changelog v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*
* Fixed ftrace stack tracer failure due to inadvertent use of 'add r7, r3, MCOUNT_INSN_SIZE' instruction instead of 'addi r7, r3, MCOUNT_INSN_SIZE'
* Fixed build error for !CONFIG_MODULES case.
* .vmlinux.arch.* files compiled under arch/powerpc/tools
* Made sure .vmlinux.arch.* files are cleaned with make clean
* num_ool_stubs_text_end used for setting up ftrace_ool_stub_text_end set to zero instead of computing to some random negative value when not required.
* Resolved checkpatch.pl warnings.
* Dropped RFC tag.
Changelog v4:
- Patches 1, 10 and 13 are new.
- Address review comments from Nick. Numerous changes throughout the patch series.
- Extend support for ftrace ool to vmlinux text up to 64MB (patch 13).
- Address remaining TODOs in support for BPF Trampolines.
- Update synchronization when patching instructions during trampoline attach/detach.
Naveen N Rao (17):
powerpc/trace: Account for -fpatchable-function-entry support by
toolchain
powerpc/kprobes: Use ftrace to determine if a probe is at function
entry
powerpc64/ftrace: Nop out additional 'std' instruction emitted by gcc
v5.x
powerpc32/ftrace: Unify 32-bit and 64-bit ftrace entry code powerpc/module_64: Convert #ifdef to IS_ENABLED() powerpc/ftrace: Remove pointer to struct module from dyn_arch_ftrace powerpc/ftrace: Skip instruction patching if the instructions are the
same
powerpc/ftrace: Move ftrace stub used for init text before _einittext powerpc64/bpf: Fold bpf_jit_emit_func_call_hlp() into
bpf_jit_emit_func_call_rel()
powerpc/ftrace: Add a postlink script to validate function tracer
kbuild: Add generic hook for architectures to use before the final
vmlinux link
powerpc64/ftrace: Move ftrace sequence out of line powerpc64/ftrace: Support .text larger than 32MB with out-of-line
stubs
powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_CALL_OPS powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS samples/ftrace: Add support for ftrace direct samples on powerpc powerpc64/bpf: Add support for bpf trampolines
arch/Kconfig | 6 + arch/powerpc/Kbuild | 2 +- arch/powerpc/Kconfig | 23 +- arch/powerpc/Makefile | 8 + arch/powerpc/Makefile.postlink | 8 + arch/powerpc/include/asm/ftrace.h | 33 +- arch/powerpc/include/asm/module.h | 5 + arch/powerpc/include/asm/ppc-opcode.h | 14 + arch/powerpc/kernel/asm-offsets.c | 11 + arch/powerpc/kernel/kprobes.c | 18 +- arch/powerpc/kernel/module_64.c | 66 +- arch/powerpc/kernel/trace/Makefile | 11 +- arch/powerpc/kernel/trace/ftrace.c | 298 ++++++- arch/powerpc/kernel/trace/ftrace_64_pg.c | 69 +- arch/powerpc/kernel/trace/ftrace_entry.S | 244 ++++-- arch/powerpc/kernel/vmlinux.lds.S | 3 +- arch/powerpc/net/bpf_jit.h | 12 + arch/powerpc/net/bpf_jit_comp.c | 847 +++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 7 +- arch/powerpc/net/bpf_jit_comp64.c | 68 +- arch/powerpc/tools/Makefile | 12 + arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 52 ++ arch/powerpc/tools/ftrace_check.sh | 50 ++ samples/ftrace/ftrace-direct-modify.c | 85 +- samples/ftrace/ftrace-direct-multi-modify.c | 101 ++- samples/ftrace/ftrace-direct-multi.c | 79 +- samples/ftrace/ftrace-direct-too.c | 83 +- samples/ftrace/ftrace-direct.c | 69 +- scripts/Makefile.vmlinux | 7 + scripts/link-vmlinux.sh | 7 +- 30 files changed, 2098 insertions(+), 200 deletions(-) create mode 100644 arch/powerpc/tools/Makefile create mode 100755 arch/powerpc/tools/ftrace-gen-ool-stubs.sh create mode 100755 arch/powerpc/tools/ftrace_check.sh
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 1/17 powerpc/trace: Account for -fpatchable-function-entry support by toolchain
From: Naveen N Rao <[email protected]>
So far, we have relied on the fact that gcc supports both -mprofile-kernel, as well as -fpatchable-function-entry, and clang supports neither. Our Makefile only checks for CONFIG_MPROFILE_KERNEL to decide which files to build. Clang has a feature request out [*] to implement -fpatchable-function-entry, and is unlikely to support -mprofile-kernel.
Update our Makefile checks so that we pick up the correct files to build once clang picks up support for -fpatchable-function-entry.
[*] https://github.com/llvm/llvm-project/issues/57031
--- arch/powerpc/kernel/trace/Makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
arch/powerpc/kernel/trace/Makefile +7 -4
@@ -9,12 +9,15 @@ CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_ftrace_64_pg.o = $(CC_FLAGS_FTRACE) endif -obj32-$(CONFIG_FUNCTION_TRACER) += ftrace.o ftrace_entry.o -ifdef CONFIG_MPROFILE_KERNEL -obj64-$(CONFIG_FUNCTION_TRACER) += ftrace.o ftrace_entry.o +ifdef CONFIG_FUNCTION_TRACER +obj32-y += ftrace.o ftrace_entry.o +ifeq ($(CONFIG_MPROFILE_KERNEL)$(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY),) +obj64-y += ftrace_64_pg.o ftrace_64_pg_entry.o else -obj64-$(CONFIG_FUNCTION_TRACER) += ftrace_64_pg.o ftrace_64_pg_entry.o +obj64-y += ftrace.o ftrace_entry.o +endif endif + obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y)
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 2/17 powerpc/kprobes: Use ftrace to determine if a probe is at function entry
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Rather than hard-coding the offset into a function to be used to determine if a kprobe is at function entry, use ftrace_location() to determine the ftrace location within the function and categorize all instructions till that offset to be function entry.
For functions that cannot be traced, we fall back to using a fixed offset of 8 (two instructions) to categorize a probe as being at function entry for 64-bit elfv2, unless we are using pcrel.
--- arch/powerpc/kernel/kprobes.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
arch/powerpc/kernel/kprobes.c +8 -10
@@ -105,24 +105,22 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)return addr; } -static bool arch_kprobe_on_func_entry (unsigned long offset) +static bool arch_kprobe_on_func_entry (unsigned long addr,unsigned long offset) { -#ifdef CONFIG_PPC64_ELF_ABI_V2 -#ifdef CONFIG_KPROBES_ON_FTRACE -return offset <=16 ; -#else -return offset <=8 ; -#endif -#else +unsigned long ip =ftrace_location (addr); + +if (ip) +return offset <= (ip - addr); +if (IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 ) && !IS_ENABLED (CONFIG_PPC_KERNEL_PCREL )) +return offset <=8 ;return !offset; -#endif }/* XXX try and fold the magic of kprobe_lookup_name() in this */ kprobe_opcode_t *arch_adjust_kprobe_addr (unsigned long addr,unsigned long offset,bool *on_func_entry) { - *on_func_entry =arch_kprobe_on_func_entry (offset); + *on_func_entry =arch_kprobe_on_func_entry (addr, offset);return (kprobe_opcode_t *)(addr + offset); }
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 3/17 powerpc64/ftrace: Nop out additional 'std' instruction emitted by gcc v5.x
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Gcc v5.x emits a 3-instruction sequence for -mprofile-kernel:
mflr r0 std r0, 16(r1) bl _mcount
Gcc v6.x moved to a simpler 2-instruction sequence by removing the 'std' instruction. The store saved the return address in the LR save area in the caller stack frame for stack unwinding. However, with dynamic ftrace, we no longer have a call to _mcount on kernel boot when ftrace is not enabled. When ftrace is enabled, that store is performed within ftrace_caller(). As such, the additional 'std' instruction is redundant. Nop it out on kernel boot.
With this change, we now use the same 2-instruction profiling sequence with both -mprofile-kernel, as well as -fpatchable-function-entry on 64-bit powerpc.
--- arch/powerpc/kernel/trace/ftrace.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +5 -1
@@ -246,8 +246,12 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)/* Expected sequence: 'mflr r0', ['std r0,16(r1)'], 'bl _mcount' */ ret =ftrace_read_inst (ip -4 , &old);if (!ret && !ppc_inst_equal (old,ppc_inst (PPC_RAW_MFLR (_R0)))) { +/* Gcc v5.x emit the additional 'std' instruction, gcc v6.x don't */ ret =ftrace_validate_inst (ip -8 ,ppc_inst (PPC_RAW_MFLR (_R0))); - ret |=ftrace_validate_inst (ip -4 ,ppc_inst (PPC_RAW_STD (_R0, _R1,16 ))); +if (ret) +return ret; + ret =ftrace_modify_code (ip -4 ,ppc_inst (PPC_RAW_STD (_R0, _R1,16 )), +ppc_inst (PPC_RAW_NOP ())); } }else {return -EINVAL ;
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 4/17 powerpc32/ftrace: Unify 32-bit and 64-bit ftrace entry code
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
On 32-bit powerpc, gcc generates a three instruction sequence for function profiling:
mflr r0 stw r0, 4(r1) bl _mcount
On kernel boot, the call to _mcount() is nop-ed out, to be patched back in when ftrace is actually enabled. The 'stw' instruction therefore is not necessary unless ftrace is enabled. Nop it out during ftrace init.
When ftrace is enabled, we want the 'stw' so that stack unwinding works properly. Perform the same within the ftrace handler, similar to 64-bit powerpc.
--- arch/powerpc/kernel/trace/ftrace.c | 6 ++++-- arch/powerpc/kernel/trace/ftrace_entry.S | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +4 -2
@@ -240,8 +240,10 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) }else if (IS_ENABLED (CONFIG_PPC32 )) {/* Expected sequence: 'mflr r0', 'stw r0,4(r1)', 'bl _mcount' */ ret =ftrace_validate_inst (ip -8 ,ppc_inst (PPC_RAW_MFLR (_R0))); -if (!ret) - ret =ftrace_validate_inst (ip -4 ,ppc_inst (PPC_RAW_STW (_R0, _R1,4 ))); +if (ret) +return ret; + ret =ftrace_modify_code (ip -4 ,ppc_inst (PPC_RAW_STW (_R0, _R1,4 )), +ppc_inst (PPC_RAW_NOP ())); }else if (IS_ENABLED (CONFIG_MPROFILE_KERNEL )) {/* Expected sequence: 'mflr r0', ['std r0,16(r1)'], 'bl _mcount' */ ret =ftrace_read_inst (ip -4 , &old);
arch/powerpc/kernel/trace/ftrace_entry.S +2 -2
@@ -33,6 +33,8 @@ * and then arrange for the ftrace function to be called. */ .macro ftrace_regs_entry allregs + /* Save the original return address in A's stack frame */ + PPC_STL r0, LRSAVE(r1) /* Create a minimal stack frame for representing B */ PPC_STLU r1, -STACK_FRAME_MIN_SIZE(r1)
@@ -44,8 +46,6 @@ SAVE_GPRS(3, 10, r1) #ifdef CONFIG_PPC64 - /* Save the original return address in A's stack frame */ - std r0, LRSAVE+SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE(r1) /* Ok to continue? */ lbz r3, PACA_FTRACE_ENABLED(r13) cmpdi r3, 0
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 5/17 powerpc/module_64: Convert #ifdef to IS_ENABLED()
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Minor refactor for converting #ifdef to IS_ENABLED().
--- arch/powerpc/kernel/module_64.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/kernel/module_64.c +2 -8
@@ -241,14 +241,8 @@ static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, } } -#ifdef CONFIG_DYNAMIC_FTRACE -/* make the trampoline to the ftrace_caller */ - relocs++; -#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS -/* an additional one for ftrace_regs_caller */ - relocs++; -#endif -#endif +/* stubs for ftrace_caller and ftrace_regs_caller */ + relocs +=IS_ENABLED (CONFIG_DYNAMIC_FTRACE ) +IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_REGS );pr_debug ("Looks like a total of %lu stubs, max\n" , relocs);return relocs *sizeof (struct ppc64_stub_entry );
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 6/17 powerpc/ftrace: Remove pointer to struct module from dyn_arch_ftrace
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Pointer to struct module is only relevant for ftrace records belonging to kernel modules. Having this field in dyn_arch_ftrace wastes memory for all ftrace records belonging to the kernel. Remove the same in favour of looking up the module from the ftrace record address, similar to other architectures.
--- arch/powerpc/include/asm/ftrace.h | 1 - arch/powerpc/kernel/trace/ftrace.c | 49 +++++++++-------- arch/powerpc/kernel/trace/ftrace_64_pg.c | 69 ++++++++++-------------- 3 files changed, 56 insertions(+), 63 deletions(-)
arch/powerpc/include/asm/ftrace.h +0 -1
@@ -24,7 +24,6 @@ unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,struct module ;struct dyn_ftrace ;struct dyn_arch_ftrace { -struct module *mod; };#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
arch/powerpc/kernel/trace/ftrace.c +28 -21
70 lines not on this page · raw message
arch/powerpc/kernel/trace/ftrace_64_pg.c +28 -41
111 lines not on this page · raw message
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 7/17 powerpc/ftrace: Skip instruction patching if the instructions are the same
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
To simplify upcoming changes to ftrace, add a check to skip actual instruction patching if the old and new instructions are the same. We still validate that the instruction is what we expect, but don't actually patch the same instruction again.
--- arch/powerpc/kernel/trace/ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +1 -1
@@ -82,7 +82,7 @@ static inline int ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_ {int ret =ftrace_validate_inst (ip, old); -if (!ret) +if (!ret && !ppc_inst_equal (old, new)) ret =patch_instruction ((u32 *)ip, new);return ret;
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 8/17 powerpc/ftrace: Move ftrace stub used for init text before _einittext
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Move the ftrace stub used to cover inittext before _einittext so that it is within kernel text, as seen through core_kernel_text(). This is required for a subsequent change to ftrace.
--- arch/powerpc/kernel/vmlinux.lds.S | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/powerpc/kernel/vmlinux.lds.S +1 -2
@@ -265,14 +265,13 @@ SECTIONS .init.text : AT(ADDR(.init.text) - LOAD_OFFSET) { _sinittext = .; INIT_TEXT - + *(.tramp.ftrace.init); /* *.init.text might be RO so we must ensure this section ends on * a page boundary. */ . = ALIGN(PAGE_SIZE); _einittext = .; - *(.tramp.ftrace.init); } :text /* .exit.text is discarded at runtime, not link time,
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 9/17 powerpc64/bpf: Fold bpf_jit_emit_func_call_hlp() into bpf_jit_emit_func_call_rel()
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Commit 61688a82e047 ("powerpc/bpf: enable kfunc call") enhanced bpf_jit_emit_func_call_hlp() to handle calls out to module region, where bpf progs are generated. The only difference now between bpf_jit_emit_func_call_hlp() and bpf_jit_emit_func_call_rel() is in handling of the initial pass where target function address is not known. Fold that logic into bpf_jit_emit_func_call_hlp() and rename it to bpf_jit_emit_func_call_rel() to simplify bpf function call JIT code.
We don't actually need to load/restore TOC across a call out to a different kernel helper or to a different bpf program since they all work with the kernel TOC. We only need to do it if we have to call out to a module function. So, guard TOC load/restore with appropriate conditions.
--- arch/powerpc/net/bpf_jit_comp64.c | 61 +++++++++---------------------- 1 file changed, 17 insertions(+), 44 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +17 -44
@@ -202,14 +202,22 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_BLR ()); } -static int -bpf_jit_emit_func_call_hlp (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) +int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) {unsigned long func_addr = func ?ppc_function_entry ((void *)func) :0 ;long reladdr; -if (WARN_ON_ONCE (!kernel_text_address (func_addr))) -return -EINVAL ; +/* bpf to bpf call, func is not known in the initial pass. Emit 5 nops as a placeholder */ +if (!func) { +for (int i =0 ; i <5 ; i++) +EMIT (PPC_RAW_NOP ()); +/* elfv1 needs an additional instruction to load addr from descriptor */ +if (IS_ENABLED (CONFIG_PPC64_ELF_ABI_V1 )) +EMIT (PPC_RAW_NOP ()); +EMIT (PPC_RAW_MTCTR (_R12)); +EMIT (PPC_RAW_BCTRL ()); +return 0 ; + }#ifdef CONFIG_PPC_KERNEL_PCREL reladdr = func_addr - local_paca->kernelbase;
@@ -266,7 +274,8 @@ bpf_jit_emit_func_call_hlp(u32 *image, u32 *fimage, struct codegen_context *ctx,* We can clobber r2 since we get called through a * function pointer (so caller will save/restore r2). */ -EMIT (PPC_RAW_LD (_R2,bpf_to_ppc (TMP_REG_2 ),8 )); +if (is_module_text_address (func_addr)) +EMIT (PPC_RAW_LD (_R2,bpf_to_ppc (TMP_REG_2 ),8 )); }else {PPC_LI64 (_R12, func);EMIT (PPC_RAW_MTCTR (_R12));
@@ -276,46 +285,14 @@ bpf_jit_emit_func_call_hlp(u32 *image, u32 *fimage, struct codegen_context *ctx,* Load r2 with kernel TOC as kernel TOC is used if function address falls * within core kernel text. */ -EMIT (PPC_RAW_LD (_R2, _R13, offsetof(struct paca_struct , kernel_toc))); +if (is_module_text_address (func_addr)) + EMIT(PPC_RAW_LD(_R2, _R13, offsetof(struct paca_struct, kernel_toc))); }#endif return 0 ; } -int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) -{ -unsigned int i, ctx_idx = ctx->idx; - -if (WARN_ON_ONCE (func &&is_module_text_address (func))) -return -EINVAL ; - -/* skip past descriptor if elf v1 */ - func +=FUNCTION_DESCR_SIZE ; - -/* Load function address into r12 */ -PPC_LI64 (_R12, func); - -/* For bpf-to-bpf function calls, the callee's address is unknown -* until the last extra pass. As seen above, we use PPC_LI64() to -* load the callee's address, but this may optimize the number of -* instructions required based on the nature of the address. -* -* Since we don't want the number of instructions emitted to increase, -* we pad the optimized PPC_LI64() call with NOPs to guarantee that -* we always have a five-instruction sequence, which is the maximum -* that PPC_LI64() can emit. -*/ -if (!image) -for (i = ctx->idx - ctx_idx; i <5 ; i++) -EMIT (PPC_RAW_NOP ()); - -EMIT (PPC_RAW_MTCTR (_R12)); -EMIT (PPC_RAW_BCTRL ()); - -return 0 ; -} -static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/*
@@ -1102,11 +1079,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codeif (ret <0 )return ret; -if (func_addr_fixed) - ret =bpf_jit_emit_func_call_hlp (image, fimage, ctx, func_addr); -else - ret =bpf_jit_emit_func_call_rel (image, fimage, ctx, func_addr); - + ret =bpf_jit_emit_func_call_rel (image, fimage, ctx, func_addr);if (ret)return ret;
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 10/17 powerpc/ftrace: Add a postlink script to validate function tracer
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Function tracer on powerpc can only work with vmlinux having a .text size of up to ~64MB due to powerpc branch instruction having a limited relative branch range of 32MB. Today, this is only detected on kernel boot when ftrace is init'ed. Add a post-link script to check the size of .text so that we can detect this at build time, and break the build if necessary.
We add a dependency on !COMPILE_TEST for CONFIG_HAVE_FUNCTION_TRACER so that allyesconfig and other test builds can continue to work without enabling ftrace.
--- arch/powerpc/Kconfig | 2 +- arch/powerpc/Makefile.postlink | 8 +++++ arch/powerpc/tools/ftrace_check.sh | 50 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 arch/powerpc/tools/ftrace_check.sh
arch/powerpc/Kconfig +1 -1
@@ -243,7 +243,7 @@ config PPC select HAVE_FUNCTION_DESCRIPTORS if PPC64_ELF_ABI_V1 select HAVE_FUNCTION_ERROR_INJECTION select HAVE_FUNCTION_GRAPH_TRACER - select HAVE_FUNCTION_TRACER if PPC64 || (PPC32 && CC_IS_GCC) + select HAVE_FUNCTION_TRACER if !COMPILE_TEST && (PPC64 || (PPC32 && CC_IS_GCC)) select HAVE_GCC_PLUGINS if GCC_VERSION >= 50200 # plugin support on gcc <= 5.1 is buggy on PPC select HAVE_GENERIC_VDSO select HAVE_HARDLOCKUP_DETECTOR_ARCH if PPC_BOOK3S_64 && SMP
arch/powerpc/Makefile.postlink +8 -0
@@ -24,6 +24,9 @@ else $(CONFIG_SHELL) $(srctree)/arch/powerpc/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@" endif +quiet_cmd_ftrace_check = CHKFTRC $@ + cmd_ftrace_check = $(CONFIG_SHELL) $(srctree)/arch/powerpc/tools/ftrace_check.sh "$(NM)" "$@" + # `@true` prevents complaint when there is nothing to be done vmlinux: FORCE
@@ -34,6 +37,11 @@ endif ifdef CONFIG_RELOCATABLE $(call if_changed,relocs_check) endif +ifdef CONFIG_FUNCTION_TRACER +ifndef CONFIG_PPC64_ELF_ABI_V1 + $(call cmd,ftrace_check) +endif +endif clean: rm -f .tmp_symbols.txt
arch/powerpc/tools/ftrace_check.sh new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 11/17 kbuild: Add generic hook for architectures to use before the final vmlinux link
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
On powerpc, we would like to be able to make a pass on vmlinux.o and generate a new object file to be linked into vmlinux. Add a generic pass in Makefile.vmlinux that architectures can use for this purpose.
Architectures need to select CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX and must provide arch/<arch>/tools/Makefile with .arch.vmlinux.o target, which will be invoked prior to the final vmlinux link step.
---
Changes in v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*
arch/Kconfig | 6 ++++++ scripts/Makefile.vmlinux | 7 +++++++ scripts/link-vmlinux.sh | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-)
arch/Kconfig +6 -0
@@ -1643,4 +1643,10 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT config ARCH_NEED_CMPXCHG_1_EMU bool +config ARCH_WANTS_PRE_LINK_VMLINUX + def_bool n + help + An architecture can select this if it provides arch/<arch>/tools/Makefile + with .arch.vmlinux.o target to be linked into vmlinux. + endmenu
scripts/Makefile.vmlinux +7 -0
@@ -22,6 +22,13 @@ targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endif +ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX +vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.o + +arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.o + $(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@ +endif + ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) # Final link of vmlinux with optional arch pass after final link
scripts/link-vmlinux.sh +6 -1
@@ -100,7 +100,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - ${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs} + ${kallsymso} ${btf_vmlinux_bin_o} ${arch_vmlinux_o} ${ldlibs} } # generate .BTF typeinfo from DWARF debuginfo
@@ -214,6 +214,11 @@ fi ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o +arch_vmlinux_o="" +if is_enabled CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX; then + arch_vmlinux_o=arch/${SRCARCH}/tools/.vmlinux.arch.o +fi + btf_vmlinux_bin_o= kallsymso= strip_debug=
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 12/17 powerpc64/ftrace: Move ftrace sequence out of line
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Function profile sequence on powerpc includes two instructions at the beginning of each function:
mflr r0 bl ftrace_caller
The call to ftrace_caller() gets nop'ed out during kernel boot and is patched in when ftrace is enabled.
Given the sequence, we cannot return from ftrace_caller with 'blr' as we need to keep LR and r0 intact. This results in link stack (return address predictor) imbalance when ftrace is enabled. To address that, we would like to use a three instruction sequence:
mflr r0 bl ftrace_caller mtlr r0
Further more, to support DYNAMIC_FTRACE_WITH_CALL_OPS, we need to reserve two instruction slots before the function. This results in a total of five instruction slots to be reserved for ftrace use on each function that is traced.
Move the function profile sequence out-of-line to minimize its impact. To do this, we reserve a single nop at function entry using -fpatchable-function-entry=1 and add a pass on vmlinux.o to determine the total number of functions that can be traced. This is then used to generate a .S file reserving the appropriate amount of space for use as ftrace stubs, which is built and linked into vmlinux.
On bootup, the stub space is split into separate stubs per function and populated with the proper instruction sequence. A pointer to the associated stub is maintained in dyn_arch_ftrace.
For modules, space for ftrace stubs is reserved from the generic module stub space.
This is restricted to and enabled by default only on 64-bit powerpc, though there are some changes to accommodate 32-bit powerpc. This is done so that 32-bit powerpc could choose to opt into this based on further tests and benchmarks.
As an example, after this patch, kernel functions will have a single nop at function entry:
<kernel_clone>:
addis r2,r12,467 addi r2,r2,-16028 nop mfocrf r11,8 ...
When ftrace is enabled, the nop is converted to an unconditional branch to the stub associated with that function:
<kernel_clone>:
addis r2,r12,467 addi r2,r2,-16028 b ftrace_ool_stub_text_end+0x11b28 mfocrf r11,8 ...
The associated stub:
<ftrace_ool_stub_text_end+0x11b28>:
mflr r0 bl ftrace_caller mtlr r0 b kernel_clone+0xc ...
This change showed an improvement of ~10% in null_syscall benchmark on a Power 10 system with ftrace enabled.
---
Changes in v5:
* Fixed ftrace stack tracer failure due to inadvertent use of 'add r7, r3, MCOUNT_INSN_SIZE' instruction instead of 'addi r7, r3, MCOUNT_INSN_SIZE'
* Fixed build error for !CONFIG_MODULES case.
* .vmlinux.arch.* files compiled under arch/powerpc/tools
* Made sure .vmlinux.arch.* files are cleaned with make clean
arch/powerpc/Kbuild | 2 +- arch/powerpc/Kconfig | 5 + arch/powerpc/Makefile | 4 + arch/powerpc/include/asm/ftrace.h | 11 ++ arch/powerpc/include/asm/module.h | 5 + arch/powerpc/kernel/asm-offsets.c | 4 + arch/powerpc/kernel/module_64.c | 58 +++++++- arch/powerpc/kernel/trace/ftrace.c | 162 +++++++++++++++++++-- arch/powerpc/kernel/trace/ftrace_entry.S | 116 +++++++++++---- arch/powerpc/tools/Makefile | 12 ++ arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 43 ++++++ 11 files changed, 384 insertions(+), 38 deletions(-) create mode 100644 arch/powerpc/tools/Makefile create mode 100755 arch/powerpc/tools/ftrace-gen-ool-stubs.sh
arch/powerpc/Kbuild +1 -1
@@ -19,4 +19,4 @@ obj-$(CONFIG_KEXEC_CORE) += kexec/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ # for cleaning -subdir- += boot +subdir- += boot tools
arch/powerpc/Kconfig +5 -0
@@ -568,6 +568,11 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh $(CC) -mbig-endian) if PPC64 && CPU_BIG_ENDIAN +config PPC_FTRACE_OUT_OF_LINE + def_bool PPC64 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY + depends on PPC64 + select ARCH_WANTS_PRE_LINK_VMLINUX + config HOTPLUG_CPU bool "Support for enabling/disabling CPUs" depends on SMP && (PPC_PSERIES || \
arch/powerpc/Makefile +4 -0
@@ -155,7 +155,11 @@ CC_FLAGS_NO_FPU := $(call cc-option,-msoft-float) ifdef CONFIG_FUNCTION_TRACER ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY +ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +CC_FLAGS_FTRACE := -fpatchable-function-entry=1 +else CC_FLAGS_FTRACE := -fpatchable-function-entry=2 +endif else CC_FLAGS_FTRACE := -pg ifdef CONFIG_MPROFILE_KERNEL
arch/powerpc/include/asm/ftrace.h +11 -0
@@ -24,6 +24,10 @@ unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,struct module ;struct dyn_ftrace ;struct dyn_arch_ftrace { +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +/* pointer to the associated out-of-line stub */ +unsigned long ool_stub; +#endif };#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
@@ -130,6 +134,13 @@ static inline u8 this_cpu_get_ftrace_enabled(void) { return 1; }#ifdef CONFIG_FUNCTION_TRACER extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[]; +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +struct ftrace_ool_stub { +u32 insn[4 ]; +}; +extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_inittext[]; +extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_inittext_count; +#endif void ftrace_free_init_tramp (void );unsigned long ftrace_call_adjust (unsigned long addr);#else
arch/powerpc/include/asm/module.h +5 -0
@@ -47,6 +47,11 @@ struct mod_arch_specific {#ifdef CONFIG_DYNAMIC_FTRACE unsigned long tramp;unsigned long tramp_regs; +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +struct ftrace_ool_stub *ool_stubs; +unsigned int ool_stub_count; +unsigned int ool_stub_index; +#endif #endif };
arch/powerpc/kernel/asm-offsets.c +4 -0
@@ -674,5 +674,9 @@ int main(void)DEFINE (BPT_SIZE ,BPT_SIZE );#endif +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +DEFINE (FTRACE_OOL_STUB_SIZE ,sizeof (struct ftrace_ool_stub )); +#endif +return 0 ; }
arch/powerpc/kernel/module_64.c +56 -2
88 lines not on this page · raw message
arch/powerpc/kernel/trace/ftrace.c +152 -10
226 lines not on this page · raw message
arch/powerpc/kernel/trace/ftrace_entry.S +91 -25
203 lines not on this page · raw message
arch/powerpc/tools/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +quiet_cmd_gen_ftrace_ool_stubs = GEN $@ + cmd_gen_ftrace_ool_stubs = $< vmlinux.o $@ + +$(obj)/.vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE + $(call if_changed,gen_ftrace_ool_stubs) + +$(obj)/.vmlinux.arch.o: $(obj)/.vmlinux.arch.S FORCE + $(call if_changed_rule,as_o_S) + +clean-files += .vmlinux.arch.S .vmlinux.arch.o
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 13/17 powerpc64/ftrace: Support .text larger than 32MB with out-of-line stubs
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
We are restricted to a .text size of ~32MB when using out-of-line function profile sequence. Allow this to be extended up to the previous limit of ~64MB by reserving space in the middle of .text.
A new config option CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE is introduced to specify the number of function stubs that are reserved in .text. On boot, ftrace utilizes stubs from this area first before using the stub area at the end of .text.
A ppc64le defconfig has ~44k functions that can be traced. A more conservative value of 32k functions is chosen as the default value of PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE so that we do not allot more space than necessary by default. If building a kernel that only has 32k trace-able functions, we won't allot any more space at the end of .text during the pass on vmlinux.o. Otherwise, only the remaining functions get space for stubs at the end of .text. This default value should help cover a .text size of ~48MB in total (including space reserved at the end of .text which can cover up to 32MB), which should be sufficient for most common builds. For a very small kernel build, this can be set to 0. Or, this can be bumped up to a larger value to support vmlinux .text size up to ~64MB.
---
Changes in v5:
* num_ool_stubs_text_end used for setting up ftrace_ool_stub_text_end set to zero instead of computing to some random negative value when not required.
arch/powerpc/Kconfig | 12 ++++++++++++ arch/powerpc/include/asm/ftrace.h | 6 ++++-- arch/powerpc/kernel/trace/ftrace.c | 21 +++++++++++++++++---- arch/powerpc/kernel/trace/ftrace_entry.S | 8 ++++++++ arch/powerpc/tools/Makefile | 2 +- arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 16 ++++++++++++---- 6 files changed, 54 insertions(+), 11 deletions(-)
arch/powerpc/Kconfig +12 -0
@@ -573,6 +573,18 @@ config PPC_FTRACE_OUT_OF_LINE depends on PPC64 select ARCH_WANTS_PRE_LINK_VMLINUX +config PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE + int "Number of ftrace out-of-line stubs to reserve within .text" + default 32768 if PPC_FTRACE_OUT_OF_LINE + default 0 + help + Number of stubs to reserve for use by ftrace. This space is + reserved within .text, and is distinct from any additional space + added at the end of .text before the final vmlinux link. Set to + zero to have stubs only be generated at the end of vmlinux (only + if the size of vmlinux is less than 32MB). Set to a higher value + if building vmlinux larger than 48MB. + config HOTPLUG_CPU bool "Support for enabling/disabling CPUs" depends on SMP && (PPC_PSERIES || \
arch/powerpc/include/asm/ftrace.h +4 -2
@@ -138,8 +138,10 @@ extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];struct ftrace_ool_stub {u32 insn[4 ]; }; -extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_inittext[]; -extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_inittext_count; +extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_text[], + ftrace_ool_stub_inittext[]; +extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count, + ftrace_ool_stub_inittext_count;#endif void ftrace_free_init_tramp (void );unsigned long ftrace_call_adjust (unsigned long addr);
arch/powerpc/kernel/trace/ftrace.c +17 -4
@@ -168,7 +168,7 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_static int ftrace_init_ool_stub (struct module *mod,struct dyn_ftrace *rec) {#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE -static int ool_stub_text_end_index, ool_stub_inittext_index; +static int ool_stub_text_index, ool_stub_text_end_index, ool_stub_inittext_index;int ret =0 , ool_stub_count, *ool_stub_index;ppc_inst_t inst;/*
@@ -191,9 +191,22 @@ static int ftrace_init_ool_stub(struct module *mod, struct dyn_ftrace *rec) ool_stub_index = &ool_stub_inittext_index; ool_stub_count = ftrace_ool_stub_inittext_count; }else if (is_kernel_text (rec->ip)) { - ool_stub = ftrace_ool_stub_text_end; - ool_stub_index = &ool_stub_text_end_index; - ool_stub_count = ftrace_ool_stub_text_end_count; +/* +* ftrace records are sorted, so we first use up the stub area within .text +* (ftrace_ool_stub_text) before using the area at the end of .text +* (ftrace_ool_stub_text_end), unless the stub is out of range of the record. +*/ +if (ool_stub_text_index >= ftrace_ool_stub_text_count || + !is_offset_in_branch_range ((long )rec->ip - + (long )&ftrace_ool_stub_text[ool_stub_text_index])) { + ool_stub = ftrace_ool_stub_text_end; + ool_stub_index = &ool_stub_text_end_index; + ool_stub_count = ftrace_ool_stub_text_end_count; + }else { + ool_stub = ftrace_ool_stub_text; + ool_stub_index = &ool_stub_text_index; + ool_stub_count = ftrace_ool_stub_text_count; + } #ifdef CONFIG_MODULES }else if (mod) { ool_stub = mod->arch.ool_stubs;
arch/powerpc/kernel/trace/ftrace_entry.S +8 -0
@@ -374,6 +374,14 @@ _GLOBAL(return_to_handler) blr #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +SYM_DATA(ftrace_ool_stub_text_count, .long CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) + +SYM_CODE_START(ftrace_ool_stub_text) + .space CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE * FTRACE_OOL_STUB_SIZE +SYM_CODE_END(ftrace_ool_stub_text) +#endif + .pushsection ".tramp.ftrace.text","aw",@progbits; .globl ftrace_tramp_text ftrace_tramp_text:
arch/powerpc/tools/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later quiet_cmd_gen_ftrace_ool_stubs = GEN $@ - cmd_gen_ftrace_ool_stubs = $< vmlinux.o $@ + cmd_gen_ftrace_ool_stubs = $< $(CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) vmlinux.o $@ $(obj)/.vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE $(call if_changed,gen_ftrace_ool_stubs)
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +12 -4
@@ -8,8 +8,9 @@ is_enabled() { grep -q "^$1=y" include/config/auto.conf } -vmlinux_o=${1} -arch_vmlinux_S=${2} +vmlinux_o=${2} +arch_vmlinux_S=${3} +arch_vmlinux_o=$(dirname ${arch_vmlinux_S})/$(basename ${arch_vmlinux_S} .S).o RELOCATION=R_PPC64_ADDR64 if is_enabled CONFIG_PPC32; then
@@ -21,15 +22,22 @@ num_ool_stubs_text=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries num_ool_stubs_inittext=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} | grep ".init.text" | grep "${RELOCATION}" | wc -l) +num_ool_stubs_text_builtin=${1} +if [ ${num_ool_stubs_text} -gt ${num_ool_stubs_text_builtin} ]; then + num_ool_stubs_text_end=$(expr ${num_ool_stubs_text} - ${num_ool_stubs_text_builtin}) +else + num_ool_stubs_text_end=0 +fi + cat > ${arch_vmlinux_S} <<EOF #include <asm/asm-offsets.h> #include <linux/linkage.h> .pushsection .tramp.ftrace.text,"aw" -SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text}) +SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text_end}) SYM_CODE_START(ftrace_ool_stub_text_end) - .space ${num_ool_stubs_text} * FTRACE_OOL_STUB_SIZE + .space ${num_ool_stubs_text_end} * FTRACE_OOL_STUB_SIZE SYM_CODE_END(ftrace_ool_stub_text_end) .popsection
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 14/17 powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_CALL_OPS
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Implement support for DYNAMIC_FTRACE_WITH_CALL_OPS similar to the arm64 implementation.
This works by patching-in a pointer to an associated ftrace_ops structure before each traceable function. If multiple ftrace_ops are associated with a call site, then a special ftrace_list_ops is used to enable iterating over all the registered ftrace_ops. If no ftrace_ops are associated with a call site, then a special ftrace_nop_ops structure is used to render the ftrace call as a no-op. ftrace trampoline can then read the associated ftrace_ops for a call site by loading from an offset from the LR, and branch directly to the associated function.
The primary advantage with this approach is that we don't have to iterate over all the registered ftrace_ops for call sites that have a single ftrace_ops registered. This is the equivalent of implementing support for dynamic ftrace trampolines, which set up a special ftrace trampoline for each registered ftrace_ops and have individual call sites branch into those directly.
A secondary advantage is that this gives us a way to add support for direct ftrace callers without having to resort to using stubs. The address of the direct call trampoline can be loaded from the ftrace_ops structure.
To support this, we reserve a nop before each function on 32-bit powerpc. For 64-bit powerpc, two nops are reserved before each out-of-line stub. During ftrace activation, we update this location with the associated ftrace_ops pointer. Then, on ftrace entry, we load from this location and call into ftrace_ops->func().
For 64-bit powerpc, we ensure that the out-of-line stub area is doubleword aligned so that ftrace_ops address can be updated atomically.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/Makefile | 4 ++ arch/powerpc/include/asm/ftrace.h | 5 +- arch/powerpc/kernel/asm-offsets.c | 4 ++ arch/powerpc/kernel/trace/ftrace.c | 59 +++++++++++++++++++++- arch/powerpc/kernel/trace/ftrace_entry.S | 36 ++++++++++--- arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 5 +- 7 files changed, 102 insertions(+), 12 deletions(-)
arch/powerpc/Kconfig +1 -0
@@ -234,6 +234,7 @@ config PPC select HAVE_DEBUG_STACKOVERFLOW select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_ARGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 + select HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if PPC_FTRACE_OUT_OF_LINE || (PPC32 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY) select HAVE_DYNAMIC_FTRACE_WITH_REGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 select HAVE_EBPF_JIT select HAVE_EFFICIENT_UNALIGNED_ACCESS
arch/powerpc/Makefile +4 -0
@@ -158,8 +158,12 @@ KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE CC_FLAGS_FTRACE := -fpatchable-function-entry=1 else +ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS # PPC32 only +CC_FLAGS_FTRACE := -fpatchable-function-entry=3,1 +else CC_FLAGS_FTRACE := -fpatchable-function-entry=2 endif +endif else CC_FLAGS_FTRACE := -pg ifdef CONFIG_MPROFILE_KERNEL
arch/powerpc/include/asm/ftrace.h +4 -1
@@ -136,8 +136,11 @@ static inline u8 this_cpu_get_ftrace_enabled(void) { return 1; }extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE struct ftrace_ool_stub { +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS +struct ftrace_ops *ftrace_op; +#endif u32 insn[4 ]; -}; +}__aligned(sizeof(unsigned long)) ;extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_text[], ftrace_ool_stub_inittext[]; extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count,
arch/powerpc/kernel/asm-offsets.c +4 -0
@@ -678,5 +678,9 @@ int main(void)DEFINE (FTRACE_OOL_STUB_SIZE ,sizeof (struct ftrace_ool_stub ));#endif +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS +OFFSET (FTRACE_OPS_FUNC , ftrace_ops, func); +#endif +return 0 ; }
arch/powerpc/kernel/trace/ftrace.c +58 -1
100 lines not on this page · raw message
arch/powerpc/kernel/trace/ftrace_entry.S +28 -8
@@ -85,11 +85,21 @@ /* Save callee's TOC in the ABI compliant location */ std r2, STK_GOT(r1) LOAD_PACA_TOC() /* get kernel TOC in r2 */ +#endif + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS + /* r7 points to the instruction following the call to ftrace */ + PPC_LL r5, -(MCOUNT_INSN_SIZE*2 + SZL)(r7) + PPC_LL r12, FTRACE_OPS_FUNC(r5) + mtctr r12 +#else /* !CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS */ +#ifdef CONFIG_PPC64 LOAD_REG_ADDR(r3, function_trace_op) ld r5,0(r3) #else lis r3,function_trace_op@ha lwz r5,function_trace_op@l(r3) +#endif #endif /* Save special regs */
@@ -205,20 +215,30 @@ #endif .endm -_GLOBAL(ftrace_regs_caller) - ftrace_regs_entry 1 - /* ftrace_call(r3, r4, r5, r6) */ +.macro ftrace_regs_func allregs +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS + bctrl +#else + .if \allregs == 1 .globl ftrace_regs_call ftrace_regs_call: + .else +.globl ftrace_call +ftrace_call: + .endif + /* ftrace_call(r3, r4, r5, r6) */ bl ftrace_stub +#endif +.endm + +_GLOBAL(ftrace_regs_caller) + ftrace_regs_entry 1 + ftrace_regs_func 1 ftrace_regs_exit 1 _GLOBAL(ftrace_caller) ftrace_regs_entry 0 - /* ftrace_call(r3, r4, r5, r6) */ -.globl ftrace_call -ftrace_call: - bl ftrace_stub + ftrace_regs_func 0 ftrace_regs_exit 0 _GLOBAL(ftrace_stub)
@@ -377,7 +397,7 @@ _GLOBAL(return_to_handler) #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE SYM_DATA(ftrace_ool_stub_text_count, .long CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) -SYM_CODE_START(ftrace_ool_stub_text) +SYM_START(ftrace_ool_stub_text, SYM_L_GLOBAL, .balign SZL) .space CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE * FTRACE_OOL_STUB_SIZE SYM_CODE_END(ftrace_ool_stub_text) #endif
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +3 -2
@@ -31,12 +31,13 @@ fi cat > ${arch_vmlinux_S} <<EOF #include <asm/asm-offsets.h> +#include <asm/ppc_asm.h> #include <linux/linkage.h> .pushsection .tramp.ftrace.text,"aw" SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text_end}) -SYM_CODE_START(ftrace_ool_stub_text_end) +SYM_START(ftrace_ool_stub_text_end, SYM_L_GLOBAL, .balign SZL) .space ${num_ool_stubs_text_end} * FTRACE_OOL_STUB_SIZE SYM_CODE_END(ftrace_ool_stub_text_end) .popsection
@@ -44,7 +45,7 @@ SYM_CODE_END(ftrace_ool_stub_text_end) .pushsection .tramp.ftrace.init,"aw" SYM_DATA(ftrace_ool_stub_inittext_count, .long ${num_ool_stubs_inittext}) -SYM_CODE_START(ftrace_ool_stub_inittext) +SYM_START(ftrace_ool_stub_inittext, SYM_L_GLOBAL, .balign SZL) .space ${num_ool_stubs_inittext} * FTRACE_OOL_STUB_SIZE SYM_CODE_END(ftrace_ool_stub_inittext) .popsection
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 15/17 powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS similar to the arm64 implementation.
ftrace direct calls allow custom trampolines to be called into directly from function ftrace call sites, bypassing the ftrace trampoline completely. This functionality is currently utilized by BPF trampolines to hook into kernel function entries.
Since we have limited relative branch range, we support ftrace direct calls through support for DYNAMIC_FTRACE_WITH_CALL_OPS. In this approach, ftrace trampoline is not entirely bypassed. Rather, it is re-purposed into a stub that reads direct_call field from the associated ftrace_ops structure and branches into that, if it is not NULL. For this, it is sufficient if we can ensure that the ftrace trampoline is reachable from all traceable functions.
When multiple ftrace_ops are associated with a call site, we utilize a call back to set pt_regs->orig_gpr3 that can then be tested on the return path from the ftrace trampoline to branch into the direct caller.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/ftrace.h | 16 ++++ arch/powerpc/kernel/asm-offsets.c | 3 + arch/powerpc/kernel/trace/ftrace.c | 11 +++ arch/powerpc/kernel/trace/ftrace_entry.S | 114 +++++++++++++++++------ 5 files changed, 116 insertions(+), 29 deletions(-)
arch/powerpc/Kconfig +1 -0
@@ -235,6 +235,7 @@ config PPC select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_ARGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 select HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if PPC_FTRACE_OUT_OF_LINE || (PPC32 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY) + select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS if HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS select HAVE_DYNAMIC_FTRACE_WITH_REGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 select HAVE_EBPF_JIT select HAVE_EFFICIENT_UNALIGNED_ACCESS
arch/powerpc/include/asm/ftrace.h +16 -0
@@ -148,6 +148,22 @@ extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count,#endif void ftrace_free_init_tramp (void );unsigned long ftrace_call_adjust (unsigned long addr); + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +/* +* When an ftrace registered caller is tracing a function that is also set by a +* register_ftrace_direct() call, it needs to be differentiated in the +* ftrace_caller trampoline so that the direct call can be invoked after the +* other ftrace ops. To do this, place the direct caller in the orig_gpr3 field +* of pt_regs. This tells ftrace_caller that there's a direct caller. +*/ +static inline void arch_ftrace_set_direct_caller (struct ftrace_regs *fregs,unsigned long addr) +{ +struct pt_regs *regs = &fregs->regs; + + regs->orig_gpr3 = addr; +} +#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ #else static inline void ftrace_free_init_tramp (void ) { }static inline unsigned long ftrace_call_adjust (unsigned long addr) {return addr; }
arch/powerpc/kernel/asm-offsets.c +3 -0
@@ -680,6 +680,9 @@ int main(void)#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS OFFSET (FTRACE_OPS_FUNC , ftrace_ops, func); +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +OFFSET (FTRACE_OPS_DIRECT_CALL , ftrace_ops, direct_call); +#endif #endif return 0 ;
arch/powerpc/kernel/trace/ftrace.c +11 -0
@@ -150,6 +150,17 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_else ip = rec->ip; +if (!is_offset_in_branch_range (addr - ip) && addr !=FTRACE_ADDR && + addr !=FTRACE_REGS_ADDR ) { +/* This can only happen with ftrace direct */ +if (!IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS )) { +pr_err ("0x%lx (0x%lx): Unexpected target address 0x%lx\n" , + ip, rec->ip, addr); +return -EINVAL ; + } + addr =FTRACE_ADDR ; + } +if (is_offset_in_branch_range (addr - ip))/* Within range */ stub = addr;
arch/powerpc/kernel/trace/ftrace_entry.S +85 -29
219 lines not on this page · raw message
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 16/17 samples/ftrace: Add support for ftrace direct samples on powerpc
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Add powerpc 32-bit and 64-bit samples for ftrace direct. This serves to show the sample instruction sequence to be used by ftrace direct calls to adhere to the ftrace ABI.
On 64-bit powerpc, TOC setup requires some additional work.
--- arch/powerpc/Kconfig | 2 + samples/ftrace/ftrace-direct-modify.c | 85 +++++++++++++++- samples/ftrace/ftrace-direct-multi-modify.c | 101 +++++++++++++++++++- samples/ftrace/ftrace-direct-multi.c | 79 ++++++++++++++- samples/ftrace/ftrace-direct-too.c | 83 +++++++++++++++- samples/ftrace/ftrace-direct.c | 69 ++++++++++++- 6 files changed, 414 insertions(+), 5 deletions(-)
arch/powerpc/Kconfig +2 -0
@@ -275,6 +275,8 @@ config PPC select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RELIABLE_STACKTRACE select HAVE_RSEQ + select HAVE_SAMPLE_FTRACE_DIRECT if HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_SETUP_PER_CPU_AREA if PPC64 select HAVE_SOFTIRQ_ON_OWN_STACK select HAVE_STACKPROTECTOR if PPC32 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r2)
samples/ftrace/ftrace-direct-modify.c +84 -1
97 lines not on this page · raw message
samples/ftrace/ftrace-direct-multi-modify.c +100 -1
113 lines not on this page · raw message
samples/ftrace/ftrace-direct-multi.c +78 -1
91 lines not on this page · raw message
samples/ftrace/ftrace-direct-too.c +82 -1
95 lines not on this page · raw message
samples/ftrace/ftrace-direct.c +68 -1
81 lines not on this page · raw message
Signature · 1 line
2.46.0
Hari Bathini
maintainerPATCH v5 17/17 powerpc64/bpf: Add support for bpf trampolines
In reply to Hari BathiniFrom: Naveen N Rao <[email protected]>
Add support for bpf_arch_text_poke() and arch_prepare_bpf_trampoline() for 64-bit powerpc. While the code is generic, BPF trampolines are only enabled on 64-bit powerpc. 32-bit powerpc will need testing and some updates.
BPF Trampolines adhere to the existing ftrace ABI utilizing a two-instruction profiling sequence, as well as the newer ABI utilizing a three-instruction profiling sequence enabling return with a 'blr'. The trampoline code itself closely follows x86 implementation.
BPF prog JIT is extended to mimic 64-bit powerpc approach for ftrace having a single nop at function entry, followed by the function profiling sequence out-of-line and a separate long branch stub for calls to trampolines that are out of range. A dummy_tramp is provided to simplify synchronization similar to arm64.
When attaching a bpf trampoline to a bpf prog, we can patch up to three things:
- the nop at bpf prog entry to go to the out-of-line stub
- the instruction in the out-of-line stub to either call the bpf trampoline directly, or to branch to the long_branch stub.
- the trampoline address before the long_branch stub.
We do not need any synchronization here since we always have a valid branch target regardless of the order in which the above stores are seen. dummy_tramp ensures that the long_branch stub goes to a valid destination on other cpus, even when the branch to the long_branch stub is seen before the updated trampoline address.
However, when detaching a bpf trampoline from a bpf prog, or if changing the bpf trampoline address, we need synchronization to ensure that other cpus can no longer branch into the older trampoline so that it can be safely freed. bpf_tramp_image_put() uses rcu_tasks to ensure all cpus make forward progress, but we still need to ensure that other cpus execute isync (or some CSI) so that they don't go back into the trampoline again.
--- arch/powerpc/include/asm/ppc-opcode.h | 14 + arch/powerpc/net/bpf_jit.h | 12 + arch/powerpc/net/bpf_jit_comp.c | 847 +++++++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 7 +- arch/powerpc/net/bpf_jit_comp64.c | 7 +- 5 files changed, 884 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +14 -0
@@ -587,12 +587,26 @@#define PPC_RAW_MTSPR (spr, d) (0x7c0003a6 | ___PPC_RS(d) | __PPC_SPR(spr))#define PPC_RAW_EIEIO () (0x7c0006ac) +/* bcl 20,31,$+4 */ +#define PPC_RAW_BCL4 () (0x429f0005)#define PPC_RAW_BRANCH (offset) (0x48000000 | PPC_LI(offset))#define PPC_RAW_BL (offset) (0x48000001 | PPC_LI(offset))#define PPC_RAW_TW (t0, a, b) (0x7c000008 | ___PPC_RS(t0) | ___PPC_RA(a) | ___PPC_RB(b))#define PPC_RAW_TRAP () PPC_RAW_TW(31, 0, 0)#define PPC_RAW_SETB (t, bfa) (0x7c000100 | ___PPC_RT(t) | ___PPC_RA((bfa) << 2)) +#ifdef CONFIG_PPC32 +#define PPC_RAW_STL PPC_RAW_STW +#define PPC_RAW_STLU PPC_RAW_STWU +#define PPC_RAW_LL PPC_RAW_LWZ +#define PPC_RAW_CMPLI PPC_RAW_CMPWI +#else +#define PPC_RAW_STL PPC_RAW_STD +#define PPC_RAW_STLU PPC_RAW_STDU +#define PPC_RAW_LL PPC_RAW_LD +#define PPC_RAW_CMPLI PPC_RAW_CMPDI +#endif +/* Deal with instructions that older assemblers aren't aware of */ #define PPC_BCCTR_FLUSH stringify_in_c(.long PPC_INST_BCCTR_FLUSH)#define PPC_CP_ABORT stringify_in_c(.long PPC_RAW_CP_ABORT)
arch/powerpc/net/bpf_jit.h +12 -0
@@ -12,6 +12,7 @@#include <asm/types.h> #include <asm/ppc-opcode.h> +#include <linux/build_bug.h> #ifdef CONFIG_PPC64_ELF_ABI_V1 #define FUNCTION_DESCR_SIZE 24
@@ -21,6 +22,9 @@#define CTX_NIA (ctx) ((unsigned long)ctx->idx * 4) +#define SZL sizeof(unsigned long) +#define BPF_INSN_SAFETY 64 +#define PLANT_INSTR (d, idx, instr) \ do { if (d) { (d)[idx] = instr; } idx++; } while (0)#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)
@@ -81,6 +85,13 @@EMIT (PPC_RAW_ORI (d, d, (uintptr_t )(i) & \0xffff )); \ } } while (0) +#define PPC_LI_ADDR PPC_LI64 +#define PPC64_LOAD_PACA () \ + EMIT(PPC_RAW_LD(_R2, _R13, offsetof(struct paca_struct, kernel_toc))) +#else +#define PPC_LI64 (d, i) BUILD_BUG() +#define PPC_LI_ADDR PPC_LI32 +#define PPC64_LOAD_PACA () BUILD_BUG()#endif /*
@@ -165,6 +176,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code 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_build_fentry_stubs (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);
arch/powerpc/net/bpf_jit_comp.c +846 -1
867 lines not on this page · raw message
arch/powerpc/net/bpf_jit_comp32.c +6 -1
@@ -127,13 +127,16 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx) {int i; +/* Instruction for trampoline attach */ +EMIT (PPC_RAW_NOP ()); +/* Initialize tail_call_cnt, to be skipped if we do tail calls. */ if (ctx->seen &SEEN_TAILCALL )EMIT (PPC_RAW_LI (_R4,0 ));else EMIT (PPC_RAW_NOP ()); -#define BPF_TAILCALL_PROLOGUE_SIZE 4 +#define BPF_TAILCALL_PROLOGUE_SIZE 8if (bpf_has_stack_frame (ctx))EMIT (PPC_RAW_STWU (_R1, _R1, -BPF_PPC_STACKFRAME (ctx)));
@@ -198,6 +201,8 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BLR ()); + +bpf_jit_build_fentry_stubs (image, ctx); }/* Relative offset needs to be calculated based on final image location */
arch/powerpc/net/bpf_jit_comp64.c +6 -1
@@ -126,6 +126,9 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx) {int i; +/* Instruction for trampoline attach */ +EMIT (PPC_RAW_NOP ()); +#ifndef CONFIG_PPC_KERNEL_PCREL if (IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 ))EMIT (PPC_RAW_LD (_R2, _R13, offsetof(struct paca_struct , kernel_toc)));
@@ -200,6 +203,8 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_MR (_R3,bpf_to_ppc (BPF_REG_0 )));EMIT (PPC_RAW_BLR ()); + +bpf_jit_build_fentry_stubs (image, ctx); }int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func)
@@ -303,7 +308,7 @@ static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 o*/ int b2p_bpf_array =bpf_to_ppc (BPF_REG_2 );int b2p_index =bpf_to_ppc (BPF_REG_3 ); -int bpf_tailcall_prologue_size =8 ; +int bpf_tailcall_prologue_size =12 ;if (!IS_ENABLED (CONFIG_PPC_KERNEL_PCREL ) &&IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 )) bpf_tailcall_prologue_size +=4 ;/* skip past the toc load */
Signature · 1 line
2.46.0
kernel test robot
build botRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
kernel test robot noticed the following build warnings:
Hi Hari,
kernel test robot noticed the following build warnings:
[auto build test WARNING on powerpc/next]
[also build test WARNING on powerpc/fixes masahiroy-kbuild/for-next masahiroy-kbuild/fixes linus/master v6.11 next-20240916] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hari-Bathini/powerpc-trace-Account-for-fpatchable-function-entry-support-by-toolchain/20240916-050056 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next patch link: https://lore.kernel.org/r/20240915205648.830121-18-hbathini%40linux.ibm.com patch subject: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines config: powerpc-allyesconfig (https://download.01.org/0day-ci/archive/20240917/[email protected]/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project bf684034844c660b778f0eba103582f582b710c9) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240917/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from arch/powerpc/net/bpf_jit_comp.c:11:
In file included from arch/powerpc/include/asm/cacheflush.h:7:
In file included from include/linux/mm.h:2228:
include/linux/vmstat.h:500:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
500 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
501 | item];
| ~~~~
include/linux/vmstat.h:507:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
507 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
508 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
514 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
include/linux/vmstat.h:519:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
519 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
520 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:528:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
528 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
529 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
>> arch/powerpc/net/bpf_jit_comp.c:872:70: warning: variable 'r4_off' is uninitialized when used here [-Wuninitialized]
872 | bpf_trampoline_setup_tail_call_cnt(image, ctx, func_frame_offset, r4_off);
| ^~~~~~
arch/powerpc/net/bpf_jit_comp.c:654:87: note: initialize the variable 'r4_off' to silence this warning
654 | int regs_off, nregs_off, ip_off, run_ctx_off, retval_off, nvr_off, alt_lr_off, r4_off;
| ^
| = 06 warnings generated.
vim +/r4_off +872 arch/powerpc/net/bpf_jit_comp.c
647
648 static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,
649 void *rw_image_end, void *ro_image,
650 const struct btf_func_model *m, u32 flags,
651 struct bpf_tramp_links *tlinks,
652 void *func_addr)
653 {
654 int regs_off, nregs_off, ip_off, run_ctx_off, retval_off, nvr_off, alt_lr_off, r4_off;
655 int i, ret, nr_regs, bpf_frame_size = 0, bpf_dummy_frame_size = 0, func_frame_offset;
656 struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
657 struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
658 struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
659 struct codegen_context codegen_ctx, *ctx;
660 u32 *image = (u32 *)rw_image;
661 ppc_inst_t branch_insn;
662 u32 *branches = NULL;
663 bool save_ret;
664
665 if (IS_ENABLED(CONFIG_PPC32))
666 return -EOPNOTSUPP;
667
668 nr_regs = m->nr_args;
669 /* Extra registers for struct arguments */
670 for (i = 0; i < m->nr_args; i++)
671 if (m->arg_size[i] > SZL)
672 nr_regs += round_up(m->arg_size[i], SZL) / SZL - 1;
673
674 if (nr_regs > MAX_BPF_FUNC_ARGS)
675 return -EOPNOTSUPP;
676
677 ctx = &codegen_ctx;
678 memset(ctx, 0, sizeof(*ctx));
679
680 /*
681 * Generated stack layout:
682 *
683 * func prev back chain [ back chain ]
684 * [ ]
685 * bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc)
686 * [ ] --
687 * LR save area [ r0 save (64-bit) ] | header
688 * [ r0 save (32-bit) ] |
689 * dummy frame for unwind [ back chain 1 ] --
690 * [ padding ] align stack frame
691 * r4_off [ r4 (tailcallcnt) ] optional - 32-bit powerpc
692 * alt_lr_off [ real lr (ool stub)] optional - actual lr
693 * [ r26 ]
694 * nvr_off [ r25 ] nvr save area
695 * retval_off [ return value ]
696 * [ reg argN ]
697 * [ ... ]
698 * regs_off [ reg_arg1 ] prog ctx context
699 * nregs_off [ args count ]
700 * ip_off [ traced function ]
701 * [ ... ]
702 * run_ctx_off [ bpf_tramp_run_ctx ]
703 * [ reg argN ]
704 * [ ... ]
705 * param_save_area [ reg_arg1 ] min 8 doublewords, per ABI
706 * [ TOC save (64-bit) ] --
707 * [ LR save (64-bit) ] | header
708 * [ LR save (32-bit) ] |
709 * bpf trampoline frame [ back chain 2 ] --
710 *
711 */
712
713 /* Minimum stack frame header */
714 bpf_frame_size = STACK_FRAME_MIN_SIZE;
715
716 /*
717 * Room for parameter save area.
718 *
719 * As per the ABI, this is required if we call into the traced
720 * function (BPF_TRAMP_F_CALL_ORIG):
721 * - if the function takes more than 8 arguments for the rest to spill onto the stack
722 * - or, if the function has variadic arguments
723 * - or, if this functions's prototype was not available to the caller
724 *
725 * Reserve space for at least 8 registers for now. This can be optimized later.
726 */
727 bpf_frame_size += (nr_regs > 8 ? nr_regs : 8) * SZL;
728
729 /* Room for struct bpf_tramp_run_ctx */
730 run_ctx_off = bpf_frame_size;
731 bpf_frame_size += round_up(sizeof(struct bpf_tramp_run_ctx), SZL);
732
733 /* Room for IP address argument */
734 ip_off = bpf_frame_size;
735 if (flags & BPF_TRAMP_F_IP_ARG)
736 bpf_frame_size += SZL;
737
738 /* Room for args count */
739 nregs_off = bpf_frame_size;
740 bpf_frame_size += SZL;
741
742 /* Room for args */
743 regs_off = bpf_frame_size;
744 bpf_frame_size += nr_regs * SZL;
745
746 /* Room for return value of func_addr or fentry prog */
747 retval_off = bpf_frame_size;
748 save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
749 if (save_ret)
750 bpf_frame_size += SZL;
751
752 /* Room for nvr save area */
753 nvr_off = bpf_frame_size;
754 bpf_frame_size += 2 * SZL;
755
756 /* Optional save area for actual LR in case of ool ftrace */
757 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE)) {
758 alt_lr_off = bpf_frame_size;
759 bpf_frame_size += SZL;
760 }
761
762 if (IS_ENABLED(CONFIG_PPC32)) {
763 if (nr_regs < 2) {
764 r4_off = bpf_frame_size;
765 bpf_frame_size += SZL;
766 } else {
767 r4_off = regs_off + SZL;
768 }
769 }
770
771 /* Padding to align stack frame, if any */
772 bpf_frame_size = round_up(bpf_frame_size, SZL * 2);
773
774 /* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */
775 bpf_dummy_frame_size = STACK_FRAME_MIN_SIZE + 64;
776
777 /* Offset to the traced function's stack frame */
778 func_frame_offset = bpf_dummy_frame_size + bpf_frame_size;
779
780 /* Create dummy frame for unwind, store original return value */
781 EMIT(PPC_RAW_STL(_R0, _R1, PPC_LR_STKOFF));
782 /* Protect red zone where tail call count goes */
783 EMIT(PPC_RAW_STLU(_R1, _R1, -bpf_dummy_frame_size));
784
785 /* Create our stack frame */
786 EMIT(PPC_RAW_STLU(_R1, _R1, -bpf_frame_size));
787
788 /* 64-bit: Save TOC and load kernel TOC */
789 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) {
790 EMIT(PPC_RAW_STD(_R2, _R1, 24));
791 PPC64_LOAD_PACA();
792 }
793
794 /* 32-bit: save tail call count in r4 */
795 if (IS_ENABLED(CONFIG_PPC32) && nr_regs < 2)
796 EMIT(PPC_RAW_STL(_R4, _R1, r4_off));
797
798 bpf_trampoline_save_args(image, ctx, func_frame_offset, nr_regs, regs_off);
799
800 /* Save our return address */
801 EMIT(PPC_RAW_MFLR(_R3));
802 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
803 EMIT(PPC_RAW_STL(_R3, _R1, alt_lr_off));
804 else
805 EMIT(PPC_RAW_STL(_R3, _R1, bpf_frame_size + PPC_LR_STKOFF));
806
807 /*
808 * Save ip address of the traced function.
809 * We could recover this from LR, but we will need to address for OOL trampoline,
810 * and optional GEP area.
811 */
812 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE) || flags & BPF_TRAMP_F_IP_ARG) {
813 EMIT(PPC_RAW_LWZ(_R4, _R3, 4));
814 EMIT(PPC_RAW_SLWI(_R4, _R4, 6));
815 EMIT(PPC_RAW_SRAWI(_R4, _R4, 6));
816 EMIT(PPC_RAW_ADD(_R3, _R3, _R4));
817 EMIT(PPC_RAW_ADDI(_R3, _R3, 4));
818 }
819
820 if (flags & BPF_TRAMP_F_IP_ARG)
821 EMIT(PPC_RAW_STL(_R3, _R1, ip_off));
822
823 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
824 /* Fake our LR for unwind */
825 EMIT(PPC_RAW_STL(_R3, _R1, bpf_frame_size + PPC_LR_STKOFF));
826
827 /* Save function arg count -- see bpf_get_func_arg_cnt() */
828 EMIT(PPC_RAW_LI(_R3, nr_regs));
829 EMIT(PPC_RAW_STL(_R3, _R1, nregs_off));
830
831 /* Save nv regs */
832 EMIT(PPC_RAW_STL(_R25, _R1, nvr_off));
833 EMIT(PPC_RAW_STL(_R26, _R1, nvr_off + SZL));
834
835 if (flags & BPF_TRAMP_F_CALL_ORIG) {
836 PPC_LI_ADDR(_R3, (unsigned long)im);
837 ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
838 (unsigned long)__bpf_tramp_enter);
839 if (ret)
840 return ret;
841 }
842
843 for (i = 0; i < fentry->nr_links; i++)
844 if (invoke_bpf_prog(image, ro_image, ctx, fentry->links[i], regs_off, retval_off,
845 run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET))
846 return -EINVAL;
847
848 if (fmod_ret->nr_links) {
849 branches = kcalloc(fmod_ret->nr_links, sizeof(u32), GFP_KERNEL);
850 if (!branches)
851 return -ENOMEM;
852
853 if (invoke_bpf_mod_ret(image, ro_image, ctx, fmod_ret, regs_off, retval_off,
854 run_ctx_off, branches)) {
855 ret = -EINVAL;
856 goto cleanup;
857 }
858 }
859
860 /* Call the traced function */
861 if (flags & BPF_TRAMP_F_CALL_ORIG) {
862 /*
863 * The address in LR save area points to the correct point in the original function
864 * with both PPC_FTRACE_OUT_OF_LINE as well as with traditional ftrace instruction
865 * sequence
866 */
867 EMIT(PPC_RAW_LL(_R3, _R1, bpf_frame_size + PPC_LR_STKOFF));
868 EMIT(PPC_RAW_MTCTR(_R3));
869
870 /* Replicate tail_call_cnt before calling the original BPF prog */
871 if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
> 872 bpf_trampoline_setup_tail_call_cnt(image, ctx, func_frame_offset, r4_off); 873
874 /* Restore args */
875 bpf_trampoline_restore_args_stack(image, ctx, func_frame_offset, nr_regs, regs_off);
876
877 /* Restore TOC for 64-bit */
878 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
879 EMIT(PPC_RAW_LD(_R2, _R1, 24));
880 EMIT(PPC_RAW_BCTRL());
881 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
882 PPC64_LOAD_PACA();
883
884 /* Store return value for bpf prog to access */
885 EMIT(PPC_RAW_STL(_R3, _R1, retval_off));
886
887 /* Restore updated tail_call_cnt */
888 if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
889 bpf_trampoline_restore_tail_call_cnt(image, ctx, func_frame_offset, r4_off);
890
891 /* Reserve space to patch branch instruction to skip fexit progs */
892 im->ip_after_call = &((u32 *)ro_image)[ctx->idx];
893 EMIT(PPC_RAW_NOP());
894 }
895
896 /* Update branches saved in invoke_bpf_mod_ret with address of do_fexit */
897 for (i = 0; i < fmod_ret->nr_links && image; i++) {
898 if (create_cond_branch(&branch_insn, &image[branches[i]],
899 (unsigned long)&image[ctx->idx], COND_NE << 16)) {
900 ret = -EINVAL;
901 goto cleanup;
902 }
903
904 image[branches[i]] = ppc_inst_val(branch_insn);
905 }
906
907 for (i = 0; i < fexit->nr_links; i++)
908 if (invoke_bpf_prog(image, ro_image, ctx, fexit->links[i], regs_off, retval_off,
909 run_ctx_off, false)) {
910 ret = -EINVAL;
911 goto cleanup;
912 }
913
914 if (flags & BPF_TRAMP_F_CALL_ORIG) {
915 im->ip_epilogue = &((u32 *)ro_image)[ctx->idx];
916 PPC_LI_ADDR(_R3, im);
917 ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
918 (unsigned long)__bpf_tramp_exit);
919 if (ret)
920 goto cleanup;
921 }
922
923 if (flags & BPF_TRAMP_F_RESTORE_REGS)
924 bpf_trampoline_restore_args_regs(image, ctx, nr_regs, regs_off);
925
926 /* Restore return value of func_addr or fentry prog */
927 if (save_ret)
928 EMIT(PPC_RAW_LL(_R3, _R1, retval_off));
929
930 /* Restore nv regs */
931 EMIT(PPC_RAW_LL(_R26, _R1, nvr_off + SZL));
932 EMIT(PPC_RAW_LL(_R25, _R1, nvr_off));
933
934 /* Epilogue */
935 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
936 EMIT(PPC_RAW_LD(_R2, _R1, 24));
937 if (flags & BPF_TRAMP_F_SKIP_FRAME) {
938 /* Skip the traced function and return to parent */
939 EMIT(PPC_RAW_ADDI(_R1, _R1, func_frame_offset));
940 EMIT(PPC_RAW_LL(_R0, _R1, PPC_LR_STKOFF));
941 EMIT(PPC_RAW_MTLR(_R0));
942 EMIT(PPC_RAW_BLR());
943 } else {
944 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE)) {
945 EMIT(PPC_RAW_LL(_R0, _R1, alt_lr_off));
946 EMIT(PPC_RAW_MTLR(_R0));
947 EMIT(PPC_RAW_ADDI(_R1, _R1, func_frame_offset));
948 EMIT(PPC_RAW_LL(_R0, _R1, PPC_LR_STKOFF));
949 EMIT(PPC_RAW_BLR());
950 } else {
951 EMIT(PPC_RAW_LL(_R0, _R1, bpf_frame_size + PPC_LR_STKOFF));
952 EMIT(PPC_RAW_MTCTR(_R0));
953 EMIT(PPC_RAW_ADDI(_R1, _R1, func_frame_offset));
954 EMIT(PPC_RAW_LL(_R0, _R1, PPC_LR_STKOFF));
955 EMIT(PPC_RAW_MTLR(_R0));
956 EMIT(PPC_RAW_BCTR());
957 }
958 }
959
960 /* Make sure the trampoline generation logic doesn't overflow */
961 if (image && WARN_ON_ONCE(&image[ctx->idx] > (u32 *)rw_image_end - BPF_INSN_SAFETY)) {
962 ret = -EFAULT;
963 goto cleanup;
964 }
965 ret = ctx->idx * 4 + BPF_INSN_SAFETY * 4;
966
967 cleanup:
968 kfree(branches);
969 return ret;
970 }
971
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
In reply to Hari BathiniQuoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] --
...
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;
What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.
Hari Bathini
maintainerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?
[1] https://lore.kernel.org/all/40b65ab2bb3a48837ab047a70887de3ccd70c56b.1474661927.git.naveen.n.rao@linux.vnet.ibm.com/
[2] https://lore.kernel.org/all/[email protected]/
Thanks
Hari
Re: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 24 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.
Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?
The follow up is fine.
It just odd to me that we currently have:
[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Hari Bathini
maintainerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 34 lines · Alexei Starovoitov
Quoted 24 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?
Quoted 8 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 4 lines
Thanks
Hari
Re: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 47 lines · Hari Bathini
Quoted 34 lines · Alexei Starovoitov
Quoted 24 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 8 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I guess ppc always has a different interrupt stack and it's not an issue?
Hari Bathini
maintainerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 62 lines · Alexei Starovoitov
Quoted 47 lines · Hari Bathini
Quoted 34 lines · Alexei Starovoitov
Quoted 24 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 8 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 6 lines
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I guess ppc always has a different interrupt stack and it's not an issue?
Yeah. On ppc64, kernel also uses redzone.
Interrupts use a different stack..
Thanks
Hari
Re: [PATCH v5 11/17] kbuild: Add generic hook for architectures to use before the final vmlinux link
In reply to Hari BathiniQuoted 29 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
On powerpc, we would like to be able to make a pass on vmlinux.o and generate a new object file to be linked into vmlinux. Add a generic pass in Makefile.vmlinux that architectures can use for this purpose.
Architectures need to select
CONFIG_ARCH_WANTS_PRE_LINK_VMLINUXand must provide arch/<arch>/tools/Makefile with .arch.vmlinux.o target, which will be invoked prior to the final vmlinux link step.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*arch/Kconfig | 6 ++++++ scripts/Makefile.vmlinux | 7 +++++++ scripts/link-vmlinux.sh | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-)arch/Kconfig +0
-0
arch/Kconfig config CC_HAS_SANE_FUNCTION_ALIGNMENT · line 1647
Quoted 5 lines
arch/Kconfig +2
-0@@ -1643,4 +1643,10 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT config ARCH_NEED_CMPXCHG_1_EMU bool +config ARCH_WANTS_PRE_LINK_VMLINUX + def_bool n
Redundant default. This line should be "bool".
Quoted 10 lines
arch/Kconfig +4
-0+ help + An architecture can select this if it provides arch/<arch>/tools/Makefile + with .arch.vmlinux.o target to be linked into vmlinux. + endmenuscripts/Makefile.vmlinux +0
-0
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 26
Quoted 5 lines
scripts/Makefile.vmlinux +2
-0@@ -22,6 +22,13 @@ targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endif +ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX +vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.o
If you move this to arch/*/tools/, there is no reason to make it a hidden file.
vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 28
Quoted 1 line
scripts/Makefile.vmlinux +1
-0+arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.o
FORCE is missing.
arch/$(SRCARCH)/tools/vmlinux.arch.o: vmlinux.o FORCE
Quoted 20 lines
scripts/Makefile.vmlinux +3
-0+ $(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@ +endif + ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) # Final link of vmlinux with optional arch pass after final linkscripts/link-vmlinux.sh +1
-1@@ -100,7 +100,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - ${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs} + ${kallsymso} ${btf_vmlinux_bin_o} ${arch_vmlinux_o} ${ldlibs} } # generate .BTF typeinfo from DWARF debuginfo
scripts/link-vmlinux.sh fi · line 219
Quoted 6 lines
scripts/link-vmlinux.sh +3
-0@@ -214,6 +214,11 @@ fi ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o +arch_vmlinux_o="" +if is_enabled CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX; then + arch_vmlinux_o=arch/${SRCARCH}/tools/.vmlinux.arch.o
arch_vmlinux_o=arch/${SRCARCH}/tools/vmlinux.arch.o
Quoted 8 lines
scripts/link-vmlinux.sh +2
-1+fi + btf_vmlinux_bin_o= kallsymso= strip_debug= --2.46.0
--
Best Regards
Masahiro Yamada
Re: [PATCH v5 12/17] powerpc64/ftrace: Move ftrace sequence out of line
In reply to Hari BathiniQuoted 115 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
Function profile sequence on powerpc includes two instructions at the beginning of each function:
mflr r0
blftrace_callerThe call to
ftrace_caller()gets nop'ed out during kernel boot and is patched in when ftrace is enabled.Given the sequence, we cannot return from
ftrace_callerwith 'blr' as we need to keep LR and r0 intact. This results in link stack (return address predictor) imbalance when ftrace is enabled. To address that, we would like to use a three instruction sequence:mflr r0 bl ftrace_caller mtlr r0Further more, to support
DYNAMIC_FTRACE_WITH_CALL_OPS, we need to reserve two instruction slots before the function. This results in a total of five instruction slots to be reserved for ftrace use on each function that is traced.Move the function profile sequence out-of-line to minimize its impact. To do this, we reserve a single nop at function entry using -fpatchable-function-entry=1 and add a pass on vmlinux.o to determine the total number of functions that can be traced. This is then used to generate a .S file reserving the appropriate amount of space for use as ftrace stubs, which is built and linked into vmlinux.
On bootup, the stub space is split into separate stubs per function and populated with the proper instruction sequence. A pointer to the associated stub is maintained in
dyn_arch_ftrace.For modules, space for ftrace stubs is reserved from the generic module stub space.
This is restricted to and enabled by default only on 64-bit powerpc, though there are some changes to accommodate 32-bit powerpc. This is done so that 32-bit powerpc could choose to opt into this based on further tests and benchmarks.
As an example, after this patch, kernel functions will have a single nop at function entry:
<kernel_clone>:addis r2,r12,467 addi r2,r2,-16028 nop mfocrf r11,8 ...When ftrace is enabled, the nop is converted to an unconditional branch to the stub associated with that function:
<kernel_clone>:addis r2,r12,467 addi r2,r2,-16028 b ftrace_ool_stub_text_end+0x11b28 mfocrf r11,8 ...The associated stub:
<ftrace_ool_stub_text_end+0x11b28>:mflr r0 bl ftrace_caller mtlr r0 b kernel_clone+0xc ...This change showed an improvement of ~10% in
null_syscallbenchmark on a Power 10 system with ftrace enabled.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
* Fixed ftrace stack tracer failure due to inadvertent use of 'add r7, r3,MCOUNT_INSN_SIZE' instruction instead of 'addi r7, r3,MCOUNT_INSN_SIZE'
* Fixed build error for !CONFIG_MODULES case.
* .vmlinux.arch.* files compiled underarch/powerpc/tools
* Made sure .vmlinux.arch.* files are cleaned withmake cleanarch/powerpc/Kbuild | 2 +- arch/powerpc/Kconfig | 5 + arch/powerpc/Makefile | 4 + arch/powerpc/include/asm/ftrace.h | 11 ++ arch/powerpc/include/asm/module.h | 5 + arch/powerpc/kernel/asm-offsets.c | 4 + arch/powerpc/kernel/module_64.c | 58 +++++++- arch/powerpc/kernel/trace/ftrace.c | 162 +++++++++++++++++++-- arch/powerpc/kernel/trace/ftrace_entry.S | 116 +++++++++++---- arch/powerpc/tools/Makefile | 12 ++ arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 43 ++++++ 11 files changed, 384 insertions(+), 38 deletions(-) create mode 100644 arch/powerpc/tools/Makefile create mode 100755 arch/powerpc/tools/ftrace-gen-ool-stubs.sharch/powerpc/Kbuild +1
-1@@ -19,4 +19,4 @@ obj-$(CONFIG_KEXEC_CORE) += kexec/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ # for cleaning -subdir- += boot +subdir- += boot toolsarch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config ARCH_USING_PATCHABLE_FUNCTION_ENTRY · line 573
Quoted 6 lines
arch/powerpc/Kconfig +3
-0@@ -568,6 +568,11 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh $(CC) -mbig-endian) if PPC64 && CPU_BIG_ENDIAN +config PPC_FTRACE_OUT_OF_LINE + def_bool PPC64 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY + depends on PPC64
PPC64 appears twice here. It is redundant.
If this config entry is user-unconfigurable,
"def_bool PPC64 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY" is enough.
"depends on PPC64" is unneeded.
Quoted 639 lines
arch/powerpc/Kconfig +2
-0+ select ARCH_WANTS_PRE_LINK_VMLINUX + config HOTPLUG_CPU bool "Support for enabling/disabling CPUs" depends on SMP && (PPC_PSERIES || \arch/powerpc/Makefile +4
-0@@ -155,7 +155,11 @@ CC_FLAGS_NO_FPU := $(call cc-option,-msoft-float) ifdef CONFIG_FUNCTION_TRACER ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY +ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +CC_FLAGS_FTRACE := -fpatchable-function-entry=1 +else CC_FLAGS_FTRACE := -fpatchable-function-entry=2 +endif else CC_FLAGS_FTRACE := -pg ifdef CONFIG_MPROFILE_KERNELarch/powerpc/include/asm/ftrace.h +11
-0@@ -24,6 +24,10 @@ unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,struct module ;struct dyn_ftrace ;struct dyn_arch_ftrace { +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +/* pointer to the associated out-of-line stub */ +unsigned long ool_stub; +#endif };#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS @@ -130,6 +134,13 @@ static inline u8 this_cpu_get_ftrace_enabled(void) { return 1; }#ifdef CONFIG_FUNCTION_TRACER extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[]; +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +struct ftrace_ool_stub { +u32 insn[4 ]; +}; +extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_inittext[]; +extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_inittext_count; +#endif void ftrace_free_init_tramp (void );unsigned long ftrace_call_adjust (unsigned long addr);#else arch/powerpc/include/asm/module.h +5
-0@@ -47,6 +47,11 @@ struct mod_arch_specific {#ifdef CONFIG_DYNAMIC_FTRACE unsigned long tramp;unsigned long tramp_regs; +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +struct ftrace_ool_stub *ool_stubs; +unsigned int ool_stub_count; +unsigned int ool_stub_index; +#endif #endif };arch/powerpc/kernel/asm-offsets.c +4
-0@@ -674,5 +674,9 @@ int main(void)DEFINE (BPT_SIZE ,BPT_SIZE );#endif +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +DEFINE (FTRACE_OOL_STUB_SIZE ,sizeof (struct ftrace_ool_stub )); +#endif +return 0 ; }arch/powerpc/kernel/module_64.c +56
-2@@ -205,7 +205,9 @@ static int relacmp(const void *_x, const void *_y)/* Get size of potential trampolines required. */ static unsigned long get_stubs_size (const Elf64_Ehdr *hdr, -const Elf64_Shdr *sechdrs) +const Elf64_Shdr *sechdrs, +char *secstrings, +struct module *me) {/* One extra reloc so it's always 0-addr terminated */ unsigned long relocs =1 ;@@ -244,6 +246,24 @@ static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,/* stubs for ftrace_caller and ftrace_regs_caller */ relocs +=IS_ENABLED (CONFIG_DYNAMIC_FTRACE ) +IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_REGS ); +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +/* stubs for the function tracer */ +for (i =1 ; i < hdr->e_shnum; i++) { +if (!strcmp (secstrings + sechdrs[i].sh_name,"__patchable_function_entries" )) { + me->arch.ool_stub_count = sechdrs[i].sh_size /sizeof (unsigned long ); + me->arch.ool_stub_index =0 ; + relocs +=roundup (me->arch.ool_stub_count *sizeof (struct ftrace_ool_stub ), +sizeof (struct ppc64_stub_entry )) / +sizeof (struct ppc64_stub_entry ); +break ; + } + } +if (i == hdr->e_shnum) { +pr_err ("%s: doesn't contain __patchable_function_entries.\n" , me->name); +return -ENOEXEC ; + } +#endif +pr_debug ("Looks like a total of %lu stubs, max\n" , relocs);return relocs *sizeof (struct ppc64_stub_entry ); }@@ -454,7 +474,7 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr,#endif /* Override the stubs size */ - sechdrs[me->arch.stubs_section].sh_size =get_stubs_size (hdr, sechdrs); + sechdrs[me->arch.stubs_section].sh_size =get_stubs_size (hdr, sechdrs, secstrings, me);return 0 ; }@@ -1079,6 +1099,37 @@ int module_trampoline_target(struct module *mod, unsigned long addr,return 0 ; } +static int setup_ftrace_ool_stubs (const Elf64_Shdr *sechdrs,unsigned long addr,struct module *me) +{ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +unsigned int i, total_stubs, num_stubs; +struct ppc64_stub_entry *stub; + + total_stubs = sechdrs[me->arch.stubs_section].sh_size /sizeof (*stub); + num_stubs =roundup (me->arch.ool_stub_count *sizeof (struct ftrace_ool_stub ), +sizeof (struct ppc64_stub_entry )) /sizeof (struct ppc64_stub_entry ); + +/* Find the next available entry */ + stub = (void *)sechdrs[me->arch.stubs_section].sh_addr; +for (i =0 ;stub_func_addr (stub[i].funcdata); i++) +if (WARN_ON (i >= total_stubs)) +return -1 ; + +if (WARN_ON (i + num_stubs > total_stubs)) +return -1 ; + + stub += i; + me->arch.ool_stubs = (struct ftrace_ool_stub *)stub; + +/* reserve stubs */ +for (i =0 ; i < num_stubs; i++) +if (patch_u32 ((void *)&stub->funcdata,PPC_RAW_NOP ())) +return -1 ; +#endif + +return 0 ; +} +int module_finalize_ftrace (struct module *mod,const Elf_Shdr *sechdrs) { mod->arch.tramp = stub_for_addr(sechdrs,@@ -1097,6 +1148,9 @@ int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)if (!mod->arch.tramp)return -ENOENT ; +if (setup_ftrace_ool_stubs (sechdrs, mod->arch.tramp, mod)) +return -ENOENT ; + return 0; }#endif arch/powerpc/kernel/trace/ftrace.c +152
-10@@ -37,7 +37,8 @@ unsigned long ftrace_call_adjust(unsigned long addr)if (addr >= (unsigned long )__exittext_begin && addr < (unsigned long )__exittext_end)return 0 ; -if (IS_ENABLED (CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY )) +if (IS_ENABLED (CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY ) && + !IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) addr +=MCOUNT_INSN_SIZE ;return addr;@@ -127,11 +128,25 @@ static unsigned long ftrace_lookup_module_stub(unsigned long ip, unsigned long a }#endif +static unsigned long ftrace_get_ool_stub (struct dyn_ftrace *rec) +{ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +return rec->arch.ool_stub; +#else +BUILD_BUG (); +#endif +} +static int ftrace_get_call_inst (struct dyn_ftrace *rec,unsigned long addr,ppc_inst_t *call_inst) { -unsigned long ip = rec->ip; +unsigned long ip;unsigned long stub; +if (IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) + ip =ftrace_get_ool_stub (rec) +MCOUNT_INSN_SIZE ;/* second instruction in stub */ +else + ip = rec->ip; +if (is_offset_in_branch_range (addr - ip))/* Within range */ stub = addr;@@ -142,7 +157,7 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_ stub =ftrace_lookup_module_stub (ip, addr);if (!stub) { -pr_err ("0x%lx: No ftrace stubs reachable\n" , ip); +pr_err ("0x%lx (0x%lx): No ftrace stubs reachable\n" , ip, rec->ip);return -EINVAL ; }@@ -150,6 +165,92 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_return 0 ; } +static int ftrace_init_ool_stub (struct module *mod,struct dyn_ftrace *rec) +{ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +static int ool_stub_text_end_index, ool_stub_inittext_index; +int ret =0 , ool_stub_count, *ool_stub_index; +ppc_inst_t inst; +/* +* See ftrace_entry.S if changing the below instruction sequence, as we rely on +* decoding the last branch instruction here to recover the correct function ip. +*/ +struct ftrace_ool_stub *ool_stub, ool_stub_template = { + .insn = { +PPC_RAW_MFLR (_R0), +PPC_RAW_NOP (),/* bl ftrace_caller */ +PPC_RAW_MTLR (_R0), +PPC_RAW_NOP ()/* b rec->ip + 4 */ + } + }; + +WARN_ON (rec->arch.ool_stub); + +if (is_kernel_inittext (rec->ip)) { + ool_stub = ftrace_ool_stub_inittext; + ool_stub_index = &ool_stub_inittext_index; + ool_stub_count = ftrace_ool_stub_inittext_count; + }else if (is_kernel_text (rec->ip)) { + ool_stub = ftrace_ool_stub_text_end; + ool_stub_index = &ool_stub_text_end_index; + ool_stub_count = ftrace_ool_stub_text_end_count; +#ifdef CONFIG_MODULES + }else if (mod) { + ool_stub = mod->arch.ool_stubs; + ool_stub_index = &mod->arch.ool_stub_index; + ool_stub_count = mod->arch.ool_stub_count; +#endif + }else { +return -EINVAL ; + } + + ool_stub += (*ool_stub_index)++; + +if (WARN_ON (*ool_stub_index > ool_stub_count)) +return -EINVAL ; + +if (!is_offset_in_branch_range ((long )rec->ip - (long )&ool_stub->insn[0 ]) || + !is_offset_in_branch_range ((long )(rec->ip +MCOUNT_INSN_SIZE ) - + (long )&ool_stub->insn[3 ])) { +pr_err ("%s: ftrace ool stub out of range (%p -> %p).\n" , + __func__, (void *)rec->ip, (void *)&ool_stub->insn[0 ]); +return -EINVAL ; + } + + rec->arch.ool_stub = (unsigned long )&ool_stub->insn[0 ]; + +/* bl ftrace_caller */ +if (!mod) + ret =ftrace_get_call_inst (rec, (unsigned long )ftrace_caller, &inst); +#ifdef CONFIG_MODULES + else +/* +* We can't use ftrace_get_call_inst() since that uses +* __module_text_address(rec->ip) to look up the module. +* But, since the module is not fully formed at this stage, +* the lookup fails. We know the target though, so generate +* the branch inst directly. +*/ + inst =ftrace_create_branch_inst (ftrace_get_ool_stub (rec) +MCOUNT_INSN_SIZE , + mod->arch.tramp,1 ); +#endif + ool_stub_template.insn[1 ] =ppc_inst_val (inst); + +/* b rec->ip + 4 */ +if (!ret &&create_branch (&inst, &ool_stub->insn[3 ], rec->ip +MCOUNT_INSN_SIZE ,0 )) +return -EINVAL ; + ool_stub_template.insn[3 ] =ppc_inst_val (inst); + +if (!ret) + ret =patch_instructions ((u32 *)ool_stub, (u32 *)&ool_stub_template, +sizeof (ool_stub_template), false); + +return ret; +#else /* !CONFIG_PPC_FTRACE_OUT_OF_LINE */ +BUILD_BUG (); +#endif +} +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS int ftrace_modify_call (struct dyn_ftrace *rec,unsigned long old_addr,unsigned long addr) {@@ -162,18 +263,29 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsignedint ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr) {ppc_inst_t old, new; -int ret; +unsigned long ip = rec->ip; +int ret =0 ;/* This can only ever be called during module load */ -if (WARN_ON (!IS_ENABLED (CONFIG_MODULES ) ||core_kernel_text (rec->ip))) +if (WARN_ON (!IS_ENABLED (CONFIG_MODULES ) ||core_kernel_text (ip)))return -EINVAL ; old =ppc_inst (PPC_RAW_NOP ()); - ret =ftrace_get_call_inst (rec, addr, &new); -if (ret) -return ret; +if (IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) { + ip =ftrace_get_ool_stub (rec) +MCOUNT_INSN_SIZE ;/* second instruction in stub */ + ret =ftrace_get_call_inst (rec, (unsigned long )ftrace_caller, &old); + } + + ret |=ftrace_get_call_inst (rec, addr, &new); + +if (!ret) + ret =ftrace_modify_code (ip, old, new); -return ftrace_modify_code (rec->ip, old, new); +if (!ret &&IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) + ret =ftrace_modify_code (rec->ip,ppc_inst (PPC_RAW_NOP ()), +ppc_inst (PPC_RAW_BRANCH ((long )ftrace_get_ool_stub (rec) - (long )rec->ip))); + +return ret; }int ftrace_make_nop (struct module *mod,struct dyn_ftrace *rec,unsigned long addr)@@ -206,6 +318,13 @@ void ftrace_replace_code(int enable) new_addr =ftrace_get_addr_new (rec); update =ftrace_update_record (rec, enable); +if (IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE ) && update !=FTRACE_UPDATE_IGNORE ) { + ip =ftrace_get_ool_stub (rec) +MCOUNT_INSN_SIZE ; + ret =ftrace_get_call_inst (rec, (unsigned long )ftrace_caller, &nop_inst); +if (ret) + goto out; + } +switch (update) {case FTRACE_UPDATE_IGNORE :default :@@ -230,6 +349,24 @@ void ftrace_replace_code(int enable)if (!ret) ret =ftrace_modify_code (ip, old, new); + +if (!ret &&IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE ) && + (update ==FTRACE_UPDATE_MAKE_NOP || update ==FTRACE_UPDATE_MAKE_CALL )) { +/* Update the actual ftrace location */ + call_inst =ppc_inst (PPC_RAW_BRANCH ((long )ftrace_get_ool_stub (rec) - + (long )rec->ip)); + nop_inst =ppc_inst (PPC_RAW_NOP ()); + ip = rec->ip; + +if (update ==FTRACE_UPDATE_MAKE_NOP ) + ret =ftrace_modify_code (ip, call_inst, nop_inst); +else + ret =ftrace_modify_code (ip, nop_inst, call_inst); + +if (ret) + goto out; + } +if (ret) goto out; }@@ -249,7 +386,8 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)/* Verify instructions surrounding the ftrace location */ if (IS_ENABLED (CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY )) {/* Expect nops */ - ret =ftrace_validate_inst (ip -4 ,ppc_inst (PPC_RAW_NOP ())); +if (!IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) + ret =ftrace_validate_inst (ip -4 ,ppc_inst (PPC_RAW_NOP ()));if (!ret) ret =ftrace_validate_inst (ip,ppc_inst (PPC_RAW_NOP ())); }else if (IS_ENABLED (CONFIG_PPC32 )) {@@ -277,6 +415,10 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)if (ret)return ret; +/* Set up out-of-line stub */ +if (IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) +return ftrace_init_ool_stub (mod, rec); +/* Nop-out the ftrace location */ new =ppc_inst (PPC_RAW_NOP ()); addr =MCOUNT_ADDR ;arch/powerpc/kernel/trace/ftrace_entry.S +91
-25@@ -56,7 +56,7 @@ SAVE_GPR(2, r1) SAVE_GPRS(11, 31, r1) .else -#ifdef CONFIG_LIVEPATCH_64 +#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE) SAVE_GPR(14, r1) #endif .endif@@ -78,10 +78,6 @@ /* Get the _mcount() call site out of LR */ mflr r7 - /* Save it as pt_regs->nip */ - PPC_STL r7, _NIP(r1) - /* Also save it in B's stackframe header for proper unwind */ - PPC_STL r7, LRSAVE+SWITCH_FRAME_SIZE(r1) /* Save the read LR in pt_regs->link */ PPC_STL r0, _LINK(r1)@@ -96,16 +92,6 @@ lwz r5,function_trace_op@l(r3) #endif -#ifdef CONFIG_LIVEPATCH_64 - mr r14, r7 /* remember old NIP */ -#endif - - /* Calculate ip from nip-4 into r3 for call below */ - subi r3, r7, MCOUNT_INSN_SIZE - - /* Put the original return address in r4 as parent_ip */ - mr r4, r0 - /* Save special regs */ PPC_STL r8, _MSR(r1) .if \allregs == 1@@ -114,17 +100,69 @@ PPC_STL r11, _CCR(r1) .endif +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE + /* Save our real return address in nvr for return */ + .if \allregs == 0 + SAVE_GPR(15, r1) + .endif + mr r15, r7 + /* + * We want the ftrace location in the function, but our lr (in r7) + * points at the 'mtlr r0' instruction in the out of line stub. To + * recover the ftrace location, we read the branch instruction in the + * stub, and adjust our lr by the branch offset. + * + * See ftrace_init_ool_stub() for the profile sequence. + */ + lwz r8, MCOUNT_INSN_SIZE(r7) + slwi r8, r8, 6 + srawi r8, r8, 6 + add r3, r7, r8 + /* + * Override our nip to point past the branch in the original function. + * This allows reliable stack trace and the ftrace stack tracer to work as-is. + */ + addi r7, r3, MCOUNT_INSN_SIZE +#else + /* Calculate ip from nip-4 into r3 for call below */ + subi r3, r7, MCOUNT_INSN_SIZE +#endif + + /* Save NIP as pt_regs->nip */ + PPC_STL r7, _NIP(r1) + /* Also save it in B's stackframe header for proper unwind */ + PPC_STL r7, LRSAVE+SWITCH_FRAME_SIZE(r1) +#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE) + mr r14, r7 /* remember old NIP */ +#endif + + /* Put the original return address in r4 as parent_ip */ + mr r4, r0 + /* Load &pt_regs in r6 for call below */ addi r6, r1, STACK_INT_FRAME_REGS .endm .macro ftrace_regs_exit allregs +#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE /* Load ctr with the possibly modified NIP */ PPC_LL r3, _NIP(r1) mtctr r3 #ifdef CONFIG_LIVEPATCH_64 cmpd r14, r3 /* has NIP been altered? */ +#endif +#else /* !CONFIG_PPC_FTRACE_OUT_OF_LINE */ + /* Load LR with the possibly modified NIP */ + PPC_LL r3, _NIP(r1) + cmpd r14, r3 /* has NIP been altered? */ + bne- 1f + + mr r3, r15 + .if \allregs == 0 + REST_GPR(15, r1) + .endif +1: mtlr r3 #endif /* Restore gprs */@@ -132,14 +170,16 @@ REST_GPRS(2, 31, r1) .else REST_GPRS(3, 10, r1) -#ifdef CONFIG_LIVEPATCH_64 +#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE) REST_GPR(14, r1) #endif .endif /* Restore possibly modified LR */ PPC_LL r0, _LINK(r1) +#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE mtlr r0 +#endif #ifdef CONFIG_PPC64 /* Restore callee's TOC */@@ -153,7 +193,16 @@ /* Based on the cmpd above, if the NIP was altered handle livepatch */ bne- livepatch_handler #endif - bctr /* jump after _mcount site */ + /* jump after _mcount site */ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE + /* + * Return with blr to keep the link stack balanced. The function profiling sequence + * uses 'mtlr r0' to restore LR. + */ + blr +#else + bctr +#endif .endm _GLOBAL(ftrace_regs_caller)@@ -177,6 +226,11 @@ _GLOBAL(ftrace_stub) #ifdef CONFIG_PPC64 ftrace_no_trace: +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE + REST_GPR(3, r1) + addi r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE + blr +#else mflr r3 mtctr r3 REST_GPR(3, r1)@@ -184,6 +238,7 @@ ftrace_no_trace: mtlr r0 bctr #endif +#endif #ifdef CONFIG_LIVEPATCH_64 /*@@ -194,11 +249,17 @@ ftrace_no_trace: * We get here when a function A, calls another function B, but B has * been live patched with a new function C. * - * On entry: - * - we have no stack frame and can not allocate one + * On entry, we have no stack frame and can not allocate one. + * + * With PPC_FTRACE_OUT_OF_LINE=n, on entry: * - LR points back to the original caller (in A) * - CTR holds the new NIP in C * - r0, r11 & r12 are free + * + * With PPC_FTRACE_OUT_OF_LINE=y, on entry: + * - r0 points back to the original caller (in A) + * - LR holds the new NIP in C + * - r11 & r12 are free */ livepatch_handler: ld r12, PACA_THREAD_INFO(r13)@@ -208,18 +269,23 @@ livepatch_handler: addi r11, r11, 24 std r11, TI_livepatch_sp(r12) - /* Save toc & real LR on livepatch stack */ - std r2, -24(r11) - mflr r12 - std r12, -16(r11) - /* Store stack end marker */ lis r12, STACK_END_MAGIC@h ori r12, r12, STACK_END_MAGIC@l std r12, -8(r11) - /* Put ctr in r12 for global entry and branch there */ + /* Save toc & real LR on livepatch stack */ + std r2, -24(r11) +#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE + mflr r12 + std r12, -16(r11) mfctr r12 +#else + std r0, -16(r11) + mflr r12 + /* Put ctr in r12 for global entry and branch there */ + mtctr r12 +#endif bctrl /*arch/powerpc/tools/Makefile new file +0
-0
arch/powerpc/tools/Makefile line 6
Quoted 6 lines
arch/powerpc/tools/Makefile new file +6
-0@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +quiet_cmd_gen_ftrace_ool_stubs = GEN $@ + cmd_gen_ftrace_ool_stubs = $< vmlinux.o $@ + +$(obj)/.vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE
$(obj)/vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE
arch/powerpc/tools/Makefile line 10
Quoted 7 lines
arch/powerpc/tools/Makefile new file +4
-0+ $(call if_changed,gen_ftrace_ool_stubs) + +$(obj)/.vmlinux.arch.o: $(obj)/.vmlinux.arch.S FORCE + $(call if_changed_rule,as_o_S)
This is unnecessary because the build rule %.S -> %.o is available in scripts/Makefile.build
arch/powerpc/tools/Makefile line 12
Quoted 2 lines
arch/powerpc/tools/Makefile new file +2
-0+ +clean-files += .vmlinux.arch.S .vmlinux.arch.o
if_changed macro needs 'targets' assignment.
This line should be replaced with:
targets += vmlinux.arch.S
Quoted 6 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +0
-0
arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 9
Quoted 9 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +9
-0@@ -0,0 +1,43 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later + +# Error out on error +set -e + +is_enabled() { + grep -q "^$1=y" include/config/auto.conf +}
Instead of checking the CONFIG option in this script, I recommend passing the 64bit flag as a command line parameter.
arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 19
Quoted 10 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +10
-0+ +vmlinux_o=${1} +arch_vmlinux_S=${2} + +RELOCATION=R_PPC64_ADDR64 +if is_enabled CONFIG_PPC32; then + RELOCATION=R_PPC_ADDR32 +fi + +num_ool_stubs_text=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} |
${CROSS_COMPILE}objdump -> ${OBJDUMP}
arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 21
Quoted 2 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +2
-0+ grep -v ".init.text" | grep "${RELOCATION}" | wc -l) +num_ool_stubs_inittext=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} |
${CROSS_COMPILE}objdump -> ${OBJDUMP}
I also recommend passing ${OBJDUMP} from the command line parameter.
Quoted 23 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh new file +22
-1+ grep ".init.text" | grep "${RELOCATION}" | wc -l) + +cat > ${arch_vmlinux_S} <<EOF +#include <asm/asm-offsets.h> +#include <linux/linkage.h> + +.pushsection .tramp.ftrace.text,"aw" +SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text}) + +SYM_CODE_START(ftrace_ool_stub_text_end) + .space ${num_ool_stubs_text} * FTRACE_OOL_STUB_SIZE +SYM_CODE_END(ftrace_ool_stub_text_end) +.popsection + +.pushsection .tramp.ftrace.init,"aw" +SYM_DATA(ftrace_ool_stub_inittext_count, .long ${num_ool_stubs_inittext}) + +SYM_CODE_START(ftrace_ool_stub_inittext) + .space ${num_ool_stubs_inittext} * FTRACE_OOL_STUB_SIZE +SYM_CODE_END(ftrace_ool_stub_inittext) +.popsection +EOF --
Quoted 2 lines
2.46.0
--
Best Regards
Masahiro Yamada
Re: [PATCH v5 13/17] powerpc64/ftrace: Support .text larger than 32MB with out-of-line stubs
In reply to Hari BathiniQuoted 47 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
We are restricted to a .text size of ~32MB when using out-of-line function profile sequence. Allow this to be extended up to the previous limit of ~64MB by reserving space in the middle of .text.
A new config option
CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVEis introduced to specify the number of function stubs that are reserved in .text. On boot, ftrace utilizes stubs from this area first before using the stub area at the end of .text.A ppc64le defconfig has ~44k functions that can be traced. A more conservative value of 32k functions is chosen as the default value of
PPC_FTRACE_OUT_OF_LINE_NUM_RESERVEso that we do not allot more space than necessary by default. If building a kernel that only has 32k trace-able functions, we won't allot any more space at the end of .text during the pass on vmlinux.o. Otherwise, only the remaining functions get space for stubs at the end of .text. This default value should help cover a .text size of ~48MB in total (including space reserved at the end of .text which can cover up to 32MB), which should be sufficient for most common builds. For a very small kernel build, this can be set to 0. Or, this can be bumped up to a larger value to support vmlinux .text size up to ~64MB.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
*num_ool_stubs_text_endused for setting upftrace_ool_stub_text_endset to zero instead of computing to some random negative value when not required.arch/powerpc/Kconfig | 12 ++++++++++++ arch/powerpc/include/asm/ftrace.h | 6 ++++-- arch/powerpc/kernel/trace/ftrace.c | 21 +++++++++++++++++---- arch/powerpc/kernel/trace/ftrace_entry.S | 8 ++++++++ arch/powerpc/tools/Makefile | 2 +- arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 16 ++++++++++++---- 6 files changed, 54 insertions(+), 11 deletions(-)arch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config PPC_FTRACE_OUT_OF_LINE · line 579
Quoted 7 lines
arch/powerpc/Kconfig +4
-0@@ -573,6 +573,18 @@ config PPC_FTRACE_OUT_OF_LINE depends on PPC64 select ARCH_WANTS_PRE_LINK_VMLINUX +config PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE + int "Number of ftrace out-of-line stubs to reserve within .text" + default 32768 if PPC_FTRACE_OUT_OF_LINE + default 0
This entry is meaningless when CONFIG_PPC_FTRACE_OUT_OF_LINE=n.
depends on PPC_FTRACE_OUT_OF_LINE
default 32768
Quoted 104 lines
arch/powerpc/Kconfig +8
-0+ help + Number of stubs to reserve for use by ftrace. This space is + reserved within .text, and is distinct from any additional space + added at the end of .text before the final vmlinux link. Set to + zero to have stubs only be generated at the end of vmlinux (only + if the size of vmlinux is less than 32MB). Set to a higher value + if building vmlinux larger than 48MB. + config HOTPLUG_CPU bool "Support for enabling/disabling CPUs" depends on SMP && (PPC_PSERIES || \arch/powerpc/include/asm/ftrace.h +4
-2@@ -138,8 +138,10 @@ extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];struct ftrace_ool_stub {u32 insn[4 ]; }; -extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_inittext[]; -extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_inittext_count; +extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_text[], + ftrace_ool_stub_inittext[]; +extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count, + ftrace_ool_stub_inittext_count;#endif void ftrace_free_init_tramp (void );unsigned long ftrace_call_adjust (unsigned long addr);arch/powerpc/kernel/trace/ftrace.c +17
-4@@ -168,7 +168,7 @@ static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_static int ftrace_init_ool_stub (struct module *mod,struct dyn_ftrace *rec) {#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE -static int ool_stub_text_end_index, ool_stub_inittext_index; +static int ool_stub_text_index, ool_stub_text_end_index, ool_stub_inittext_index;int ret =0 , ool_stub_count, *ool_stub_index;ppc_inst_t inst;/* @@ -191,9 +191,22 @@ static int ftrace_init_ool_stub(struct module *mod, struct dyn_ftrace *rec) ool_stub_index = &ool_stub_inittext_index; ool_stub_count = ftrace_ool_stub_inittext_count; }else if (is_kernel_text (rec->ip)) { - ool_stub = ftrace_ool_stub_text_end; - ool_stub_index = &ool_stub_text_end_index; - ool_stub_count = ftrace_ool_stub_text_end_count; +/* +* ftrace records are sorted, so we first use up the stub area within .text +* (ftrace_ool_stub_text) before using the area at the end of .text +* (ftrace_ool_stub_text_end), unless the stub is out of range of the record. +*/ +if (ool_stub_text_index >= ftrace_ool_stub_text_count || + !is_offset_in_branch_range ((long )rec->ip - + (long )&ftrace_ool_stub_text[ool_stub_text_index])) { + ool_stub = ftrace_ool_stub_text_end; + ool_stub_index = &ool_stub_text_end_index; + ool_stub_count = ftrace_ool_stub_text_end_count; + }else { + ool_stub = ftrace_ool_stub_text; + ool_stub_index = &ool_stub_text_index; + ool_stub_count = ftrace_ool_stub_text_count; + } #ifdef CONFIG_MODULES }else if (mod) { ool_stub = mod->arch.ool_stubs;arch/powerpc/kernel/trace/ftrace_entry.S +8
-0@@ -374,6 +374,14 @@ _GLOBAL(return_to_handler) blr #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE +SYM_DATA(ftrace_ool_stub_text_count, .long CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) + +SYM_CODE_START(ftrace_ool_stub_text) + .space CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE * FTRACE_OOL_STUB_SIZE +SYM_CODE_END(ftrace_ool_stub_text) +#endif + .pushsection ".tramp.ftrace.text","aw",@progbits; .globl ftrace_tramp_text ftrace_tramp_text:arch/powerpc/tools/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later quiet_cmd_gen_ftrace_ool_stubs = GEN $@ - cmd_gen_ftrace_ool_stubs = $< vmlinux.o $@ + cmd_gen_ftrace_ool_stubs = $< $(CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) vmlinux.o $@ $(obj)/.vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE $(call if_changed,gen_ftrace_ool_stubs)arch/powerpc/tools/ftrace-gen-ool-stubs.sh +0
-0
arch/powerpc/tools/ftrace-gen-ool-stubs.sh is_enabled() { · line 13
Quoted 8 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +3
-2@@ -8,8 +8,9 @@ is_enabled() { grep -q "^$1=y" include/config/auto.conf } -vmlinux_o=${1} -arch_vmlinux_S=${2} +vmlinux_o=${2} +arch_vmlinux_S=${3} +arch_vmlinux_o=$(dirname ${arch_vmlinux_S})/$(basename ${arch_vmlinux_S} .S).o
arch_vmlinux_o is not used in this script. Delete it.
Quoted 29 lines
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +9
-3RELOCATION=R_PPC64_ADDR64 if is_enabled CONFIG_PPC32; then@@ -21,15 +22,22 @@ num_ool_stubs_text=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries num_ool_stubs_inittext=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} | grep ".init.text" | grep "${RELOCATION}" | wc -l) +num_ool_stubs_text_builtin=${1} +if [ ${num_ool_stubs_text} -gt ${num_ool_stubs_text_builtin} ]; then + num_ool_stubs_text_end=$(expr ${num_ool_stubs_text} - ${num_ool_stubs_text_builtin}) +else + num_ool_stubs_text_end=0 +fi + cat > ${arch_vmlinux_S} <<EOF #include <asm/asm-offsets.h> #include <linux/linkage.h> .pushsection .tramp.ftrace.text,"aw" -SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text}) +SYM_DATA(ftrace_ool_stub_text_end_count, .long ${num_ool_stubs_text_end}) SYM_CODE_START(ftrace_ool_stub_text_end) - .space ${num_ool_stubs_text} * FTRACE_OOL_STUB_SIZE + .space ${num_ool_stubs_text_end} * FTRACE_OOL_STUB_SIZE SYM_CODE_END(ftrace_ool_stub_text_end) .popsection --
Quoted 2 lines
2.46.0
--
Best Regards
Masahiro Yamada
Quoted 21 lines · Hari Bathini
This is v5 of the series posted here:
https://lore.kernel.org/all/[email protected]/This series reworks core ftrace support on powerpc to have the function profiling sequence moved out of line. This enables us to have a single nop at kernel function entry virtually eliminating effect of the function tracer when it is not enabled. The function profile sequence is moved out of line and is allocated at two separate places depending on a new config option.
For 64-bit powerpc, the function profiling sequence is also updated to include an additional instruction 'mtlr r0' after the usual two-instruction sequence to fix link stack imbalance (return address predictor) when ftrace is enabled. This showed an improvement of ~10% in
null_syscallbenchmark (NR_LOOPS=10000000) on a Power 10 system with ftrace enabled.Finally, support for ftrace direct calls is added based on support for
DYNAMIC_FTRACE_WITH_CALL_OPS. BPF Trampoline support is added atop this.
Quoted 8 lines
Support for ftrace direct calls is added for 32-bit powerpc. There is some code to enable bpf trampolines for 32-bit powerpc, but it is not complete and will need to be pursued separately.
Patches 1 to 10 are independent of this series and can go in separately though. Rest of the patches depend on the series from Benjamin Gray adding support for
patch_uint()andpatch_ulong():
https://lore.kernel.org/all/[email protected]/
It is getting better.
I attached a diff for improvements.
Also, please run 'shellcheck' and eliminate
as many warnings as you can.
$ shellcheck arch/powerpc/tools/ftrace-gen-ool-stubs.sh
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 19:
num_ool_stubs_text=$(${OBJDUMP} -r -j __patchable_function_entries ${vmlinux_o} |
^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
num_ool_stubs_text=$(${OBJDUMP} -r -j __patchable_function_entries "${vmlinux_o}" |
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 20:
grep -v ".init.text" | grep "${RELOCATION}" | wc -l)
^------------------^SC2126 (style): Consider using 'grep -c' instead of 'grep|wc -l'.
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 21:
num_ool_stubs_inittext=$(${OBJDUMP} -r -j __patchable_function_entries ${vmlinux_o} |
^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
num_ool_stubs_inittext=$(${OBJDUMP} -r -j __patchable_function_entries "${vmlinux_o}" |
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 22:
grep ".init.text" | grep "${RELOCATION}" | wc -l)
^------------------^
SC2126 (style): Consider using 'grep -c' instead of 'grep|wc -l'.
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 25:
if [ ${num_ool_stubs_text} -gt ${num_ool_stubs_text_builtin} ]; then
^-------------------^ SC2086 (info): Double quote to prevent
globbing and word splitting.
^---------------------------^ SC2086(info): Double quote to prevent globbing and word splitting.
Did you mean:
if [ "${num_ool_stubs_text}" -gt "${num_ool_stubs_text_builtin}" ]; then
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 26:
num_ool_stubs_text_end=$(expr ${num_ool_stubs_text} - ${num_ool_stubs_text_builtin})
^--^ SC2003 (style): expr is
antiquated. Consider rewriting this using $((..)), ${} or [[ ]].
^-------------------^ SC2086(info): Double quote to prevent globbing and word splitting.
^---------------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
num_ool_stubs_text_end=$(expr "${num_ool_stubs_text}" - "${num_ool_stubs_text_builtin}")
In arch/powerpc/tools/ftrace-gen-ool-stubs.sh line 31:
cat > ${arch_vmlinux_S} <<EOF
^---------------^ SC2086 (info): Double quote to prevent
globbing and word splitting.
Did you mean:
cat > "${arch_vmlinux_S}" <<EOF
For more information:
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
https://www.shellcheck.net/wiki/SC2003 -- expr is antiquated. Consider rewr...
https://www.shellcheck.net/wiki/SC2126 -- Consider using 'grep -c' instead ...
Quoted 88 lines
Changelog v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*
* Fixed ftrace stack tracer failure due to inadvertent use of 'add r7, r3,MCOUNT_INSN_SIZE' instruction instead of 'addi r7, r3,MCOUNT_INSN_SIZE'
* Fixed build error for !CONFIG_MODULES case.
* .vmlinux.arch.* files compiled underarch/powerpc/tools
* Made sure .vmlinux.arch.* files are cleaned withmake clean
*num_ool_stubs_text_endused for setting upftrace_ool_stub_text_endset to zero instead of computing to some random negative value when not required.
* Resolved checkpatch.pl warnings.
* Dropped RFC tag.Changelog v4:
- Patches 1, 10 and 13 are new.
- Address review comments from Nick. Numerous changes throughout the patch series.
- Extend support for ftrace ool to vmlinux text up to 64MB (patch 13).
- Address remaining TODOs in support for BPF Trampolines.
- Update synchronization when patching instructions during trampoline attach/detach.Naveen N Rao (17):
powerpc/trace: Account for -fpatchable-function-entry support by
toolchain
powerpc/kprobes: Use ftrace to determine if a probe is at function
entry
powerpc64/ftrace: Nop out additional 'std' instruction emitted by gccv5.xpowerpc32/ftrace: Unify 32-bit and 64-bit ftrace entry code powerpc/module_64: Convert #ifdef to
IS_ENABLED()powerpc/ftrace: Remove pointer to struct module fromdyn_arch_ftracepowerpc/ftrace: Skip instruction patching if the instructions are the
same
powerpc/ftrace: Move ftrace stub used for init text before_einittextpowerpc64/bpf: Foldbpf_jit_emit_func_call_hlp()into
bpf_jit_emit_func_call_rel()
powerpc/ftrace: Add a postlink script to validate function tracer
kbuild: Add generic hook for architectures to use before the final
vmlinux link
powerpc64/ftrace: Move ftrace sequence out of line powerpc64/ftrace: Support .text larger than 32MB with out-of-line
stubs
powerpc/ftrace: Add support forDYNAMIC_FTRACE_WITH_CALL_OPSpowerpc/ftrace: Add support forDYNAMIC_FTRACE_WITH_DIRECT_CALLSsamples/ftrace: Add support for ftrace direct samples on powerpc powerpc64/bpf: Add support for bpf trampolinesarch/Kconfig | 6 + arch/powerpc/Kbuild | 2 +- arch/powerpc/Kconfig | 23 +- arch/powerpc/Makefile | 8 + arch/powerpc/Makefile.postlink | 8 + arch/powerpc/include/asm/ftrace.h | 33 +- arch/powerpc/include/asm/module.h | 5 + arch/powerpc/include/asm/ppc-opcode.h | 14 + arch/powerpc/kernel/asm-offsets.c | 11 + arch/powerpc/kernel/kprobes.c | 18 +- arch/powerpc/kernel/module_64.c | 66 +- arch/powerpc/kernel/trace/Makefile | 11 +- arch/powerpc/kernel/trace/ftrace.c | 298 ++++++- arch/powerpc/kernel/trace/ftrace_64_pg.c | 69 +- arch/powerpc/kernel/trace/ftrace_entry.S | 244 ++++-- arch/powerpc/kernel/vmlinux.lds.S | 3 +- arch/powerpc/net/bpf_jit.h | 12 + arch/powerpc/net/bpf_jit_comp.c | 847 +++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 7 +- arch/powerpc/net/bpf_jit_comp64.c | 68 +- arch/powerpc/tools/Makefile | 12 + arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 52 ++ arch/powerpc/tools/ftrace_check.sh | 50 ++ samples/ftrace/ftrace-direct-modify.c | 85 +- samples/ftrace/ftrace-direct-multi-modify.c | 101 ++- samples/ftrace/ftrace-direct-multi.c | 79 +- samples/ftrace/ftrace-direct-too.c | 83 +- samples/ftrace/ftrace-direct.c | 69 +- scripts/Makefile.vmlinux | 7 + scripts/link-vmlinux.sh | 7 +- 30 files changed, 2098 insertions(+), 200 deletions(-) create mode 100644 arch/powerpc/tools/Makefile create mode 100755 arch/powerpc/tools/ftrace-gen-ool-stubs.sh create mode 100755 arch/powerpc/tools/ftrace_check.sh--
2.46.0
Signature · 2 lines
Best Regards Masahiro Yamada
From 0e96689efc977542a47e815a78892833e0305d79 Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <[email protected]>
Date: Wed, 9 Oct 2024 23:37:47 +0900
Subject: [PATCH] fixup
--- arch/Kconfig | 2 +- arch/powerpc/Kconfig | 5 ++--- arch/powerpc/tools/.gitignore | 2 ++ arch/powerpc/tools/Makefile | 7 ++----- arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 5 ++--- scripts/Makefile.vmlinux | 4 ++-- scripts/link-vmlinux.sh | 2 +- 7 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 arch/powerpc/tools/.gitignore
arch/Kconfig +1 -1
@@ -1685,7 +1685,7 @@ config ARCH_NEED_CMPXCHG_1_EMU bool config ARCH_WANTS_PRE_LINK_VMLINUX - def_bool n + bool help An architecture can select this if it provides arch/<arch>/tools/Makefile with .arch.vmlinux.o target to be linked into vmlinux.
arch/powerpc/Kconfig +2 -3
@@ -575,13 +575,12 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY config PPC_FTRACE_OUT_OF_LINE def_bool PPC64 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY - depends on PPC64 select ARCH_WANTS_PRE_LINK_VMLINUX config PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE int "Number of ftrace out-of-line stubs to reserve within .text" - default 32768 if PPC_FTRACE_OUT_OF_LINE - default 0 + depends on PPC_FTRACE_OUT_OF_LINE + default 32768 help Number of stubs to reserve for use by ftrace. This space is reserved within .text, and is distinct from any additional space
arch/powerpc/tools/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +/vmlinux.arch.S
arch/powerpc/tools/Makefile +2 -5
@@ -3,10 +3,7 @@ quiet_cmd_gen_ftrace_ool_stubs = GEN $@ cmd_gen_ftrace_ool_stubs = $< $(CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) vmlinux.o $@ -$(obj)/.vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE +$(obj)/vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE $(call if_changed,gen_ftrace_ool_stubs) -$(obj)/.vmlinux.arch.o: $(obj)/.vmlinux.arch.S FORCE - $(call if_changed_rule,as_o_S) - -clean-files += .vmlinux.arch.S .vmlinux.arch.o +targets += vmlinux.arch.S
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +2 -3
@@ -10,16 +10,15 @@ is_enabled() { vmlinux_o=${2} arch_vmlinux_S=${3} -arch_vmlinux_o=$(dirname ${arch_vmlinux_S})/$(basename ${arch_vmlinux_S} .S).o RELOCATION=R_PPC64_ADDR64 if is_enabled CONFIG_PPC32; then RELOCATION=R_PPC_ADDR32 fi -num_ool_stubs_text=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} | +num_ool_stubs_text=$(${OBJDUMP} -r -j __patchable_function_entries ${vmlinux_o} | grep -v ".init.text" | grep "${RELOCATION}" | wc -l) -num_ool_stubs_inittext=$(${CROSS_COMPILE}objdump -r -j __patchable_function_entries ${vmlinux_o} | +num_ool_stubs_inittext=$(${OBJDUMP} -r -j __patchable_function_entries ${vmlinux_o} | grep ".init.text" | grep "${RELOCATION}" | wc -l) num_ool_stubs_text_builtin=${1}
scripts/Makefile.vmlinux +2 -2
@@ -23,9 +23,9 @@ vmlinux: .vmlinux.export.o endif ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX -vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.o +vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o -arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.o +arch/$(SRCARCH)/tools/vmlinux.arch.o: vmlinux.o FORCE $(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@ endif
scripts/link-vmlinux.sh +1 -1
@@ -200,7 +200,7 @@ ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o arch_vmlinux_o="" if is_enabled CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX; then - arch_vmlinux_o=arch/${SRCARCH}/tools/.vmlinux.arch.o + arch_vmlinux_o=arch/${SRCARCH}/tools/vmlinux.arch.o fi btf_vmlinux_bin_o=
Signature · 1 line
2.43.0
Michael Ellerman
reviewerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
In reply to Alexei StarovoitovQuoted 55 lines · Alexei Starovoitov
Quoted 40 lines · Hari Bathini
Quoted 31 lines · Alexei Starovoitov
Quoted 21 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
arch/powerpc/net/bpf_jit_comp.c +8
-0+ +/* +* Generated stack layout: +* +* func prev back chain [ back chain ] +* [ ] +* bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) +* [ ] -- ...
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp.c bool bpf_jit_supports_far_kfunc_call(void) · line 775
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp.c +3
-0+ +/* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size =STACK_FRAME_MIN_SIZE +64 ;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 7 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 3 lines
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I assume that issue is that the interrupt entry unconditionally writes some data below the stack pointer, disregarding the red zone?
Quoted 2 lines
I guess ppc always has a different interrupt stack and it's not an issue?
No, the interrupt entry allocates a frame that is big enough to cover the red zone as well as the space it needs to save registers.
See STACK_INT_FRAME_SIZE which includes KERNEL_REDZONE_SIZE:
Which is renamed to INT_FRAME_SIZE in asm-offsets.c and then is used in the interrupt entry here:
cheers
Hari Bathini
maintainerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 69 lines · Michael Ellerman
Quoted 55 lines · Alexei Starovoitov
Quoted 40 lines · Hari Bathini
Quoted 31 lines · Alexei Starovoitov
Quoted 21 lines · Hari Bathini
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Hari Bathini
(quoted diff) +8
-0+ + /* + * Generated stack layout: + * + * func prev back chain [ back chain ] + * [ ] + * bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc) + * [ ] --...
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ + /* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */ + bpf_dummy_frame_size = STACK_FRAME_MIN_SIZE + 64;What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 7 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 3 lines
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I assume that issue is that the interrupt entry unconditionally writes some data below the stack pointer, disregarding the red zone?
Quoted 2 lines
I guess ppc always has a different interrupt stack and it's not an issue?
No, the interrupt entry allocates a frame that is big enough to cover the red zone as well as the space it needs to save registers.
Quoted 8 lines
See
STACK_INT_FRAME_SIZEwhich includesKERNEL_REDZONE_SIZE:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/include/asm/ptrace.h?commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n165Which is renamed to
INT_FRAME_SIZEinasm-offsets.cand then is used in the interrupt entry here:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/powerpc/kernel/exceptions-64s.S?commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n497
Thanks for clarifying that, Michael.
Only async interrupt handlers use different interrupt stacks, right?
Thanks
Hari
Hari Bathini
maintainerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
Quoted 78 lines · Hari Bathini
Quoted 75 lines · Michael Ellerman
Quoted 57 lines · Alexei Starovoitov
Quoted 42 lines · Hari Bathini
Quoted 33 lines · Alexei Starovoitov
Quoted 23 lines · Hari Bathini
Quoted 17 lines · Alexei Starovoitov
Quoted 10 lines · Hari Bathini
+
+ /*
+ * Generated stack layout:
+ *
+ * func prev back chain [ back chain ] + * [ ] + * bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc)
+ * [ ] --...
Quoted 4 lines
+
+ /* Dummy frame size for proper unwind - includes 64- bytes red zone for 64-bit powerpc */
+bpf_dummy_frame_size=STACK_FRAME_MIN_SIZE+ 64;Quoted 4 lines
What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 7 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 3 lines
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I assume that issue is that the interrupt entry unconditionally writes some data below the stack pointer, disregarding the red zone?
Quoted 2 lines
I guess ppc always has a different interrupt stack and it's not an issue?
No, the interrupt entry allocates a frame that is big enough to cover the red zone as well as the space it needs to save registers.
See
STACK_INT_FRAME_SIZEwhich includesKERNEL_REDZONE_SIZE:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
tree/arch/powerpc/include/asm/ptrace.h? commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n165
Quoted 7 lines
Quoted 4 lines
Which is renamed to
INT_FRAME_SIZEinasm-offsets.cand then is used in the interrupt entry here:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
tree/arch/powerpc/kernel/exceptions-64s.S? commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n497Thanks for clarifying that, Michael.
Only async interrupt handlers use different interrupt stacks, right?
... and separate emergency stack for some special cases...
Thanks
Hari
Hari Bathini
maintainerRe: [PATCH v5 11/17] kbuild: Add generic hook for architectures to use before the final vmlinux link
In reply to Masahiro YamadaQuoted 57 lines · Masahiro Yamada
Quoted 29 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
On powerpc, we would like to be able to make a pass on vmlinux.o and generate a new object file to be linked into vmlinux. Add a generic pass in Makefile.vmlinux that architectures can use for this purpose.
Architectures need to select
CONFIG_ARCH_WANTS_PRE_LINK_VMLINUXand must provide arch/<arch>/tools/Makefile with .arch.vmlinux.o target, which will be invoked prior to the final vmlinux link step.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*arch/Kconfig | 6 ++++++ scripts/Makefile.vmlinux | 7 +++++++ scripts/link-vmlinux.sh | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-)arch/Kconfig +0
-0arch/Kconfig config CC_HAS_SANE_FUNCTION_ALIGNMENT · line 1647
Quoted 5 lines
arch/Kconfig +2
-0@@ -1643,4 +1643,10 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT config ARCH_NEED_CMPXCHG_1_EMU bool +config ARCH_WANTS_PRE_LINK_VMLINUX + def_bool nRedundant default. This line should be "bool".
Quoted 13 lines
arch/Kconfig +4
-0+ help + An architecture can select this if it provides arch/<arch>/tools/Makefile + with .arch.vmlinux.o target to be linked into vmlinux. + endmenuscripts/Makefile.vmlinux +0
-0@@ -22,6 +22,13 @@ targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endif
Quoted 5 lines
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 26
Quoted 2 lines
scripts/Makefile.vmlinux +2
-0+ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX +vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.oIf you move this to
arch/*/tools/, there is no reason to make it a hidden file.
Thanks for reviewing and the detailed comments, Masahiro.
Quoted 10 lines
vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 28
Quoted 1 line
scripts/Makefile.vmlinux +1
-0+arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.oFORCE is missing.
I dropped FORCE as it was rebuilding vmlinux on every invocation of make irrespective of whether vmlinux.o changed or not.. Just curious if the changes you suggested makes FORCE necessary or FORCE was expected even without the other changes you suggested?
Thanks
Hari
Re: [PATCH v5 11/17] kbuild: Add generic hook for architectures to use before the final vmlinux link
Quoted 75 lines · Hari Bathini
Quoted 57 lines · Masahiro Yamada
Quoted 29 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
On powerpc, we would like to be able to make a pass on vmlinux.o and generate a new object file to be linked into vmlinux. Add a generic pass in Makefile.vmlinux that architectures can use for this purpose.
Architectures need to select
CONFIG_ARCH_WANTS_PRE_LINK_VMLINUXand must provide arch/<arch>/tools/Makefile with .arch.vmlinux.o target, which will be invoked prior to the final vmlinux link step.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*arch/Kconfig | 6 ++++++ scripts/Makefile.vmlinux | 7 +++++++ scripts/link-vmlinux.sh | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-)arch/Kconfig +0
-0arch/Kconfig config CC_HAS_SANE_FUNCTION_ALIGNMENT · line 1647
Quoted 5 lines
arch/Kconfig +2
-0@@ -1643,4 +1643,10 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT config ARCH_NEED_CMPXCHG_1_EMU bool +config ARCH_WANTS_PRE_LINK_VMLINUX + def_bool nRedundant default. This line should be "bool".
Quoted 13 lines
arch/Kconfig +4
-0+ help + An architecture can select this if it provides arch/<arch>/tools/Makefile + with .arch.vmlinux.o target to be linked into vmlinux. + endmenuscripts/Makefile.vmlinux +0
-0@@ -22,6 +22,13 @@ targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endifQuoted 5 lines
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 26
Quoted 2 lines
scripts/Makefile.vmlinux +2
-0+ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX +vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.oIf you move this to
arch/*/tools/, there is no reason to make it a hidden file.Thanks for reviewing and the detailed comments, Masahiro.
Quoted 7 lines
vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o
Quoted 7 lines
Quoted 3 lines
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 28
Quoted 1 line
scripts/Makefile.vmlinux +1
-0+arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.oFORCE is missing.
I dropped FORCE as it was rebuilding vmlinux on every invocation of
makeirrespective of whether vmlinux.o changed or not..
It is because you did not add vmlinux.arch.S to 'targets'
See my comment in 12/17.
targets += vmlinux.arch.S
Quoted 2 lines
Just curious if the changes you suggested makes FORCE necessary or FORCE was expected even without the other changes you suggested?
FORCE is necessary.
arch/powerpc/tools/Makefile must be checked every time.
When arch/powerpc/tools/ftrace-gen-ool-stubs.sh is changed, vmlinux must be relinked.
Quoted 2 lines
Thanks
Hari
--
Best Regards
Masahiro Yamada
Hari Bathini
maintainerRe: [PATCH v5 11/17] kbuild: Add generic hook for architectures to use before the final vmlinux link
Hello Masahiro,
Quoted 96 lines · Masahiro Yamada
Quoted 75 lines · Hari Bathini
Quoted 57 lines · Masahiro Yamada
Quoted 29 lines · Hari Bathini
From: Naveen N Rao <[email protected]>
On powerpc, we would like to be able to make a pass on vmlinux.o and generate a new object file to be linked into vmlinux. Add a generic pass in Makefile.vmlinux that architectures can use for this purpose.
Architectures need to select
CONFIG_ARCH_WANTS_PRE_LINK_VMLINUXand must provide arch/<arch>/tools/Makefile with .arch.vmlinux.o target, which will be invoked prior to the final vmlinux link step.Signed-off-by Naveen N Rao <[email protected]>Signed-off-by Hari Bathini <[email protected]>---
Changes in v5:
* Intermediate files named .vmlinux.arch.* instead of .arch.vmlinux.*arch/Kconfig | 6 ++++++ scripts/Makefile.vmlinux | 7 +++++++ scripts/link-vmlinux.sh | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-)arch/Kconfig +0
-0arch/Kconfig config CC_HAS_SANE_FUNCTION_ALIGNMENT · line 1647
Quoted 5 lines
arch/Kconfig +2
-0@@ -1643,4 +1643,10 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT config ARCH_NEED_CMPXCHG_1_EMU bool +config ARCH_WANTS_PRE_LINK_VMLINUX + def_bool nRedundant default. This line should be "bool".
Quoted 13 lines
arch/Kconfig +4
-0+ help + An architecture can select this if it provides arch/<arch>/tools/Makefile + with .arch.vmlinux.o target to be linked into vmlinux. + endmenuscripts/Makefile.vmlinux +0
-0@@ -22,6 +22,13 @@ targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endifQuoted 5 lines
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 26
Quoted 2 lines
scripts/Makefile.vmlinux +2
-0+ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX +vmlinux: arch/$(SRCARCH)/tools/.vmlinux.arch.oIf you move this to
arch/*/tools/, there is no reason to make it a hidden file.Thanks for reviewing and the detailed comments, Masahiro.
Quoted 7 lines
vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o
Quoted 7 lines
Quoted 3 lines
scripts/Makefile.vmlinux targets += .vmlinux.export.o · line 28
Quoted 1 line
scripts/Makefile.vmlinux +1
-0+arch/$(SRCARCH)/tools/.vmlinux.arch.o: vmlinux.oFORCE is missing.
I dropped FORCE as it was rebuilding vmlinux on every invocation of
makeirrespective of whether vmlinux.o changed or not..It is because you did not add
vmlinux.arch.Sto 'targets'See my comment in 12/17.
targets += vmlinux.arch.SQuoted 2 lines
Just curious if the changes you suggested makes FORCE necessary or FORCE was expected even without the other changes you suggested?
Quoted 7 lines
FORCE is necessary.
arch/powerpc/tools/Makefilemust be checked every time.When
arch/powerpc/tools/ftrace-gen-ool-stubs.shis changed, vmlinux must be relinked.
Thanks for the review and clarifications!
Posted v6 with the changes. Please review:
https://lore.kernel.org/all/[email protected]/
- Hari
Michael Ellerman
reviewerRe: [PATCH v5 17/17] powerpc64/bpf: Add support for bpf trampolines
In reply to Hari BathiniQuoted 85 lines · Hari Bathini
Quoted 84 lines · Hari Bathini
Quoted 83 lines · Michael Ellerman
Quoted 60 lines · Alexei Starovoitov
Quoted 44 lines · Hari Bathini
Quoted 35 lines · Alexei Starovoitov
Quoted 24 lines · Hari Bathini
Quoted 18 lines · Alexei Starovoitov
Quoted 10 lines · Hari Bathini
+
+ /*
+ * Generated stack layout:
+ *
+ * func prev back chain [ back chain ] + * [ ] + * bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc)
+ * [ ] --...
Quoted 4 lines
+
+ /* Dummy frame size for proper unwind - includes 64- bytes red zone for 64-bit powerpc */
+bpf_dummy_frame_size=STACK_FRAME_MIN_SIZE+ 64;Quoted 4 lines
What is the goal of such a large "red zone" ? The kernel stack is a limited resource. Why reserve 64 bytes ?
tail call cnt can probably be optional as well.Quoted 8 lines
Hi Alexei, thanks for reviewing.
FWIW, the redzone on ppc64 is 288 bytes. BPF JIT for ppc64 was using a redzone of 80 bytes since tailcall support was introduced [1]. It came down to 64 bytes thanks to [2]. The red zone is being used to save NVRs and tail call count when a stack is not setup. I do agree that we should look at optimizing it further. Do you think the optimization should go as part of PPC64 trampoline enablement being done here or should that be taken up as a separate item, maybe?Quoted 7 lines
The follow up is fine.
It just odd to me that we currently have:[ unused red zone ] 208 bytes protected
I simply don't understand why we need to waste this much stack space. Why can't it be zero today ?
Quoted 13 lines
The ABI for ppc64 has a redzone of 288 bytes below the current stack pointer that can be used as a scratch area until a new stack frame is created. So, no wastage of stack space as such. It is just red zone that can be used before a new stack frame is created. The comment there is only to show how redzone is being used in ppc64 BPF JIT. I think the confusion is with the mention of "208 bytes" as protected. As not all of that scratch area is used, it mentions the remaining as unused. Essentially 288 bytes below current stack pointer is protected from debuggers and interrupt code (red zone). Note that it should be 224 bytes of unused red zone instead of 208 bytes as red zone usage in ppc64 BPF JIT come down from 80 bytes to 64 bytes since [2]. Hope that clears the misunderstanding..
Quoted 3 lines
I see. That makes sense. So it's similar to amd64 red zone, but there we have an issue with irqs, hence the kernel is compiled with -mno-red-zone.
I assume that issue is that the interrupt entry unconditionally writes some data below the stack pointer, disregarding the red zone?
Quoted 2 lines
I guess ppc always has a different interrupt stack and it's not an issue?
No, the interrupt entry allocates a frame that is big enough to cover the red zone as well as the space it needs to save registers.
See
STACK_INT_FRAME_SIZEwhich includesKERNEL_REDZONE_SIZE:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
tree/arch/powerpc/include/asm/ptrace.h? commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n165Which is renamed to
INT_FRAME_SIZEinasm-offsets.cand then is used in the interrupt entry here:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
tree/arch/powerpc/kernel/exceptions-64s.S? commit=8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b#n497Thanks for clarifying that, Michael.
Only async interrupt handlers use different interrupt stacks, right?... and separate emergency stack for some special cases...
There isn't a neat rule like sync/async.
Most interrupts use the normal kernel stack, whether sync or async.
External interrupts switch to a separate hard interrupt stack (hardirq_ctx) in call_do_irq(), but only after coming in on the kernel stack first.
Some interrupts use the emergency stack (in some cases), eg. HMI, soft NMI (fake), TM bad thing (program check), or their own stack, system reset (nmi_emergency_sp), machine check (mc_emergency_sp).
cheers