crypto: arm/chacha-neon - optimize for non-block size multiples
Subsystems (2)
PATCH 1/1 crypto: arm/chacha-neon - optimize for non-block size multiples
The current NEON based ChaCha implementation for ARM is optimized for multiples of 4x the ChaCha block size (64 bytes). This makes sense for block encryption, but given that ChaCha is also often used in the context of networking, it makes sense to consider arbitrary length inputs as well.
For example, WireGuard typically uses 1420 byte packets, and performing ChaCha encryption involves 5 invocations of chacha_4block_xor_neon() and 3 invocations of chacha_block_xor_neon(), where the last one also involves a memcpy() using a buffer on the stack to process the final chunk of 1420 % 64 == 12 bytes.
Let's optimize for this case as well, by letting chacha_4block_xor_neon() deal with any input size between 64 and 256 bytes, using NEON permutation instructions and overlapping loads and stores. This way, the 140 byte tail of a 1420 byte input buffer can simply be processed in one go.
On out-of-order microarchitectures such as Cortex-A57, this results in a speedup for 1420 byte blocks of about 21%, without any signficant performance impact of the power-of-2 block sizes. On lower end cores such as Cortex-A53, the speedup for 1420 byte blocks is only about 2%, but also without impacting other input sizes.
--- arch/arm/crypto/chacha-glue.c | 23 ++-- arch/arm/crypto/chacha-neon-core.S | 124 +++++++++++++++++--- 2 files changed, 116 insertions(+), 31 deletions(-)
arch/arm/crypto/chacha-glue.c +9 -14
@@ -23,7 +23,7 @@ asmlinkage void chacha_block_xor_neon(const u32 *state, u8 *dst, const u8 *src,int nrounds); asmlinkage void chacha_4block_xor_neon(const u32 *state, u8 *dst, const u8 *src, -int nrounds); +int nrounds,unsigned int nbytes); asmlinkage void hchacha_block_arm(const u32 *state, u32 *out, int nrounds); asmlinkage void hchacha_block_neon(const u32 *state, u32 *out, int nrounds);
@@ -42,19 +42,14 @@ static void chacha_doneon(u32 *state, u8 *dst, const u8 *src, {u8 buf[CHACHA_BLOCK_SIZE ]; -while (bytes >=CHACHA_BLOCK_SIZE *4 ) { -chacha_4block_xor_neon (state, dst, src, nrounds); - bytes -=CHACHA_BLOCK_SIZE *4 ; - src +=CHACHA_BLOCK_SIZE *4 ; - dst +=CHACHA_BLOCK_SIZE *4 ; - state[12 ] +=4 ; - } -while (bytes >=CHACHA_BLOCK_SIZE ) { -chacha_block_xor_neon (state, dst, src, nrounds); - bytes -=CHACHA_BLOCK_SIZE ; - src +=CHACHA_BLOCK_SIZE ; - dst +=CHACHA_BLOCK_SIZE ; - state[12 ]++; +while (bytes >CHACHA_BLOCK_SIZE ) { +unsigned int l =min (bytes,CHACHA_BLOCK_SIZE *4U ); + +chacha_4block_xor_neon (state, dst, src, nrounds, l); + bytes -= l; + src += l; + dst += l; + state[12 ] +=DIV_ROUND_UP (l,CHACHA_BLOCK_SIZE ); }if (bytes) {memcpy (buf, src, bytes);
arch/arm/crypto/chacha-neon-core.S +107 -17
175 lines not on this page · raw message
Signature · 1 line
2.17.1
Cool patch! I look forward to getting out the old arm32 rig and benching this. One question:
Quoted 5 lines · Ard Biesheuvel
On out-of-order microarchitectures such as Cortex-A57, this results in a speedup for 1420 byte blocks of about 21%, without any signficant performance impact of the power-of-2 block sizes. On lower end cores such as Cortex-A53, the speedup for 1420 byte blocks is only about 2%, but also without impacting other input sizes.
A57 and A53 are 64-bit, but this is code for 32-bit arm, right? So the comparison is more like A15 vs A5? Or are you running 32-bit kernels on armv8 hardware?
Quoted 11 lines · Jason A. Donenfeld
Cool patch! I look forward to getting out the old arm32 rig and benching this. One question:
Quoted 5 lines · Ard Biesheuvel
On out-of-order microarchitectures such as Cortex-A57, this results in a speedup for 1420 byte blocks of about 21%, without any signficant performance impact of the power-of-2 block sizes. On lower end cores such as Cortex-A53, the speedup for 1420 byte blocks is only about 2%, but also without impacting other input sizes.
Quoted 3 lines
A57 and A53 are 64-bit, but this is code for 32-bit arm, right? So the comparison is more like A15 vs A5? Or are you running 32-bit kernels on armv8 hardware?
The latter. The only 32-bit hardware I have in my drawer is Cortex-A8, which I expect to benefit from this change, but the way its micro-architecture integrates the NEON stages into the pipeline is a bit odd, and therefore, you cannot really extrapolate from those results for other cores.
Cortex-A57 and Cortex-A15 should be fairly similar, so that is really the target for this optimization. Cortex-A5 and A7 already omit the NEON code path entirely, so they are not affected in the first place. Cortex-A53 is significant because this is what the Raspberry Pi3 uses (and it ships with a 32-bit kernel)