1 From af386d2b1f9207290a12aa97ecec8b428f3bebb2 Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Thu, 23 Apr 2020 15:54:04 -0600
4 Subject: [PATCH 057/124] crypto: arch/lib - limit simd usage to 4k chunks
6 commit 706024a52c614b478b63f7728d202532ce6591a9 upstream.
8 The initial Zinc patchset, after some mailing list discussion, contained
9 code to ensure that kernel_fpu_enable would not be kept on for more than
10 a 4k chunk, since it disables preemption. The choice of 4k isn't totally
11 scientific, but it's not a bad guess either, and it's what's used in
12 both the x86 poly1305, blake2s, and nhpoly1305 code already (in the form
13 of PAGE_SIZE, which this commit corrects to be explicitly 4k for the
16 Ard did some back of the envelope calculations and found that
17 at 5 cycles/byte (overestimate) on a 1ghz processor (pretty slow), 4k
18 means we have a maximum preemption disabling of 20us, which Sebastian
19 confirmed was probably a good limit.
21 Unfortunately the chunking appears to have been left out of the final
22 patchset that added the glue code. So, this commit adds it back in.
24 Fixes: 84e03fa39fbe ("crypto: x86/chacha - expose SIMD ChaCha routine as library function")
25 Fixes: b3aad5bad26a ("crypto: arm64/chacha - expose arm64 ChaCha routine as library function")
26 Fixes: a44a3430d71b ("crypto: arm/chacha - expose ARM ChaCha routine as library function")
27 Fixes: d7d7b8535662 ("crypto: x86/poly1305 - wire up faster implementations for kernel")
28 Fixes: f569ca164751 ("crypto: arm64/poly1305 - incorporate OpenSSL/CRYPTOGAMS NEON implementation")
29 Fixes: a6b803b3ddc7 ("crypto: arm/poly1305 - incorporate OpenSSL/CRYPTOGAMS NEON implementation")
30 Fixes: ed0356eda153 ("crypto: blake2s - x86_64 SIMD implementation")
31 Cc: Eric Biggers <ebiggers@google.com>
32 Cc: Ard Biesheuvel <ardb@kernel.org>
33 Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
34 Cc: stable@vger.kernel.org
35 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
36 Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
37 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
38 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
40 arch/arm/crypto/chacha-glue.c | 14 +++++++++++---
41 arch/arm/crypto/poly1305-glue.c | 15 +++++++++++----
42 arch/arm64/crypto/chacha-neon-glue.c | 14 +++++++++++---
43 arch/arm64/crypto/poly1305-glue.c | 15 +++++++++++----
44 arch/x86/crypto/blake2s-glue.c | 10 ++++------
45 arch/x86/crypto/chacha_glue.c | 14 +++++++++++---
46 arch/x86/crypto/poly1305_glue.c | 13 ++++++-------
47 7 files changed, 65 insertions(+), 30 deletions(-)
49 --- a/arch/arm/crypto/chacha-glue.c
50 +++ b/arch/arm/crypto/chacha-glue.c
51 @@ -91,9 +91,17 @@ void chacha_crypt_arch(u32 *state, u8 *d
55 - kernel_neon_begin();
56 - chacha_doneon(state, dst, src, bytes, nrounds);
59 + unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
61 + kernel_neon_begin();
62 + chacha_doneon(state, dst, src, todo, nrounds);
70 EXPORT_SYMBOL(chacha_crypt_arch);
72 --- a/arch/arm/crypto/poly1305-glue.c
73 +++ b/arch/arm/crypto/poly1305-glue.c
74 @@ -160,13 +160,20 @@ void poly1305_update_arch(struct poly130
75 unsigned int len = round_down(nbytes, POLY1305_BLOCK_SIZE);
77 if (static_branch_likely(&have_neon) && do_neon) {
78 - kernel_neon_begin();
79 - poly1305_blocks_neon(&dctx->h, src, len, 1);
82 + unsigned int todo = min_t(unsigned int, len, SZ_4K);
84 + kernel_neon_begin();
85 + poly1305_blocks_neon(&dctx->h, src, todo, 1);
92 poly1305_blocks_arm(&dctx->h, src, len, 1);
96 nbytes %= POLY1305_BLOCK_SIZE;
99 --- a/arch/arm64/crypto/chacha-neon-glue.c
100 +++ b/arch/arm64/crypto/chacha-neon-glue.c
101 @@ -87,9 +87,17 @@ void chacha_crypt_arch(u32 *state, u8 *d
102 !crypto_simd_usable())
103 return chacha_crypt_generic(state, dst, src, bytes, nrounds);
105 - kernel_neon_begin();
106 - chacha_doneon(state, dst, src, bytes, nrounds);
109 + unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
111 + kernel_neon_begin();
112 + chacha_doneon(state, dst, src, todo, nrounds);
120 EXPORT_SYMBOL(chacha_crypt_arch);
122 --- a/arch/arm64/crypto/poly1305-glue.c
123 +++ b/arch/arm64/crypto/poly1305-glue.c
124 @@ -143,13 +143,20 @@ void poly1305_update_arch(struct poly130
125 unsigned int len = round_down(nbytes, POLY1305_BLOCK_SIZE);
127 if (static_branch_likely(&have_neon) && crypto_simd_usable()) {
128 - kernel_neon_begin();
129 - poly1305_blocks_neon(&dctx->h, src, len, 1);
132 + unsigned int todo = min_t(unsigned int, len, SZ_4K);
134 + kernel_neon_begin();
135 + poly1305_blocks_neon(&dctx->h, src, todo, 1);
142 poly1305_blocks(&dctx->h, src, len, 1);
146 nbytes %= POLY1305_BLOCK_SIZE;
149 --- a/arch/x86/crypto/blake2s-glue.c
150 +++ b/arch/x86/crypto/blake2s-glue.c
151 @@ -32,16 +32,16 @@ void blake2s_compress_arch(struct blake2
154 /* SIMD disables preemption, so relax after processing each page. */
155 - BUILD_BUG_ON(PAGE_SIZE / BLAKE2S_BLOCK_SIZE < 8);
156 + BUILD_BUG_ON(SZ_4K / BLAKE2S_BLOCK_SIZE < 8);
158 if (!static_branch_likely(&blake2s_use_ssse3) || !crypto_simd_usable()) {
159 blake2s_compress_generic(state, block, nblocks, inc);
165 const size_t blocks = min_t(size_t, nblocks,
166 - PAGE_SIZE / BLAKE2S_BLOCK_SIZE);
167 + SZ_4K / BLAKE2S_BLOCK_SIZE);
170 if (IS_ENABLED(CONFIG_AS_AVX512) &&
171 @@ -52,10 +52,8 @@ void blake2s_compress_arch(struct blake2
177 block += blocks * BLAKE2S_BLOCK_SIZE;
181 EXPORT_SYMBOL(blake2s_compress_arch);
183 --- a/arch/x86/crypto/chacha_glue.c
184 +++ b/arch/x86/crypto/chacha_glue.c
185 @@ -154,9 +154,17 @@ void chacha_crypt_arch(u32 *state, u8 *d
186 bytes <= CHACHA_BLOCK_SIZE)
187 return chacha_crypt_generic(state, dst, src, bytes, nrounds);
189 - kernel_fpu_begin();
190 - chacha_dosimd(state, dst, src, bytes, nrounds);
193 + unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
195 + kernel_fpu_begin();
196 + chacha_dosimd(state, dst, src, todo, nrounds);
204 EXPORT_SYMBOL(chacha_crypt_arch);
206 --- a/arch/x86/crypto/poly1305_glue.c
207 +++ b/arch/x86/crypto/poly1305_glue.c
208 @@ -91,8 +91,8 @@ static void poly1305_simd_blocks(void *c
209 struct poly1305_arch_internal *state = ctx;
211 /* SIMD disables preemption, so relax after processing each page. */
212 - BUILD_BUG_ON(PAGE_SIZE < POLY1305_BLOCK_SIZE ||
213 - PAGE_SIZE % POLY1305_BLOCK_SIZE);
214 + BUILD_BUG_ON(SZ_4K < POLY1305_BLOCK_SIZE ||
215 + SZ_4K % POLY1305_BLOCK_SIZE);
217 if (!IS_ENABLED(CONFIG_AS_AVX) || !static_branch_likely(&poly1305_use_avx) ||
218 (len < (POLY1305_BLOCK_SIZE * 18) && !state->is_base2_26) ||
219 @@ -102,8 +102,8 @@ static void poly1305_simd_blocks(void *c
224 - const size_t bytes = min_t(size_t, len, PAGE_SIZE);
226 + const size_t bytes = min_t(size_t, len, SZ_4K);
229 if (IS_ENABLED(CONFIG_AS_AVX512) && static_branch_likely(&poly1305_use_avx512))
230 @@ -113,11 +113,10 @@ static void poly1305_simd_blocks(void *c
232 poly1305_blocks_avx(ctx, inp, bytes, padbit);
243 static void poly1305_simd_emit(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],