Skip to contentSkip to conversation

arm64/fpsimd: Avoid unnecessary per-CPU buffers for EFI runtime calls

PATCH Applied Ard Biesheuvel 3 participants 3 messages Mar 18, 2025 – Apr 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/fpsimd: Avoid unnecessary per-CPU buffers for EFI runtime calls

From: Ard Biesheuvel <[email protected]>

The EFI specification has some elaborate rules about which runtime services may be called while another runtime service call is already in progress. In Linux, however, for simplicity, all EFI runtime service invocations are serialized via the efi_runtime_lock semaphore.

This implies that calls to the helper pair arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() are serialized too, and are guaranteed not to nest. Furthermore, the arm64 arch code has its own spinlock to serialize use of the EFI runtime stack, of which only a single instance exists.

This all means that the FP/SIMD and SVE state preserve/restore logic in __efi_fpsimd_begin() and __efi_fpsimd_end() are also serialized, and only a single instance of the associated per-CPU variables can ever be in use at the same time. There is therefore no need at all for per-CPU variables here, and they can all be replaced with singleton instances. This saves a non-trivial amount of memory on systems with many CPUs.

To be more robust against potential future changes in the core EFI code that may invalidate the reasoning above, move the invocations of __efi_fpsimd_begin() and __efi_fpsimd_end() into the critical section covered by the efi_rt_lock spinlock.

---
 arch/arm64/kernel/efi.c    |  4 +--
 arch/arm64/kernel/fpsimd.c | 54 ++++++++++++++++++--------------------
 2 files changed, 27 insertions(+), 31 deletions(-)
arch/arm64/kernel/efi.c +2 -2
index 1d25d8899dbf..250e9d7c08a7 100644
@@ -169,14 +169,14 @@ static DEFINE_RAW_SPINLOCK(efi_rt_lock);
 void arch_efi_call_virt_setup(void)
 {
 	efi_virtmap_load();
-	__efi_fpsimd_begin();
 	raw_spin_lock(&efi_rt_lock);
+	__efi_fpsimd_begin();
 }
 
 void arch_efi_call_virt_teardown(void)
 {
-	raw_spin_unlock(&efi_rt_lock);
 	__efi_fpsimd_end();
+	raw_spin_unlock(&efi_rt_lock);
 	efi_virtmap_unload();
 }
 
arch/arm64/kernel/fpsimd.c +25 -29

132 lines not on this page · raw message

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The EFI specification has some elaborate rules about which runtime services may be called while another runtime service call is already in progress. In Linux, however, for simplicity, all EFI runtime service invocations are serialized via the efi_runtime_lock semaphore.

This implies that calls to the helper pair arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() are serialized too, and are guaranteed not to nest. Furthermore, the arm64 arch code has its own spinlock to serialize use of the EFI runtime stack, of which only a single instance exists.

Quoted 6 lines

This all means that the FP/SIMD and SVE state preserve/restore logic in __efi_fpsimd_begin() and __efi_fpsimd_end() are also serialized, and only a single instance of the associated per-CPU variables can ever be in use at the same time. There is therefore no need at all for per-CPU variables here, and they can all be replaced with singleton instances. This saves a non-trivial amount of memory on systems with many CPUs.

That makes life a lot easier all round, and like you say the memory savings are nice.

arch/arm64/kernel/fpsimd.c void __efi_fpsimd_begin(void) · line 2012

Quoted 3 lines
arch/arm64/kernel/fpsimd.c +1 -2
-		if (system_supports_sve() && likely(efi_sve_state)) {
-			char *sve_state = this_cpu_ptr(efi_sve_state);
+		if (system_supports_sve() && efi_sve_state != NULL) {

The change does remove a bunch of these likely() annotations, though the chances that they were ever having an impact seem minimal.

maintainer Applied
#2 lore raw
In reply to Ard Biesheuvel
Quoted 5 lines · Ard Biesheuvel

The EFI specification has some elaborate rules about which runtime services may be called while another runtime service call is already in progress. In Linux, however, for simplicity, all EFI runtime service invocations are serialized via the efi_runtime_lock semaphore.

Quoted 7 lines

This implies that calls to the helper pair arch_efi_call_virt_setup() and arch_efi_call_virt_teardown() are serialized too, and are guaranteed not to nest. Furthermore, the arm64 arch code has its own spinlock to serialize use of the EFI runtime stack, of which only a single instance exists.

[...]

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

[1/1] arm64/fpsimd: Avoid unnecessary per-CPU buffers for EFI runtime calls

      https://git.kernel.org/arm64/c/e04796c8b598

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev