Skip to contentSkip to conversation

static call support for arm64

PATCH In review Ard Biesheuvel 5 participants 15 messages Nov 5, 2021 – Mar 12

Subsystems (2)
lore
Share & tools
lore

Ard Biesheuvel

author reviewer
#0 lore raw

v6 cover static call support for arm64

This implements non-inline static calls for arm64. This is rather straight-forward, as we don't rely on any tooling to look for static call sites etc. The only minor complication is Clang CFI, which is already in mainline for arm64, and requires a little tweak to ensure that we don't end up patching the CFI jump table instead of the static call trampoline itself.

Changes since v5:
- drop the patch that works around issues with references to symbols with static linkage from asm blocks; this is specific to Clang+ThinLTO in versions before 13, so we can just decide not to support that config.
- add a patch to use non-function type symbols for the trampolines, to ensure that taking the address gives us the trampoline itself rather than the address of a CFI jump table entry that branches to it.

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

Cc Mark Rutland <[email protected]>
Cc Quentin Perret <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc James Morse <[email protected]>
Cc Will Deacon <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Kees Cook <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Steven Rostedt <[email protected]>

Ard Biesheuvel (2):
static_call: use non-function types to refer to the trampolines
arm64: implement support for static call trampolines

 arch/arm64/Kconfig                   |  2 +
 arch/arm64/include/asm/static_call.h | 40 ++++++++++
 arch/arm64/kernel/patching.c         | 77 +++++++++++++++++++-
 arch/arm64/kernel/vmlinux.lds.S      |  1 +
 include/linux/static_call.h          |  4 +-
 include/linux/static_call_types.h    | 11 ++-
 6 files changed, 127 insertions(+), 8 deletions(-)
 create mode 100644 arch/arm64/include/asm/static_call.h
Signature · 1 line
2.30.2
author reviewer
#1 lore raw

PATCH v6 1/2 static_call: use non-function types to refer to the trampolines

In order to prevent CFI enabled code from grabbing a jump table entry that jumps to the trampoline, rather than the trampoline itself, use an incomplete non-function type for the trampoline, and cast it to the right type only when invoking it.

---
 include/linux/static_call.h       |  4 ++--
 include/linux/static_call_types.h | 11 ++++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)
include/linux/static_call.h +2 -2
index 3e56a9751c06..616607393273 100644
@@ -151,7 +151,7 @@ extern void arch_static_call_transform(void *site, void *tramp, void *func, bool
 
 #define static_call_update(name, func)					\
 ({									\
-	typeof(&STATIC_CALL_TRAMP(name)) __F = (func);			\
+	typeof(&STATIC_CALL_TYPE(name)) __F = (func);			\
 	__static_call_update(&STATIC_CALL_KEY(name),			\
 			     STATIC_CALL_TRAMP_ADDR(name), __F);	\
 })
@@ -306,7 +306,7 @@ static inline void __static_call_nop(void) { }
 	void *func = READ_ONCE(STATIC_CALL_KEY(name).func);		\
 	if (!func)							\
 		func = &__static_call_nop;				\
-	(typeof(STATIC_CALL_TRAMP(name))*)func;				\
+	(typeof(&STATIC_CALL_TYPE(name)))func;				\
 })
 
 #define static_call_cond(name)	(void)__static_call_cond(name)
include/linux/static_call_types.h +8 -3
index 5a00b8b2cf9f..5e658ef537e4 100644
@@ -18,6 +18,9 @@
 #define STATIC_CALL_TRAMP(name)		__PASTE(STATIC_CALL_TRAMP_PREFIX, name)
 #define STATIC_CALL_TRAMP_STR(name)	__stringify(STATIC_CALL_TRAMP(name))
 
+#define STATIC_CALL_TYPE_PREFIX		__SCtype__
+#define STATIC_CALL_TYPE(name)		__PASTE(STATIC_CALL_TYPE_PREFIX, name)
+
 /*
  * Flags in the low bits of static_call_site::key.
  */
@@ -36,11 +39,13 @@ struct static_call_site {
 
 #define DECLARE_STATIC_CALL(name, func)					\
 	extern struct static_call_key STATIC_CALL_KEY(name);		\
-	extern typeof(func) STATIC_CALL_TRAMP(name);
+	extern struct static_call_tramp STATIC_CALL_TRAMP(name);	\
+	extern typeof(func) STATIC_CALL_TYPE(name)
 
 #ifdef CONFIG_HAVE_STATIC_CALL
 
-#define __raw_static_call(name)	(&STATIC_CALL_TRAMP(name))
+#define __raw_static_call(name)						\
+	((typeof(&STATIC_CALL_TYPE(name)))&STATIC_CALL_TRAMP(name))
 
 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
 
@@ -96,7 +101,7 @@ struct static_call_key {
 };
 
 #define static_call(name)						\
-	((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
+	((typeof(&STATIC_CALL_TYPE(name)))(STATIC_CALL_KEY(name).func))
 
 #endif /* CONFIG_HAVE_STATIC_CALL */
 
Signature · 1 line
2.30.2
author reviewer
#3 lore raw

PATCH v6 2/2 arm64: implement support for static call trampolines

In reply to 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.

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                   |  2 +
 arch/arm64/include/asm/static_call.h | 40 ++++++++++
 arch/arm64/kernel/patching.c         | 77 +++++++++++++++++++-
 arch/arm64/kernel/vmlinux.lds.S      |  1 +
 4 files changed, 117 insertions(+), 3 deletions(-)
arch/arm64/Kconfig +2 -0
index 176d6fddc4f2..ccc33b85769c 100644
@@ -193,6 +193,8 @@ config ARM64
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
+	# https://github.com/ClangBuiltLinux/linux/issues/1354
+	select HAVE_STATIC_CALL if !LTO_CLANG_THIN || CLANG_VERSION >= 130000
 	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
new file mode 100644
index 000000000000..6ee918991510
@@ -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 (provided that 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.
+ */
+#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 +74 -3

101 lines not on this page · raw message

arch/arm64/kernel/vmlinux.lds.S +1 -0
index 50bab186c49b..e16860a14eaf 100644
@@ -173,6 +173,7 @@ SECTIONS
 			HIBERNATE_TEXT
 			KEXEC_TEXT
 			TRAMP_TEXT
+			STATIC_CALL_TEXT
 			*(.gnu.warning)
 		. = ALIGN(16);
 		*(.got)			/* Global offset table		*/
Signature · 1 line
2.30.2
maintainer Acked-by on v6 1/2
#2 lore raw

Re: [PATCH v6 1/2] static_call: use non-function types to refer to the trampolines

In reply to Ard Biesheuvel
Quoted 6 lines · Ard Biesheuvel

In order to prevent CFI enabled code from grabbing a jump table entry that jumps to the trampoline, rather than the trampoline itself, use an incomplete non-function type for the trampoline, and cast it to the right type only when invoking it.

Very grudingly:

Quoted 69 lines
---
 include/linux/static_call.h       |  4 ++--
 include/linux/static_call_types.h | 11 ++++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)
include/linux/static_call.h +2 -2
index 3e56a9751c06..616607393273 100644
@@ -151,7 +151,7 @@ extern void arch_static_call_transform(void *site, void *tramp, void *func, bool
 
 #define static_call_update(name, func)					\
 ({									\
-	typeof(&STATIC_CALL_TRAMP(name)) __F = (func);			\
+	typeof(&STATIC_CALL_TYPE(name)) __F = (func);			\
 	__static_call_update(&STATIC_CALL_KEY(name),			\
 			     STATIC_CALL_TRAMP_ADDR(name), __F);	\
 })
@@ -306,7 +306,7 @@ static inline void __static_call_nop(void) { }
 	void *func = READ_ONCE(STATIC_CALL_KEY(name).func);		\
 	if (!func)							\
 		func = &__static_call_nop;				\
-	(typeof(STATIC_CALL_TRAMP(name))*)func;				\
+	(typeof(&STATIC_CALL_TYPE(name)))func;				\
 })
 
 #define static_call_cond(name)	(void)__static_call_cond(name)
include/linux/static_call_types.h +8 -3
index 5a00b8b2cf9f..5e658ef537e4 100644
@@ -18,6 +18,9 @@
 #define STATIC_CALL_TRAMP(name)		__PASTE(STATIC_CALL_TRAMP_PREFIX, name)
 #define STATIC_CALL_TRAMP_STR(name)	__stringify(STATIC_CALL_TRAMP(name))
 
+#define STATIC_CALL_TYPE_PREFIX		__SCtype__
+#define STATIC_CALL_TYPE(name)		__PASTE(STATIC_CALL_TYPE_PREFIX, name)
+
 /*
  * Flags in the low bits of static_call_site::key.
  */
@@ -36,11 +39,13 @@ struct static_call_site {
 
 #define DECLARE_STATIC_CALL(name, func)					\
 	extern struct static_call_key STATIC_CALL_KEY(name);		\
-	extern typeof(func) STATIC_CALL_TRAMP(name);
+	extern struct static_call_tramp STATIC_CALL_TRAMP(name);	\
+	extern typeof(func) STATIC_CALL_TYPE(name)
 
 #ifdef CONFIG_HAVE_STATIC_CALL
 
-#define __raw_static_call(name)	(&STATIC_CALL_TRAMP(name))
+#define __raw_static_call(name)						\
+	((typeof(&STATIC_CALL_TYPE(name)))&STATIC_CALL_TRAMP(name))
 
 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
 
@@ -96,7 +101,7 @@ struct static_call_key {
 };
 
 #define static_call(name)						\
-	((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
+	((typeof(&STATIC_CALL_TYPE(name)))(STATIC_CALL_KEY(name).func))
 
 #endif /* CONFIG_HAVE_STATIC_CALL */
 
Signature · 1 line
2.30.2
maintainer
#4 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

In reply to Ard Biesheuvel
Quoted 15 lines · Ard Biesheuvel
arch/arm64/include/asm/static_call.h new file +9 -0
new file mode 100644
index 000000000000..6ee918991510
@@ -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 (provided that we rewrite the BTI C
+ * instruction as well). This means the literal and the instruction are always

arch/arm64/include/asm/static_call.h line 19

Quoted 10 lines
arch/arm64/include/asm/static_call.h new file +10 -0
+ * 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.
+ */

Thanks for the comment!

Quoted 10 lines
arch/arm64/kernel/patching.c +4 -0
index 771f543464e0..a265a87d4d9e 100644
 
 
+static void *strip_cfi_jt(void *addr)
+{
+	if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+		void *p = addr;

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 107

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+		u32 insn;
+
+		/*
+		 * Taking the address of a function produces the address of the
+		 * jump table entry when Clang CFI is enabled. Such entries are
+		 * ordinary jump instructions, preceded by a BTI C instruction
+		 * if BTI is enabled for the kernel.
+		 */
+		if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
+			p += 4;

Perhaps:

		if (aarch64_insn_is_bti(le32_to_cpup(p)))
			p += 4;

Perhapser still, add:

		else
			WARN_ON(IS_ENABLED(CONFIG_ARM64_BTI_KERNEL));

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 9 lines
arch/arm64/kernel/patching.c +9 -0
+
+		insn = le32_to_cpup(p);
+		if (aarch64_insn_is_b(insn))
+			return p + aarch64_get_branch_offset(insn);
+
+		WARN_ON(1);
+	}
+	return addr;
+}

Also, can this please have a comment decrying the lack of built-in for this?

author reviewer
#5 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Quoted 53 lines · Peter Zijlstra
Quoted 15 lines · Ard Biesheuvel
arch/arm64/include/asm/static_call.h new file +9 -0
new file mode 100644
index 000000000000..6ee918991510
@@ -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 (provided that we rewrite the BTI C
+ * instruction as well). This means the literal and the instruction are always

arch/arm64/include/asm/static_call.h line 19

Quoted 10 lines
arch/arm64/include/asm/static_call.h new file +10 -0
+ * 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.
+ */

Thanks for the comment!

Quoted 10 lines
arch/arm64/kernel/patching.c +4 -0
index 771f543464e0..a265a87d4d9e 100644
 
 
+static void *strip_cfi_jt(void *addr)
+{
+     if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+             void *p = addr;

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 107

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+             u32 insn;
+
+             /*
+              * Taking the address of a function produces the address of the
+              * jump table entry when Clang CFI is enabled. Such entries are
+              * ordinary jump instructions, preceded by a BTI C instruction
+              * if BTI is enabled for the kernel.
+              */
+             if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
+                     p += 4;

Perhaps:

Quoted 1 line
                if (aarch64_insn_is_bti(le32_to_cpup(p)))

That instruction does not exist yet, and it begs the question which type of BTI instruction we want to detect.

Quoted 6 lines
                        p += 4;

Perhapser still, add:

                else
                        WARN_ON(IS_ENABLED(CONFIG_ARM64_BTI_KERNEL));

There's already a WARN() below that will trigger and return the original address if the entry did not have the expected layout, which means a direct branch at offset 0x0 or 0x4 depending on whether BTI is on.

So I could add a WARN() here as well, but I'd prefer to keep the one at the bottom, which makes the one here slightly redundant.

Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/patching.c +4 -0
+
+             insn = le32_to_cpup(p);
+             if (aarch64_insn_is_b(insn))
+                     return p + aarch64_get_branch_offset(insn);
Quoted 8 lines

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 5 lines
arch/arm64/kernel/patching.c +5 -0
+
+             WARN_ON(1);
+     }
+     return addr;
+}

Also, can this please have a comment decrying the lack of built-in for this?

Sure.

maintainer
#6 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Quoted 20 lines · Ard Biesheuvel
Quoted 16 lines · Peter Zijlstra
Quoted 4 lines
arch/arm64/kernel/patching.c +4 -0
+static void *strip_cfi_jt(void *addr)
+{
+     if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+             void *p = addr;

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 107

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+             u32 insn;
+
+             /*
+              * Taking the address of a function produces the address of the
+              * jump table entry when Clang CFI is enabled. Such entries are
+              * ordinary jump instructions, preceded by a BTI C instruction
+              * if BTI is enabled for the kernel.
+              */
+             if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
+                     p += 4;

Perhaps:

Quoted 1 line
                if (aarch64_insn_is_bti(le32_to_cpup(p)))
Quoted 2 lines

That instruction does not exist yet, and it begs the question which type of BTI instruction we want to detect.

Yeah, I actually checked, but I figured the intent was clear enough. I figured all of them?

Quoted 7 lines
Quoted 6 lines
                        p += 4;

Perhapser still, add:

                else
                        WARN_ON(IS_ENABLED(CONFIG_ARM64_BTI_KERNEL));
Quoted 7 lines

There's already a WARN() below that will trigger and return the original address if the entry did not have the expected layout, which means a direct branch at offset 0x0 or 0x4 depending on whether BTI is on.

So I could add a WARN() here as well, but I'd prefer to keep the one at the bottom, which makes the one here slightly redundant.

Sure, that works. The slightly more paranoid me would tell you that the code as is might match something you didn't want it to.

Eg. without the extra WARN, you could accidentally match a B instruction without BTI on a BTI kernel build. Or your initial version could even match:

	RET;
	B ponies;

on a BTI kernel.

My point being that since we're not exactly sure what a future compiler will generate for us here, we'd best be maximally paranoid about what we're willing to accept.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/patching.c +4 -0
+
+             insn = le32_to_cpup(p);
+             if (aarch64_insn_is_b(insn))
+                     return p + aarch64_get_branch_offset(insn);
Quoted 8 lines

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 5 lines
arch/arm64/kernel/patching.c +5 -0
+
+             WARN_ON(1);
+     }
+     return addr;
+}

Also, can this please have a comment decrying the lack of built-in for this?

Quoted 1 line

Sure.

Which ties in with that. Once it's a built-in, we can be sure the compiler knows what it needs to do to undo it's own magic.

reviewer
#7 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

In reply to Ard Biesheuvel

Hi Ard,

Quoted 13 lines · Ard Biesheuvel
arch/arm64/kernel/patching.c +13 -0
+static void *strip_cfi_jt(void *addr)
+{
+	if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+		void *p = addr;
+		u32 insn;
+
+		/*
+		 * Taking the address of a function produces the address of the
+		 * jump table entry when Clang CFI is enabled. Such entries are
+		 * ordinary jump instructions, preceded by a BTI C instruction
+		 * if BTI is enabled for the kernel.
+		 */
+		if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+			p += 4;
+
+		insn = le32_to_cpup(p);
+		if (aarch64_insn_is_b(insn))
+			return p + aarch64_get_branch_offset(insn);
+
+		WARN_ON(1);
+	}
+	return addr;
+}

I'm somewhat uncomfortable with this, because it seems like the compiler could easily violate our expectations in future, and then we're in for a massive headache. I assume clang doesn't provide any guarnatee as to the format of the jump table entries (and e.g. I can see scope for branch padding breaking this).

In trying to sidestep that I ended up with:

https://lore.kernel.org/linux-arm-kernel/[email protected]/

... which I think is a good option for PREEMPT_DYNAMIC, but I don't know if there were other places where we believe static calls would be critical for performance rather than a nice-to-have, and whether we truly need static calls on arm64. My mind is leaning towards "avoid if reasonable" at the moment (or at least make that mutually exclusive with CFI so we can avoid that specific fun).

I see you had at least one other user in:

https://lore.kernel.org/r/[email protected]

... what were your thoughts on the criticality of that?

FWIW other than the above this looks good to me. My major concern here is fragility/maintenance, and secondly whether we're gaining much in practice. So if you think we really need this, I'm not going to stand in the way.

Thanks
Mark.

Quoted 60 lines
arch/arm64/kernel/patching.c +42 -0
+void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
+{
+	/*
+	 * -0x8	<literal>
+	 *  0x0	bti c		<--- trampoline entry point
+	 *  0x4	<branch or nop>
+	 *  0x8	ldr x16, <literal>
+	 *  0xc	cbz x16, 20
+	 * 0x10	br x16
+	 * 0x14	ret
+	 */
+	struct {
+		u64	literal;
+		__le32	insn[2];
+	} insns;
+	u32 insn;
+	int ret;
+
+	insn = aarch64_insn_gen_hint(AARCH64_INSN_HINT_BTIC);
+	insns.literal = (u64)func;
+	insns.insn[0] = cpu_to_le32(insn);
+
+	if (!func) {
+		insn = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_LR,
+						   AARCH64_INSN_BRANCH_RETURN);
+	} else {
+		insn = aarch64_insn_gen_branch_imm((u64)tramp + 4,
+						   (u64)strip_cfi_jt(func),
+						   AARCH64_INSN_BRANCH_NOLINK);
+
+		/*
+		 * Use a NOP if the branch target is out of range, and rely on
+		 * the indirect call instead.
+		 */
+		if (insn == AARCH64_BREAK_FAULT)
+			insn = aarch64_insn_gen_hint(AARCH64_INSN_HINT_NOP);
+	}
+	insns.insn[1] = cpu_to_le32(insn);
+
+	ret = __aarch64_insn_write(tramp - 8, &insns, sizeof(insns));
+	if (!WARN_ON(ret))
+		caches_clean_inval_pou((u64)tramp - 8, sizeof(insns));
 }
 
 int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
