powerpc64/ftrace: fixes clang build issues
cover powerpc64/ftrace: fixes clang build issues
Support for -fpatchable-function-entry on ppc64le was added in Clang with [1]. Faced a couple of issues while building the linux kernel with Clang though. Patches 1 & 2 address those issues. The last patch provides workaround to ensure, an open issue [2] in Clang with -fpatchable-function-entry support, does not impact the linux ftrace subsystem.
[1] https://github.com/llvm/llvm-project/pull/151569
[2] https://github.com/llvm/llvm-project/issues/163706
Hari Bathini (3):
powerpc64: make clang cross-build friendly powerpc64/ftrace: fix OOL stub count with clang powerpc64/ftrace: workaround clang recording GEP in
__patchable_function_entries
arch/powerpc/Kconfig | 5 ++--
arch/powerpc/kernel/trace/ftrace.c | 26 ++++++++++++++++---
....sh => check-fpatchable-function-entry.sh} | 0
arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 4 +--
4 files changed, 27 insertions(+), 8 deletions(-)
rename arch/powerpc/tools/{gcc-check-fpatchable-function-entry.sh => check-fpatchable-function-entry.sh} (100%)Signature · 1 line
2.51.1
PATCH 1/3 powerpc64: make clang cross-build friendly
ARCH_USING_PATCHABLE_FUNCTION_ENTRY depends on toolchain support for -fpatchable-function-entry option. The current script that checks for this support only handles GCC. Rename the script and extend it to detect support for -fpatchable-function-entry with Clang as well, allowing clean cross-compilation with Clang toolchains.
---
arch/powerpc/Kconfig | 5 +++--
...-function-entry.sh => check-fpatchable-function-entry.sh} | 0
2 files changed, 3 insertions(+), 2 deletions(-)
rename arch/powerpc/tools/{gcc-check-fpatchable-function-entry.sh => check-fpatchable-function-entry.sh} (100%)arch/powerpc/Kconfig +3 -2
@@ -568,8 +568,9 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY depends on FUNCTION_TRACER && (PPC32 || PPC64_ELF_ABI_V2) depends on $(cc-option,-fpatchable-function-entry=2) def_bool y if PPC32 - 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 + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_GCC + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -target ppc64le -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_CLANG + def_bool $(success,$(srctree)/arch/powerpc/tools/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
arch/powerpc/tools/check-fpatchable-function-entry.sh renamed from arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh +0 -0
Signature · 1 line
2.51.1
PATCH 2/3 powerpc64/ftrace: fix OOL stub count with clang
In reply to Hari BathiniThe total number of out-of-line (OOL) stubs required for function tracing is determined using the following command:
$(OBJDUMP) -r -j __patchable_function_entries vmlinux.o
While this works correctly with GNU objdump, llvm-objdump does not list the expected relocation records for this section. Fix this by using the -d option and counting R_PPC64_ADDR64 relocation entries. This works as desired with both objdump and llvm-objdump.
--- arch/powerpc/tools/ftrace-gen-ool-stubs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/tools/ftrace-gen-ool-stubs.sh +2 -2
@@ -15,9 +15,9 @@ if [ -z "$is_64bit" ]; then RELOCATION=R_PPC_ADDR32 fi -num_ool_stubs_total=$($objdump -r -j __patchable_function_entries "$vmlinux_o" | +num_ool_stubs_total=$($objdump -r -j __patchable_function_entries -d "$vmlinux_o" | grep -c "$RELOCATION") -num_ool_stubs_inittext=$($objdump -r -j __patchable_function_entries "$vmlinux_o" | +num_ool_stubs_inittext=$($objdump -r -j __patchable_function_entries -d "$vmlinux_o" | grep -e ".init.text" -e ".text.startup" | grep -c "$RELOCATION") num_ool_stubs_text=$((num_ool_stubs_total - num_ool_stubs_inittext))
Signature · 1 line
2.51.1
PATCH 3/3 powerpc64/ftrace: workaround clang recording GEP in __patchable_function_entries
In reply to Hari BathiniSupport for -fpatchable-function-entry on ppc64le was added in Clang with [1]. However, when no prefix NOPs are specified - as is the case with CONFIG_PPC_FTRACE_OUT_OF_LINE - the first NOP is emitted at LEP, but Clang records the Global Entry Point (GEP) unlike GCC which does record the Local Entry Point (LEP). Issue [2] has been raised to align Clang's behavior with GCC. As a temporary workaround to ensure ftrace initialization works as expected with Clang, derive the LEP using ppc_function_entry() for kernel symbols and by looking for the below module GEP sequence for module addresses, until [2] is resolved:
ld r2, -8(r12) add r2, r2, r12
[1] https://github.com/llvm/llvm-project/pull/151569
[2] https://github.com/llvm/llvm-project/issues/163706
--- arch/powerpc/kernel/trace/ftrace.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +22 -4
@@ -37,11 +37,29 @@ 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 ) && - !IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) { - addr +=MCOUNT_INSN_SIZE ; -if (IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS )) +if (IS_ENABLED (CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY )) { +if (!IS_ENABLED (CONFIG_PPC_FTRACE_OUT_OF_LINE )) { addr +=MCOUNT_INSN_SIZE ; +if (IS_ENABLED (CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS )) + addr +=MCOUNT_INSN_SIZE ; + }else if (IS_ENABLED (CONFIG_CC_IS_CLANG ) &&IS_ENABLED (CONFIG_PPC64 )) { +/* +* addr points to global entry point though the NOP was emitted at local +* entry point due to https://github.com/llvm/llvm-project/issues/163706 +* Handle that here with ppc_function_entry() for kernel symbols while +* adjusting module addresses in the else case, by looking for the below +* module global entry point sequence: +* ld r2, -8(r12) +* add r2, r2, r12 +*/ +if (is_kernel_text (addr) ||is_kernel_inittext (addr)) + addr =ppc_function_entry ((void *)addr); +else if ((ppc_inst_val (ppc_inst_read ((u32 *)addr)) == +PPC_RAW_LD (_R2, _R12,-8 )) && + (ppc_inst_val (ppc_inst_read ((u32 *)(addr+4 ))) == +PPC_RAW_ADD (_R2, _R2, _R12))) + addr +=8 ; + } }return addr;
Signature · 1 line
2.51.1
Re: [PATCH 1/3] powerpc64: make clang cross-build friendly
In reply to Hari BathiniQuoted 18 lines · Hari Bathini
ARCH_USING_PATCHABLE_FUNCTION_ENTRYdepends on toolchain support for -fpatchable-function-entry option. The current script that checks for this support only handles GCC. Rename the script and extend it to detect support for -fpatchable-function-entry with Clang as well, allowing clean cross-compilation with Clang toolchains.Signed-off-by Hari Bathini <[email protected]>--- arch/powerpc/Kconfig | 5 +++-- ...-function-entry.sh => check-fpatchable-function-entry.sh} | 0 2 files changed, 3 insertions(+), 2 deletions(-) rename arch/powerpc/tools/{gcc-check-fpatchable-function-entry.sh => check-fpatchable-function-entry.sh} (100%)arch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config ARCH_USING_PATCHABLE_FUNCTION_ENTRY · line 572
Quoted 7 lines
arch/powerpc/Kconfig +2
-2@@ -568,8 +568,9 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY depends on FUNCTION_TRACER && (PPC32 || PPC64_ELF_ABI_V2) depends on $(cc-option,-fpatchable-function-entry=2) def_bool y if PPC32 - 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 + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_GCC + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -target ppc64le -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_CLANG
Can you instead pass $(CLANG_FLAGS) to retain the same command across gcc/clang?
- Naveen
Re: [PATCH 3/3] powerpc64/ftrace: workaround clang recording GEP in __patchable_function_entries
In reply to Hari BathiniQuoted 6 lines · Hari Bathini
Support for -fpatchable-function-entry on ppc64le was added in Clang with [1]. However, when no prefix NOPs are specified - as is the case with
CONFIG_PPC_FTRACE_OUT_OF_LINE- the first NOP is emitted at LEP, but Clang records the Global Entry Point (GEP) unlike GCC which does record the Local Entry Point (LEP). Issue [2] has been raised to align Clang's behavior with GCC. As a temporary workaround to ensure ftrace
Any reason to put in a "temporary" workaround rather than waiting for this to be fixed?
- Naveen
Re: [PATCH 1/3] powerpc64: make clang cross-build friendly
In reply to Naveen N RaoThanks for the review, Naveen.
Quoted 14 lines · Naveen N Rao
Quoted 13 lines · Hari Bathini
ARCH_USING_PATCHABLE_FUNCTION_ENTRYdepends on toolchain support for -fpatchable-function-entry option. The current script that checks for this support only handles GCC. Rename the script and extend it to detect support for -fpatchable-function-entry with Clang as well, allowing clean cross-compilation with Clang toolchains.Signed-off-by Hari Bathini <[email protected]>--- arch/powerpc/Kconfig | 5 +++-- ...-function-entry.sh => check-fpatchable-function-entry.sh} | 0 2 files changed, 3 insertions(+), 2 deletions(-)rename
arch/powerpc/tools/{gcc-check-fpatchable-function-entry.sh=>check-fpatchable-function-entry.sh} (100%)
Quoted 15 lines
arch/powerpc/Kconfig config ARCH_USING_PATCHABLE_FUNCTION_ENTRY · line 572
Quoted 12 lines
arch/powerpc/Kconfig +2
-2@@ -568,8 +568,9 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY depends on FUNCTION_TRACER && (PPC32 || PPC64_ELF_ABI_V2) depends on $(cc-option,-fpatchable-function-entry=2) def_bool y if PPC32 - 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 + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_GCC + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -target ppc64le -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_CLANGCan you instead pass $(CLANG_FLAGS) to retain the same command across gcc/clang?
Should work, I guess.
But do I need to test for any additional clang flags that may interfere with what we are trying to check here?
- Hari
Re: [PATCH 3/3] powerpc64/ftrace: workaround clang recording GEP in __patchable_function_entries
In reply to Naveen N RaoQuoted 10 lines · Naveen N Rao
Quoted 6 lines · Hari Bathini
Support for -fpatchable-function-entry on ppc64le was added in Clang with [1]. However, when no prefix NOPs are specified - as is the case with
CONFIG_PPC_FTRACE_OUT_OF_LINE- the first NOP is emitted at LEP, but Clang records the Global Entry Point (GEP) unlike GCC which does record the Local Entry Point (LEP). Issue [2] has been raised to align Clang's behavior with GCC. As a temporary workaround to ensure ftraceAny reason to put in a "temporary" workaround rather than waiting for this to be fixed?
Pushing for this temporary workaround as the fix ETA for clang is not available at the moment and the temporary workaround is likely harmless even on a clang version with the fix included..
- Hari
Re: [PATCH 1/3] powerpc64: make clang cross-build friendly
In reply to Hari BathiniQuoted 33 lines · Hari Bathini
Thanks for the review, Naveen.
Quoted 14 lines · Naveen N Rao
Quoted 13 lines · Hari Bathini
ARCH_USING_PATCHABLE_FUNCTION_ENTRYdepends on toolchain support for -fpatchable-function-entry option. The current script that checks for this support only handles GCC. Rename the script and extend it to detect support for -fpatchable-function-entry with Clang as well, allowing clean cross-compilation with Clang toolchains.Signed-off-by Hari Bathini <[email protected]>--- arch/powerpc/Kconfig | 5 +++-- ...-function-entry.sh => check-fpatchable-function-entry.sh} | 0 2 files changed, 3 insertions(+), 2 deletions(-)rename
arch/powerpc/tools/{gcc-check-fpatchable-function-entry.sh=>check-fpatchable-function-entry.sh} (100%)Quoted 15 lines
arch/powerpc/Kconfig config ARCH_USING_PATCHABLE_FUNCTION_ENTRY · line 572
Quoted 12 lines
arch/powerpc/Kconfig +2
-2@@ -568,8 +568,9 @@ config ARCH_USING_PATCHABLE_FUNCTION_ENTRY depends on FUNCTION_TRACER && (PPC32 || PPC64_ELF_ABI_V2) depends on $(cc-option,-fpatchable-function-entry=2) def_bool y if PPC32 - 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 + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_GCC + def_bool $(success,$(srctree)/arch/powerpc/tools/check-fpatchable-function-entry.sh $(CC) -target ppc64le -mlittle-endian) if PPC64 && CPU_LITTLE_ENDIAN && CC_IS_CLANGCan you instead pass $(CLANG_FLAGS) to retain the same command across gcc/clang?
Quoted 3 lines
Should work, I guess.
But do I need to test for any additional clang flags that may interfere with what we are trying to check here?
From what I can tell, $(CLANG_FLAGS) includes a fairly static set of flags which will be included alongside other $CFLAGS. So, I don't think anything special should be needed there.
- Naveen