crypto: ahash - Allow async stack requests when specified
Herbert Xu
maintainerv2 cover crypto: ahash - Allow async stack requests when specified
v2 fixes the inverted tests that broke everything.
This patch series adds support for passing stack requests down to algorithms that do not do DMA on the request context, e.g., phmac.
Herbert Xu (2):
crypto: ahash - Allow async stack requests when specified
crypto: s390/phmac - Allow stack requests
arch/s390/crypto/phmac_s390.c | 1 + crypto/ahash.c | 22 ++++++++++++++++++---- include/crypto/internal/hash.h | 3 +++ 3 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH v2 1/2 crypto: ahash - Allow async stack requests when specified
As it stands stack requests are forbidden for async algorithms because of the inability to perform DMA on stack memory.
However, some async algorithms do not perform DMA and are able to handle stack requests. Allow such uses by addnig a new type bit CRYPTO_AHASH_ALG_STACK_REQ. When it is set on the algorithm stack requests will be allowed even if the algorithm is asynchronous.
--- crypto/ahash.c | 22 ++++++++++++++++++---- include/crypto/internal/hash.h | 3 +++ 2 files changed, 21 insertions(+), 4 deletions(-)
crypto/ahash.c +18 -4
@@ -49,6 +49,20 @@ static inline bool crypto_ahash_need_fallback(struct crypto_ahash *tfm)CRYPTO_ALG_NEED_FALLBACK ; } +static inline bool crypto_ahash_stack_req_ok (struct ahash_request *req) +{ +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); + +if (!ahash_req_on_stack (req)) +return true; + +if (!ahash_is_async (tfm)) +return true; + +return crypto_ahash_alg (tfm)->halg.base.cra_flags & +CRYPTO_AHASH_ALG_STACK_REQ ; +} +static inline void ahash_op_done (void *data,int err,int (*finish)(struct ahash_request *,int )) {
@@ -376,7 +390,7 @@ int crypto_ahash_init(struct ahash_request *req)return crypto_shash_init (prepare_shash_desc (req, tfm));if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ; -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (crypto_ahash_block_only (tfm)) {u8 *buf =ahash_request_ctx (req);
@@ -451,7 +465,7 @@ int crypto_ahash_update(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_update (req,ahash_request_ctx (req)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (!crypto_ahash_block_only (tfm))return ahash_do_req_chain (req, &crypto_ahash_alg (tfm)->update);
@@ -531,7 +545,7 @@ int crypto_ahash_finup(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_finup (req,ahash_request_ctx (req)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (!crypto_ahash_alg (tfm)->finup)return ahash_def_finup (req);
@@ -569,7 +583,7 @@ int crypto_ahash_digest(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_digest (req,prepare_shash_desc (req, tfm)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ;
include/crypto/internal/hash.h +3 -0
@@ -23,6 +23,9 @@/* This bit is set by the Crypto API if export_core is not supported. */ #define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000 +/* This bit is set by the Crypto API if stack requests are supported. */ +#define CRYPTO_AHASH_ALG_STACK_REQ 0x10000000 +#define HASH_FBREQ_ON_STACK (name, req) \ char __##name##_req[sizeof(struct ahash_request) + \ MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
Signature · 1 line
2.39.5
Mikulas Patocka
maintainerHi
Would it be possible to export the function crypto_ahash_stack_req_ok, so that I could know up-front whether having a request on the stack will succeed or not?
Perhaps the function crypto_ahash_stack_req_ok could take "struct crypto_ahash *" argument rather than "struct ahash_request, *" so that I would know in advance whether it makes sense to try to build the request on the stack or not.
Mikulas
Quoted 92 lines · Herbert Xu
As it stands stack requests are forbidden for async algorithms because of the inability to perform DMA on stack memory.
However, some async algorithms do not perform DMA and are able to handle stack requests. Allow such uses by addnig a new type bit
CRYPTO_AHASH_ALG_STACK_REQ. When it is set on the algorithm stack requests will be allowed even if the algorithm is asynchronous.Signed-off-by Herbert Xu <[email protected]>--- crypto/ahash.c | 22 ++++++++++++++++++---- include/crypto/internal/hash.h | 3 +++ 2 files changed, 21 insertions(+), 4 deletions(-)crypto/ahash.c +18
-4@@ -49,6 +49,20 @@ static inline bool crypto_ahash_need_fallback(struct crypto_ahash *tfm)CRYPTO_ALG_NEED_FALLBACK ; } +static inline bool crypto_ahash_stack_req_ok (struct ahash_request *req) +{ +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); + +if (!ahash_req_on_stack (req)) +return true; + +if (!ahash_is_async (tfm)) +return true; + +return crypto_ahash_alg (tfm)->halg.base.cra_flags & +CRYPTO_AHASH_ALG_STACK_REQ ; +} +static inline void ahash_op_done (void *data,int err,int (*finish)(struct ahash_request *,int )) {@@ -376,7 +390,7 @@ int crypto_ahash_init(struct ahash_request *req)return crypto_shash_init (prepare_shash_desc (req, tfm));if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ; -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (crypto_ahash_block_only (tfm)) {u8 *buf =ahash_request_ctx (req);@@ -451,7 +465,7 @@ int crypto_ahash_update(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_update (req,ahash_request_ctx (req)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (!crypto_ahash_block_only (tfm))return ahash_do_req_chain (req, &crypto_ahash_alg (tfm)->update);@@ -531,7 +545,7 @@ int crypto_ahash_finup(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_finup (req,ahash_request_ctx (req)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (!crypto_ahash_alg (tfm)->finup)return ahash_def_finup (req);@@ -569,7 +583,7 @@ int crypto_ahash_digest(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_digest (req,prepare_shash_desc (req, tfm)); -if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +if (!crypto_ahash_stack_req_ok (req))return -EAGAIN ;if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ;include/crypto/internal/hash.h +3
-0@@ -23,6 +23,9 @@/* This bit is set by the Crypto API if export_core is not supported. */ #define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000 +/* This bit is set by the Crypto API if stack requests are supported. */ +#define CRYPTO_AHASH_ALG_STACK_REQ 0x10000000 +#define HASH_FBREQ_ON_STACK (name, req) \ char __##name##_req[sizeof(struct ahash_request) + \ MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \Signature · 1 line
2.39.5
Herbert Xu
maintainerQuoted 9 lines · Mikulas Patocka
Would it be possible to export the function
crypto_ahash_stack_req_ok, so that I could know up-front whether having a request on the stack will succeed or not?Perhaps the function
crypto_ahash_stack_req_okcould take "structcrypto_ahash*" argument rather than "structahash_request, *" so that I would know in advance whether it makes sense to try to build the request on the stack or not.
I think the pain point is the use of SG lists. If we could convert them to something like iov's then you should be able to supply stack memory unconditionally.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Mikulas Patocka
maintainerQuoted 11 lines · Herbert Xu
Quoted 9 lines · Mikulas Patocka
Would it be possible to export the function
crypto_ahash_stack_req_ok, so that I could know up-front whether having a request on the stack will succeed or not?Perhaps the function
crypto_ahash_stack_req_okcould take "structcrypto_ahash*" argument rather than "structahash_request, *" so that I would know in advance whether it makes sense to try to build the request on the stack or not.
Quoted 5 lines
I think the pain point is the use of SG lists. If we could convert them to something like iov's then you should be able to supply stack memory unconditionally.
Cheers,
On architectures without automatic DMA/cache coherency, you must make sure that you do not write to a cacheline simultaneously using DMA and CPU. This would be hard to guarantee if the crypto request is on the stack because it may share a cacheline with many other local variables.
Mikulas
Herbert Xu
maintainerPATCH v2 2/2 crypto: s390/phmac - Allow stack requests
In reply to Herbert XuAs phmac does not perform DMA, it can support stack requests.
--- arch/s390/crypto/phmac_s390.c | 1 + 1 file changed, 1 insertion(+)
arch/s390/crypto/phmac_s390.c +1 -0
@@ -932,6 +932,7 @@ static int phmac_do_one_request(struct crypto_engine *engine, void *areq) .cra_blocksize = SHA##x##_BLOCK_SIZE, \ .cra_priority = 400, \ .cra_flags = CRYPTO_ALG_ASYNC | \ + CRYPTO_AHASH_ALG_STACK_REQ |\ CRYPTO_ALG_NO_FALLBACK, \ .cra_ctxsize = sizeof(struct phmac_tfm_ctx), \ .cra_module = THIS_MODULE, \
Signature · 1 line
2.39.5