Skip to content

Commit bd2ad88

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: arm64/aes-ce-ccm - move kernel mode neon en/disable into loop
When kernel mode NEON was first introduced on arm64, the preserve and restore of the userland NEON state was completely unoptimized, and involved saving all registers on each call to kernel_neon_begin(), and restoring them on each call to kernel_neon_end(). For this reason, the NEON crypto code that was introduced at the time keeps the NEON enabled throughout the execution of the crypto API methods, which may include calls back into the crypto API that could result in memory allocation or other actions that we should avoid when running with preemption disabled. Since then, we have optimized the kernel mode NEON handling, which now restores lazily (upon return to userland), and so the preserve action is only costly the first time it is called after entering the kernel. So let's put the kernel_neon_begin() and kernel_neon_end() calls around the actual invocations of the NEON crypto code, and run the remainder of the code with kernel mode NEON disabled (and preemption enabled) Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 702202f commit bd2ad88

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

arch/arm64/crypto/aes-ce-ccm-glue.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ static int ccm_init_mac(struct aead_request *req, u8 maciv[], u32 msglen)
107107
}
108108

109109
static void ccm_update_mac(struct crypto_aes_ctx *key, u8 mac[], u8 const in[],
110-
u32 abytes, u32 *macp, bool use_neon)
110+
u32 abytes, u32 *macp)
111111
{
112-
if (likely(use_neon)) {
112+
if (may_use_simd()) {
113+
kernel_neon_begin();
113114
ce_aes_ccm_auth_data(mac, in, abytes, macp, key->key_enc,
114115
num_rounds(key));
116+
kernel_neon_end();
115117
} else {
116118
if (*macp > 0 && *macp < AES_BLOCK_SIZE) {
117119
int added = min(abytes, AES_BLOCK_SIZE - *macp);
@@ -143,8 +145,7 @@ static void ccm_update_mac(struct crypto_aes_ctx *key, u8 mac[], u8 const in[],
143145
}
144146
}
145147

146-
static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[],
147-
bool use_neon)
148+
static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[])
148149
{
149150
struct crypto_aead *aead = crypto_aead_reqtfm(req);
150151
struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
@@ -163,7 +164,7 @@ static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[],
163164
ltag.len = 6;
164165
}
165166

166-
ccm_update_mac(ctx, mac, (u8 *)&ltag, ltag.len, &macp, use_neon);
167+
ccm_update_mac(ctx, mac, (u8 *)&ltag, ltag.len, &macp);
167168
scatterwalk_start(&walk, req->src);
168169

169170
do {
@@ -175,7 +176,7 @@ static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[],
175176
n = scatterwalk_clamp(&walk, len);
176177
}
177178
p = scatterwalk_map(&walk);
178-
ccm_update_mac(ctx, mac, p, n, &macp, use_neon);
179+
ccm_update_mac(ctx, mac, p, n, &macp);
179180
len -= n;
180181

181182
scatterwalk_unmap(p);
@@ -242,43 +243,42 @@ static int ccm_encrypt(struct aead_request *req)
242243
u8 __aligned(8) mac[AES_BLOCK_SIZE];
243244
u8 buf[AES_BLOCK_SIZE];
244245
u32 len = req->cryptlen;
245-
bool use_neon = may_use_simd();
246246
int err;
247247

248248
err = ccm_init_mac(req, mac, len);
249249
if (err)
250250
return err;
251251

252-
if (likely(use_neon))
253-
kernel_neon_begin();
254-
255252
if (req->assoclen)
256-
ccm_calculate_auth_mac(req, mac, use_neon);
253+
ccm_calculate_auth_mac(req, mac);
257254

258255
/* preserve the original iv for the final round */
259256
memcpy(buf, req->iv, AES_BLOCK_SIZE);
260257

261258
err = skcipher_walk_aead_encrypt(&walk, req, true);
262259

263-
if (likely(use_neon)) {
260+
if (may_use_simd()) {
264261
while (walk.nbytes) {
265262
u32 tail = walk.nbytes % AES_BLOCK_SIZE;
266263

267264
if (walk.nbytes == walk.total)
268265
tail = 0;
269266

267+
kernel_neon_begin();
270268
ce_aes_ccm_encrypt(walk.dst.virt.addr,
271269
walk.src.virt.addr,
272270
walk.nbytes - tail, ctx->key_enc,
273271
num_rounds(ctx), mac, walk.iv);
272+
kernel_neon_end();
274273

275274
err = skcipher_walk_done(&walk, tail);
276275
}
277-
if (!err)
276+
if (!err) {
277+
kernel_neon_begin();
278278
ce_aes_ccm_final(mac, buf, ctx->key_enc,
279279
num_rounds(ctx));
280-
281-
kernel_neon_end();
280+
kernel_neon_end();
281+
}
282282
} else {
283283
err = ccm_crypt_fallback(&walk, mac, buf, ctx, true);
284284
}
@@ -301,43 +301,42 @@ static int ccm_decrypt(struct aead_request *req)
301301
u8 __aligned(8) mac[AES_BLOCK_SIZE];
302302
u8 buf[AES_BLOCK_SIZE];
303303
u32 len = req->cryptlen - authsize;
304-
bool use_neon = may_use_simd();
305304
int err;
306305

307306
err = ccm_init_mac(req, mac, len);
308307
if (err)
309308
return err;
310309

311-
if (likely(use_neon))
312-
kernel_neon_begin();
313-
314310
if (req->assoclen)
315-
ccm_calculate_auth_mac(req, mac, use_neon);
311+
ccm_calculate_auth_mac(req, mac);
316312

317313
/* preserve the original iv for the final round */
318314
memcpy(buf, req->iv, AES_BLOCK_SIZE);
319315

320316
err = skcipher_walk_aead_decrypt(&walk, req, true);
321317

322-
if (likely(use_neon)) {
318+
if (may_use_simd()) {
323319
while (walk.nbytes) {
324320
u32 tail = walk.nbytes % AES_BLOCK_SIZE;
325321

326322
if (walk.nbytes == walk.total)
327323
tail = 0;
328324

325+
kernel_neon_begin();
329326
ce_aes_ccm_decrypt(walk.dst.virt.addr,
330327
walk.src.virt.addr,
331328
walk.nbytes - tail, ctx->key_enc,
332329
num_rounds(ctx), mac, walk.iv);
330+
kernel_neon_end();
333331

334332
err = skcipher_walk_done(&walk, tail);
335333
}
336-
if (!err)
334+
if (!err) {
335+
kernel_neon_begin();
337336
ce_aes_ccm_final(mac, buf, ctx->key_enc,
338337
num_rounds(ctx));
339-
340-
kernel_neon_end();
338+
kernel_neon_end();
339+
}
341340
} else {
342341
err = ccm_crypt_fallback(&walk, mac, buf, ctx, false);
343342
}

0 commit comments

Comments
 (0)