Skip to contentSkip to conversation

crypto: acomp - Add segmentation API for compression

PATCH In review Herbert Xu 3 participants 10 messages Dec 26, 2025 – Jan 14 Crypto API

lore
Share & tools
lore

Herbert Xu

author maintainer
#0 lore raw

cover crypto: acomp - Add segmentation API for compression

This patch adds segmentation support for compression and provides a wrapper for existing algorithms so that they can accept a single segment while returning the compressed length or error through the dst SG list length.

Herbert Xu (2):
crypto: acomp - Add bit to indicate segmentation support
crypto: acomp - Add trivial segmentation wrapper

Kanchana P Sridhar (1):
crypto: acomp - Define a unit_size in struct acomp_req to enable
batching.

 crypto/acompress.c                  | 25 ++++++++++++++++---
 include/crypto/acompress.h          | 37 +++++++++++++++++++++++++++++
 include/crypto/algapi.h             |  5 ++++
 include/crypto/internal/acompress.h |  5 ++++
 include/linux/crypto.h              |  3 +++
 5 files changed, 72 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/3 crypto: acomp - Define a unit_size in struct acomp_req to enable batching.

From: Kanchana P Sridhar <[email protected]>

We add a new @unit_size data member to struct acomp_req along with a helper function acomp_request_set_unit_size() for kernel modules to set the unit size to use while breaking down the request's src/dst scatterlists.

An acomp_alg can implement batching by using the @req->unit_size to break down the SG lists passed in via @req->dst and/or @req->src, to submit individual @req->slen/@req->unit_size compress jobs or @req->dlen/@req->unit_size decompress jobs, for batch compression and batch decompression respectively.

In case of batch compression, the folio's pages for the batch can be retrieved from the @req->src scatterlist by using an struct sg_page_iter after determining the number of pages as @req->slen/@req->unit_size.

---
 include/crypto/acompress.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
include/crypto/acompress.h +36 -0
index 9eacb9fa375d..0f1334168f1b 100644
@@ -79,6 +79,7 @@ struct acomp_req_chain {
  * @dvirt:	Destination virtual address
  * @slen:	Size of the input buffer
  * @dlen:	Size of the output buffer and number of bytes produced
+ * @unit_size:  Unit size for the request for use in batching
  * @chain:	Private API code data, do not use
  * @__ctx:	Start of private context data
  */
@@ -94,6 +95,7 @@ struct acomp_req {
 	};
 	unsigned int slen;
 	unsigned int dlen;
+	unsigned int unit_size;
 
 	struct acomp_req_chain chain;
 
@@ -328,9 +330,43 @@ static inline void acomp_request_set_callback(struct acomp_req *req,
 {
 	flgs &= ~CRYPTO_ACOMP_REQ_PRIVATE;
 	flgs |= req->base.flags & CRYPTO_ACOMP_REQ_PRIVATE;
+	req->unit_size = 0;
 	crypto_request_set_callback(&req->base, flgs, cmpl, data);
 }
 
+/**
+ * acomp_request_set_unit_size() -- Sets the unit size for the request.
+ *
+ * As suggested by Herbert Xu, this is a new helper function that enables
+ * batching for zswap, IPComp, etc.
+ *
+ * Example usage model:
+ *
+ * A module like zswap that wants to use batch compression of @nr_pages with
+ * crypto_acomp must create an output SG table for the batch, initialized to
+ * contain @nr_pages SG lists. Each scatterlist is mapped to the nth
+ * destination buffer for the batch.
+ *
+ * An acomp_alg can implement batching by using the @req->unit_size to
+ * break down the SG lists passed in via @req->dst and/or @req->src, to
+ * submit individual @req->slen/@req->unit_size compress jobs or
+ * @req->dlen/@req->unit_size decompress jobs, for batch compression and
+ * batch decompression respectively.
+ *
+ * This API must be called after acomp_request_set_callback(),
+ * which sets @req->unit_size to 0.
+ *
+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.
+ *
+ * @req:	asynchronous compress request
+ * @du:		data unit size of the input buffer scatterlist.
+ */
+static inline void acomp_request_set_unit_size(struct acomp_req *req,
+					       unsigned int du)
+{
+	req->unit_size = du;
+}
+
 /**
  * acomp_request_set_params() -- Sets request parameters
  *
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/3] crypto: acomp - Define a unit_size in struct acomp_req to enable batching.

Quoted 13 lines · Herbert Xu

From: Kanchana P Sridhar <[email protected]>

We add a new @unit_size data member to struct acomp_req along with a helper function acomp_request_set_unit_size() for kernel modules to set the unit size to use while breaking down the request's src/dst scatterlists.

An acomp_alg can implement batching by using the @req->unit_size to break down the SG lists passed in via @req->dst and/or @req->src, to submit individual @req->slen/@req->unit_size compress jobs or @req->dlen/@req->unit_size decompress jobs, for batch compression and batch decompression respectively.

Quoted 8 lines

In case of batch compression, the folio's pages for the batch can be retrieved from the @req->src scatterlist by using an struct sg_page_iter after determining the number of pages as @req->slen/@req->unit_size.

---

A few comments below.

include/crypto/acompress.h struct acomp_req_chain { · line 82

Quoted 12 lines
 include/crypto/acompress.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
include/crypto/acompress.h +1 -0
index 9eacb9fa375d..0f1334168f1b 100644
@@ -79,6 +79,7 @@ struct acomp_req_chain {
  * @dvirt:	Destination virtual address
  * @slen:	Size of the input buffer
  * @dlen:	Size of the output buffer and number of bytes produced
+ * @unit_size:  Unit size for the request for use in batching

NIT: use a tab after the colon for consistency with the other fields.

Quoted 14 lines
include/crypto/acompress.h +1 -0
  * @chain:	Private API code data, do not use
  * @__ctx:	Start of private context data
  */
@@ -94,6 +95,7 @@ struct acomp_req {
 	};
 	unsigned int slen;
 	unsigned int dlen;
+	unsigned int unit_size;
 
 	struct acomp_req_chain chain;
 
@@ -328,9 +330,43 @@ static inline void acomp_request_set_callback(struct acomp_req *req,
 {
 	flgs &= ~CRYPTO_ACOMP_REQ_PRIVATE;

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req, · line 341

Quoted 10 lines
include/crypto/acompress.h +6 -0
 	flgs |= req->base.flags & CRYPTO_ACOMP_REQ_PRIVATE;
+	req->unit_size = 0;
 	crypto_request_set_callback(&req->base, flgs, cmpl, data);
 }
 
+/**
+ * acomp_request_set_unit_size() -- Sets the unit size for the request.
+ *
+ * As suggested by Herbert Xu, this is a new helper function that enables
+ * batching for zswap, IPComp, etc.

This sentence should be removed. We shouldn't be mentioning on the documentation that this function is new or who suggested the change.

Instead, explain what batching actually means in this context: that it allows multiple independent compression (or decompression) operations to be submitted in a single request, where each segment is processed independently. It would also be helpful to clarify that each unit has the same size for source and destination, and that each segment must be physically contiguous, if that is the case.

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req, · line 345

Quoted 4 lines
include/crypto/acompress.h +4 -0
+ *
+ * Example usage model:
+ *
+ * A module like zswap that wants to use batch compression of @nr_pages with

Pages or folios?

Also, @nr_pages is not a parameter of this function. Is the usage of @ correct?

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req, · line 357

Quoted 12 lines
include/crypto/acompress.h +12 -0
+ * crypto_acomp must create an output SG table for the batch, initialized to
+ * contain @nr_pages SG lists. Each scatterlist is mapped to the nth
+ * destination buffer for the batch.
+ *
+ * An acomp_alg can implement batching by using the @req->unit_size to
+ * break down the SG lists passed in via @req->dst and/or @req->src, to
+ * submit individual @req->slen/@req->unit_size compress jobs or
+ * @req->dlen/@req->unit_size decompress jobs, for batch compression and
+ * batch decompression respectively.
+ *
+ * This API must be called after acomp_request_set_callback(),
+ * which sets @req->unit_size to 0.

This mentions a constraint, but does not explain why. Explain why this restriction is needed.

Quoted 18 lines
include/crypto/acompress.h +12 -0
+ *
+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.
+ *
+ * @req:	asynchronous compress request
+ * @du:		data unit size of the input buffer scatterlist.
+ */
+static inline void acomp_request_set_unit_size(struct acomp_req *req,
+					       unsigned int du)
+{
+	req->unit_size = du;
+}
+
 /**
  * acomp_request_set_params() -- Sets request parameters
  *
Signature · 1 line
2.47.3

Thanks,

Signature · 1 line
Giovanni
#3 lore raw

RE: [PATCH 1/3] crypto: acomp - Define a unit_size in struct acomp_req to enable batching.

Quoted 29 lines

-----Original Message-----
From: Cabiddu, Giovanni <[email protected]>
Sent: Monday, December 29, 2025 3:07 AM
To: Herbert Xu <[email protected]>
Cc: Linux Crypto Mailing List <[email protected]>; [email protected]; [email protected]; [email protected]; Sridhar, Kanchana P <[email protected]>
Subject: Re: [PATCH 1/3] crypto: acomp - Define a unit_size in struct acomp_req to enable batching.

Quoted 17 lines · Herbert Xu

From: Kanchana P Sridhar <[email protected]>

We add a new @unit_size data member to struct acomp_req along with a helper function acomp_request_set_unit_size() for kernel modules to set the unit size to use while breaking down the request's src/dst scatterlists.

An acomp_alg can implement batching by using the @req->unit_size to break down the SG lists passed in via @req->dst and/or @req->src, to submit individual @req->slen/@req->unit_size compress jobs or @req->dlen/@req->unit_size decompress jobs, for batch compression and batch decompression respectively.

In case of batch compression, the folio's pages for the batch can be retrieved from the @req->src scatterlist by using an struct sg_page_iter after determining the number of pages as @req->slen/@req->unit_size.

Quoted 6 lines
Quoted 4 lines

---

A few comments below.

Hi Giovanni,

Thanks for the code review comments.

Quoted 5 lines
Quoted 2 lines
 include/crypto/acompress.h | 36

++++++++++++++++++++++++++++++++++++

Quoted 2 lines
 1 file changed, 36 insertions(+)
Quoted 10 lines

include/crypto/acompress.h struct acomp_req_chain { · line 82

Quoted 9 lines
include/crypto/acompress.h +1 -0
index 9eacb9fa375d..0f1334168f1b 100644
@@ -79,6 +79,7 @@ struct acomp_req_chain {
  * @dvirt:	Destination virtual address
  * @slen:	Size of the input buffer
  * @dlen:	Size of the output buffer and number of bytes produced
+ * @unit_size:  Unit size for the request for use in batching

NIT: use a tab after the colon for consistency with the other fields.

Sure.

Quoted 21 lines

include/crypto/acompress.h static inline void

Quoted 13 lines
include/crypto/acompress.h +1 -0
  * @chain:	Private API code data, do not use
  * @__ctx:	Start of private context data
  */
@@ -94,6 +95,7 @@ struct acomp_req {
 	};
 	unsigned int slen;
 	unsigned int dlen;
+	unsigned int unit_size;
 
 	struct acomp_req_chain chain;
 
@@ -328,9 +330,43 @@ static inline void

acomp_request_set_callback(struct acomp_req *req,

Quoted 7 lines
include/crypto/acompress.h +1 -0
 {
 	flgs &= ~CRYPTO_ACOMP_REQ_PRIVATE;
 	flgs |= req->base.flags & CRYPTO_ACOMP_REQ_PRIVATE;
+	req->unit_size = 0;
 	crypto_request_set_callback(&req->base, flgs, cmpl, data);
 }
 
Quoted 7 lines

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req, · line 341

Quoted 5 lines
include/crypto/acompress.h +5 -0
+/**
+ * acomp_request_set_unit_size() -- Sets the unit size for the request.
+ *
+ * As suggested by Herbert Xu, this is a new helper function that enables
+ * batching for zswap, IPComp, etc.

This sentence should be removed. We shouldn't be mentioning on the documentation that this function is new or who suggested the change.

Sounds good.

Quoted 7 lines

Instead, explain what batching actually means in this context: that it allows multiple independent compression (or decompression) operations to be submitted in a single request, where each segment is processed independently. It would also be helpful to clarify that each unit has the same size for source and destination, and that each segment must be physically contiguous, if that is the case.

Ok, I will add the appropriate explanations in this context.

Thanks,
Kanchana

Quoted 50 lines

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req,

Quoted 5 lines
include/crypto/acompress.h +4 -0
+ *
+ * Example usage model:
+ *
+ * A module like zswap that wants to use batch compression of @nr_pages

with
Pages or folios?

Also, @nr_pages is not a parameter of this function. Is the usage of @ correct?

include/crypto/acompress.h static inline void · line 346

Quoted 1 line
include/crypto/acompress.h +1 -0
+ * crypto_acomp must create an output SG table for the batch, initialized

to

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req,

Quoted 7 lines
include/crypto/acompress.h +7 -0
+ * contain @nr_pages SG lists. Each scatterlist is mapped to the nth
+ * destination buffer for the batch.
+ *
+ * An acomp_alg can implement batching by using the @req->unit_size to
+ * break down the SG lists passed in via @req->dst and/or @req->src, to
+ * submit individual @req->slen/@req->unit_size compress jobs or
+ * @req->dlen/@req->unit_size decompress jobs, for batch compression

and

include/crypto/acompress.h static inline void acomp_request_set_callback(struct acomp_req *req, · line 357

Quoted 4 lines
include/crypto/acompress.h +4 -0
+ * batch decompression respectively.
+ *
+ * This API must be called after acomp_request_set_callback(),
+ * which sets @req->unit_size to 0.

This mentions a constraint, but does not explain why. Explain why this restriction is needed.

Quoted 16 lines
include/crypto/acompress.h +12 -1
+ *
+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.
+ *
+ * @req:	asynchronous compress request
+ * @du:		data unit size of the input buffer scatterlist.
+ */
+static inline void acomp_request_set_unit_size(struct acomp_req *req,
+					       unsigned int du)
+{
+	req->unit_size = du;
+}
+
 /**
  * acomp_request_set_params() -- Sets request parameters
  *
--
Quoted 2 lines

2.47.3

Thanks,

--
Giovanni

author maintainer
#4 lore raw

PATCH 2/3 crypto: acomp - Add bit to indicate segmentation support

In reply to Herbert Xu

Add a bit to the crypto_alg flags to indicate support for segmentation. Also add a helper for acomp to test whether a given tfm supports segmentation.

---
 include/crypto/algapi.h             | 5 +++++
 include/crypto/internal/acompress.h | 5 +++++
 include/linux/crypto.h              | 3 +++
 3 files changed, 13 insertions(+)
include/crypto/algapi.h +5 -0
index 05deea9dac5e..7d406cfe5751 100644
@@ -280,6 +280,11 @@ static inline bool crypto_tfm_req_virt(struct crypto_tfm *tfm)
 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_REQ_VIRT;
 }
 
+static inline bool crypto_tfm_req_seg(struct crypto_tfm *tfm)
+{
+	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_REQ_SEG;
+}
+
 static inline u32 crypto_request_flags(struct crypto_async_request *req)
 {
 	return req->flags & ~CRYPTO_TFM_REQ_ON_STACK;
include/crypto/internal/acompress.h +5 -0
index 2d97440028ff..366dbdb987e8 100644
@@ -188,6 +188,11 @@ static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm)
 	return crypto_tfm_req_virt(&tfm->base);
 }
 
+static inline bool crypto_acomp_req_seg(struct crypto_acomp *tfm)
+{
+	return crypto_tfm_req_seg(&tfm->base);
+}
+
 void crypto_acomp_free_streams(struct crypto_acomp_streams *s);
 int crypto_acomp_alloc_streams(struct crypto_acomp_streams *s);
 
include/linux/crypto.h +3 -0
index a2137e19be7d..89b9c3f87f4d 100644
@@ -139,6 +139,9 @@
 /* Set if the algorithm cannot have a fallback (e.g., phmac). */
 #define CRYPTO_ALG_NO_FALLBACK		0x00080000
 
+/* Set if the algorithm supports segmentation. */
+#define CRYPTO_ALG_REQ_SEG		0x00100000
+
 /* The high bits 0xff000000 are reserved for type-specific flags. */
 
 /*
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 3/3 crypto: acomp - Add trivial segmentation wrapper

In reply to Herbert Xu

Add a trivial segmentation wrapper that only supports compression with a segment count of exactly one.

The reason is that the first user zswap will only allocate the extra memory if the underlying algorithm supports segmentation, and otherwise only one segment will be given at a time.

Having this wrapper means that the same calling convention can be used for all algorithms, regardless of segmentation support.

---
 crypto/acompress.c         | 25 ++++++++++++++++++++++---
 include/crypto/acompress.h |  1 +
 2 files changed, 23 insertions(+), 3 deletions(-)
crypto/acompress.c +22 -3
index be28cbfd22e3..d97a90a5ee46 100644
@@ -170,8 +170,13 @@ static void acomp_save_req(struct acomp_req *req, crypto_completion_t cplt)
 
 	state->compl = req->base.complete;
 	state->data = req->base.data;
+	state->unit_size = req->unit_size;
+	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
+					  CRYPTO_ACOMP_REQ_DST_VIRT);
+
 	req->base.complete = cplt;
 	req->base.data = state;
+	req->unit_size = 0;
 }
 
 static void acomp_restore_req(struct acomp_req *req)
@@ -180,6 +185,7 @@ static void acomp_restore_req(struct acomp_req *req)
 
 	req->base.complete = state->compl;
 	req->base.data = state->data;
+	req->unit_size = state->unit_size;
 }
 
 static void acomp_reqchain_virt(struct acomp_req *req)
@@ -198,9 +204,6 @@ static void acomp_virt_to_sg(struct acomp_req *req)
 {
 	struct acomp_req_chain *state = &req->chain;
 
-	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
-					  CRYPTO_ACOMP_REQ_DST_VIRT);
-
 	if (acomp_request_src_isvirt(req)) {
 		unsigned int slen = req->slen;
 		const u8 *svirt = req->svirt;
@@ -248,6 +251,10 @@ static int acomp_reqchain_finish(struct acomp_req *req, int err)
 {
 	acomp_reqchain_virt(req);
 	acomp_restore_req(req);
+
+	if (req->unit_size)
+		req->dst->length = err ?: req->dlen;
+
 	return err;
 }
 
@@ -272,6 +279,9 @@ static int acomp_do_req_chain(struct acomp_req *req, bool comp)
 {
 	int err;
 
+	if (req->unit_size && req->slen > req->unit_size)
+		return -ENOSYS;
+
 	acomp_save_req(req, acomp_reqchain_done);
 
 	err = acomp_do_one_req(req, comp);
@@ -287,6 +297,13 @@ int crypto_acomp_compress(struct acomp_req *req)
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size) {
+		if (!acomp_request_issg(req))
+			return -EINVAL;
+		if (crypto_acomp_req_seg(tfm))
+			return crypto_acomp_reqtfm(req)->compress(req);
+		return acomp_do_req_chain(req, true);
+	}
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->compress(req);
 	return acomp_do_req_chain(req, true);
@@ -299,6 +316,8 @@ int crypto_acomp_decompress(struct acomp_req *req)
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size)
+		return -ENOSYS;
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->decompress(req);
 	return acomp_do_req_chain(req, false);
include/crypto/acompress.h +1 -0
index 0f1334168f1b..965eab7738ba 100644
@@ -67,6 +67,7 @@ struct acomp_req_chain {
 		struct folio *dfolio;
 	};
 	u32 flags;
+	u32 unit_size;
 };
 
 /**
Signature · 1 line
2.47.3
#6 lore raw

RE: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Quoted 89 lines

-----Original Message-----
From: Herbert Xu <[email protected]>
Sent: Thursday, December 25, 2025 4:38 PM
To: Linux Crypto Mailing List <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Sridhar, Kanchana P <[email protected]>
Subject: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Add a trivial segmentation wrapper that only supports compression with a segment count of exactly one.

The reason is that the first user zswap will only allocate the extra memory if the underlying algorithm supports segmentation, and otherwise only one segment will be given at a time.

Having this wrapper means that the same calling convention can be used for all algorithms, regardless of segmentation support.

---
 crypto/acompress.c         | 25 ++++++++++++++++++++++---
 include/crypto/acompress.h |  1 +
 2 files changed, 23 insertions(+), 3 deletions(-)
crypto/acompress.c +5 -0
index be28cbfd22e3..d97a90a5ee46 100644
@@ -170,8 +170,13 @@ static void acomp_save_req(struct acomp_req *req,
 crypto_completion_t cplt)
 
 	state->compl = req->base.complete;
 	state->data = req->base.data;
+	state->unit_size = req->unit_size;
+	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
+					  CRYPTO_ACOMP_REQ_DST_VIRT);
+
 	req->base.complete = cplt;
 	req->base.data = state;
+	req->unit_size = 0;
 }
 

static void acomp_restore_req(struct acomp_req *req)

crypto/acompress.c +1 -0
@@ -180,6 +185,7 @@ static void acomp_restore_req(struct acomp_req
 *req)
 
 	req->base.complete = state->compl;
 	req->base.data = state->data;
+	req->unit_size = state->unit_size;
 }
 

static void acomp_reqchain_virt(struct acomp_req *req)

crypto/acompress.c +7 -3
@@ -198,9 +204,6 @@ static void acomp_virt_to_sg(struct acomp_req *req)
 {
 	struct acomp_req_chain *state = &req->chain;
 
-	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
-					  CRYPTO_ACOMP_REQ_DST_VIRT);
-
 	if (acomp_request_src_isvirt(req)) {
 		unsigned int slen = req->slen;
 		const u8 *svirt = req->svirt;
@@ -248,6 +251,10 @@ static int acomp_reqchain_finish(struct acomp_req
 *req, int err)
 {
 	acomp_reqchain_virt(req);
 	acomp_restore_req(req);
+
+	if (req->unit_size)
+		req->dst->length = err ?: req->dlen;
+
 	return err;
 }
@@ -272,6 +279,9 @@ static int acomp_do_req_chain(struct acomp_req
 *req, bool comp)
 {
 	int err;
 
+	if (req->unit_size && req->slen > req->unit_size)
+		return -ENOSYS;
+
 	acomp_save_req(req, acomp_reqchain_done);
 
crypto/acompress.c +0 -0
 	err = acomp_do_one_req(req, comp);
@@ -287,6 +297,13 @@ int crypto_acomp_compress(struct acomp_req
 *req)

crypto/acompress.c int crypto_acomp_compress(struct acomp_req · line 307

Quoted 10 lines
crypto/acompress.c +7 -0
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size) {
+		if (!acomp_request_issg(req))
+			return -EINVAL;
+		if (crypto_acomp_req_seg(tfm))
+			return crypto_acomp_reqtfm(req)->compress(req);
+		return acomp_do_req_chain(req, true);
+	}

Happy New Year to everyone!

Hi Herbert,

Thanks for the segmentation patches. I have integrated the changes with my latest v14 patch-set I have been working on that incorporates the rest of the comments. I had to re-organize the structure of crypto_acomp_compress() to make sure that performance does not regress as a result of the 3 additional conditionals introduced. I also had to inline acomp_do_req_chain() to not regress usemem/zstd performance with PMD folios due to the added computes.

crypto/acompress.c int crypto_acomp_decompress(struct acomp_req · line 321

Quoted 10 lines
crypto/acompress.c +0 -0
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->compress(req);
crypto/acompress.c +2 -0
 	return acomp_do_req_chain(req, true);
@@ -299,6 +316,8 @@ int crypto_acomp_decompress(struct acomp_req
 *req)
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size)
+		return -ENOSYS;

In order to be able to use the same zswap per-CPU acomp_req for compressions and decompressions, I kept crypto_acomp_decompress() unchanged and to not flag an error "if (req->unit_size)" for now. Besides, the added conditional impacts performance for the workloads that I have been using (usemem, kernel_compilation)

I wanted to share these updates and get your thoughts. If the above proposed changes from my integration sound Ok, can you please let me know if I can include your patches in v14, or if you would prefer the segmentation patch-set is accepted that I can then rebase to?

Thanks,
Kanchana

Quoted 17 lines
crypto/acompress.c +0 -0
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->decompress(req);
crypto/acompress.c +0 -0
 	return acomp_do_req_chain(req, false);
include/crypto/acompress.h +1 -1
index 0f1334168f1b..965eab7738ba 100644
@@ -67,6 +67,7 @@ struct acomp_req_chain {
 		struct folio *dfolio;
 	};
 	u32 flags;
+	u32 unit_size;
 };
 
 /**
--

2.47.3

#7 lore raw

RE: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Quoted 144 lines

-----Original Message-----
From: Sridhar, Kanchana P <[email protected]>
Sent: Tuesday, January 6, 2026 8:27 PM
To: Herbert Xu <[email protected]>; Linux Crypto Mailing List <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Sridhar, Kanchana P <[email protected]>
Subject: RE: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

crypto/acompress.c static void acomp_save_req(struct acomp_req

Quoted 30 lines

-----Original Message-----
From: Herbert Xu <[email protected]>
Sent: Thursday, December 25, 2025 4:38 PM
To: Linux Crypto Mailing List <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Sridhar, Kanchana P <[email protected]>
Subject: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Add a trivial segmentation wrapper that only supports compression with a segment count of exactly one.

The reason is that the first user zswap will only allocate the extra memory if the underlying algorithm supports segmentation, and otherwise only one segment will be given at a time.

Having this wrapper means that the same calling convention can be used for all algorithms, regardless of segmentation support.

---
 crypto/acompress.c         | 25 ++++++++++++++++++++++---
 include/crypto/acompress.h |  1 +
 2 files changed, 23 insertions(+), 3 deletions(-)
crypto/acompress.c +0 -0
index be28cbfd22e3..d97a90a5ee46 100644
@@ -170,8 +170,13 @@ static void acomp_save_req(struct acomp_req

*req,

crypto/acompress.c static void acomp_virt_to_sg(struct acomp_req

Quoted 24 lines
crypto/acompress.c +5 -0
 crypto_completion_t cplt)
 
 	state->compl = req->base.complete;
 	state->data = req->base.data;
+	state->unit_size = req->unit_size;
+	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
+					  CRYPTO_ACOMP_REQ_DST_VIRT);
+
 	req->base.complete = cplt;
 	req->base.data = state;
+	req->unit_size = 0;
 }
 

static void acomp_restore_req(struct acomp_req *req)

crypto/acompress.c +1 -0
@@ -180,6 +185,7 @@ static void acomp_restore_req(struct acomp_req
 *req)
 
 	req->base.complete = state->compl;
 	req->base.data = state->data;
+	req->unit_size = state->unit_size;
 }
 

static void acomp_reqchain_virt(struct acomp_req *req)

crypto/acompress.c +0 -0
@@ -198,9 +204,6 @@ static void acomp_virt_to_sg(struct acomp_req

*req)

crypto/acompress.c static int acomp_reqchain_finish(struct

Quoted 10 lines
crypto/acompress.c +0 -3
 {
 	struct acomp_req_chain *state = &req->chain;
 
-	state->flags = req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
-					  CRYPTO_ACOMP_REQ_DST_VIRT);
-
 	if (acomp_request_src_isvirt(req)) {
 		unsigned int slen = req->slen;
 		const u8 *svirt = req->svirt;
@@ -248,6 +251,10 @@ static int acomp_reqchain_finish(struct

acomp_req

Quoted 24 lines
crypto/acompress.c +7 -0
 *req, int err)
 {
 	acomp_reqchain_virt(req);
 	acomp_restore_req(req);
+
+	if (req->unit_size)
+		req->dst->length = err ?: req->dlen;
+
 	return err;
 }
@@ -272,6 +279,9 @@ static int acomp_do_req_chain(struct acomp_req
 *req, bool comp)
 {
 	int err;
 
+	if (req->unit_size && req->slen > req->unit_size)
+		return -ENOSYS;
+
 	acomp_save_req(req, acomp_reqchain_done);
 
crypto/acompress.c +0 -0
 	err = acomp_do_one_req(req, comp);
@@ -287,6 +297,13 @@ int crypto_acomp_compress(struct acomp_req
 *req)

crypto/acompress.c int crypto_acomp_compress(struct acomp_req · line 307

Quoted 10 lines
crypto/acompress.c +7 -0
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size) {
+		if (!acomp_request_issg(req))
+			return -EINVAL;
+		if (crypto_acomp_req_seg(tfm))
+			return crypto_acomp_reqtfm(req)->compress(req);
+		return acomp_do_req_chain(req, true);
+	}

Happy New Year to everyone!

Hi Herbert,

Thanks for the segmentation patches. I have integrated the changes with my latest v14 patch-set I have been working on that incorporates the rest of the comments. I had to re-organize the structure of crypto_acomp_compress() to make sure that performance does not regress as a result of the 3 additional conditionals introduced. I also had to inline acomp_do_req_chain() to not regress usemem/zstd performance with PMD folios due to the added computes.

crypto/acompress.c int crypto_acomp_decompress(struct acomp_req · line 321

Quoted 10 lines
crypto/acompress.c +0 -0
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->compress(req);
crypto/acompress.c +2 -0
 	return acomp_do_req_chain(req, true);
@@ -299,6 +316,8 @@ int crypto_acomp_decompress(struct acomp_req
 *req)
 
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+	if (req->unit_size)
+		return -ENOSYS;

In order to be able to use the same zswap per-CPU acomp_req for compressions
and decompressions, I kept crypto_acomp_decompress() unchanged and to not flag an error "if (req->unit_size)" for now. Besides, the added conditional impacts performance for the workloads that I have been using (usemem, kernel_compilation)

Quoted 4 lines

I wanted to share these updates and get your thoughts. If the above proposed changes from my integration sound Ok, can you please let me know if I can include your patches in v14, or if you would prefer the segmentation patch-set is accepted that I can then rebase to?

Hi Herbert,

Just wanted to follow up and get your suggestions on whether I should go ahead with incorporating the segmentation API patches into v14 of my zswap compression batching patch series, with the changes I had to make, as described earlier? I would appreciate your letting me know.

Thanks,
Kanchana

Quoted 21 lines

Thanks,
Kanchana

Quoted 17 lines
crypto/acompress.c +0 -0
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
 		return crypto_acomp_reqtfm(req)->decompress(req);
crypto/acompress.c +0 -0
 	return acomp_do_req_chain(req, false);
include/crypto/acompress.h +1 -1
index 0f1334168f1b..965eab7738ba 100644
@@ -67,6 +67,7 @@ struct acomp_req_chain {
 		struct folio *dfolio;
 	};
 	u32 flags;
+	u32 unit_size;
 };
 
 /**
--

2.47.3

author maintainer
#8 lore raw

Re: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Quoted 5 lines · Kanchana P

Just wanted to follow up and get your suggestions on whether I should go ahead with incorporating the segmentation API patches into v14 of my zswap compression batching patch series, with the changes I had to make, as described earlier? I would appreciate your letting me know.

Yes let's go ahead with your changes. 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
#9 lore raw

RE: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Quoted 12 lines

-----Original Message-----
From: Herbert Xu <[email protected]>
Sent: Tuesday, January 13, 2026 9:06 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Linux Crypto Mailing List <[email protected]>; [email protected]; [email protected]; [email protected]; Feghali, Wajdi K <[email protected]>
Subject: Re: [PATCH 3/3] crypto: acomp - Add trivial segmentation wrapper

Quoted 1 line · Kanchana P
Quoted 8 lines
Quoted 2 lines

Just wanted to follow up and get your suggestions on whether I should go ahead with incorporating the segmentation API patches into v14 of my

zswap

Quoted 1 line

compression batching patch series, with the changes I had to make, as

described

Quoted 1 line

earlier? I would appreciate your letting me know.

Yes let's go ahead with your changes. Thanks,

Thanks Herbert, this sounds good.

Best regards,
Kanchana

Quoted 4 lines

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