Skip to content

Commit a5e5c13

Browse files
horiagherbertx
authored andcommitted
crypto: caam - fix S/G table passing page boundary
According to CAAM RM: -crypto engine reads 4 S/G entries (64 bytes) at a time, even if the S/G table has fewer entries -it's the responsibility of the user / programmer to make sure this HW behaviour has no side effect The drivers do not take care of this currently, leading to IOMMU faults when the S/G table ends close to a page boundary - since only one page is DMA mapped, while CAAM's DMA engine accesses two pages. Fix this by rounding up the number of allocated S/G table entries to a multiple of 4. Note that in case of two *contiguous* S/G tables, only the last table might needs extra entries. Signed-off-by: Horia Geantă <horia.geanta@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent dcd9c76 commit a5e5c13

6 files changed

Lines changed: 147 additions & 37 deletions

File tree

‎drivers/crypto/caam/caamalg.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,16 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
13811381
}
13821382
}
13831383

1384+
/*
1385+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1386+
* the end of the table by allocating more S/G entries.
1387+
*/
13841388
sec4_sg_len = mapped_src_nents > 1 ? mapped_src_nents : 0;
1385-
sec4_sg_len += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
1389+
if (mapped_dst_nents > 1)
1390+
sec4_sg_len += pad_sg_nents(mapped_dst_nents);
1391+
else
1392+
sec4_sg_len = pad_sg_nents(sec4_sg_len);
1393+
13861394
sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
13871395

13881396
/* allocate space for base edesc and hw desc commands, link tables */
@@ -1720,7 +1728,25 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
17201728
else
17211729
sec4_sg_ents = mapped_src_nents + !!ivsize;
17221730
dst_sg_idx = sec4_sg_ents;
1723-
sec4_sg_ents += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
1731+
1732+
/*
1733+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1734+
* the end of the table by allocating more S/G entries. Logic:
1735+
* if (src != dst && output S/G)
1736+
* pad output S/G, if needed
1737+
* else if (src == dst && S/G)
1738+
* overlapping S/Gs; pad one of them
1739+
* else if (input S/G) ...
1740+
* pad input S/G, if needed
1741+
*/
1742+
if (mapped_dst_nents > 1)
1743+
sec4_sg_ents += pad_sg_nents(mapped_dst_nents);
1744+
else if ((req->src == req->dst) && (mapped_src_nents > 1))
1745+
sec4_sg_ents = max(pad_sg_nents(sec4_sg_ents),
1746+
!!ivsize + pad_sg_nents(mapped_src_nents));
1747+
else
1748+
sec4_sg_ents = pad_sg_nents(sec4_sg_ents);
1749+
17241750
sec4_sg_bytes = sec4_sg_ents * sizeof(struct sec4_sg_entry);
17251751

17261752
/*

‎drivers/crypto/caam/caamalg_qi.c‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Based on caamalg.c
55
*
66
* Copyright 2013-2016 Freescale Semiconductor, Inc.
7-
* Copyright 2016-2018 NXP
7+
* Copyright 2016-2019 NXP
88
*/
99

1010
#include "compat.h"
@@ -1019,9 +1019,24 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
10191019
/*
10201020
* Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
10211021
* Input is not contiguous.
1022+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1023+
* the end of the table by allocating more S/G entries. Logic:
1024+
* if (src != dst && output S/G)
1025+
* pad output S/G, if needed
1026+
* else if (src == dst && S/G)
1027+
* overlapping S/Gs; pad one of them
1028+
* else if (input S/G) ...
1029+
* pad input S/G, if needed
10221030
*/
1023-
qm_sg_ents = 1 + !!ivsize + mapped_src_nents +
1024-
(mapped_dst_nents > 1 ? mapped_dst_nents : 0);
1031+
qm_sg_ents = 1 + !!ivsize + mapped_src_nents;
1032+
if (mapped_dst_nents > 1)
1033+
qm_sg_ents += pad_sg_nents(mapped_dst_nents);
1034+
else if ((req->src == req->dst) && (mapped_src_nents > 1))
1035+
qm_sg_ents = max(pad_sg_nents(qm_sg_ents),
1036+
1 + !!ivsize + pad_sg_nents(mapped_src_nents));
1037+
else
1038+
qm_sg_ents = pad_sg_nents(qm_sg_ents);
1039+
10251040
sg_table = &edesc->sgt[0];
10261041
qm_sg_bytes = qm_sg_ents * sizeof(*sg_table);
10271042
if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize >
@@ -1276,7 +1291,24 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
12761291
qm_sg_ents = 1 + mapped_src_nents;
12771292
dst_sg_idx = qm_sg_ents;
12781293

