Skip to contentSkip to conversation

arm64/fpsimd: Implement strict mode for kernel mode SIMD

RFC PATCH In review Ard Biesheuvel 2 participants 2 messages Oct 3, 2025 arm64

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm64/fpsimd: Implement strict mode for kernel mode SIMD

From: Ard Biesheuvel <[email protected]>

The arch-specific code on arm64 that uses the FP/SIMD register file takes great care to use FP/SIMD enabled C code only from isolated compilation units. This is needed because that code is compiled with the -mgeneral-regs-only compiler flag omitted, and this permits the compiler to use FP/SIMD registers anywhere, including for things like spilling. This could result in unrelated FP/SIMD state getting corrupted, as that may only be manipulated from within a kernel_neon_begin/end block.

The generic kernel mode FPU API, as used by the amdgpu driver, also omits the -mgeneral-regs-only flag when building FP/SIMD code, but without any guardrails, potentially resulting in user data corruption if any of that code is reachable from outside of a kernel_fpu_begin/end pair.

On arm64, this issue is more severe than on other architectures, as access to the FP/SIMD registers is never disabled, and so such corruption could happen silently.

So implement a strict FP/SIMD mode that does disable access to the FP/SIMD registers, so that inadvertent accesses trap and cause an exception.

---
 arch/arm64/Kconfig               | 11 ++++++++
 arch/arm64/include/asm/fpsimd.h  | 29 +++++++++++++++++++
 arch/arm64/kernel/entry-common.c |  3 ++
 arch/arm64/kernel/fpsimd.c       | 48 +++++++++++++++++++++++---------
 4 files changed, 78 insertions(+), 13 deletions(-)
arch/arm64/Kconfig +11 -0
index 84c7a455d16c..53162e13c0d4 100644
@@ -384,6 +384,17 @@ config SMP
 config KERNEL_MODE_NEON
 	def_bool y
 
+config STRICT_KERNEL_FPSIMD
+	bool "Enable strict checking of kernel mode FP/SIMD register accesses"
+	default DRM_AMDGPU
+	help
+	  Disable access to the FP/SIMD register file when entering the kernel,
+	  and only enable it temporarily when using it for kernel mode FP/SIMD,
+	  or for context switching the contents. This ensures that inadvertent
+	  accesses from code that fails to use the kernel NEON begin/end API
+	  properly will trigger an exception, instead of silently corrupting
+	  user data.
+
 config FIX_EARLYCON_MEM
 	def_bool y
 
arch/arm64/include/asm/fpsimd.h +29 -0
index b8cf0ea43cc0..0681c081db79 100644
@@ -57,6 +57,35 @@ static inline void cpacr_restore(unsigned long cpacr)
 	isb();
 }
 
+static inline void cpacr_enable_fpsimd(void)
+{
+	sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_FPEN);
+	isb();
+}
+
+static inline void cpacr_disable_fpsimd(void)
+{
+	sysreg_clear_set(cpacr_el1, CPACR_EL1_FPEN, 0);
+	isb();
+}
+
+static inline bool fpsimd_kmode_strict(void)
+{
+	return IS_ENABLED(CONFIG_STRICT_KERNEL_FPSIMD);
+}
+
+static inline void fpsimd_kmode_enable_access(void)
+{
+	if (fpsimd_kmode_strict())
+		cpacr_enable_fpsimd();
+}
+
+static inline void fpsimd_kmode_disable_access(void)
+{
+	if (fpsimd_kmode_strict())
+		cpacr_disable_fpsimd();
+}
+
 /*
  * When we defined the maximum SVE vector length we defined the ABI so
  * that the maximum vector length included all the reserved for future
arch/arm64/kernel/entry-common.c +3 -0
index f546a914f041..3b83989c768c 100644
@@ -21,6 +21,7 @@
 #include <asm/daifflags.h>
 #include <asm/esr.h>
 #include <asm/exception.h>
+#include <asm/fpsimd.h>
 #include <asm/irq_regs.h>
 #include <asm/kprobes.h>
 #include <asm/mmu.h>
@@ -88,6 +89,7 @@ static __always_inline void __enter_from_user_mode(struct pt_regs *regs)
 
 static __always_inline void arm64_enter_from_user_mode(struct pt_regs *regs)
 {
+	fpsimd_kmode_disable_access();
 	__enter_from_user_mode(regs);
 }
 
@@ -104,6 +106,7 @@ static __always_inline void arm64_exit_to_user_mode(struct pt_regs *regs)
 	local_daif_mask();
 	mte_check_tfsr_exit();
 	exit_to_user_mode();
+	fpsimd_kmode_enable_access();
 }
 
 asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)
arch/arm64/kernel/fpsimd.c +35 -13

162 lines not on this page · raw message

Signature · 1 line
2.51.0.618.g983fd99d29-goog
#1 lore raw
Quoted 3 lines · Ard Biesheuvel

So implement a strict FP/SIMD mode that does disable access to the FP/SIMD registers, so that inadvertent accesses trap and cause an exception.

I like this idea. However, my first thought when I stated looking through the code was that it lines up with the idea I had to move the way we access to the FP state (kernel_neon_*(), disabling preemption to access userspace state in registers and flushing to memory) to use a get/put model. The idea was to make the patterns we use for taking ownership of the FP state and the lifetime of that ownership more consistent, and support validation like this. It's a bigger change than what you've done here but with the updates to manage the traps it'll be touching a lot of the same places and I think it'd be really useful from a maintainability point of view even if we don't actually end up doing anything with the traps.

I'll have a proper look at the change next week.