[PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.

Kanchana P Sridhar posted 24 patches 2 months ago
There is a newer version of this series
[PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Kanchana P Sridhar 2 months ago
This commit adds a get_batch_size() interface to:

  struct acomp_alg
  struct crypto_acomp

A crypto_acomp compression algorithm that supports batching of compressions
and decompressions must provide an implementation for this API to return
the maximum batch-size that the compressor supports, so that kernel
users of crypto_acomp, such as zswap, can allocate resources for
submitting multiple compress/decompress jobs that can be batched, and
invoke batching of [de]compressions.

A new helper function acomp_has_async_batching() can be invoked to query
if a crypto_acomp implements get_batch_size().

The new crypto_acomp_batch_size() API uses this helper function to return
the batch-size for compressors that implement get_batch_size(). If no
implementation is provided by the crypto_acomp, a default of "1" is
returned for the batch-size.

zswap can invoke crypto_acomp_batch_size() to query the maximum number
of requests that can be batch [de]compressed. Based on this, zswap
can use the minimum of any zswap-specific upper limits for batch-size
and the compressor's max batch-size, to allocate batching resources.

Signed-off-by: Kanchana P Sridhar <kanchana.p.sridhar@intel.com>
---
 crypto/acompress.c                  |  1 +
 include/crypto/acompress.h          | 27 +++++++++++++++++++++++++++
 include/crypto/internal/acompress.h |  3 +++
 3 files changed, 31 insertions(+)

diff --git a/crypto/acompress.c b/crypto/acompress.c
index be28cbfd22e32..f440724719655 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -105,6 +105,7 @@ static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
 
 	acomp->compress = alg->compress;
 	acomp->decompress = alg->decompress;
+	acomp->get_batch_size = alg->get_batch_size;
 	acomp->reqsize = alg->base.cra_reqsize;
 
 	acomp->base.exit = crypto_acomp_exit_tfm;
diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h
index 0312322d2ca03..898104745cd24 100644
--- a/include/crypto/acompress.h
+++ b/include/crypto/acompress.h
@@ -108,6 +108,8 @@ struct acomp_req {
  *
  * @compress:		Function performs a compress operation
  * @decompress:		Function performs a de-compress operation
+ * @get_batch_size:	Maximum batch-size for batching compress/decompress
+ *			operations.
  * @reqsize:		Context size for (de)compression requests
  * @fb:			Synchronous fallback tfm
  * @base:		Common crypto API algorithm data structure
@@ -115,6 +117,7 @@ struct acomp_req {
 struct crypto_acomp {
 	int (*compress)(struct acomp_req *req);
 	int (*decompress)(struct acomp_req *req);
+	unsigned int (*get_batch_size)(void);
 	unsigned int reqsize;
 	struct crypto_tfm base;
 };
@@ -205,6 +208,13 @@ static inline bool acomp_is_async(struct crypto_acomp *tfm)
 	       CRYPTO_ALG_ASYNC;
 }
 
+static inline bool acomp_has_async_batching(struct crypto_acomp *tfm)
+{
+	return (acomp_is_async(tfm) &&
+		(crypto_comp_alg_common(tfm)->base.cra_flags & CRYPTO_ALG_TYPE_ACOMPRESS) &&
+		tfm->get_batch_size);
+}
+
 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
 {
 	return __crypto_acomp_tfm(req->base.tfm);
@@ -545,6 +555,23 @@ int crypto_acomp_compress(struct acomp_req *req);
  */
 int crypto_acomp_decompress(struct acomp_req *req);
 
+/**
+ * crypto_acomp_batch_size() -- Get the algorithm's batch size
+ *
+ * Function returns the algorithm's batch size for batching operations
+ *
+ * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
+ *
+ * Return:	crypto_acomp's batch size.
+ */
+static inline unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm)
+{
+	if (acomp_has_async_batching(tfm))
+		return tfm->get_batch_size();
+
+	return 1;
+}
+
 static inline struct acomp_req *acomp_request_on_stack_init(
 	char *buf, struct crypto_acomp *tfm)
 {
diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h
index ffffd88bbbad3..2325ee18e7a10 100644
--- a/include/crypto/internal/acompress.h
+++ b/include/crypto/internal/acompress.h
@@ -28,6 +28,8 @@
  *
  * @compress:	Function performs a compress operation
  * @decompress:	Function performs a de-compress operation
+ * @get_batch_size:	Maximum batch-size for batching compress/decompress
+ *			operations.
  * @init:	Initialize the cryptographic transformation object.
  *		This function is used to initialize the cryptographic
  *		transformation object. This function is called only once at
@@ -46,6 +48,7 @@
 struct acomp_alg {
 	int (*compress)(struct acomp_req *req);
 	int (*decompress)(struct acomp_req *req);
+	unsigned int (*get_batch_size)(void);
 	int (*init)(struct crypto_acomp *tfm);
 	void (*exit)(struct crypto_acomp *tfm);
 
-- 
2.27.0
Re: [PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Herbert Xu 1 month, 3 weeks ago
On Thu, Jul 31, 2025 at 09:36:36PM -0700, Kanchana P Sridhar wrote:
>
> diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h
> index ffffd88bbbad3..2325ee18e7a10 100644
> --- a/include/crypto/internal/acompress.h
> +++ b/include/crypto/internal/acompress.h
> @@ -28,6 +28,8 @@
>   *
>   * @compress:	Function performs a compress operation
>   * @decompress:	Function performs a de-compress operation
> + * @get_batch_size:	Maximum batch-size for batching compress/decompress
> + *			operations.
>   * @init:	Initialize the cryptographic transformation object.
>   *		This function is used to initialize the cryptographic
>   *		transformation object. This function is called only once at
> @@ -46,6 +48,7 @@
>  struct acomp_alg {
>  	int (*compress)(struct acomp_req *req);
>  	int (*decompress)(struct acomp_req *req);
> +	unsigned int (*get_batch_size)(void);

I can't imagine a situation where this needs to be dynamic.
Please just make it a static value rather than a callback function.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
RE: [PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Sridhar, Kanchana P 1 month, 1 week ago
> -----Original Message-----
> From: Herbert Xu <herbert@gondor.apana.org.au>
> Sent: Thursday, August 14, 2025 10:29 PM
> To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> Cc: linux-kernel@vger.kernel.org; linux-mm@kvack.org;
> hannes@cmpxchg.org; yosry.ahmed@linux.dev; nphamcs@gmail.com;
> chengming.zhou@linux.dev; usamaarif642@gmail.com;
> ryan.roberts@arm.com; 21cnbao@gmail.com;
> ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> <kristen.c.accardi@intel.com>; Gomes, Vinicius <vinicius.gomes@intel.com>;
> Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> <vinodh.gopal@intel.com>
> Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> crypto_acomp_batch_size() to get an algorithm's batch-size.
> 
> On Thu, Jul 31, 2025 at 09:36:36PM -0700, Kanchana P Sridhar wrote:
> >
> > diff --git a/include/crypto/internal/acompress.h
> b/include/crypto/internal/acompress.h
> > index ffffd88bbbad3..2325ee18e7a10 100644
> > --- a/include/crypto/internal/acompress.h
> > +++ b/include/crypto/internal/acompress.h
> > @@ -28,6 +28,8 @@
> >   *
> >   * @compress:	Function performs a compress operation
> >   * @decompress:	Function performs a de-compress operation
> > + * @get_batch_size:	Maximum batch-size for batching
> compress/decompress
> > + *			operations.
> >   * @init:	Initialize the cryptographic transformation object.
> >   *		This function is used to initialize the cryptographic
> >   *		transformation object. This function is called only once at
> > @@ -46,6 +48,7 @@
> >  struct acomp_alg {
> >  	int (*compress)(struct acomp_req *req);
> >  	int (*decompress)(struct acomp_req *req);
> > +	unsigned int (*get_batch_size)(void);
> 
> I can't imagine a situation where this needs to be dynamic.
> Please just make it a static value rather than a callback function.

Hi Herbert,

I am not sure I understand.. Kernel users such as zswap/zram need to query
the algorithm to get the maximum supported batch-size so they can allocate
resources for dst buffers. The get_batch_size() callback and associated
crypto_acomp_batch_size() wrapper help accomplish this.

Can you please clarify what you mean by "static value"?

Thanks,
Kanchana

> 
> Thanks,
> --
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Nhat Pham 1 month, 1 week ago
On Fri, Aug 22, 2025 at 12:31 PM Sridhar, Kanchana P
<kanchana.p.sridhar@intel.com> wrote:
>
>
> > -----Original Message-----
> > From: Herbert Xu <herbert@gondor.apana.org.au>
> > Sent: Thursday, August 14, 2025 10:29 PM
> > To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> > Cc: linux-kernel@vger.kernel.org; linux-mm@kvack.org;
> > hannes@cmpxchg.org; yosry.ahmed@linux.dev; nphamcs@gmail.com;
> > chengming.zhou@linux.dev; usamaarif642@gmail.com;
> > ryan.roberts@arm.com; 21cnbao@gmail.com;
> > ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> > senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> > davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> > ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> > <kristen.c.accardi@intel.com>; Gomes, Vinicius <vinicius.gomes@intel.com>;
> > Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> > <vinodh.gopal@intel.com>
> > Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> > crypto_acomp_batch_size() to get an algorithm's batch-size.
> >
> > On Thu, Jul 31, 2025 at 09:36:36PM -0700, Kanchana P Sridhar wrote:
> > >
> > > diff --git a/include/crypto/internal/acompress.h
> > b/include/crypto/internal/acompress.h
> > > index ffffd88bbbad3..2325ee18e7a10 100644
> > > --- a/include/crypto/internal/acompress.h
> > > +++ b/include/crypto/internal/acompress.h
> > > @@ -28,6 +28,8 @@
> > >   *
> > >   * @compress:      Function performs a compress operation
> > >   * @decompress:    Function performs a de-compress operation
> > > + * @get_batch_size:        Maximum batch-size for batching
> > compress/decompress
> > > + *                 operations.
> > >   * @init:  Initialize the cryptographic transformation object.
> > >   *         This function is used to initialize the cryptographic
> > >   *         transformation object. This function is called only once at
> > > @@ -46,6 +48,7 @@
> > >  struct acomp_alg {
> > >     int (*compress)(struct acomp_req *req);
> > >     int (*decompress)(struct acomp_req *req);
> > > +   unsigned int (*get_batch_size)(void);
> >
> > I can't imagine a situation where this needs to be dynamic.
> > Please just make it a static value rather than a callback function.
>
> Hi Herbert,
>
> I am not sure I understand.. Kernel users such as zswap/zram need to query
> the algorithm to get the maximum supported batch-size so they can allocate
> resources for dst buffers. The get_batch_size() callback and associated
> crypto_acomp_batch_size() wrapper help accomplish this.

I think he meant stored it as a static unsigned int field, rather than
a function pointer (i.e dynamic) like this.

Does batch size ever change at runtime?
RE: [PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Sridhar, Kanchana P 1 month, 1 week ago
> -----Original Message-----
> From: Nhat Pham <nphamcs@gmail.com>
> Sent: Friday, August 22, 2025 2:48 PM
> To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>; linux-
> kernel@vger.kernel.org; linux-mm@kvack.org; hannes@cmpxchg.org;
> yosry.ahmed@linux.dev; chengming.zhou@linux.dev;
> usamaarif642@gmail.com; ryan.roberts@arm.com; 21cnbao@gmail.com;
> ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> <kristen.c.accardi@intel.com>; Gomes, Vinicius <vinicius.gomes@intel.com>;
> Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> <vinodh.gopal@intel.com>
> Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> crypto_acomp_batch_size() to get an algorithm's batch-size.
> 
> On Fri, Aug 22, 2025 at 12:31 PM Sridhar, Kanchana P
> <kanchana.p.sridhar@intel.com> wrote:
> >
> >
> > > -----Original Message-----
> > > From: Herbert Xu <herbert@gondor.apana.org.au>
> > > Sent: Thursday, August 14, 2025 10:29 PM
> > > To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> > > Cc: linux-kernel@vger.kernel.org; linux-mm@kvack.org;
> > > hannes@cmpxchg.org; yosry.ahmed@linux.dev; nphamcs@gmail.com;
> > > chengming.zhou@linux.dev; usamaarif642@gmail.com;
> > > ryan.roberts@arm.com; 21cnbao@gmail.com;
> > > ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> > > senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> > > davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> > > ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> > > <kristen.c.accardi@intel.com>; Gomes, Vinicius
> <vinicius.gomes@intel.com>;
> > > Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> > > <vinodh.gopal@intel.com>
> > > Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> > > crypto_acomp_batch_size() to get an algorithm's batch-size.
> > >
> > > On Thu, Jul 31, 2025 at 09:36:36PM -0700, Kanchana P Sridhar wrote:
> > > >
> > > > diff --git a/include/crypto/internal/acompress.h
> > > b/include/crypto/internal/acompress.h
> > > > index ffffd88bbbad3..2325ee18e7a10 100644
> > > > --- a/include/crypto/internal/acompress.h
> > > > +++ b/include/crypto/internal/acompress.h
> > > > @@ -28,6 +28,8 @@
> > > >   *
> > > >   * @compress:      Function performs a compress operation
> > > >   * @decompress:    Function performs a de-compress operation
> > > > + * @get_batch_size:        Maximum batch-size for batching
> > > compress/decompress
> > > > + *                 operations.
> > > >   * @init:  Initialize the cryptographic transformation object.
> > > >   *         This function is used to initialize the cryptographic
> > > >   *         transformation object. This function is called only once at
> > > > @@ -46,6 +48,7 @@
> > > >  struct acomp_alg {
> > > >     int (*compress)(struct acomp_req *req);
> > > >     int (*decompress)(struct acomp_req *req);
> > > > +   unsigned int (*get_batch_size)(void);
> > >
> > > I can't imagine a situation where this needs to be dynamic.
> > > Please just make it a static value rather than a callback function.
> >
> > Hi Herbert,
> >
> > I am not sure I understand.. Kernel users such as zswap/zram need to query
> > the algorithm to get the maximum supported batch-size so they can allocate
> > resources for dst buffers. The get_batch_size() callback and associated
> > crypto_acomp_batch_size() wrapper help accomplish this.
> 
> I think he meant stored it as a static unsigned int field, rather than
> a function pointer (i.e dynamic) like this.

I see. Got it! Sure, I will make this change in v12. Thanks Nhat!

Best regards,
Kanchana

> 
> Does batch size ever change at runtime?
RE: [PATCH v11 18/24] crypto: acomp - Add crypto_acomp_batch_size() to get an algorithm's batch-size.
Posted by Sridhar, Kanchana P 1 month, 1 week ago
> -----Original Message-----
> From: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> Sent: Friday, August 22, 2025 2:58 PM
> To: Nhat Pham <nphamcs@gmail.com>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>; linux-
> kernel@vger.kernel.org; linux-mm@kvack.org; hannes@cmpxchg.org;
> yosry.ahmed@linux.dev; chengming.zhou@linux.dev;
> usamaarif642@gmail.com; ryan.roberts@arm.com; 21cnbao@gmail.com;
> ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> <kristen.c.accardi@intel.com>; Gomes, Vinicius <vinicius.gomes@intel.com>;
> Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> <vinodh.gopal@intel.com>; Sridhar, Kanchana P
> <kanchana.p.sridhar@intel.com>
> Subject: RE: [PATCH v11 18/24] crypto: acomp - Add
> crypto_acomp_batch_size() to get an algorithm's batch-size.
> 
> 
> > -----Original Message-----
> > From: Nhat Pham <nphamcs@gmail.com>
> > Sent: Friday, August 22, 2025 2:48 PM
> > To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> > Cc: Herbert Xu <herbert@gondor.apana.org.au>; linux-
> > kernel@vger.kernel.org; linux-mm@kvack.org; hannes@cmpxchg.org;
> > yosry.ahmed@linux.dev; chengming.zhou@linux.dev;
> > usamaarif642@gmail.com; ryan.roberts@arm.com; 21cnbao@gmail.com;
> > ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> > senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> > davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> > ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> > <kristen.c.accardi@intel.com>; Gomes, Vinicius
> <vinicius.gomes@intel.com>;
> > Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> > <vinodh.gopal@intel.com>
> > Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> > crypto_acomp_batch_size() to get an algorithm's batch-size.
> >
> > On Fri, Aug 22, 2025 at 12:31 PM Sridhar, Kanchana P
> > <kanchana.p.sridhar@intel.com> wrote:
> > >
> > >
> > > > -----Original Message-----
> > > > From: Herbert Xu <herbert@gondor.apana.org.au>
> > > > Sent: Thursday, August 14, 2025 10:29 PM
> > > > To: Sridhar, Kanchana P <kanchana.p.sridhar@intel.com>
> > > > Cc: linux-kernel@vger.kernel.org; linux-mm@kvack.org;
> > > > hannes@cmpxchg.org; yosry.ahmed@linux.dev; nphamcs@gmail.com;
> > > > chengming.zhou@linux.dev; usamaarif642@gmail.com;
> > > > ryan.roberts@arm.com; 21cnbao@gmail.com;
> > > > ying.huang@linux.alibaba.com; akpm@linux-foundation.org;
> > > > senozhatsky@chromium.org; linux-crypto@vger.kernel.org;
> > > > davem@davemloft.net; clabbe@baylibre.com; ardb@kernel.org;
> > > > ebiggers@google.com; surenb@google.com; Accardi, Kristen C
> > > > <kristen.c.accardi@intel.com>; Gomes, Vinicius
> > <vinicius.gomes@intel.com>;
> > > > Feghali, Wajdi K <wajdi.k.feghali@intel.com>; Gopal, Vinodh
> > > > <vinodh.gopal@intel.com>
> > > > Subject: Re: [PATCH v11 18/24] crypto: acomp - Add
> > > > crypto_acomp_batch_size() to get an algorithm's batch-size.
> > > >
> > > > On Thu, Jul 31, 2025 at 09:36:36PM -0700, Kanchana P Sridhar wrote:
> > > > >
> > > > > diff --git a/include/crypto/internal/acompress.h
> > > > b/include/crypto/internal/acompress.h
> > > > > index ffffd88bbbad3..2325ee18e7a10 100644
> > > > > --- a/include/crypto/internal/acompress.h
> > > > > +++ b/include/crypto/internal/acompress.h
> > > > > @@ -28,6 +28,8 @@
> > > > >   *
> > > > >   * @compress:      Function performs a compress operation
> > > > >   * @decompress:    Function performs a de-compress operation
> > > > > + * @get_batch_size:        Maximum batch-size for batching
> > > > compress/decompress
> > > > > + *                 operations.
> > > > >   * @init:  Initialize the cryptographic transformation object.
> > > > >   *         This function is used to initialize the cryptographic
> > > > >   *         transformation object. This function is called only once at
> > > > > @@ -46,6 +48,7 @@
> > > > >  struct acomp_alg {
> > > > >     int (*compress)(struct acomp_req *req);
> > > > >     int (*decompress)(struct acomp_req *req);
> > > > > +   unsigned int (*get_batch_size)(void);
> > > >
> > > > I can't imagine a situation where this needs to be dynamic.
> > > > Please just make it a static value rather than a callback function.
> > >
> > > Hi Herbert,
> > >
> > > I am not sure I understand.. Kernel users such as zswap/zram need to
> query
> > > the algorithm to get the maximum supported batch-size so they can
> allocate
> > > resources for dst buffers. The get_batch_size() callback and associated
> > > crypto_acomp_batch_size() wrapper help accomplish this.
> >
> > I think he meant stored it as a static unsigned int field, rather than
> > a function pointer (i.e dynamic) like this.
> 
> I see. Got it! Sure, I will make this change in v12. Thanks Nhat!
> 
> Best regards,
> Kanchana
> 
> >
> > Does batch size ever change at runtime?

No, batch size doesn't change at runtime.

Thanks,
Kanchana