arch/arm64/kernel/vmlinux.lds.S +1 -0
index 50bab186c49b..e16860a14eaf 100644
@@ -173,6 +173,7 @@ SECTIONS
 			HIBERNATE_TEXT
 			KEXEC_TEXT
 			TRAMP_TEXT
+			STATIC_CALL_TEXT
 			*(.gnu.warning)
 		. = ALIGN(16);
 		*(.got)			/* Global offset table		*/
Signature · 1 line
2.30.2
author reviewer
#8 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Quoted 43 lines · Mark Rutland

Hi Ard,

Quoted 13 lines · Ard Biesheuvel
arch/arm64/kernel/patching.c +13 -0
+static void *strip_cfi_jt(void *addr)
+{
+     if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+             void *p = addr;
+             u32 insn;
+
+             /*
+              * Taking the address of a function produces the address of the
+              * jump table entry when Clang CFI is enabled. Such entries are
+              * ordinary jump instructions, preceded by a BTI C instruction
+              * if BTI is enabled for the kernel.
+              */
+             if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+                     p += 4;
+
+             insn = le32_to_cpup(p);
+             if (aarch64_insn_is_b(insn))
+                     return p + aarch64_get_branch_offset(insn);
+
+             WARN_ON(1);
+     }
+     return addr;
+}

