Skip to contentSkip to conversation

arm64: BTI cleanups

PATCH Applied Mark Brown 4 participants 7 messages Dec 14, 2021

Subsystems (3)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

v4 cover arm64: BTI cleanups

This patch series brings together a few cleanups for the assembly portions of the in kernel BTI support.

v4:
- Generate BTIs unconditionally.
v3:
- Tweak the assembler macro for BTI as suggested by Ard, making it more compact.
v2:
- Pull in the assembler macro for BTI proposed by Mark Rutland.
- Unconditionally override the SYM_FUNC macros.

Mark Brown (3):
arm64: Add macro version of the BTI instruction
arm64: Unconditionally override SYM_FUNC macros
arm64: Use BTI C directly and unconditionally

 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 10 ++++++++++
 arch/arm64/include/asm/linkage.h   | 31 +++++++++++-------------------
 arch/arm64/kernel/entry-ftrace.S   |  8 ++------
 arch/arm64/lib/kasan_sw_tags.S     |  4 +---
 5 files changed, 29 insertions(+), 34 deletions(-)

base-commit: 0fcfb00b28c0b7884635dacf38e46d60bf3d4eb1

Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#1 lore raw

PATCH v4 1/3 arm64: Add macro version of the BTI instruction

BTI is only available from v8.5 so we need to encode it using HINT in generic code and for older toolchains. Add an assembler macro based on one written by Mark Rutland which lets us use the mnemonic and update the existing users.

---
 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 10 ++++++++++
 arch/arm64/include/asm/linkage.h   |  7 +------
 3 files changed, 16 insertions(+), 11 deletions(-)
arch/arm64/crypto/aes-modes.S +5 -5
index b495de22bb38..ff01f0167ba2 100644
@@ -363,15 +363,15 @@ ST5(	mov		v4.16b, vctr.16b		)
 	adr		x16, 1f
 	sub		x16, x16, x12, lsl #3
 	br		x16
-	hint		34			// bti c
+	bti		c
 	mov		v0.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v1.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v2.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v3.d[0], vctr.d[0]
-ST5(	hint		34				)
+ST5(	bti		c				)
 ST5(	mov		v4.d[0], vctr.d[0]		)
 1:	b		2f
 	.previous
arch/arm64/include/asm/assembler.h +10 -0
index 136d13f3d6e9..e8bd0af0141c 100644
@@ -790,6 +790,16 @@ alternative_endif
 .Lnoyield_\@:
 	.endm
 
+/*
+ * Branch Target Identifier (BTI)
+ */
+	.macro  bti, targets
+	.equ	.L__bti_targets_c, 34
+	.equ	.L__bti_targets_j, 36
+	.equ	.L__bti_targets_jc,38
+	hint	#.L__bti_targets_\targets
+	.endm
+
 /*
  * This macro emits a program property note section identifying
  * architecture features which require special handling, mainly for
arch/arm64/include/asm/linkage.h +1 -6
index 9906541a6861..c5d0c11d7709 100644
@@ -6,12 +6,7 @@
 
 #if defined(CONFIG_ARM64_BTI_KERNEL) && defined(__aarch64__)
 
-/*
- * Since current versions of gas reject the BTI instruction unless we
- * set the architecture version to v8.5 we use the hint instruction
- * instead.
- */
-#define BTI_C hint 34 ;
+#define BTI_C bti c ;
 
 /*
  * When using in-kernel BTI we need to ensure that PCS-conformant assembly
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#2 lore raw

PATCH v4 2/3 arm64: Unconditionally override SYM_FUNC macros

In reply to Mark Brown

Currently we only override the SYM_FUNC macros when we need to insert BTI C into them, do this unconditionally to make it more likely that we'll notice bugs in our override.

---
 arch/arm64/include/asm/linkage.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
arch/arm64/include/asm/linkage.h +11 -5
index c5d0c11d7709..1cfa8bb33edd 100644
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
+#else
+
+#define BTI_C
+
+#endif
+
 /*
- * When using in-kernel BTI we need to ensure that PCS-conformant assembly
- * functions have suitable annotations.  Override SYM_FUNC_START to insert
- * a BTI landing pad at the start of everything.
+ * When using in-kernel BTI we need to ensure that PCS-conformant
+ * assembly functions have suitable annotations.  Override
+ * SYM_FUNC_START to insert a BTI landing pad at the start of
+ * everything, the override is done unconditionally so we're more
+ * likely to notice any drift from the overridden definitions.
  */
 #define SYM_FUNC_START(name)				\
 	SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)	\
@@ -37,8 +45,6 @@
 	SYM_START(name, SYM_L_WEAK, SYM_A_NONE)		\
 	BTI_C
 
-#endif
-
 /*
  * Annotate a function as position independent, i.e., safe to be called before
  * the kernel virtual mapping is activated.
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#3 lore raw

PATCH v4 3/3 arm64: Use BTI C directly and unconditionally

In reply to Mark Brown

Now we have a macro for BTI C that looks like a regular instruction change all the users of the current BTI_C macro to just emit a BTI C directly and remove the macro.

This does mean that we now unconditionally BTI annotate all assembly functions, meaning that they are worse in this respect than code generated by the compiler. The overhead should be minimal for implementations with a reasonable HINT implementation.

---
 arch/arm64/include/asm/linkage.h | 22 ++++++----------------
 arch/arm64/kernel/entry-ftrace.S |  8 ++------
 arch/arm64/lib/kasan_sw_tags.S   |  4 +---
 3 files changed, 9 insertions(+), 25 deletions(-)
arch/arm64/include/asm/linkage.h +6 -16
index 1cfa8bb33edd..9065e4749b42 100644
@@ -4,16 +4,6 @@
 #define __ALIGN		.align 2
 #define __ALIGN_STR	".align 2"
 
-#if defined(CONFIG_ARM64_BTI_KERNEL) && defined(__aarch64__)
-
-#define BTI_C bti c ;
-
-#else
-
-#define BTI_C
-
-#endif
-
 /*
  * When using in-kernel BTI we need to ensure that PCS-conformant
  * assembly functions have suitable annotations.  Override
@@ -23,27 +13,27 @@
  */
 #define SYM_FUNC_START(name)				\
 	SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)	\
-	BTI_C
+	bti c ;
 
 #define SYM_FUNC_START_NOALIGN(name)			\
 	SYM_START(name, SYM_L_GLOBAL, SYM_A_NONE)	\
-	BTI_C
+	bti c ;
 
 #define SYM_FUNC_START_LOCAL(name)			\
 	SYM_START(name, SYM_L_LOCAL, SYM_A_ALIGN)	\
-	BTI_C
+	bti c ;
 
 #define SYM_FUNC_START_LOCAL_NOALIGN(name)		\
 	SYM_START(name, SYM_L_LOCAL, SYM_A_NONE)	\
-	BTI_C
+	bti c ;
 
 #define SYM_FUNC_START_WEAK(name)			\
 	SYM_START(name, SYM_L_WEAK, SYM_A_ALIGN)	\
-	BTI_C
+	bti c ;
 
 #define SYM_FUNC_START_WEAK_NOALIGN(name)		\
 	SYM_START(name, SYM_L_WEAK, SYM_A_NONE)		\
-	BTI_C
+	bti c ;
 
 /*
  * Annotate a function as position independent, i.e., safe to be called before
arch/arm64/kernel/entry-ftrace.S +2 -6
index 8cf970d219f5..e535480a4069 100644
@@ -77,17 +77,13 @@
 	.endm
 
 SYM_CODE_START(ftrace_regs_caller)
-#ifdef BTI_C
-	BTI_C
-#endif
+	bti	c
 	ftrace_regs_entry	1
 	b	ftrace_common
 SYM_CODE_END(ftrace_regs_caller)
 
 SYM_CODE_START(ftrace_caller)
-#ifdef BTI_C
-	BTI_C
-#endif
+	bti	c
 	ftrace_regs_entry	0
 	b	ftrace_common
 SYM_CODE_END(ftrace_caller)
arch/arm64/lib/kasan_sw_tags.S +1 -3
index 5b04464c045e..20784ce75def 100644
@@ -38,9 +38,7 @@
  * incremented by 256 prior to return).
  */
 SYM_CODE_START(__hwasan_tag_mismatch)
-#ifdef BTI_C
-	BTI_C
-#endif
+	bti	c
 	add	x29, sp, #232
 	stp	x2, x3, [sp, #8 * 2]
 	stp	x4, x5, [sp, #8 * 4]
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v4 3/3
#4 lore raw

Re: [PATCH v4 3/3] arm64: Use BTI C directly and unconditionally

Quoted 11 lines · Mark Brown

Now we have a macro for BTI C that looks like a regular instruction change all the users of the current BTI_C macro to just emit a BTI C directly and remove the macro.

This does mean that we now unconditionally BTI annotate all assembly functions, meaning that they are worse in this respect than code generated by the compiler. The overhead should be minimal for implementations with a reasonable HINT implementation.

Quoted 101 lines
---
 arch/arm64/include/asm/linkage.h | 22 ++++++----------------
 arch/arm64/kernel/entry-ftrace.S |  8 ++------
 arch/arm64/lib/kasan_sw_tags.S   |  4 +---
 3 files changed, 9 insertions(+), 25 deletions(-)
arch/arm64/include/asm/linkage.h +6 -16
index 1cfa8bb33edd..9065e4749b42 100644
@@ -4,16 +4,6 @@
 #define __ALIGN                .align 2
 #define __ALIGN_STR    ".align 2"
 
-#if defined(CONFIG_ARM64_BTI_KERNEL) && defined(__aarch64__)
-
-#define BTI_C bti c ;
-
-#else
-
-#define BTI_C
-
-#endif
-
 /*
  * When using in-kernel BTI we need to ensure that PCS-conformant
  * assembly functions have suitable annotations.  Override
@@ -23,27 +13,27 @@
  */
 #define SYM_FUNC_START(name)                           \
        SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)      \
-       BTI_C
+       bti c ;
 
 #define SYM_FUNC_START_NOALIGN(name)                   \
        SYM_START(name, SYM_L_GLOBAL, SYM_A_NONE)       \
-       BTI_C
+       bti c ;
 
 #define SYM_FUNC_START_LOCAL(name)                     \
        SYM_START(name, SYM_L_LOCAL, SYM_A_ALIGN)       \
-       BTI_C
+       bti c ;
 
 #define SYM_FUNC_START_LOCAL_NOALIGN(name)             \
        SYM_START(name, SYM_L_LOCAL, SYM_A_NONE)        \
-       BTI_C
+       bti c ;
 
 #define SYM_FUNC_START_WEAK(name)                      \
        SYM_START(name, SYM_L_WEAK, SYM_A_ALIGN)        \
-       BTI_C
+       bti c ;
 
 #define SYM_FUNC_START_WEAK_NOALIGN(name)              \
        SYM_START(name, SYM_L_WEAK, SYM_A_NONE)         \
-       BTI_C
+       bti c ;
 
 /*
  * Annotate a function as position independent, i.e., safe to be called before
arch/arm64/kernel/entry-ftrace.S +2 -6
index 8cf970d219f5..e535480a4069 100644
@@ -77,17 +77,13 @@
        .endm
 
 SYM_CODE_START(ftrace_regs_caller)
-#ifdef BTI_C
-       BTI_C
-#endif
+       bti     c
        ftrace_regs_entry       1
        b       ftrace_common
 SYM_CODE_END(ftrace_regs_caller)
 
 SYM_CODE_START(ftrace_caller)
-#ifdef BTI_C
-       BTI_C
-#endif
+       bti     c
        ftrace_regs_entry       0
        b       ftrace_common
 SYM_CODE_END(ftrace_caller)
arch/arm64/lib/kasan_sw_tags.S +1 -4
index 5b04464c045e..20784ce75def 100644
@@ -38,9 +38,7 @@
  * incremented by 256 prior to return).
  */
 SYM_CODE_START(__hwasan_tag_mismatch)
-#ifdef BTI_C
-       BTI_C
-#endif
+       bti     c
        add     x29, sp, #232
        stp     x2, x3, [sp, #8 * 2]
        stp     x4, x5, [sp, #8 * 4]
--
Quoted 2 lines

2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer Acked-by on the v4 series
#5 lore raw
In reply to Mark Brown
Quoted 12 lines · Mark Brown

This patch series brings together a few cleanups for the assembly portions of the in kernel BTI support.

v4:
- Generate BTIs unconditionally.
v3:
- Tweak the assembler macro for BTI as suggested by Ard, making it more compact.
v2:
- Pull in the assembler macro for BTI proposed by Mark Rutland.
- Unconditionally override the SYM_FUNC macros.

Quoted 4 lines

Mark Brown (3):
arm64: Add macro version of the BTI instruction
arm64: Unconditionally override SYM_FUNC macros
arm64: Use BTI C directly and unconditionally

For the series:

Mark.

Quoted 8 lines
 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 10 ++++++++++
 arch/arm64/include/asm/linkage.h   | 31 +++++++++++-------------------
 arch/arm64/kernel/entry-ftrace.S   |  8 ++------
 arch/arm64/lib/kasan_sw_tags.S     |  4 +---
 5 files changed, 29 insertions(+), 34 deletions(-)
Quoted 4 lines

base-commit: 0fcfb00b28c0b7884635dacf38e46d60bf3d4eb1

Signature · 1 line
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer Applied
#6 lore raw
In reply to Mark Brown
Quoted 12 lines · Mark Brown

This patch series brings together a few cleanups for the assembly portions of the in kernel BTI support.

v4:
- Generate BTIs unconditionally.
v3:
- Tweak the assembler macro for BTI as suggested by Ard, making it more compact.
v2:
- Pull in the assembler macro for BTI proposed by Mark Rutland.
- Unconditionally override the SYM_FUNC macros.

Quoted 1 line

[...]

Applied to arm64 (for-next/bti), thanks!

[1/3] arm64: Add macro version of the BTI instruction

      https://git.kernel.org/arm64/c/9be34be87cc8
[2/3] arm64: Unconditionally override SYM_FUNC macros
      https://git.kernel.org/arm64/c/481ee45ce9e0
[3/3] arm64: Use BTI C directly and unconditionally
      https://git.kernel.org/arm64/c/742a15b1a23a
Signature · 7 lines
Catalin


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel