Skip to content

Commit 0971f18

Browse files
oferhzgregkh
authored andcommitted
crypto: inside-secure - per request invalidation
[ Upstream commit 1eb7b40 ] When an invalidation request is needed we currently override the context .send and .handle_result helpers. This is wrong as under high load other requests can already be queued and overriding the context helpers will make them execute the wrong .send and .handle_result functions. This commit fixes this by adding a needs_inv flag in the request to choose the action to perform when sending requests or handling their results. This flag will be set when needed (i.e. when the context flag will be set). Fixes: 1b44c5a ("crypto: inside-secure - add SafeXcel EIP197 crypto engine driver") Signed-off-by: Ofer Heifetz <oferh@marvell.com> [Antoine: commit message, and removed non related changes from the original commit] Signed-off-by: Antoine Tenart <antoine.tenart@free-electrons.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <alexander.levin@verizon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0653ba0 commit 0971f18

2 files changed

Lines changed: 111 additions & 27 deletions

File tree

‎drivers/crypto/inside-secure/safexcel_cipher.c‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <crypto/aes.h>
1616
#include <crypto/skcipher.h>
17+
#include <crypto/internal/skcipher.h>
1718

1819
#include "safexcel.h"
1920

@@ -33,6 +34,10 @@ struct safexcel_cipher_ctx {
3334
unsigned int key_len;
3435
};
3536

37+
struct safexcel_cipher_req {
38+
bool needs_inv;
39+
};
40+
3641
static void safexcel_cipher_token(struct safexcel_cipher_ctx *ctx,
3742
struct crypto_async_request *async,
3843
struct safexcel_command_desc *cdesc,
@@ -126,9 +131,9 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
126131
return 0;
127132
}
128133

129-
static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
130-
struct crypto_async_request *async,
131-
bool *should_complete, int *ret)
134+
static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
135+
struct crypto_async_request *async,
136+
bool *should_complete, int *ret)
132137
{
133138
struct skcipher_request *req = skcipher_request_cast(async);
134139
struct safexcel_result_desc *rdesc;
@@ -265,7 +270,6 @@ static int safexcel_aes_send(struct crypto_async_request *async,
265270
spin_unlock_bh(&priv->ring[ring].egress_lock);
266271

267272
request->req = &req->base;
268-
ctx->base.handle_result = safexcel_handle_result;
269273

270274
*commands = n_cdesc;
271275
*results = n_rdesc;
@@ -341,8 +345,6 @@ static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
341345

342346
ring = safexcel_select_ring(priv);
343347
ctx->base.ring = ring;
344-
ctx->base.needs_inv = false;
345-
ctx->base.send = safexcel_aes_send;
346348

347349
spin_lock_bh(&priv->ring[ring].queue_lock);
348350
enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async);
@@ -359,6 +361,26 @@ static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
359361
return ndesc;
360362
}
361363