I'm somewhat uncomfortable with this, because it seems like the compiler could easily violate our expectations in future, and then we're in for a massive headache. I assume clang doesn't provide any guarnatee as to the format of the jump table entries (and e.g. I can see scope for branch padding breaking this).

In trying to sidestep that I ended up with:

https://lore.kernel.org/linux-arm-kernel/[email protected]/

... which I think is a good option for PREEMPT_DYNAMIC, but I don't know if there were other places where we believe static calls would be critical for performance rather than a nice-to-have, and whether we truly need static calls on arm64. My mind is leaning towards "avoid if reasonable" at the moment (or at least make that mutually exclusive with CFI so we can avoid that specific fun).

Quoted 6 lines

I see you had at least one other user in:

https://lore.kernel.org/r/[email protected]

... what were your thoughts on the criticality of that?

That particular use case does not rely on static calls being fast at all, so there it doesn't really matter which variety we implement. The reason I sent it out today is because it gives some test coverage for static calls used in a way that the API as designed should support, but which turned out to be slightly broken in practice.

Quoted 4 lines

FWIW other than the above this looks good to me. My major concern here is fragility/maintenance, and secondly whether we're gaining much in practice. So if you think we really need this, I'm not going to stand in the way.

Android relies heavily on tracepoints for vendor hooks, and given the performance impact of CFI on indirect calls, there has been interest in enabling static calls to replace them.