1279-
qm_sg_ents += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
1294+
/*
1295+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1296+
* the end of the table by allocating more S/G entries. Logic:
1297+
* if (src != dst && output S/G)
1298+
* pad output S/G, if needed
1299+
* else if (src == dst && S/G)
1300+
* overlapping S/Gs; pad one of them
1301+
* else if (input S/G) ...
1302+
* pad input S/G, if needed
1303+
*/
1304+
if (mapped_dst_nents > 1)
1305+
qm_sg_ents += pad_sg_nents(mapped_dst_nents);
1306+
else if ((req->src == req->dst) && (mapped_src_nents > 1))
1307+
qm_sg_ents = max(pad_sg_nents(qm_sg_ents),
1308+
1 + pad_sg_nents(mapped_src_nents));
1309+
else
1310+
qm_sg_ents = pad_sg_nents(qm_sg_ents);
1311+
12801312
qm_sg_bytes = qm_sg_ents * sizeof(struct qm_sg_entry);
12811313
if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes +
12821314
ivsize > CAAM_QI_MEMCACHE_SIZE)) {

‎drivers/crypto/caam/caamalg_qi2.c‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
22
/*
33
* Copyright 2015-2016 Freescale Semiconductor Inc.
4-
* Copyright 2017-2018 NXP
4+
* Copyright 2017-2019 NXP
55
*/
66

77
#include "compat.h"
@@ -460,9 +460,25 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
460460
/*
461461
* Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
462462
* Input is not contiguous.
463+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
464+
* the end of the table by allocating more S/G entries. Logic:
465+
* if (src != dst && output S/G)
466+
* pad output S/G, if needed
467+
* else if (src == dst && S/G)
468+
* overlapping S/Gs; pad one of them
469+
* else if (input S/G) ...
470+
* pad input S/G, if needed
463471
*/
464-
qm_sg_nents = 1 + !!ivsize + mapped_src_nents +
465-
(mapped_dst_nents > 1 ? mapped_dst_nents : 0);
472+
qm_sg_nents = 1 + !!ivsize + mapped_src_nents;
473+
if (mapped_dst_nents > 1)
474+
qm_sg_nents += pad_sg_nents(mapped_dst_nents);
475+
else if ((req->src == req->dst) && (mapped_src_nents > 1))
476+
qm_sg_nents = max(pad_sg_nents(qm_sg_nents),
477+
1 + !!ivsize +
478+
pad_sg_nents(mapped_src_nents));
479+
else
480+
qm_sg_nents = pad_sg_nents(qm_sg_nents);
481+
466482
sg_table = &edesc->sgt[0];
467483
qm_sg_bytes = qm_sg_nents * sizeof(*sg_table);
468484
if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize >
@@ -1086,7 +1102,24 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req)
10861102
qm_sg_ents = 1 + mapped_src_nents;
10871103
dst_sg_idx = qm_sg_ents;
10881104

1089-
qm_sg_ents += mapped_dst_nents > 1 ? mapped_dst_nents : 0;
1105+
/*
1106+
* HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1107+
* the end of the table by allocating more S/G entries. Logic:
1108+
* if (src != dst && output S/G)
1109+
* pad output S/G, if needed
1110+
* else if (src == dst && S/G)
1111+
* overlapping S/Gs; pad one of them
1112+
* else if (input S/G) ...
1113+
* pad input S/G, if needed
1114+
*/
1115+
if (mapped_dst_nents > 1)
1116+
qm_sg_ents += pad_sg_nents(mapped_dst_nents);
1117+
else if ((req->src == req->dst) && (mapped_src_nents > 1))
1118+
qm_sg_ents = max(pad_sg_nents(qm_sg_ents),
1119+
1 + pad_sg_nents(mapped_src_nents));
1120+
else
1121+
qm_sg_ents = pad_sg_nents(qm_sg_ents);
1122+
10901123
qm_sg_bytes = qm_sg_ents * sizeof(struct dpaa2_sg_entry);
10911124
if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes +
10921125
ivsize > CAAM_QI_MEMCACHE_SIZE)) {
@@ -3418,7 +3451,7 @@ static int ahash_update_ctx(struct ahash_request *req)
34183451

34193452
edesc->src_nents = src_nents;
34203453
qm_sg_src_index = 1 + (*buflen ? 1 : 0);
3421-
qm_sg_bytes = (qm_sg_src_index + mapped_nents) *
3454+
qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
34223455
sizeof(*sg_table);
34233456
sg_table = &edesc->sgt[0];
34243457

@@ -3503,7 +3536,7 @@ static int ahash_final_ctx(struct ahash_request *req)
35033536
gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
35043537
GFP_KERNEL : GFP_ATOMIC;
35053538
int buflen = *current_buflen(state);
3506-
int qm_sg_bytes, qm_sg_src_index;
3539+
int qm_sg_bytes;
35073540
int digestsize = crypto_ahash_digestsize(ahash);
35083541
struct ahash_edesc *edesc;
35093542
struct dpaa2_sg_entry *sg_table;
@@ -3514,8 +3547,7 @@ static int ahash_final_ctx(struct ahash_request *req)
35143547
if (!edesc)
35153548
return -ENOMEM;
35163549

3517-
qm_sg_src_index = 1 + (buflen ? 1 : 0);
3518-
qm_sg_bytes = qm_sg_src_index * sizeof(*sg_table);
3550+
qm_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) * sizeof(*sg_table);
35193551
sg_table = &edesc->sgt[0];
35203552

35213553
ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
@@ -3527,7 +3559,7 @@ static int ahash_final_ctx(struct ahash_request *req)
35273559
if (ret)
35283560
goto unmap_ctx;
35293561

3530-
dpaa2_sg_set_final(sg_table + qm_sg_src_index - 1, true);
3562+
dpaa2_sg_set_final(sg_table + (buflen ? 1 : 0), true);
35313563

35323564
edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
35333565
DMA_TO_DEVICE);
@@ -3608,7 +3640,8 @@ static int ahash_finup_ctx(struct ahash_request *req)
36083640

36093641
edesc->src_nents = src_nents;
36103642
qm_sg_src_index = 1 + (buflen ? 1 : 0);
3611-
qm_sg_bytes = (qm_sg_src_index + mapped_nents) * sizeof(*sg_table);
3643+
qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
3644+
sizeof(*sg_table);
36123645
sg_table = &edesc->sgt[0];
36133646

36143647
ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
@@ -3705,7 +3738,7 @@ static int ahash_digest(struct ahash_request *req)
37053738
int qm_sg_bytes;
37063739
struct dpaa2_sg_entry *sg_table = &edesc->sgt[0];
37073740

3708-
qm_sg_bytes = mapped_nents * sizeof(*sg_table);
3741+
qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table);
37093742
sg_to_qm_sg_last(req->src, mapped_nents, sg_table, 0);
37103743
edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
37113744
qm_sg_bytes, DMA_TO_DEVICE);
@@ -3877,7 +3910,8 @@ static int ahash_update_no_ctx(struct ahash_request *req)
38773910
}
38783911

38793912
edesc->src_nents = src_nents;
3880-
qm_sg_bytes = (1 + mapped_nents) * sizeof(*sg_table);
3913+
qm_sg_bytes = pad_sg_nents(1 + mapped_nents) *
3914+
sizeof(*sg_table);
38813915
sg_table = &edesc->sgt[0];
38823916

38833917
ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
@@ -3996,7 +4030,7 @@ static int ahash_finup_no_ctx(struct ahash_request *req)
39964030
}
39974031

39984032
edesc->src_nents = src_nents;
3999-
qm_sg_bytes = (2 + mapped_nents) * sizeof(*sg_table);
4033+
qm_sg_bytes = pad_sg_nents(2 + mapped_nents) * sizeof(*sg_table);
40004034
sg_table = &edesc->sgt[0];
40014035

40024036
ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
@@ -4111,7 +4145,8 @@ static int ahash_update_first(struct ahash_request *req)
41114145
int qm_sg_bytes;
41124146

41134147
sg_to_qm_sg_last(req->src, mapped_nents, sg_table, 0);
4114-
qm_sg_bytes = mapped_nents * sizeof(*sg_table);
4148+
qm_sg_bytes = pad_sg_nents(mapped_nents) *
4149+
sizeof(*sg_table);
41154150
edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
41164151
qm_sg_bytes,
41174152
DMA_TO_DEVICE);

‎drivers/crypto/caam/caamhash.c‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
759759

760760
if (nents > 1 || first_sg) {
761761
struct sec4_sg_entry *sg = edesc->sec4_sg;
762-
unsigned int sgsize = sizeof(*sg) * (first_sg + nents);
762+
unsigned int sgsize = sizeof(*sg) *
763+
pad_sg_nents(first_sg + nents);
763764

764765
sg_to_sec4_sg_last(req->src, nents, sg + first_sg, 0);
765766

@@ -819,6 +820,8 @@ static int ahash_update_ctx(struct ahash_request *req)
819820
}
820821

