arm64: implement support for static call trampolines
Subsystems (2)
Ard Biesheuvel
reviewerPATCH v4 1/1 arm64: implement support for static call trampolines
Implement 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.
---
This is a follow-up to Frederic's rebase [0] of my v3 sent out a while ago [1]. Even though Frederic's changes depend on the static call API, this patch can be reviewed in isolation, so I am sending it as a single patch.
[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/static_call.h | 25 ++++++++++ arch/arm64/kernel/patching.c | 51 ++++++++++++++++++-- arch/arm64/kernel/vmlinux.lds.S | 1 + 4 files changed, 75 insertions(+), 3 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -192,6 +192,7 @@ config ARM64 select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP 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 +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +#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 +48 -3
69 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