Quentin, anything to add here?

#9 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Quoted 62 lines · Ard Biesheuvel
Quoted 43 lines · Mark Rutland

Hi Ard,

Quoted 13 lines · Ard Biesheuvel
arch/arm64/kernel/patching.c +13 -0
+static void *strip_cfi_jt(void *addr)
+{
+     if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+             void *p = addr;
+             u32 insn;
+
+             /*
+              * Taking the address of a function produces the address of the
+              * jump table entry when Clang CFI is enabled. Such entries are
+              * ordinary jump instructions, preceded by a BTI C instruction
+              * if BTI is enabled for the kernel.
+              */
+             if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 116

Quoted 10 lines
arch/arm64/kernel/patching.c +10 -0
+                     p += 4;
+
+             insn = le32_to_cpup(p);
+             if (aarch64_insn_is_b(insn))
+                     return p + aarch64_get_branch_offset(insn);
+
+             WARN_ON(1);
+     }
+     return addr;
+}

I'm somewhat uncomfortable with this, because it seems like the compiler could easily violate our expectations in future, and then we're in for a massive headache. I assume clang doesn't provide any guarnatee as to the format of the jump table entries (and e.g. I can see scope for branch padding breaking this).

In trying to sidestep that I ended up with:

https://lore.kernel.org/linux-arm-kernel/[email protected]/

... which I think is a good option for PREEMPT_DYNAMIC, but I don't know if there were other places where we believe static calls would be critical for performance rather than a nice-to-have, and whether we truly need static calls on arm64. My mind is leaning towards "avoid if reasonable" at the moment (or at least make that mutually exclusive with CFI so we can avoid that specific fun).

Quoted 6 lines

I see you had at least one other user in:

https://lore.kernel.org/r/[email protected]

... what were your thoughts on the criticality of that?

That particular use case does not rely on static calls being fast at all, so there it doesn't really matter which variety we implement. The reason I sent it out today is because it gives some test coverage for static calls used in a way that the API as designed should support, but which turned out to be slightly broken in practice.

Quoted 4 lines

FWIW other than the above this looks good to me. My major concern here is fragility/maintenance, and secondly whether we're gaining much in practice. So if you think we really need this, I'm not going to stand in the way.

Quoted 5 lines

Android relies heavily on tracepoints for vendor hooks, and given the performance impact of CFI on indirect calls, there has been interest in enabling static calls to replace them.

Quentin, anything to add here?

Yes, Android should definitely benefit from static calls.

