Skip to contentSkip to conversation

crypto: ahash - Allow async stack requests when specified

PATCH Superseded Herbert Xu 2 participants 5 messages Sep 9, 2025 – Sep 11, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Herbert Xu

author maintainer
#0 lore raw

cover crypto: ahash - Allow async stack requests when specified

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
author maintainer
#1 lore raw

PATCH 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
index a227793d2c5b..5ea72eb2ea91 100644
@@ -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
index 6ec5f2f37ccb..79899d36032b 100644
@@ -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
#2 lore raw
Quoted 4 lines · Herbert Xu

As it stands stack requests are forbidden for async algorithms because of the inability to perform DMA on stack memory.

That is not the only reason.

Quoted 5 lines

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.

If the algorithm is asynchronous, it may return -EINPROGRESS to the caller, and proceed to access the request structure after that. So this only works if the caller waits on the completion, and does not return (and release its stack frame) before the request is completed.

This makes this feature rather limited in usefulness imo - truly async hashes are only performant (if they ever are) if many requests can be in flight at the same time, and using stack requests makes that impossible.

Quoted 85 lines
---
 crypto/ahash.c                 | 22 ++++++++++++++++++----
 include/crypto/internal/hash.h |  3 +++
 2 files changed, 21 insertions(+), 4 deletions(-)
crypto/ahash.c +18 -4
index a227793d2c5b..5ea72eb2ea91 100644
@@ -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 -1
index 6ec5f2f37ccb..79899d36032b 100644
@@ -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; \
--

2.39.5

author maintainer
#3 lore raw
Quoted 10 lines · Ard Biesheuvel

If the algorithm is asynchronous, it may return -EINPROGRESS to the caller, and proceed to access the request structure after that. So this only works if the caller waits on the completion, and does not return (and release its stack frame) before the request is completed.

This makes this feature rather limited in usefulness imo - truly async hashes are only performant (if they ever are) if many requests can be in flight at the same time, and using stack requests makes that impossible.

This patch was meant to deal with the storage subsystem usage of ahash where the caller always waited for the hash to complete synchronously. In that particular context, it would be safe to use a request object on the stack if it was not also used for DMA.

Of course it turned out that this won't be useful for other reasons, so I'll just drop this.

Thanks,

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
author maintainer
#4 lore raw

PATCH 2/2 crypto: s390/phmac - Allow stack requests

In reply to Herbert Xu

As 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
index 7ecfdc4fba2d..ead4c0d3523e 100644
@@ -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