821822
if (to_hash) {
823+
int pad_nents;
824+
822825
src_nents = sg_nents_for_len(req->src,
823826
req->nbytes - (*next_buflen));
824827
if (src_nents < 0) {
@@ -838,15 +841,14 @@ static int ahash_update_ctx(struct ahash_request *req)
838841
}
839842

840843
sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
841-
sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
842-
sizeof(struct sec4_sg_entry);
844+
pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
845+
sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
843846

844847
/*
845848
* allocate space for base edesc and hw desc commands,
846849
* link tables
847850
*/
848-
edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
849-
ctx->sh_desc_update,
851+
edesc = ahash_edesc_alloc(ctx, pad_nents, ctx->sh_desc_update,
850852
ctx->sh_desc_update_dma, flags);
851853
if (!edesc) {
852854
dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
@@ -935,18 +937,17 @@ static int ahash_final_ctx(struct ahash_request *req)
935937
GFP_KERNEL : GFP_ATOMIC;
936938
int buflen = *current_buflen(state);
937939
u32 *desc;
938-
int sec4_sg_bytes, sec4_sg_src_index;
940+
int sec4_sg_bytes;
939941
int digestsize = crypto_ahash_digestsize(ahash);
940942
struct ahash_edesc *edesc;
941943
int ret;
942944

943-
sec4_sg_src_index = 1 + (buflen ? 1 : 0);
944-
sec4_sg_bytes = sec4_sg_src_index * sizeof(struct sec4_sg_entry);
945+
sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
946+
sizeof(struct sec4_sg_entry);
945947

946948
/* allocate space for base edesc and hw desc commands, link tables */
947-
edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index,
948-
ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
949-
flags);
949+
edesc = ahash_edesc_alloc(ctx, 4, ctx->sh_desc_fin,
950+
ctx->sh_desc_fin_dma, flags);
950951
if (!edesc)
951952
return -ENOMEM;
952953

@@ -963,7 +964,7 @@ static int ahash_final_ctx(struct ahash_request *req)
963964
if (ret)
964965
goto unmap_ctx;
965966

966-
sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index - 1);
967+
sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
967968

968969
edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
969970
sec4_sg_bytes, DMA_TO_DEVICE);
@@ -1246,6 +1247,8 @@ static int ahash_update_no_ctx(struct ahash_request *req)
12461247
}
12471248

12481249
if (to_hash) {
1250+
int pad_nents;
1251+
12491252
src_nents = sg_nents_for_len(req->src,
12501253
req->nbytes - *next_buflen);
12511254
if (src_nents < 0) {
@@ -1264,14 +1267,14 @@ static int ahash_update_no_ctx(struct ahash_request *req)
12641267
mapped_nents = 0;
12651268
}
12661269

1267-
sec4_sg_bytes = (1 + mapped_nents) *
1268-
sizeof(struct sec4_sg_entry);
1270+
pad_nents = pad_sg_nents(1 + mapped_nents);
1271+
sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
12691272

12701273
/*
12711274
* allocate space for base edesc and hw desc commands,
12721275
* link tables
12731276
*/
1274-
edesc = ahash_edesc_alloc(ctx, 1 + mapped_nents,
1277+
edesc = ahash_edesc_alloc(ctx, pad_nents,
12751278
ctx->sh_desc_update_first,
12761279
ctx->sh_desc_update_first_dma,
12771280
flags);

‎drivers/crypto/caam/caampkc.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* caam - Freescale FSL CAAM support for Public Key Cryptography
44
*
55
* Copyright 2016 Freescale Semiconductor, Inc.
6-
* Copyright 2018 NXP
6+
* Copyright 2018-2019 NXP
77
*
88
* There is no Shared Descriptor for PKC so that the Job Descriptor must carry
99
* all the desired key parameters, input and output pointers.
@@ -239,8 +239,11 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
239239

240240
if (src_nents > 1)
241241
sec4_sg_len = src_nents;
242+
242243
if (dst_nents > 1)
243-
sec4_sg_len += dst_nents;
244+
sec4_sg_len += pad_sg_nents(dst_nents);
245+
else
246+
sec4_sg_len = pad_sg_nents(sec4_sg_len);
244247

245248
sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
246249

‎drivers/crypto/caam/desc_constr.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* caam descriptor construction helper functions
44
*
55
* Copyright 2008-2012 Freescale Semiconductor, Inc.
6+
* Copyright 2019 NXP
67
*/
78

89
#ifndef DESC_CONSTR_H
@@ -37,6 +38,16 @@
3738

3839
extern bool caam_little_end;
3940

41+
/*
42+
* HW fetches 4 S/G table entries at a time, irrespective of how many entries
43+
* are in the table. It's SW's responsibility to make sure these accesses
44+
* do not have side effects.
45+
*/
46+
static inline int pad_sg_nents(int sg_nents)
47+
{
48+
return ALIGN(sg_nents, 4);
49+
}
50+
4051
static inline int desc_len(u32 * const desc)
4152
{
4253
return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK;

0 commit comments

Comments
 (0)