[PATCH v4 0/3] arm64: BTI cleanups

STALE1692d

Revision v4 of 3 in this series.

7 messages, 4 authors, 2021-12-14 · open the first message on its own page

[PATCH v4 0/3] arm64: BTI cleanups

From: Mark Brown <broonie@kernel.org>
Date: 2021-12-14 15:29:09

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
-- 
2.30.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

From: Mark Brown <broonie@kernel.org>
Date: 2021-12-14 15:29:30

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.

Suggested-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Will Deacon <will@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 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(-)
diff --git a/arch/arm64/crypto/aes-modes.S b/arch/arm64/crypto/aes-modes.S
index b495de22bb38..ff01f0167ba2 100644
--- a/arch/arm64/crypto/aes-modes.S
+++ b/arch/arm64/crypto/aes-modes.S
@@ -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
diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index 136d13f3d6e9..e8bd0af0141c 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -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
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index 9906541a6861..c5d0c11d7709 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -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
-- 
2.30.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

[PATCH v4 2/3] arm64: Unconditionally override SYM_FUNC macros

From: Mark Brown <broonie@kernel.org>
Date: 2021-12-14 15:29:34

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.

Suggested-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/include/asm/linkage.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index c5d0c11d7709..1cfa8bb33edd 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -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.
-- 
2.30.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

From: Mark Brown <broonie@kernel.org>
Date: 2021-12-14 15:29:47

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.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 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(-)
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index 1cfa8bb33edd..9065e4749b42 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -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
diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S
index 8cf970d219f5..e535480a4069 100644
--- a/arch/arm64/kernel/entry-ftrace.S
+++ b/arch/arm64/kernel/entry-ftrace.S
@@ -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)
diff --git a/arch/arm64/lib/kasan_sw_tags.S b/arch/arm64/lib/kasan_sw_tags.S
index 5b04464c045e..20784ce75def 100644
--- a/arch/arm64/lib/kasan_sw_tags.S
+++ b/arch/arm64/lib/kasan_sw_tags.S
@@ -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]
-- 
2.30.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

From: Ard Biesheuvel <ardb@kernel.org>
Date: 2021-12-14 15:37:36

On Tue, 14 Dec 2021 at 16:28, Mark Brown [off-list ref] wrote:
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.

Signed-off-by: Mark Brown <broonie@kernel.org>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
quoted hunk
---
 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(-)
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index 1cfa8bb33edd..9065e4749b42 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -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
diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S
index 8cf970d219f5..e535480a4069 100644
--- a/arch/arm64/kernel/entry-ftrace.S
+++ b/arch/arm64/kernel/entry-ftrace.S
@@ -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)
diff --git a/arch/arm64/lib/kasan_sw_tags.S b/arch/arm64/lib/kasan_sw_tags.S
index 5b04464c045e..20784ce75def 100644
--- a/arch/arm64/lib/kasan_sw_tags.S
+++ b/arch/arm64/lib/kasan_sw_tags.S
@@ -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]
--
2.30.2
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Re: [PATCH v4 0/3] arm64: BTI cleanups

From: Mark Rutland <mark.rutland@arm.com>
Date: 2021-12-14 16:01:26

On Tue, Dec 14, 2021 at 03:27:11PM +0000, Mark Brown wrote:
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
For the series:

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.
 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
-- 
2.30.2
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Re: [PATCH v4 0/3] arm64: BTI cleanups

From: Catalin Marinas <catalin.marinas@arm.com>
Date: 2021-12-14 19:17:40

On Tue, 14 Dec 2021 15:27:11 +0000, Mark Brown wrote:
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.

[...]
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

-- 
Catalin


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help