arm64: implement support for static call trampolines
Subsystems (2)
Ard Biesheuvel
reviewerPATCH 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.
Since static call targets may be located in modules loaded out of direct branching range, we need to be able to fall back to issuing a ADRP/ADD pair to load the branch target into R16 and use a BR instruction. As this involves patching more than a single B or NOP instruction (for which the architecture makes special provisions in terms of the synchronization needed), we should take care to only use aarch64_insn_patch_text_nosync() if the subsequent instruction is still a 'RET' (which guarantees that the one being patched is a B or a NOP)
---
Note that I needed the changes after the --8<-- below to enable the selftest, since it assumes HAVE_STATIC_CALL_INLINE
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/static_call.h | 26 ++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/static_call.c | 52 ++++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 1 + 5 files changed, 81 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +1 -0
@@ -172,6 +172,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 +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +#define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insns) \ + asm(".pushsection .static_call.text, \"ax\" \n" \ + ".align 2 \n" \ + ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \ + STATIC_CALL_TRAMP_STR(name) ": \n" \ + "hint 34; /* BTI C */ \n" \ + insns " \n" \ + ".popsection \n") + +/* +* We have to account for the possibility that the static call site may +* be updated to refer to a target that is out of range for an ordinary +* 'B' branch instruction, and so we need to pre-allocate some space for +* a ADRP/ADD/BR sequence. +*/ +#define ARCH_DEFINE_STATIC_CALL_TRAMP (name, func) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "b " #func "; ret; br x16") + +#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP (name) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "nop; ret; br x16") + +#endif /* _ASM_STATIC_CALL_H */
arch/arm64/kernel/Makefile +1 -1
@@ -17,7 +17,7 @@ obj-y := debug-monitors.o entry.o irq.o fpsimd.o \ return_address.o cpuinfo.o cpu_errata.o \ cpufeature.o alternative.o cacheinfo.o \ smp.o smp_spin_table.o topology.o smccc-call.o \ - syscall.o proton-pack.o + syscall.o proton-pack.o static_call.o targets += efi-entry.o
arch/arm64/kernel/static_call.c new file +52 -0
52 lines not on this page · raw message
arch/arm64/kernel/vmlinux.lds.S +1 -0
@@ -123,6 +123,7 @@ SECTIONS IDMAP_TEXT HIBERNATE_TEXT TRAMP_TEXT + STATIC_CALL_TEXT *(.fixup) *(.gnu.warning) . = ALIGN(16);
Signature · 4 lines
2.17.1 ---------------8<---------------------
kernel/Makefile +1 -1
@@ -111,7 +111,7 @@ obj-$(CONFIG_CPU_PM) += cpu_pm.o obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o -obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_HAVE_STATIC_CALL) += static_call.o obj-$(CONFIG_PERF_EVENTS) += events/
kernel/static_call.c +3 -0
@@ -10,6 +10,8 @@#include <linux/processor.h> #include <asm/sections.h> +#ifdef CONFIG_STATIC_CALL_INLINE +extern struct static_call_site __start_static_call_sites[], __stop_static_call_sites[];
@@ -437,6 +439,7 @@ int __init static_call_init(void) return 0; }early_initcall (static_call_init); +#endif #ifdef CONFIG_STATIC_CALL_SELFTEST
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Peter Zijlstra
maintainerQuoted 4 lines · Ard Biesheuvel
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.
Quoted 9 lines
Since static call targets may be located in modules loaded out of direct branching range, we need to be able to fall back to issuing a ADRP/ADD pair to load the branch target into R16 and use a BR instruction. As this involves patching more than a single B or NOP instruction (for which the architecture makes special provisions in terms of the synchronization needed), we should take care to only use
aarch64_insn_patch_text_nosync()if the subsequent instruction is still a 'RET' (which guarantees that the one being patched is a B or a NOP)
Aside of lacking objtool support (which is being worked on), is there anything else in the way of also doing inline patching for ARM64?
That is; if the function is not reachable by the immediate you can always leave (re-instate) the call to the trampoline after patching that.
Anyway, nice to see ARM64 support, thanks!
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ard Biesheuvel
reviewerQuoted 16 lines · Peter Zijlstra
Quoted 4 lines · Ard Biesheuvel
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.
Quoted 9 lines
Since static call targets may be located in modules loaded out of direct branching range, we need to be able to fall back to issuing a ADRP/ADD pair to load the branch target into R16 and use a BR instruction. As this involves patching more than a single B or NOP instruction (for which the architecture makes special provisions in terms of the synchronization needed), we should take care to only use
aarch64_insn_patch_text_nosync()if the subsequent instruction is still a 'RET' (which guarantees that the one being patched is a B or a NOP)
Quoted 3 lines
Aside of lacking objtool support (which is being worked on), is there anything else in the way of also doing inline patching for ARM64?
I implemented a GCC plugin for this a while ago [0], but I guess we'd need to see some evidence first that it will actually make a difference.
Quoted 4 lines
That is; if the function is not reachable by the immediate you can always leave (re-instate) the call to the trampoline after patching that.
I don't think there is anything fundamentally preventing us from doing
that: the trampolines are guaranteed to be in range for all the callers, as modules that are far away will have their own PLT trampoline inside the module itself, so we can always fall back to the trampoline if we cannot patch the branch itself. However, we might be able to do better here, i.e., at patch time, we could detect whether the call site points into a module trampoline, and update that one, instead of bouncing from one trampoline to the next.
But again, we'd need to see some use cases first where it makes a difference. And actually, the same reasoning applies to this patch - I haven't done any performance testing, nor do I have any use cases in mind where this sits on a hot path. The reason I have been looking at static calls is to clean up kludges such as the one we have in lib/crc-t10dif.c for switching to an arch optimized implementation at runtime.
Quoted 1 line
Anyway, nice to see ARM64 support, thanks!
[0] https://git.kernel.org/pub/scm/linux/kernel/git/ardb/linux.git/log/?h=static-calls
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Peter Zijlstra
maintainerQuoted 3 lines · Ard Biesheuvel
I implemented a GCC plugin for this a while ago [0], but I guess we'd need to see some evidence first that it will actually make a difference.
https://lkml.kernel.org/r/[email protected]
might provide fertile benchmarking ground. Of course, you'd need to port it to arm64 first :-)
Anyway, Frederic will reportedly go work on getting that in shape and merged.
There's also this:
https://lkml.kernel.org/r/[email protected]
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel