Skip to contentSkip to conversation

arm64: BTI cleanups

PATCH Superseded Mark Brown 3 participants 12 messages Dec 8, 2021 – Dec 14, 2021

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

v3 cover arm64: BTI cleanups

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

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 (2):
arm64: Add macro version of the BTI instruction
arm64: Unconditionally override SYM_FUNC macros

 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 10 ++++++++++
 arch/arm64/include/asm/linkage.h   | 23 ++++++++++++-----------
 arch/arm64/kernel/entry-ftrace.S   |  4 ----
 arch/arm64/lib/kasan_sw_tags.S     |  2 --
 5 files changed, 27 insertions(+), 22 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 v3 1/2 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 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
maintainer Acked-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] arm64: Add macro version of the BTI instruction

Quoted 5 lines · Mark Brown

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 written by Mark Rutland which lets us use the mnemonic and update the existing users.

Quoted 7 lines
---
 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(-)

Will

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

author
#3 lore raw

PATCH v3 2/2 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. Since we now unconditionally provide the BTI_C macro used to do that let's always override them, that way any issues with our overrides it'll show up more consistently.

Since this means that we now have an unconditional definition of BTI_C update the other users to remove the ifdefs around usage, making them look a bit neater.

---
 arch/arm64/include/asm/linkage.h | 16 +++++++++++-----
 arch/arm64/kernel/entry-ftrace.S |  4 ----
 arch/arm64/lib/kasan_sw_tags.S   |  2 --
 3 files changed, 11 insertions(+), 11 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.
arch/arm64/kernel/entry-ftrace.S +0 -4
index 8cf970d219f5..46a2de864794 100644
@@ -77,17 +77,13 @@
 	.endm
 
 SYM_CODE_START(ftrace_regs_caller)
-#ifdef BTI_C
 	BTI_C
-#endif
 	ftrace_regs_entry	1
 	b	ftrace_common
 SYM_CODE_END(ftrace_regs_caller)
 
 SYM_CODE_START(ftrace_caller)
-#ifdef BTI_C
 	BTI_C
-#endif
 	ftrace_regs_entry	0
 	b	ftrace_common
 SYM_CODE_END(ftrace_caller)
arch/arm64/lib/kasan_sw_tags.S +0 -2
index 5b04464c045e..a6d6fa2f761e 100644
@@ -38,9 +38,7 @@
  * incremented by 256 prior to return).
  */
 SYM_CODE_START(__hwasan_tag_mismatch)
-#ifdef BTI_C
 	BTI_C
-#endif
 	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
maintainer
#4 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 22 lines · Mark Brown

Currently we only override the SYM_FUNC macros when we need to insert BTI C into them. Since we now unconditionally provide the BTI_C macro used to do that let's always override them, that way any issues with our overrides it'll show up more consistently.

Since this means that we now have an unconditional definition of BTI_C update the other users to remove the ifdefs around usage, making them look a bit neater.

---
 arch/arm64/include/asm/linkage.h | 16 +++++++++++-----
 arch/arm64/kernel/entry-ftrace.S |  4 ----
 arch/arm64/lib/kasan_sw_tags.S   |  2 --
 3 files changed, 11 insertions(+), 11 deletions(-)
arch/arm64/include/asm/linkage.h +0 -0
index c5d0c11d7709..1cfa8bb33edd 100644

arch/arm64/include/asm/linkage.h line 15

Quoted 8 lines
arch/arm64/include/asm/linkage.h +5 -0
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
+#else
+
+#define BTI_C
+
+#endif

Why do we need this hunk? Having the hint instruction should be fine, no?

Will

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

author
#5 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 8 lines · Will Deacon
Quoted 6 lines · Mark Brown
arch/arm64/include/asm/linkage.h +0 -0
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
Quoted 7 lines

arch/arm64/include/asm/linkage.h line 15

Quoted 5 lines
arch/arm64/include/asm/linkage.h +5 -0
+#else
+
+#define BTI_C
+
+#endif

Why do we need this hunk? Having the hint instruction should be fine, no?

We could unconditionally insert the hint but that would be a step back from what we currently have and would mean that hand written assembly would be that little bit worse than what the compiler outputs. If this had been causing us issues I could see simplifying but I'm not aware of any - the issues I'm aware of have been due to not adding the landing pads in SYM_CODE, not SYM_FUNC.

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

maintainer
#6 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 17 lines · Mark Brown
Quoted 8 lines · Will Deacon
Quoted 6 lines · Mark Brown
arch/arm64/include/asm/linkage.h +0 -0
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
Quoted 7 lines

arch/arm64/include/asm/linkage.h line 15

Quoted 5 lines
arch/arm64/include/asm/linkage.h +5 -0
+#else
+
+#define BTI_C
+
+#endif

Why do we need this hunk? Having the hint instruction should be fine, no?

Quoted 6 lines

We could unconditionally insert the hint but that would be a step back from what we currently have and would mean that hand written assembly would be that little bit worse than what the compiler outputs. If this had been causing us issues I could see simplifying but I'm not aware of any - the issues I'm aware of have been due to not adding the landing pads in SYM_CODE, not SYM_FUNC.

I don't have a strong opinion here, so whatever you like. I just tend to think that most people will have BTI enabled and there's something to be said for having an instruction always emitted when you have a token in assembly code.

Will

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

#7 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 26 lines · Will Deacon
Quoted 17 lines · Mark Brown
Quoted 8 lines · Will Deacon
Quoted 6 lines · Mark Brown
arch/arm64/include/asm/linkage.h +0 -0
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
Quoted 7 lines

arch/arm64/include/asm/linkage.h line 15

Quoted 5 lines
arch/arm64/include/asm/linkage.h +5 -0
+#else
+
+#define BTI_C
+
+#endif

Why do we need this hunk? Having the hint instruction should be fine, no?

Quoted 6 lines

We could unconditionally insert the hint but that would be a step back from what we currently have and would mean that hand written assembly would be that little bit worse than what the compiler outputs. If this had been causing us issues I could see simplifying but I'm not aware of any - the issues I'm aware of have been due to not adding the landing pads in SYM_CODE, not SYM_FUNC.

Quoted 5 lines

I don't have a strong opinion here, so whatever you like. I just tend to think that most people will have BTI enabled and there's something to be said for having an instruction always emitted when you have a token in assembly code.

+1

I already pointed out computed gotos, which are admittedly rare, but there are other reasons why adhering to WYSIWYG is strongly preferred for asm code IMO.

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

author
#8 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 7 lines · Ard Biesheuvel
Quoted 4 lines · Will Deacon

I don't have a strong opinion here, so whatever you like. I just tend to think that most people will have BTI enabled and there's something to be said for having an instruction always emitted when you have a token in assembly code.

Quoted 5 lines

+1

I already pointed out computed gotos, which are admittedly rare, but there are other reasons why adhering to WYSIWYG is strongly preferred for asm code IMO.

The use case for this macro is for SYM_FUNC_START() which is going to be emitting the BTI without it really appearing directly in the code so I'm not sure how much I buy that TBH. I think you're pushing back on uses of the macro outside of linkage.h more than on what linkage.h is doing, that does make more sense to me.

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

#9 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 15 lines · Mark Brown
Quoted 7 lines · Ard Biesheuvel
Quoted 4 lines · Will Deacon

I don't have a strong opinion here, so whatever you like. I just tend to think that most people will have BTI enabled and there's something to be said for having an instruction always emitted when you have a token in assembly code.

Quoted 5 lines

+1

I already pointed out computed gotos, which are admittedly rare, but there are other reasons why adhering to WYSIWYG is strongly preferred for asm code IMO.

Quoted 5 lines

The use case for this macro is for SYM_FUNC_START() which is going to be emitting the BTI without it really appearing directly in the code so I'm not sure how much I buy that TBH. I think you're pushing back on uses of the macro outside of linkage.h more than on what linkage.h is doing, that does make more sense to me.

Fair point, but we don't really have any control over where else BTI_C may end up being used, no?

In any case, I don't think it's worth it blowing up the validation matrix over this, so if we can emit the same code for all configurations, we should IMO

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

author
#10 lore raw

Re: [PATCH v3 2/2] arm64: Unconditionally override SYM_FUNC macros

Quoted 6 lines · Ard Biesheuvel
Quoted 3 lines · Mark Brown
Quoted 3 lines

I already pointed out computed gotos, which are admittedly rare, but there are other reasons why adhering to WYSIWYG is strongly preferred for asm code IMO.

Quoted 8 lines
Quoted 5 lines

The use case for this macro is for SYM_FUNC_START() which is going to be emitting the BTI without it really appearing directly in the code so I'm not sure how much I buy that TBH. I think you're pushing back on uses of the macro outside of linkage.h more than on what linkage.h is doing, that does make more sense to me.

Fair point, but we don't really have any control over where else BTI_C may end up being used, no?

It's also a macro, which should put people off, and there's precious little arm64 asm outside of arch/arm64 so I'm not sure how worried I am about that.

Quoted 3 lines

In any case, I don't think it's worth it blowing up the validation matrix over this, so if we can emit the same code for all configurations, we should IMO

We could have been doing that since the BTI kernel support was originally merged... TBH I'm just really surprised everyone's pushing to generate worse code here, feels not the direction people usually go in.

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

Acked-by on the v3 series
#11 lore raw
In reply to Mark Brown
Quoted 11 lines · Mark Brown

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

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 (2):
arm64: Add macro version of the BTI instruction
arm64: Unconditionally override SYM_FUNC macros

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

base-commit: 0fcfb00b28c0b7884635dacf38e46d60bf3d4eb1 --
2.30.2

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