Skip to contentSkip to conversation

arm64: fpsimd: improve stacking logic in non-interruptible context

PATCH Superseded Ard Biesheuvel 1 participant 1 message Dec 14, 2016 arm64

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 arm64: fpsimd: improve stacking logic in non-interruptible context

Currently, we allow kernel mode NEON in softirq or hardirq context by stacking and unstacking a slice of the NEON register file for each call to kernel_neon_begin() and kernel_neon_end(), respectively.

Given that
a) a CPU typically spends most of its time in userland, during which time no kernel mode NEON in process context is in progress,
b) a CPU spends most of its time in the kernel doing other things than kernel mode NEON when it gets interrupted to perform kernel mode NEON in softirq context

the stacking and subsequent unstacking is only necessary if we are interrupting a thread while it is performing kernel mode NEON in process context, which means that in all other cases, we can simply preserve the userland FP/SIMD state once, and only restore it upon return to userland, even if we are being invoked from softirq or hardirq context.

However, with support being added to teh arm64 kernel for Scalable Vector Extensions (SVE), which shares the bottom 128 bits of each FP/SIMD register, but could scale up to 2048 bits per register, the nested stacking and unstacking that occurs in interrupt context is no longer sufficient, given that the register contents will be truncated to 128 bits upon restore, unless we add support for stacking/unstacking the entire SVE state, which does not sound that appealing.

This means that the FP/SIMD save state operation that encounters the userland state first *has* to be able to run to completion (since any interruption could truncate the contents of the registers, which would result in corrupted state to be restored once the interrupted context is allowed to resume preserving the state)

Since executing all code involving the FP/SIMD state with interrupts disabled is undesirable, let's ban kernel mode NEON in hardirq context altogether. This is a small price to pay, given that the primary use case of kernel mode NEON, crypto, can deal with this quite easily (and simply falls back to generic scalar algorithms whose worse performance should not matter in hardirq context anyway)

With hardirq context removed from the equation, we can modify the FP/SIMD state manipulation code to execute with softirqs disable. This allows the critical sections to complete without the risk of having the register contents getting corrupted half way through.

---
v7:
- ban kernel mode NEON in hardirq context, and execute all FP/SIMD state manipulations with softirqs disabled

v6:
- use a spinlock instead of disabling interrupts

v5:
- perform the test-and-set and the fpsimd_save_state with interrupts disabled, to prevent nested kernel_neon_begin()/_end() pairs to clobber the state while it is being preserved

v4:
- use this_cpu_inc/dec, which give sufficient guarantees regarding concurrency, but do not imply SMP barriers, which are not needed here

v3:
- avoid corruption by concurrent invocations of kernel_neon_begin()/_end()

v2:
- BUG() on unexpected values of the nesting level
- relax the BUG() on num_regs>32 to a WARN, given that nothing actually breaks in that case

 arch/arm64/include/asm/Kbuild |  1 -
 arch/arm64/include/asm/simd.h | 16 ++++
 arch/arm64/kernel/fpsimd.c    | 77 ++++++++++++++------
 3 files changed, 72 insertions(+), 22 deletions(-)
arch/arm64/include/asm/Kbuild +0 -1
index 44e1d7f10add..39ca0409e157 100644
@@ -33,7 +33,6 @@ generic-y += segment.h
 generic-y += sembuf.h
 generic-y += serial.h
 generic-y += shmbuf.h
-generic-y += simd.h
 generic-y += sizes.h
 generic-y += socket.h
 generic-y += sockios.h
arch/arm64/include/asm/simd.h new file +16 -0
new file mode 100644
index 000000000000..142eca29125a
@@ -0,0 +1,16 @@
+
+#include <linux/hardirq.h>
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * On arm64, we allow kernel mode NEON in softirq context but not in hardirq
+ * context, due to the fact that the NEON register file may be shared with SVE,
+ * whose state may too large to preserve/restore efficiently at each invocation
+ * of kernel_neon_begin()/_end() in hardirq context.
+ */
+static __must_check inline bool may_use_simd(void)
+{
+	return !in_irq();
+}
arch/arm64/kernel/fpsimd.c +56 -21

147 lines not on this page · raw message

Signature · 1 line
2.7.4