static call support for arm64
Subsystems (2)
Ard Biesheuvel
reviewerv5 cover static call support for arm64
Changes since v4:
- add preparatory patch to address generic CFI/LTO issues with static calls
- add comment to patch #2 describing the trampoline layout
- add handling of Clang CFI jump table entries
- add PeterZ's ack to patch #2
Ard Biesheuvel (2):
static_call: force symbol references with external linkage for CFI/LTO
arm64: implement support for static call trampolines
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/static_call.h | 40 +++++++++++ arch/arm64/kernel/patching.c | 72 +++++++++++++++++++- arch/arm64/kernel/vmlinux.lds.S | 1 + include/linux/static_call.h | 21 +++++- 5 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 arch/arm64/include/asm/static_call.h
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ard Biesheuvel
reviewerPATCH v5 1/2 static_call: force symbol references with external linkage for CFI/LTO
When building with Clang with CFI or LTO enabled, the linker may decide not to emit function symbols with static linkage at all, or emit them under a different symbol name. This breaks static calls, given that we refer to such functions both from C code and from assembler, and we expect the names to be the same.
So let's force the use of an alias with external linkage in a way that is visible to the compiler. This ensures that the C name and the asm name are identical.
--- include/linux/static_call.h | 21 ++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
include/linux/static_call.h +19 -2
@@ -327,10 +327,27 @@ static inline int static_call_text_reserved(void *start, void *end)#endif /* CONFIG_HAVE_STATIC_CALL */ +#ifdef CONFIG_LTO +/* +* DEFINE_STATIC_CALL() accepts any function symbol reference for its _func +* argument, but this may cause problems under Clang LTO/CFI if the function +* symbol has static linkage, because the symbol names exposed at the +* asm/object level may deviate from the C names. So let's force the reference +* to go via an alias with external linkage instead. +*/ +#define _DEFINE_STATIC_CALL (name, _func, _init, _alias) \ + extern typeof(_func) _alias __alias(_init); \ + __DEFINE_STATIC_CALL(name, _func, _alias) +#else +#define _DEFINE_STATIC_CALL (name, _func, _init, _alias) \ + __DEFINE_STATIC_CALL(name, _func, _init) +#endif +#define DEFINE_STATIC_CALL (name, _func) \ - __DEFINE_STATIC_CALL(name, _func, _func) + _DEFINE_STATIC_CALL(name, _func, _func, __UNIQUE_ID(_func))#define DEFINE_STATIC_CALL_RET0 (name, _func) \ - __DEFINE_STATIC_CALL(name, _func, __static_call_return0) + _DEFINE_STATIC_CALL(name, _func, __static_call_return0, \ + __UNIQUE_ID(_func))#endif /* _LINUX_STATIC_CALL_H */
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ard Biesheuvel
reviewerPATCH v5 2/2 arm64: implement support for static call trampolines
In reply to Ard BiesheuvelImplement arm64 support for the 'unoptimized' static call variety, which routes all calls through a single trampoline that is patched to perform a tail call to the selected function.
It is expected that the direct branch instruction will be able to cover the common case. However, given that static call targets may be located in modules loaded out of direct branching range, we need a fallback path that loads the address into R16 and uses a branch-to-register (BR) instruction to perform an indirect call.
Unlike on x86, there is no pressing need on arm64 to avoid indirect calls at all cost, but hiding it from the compiler as is done here does have some benefits:
- the literal is located in .text, which gives us the same robustness advantage that code patching does;
- no performance hit on CFI enabled Clang builds that decorate compiler emitted indirect calls with branch target validity checks.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/static_call.h | 40 +++++++++++ arch/arm64/kernel/patching.c | 72 +++++++++++++++++++- arch/arm64/kernel/vmlinux.lds.S | 1 + 4 files changed, 111 insertions(+), 3 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -193,6 +193,7 @@ config ARM64 select HAVE_PERF_USER_STACK_DUMP select HAVE_PREEMPT_DYNAMIC select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_STATIC_CALL select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUTEX_CMPXCHG if FUTEX select MMU_GATHER_RCU_TABLE_FREE
arch/arm64/include/asm/static_call.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +/* +* The sequence below is laid out in a way that guarantees that the literal and +* the instruction are always covered by the same cacheline, and can be updated +* using a single store-pair instruction (if we rewrite the BTI C instruction +* as well). This means the literal and the instruction are always in sync when +* observed via the D-side. +* +* However, this does not guarantee that the I-side will catch up immediately +* as well: until the I-cache maintenance completes, CPUs may branch to the old +* target, or execute a stale NOP or RET. We deal with this by writing the +* literal unconditionally, even if it is 0x0 or the branch is in range. That +* way, a stale NOP will fall through and call the new target via an indirect +* call. Stale RETs or Bs will be taken as before, and branch to the old +* target until the I-side catches up. +*/ +#define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insn) \ + asm(" .pushsection .static_call.text, \"ax\" \n" \ + " .align 4 \n" \ + " .globl " STATIC_CALL_TRAMP_STR(name) " \n" \ + "0: .quad 0x0 \n" \ + STATIC_CALL_TRAMP_STR(name) ": \n" \ + " hint 34 /* BTI C */ \n" \ + insn " \n" \ + " ldr x16, 0b \n" \ + " cbz x16, 1f \n" \ + " br x16 \n" \ + "1: ret \n" \ + " .popsection \n") + +#define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "b " #func) + +#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP (name) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret") + +#endif /* _ASM_STATIC_CALL_H */
arch/arm64/kernel/patching.c +69 -3
90 lines not on this page · raw message
arch/arm64/kernel/vmlinux.lds.S +1 -0
@@ -161,6 +161,7 @@ SECTIONS IDMAP_TEXT HIBERNATE_TEXT TRAMP_TEXT + STATIC_CALL_TEXT *(.fixup) *(.gnu.warning) . = ALIGN(16);
Signature · 7 lines
2.30.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel