2017-12-26 16:35:05

by Antoine Tenart

[permalink] [raw]
Subject: [PATCH 0/2] crypto: inside-secure - 2 extra fixes

Hi Herbert,

This series contains two fixes for the Inside Secure SafeXcel crypto
engine driver. The first one removes a warning when the DMA-API debug is
activated and the second one is fixing an issue with the driver in
certain cases where the input length is a multiple of a block size.

Thanks!
Antoine

Antoine Tenart (2):
crypto: inside-secure - avoid unmapping DMA memory that was not mapped
crypto: inside-secure - fix hash when length is a multiple of a block

drivers/crypto/inside-secure/safexcel_hash.c | 54 ++++++++++++++++++----------
1 file changed, 36 insertions(+), 18 deletions(-)

--
2.14.3


2017-12-26 16:35:04

by Antoine Tenart

[permalink] [raw]
Subject: [PATCH 2/2] crypto: inside-secure - fix hash when length is a multiple of a block

This patch fixes the hash support in the SafeXcel driver when the update
size is a multiple of a block size, and when a final call is made just
after with a size of 0. In such cases the driver should cache the last
block from the update to avoid handling 0 length data on the final call
(that's a hardware limitation).

Fixes: 1b44c5a60c13 ("crypto: inside-secure - add SafeXcel EIP197 crypto engine driver")
Signed-off-by: Antoine Tenart <[email protected]>
---
drivers/crypto/inside-secure/safexcel_hash.c | 34 ++++++++++++++++++++--------
1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c b/drivers/crypto/inside-secure/safexcel_hash.c
index fbc03d6e7db7..122a2a58e98f 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -189,17 +189,31 @@ static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
else
cache_len = queued - areq->nbytes;

- /*
- * If this is not the last request and the queued data does not fit
- * into full blocks, cache it for the next send() call.
- */
- extra = queued & (crypto_ahash_blocksize(ahash) - 1);
- if (!req->last_req && extra) {
- sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
- req->cache_next, extra, areq->nbytes - extra);
+ if (!req->last_req) {
+ /* If this is not the last request and the queued data does not
+ * fit into full blocks, cache it for the next send() call.
+ */
+ extra = queued & (crypto_ahash_blocksize(ahash) - 1);
+ if (!extra)
+ /* If this is not the last request and the queued data
+ * is a multiple of a block, cache the last one for now.
+ */
+ extra = queued - crypto_ahash_blocksize(ahash);

- queued -= extra;
- len -= extra;
+ if (extra) {
+ sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
+ req->cache_next, extra,
+ areq->nbytes - extra);
+
+ queued -= extra;
+ len -= extra;
+
+ if (!queued) {
+ *commands = 0;
+ *results = 0;
+ return 0;
+ }
+ }
}

spin_lock_bh(&priv->ring[ring].egress_lock);
--
2.14.3

2017-12-26 16:35:04

by Antoine Tenart

[permalink] [raw]
Subject: [PATCH 1/2] crypto: inside-secure - avoid unmapping DMA memory that was not mapped

This patch adds a parameter in the SafeXcel ahash request structure to
keep track of the number of SG entries mapped. This allows not to call
dma_unmap_sg() when dma_map_sg() wasn't called in the first place. This
also removes a warning when the debugging of the DMA-API is enabled in
the kernel configuration: "DMA-API: device driver tries to free DMA
memory it has not allocated".

Fixes: 1b44c5a60c13 ("crypto: inside-secure - add SafeXcel EIP197 crypto engine driver")
Signed-off-by: Antoine Tenart <[email protected]>
---
drivers/crypto/inside-secure/safexcel_hash.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c b/drivers/crypto/inside-secure/safexcel_hash.c
index d94614afc53d..fbc03d6e7db7 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -33,6 +33,8 @@ struct safexcel_ahash_req {
bool hmac;
bool needs_inv;

+ int nents;
+
u8 state_sz; /* expected sate size, only set once */
u32 state[SHA256_DIGEST_SIZE / sizeof(u32)] __aligned(sizeof(u32));

@@ -151,8 +153,10 @@ static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int rin
memcpy(areq->result, sreq->state,
crypto_ahash_digestsize(ahash));

- dma_unmap_sg(priv->dev, areq->src,
- sg_nents_for_len(areq->src, areq->nbytes), DMA_TO_DEVICE);
+ if (sreq->nents) {
+ dma_unmap_sg(priv->dev, areq->src, sreq->nents, DMA_TO_DEVICE);
+ sreq->nents = 0;
+ }

safexcel_free_context(priv, async, sreq->state_sz);

@@ -177,7 +181,7 @@ static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
struct safexcel_command_desc *cdesc, *first_cdesc = NULL;
struct safexcel_result_desc *rdesc;
struct scatterlist *sg;
- int i, nents, queued, len, cache_len, extra, n_cdesc = 0, ret = 0;
+ int i, queued, len, cache_len, extra, n_cdesc = 0, ret = 0;

queued = len = req->len - req->processed;
if (queued < crypto_ahash_blocksize(ahash))
@@ -233,15 +237,15 @@ static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
}

/* Now handle the current ahash request buffer(s) */
- nents = dma_map_sg(priv->dev, areq->src,
- sg_nents_for_len(areq->src, areq->nbytes),
- DMA_TO_DEVICE);
- if (!nents) {
+ req->nents = dma_map_sg(priv->dev, areq->src,
+ sg_nents_for_len(areq->src, areq->nbytes),
+ DMA_TO_DEVICE);
+ if (!req->nents) {
ret = -ENOMEM;
goto cdesc_rollback;
}

- for_each_sg(areq->src, sg, nents, i) {
+ for_each_sg(areq->src, sg, req->nents, i) {
int sglen = sg_dma_len(sg);

/* Do not overflow the request */
--
2.14.3

2018-01-05 11:16:16

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH 0/2] crypto: inside-secure - 2 extra fixes

On Tue, Dec 26, 2017 at 05:21:15PM +0100, Antoine Tenart wrote:
> Hi Herbert,
>
> This series contains two fixes for the Inside Secure SafeXcel crypto
> engine driver. The first one removes a warning when the DMA-API debug is
> activated and the second one is fixing an issue with the driver in
> certain cases where the input length is a multiple of a block size.
>
> Thanks!
> Antoine
>
> Antoine Tenart (2):
> crypto: inside-secure - avoid unmapping DMA memory that was not mapped
> crypto: inside-secure - fix hash when length is a multiple of a block
>
> drivers/crypto/inside-secure/safexcel_hash.c | 54 ++++++++++++++++++----------
> 1 file changed, 36 insertions(+), 18 deletions(-)

All applied. Thanks.
--
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt