Skip to contentSkip to conversation

ARM: kernel mode NEON in softirq context

RFC PATCH New Ard Biesheuvel 2 participants 7 messages Jan 9, 2017 – Jan 11, 2017

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

cover ARM: kernel mode NEON in softirq context

Patch #1 in this series adds support for using the NEON in kernel mode while executing in softirq context. By allowing this, subsystems that perform non-trivial crypto in softirq context (such as CCMP in the mac80211 layer) can use algorithms such as the AES-CCM driver in patch #2, which is 13x faster than the generic CCM driver.

Ard Biesheuvel (2):
ARM: vfp - allow kernel mode NEON in softirq context
crypto: arm/aes - add CCM driver using ARMv8 Crypto Extensions

 arch/arm/crypto/Kconfig           |   8 +
 arch/arm/crypto/Makefile          |   2 +
 arch/arm/crypto/aes-ce-ccm-core.S | 234 +++++++++++++
 arch/arm/crypto/aes-ce-ccm-glue.c | 360 ++++++++++++++++++++
 arch/arm/vfp/vfpmodule.c          |  22 +-
 5 files changed, 620 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm/crypto/aes-ce-ccm-core.S
 create mode 100644 arch/arm/crypto/aes-ce-ccm-glue.c
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH 1/2 ARM: vfp - allow kernel mode NEON in softirq context

This updates the kernel mode NEON handling to allow the NEON to be used in softirq context as well as process context. This involves disabling softirq processing when the NEON is used in kernel mode in process context, and dealing with the situation where 'current' is not the owner of the userland context that is present in the NEON register file when the NEON is enabled in kernel mode.

The rationale for this change is that the NEON is shared with the ARMv8 Crypto Extensions (which are also defined for the AArch32 execution state), which can give a huge performance boost (15x) to use cases like mac80211 CCMP processing, which executes in softirq context.

---
 arch/arm/vfp/vfpmodule.c | 22 ++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
arch/arm/vfp/vfpmodule.c +16 -6
index 569d5a650a4a..814752811537 100644
@@ -690,26 +690,33 @@ void kernel_neon_begin(void)
 	u32 fpexc;
 
 	/*
-	 * Kernel mode NEON is only allowed outside of interrupt context
+	 * Kernel mode NEON is only allowed outside of hardirq context
 	 * with preemption disabled. This will make sure that the kernel
 	 * mode NEON register contents never need to be preserved.
 	 */
-	BUG_ON(in_interrupt());
+	BUG_ON(in_irq());
 	cpu = get_cpu();
 
+	/*
+	 * Disable softirq processing while the NEON is used by the kernel in
+	 * process context. This ensures that only a single kernel mode NEON
+	 * state is live at any given time.
+	 */
+	if (!in_serving_softirq())
+		local_bh_disable();
+
 	fpexc = fmrx(FPEXC) | FPEXC_EN;
 	fmxr(FPEXC, fpexc);
 
 	/*
-	 * Save the userland NEON/VFP state. Under UP,
-	 * the owner could be a task other than 'current'
+	 * Save the userland NEON/VFP state. Under UP, or when executing in
+	 * softirq context, the owner could be a task other than 'current'
 	 */
 	if (vfp_state_in_hw(cpu, thread))
 		vfp_save_state(&thread->vfpstate, fpexc);
-#ifndef CONFIG_SMP
 	else if (vfp_current_hw_state[cpu] != NULL)
 		vfp_save_state(vfp_current_hw_state[cpu], fpexc);
-#endif
+
 	vfp_current_hw_state[cpu] = NULL;
 }
 EXPORT_SYMBOL(kernel_neon_begin);
@@ -718,7 +725,10 @@ void kernel_neon_end(void)
 {
 	/* Disable the NEON/VFP unit. */
 	fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
+	if (!in_serving_softirq())
+		local_bh_enable();
 	put_cpu();
+
 }
 EXPORT_SYMBOL(kernel_neon_end);
 
Signature · 1 line
2.7.4
#2 lore raw

RFC PATCH 1/2 ARM: vfp - allow kernel mode NEON in softirq context

Quoted 48 lines · Ard Biesheuvel

This updates the kernel mode NEON handling to allow the NEON to be used in softirq context as well as process context. This involves disabling softirq processing when the NEON is used in kernel mode in process context, and dealing with the situation where 'current' is not the owner of the userland context that is present in the NEON register file when the NEON is enabled in kernel mode.

The rationale for this change is that the NEON is shared with the ARMv8 Crypto Extensions (which are also defined for the AArch32 execution state), which can give a huge performance boost (15x) to use cases like mac80211 CCMP processing, which executes in softirq context.

---
 arch/arm/vfp/vfpmodule.c | 22 ++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
arch/arm/vfp/vfpmodule.c +10 -4
index 569d5a650a4a..814752811537 100644
@@ -690,26 +690,33 @@ void kernel_neon_begin(void)
        u32 fpexc;
 
        /*
-        * Kernel mode NEON is only allowed outside of interrupt context
+        * Kernel mode NEON is only allowed outside of hardirq context
         * with preemption disabled. This will make sure that the kernel
         * mode NEON register contents never need to be preserved.
         */
-       BUG_ON(in_interrupt());
+       BUG_ON(in_irq());
        cpu = get_cpu();
 
+       /*
+        * Disable softirq processing while the NEON is used by the kernel in
+        * process context. This ensures that only a single kernel mode NEON
+        * state is live at any given time.
+        */
+       if (!in_serving_softirq())
+               local_bh_disable();
+
        fpexc = fmrx(FPEXC) | FPEXC_EN;
        fmxr(FPEXC, fpexc);
 
        /*
-        * Save the userland NEON/VFP state. Under UP,
-        * the owner could be a task other than 'current'

arch/arm/vfp/vfpmodule.c void kernel_neon_begin(void) · line 719

Quoted 10 lines
arch/arm/vfp/vfpmodule.c +3 -2
+        * Save the userland NEON/VFP state. Under UP, or when executing in
+        * softirq context, the owner could be a task other than 'current'
         */
        if (vfp_state_in_hw(cpu, thread))
                vfp_save_state(&thread->vfpstate, fpexc);
-#ifndef CONFIG_SMP
        else if (vfp_current_hw_state[cpu] != NULL)
                vfp_save_state(vfp_current_hw_state[cpu], fpexc);
-#endif
+

Actually, I think this should not be necessary (and the change to the comment is incorrect). Whether we're in process or softirq context makes no difference here, and the comment is slightly confusing: under SMP, the owner could also be a task other than 'current', but due to the eager preserve, the latest userland NEON state will already have been recorded, and there is no need doing it again.

Quoted 17 lines
arch/arm/vfp/vfpmodule.c +3 -1
        vfp_current_hw_state[cpu] = NULL;
 }
 EXPORT_SYMBOL(kernel_neon_begin);
@@ -718,7 +725,10 @@ void kernel_neon_end(void)
 {
        /* Disable the NEON/VFP unit. */
        fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
+       if (!in_serving_softirq())
+               local_bh_enable();
        put_cpu();
+
 }
 EXPORT_SYMBOL(kernel_neon_end);
 
--

2.7.4

#3 lore raw

RFC PATCH 1/2 ARM: vfp - allow kernel mode NEON in softirq context

In reply to Ard Biesheuvel
Quoted 6 lines · Ard Biesheuvel

This updates the kernel mode NEON handling to allow the NEON to be used in softirq context as well as process context. This involves disabling softirq processing when the NEON is used in kernel mode in process context, and dealing with the situation where 'current' is not the owner of the userland context that is present in the NEON register file when the NEON is enabled in kernel mode.

I really don't like this idea as-is.

We have cases where kernel code accesses VFP to (eg) save or restore register state, such as during signal handling. We assume that this will not be interrupted by another user, and that if we enable access to the VFP, it will stay enabled. If it gets disabled beneath us, then things won't go well.

For example, consider vfp_sync_hwstate():