Modules attaching to tracepoints cause a measurable overhead w/ CFI as the jump target is a bit harder to verify if it is not in-kernel. But sadly that's a common pattern for GKI. The current 'workaround' in Android has been to just plain disable CFI around all tracepoints in the kernel, which is a bit sad from a security PoV. But there was really no other option at the time, and we needed the performance back. Static calls would be a far superior solution as they would avoid much of the CFI overhead, and are not vulnerable in the CFI sense (that is, the branch target can't be easily overridden with a random OOB write from a dodgy driver). So yes, we'd really like to have those please :)

Thanks,
Quentin

reviewer
#10 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Hi,

Quoted 4 lines · Quentin Perret
Quoted 3 lines · Ard Biesheuvel

Android relies heavily on tracepoints for vendor hooks, and given the performance impact of CFI on indirect calls, there has been interest in enabling static calls to replace them.

Hhmm.... what exactly is a "vendor hook" in this context, and what is it doing with a tracepoint? From an upstream perspective that sounds somewhat fishy usage.

Quoted 6 lines
Quoted 1 line

Quentin, anything to add here?

Yes, Android should definitely benefit from static calls.

Modules attaching to tracepoints cause a measurable overhead w/ CFI as the jump target is a bit harder to verify if it is not in-kernel.

Where does that additional overhead come from when the target is not in-kernel?

I hope that I am wrong in understanding that __cfi_slowpath_diag() means we're always doing an out-of-line check when calling into a module?

If that were the case, that would seem to be a much more general problem with the current clang CFI scheme, and my fear here is that we're adding fragility and complexity in specific plces to work around general problems with the CFI scheme.

Thanks,
Mark.

Quoted 11 lines

But sadly that's a common pattern for GKI. The current 'workaround' in Android has been to just plain disable CFI around all tracepoints in the kernel, which is a bit sad from a security PoV. But there was really no other option at the time, and we needed the performance back. Static calls would be a far superior solution as they would avoid much of the CFI overhead, and are not vulnerable in the CFI sense (that is, the branch target can't be easily overridden with a random OOB write from a dodgy driver). So yes, we'd really like to have those please :)

Thanks,
Quentin

#11 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Hi Mark,

Quoted 11 lines · Mark Rutland

Hi,

Quoted 4 lines · Quentin Perret
Quoted 3 lines · Ard Biesheuvel

Android relies heavily on tracepoints for vendor hooks, and given the performance impact of CFI on indirect calls, there has been interest in enabling static calls to replace them.

Hhmm.... what exactly is a "vendor hook" in this context, and what is it doing with a tracepoint? From an upstream perspective that sounds somewhat fishy usage.

Right, 'vendor hooks' are an ugly Android-specific hack that I hope we will be able to get rid off overtime. And I don't think upstream should care about any of this TBH. But it's not the only use-case in Android for having modules attached to tracepoints, and the other is a bit more relevant to upstream. So I'd day it makes sense to have that discussion here.

Specifically, we've got a bunch of 'empty' tracepoints *upstream* in e.g. the scheduler that don't have any trace events associated with them (see the exported TPs at the top of kernel/sched/core.c for instance). They're exported with no in-kernel user on purpose. The only reason they exist is to allow people to attach modules to them, and do whatever they need from there (collect stats, write to the trace buffer, ...). That way the kernel doesn't commit to any userspace ABI, and the maintenance burden falls on whoever maintains the module instead.

But nowadays virtually every vendor/OEM in the Android world attaches to those TPs, in production, to gather stats and whatnot. And given that some of them are hooked in scheduler hot paths, we'd really like those to be low overhead. I wouldn't be surprised if other distros get the same issues at some point FWIW -- they all collect SCHED_DEBUG stats and such.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Quentin, anything to add here?

Yes, Android should definitely benefit from static calls.

Quoted 7 lines
Quoted 2 lines

Modules attaching to tracepoints cause a measurable overhead w/ CFI as the jump target is a bit harder to verify if it is not in-kernel.

Where does that additional overhead come from when the target is not in-kernel?

I hope that I am wrong in understanding that __cfi_slowpath_diag() means we're always doing an out-of-line check when calling into a module?

Nope, I think you're right.

Quoted 4 lines

If that were the case, that would seem to be a much more general problem with the current clang CFI scheme, and my fear here is that we're adding fragility and complexity in specific plces to work around general problems with the CFI scheme.

Right, no objection from me if we want to optimize the CFI slowpath instead if we can find a way to do that.

A few thoughts:

- attaching and detaching to TPs is a very infrequent operation, so having to do CFI checks (however cheap) before every call is a bit sad as the target doesn't change;

- so far the CFI overhead has been visible in practice mainly for tracepoints and not really anywhere else. The cost of a kernel-to-module indirect calls for e.g. driver operations seems to often (though not always) be somewhat small compared to the work done by the driver itself. And I think that module-to-kernel calls should be mostly unaffected as we will either resolve them with PC-relative instructions if within range, or via the module PLT which doesn't include CFI checks IIRC.

Thanks,
Quentin