364+
static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
365+
struct crypto_async_request *async,
366+
bool *should_complete, int *ret)
367+
{
368+
struct skcipher_request *req = skcipher_request_cast(async);
369+
struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
370+
int err;
371+
372+
if (sreq->needs_inv) {
373+
sreq->needs_inv = false;
374+
err = safexcel_handle_inv_result(priv, ring, async,
375+
should_complete, ret);
376+
} else {
377+
err = safexcel_handle_req_result(priv, ring, async,
378+
should_complete, ret);
379+
}
380+
381+
return err;
382+
}
383+
362384
static int safexcel_cipher_send_inv(struct crypto_async_request *async,
363385
int ring, struct safexcel_request *request,
364386
int *commands, int *results)
@@ -368,8 +390,6 @@ static int safexcel_cipher_send_inv(struct crypto_async_request *async,
368390
struct safexcel_crypto_priv *priv = ctx->priv;
369391
int ret;
370392

371-
ctx->base.handle_result = safexcel_handle_inv_result;
372-
373393
ret = safexcel_invalidate_cache(async, &ctx->base, priv,
374394
ctx->base.ctxr_dma, ring, request);
375395
if (unlikely(ret))
@@ -381,11 +401,29 @@ static int safexcel_cipher_send_inv(struct crypto_async_request *async,
381401
return 0;
382402
}
383403

404+
static int safexcel_send(struct crypto_async_request *async,
405+
int ring, struct safexcel_request *request,
406+
int *commands, int *results)
407+
{
408+
struct skcipher_request *req = skcipher_request_cast(async);
409+
struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
410+
int ret;
411+
412+
if (sreq->needs_inv)
413+
ret = safexcel_cipher_send_inv(async, ring, request,
414+
commands, results);
415+
else
416+
ret = safexcel_aes_send(async, ring, request,
417+
commands, results);
418+
return ret;
419+
}
420+
384421
static int safexcel_cipher_exit_inv(struct crypto_tfm *tfm)
385422
{
386423
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
387424
struct safexcel_crypto_priv *priv = ctx->priv;
388425
struct skcipher_request req;
426+
struct safexcel_cipher_req *sreq = skcipher_request_ctx(&req);
389427
struct safexcel_inv_result result = {};
390428
int ring = ctx->base.ring;
391429

@@ -399,7 +437,7 @@ static int safexcel_cipher_exit_inv(struct crypto_tfm *tfm)
399437
skcipher_request_set_tfm(&req, __crypto_skcipher_cast(tfm));
400438
ctx = crypto_tfm_ctx(req.base.tfm);
401439
ctx->base.exit_inv = true;
402-
ctx->base.send = safexcel_cipher_send_inv;
440+
sreq->needs_inv = true;
403441

404442
spin_lock_bh(&priv->ring[ring].queue_lock);
405443
crypto_enqueue_request(&priv->ring[ring].queue, &req.base);
@@ -424,19 +462,21 @@ static int safexcel_aes(struct skcipher_request *req,
424462
enum safexcel_cipher_direction dir, u32 mode)
425463
{
426464
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
465+
struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
427466
struct safexcel_crypto_priv *priv = ctx->priv;
428467
int ret, ring;
429468

469+
sreq->needs_inv = false;
430470
ctx->direction = dir;
431471
ctx->mode = mode;
432472

433473
if (ctx->base.ctxr) {
434-
if (ctx->base.needs_inv)
435-
ctx->base.send = safexcel_cipher_send_inv;
474+
if (ctx->base.needs_inv) {
475+
sreq->needs_inv = true;
476+
ctx->base.needs_inv = false;
477+
}
436478
} else {
437479
ctx->base.ring = safexcel_select_ring(priv);
438-
ctx->base.send = safexcel_aes_send;
439-
440480
ctx->base.ctxr = dma_pool_zalloc(priv->context_pool,
441481
EIP197_GFP_FLAGS(req->base),
442482
&ctx->base.ctxr_dma);
@@ -476,6 +516,11 @@ static int safexcel_skcipher_cra_init(struct crypto_tfm *tfm)
476516
alg.skcipher.base);
477517

478518
ctx->priv = tmpl->priv;
519+
ctx->base.send = safexcel_send;
520+
ctx->base.handle_result = safexcel_handle_result;
521+
522+
crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
523+
sizeof(struct safexcel_cipher_req));
479524

480525
return 0;
481526
}

‎drivers/crypto/inside-secure/safexcel_hash.c‎

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct safexcel_ahash_req {
3232
bool last_req;
3333
bool finish;
3434
bool hmac;
35+
bool needs_inv;
3536

3637
int nents;
3738

@@ -121,9 +122,9 @@ static void safexcel_context_control(struct safexcel_ahash_ctx *ctx,
121122
}
122123
}
123124

124-
static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
125-
struct crypto_async_request *async,
126-
bool *should_complete, int *ret)
125+
static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
126+
struct crypto_async_request *async,
127+
bool *should_complete, int *ret)
127128
{
128129
struct safexcel_result_desc *rdesc;
129130
struct ahash_request *areq = ahash_request_cast(async);
@@ -169,9 +170,9 @@ static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
169170
return 1;
170171
}
171172

172-
static int safexcel_ahash_send(struct crypto_async_request *async, int ring,
173-
struct safexcel_request *request, int *commands,
174-
int *results)
173+
static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
174+
struct safexcel_request *request,
175+
int *commands, int *results)
175176
{
176177
struct ahash_request *areq = ahash_request_cast(async);
177178
struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
@@ -310,7 +311,6 @@ static int safexcel_ahash_send(struct crypto_async_request *async, int ring,
310311

311312
req->processed += len;
312313
request->req = &areq->base;
313-
ctx->base.handle_result = safexcel_handle_result;
314314

315315
*commands = n_cdesc;
316316
*results = 1;
@@ -394,8 +394,6 @@ static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
394394

395395
ring = safexcel_select_ring(priv);
396396
ctx->base.ring = ring;
397-
ctx->base.needs_inv = false;
398-
ctx->base.send = safexcel_ahash_send;
399397

400398
spin_lock_bh(&priv->ring[ring].queue_lock);
401399
enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async);
@@ -412,6 +410,26 @@ static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
412410
return 1;
413411
}
414412

413+
static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
414+
struct crypto_async_request *async,
415+
bool *should_complete, int *ret)
416+
{
417+
struct ahash_request *areq = ahash_request_cast(async);
418+
struct safexcel_ahash_req *req = ahash_request_ctx(areq);
419+
int err;
420+
421+
if (req->needs_inv) {
422+
req->needs_inv = false;
423+
err = safexcel_handle_inv_result(priv, ring, async,
424+
should_complete, ret);
425+
} else {
426+
err = safexcel_handle_req_result(priv, ring, async,
427+
should_complete, ret);
428+
}
429+
430+
return err;
431+
}
432+
415433
static int safexcel_ahash_send_inv(struct crypto_async_request *async,
416434
int ring, struct safexcel_request *request,
417435
int *commands, int *results)
@@ -420,7 +438,6 @@ static int safexcel_ahash_send_inv(struct crypto_async_request *async,
420438
struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
421439
int ret;
422440

423-
ctx->base.handle_result = safexcel_handle_inv_result;
424441
ret = safexcel_invalidate_cache(async, &ctx->base, ctx->priv,
425442
ctx->base.ctxr_dma, ring, request);
426443
if (unlikely(ret))
@@ -432,11 +449,29 @@ static int safexcel_ahash_send_inv(struct crypto_async_request *async,
432449
return 0;
433450
}
434451

452+
static int safexcel_ahash_send(struct crypto_async_request *async,
453+
int ring, struct safexcel_request *request,
454+
int *commands, int *results)
455+
{
456+
struct ahash_request *areq = ahash_request_cast(async);
457+
struct safexcel_ahash_req *req = ahash_request_ctx(areq);
458+
int ret;
459+
460+
if (req->needs_inv)
461+
ret = safexcel_ahash_send_inv(async, ring, request,
462+
commands, results);
463+
else
464+
ret = safexcel_ahash_send_req(async, ring, request,
465+
commands, results);
466+
return ret;
467+
}
468+
435469
static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm)
436470
{
437471
struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
438472
struct safexcel_crypto_priv *priv = ctx->priv;
439473
struct ahash_request req;
474+
struct safexcel_ahash_req *rctx = ahash_request_ctx(&req);
440475
struct safexcel_inv_result result = {};
441476
int ring = ctx->base.ring;
442477

@@ -450,7 +485,7 @@ static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm)
450485
ahash_request_set_tfm(&req, __crypto_ahash_cast(tfm));
451486
ctx = crypto_tfm_ctx(req.base.tfm);
452487
ctx->base.exit_inv = true;
453-
ctx->base.send = safexcel_ahash_send_inv;
488+
rctx->needs_inv = true;
454489

455490
spin_lock_bh(&priv->ring[ring].queue_lock);
456491
crypto_enqueue_request(&priv->ring[ring].queue, &req.base);
@@ -501,14 +536,16 @@ static int safexcel_ahash_enqueue(struct ahash_request *areq)
501536
struct safexcel_crypto_priv *priv = ctx->priv;
502537
int ret, ring;
503538

504-
ctx->base.send = safexcel_ahash_send;
539+
req->needs_inv = false;
505540

506541
if (req->processed && ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED)
507542
ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq);
508543

509544
if (ctx->base.ctxr) {
510-
if (ctx->base.needs_inv)
511-
ctx->base.send = safexcel_ahash_send_inv;
545+
if (ctx->base.needs_inv) {
546+
ctx->base.needs_inv = false;
547+
req->needs_inv = true;
548+
}
512549
} else {
513550
ctx->base.ring = safexcel_select_ring(priv);
514551
ctx->base.ctxr = dma_pool_zalloc(priv->context_pool,
@@ -642,6 +679,8 @@ static int safexcel_ahash_cra_init(struct crypto_tfm *tfm)
642679
struct safexcel_alg_template, alg.ahash);
643680

644681
ctx->priv = tmpl->priv;
682+
ctx->base.send = safexcel_ahash_send;
683+
ctx->base.handle_result = safexcel_handle_result;
645684

646685
crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
647686
sizeof(struct safexcel_ahash_req));

0 commit comments

Comments
 (0)