[RFC PATCH 1/3] crypto: Add pre_alloc and post_free callbacks for acomp algorithms

From: Andre Glover
Date: Wed May 01 2024 - 17:46:49 EST


Add callbacks to acomp crypto algorithms that facilitate the allocation and
subsequent freeing of resources required by an acomp_req before and after
a series of compress and/or decompress operations.

Signed-off-by: Andre Glover <andre.glover@xxxxxxxxxxxxxxx>
---
crypto/acompress.c | 13 +++++++++++++
include/crypto/acompress.h | 2 ++
include/crypto/internal/acompress.h | 6 ++++++
3 files changed, 21 insertions(+)

diff --git a/crypto/acompress.c b/crypto/acompress.c
index 6fdf0ff9f3c0..873918be75cc 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -71,9 +71,13 @@ static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)

acomp->compress = alg->compress;
acomp->decompress = alg->decompress;
+
acomp->dst_free = alg->dst_free;
acomp->reqsize = alg->reqsize;

+ acomp->pre_alloc = alg->pre_alloc;
+ acomp->post_free = alg->post_free;
+
if (alg->exit)
acomp->base.exit = crypto_acomp_exit_tfm;

@@ -129,6 +133,12 @@ struct acomp_req *acomp_request_alloc(struct crypto_acomp *acomp)
struct acomp_req *req;

req = __acomp_request_alloc(acomp);
+
+ if (req && (acomp->pre_alloc && acomp->pre_alloc(req))) {
+ __acomp_request_free(req);
+ return NULL;
+ }
+
if (req && (tfm->__crt_alg->cra_type != &crypto_acomp_type))
return crypto_acomp_scomp_alloc_ctx(req);

@@ -144,6 +154,9 @@ void acomp_request_free(struct acomp_req *req)
if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
crypto_acomp_scomp_free_ctx(req);

+ if (acomp->post_free)
+ acomp->post_free(req);
+
if (req->flags & CRYPTO_ACOMP_ALLOC_OUTPUT) {
acomp->dst_free(req->dst);
req->dst = NULL;
diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h
index 54937b615239..2b73cef2f430 100644
--- a/include/crypto/acompress.h
+++ b/include/crypto/acompress.h
@@ -51,6 +51,8 @@ struct acomp_req {
struct crypto_acomp {
int (*compress)(struct acomp_req *req);
int (*decompress)(struct acomp_req *req);
+ int (*pre_alloc)(struct acomp_req *req);
+ void (*post_free)(struct acomp_req *req);
void (*dst_free)(struct scatterlist *dst);
unsigned int reqsize;
struct crypto_tfm base;
diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h
index d00392d1988e..081e1cf5235f 100644
--- a/include/crypto/internal/acompress.h
+++ b/include/crypto/internal/acompress.h
@@ -29,6 +29,10 @@
* @exit: Deinitialize the cryptographic transformation object. This is a
* counterpart to @init, used to remove various changes set in
* @init.
+ * @pre_alloc: Function that performs any pre-allocation and setup that an
+ * algorithm may require on a per req basis.
+ * @post_free: Function that performs any post freeing that an algorithm
+ * may require on a per req basis.
*
* @reqsize: Context size for (de)compression requests
* @base: Common crypto API algorithm data structure
@@ -40,6 +44,8 @@ struct acomp_alg {
void (*dst_free)(struct scatterlist *dst);
int (*init)(struct crypto_acomp *tfm);
void (*exit)(struct crypto_acomp *tfm);
+ int (*pre_alloc)(struct acomp_req *req);
+ void (*post_free)(struct acomp_req *req);

unsigned int reqsize;

--
2.27.0