#12 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

In reply to Ard Biesheuvel
Quoted 18 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.

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.

Quoted 3 lines

---

I'm starting to testing this out on top of 7.0-rc3...

Quoted 11 lines
 arch/arm64/Kconfig                   |  2 +
 arch/arm64/include/asm/static_call.h | 40 ++++++++++
 arch/arm64/kernel/patching.c         | 77 +++++++++++++++++++-
 arch/arm64/kernel/vmlinux.lds.S      |  1 +
 4 files changed, 117 insertions(+), 3 deletions(-)
arch/arm64/Kconfig +0 -0
index 176d6fddc4f2..ccc33b85769c 100644

arch/arm64/Kconfig config ARM64 · line 197

Quoted 5 lines
arch/arm64/Kconfig +2 -0
@@ -193,6 +193,8 @@ config ARM64
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
+	# https://github.com/ClangBuiltLinux/linux/issues/1354
+	select HAVE_STATIC_CALL if !LTO_CLANG_THIN || CLANG_VERSION >= 130000

I got a circular dependency error on this...

error: recursive dependency detected!
symbol GCOV_KERNEL depends on DEBUG_FS
symbol DEBUG_FS is selected by GPIO_VIRTUSER
symbol GPIO_VIRTUSER depends on GPIOLIB
symbol GPIOLIB is selected by CEC_GPIO
symbol CEC_GPIO depends on PREEMPTION
symbol PREEMPTION is selected by PREEMPT_BUILD
symbol PREEMPT_BUILD is selected by PREEMPT_DYNAMIC symbol PREEMPT_DYNAMIC depends on HAVE_PREEMPT_DYNAMIC symbol HAVE_PREEMPT_DYNAMIC is selected by HAVE_PREEMPT_DYNAMIC_CALL symbol HAVE_PREEMPT_DYNAMIC_CALL depends on HAVE_STATIC_CALL symbol HAVE_STATIC_CALL is selected by LTO_CLANG_THIN symbol LTO_CLANG_THIN is part of choice block at arch/Kconfig:817 symbol <choice> unknown is visible depending on LTO_CLANG_FULL symbol LTO_CLANG_FULL prompt is visible depending on HAS_LTO_CLANG symbol HAS_LTO_CLANG depends on GCOV_KERNEL

...so I just dropped the checks altogether for now.

Quoted 38 lines
arch/arm64/Kconfig +0 -0
 	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 +29 -0
new file mode 100644
index 000000000000..6ee918991510
@@ -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 (provided that 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.
+ */
+#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" \

arch/arm64/include/asm/static_call.h line 39

Quoted 10 lines
arch/arm64/include/asm/static_call.h new file +10 -0
+	    "	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")
+

Now that we have the RET0 implementation I added:

arch/arm64/include/asm/static_call.h +3 -0
@@ -37,4 +37,7 @@
 #define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)			\
 	__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret")
 
+#define ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name)			\
+	__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "mov x0, xzr")
+
 #endif /* _ASM_STATIC_CALL_H */
Quoted 34 lines
arch/arm64/include/asm/static_call.h new file +1 -0
+#endif /* _ASM_STATIC_CALL_H */
arch/arm64/kernel/patching.c +3 -2
index 771f543464e0..a265a87d4d9e 100644
@@ -3,6 +3,7 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/spinlock.h>
+#include <linux/static_call.h>
 #include <linux/stop_machine.h>
 #include <linux/uaccess.h>
 
@@ -66,7 +67,7 @@ int __kprobes aarch64_insn_read(void *addr, u32 *insnp)
 	return ret;
 }
 
-static int __kprobes __aarch64_insn_write(void *addr, __le32 insn)
+static int __kprobes __aarch64_insn_write(void *addr, void *insn, int size)
 {
 	void *waddr = addr;
 	unsigned long flags = 0;
@@ -75,7 +76,7 @@ static int __kprobes __aarch64_insn_write(void *addr, __le32 insn)
 	raw_spin_lock_irqsave(&patch_lock, flags);
 	waddr = patch_map(addr, FIX_TEXT_POKE0);
 
-	ret = copy_to_kernel_nofault(waddr, &insn, AARCH64_INSN_SIZE);
+	ret = copy_to_kernel_nofault(waddr, insn, size);
 
 	patch_unmap(FIX_TEXT_POKE0);
 	raw_spin_unlock_irqrestore(&patch_lock, flags);
@@ -85,7 +86,77 @@ static int __kprobes __aarch64_insn_write(void *addr, __le32 insn)
 
 int __kprobes aarch64_insn_write(void *addr, u32 insn)

arch/arm64/kernel/patching.c static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) · line 96

Quoted 10 lines
arch/arm64/kernel/patching.c +8 -1
 {
-	return __aarch64_insn_write(addr, cpu_to_le32(insn));
+	__le32 i = cpu_to_le32(insn);
+
+	return __aarch64_insn_write(addr, &i, AARCH64_INSN_SIZE);
+}
+
+static void *strip_cfi_jt(void *addr)
+{
+	if (IS_ENABLED(CONFIG_CFI_CLANG)) {

I believe this is now just "CONFIG_CFI".

Quoted 81 lines
arch/arm64/kernel/patching.c +63 -0
+		void *p = addr;
+		u32 insn;
+
+		/*
+		 * Taking the address of a function produces the address of the
+		 * jump table entry when Clang CFI is enabled. Such entries are
+		 * ordinary jump instructions, preceded by a BTI C instruction
+		 * if BTI is enabled for the kernel.
+		 */
+		if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
+			p += 4;
+
+		insn = le32_to_cpup(p);
+		if (aarch64_insn_is_b(insn))
+			return p + aarch64_get_branch_offset(insn);
+
+		WARN_ON(1);
+	}
+	return addr;
+}
+
+void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
+{
+	/*
+	 * -0x8	<literal>
+	 *  0x0	bti c		<--- trampoline entry point
+	 *  0x4	<branch or nop>
+	 *  0x8	ldr x16, <literal>
+	 *  0xc	cbz x16, 20
+	 * 0x10	br x16
+	 * 0x14	ret
+	 */
+	struct {
+		u64	literal;
+		__le32	insn[2];
+	} insns;
+	u32 insn;
+	int ret;
+
+	insn = aarch64_insn_gen_hint(AARCH64_INSN_HINT_BTIC);
+	insns.literal = (u64)func;
+	insns.insn[0] = cpu_to_le32(insn);
+
+	if (!func) {
+		insn = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_LR,
+						   AARCH64_INSN_BRANCH_RETURN);
+	} else {
+		insn = aarch64_insn_gen_branch_imm((u64)tramp + 4,
+						   (u64)strip_cfi_jt(func),
+						   AARCH64_INSN_BRANCH_NOLINK);
+
+		/*
+		 * Use a NOP if the branch target is out of range, and rely on
+		 * the indirect call instead.
+		 */
+		if (insn == AARCH64_BREAK_FAULT)
+			insn = aarch64_insn_gen_hint(AARCH64_INSN_HINT_NOP);
+	}
+	insns.insn[1] = cpu_to_le32(insn);
+
+	ret = __aarch64_insn_write(tramp - 8, &insns, sizeof(insns));
+	if (!WARN_ON(ret))
+		caches_clean_inval_pou((u64)tramp - 8, sizeof(insns));
 }
 
 int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
arch/arm64/kernel/vmlinux.lds.S +1 -0
index 50bab186c49b..e16860a14eaf 100644
@@ -173,6 +173,7 @@ SECTIONS
 			HIBERNATE_TEXT
 			KEXEC_TEXT
 			TRAMP_TEXT
+			STATIC_CALL_TEXT
 			*(.gnu.warning)
 		. = ALIGN(16);
 		*(.got)			/* Global offset table		*/
Signature · 1 line
2.30.2

A CFI crash was reported in [1]. Also, I was able to reproduce a CFI failure with just a simple "linux-perf" command. With this patchset I no longer see these issues. Awesome!

[1] https://lore.kernel.org/all/[email protected]/

--
Carlos Llamas

author reviewer
#13 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Hi Carlos,

Quoted 19 lines · Carlos Llamas
Quoted 18 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.

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.

Quoted 6 lines
Quoted 3 lines

---

I'm starting to testing this out on top of 7.0-rc3...

Please use the v3 I referred to on the thread. The code patching is a bit hairy, and so we should only consider that if there is a real use case for it.

Same goes for the special handling of the ret0 case - AFAIR, the v3 handles that transparently as it will just use the generic RET0 handler as the target.

...

Quoted 7 lines

A CFI crash was reported in [1]. Also, I was able to reproduce a CFI failure with just a simple "linux-perf" command. With this patchset I no longer see these issues. Awesome!

[1] https://lore.kernel.org/all/[email protected]/

Awesome!

#14 lore raw

Re: [PATCH v6 2/2] arm64: implement support for static call trampolines

Quoted 26 lines · Ard Biesheuvel

Hi Carlos,

Quoted 23 lines · Carlos Llamas
Quoted 21 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.

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.

---

Quoted 6 lines
Quoted 2 lines

I'm starting to testing this out on top of 7.0-rc3...

Please use the v3 I referred to on the thread. The code patching is a bit hairy, and so we should only consider that if there is a real use case for it.

Same goes for the special handling of the ret0 case - AFAIR, the v3 handles that transparently as it will just use the generic RET0 handler as the target.

IIUC, the trampoline tests R16 and returns early if unset. However, for the RET0 case the register would point to __static_call_return0 and then do the indirect call anyway. I suppose CFI is fine with this as like you mentioned, this is "hidden" from the compiler.

However, the R16 test seems unnecessary for the RET0 case, as it is always set to _something_ right? Or maybe I'm missing something?

Either way, now that I've rebased the patch I can send a... v7? With the required minor tweaks for RET0 and such.

--
Carlos Llamas