vfp_sync_hwstate()
vfp_state_in_hw() => true
fpexc read
softirq happens
kernel_neon_begin()
kernel_neon_end()
fpexc re-enabled
current register state saved out (corrupting what was there) fpexc restored, possible in an enabled state

Or we could have:

vfp_sync_hwstate()
vfp_state_in_hw() => true
softirq happens
kernel_neon_begin()
kernel_neon_end()
fpexc read
fpexc re-enabled
current register state saved out (corrupting what was there) fpexc disabled

Or worse:

vfp_sync_hwstate()
vfp_state_in_hw() => true
fpexc read
fpexc re-enabled
softirq happens
kernel_neon_begin()
kernel_neon_end()
current register state saved out, blowing up because VFP is
unexpectedly disabled

So we would need to disable softirqs around every sensitive point in the VFP support code, and over all VFP instruction emulations for those VFPs which bounce "difficult" operations to the kernel support code.

Quoted 4 lines

The rationale for this change is that the NEON is shared with the ARMv8 Crypto Extensions (which are also defined for the AArch32 execution state), which can give a huge performance boost (15x) to use cases like mac80211 CCMP processing, which executes in softirq context.

I think, once the implementation is more correct, this would need to be re-evaluated, and I'd also like other more general performance measurements as well (eg, latency.)

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#4 lore raw

RFC PATCH 1/2 ARM: vfp - allow kernel mode NEON in softirq context

Quoted 52 lines · Russell King - ARM Linux
Quoted 6 lines · Ard Biesheuvel

This updates the kernel mode NEON handling to allow the NEON to be used in softirq context as well as process context. This involves disabling softirq processing when the NEON is used in kernel mode in process context, and dealing with the situation where 'current' is not the owner of the userland context that is present in the NEON register file when the NEON is enabled in kernel mode.

I really don't like this idea as-is.

We have cases where kernel code accesses VFP to (eg) save or restore register state, such as during signal handling. We assume that this will not be interrupted by another user, and that if we enable access to the VFP, it will stay enabled. If it gets disabled beneath us, then things won't go well.

For example, consider vfp_sync_hwstate():

vfp_sync_hwstate()
vfp_state_in_hw() => true
fpexc read
softirq happens
kernel_neon_begin()
kernel_neon_end()
fpexc re-enabled
current register state saved out (corrupting what was there) fpexc restored, possible in an enabled state

Or we could have:

vfp_sync_hwstate()
vfp_state_in_hw() => true
softirq happens
kernel_neon_begin()
kernel_neon_end()
fpexc read
fpexc re-enabled
current register state saved out (corrupting what was there) fpexc disabled

Or worse:

vfp_sync_hwstate()
vfp_state_in_hw() => true
fpexc read
fpexc re-enabled
softirq happens
kernel_neon_begin()
kernel_neon_end()
current register state saved out, blowing up because VFP is
unexpectedly disabled

Quoted 4 lines

So we would need to disable softirqs around every sensitive point in the VFP support code, and over all VFP instruction emulations for those VFPs which bounce "difficult" operations to the kernel support code.

Ah yes, I should have known it couldn't be that simple.

Thanks for the critique: i will look into the impact of making these changes.

Quoted 9 lines
Quoted 4 lines

The rationale for this change is that the NEON is shared with the ARMv8 Crypto Extensions (which are also defined for the AArch32 execution state), which can give a huge performance boost (15x) to use cases like mac80211 CCMP processing, which executes in softirq context.

I think, once the implementation is more correct, this would need to be re-evaluated, and I'd also like other more general performance measurements as well (eg, latency.)

Re latency, I thought about adding a kernel_neon_yield(), which does a kernel_neon_end()/do_softirq()/kernel_neon_begin() sequence if any softirqs are pending, to be invoked by kernel mode NEON users at times when there are no live NEON registers. But in-kernel users of the crypto API are naturally quantised into disk sectors, pages or network packets, so I would not expect any noticeable starvation to occur. But that does mean such algorithms should not be exposed to userland (which sounds like a bad idea in any case, given that userland can simply execute the same instructions)

#5 lore raw

RFC PATCH 1/2 ARM: vfp - allow kernel mode NEON in softirq context

Quoted 9 lines · Ard Biesheuvel

Re latency, I thought about adding a kernel_neon_yield(), which does a kernel_neon_end()/do_softirq()/kernel_neon_begin() sequence if any softirqs are pending, to be invoked by kernel mode NEON users at times when there are no live NEON registers. But in-kernel users of the crypto API are naturally quantised into disk sectors, pages or network packets, so I would not expect any noticeable starvation to occur. But that does mean such algorithms should not be exposed to userland (which sounds like a bad idea in any case, given that userland can simply execute the same instructions)

I was actually thinking about the impact of adding softirq disabling over much of the VFP code necessary to make this safe, rather than the softirq disable coming from the kernel mode neon itself.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#6 lore raw

RFC PATCH 2/2 crypto: arm/aes - add CCM driver using ARMv8 Crypto Extensions

In reply to Ard Biesheuvel

This is a straight port of the arm64 driver that implements AES in CCM mode using the ARMv8 Crypto Extensions instructions. It is ~13x faster than the generic CCM code using scalar AES.

---
 arch/arm/crypto/Kconfig           |   8 +
 arch/arm/crypto/Makefile          |   2 +
 arch/arm/crypto/aes-ce-ccm-core.S | 234 +++++++++++++
 arch/arm/crypto/aes-ce-ccm-glue.c | 360 ++++++++++++++++++++
 4 files changed, 604 insertions(+)
arch/arm/crypto/Kconfig +8 -0
index f1de658c3c8f..f933cae8d76b 100644
@@ -92,6 +92,14 @@ config CRYPTO_AES_ARM_CE
 	  Use an implementation of AES in CBC, CTR and XTS modes that uses
 	  ARMv8 Crypto Extensions
 
+config CRYPTO_AES_ARM_CE_CCM
+	tristate "AES in CCM mode using ARMv8 Crypto Extensions"
+	depends on KERNEL_MODE_NEON && m
+	select CRYPTO_ALGAPI
+	select CRYPTO_AES
+	select CRYPTO_AEAD
+	select CRYPTO_CCM
+
 config CRYPTO_GHASH_ARM_CE
 	tristate "PMULL-accelerated GHASH using ARMv8 Crypto Extensions"
 	depends on KERNEL_MODE_NEON
arch/arm/crypto/Makefile +2 -0
index 6eda6ffafea9..1b4f3a5f918c 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_CRYPTO_SHA512_ARM) += sha512-arm.o
 obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o
 
 ce-obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
+ce-obj-$(CONFIG_CRYPTO_AES_ARM_CE_CCM) += aes-ce-ccm.o
 ce-obj-$(CONFIG_CRYPTO_SHA1_ARM_CE) += sha1-arm-ce.o
 ce-obj-$(CONFIG_CRYPTO_SHA2_ARM_CE) += sha2-arm-ce.o
 ce-obj-$(CONFIG_CRYPTO_GHASH_ARM_CE) += ghash-arm-ce.o
@@ -38,6 +39,7 @@ sha512-arm-y	:= sha512-core.o sha512-glue.o $(sha512-arm-neon-y)
 sha1-arm-ce-y	:= sha1-ce-core.o sha1-ce-glue.o
 sha2-arm-ce-y	:= sha2-ce-core.o sha2-ce-glue.o
 aes-arm-ce-y	:= aes-ce-core.o aes-ce-glue.o
+aes-ce-ccm-y	:= aes-ce-ccm-core.o aes-ce-ccm-glue.o
 ghash-arm-ce-y	:= ghash-ce-core.o ghash-ce-glue.o
 crct10dif-arm-ce-y	:= crct10dif-ce-core.o crct10dif-ce-glue.o
 crc32-arm-ce-y:= crc32-ce-core.o crc32-ce-glue.o
arch/arm/crypto/aes-ce-ccm-core.S new file +234 -0

234 lines not on this page · raw message

arch/arm/crypto/aes-ce-ccm-glue.c new file +360 -0

360 lines not on this page · raw message

Signature · 1 line
2.7.4