Skip to content

Commit f248945

Browse files
ebiggersherbertx
authored andcommitted
crypto: skcipher - clean up initialization of skcipher_walk::flags
- Initialize SKCIPHER_WALK_SLEEP in a consistent way, and check for atomic=true at the same time as CRYPTO_TFM_REQ_MAY_SLEEP. Technically atomic=true only needs to apply after the first step, but it is very rarely used. We should optimize for the common case. So, check 'atomic' alongside CRYPTO_TFM_REQ_MAY_SLEEP. This is more efficient. - Initialize flags other than SKCIPHER_WALK_SLEEP to 0 rather than preserving them. No caller actually initializes the flags, which makes it impossible to use their original values for anything. Indeed, that does not happen and all meaningful flags get overridden anyway. It may have been thought that just clearing one flag would be faster than clearing all flags, but that's not the case as the former is a read-write operation whereas the latter is just a write. - Move the explicit clearing of SKCIPHER_WALK_SLOW, SKCIPHER_WALK_COPY, and SKCIPHER_WALK_DIFF into skcipher_walk_done(), since it is now only needed on non-first steps. Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent d97d066 commit f248945

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

crypto/skcipher.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ int skcipher_walk_done(struct skcipher_walk *walk, int res)
148148
if (total) {
149149
crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
150150
CRYPTO_TFM_REQ_MAY_SLEEP : 0);
151+
walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
152+
SKCIPHER_WALK_DIFF);
151153
return skcipher_walk_next(walk);
152154
}
153155

@@ -235,9 +237,6 @@ static int skcipher_walk_next(struct skcipher_walk *walk)
235237
unsigned int bsize;
236238
unsigned int n;
237239

238-
walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
239-
SKCIPHER_WALK_DIFF);
240-
241240
n = walk->total;
242241
bsize = min(walk->stride, max(n, walk->blocksize));
243242
n = scatterwalk_clamp(&walk->in, n);
@@ -311,25 +310,24 @@ int skcipher_walk_virt(struct skcipher_walk *walk,
311310
{
312311
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
313312
struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
314-
int err = 0;
315313

316314
might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
317315

318316
walk->total = req->cryptlen;
319317
walk->nbytes = 0;
320318
walk->iv = req->iv;
321319
walk->oiv = req->iv;
320+
if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic)
321+
walk->flags = SKCIPHER_WALK_SLEEP;
322+
else
323+
walk->flags = 0;
322324

323325
if (unlikely(!walk->total))
324-
goto out;
326+
return 0;
325327

326328
scatterwalk_start(&walk->in, req->src);
327329
scatterwalk_start(&walk->out, req->dst);
328330

329-
walk->flags &= ~SKCIPHER_WALK_SLEEP;
330-
walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
331-
SKCIPHER_WALK_SLEEP : 0;
332-
333331
walk->blocksize = crypto_skcipher_blocksize(tfm);
334332
walk->ivsize = crypto_skcipher_ivsize(tfm);
335333
walk->alignmask = crypto_skcipher_alignmask(tfm);
@@ -339,23 +337,22 @@ int skcipher_walk_virt(struct skcipher_walk *walk,
339337
else
340338
walk->stride = alg->walksize;
341339

342-
err = skcipher_walk_first(walk);
343-
out:
344-
walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
345-
346-
return err;
340+
return skcipher_walk_first(walk);
347341
}
348342
EXPORT_SYMBOL_GPL(skcipher_walk_virt);
349343

350344
static int skcipher_walk_aead_common(struct skcipher_walk *walk,
351345
struct aead_request *req, bool atomic)
352346
{
353347
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
354-
int err;
355348

356349
walk->nbytes = 0;
357350
walk->iv = req->iv;
358351
walk->oiv = req->iv;
352+
if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic)
353+
walk->flags = SKCIPHER_WALK_SLEEP;
354+
else
355+
walk->flags = 0;
359356

360357
if (unlikely(!walk->total))
361358
return 0;
@@ -369,22 +366,12 @@ static int skcipher_walk_aead_common(struct skcipher_walk *walk,
369366
scatterwalk_done(&walk->in, 0, walk->total);
370367
scatterwalk_done(&walk->out, 0, walk->total);
371368

372-
if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
373-
walk->flags |= SKCIPHER_WALK_SLEEP;
374-
else
375-
walk->flags &= ~SKCIPHER_WALK_SLEEP;
376-
377369
walk->blocksize = crypto_aead_blocksize(tfm);
378370
walk->stride = crypto_aead_chunksize(tfm);
379371
walk->ivsize = crypto_aead_ivsize(tfm);
380372
walk->alignmask = crypto_aead_alignmask(tfm);
381373

382-
err = skcipher_walk_first(walk);
383-
384-
if (atomic)
385-
walk->flags &= ~SKCIPHER_WALK_SLEEP;
386-
387-
return err;
374+
return skcipher_walk_first(walk);
388375
}
389376

390377
int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,

0 commit comments

Comments
 (0)