Skip to contentSkip to conversation

arm64: mte: switch GCR_EL1 on task switch rather than entry/exit

PATCH Superseded Peter Collingbourne 2 participants 4 messages Jul 2, 2021 – Jul 5, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mte: switch GCR_EL1 on task switch rather than entry/exit

Accessing GCR_EL1 and issuing an ISB can be expensive on some microarchitectures. To avoid taking this performance hit on every kernel entry/exit, switch GCR_EL1 on task switch rather than entry/exit. This is essentially a revert of commit bad1e1c663e0 ("arm64: mte: switch GCR_EL1 in kernel entry and exit").

This requires changing how we generate random tags for HW tag-based KASAN, since at this point IRG would use the user's exclusion mask, which may not be suitable for kernel use. In this patch I chose to take the modulus of CNTVCT_EL0, however alternative approaches are possible.

---
 arch/arm64/include/asm/mte-kasan.h | 15 ++++--
 arch/arm64/include/asm/mte.h       |  6 ---
 arch/arm64/kernel/entry.S          | 41 ----------------
 arch/arm64/kernel/mte.c            | 76 ++++++++++++------------------
 4 files changed, 40 insertions(+), 98 deletions(-)
arch/arm64/include/asm/mte-kasan.h +10 -5
index ddd4d17cf9a0..e9b3c1bdbba3 100644
@@ -13,6 +13,8 @@
 
 #ifdef CONFIG_ARM64_MTE
 
+extern u64 mte_tag_mod;
+
 /*
  * These functions are meant to be only used from KASAN runtime through
  * the arch_*() interface defined in asm/memory.h.
@@ -37,15 +39,18 @@ static inline u8 mte_get_mem_tag(void *addr)
 	return mte_get_ptr_tag(addr);
 }
 
-/* Generate a random tag. */
+/*
+ * Generate a random tag. We can't use IRG because the user's GCR_EL1 is still
+ * installed for performance reasons. Instead, take the modulus of the
+ * architected timer which should be random enough for our purposes.
+ */
 static inline u8 mte_get_random_tag(void)
 {
-	void *addr;
+	u64 cntvct;
 
-	asm(__MTE_PREAMBLE "irg %0, %0"
-		: "=r" (addr));
+	asm("mrs %0, cntvct_el0" : "=r"(cntvct));
 
-	return mte_get_ptr_tag(addr);
+	return 0xF0 | (cntvct % mte_tag_mod);
 }
 
 /*
arch/arm64/include/asm/mte.h +0 -6
index 719687412798..412b94efcb11 100644
@@ -16,8 +16,6 @@
 
 #include <asm/pgtable-types.h>
 
-extern u64 gcr_kernel_excl;
-
 void mte_clear_page_tags(void *addr);
 unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
 				      unsigned long n);
@@ -40,7 +38,6 @@ void mte_free_tag_storage(char *storage);
 void mte_sync_tags(pte_t *ptep, pte_t pte);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
-void mte_update_sctlr_user(struct task_struct *task);
 void mte_thread_switch(struct task_struct *next);
 void mte_suspend_enter(void);
 void mte_suspend_exit(void);
@@ -63,9 +60,6 @@ static inline void mte_copy_page_tags(void *kto, const void *kfrom)
 static inline void mte_thread_init_user(void)
 {
 }
-static inline void mte_update_sctlr_user(struct task_struct *task)
-{
-}
 static inline void mte_thread_switch(struct task_struct *next)
 {
 }
arch/arm64/kernel/entry.S +0 -41

59 lines not on this page · raw message

arch/arm64/kernel/mte.c +30 -46

130 lines not on this page · raw message

Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#1 lore raw
Quoted 4 lines · Peter Collingbourne

Accessing GCR_EL1 and issuing an ISB can be expensive on some microarchitectures. To avoid taking this performance hit on every kernel entry/exit, switch GCR_EL1 on task switch rather than entry/exit.

Is it the ISB that's causing issues or the MRS/MSR as well? I think we can avoid the ISB when PtrAuth is enabled by shuffling the entry code a bit. We can also simplify the mte_set_gcr macro to avoid an MRS.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#2 lore raw
Quoted 10 lines · Catalin Marinas
Quoted 4 lines · Peter Collingbourne

Accessing GCR_EL1 and issuing an ISB can be expensive on some microarchitectures. To avoid taking this performance hit on every kernel entry/exit, switch GCR_EL1 on task switch rather than entry/exit.

Is it the ISB that's causing issues or the MRS/MSR as well? I think we can avoid the ISB when PtrAuth is enabled by shuffling the entry code a bit. We can also simplify the mte_set_gcr macro to avoid an MRS.

This was the first thing that I tried on our hardware:

arch/arm64/kernel/entry.S +1 -2
index 7312eafec946..8699ab28a924 100644
@@ -204,7 +204,6 @@ alternative_else_nop_endif
        ldr_l   \tmp, gcr_kernel_excl
 
        mte_set_gcr \tmp, \tmp2
-       isb
 1:
 #endif
        .endm
@@ -277,13 +276,13 @@ alternative_if ARM64_HAS_ADDRESS_AUTH
        orr     x0, x0, SCTLR_ELx_ENIA
        msr     sctlr_el1, x0
 2:
-       isb
 alternative_else_nop_endif
 #endif
 
        apply_ssbd 1, x22, x23
 
        mte_set_kernel_gcr x22, x23
+       isb
 
        scs_load tsk, x20
        .else

However, on most of the cores this led to only around half of the performance improvement of the patch that I sent. Which is somewhat surprising, but it is what it is.

But I would like to get IRG out of the kernel (at least in production kernels) for other reasons. I would at some point like to add a deterministic IRG mode (to support record/replay debugging). This will require setting RRND=0 and a per-task RGSR. If we then allow IRG in the kernel we would need to manually switch RGSR here as well.

Peter

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

maintainer
#3 lore raw
Quoted 41 lines · Peter Collingbourne
Quoted 9 lines · Catalin Marinas
Quoted 4 lines · Peter Collingbourne

Accessing GCR_EL1 and issuing an ISB can be expensive on some microarchitectures. To avoid taking this performance hit on every kernel entry/exit, switch GCR_EL1 on task switch rather than entry/exit.

Is it the ISB that's causing issues or the MRS/MSR as well? I think we can avoid the ISB when PtrAuth is enabled by shuffling the entry code a bit. We can also simplify the mte_set_gcr macro to avoid an MRS.

This was the first thing that I tried on our hardware:

arch/arm64/kernel/entry.S +1 -2
index 7312eafec946..8699ab28a924 100644
@@ -204,7 +204,6 @@ alternative_else_nop_endif
        ldr_l   \tmp, gcr_kernel_excl
 
        mte_set_gcr \tmp, \tmp2
-       isb
 1:
 #endif
        .endm
@@ -277,13 +276,13 @@ alternative_if ARM64_HAS_ADDRESS_AUTH
        orr     x0, x0, SCTLR_ELx_ENIA
        msr     sctlr_el1, x0
 2:
-       isb
 alternative_else_nop_endif
 #endif
 
        apply_ssbd 1, x22, x23
 
        mte_set_kernel_gcr x22, x23
+       isb
 
        scs_load tsk, x20
        .else
Quoted 3 lines

However, on most of the cores this led to only around half of the performance improvement of the patch that I sent. Which is somewhat surprising, but it is what it is.

BTW, can you also modify mte_set_kernel_gcr to only do a write to the GCR_EL1 register rather than a read-modify-write?

Signature · 6 lines
Catalin

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