AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/11/09 05:46 flow patch-triage
6m Args:
null

Results:
{
  "EnableConfigs": [
    "ZSWAP",
    "SWAP",
    "MEMCG",
    "CRYPTO_ACOMP2",
    "ZSWAP_DEFAULT_ON"
  ],
  "FocusSymbols": [
    "zswap_store",
    "zswap_store_pages",
    "zswap_compress",
    "zswap_tree_stores",
    "obj_cgroup_charge_zswap_many",
    "crypto_acomp_compress",
    "crypto_acomp_batch_size"
  ],
  "KMSANReasoning": "The patch series introduces batching support for crypto asynchronous compression (acomp) and leverages it in zswap to store pages in batches (bounded by ZSWAP_MAX_BATCH_SIZE).\n\n1. Data structures and initialization:\n- `struct acomp_req` gains a `unit_size` field, which is explicitly initialized to 0 in `acomp_request_set_callback()` and in `acomp_save_req()`, and set by the caller via `acomp_request_set_unit_size()`. When allocated dynamically, `acomp_request_alloc()` uses `kzalloc()`.\n- In `zswap`, `struct zswap_entry` is packed with bitfields. In `zswap_init_entries()`, all fields (`handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, `lru`) are initialized; `length` and `handle` are updated upon successful compression in `zswap_update_entries()`. On failure before completion, only `handle` (initialized to `ERR_PTR(-EINVAL)`) is checked to determine if zsmalloc freeing is necessary before freeing the entries back to the cache. No uninitialized struct fields are read.\n- The per-CPU compression destination buffers `buffers[i]` are written to by the compression backend and read up to `dlen` (the compressed output size) into zsmalloc via `zs_obj_write()`. Incompressible pages read directly from the source folio.\n\n2. Userspace exposure and control flow:\n- No kernel memory or padding is exposed or copied to userspace (no `copy_to_user`, ioctl, netlink, or socket options).\n- No conditional branching depends on uninitialized memory or uninitialized struct padding.\n\n3. KASAN vs KMSAN applicability:\n- Potential risks in this patch series involve buffer indexing (`ZSWAP_MAX_BATCH_SIZE`), scatterlist iteration bounds, memory allocation failure handling, and concurrency/locking around `comp_ctx-\u003eareq.mutex`. These are all covered by KASAN, LOCKDEP, and standard kernel debugging facilities.\n- There are no uninitialized memory usage risks or info-leaks that would require a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch introduces batch compression and storing functionality into zswap and crypto acompress, modifying core memory management and crypto subsystems. It restructures folio compression and xarray/LRU insertion in zswap, adjusts reference counting and error unwinding, adds bulk charging for memory cgroups (obj_cgroup_charge_zswap_many) with new VM_WARN_ON_ONCE assertions, and modifies request chaining in crypto acompress. This code is fully reachable in virtualized environments under memory pressure when swap/zswap is enabled.",
  "WorthFuzzing": true
}

1/1 2026/11/09 05:46 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 8c3cd021912957225a6d79bc78e90ca864c09e4b\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Mon Nov 9 05:46:48 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/crypto/acompress.c b/crypto/acompress.c\nindex c8b4b8589769e3..df9e42f519c358 100644\n--- a/crypto/acompress.c\n+++ b/crypto/acompress.c\n@@ -166,14 +166,18 @@ static void acomp_save_req(struct acomp_req *req, crypto_completion_t cplt)\n \n \tstate-\u003ecompl = req-\u003ebase.complete;\n \tstate-\u003edata = req-\u003ebase.data;\n+\tstate-\u003eunit_size = req-\u003eunit_size;\n+\n \treq-\u003ebase.complete = cplt;\n \treq-\u003ebase.data = req;\n+\treq-\u003eunit_size = 0;\n }\n \n static void acomp_restore_req(struct acomp_req *req)\n {\n \treq-\u003ebase.complete = req-\u003echain.compl;\n \treq-\u003ebase.data = req-\u003echain.data;\n+\treq-\u003eunit_size = req-\u003echain.unit_size;\n }\n \n static void acomp_reqchain_virt(struct acomp_req *req)\n@@ -242,6 +246,10 @@ static int acomp_reqchain_finish(struct acomp_req *req, int err)\n {\n \tacomp_reqchain_virt(req);\n \tacomp_restore_req(req);\n+\n+\tif (req-\u003eunit_size)\n+\t\treq-\u003edst-\u003elength = unlikely(err) ? err : req-\u003edlen;\n+\n \treturn err;\n }\n \n@@ -266,10 +274,13 @@ static int acomp_do_req_chain(struct acomp_req *req, bool comp)\n {\n \tint err;\n \n+\tif (unlikely(req-\u003eunit_size \u0026\u0026 req-\u003eslen \u003e req-\u003eunit_size))\n+\t\treturn -ENOSYS;\n+\n \tacomp_save_req(req, acomp_reqchain_done);\n \n \terr = acomp_do_one_req(req, comp);\n-\tif (err == -EBUSY || err == -EINPROGRESS)\n+\tif (unlikely(err == -EBUSY || err == -EINPROGRESS))\n \t\treturn err;\n \n \treturn acomp_reqchain_finish(req, err);\n@@ -279,10 +290,27 @@ int crypto_acomp_compress(struct acomp_req *req)\n {\n \tstruct crypto_acomp *tfm = crypto_acomp_reqtfm(req);\n \n+\tif (likely((req-\u003eunit_size == req-\u003eslen) \u0026\u0026 acomp_request_issg(req) \u0026\u0026\n+\t\t    !crypto_acomp_req_seg(tfm) \u0026\u0026 !acomp_is_async(tfm))) {\n+\t\tint err = tfm-\u003ecompress(req);\n+\n+\t\treq-\u003edst-\u003elength = unlikely(err) ? err : req-\u003edlen;\n+\t\treturn err;\n+\t}\n+\n \tif (acomp_req_on_stack(req) \u0026\u0026 acomp_is_async(tfm))\n \t\treturn -EAGAIN;\n+\n+\tif (req-\u003eunit_size \u0026\u0026 acomp_request_issg(req)) {\n+\t\tif (!crypto_acomp_req_seg(tfm))\n+\t\t\treturn acomp_do_req_chain(req, true);\n+\n+\t\treturn tfm-\u003ecompress(req);\n+\t}\n+\n \tif (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))\n-\t\treturn crypto_acomp_reqtfm(req)-\u003ecompress(req);\n+\t\treturn tfm-\u003ecompress(req);\n+\n \treturn acomp_do_req_chain(req, true);\n }\n EXPORT_SYMBOL_GPL(crypto_acomp_compress);\n@@ -294,11 +322,25 @@ int crypto_acomp_decompress(struct acomp_req *req)\n \tif (acomp_req_on_stack(req) \u0026\u0026 acomp_is_async(tfm))\n \t\treturn -EAGAIN;\n \tif (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))\n-\t\treturn crypto_acomp_reqtfm(req)-\u003edecompress(req);\n+\t\treturn tfm-\u003edecompress(req);\n \treturn acomp_do_req_chain(req, false);\n }\n EXPORT_SYMBOL_GPL(crypto_acomp_decompress);\n \n+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm)\n+{\n+\tif (acomp_is_async(tfm) \u0026\u0026\n+\t    (crypto_comp_alg_common(tfm)-\u003ebase.cra_type == \u0026crypto_acomp_type)) {\n+\t\tstruct acomp_alg *alg = crypto_acomp_alg(tfm);\n+\n+\t\tif (alg \u0026\u0026 alg-\u003ebatch_size \u003e 1)\n+\t\t\treturn alg-\u003ebatch_size;\n+\t}\n+\n+\treturn 1;\n+}\n+EXPORT_SYMBOL_GPL(crypto_acomp_batch_size);\n+\n void comp_prepare_alg(struct comp_alg_common *alg)\n {\n \tstruct crypto_alg *base = \u0026alg-\u003ebase;\ndiff --git a/include/crypto/acompress.h b/include/crypto/acompress.h\nindex 5d5358dfab7345..3ce4704dc8795d 100644\n--- a/include/crypto/acompress.h\n+++ b/include/crypto/acompress.h\n@@ -67,6 +67,7 @@ struct acomp_req_chain {\n \t\tstruct folio *dfolio;\n \t};\n \tu32 flags;\n+\tu32 unit_size;\n };\n \n /**\n@@ -79,6 +80,7 @@ struct acomp_req_chain {\n  * @dvirt:\tDestination virtual address\n  * @slen:\tSize of the input buffer\n  * @dlen:\tSize of the output buffer and number of bytes produced\n+ * @unit_size:\tUnit size for the request for use in batching\n  * @chain:\tPrivate API code data, do not use\n  * @__ctx:\tStart of private context data\n  */\n@@ -94,6 +96,7 @@ struct acomp_req {\n \t};\n \tunsigned int slen;\n \tunsigned int dlen;\n+\tunsigned int unit_size;\n \n \tstruct acomp_req_chain chain;\n \n@@ -197,7 +200,7 @@ static inline void acomp_request_set_tfm(struct acomp_req *req,\n \tcrypto_request_set_tfm(\u0026req-\u003ebase, crypto_acomp_tfm(tfm));\n }\n \n-static inline bool acomp_is_async(struct crypto_acomp *tfm)\n+static __always_inline bool acomp_is_async(struct crypto_acomp *tfm)\n {\n \treturn crypto_comp_alg_common(tfm)-\u003ebase.cra_flags \u0026\n \t       CRYPTO_ALG_ASYNC;\n@@ -292,7 +295,7 @@ static inline void *acomp_request_extra(struct acomp_req *req)\n \treturn (void *)((char *)req + len);\n }\n \n-static inline bool acomp_req_on_stack(struct acomp_req *req)\n+static __always_inline bool acomp_req_on_stack(struct acomp_req *req)\n {\n \treturn crypto_req_on_stack(\u0026req-\u003ebase);\n }\n@@ -329,9 +332,56 @@ static inline void acomp_request_set_callback(struct acomp_req *req,\n {\n \tflgs \u0026= ~CRYPTO_ACOMP_REQ_PRIVATE;\n \tflgs |= req-\u003ebase.flags \u0026 CRYPTO_ACOMP_REQ_PRIVATE;\n+\treq-\u003eunit_size = 0;\n \tcrypto_request_set_callback(\u0026req-\u003ebase, flgs, cmpl, data);\n }\n \n+/**\n+ * acomp_request_set_unit_size() -- Sets the unit size for the request.\n+ *\n+ * This is a helper function that enables batching for zswap, IPComp, etc.\n+ * It allows multiple independent compression (or decompression) operations to\n+ * be submitted in a single request's SG lists, where each SG list (\"segment\")\n+ * is processed independently. The unit size helps derive segments from a\n+ * single request. crypto_acomp does not expect the segments to be related in\n+ * any way.\n+ *\n+ * Example usage model:\n+ *\n+ * A module such as zswap that's configured to use a batching compressor, can\n+ * accomplish batch compression of \"nr_pages\" with crypto_acomp by creating an\n+ * output SG table for the batch, initialized to contain \"nr_pages\" SG\n+ * lists. Each scatterlist is mapped to the nth destination buffer for the\n+ * batch. Depending on whether the @req is used for batch compress/decompress,\n+ * zswap must set the @req's source/destination length to be\n+ * \"nr_pages * @req-\u003eunit_size\" respectively.\n+ *\n+ * An acomp_alg can implement batch compression by using the @req-\u003eunit_size\n+ * to break down the SG lists passed in via @req-\u003edst to submit individual\n+ * \"@req-\u003eslen/@req-\u003eunit_size\" compress jobs to be processed as a batch.\n+ *\n+ * Similarly, zswap can implement batch decompression by passing an\n+ * SG table with \"nr_pages\" SG lists via @req-\u003esrc to process\n+ * \"@req-\u003edlen/@req-\u003eunit_size\" decompress jobs as a batch.\n+ *\n+ * This API must be called after acomp_request_set_callback(),\n+ * which sets @req-\u003eunit_size to 0. This makes it easy for users of\n+ * crypto_acomp to rely on a default of not opting in to batching.\n+ * Users such as zswap opt in to batching by defining @req-\u003eunit_size\n+ * to a non-zero value for use by acomp_algs supporting batching.\n+ *\n+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.\n+ *\n+ * @req:\tasynchronous compress/decompress request\n+ * @du:\t\tdata unit size of the input/output buffer scatterlist.\n+ */\n+static inline void acomp_request_set_unit_size(struct acomp_req *req,\n+\t\t\t\t\t       unsigned int du)\n+{\n+\treq-\u003eunit_size = du;\n+}\n+\n+\n /**\n  * acomp_request_set_params() -- Sets request parameters\n  *\n@@ -543,6 +593,18 @@ int crypto_acomp_compress(struct acomp_req *req);\n  */\n int crypto_acomp_decompress(struct acomp_req *req);\n \n+/**\n+ * crypto_acomp_batch_size() -- Get the algorithm's batch size\n+ *\n+ * Function returns the algorithm's batch size for batching operations\n+ *\n+ * @tfm:\tACOMPRESS tfm handle allocated with crypto_alloc_acomp()\n+ *\n+ * Return:\t@tfm's acomp_alg's @batch_size, if it has defined a\n+ *\t\t@batch_size greater than 1; else return 1.\n+ */\n+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm);\n+\n static inline struct acomp_req *acomp_request_on_stack_init(\n \tchar *buf, struct crypto_acomp *tfm)\n {\ndiff --git a/include/crypto/algapi.h b/include/crypto/algapi.h\nindex 05deea9dac5e88..7d406cfe57519f 100644\n--- a/include/crypto/algapi.h\n+++ b/include/crypto/algapi.h\n@@ -280,6 +280,11 @@ static inline bool crypto_tfm_req_virt(struct crypto_tfm *tfm)\n \treturn tfm-\u003e__crt_alg-\u003ecra_flags \u0026 CRYPTO_ALG_REQ_VIRT;\n }\n \n+static inline bool crypto_tfm_req_seg(struct crypto_tfm *tfm)\n+{\n+\treturn tfm-\u003e__crt_alg-\u003ecra_flags \u0026 CRYPTO_ALG_REQ_SEG;\n+}\n+\n static inline u32 crypto_request_flags(struct crypto_async_request *req)\n {\n \treturn req-\u003eflags \u0026 ~CRYPTO_TFM_REQ_ON_STACK;\ndiff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h\nindex 9cd37df32dc40a..2d4d302d1b3f18 100644\n--- a/include/crypto/internal/acompress.h\n+++ b/include/crypto/internal/acompress.h\n@@ -28,6 +28,8 @@\n  *\n  * @compress:\tFunction performs a compress operation\n  * @decompress:\tFunction performs a de-compress operation\n+ * @batch_size:\tMaximum batch-size for batching compress/decompress\n+ *\t\toperations.\n  * @init:\tInitialize the cryptographic transformation object.\n  *\t\tThis function is used to initialize the cryptographic\n  *\t\ttransformation object. This function is called only once at\n@@ -47,6 +49,7 @@\n struct acomp_alg {\n \tint (*compress)(struct acomp_req *req);\n \tint (*decompress)(struct acomp_req *req);\n+\tunsigned int batch_size;\n \tint (*init)(struct crypto_acomp *tfm);\n \tvoid (*exit)(struct crypto_acomp *tfm);\n \n@@ -146,7 +149,7 @@ void crypto_unregister_acomp(struct acomp_alg *alg);\n int crypto_register_acomps(struct acomp_alg *algs, int count);\n void crypto_unregister_acomps(struct acomp_alg *algs, int count);\n \n-static inline bool acomp_request_issg(struct acomp_req *req)\n+static __always_inline bool acomp_request_issg(struct acomp_req *req)\n {\n \treturn !(req-\u003ebase.flags \u0026 (CRYPTO_ACOMP_REQ_SRC_VIRT |\n \t\t\t\t    CRYPTO_ACOMP_REQ_DST_VIRT));\n@@ -189,6 +192,11 @@ static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm)\n \treturn crypto_tfm_req_virt(\u0026tfm-\u003ebase);\n }\n \n+static __always_inline bool crypto_acomp_req_seg(struct crypto_acomp *tfm)\n+{\n+\treturn crypto_tfm_req_seg(\u0026tfm-\u003ebase);\n+}\n+\n void crypto_acomp_free_streams(struct crypto_acomp_streams *s);\n int crypto_acomp_alloc_streams(struct crypto_acomp_streams *s);\n \ndiff --git a/include/linux/crypto.h b/include/linux/crypto.h\nindex b7c97f1c47c924..0628b62e494668 100644\n--- a/include/linux/crypto.h\n+++ b/include/linux/crypto.h\n@@ -139,6 +139,9 @@\n /* Set if the algorithm cannot have a fallback (e.g., phmac). */\n #define CRYPTO_ALG_NO_FALLBACK\t\t0x00080000\n \n+/* Set if the algorithm supports segmentation. */\n+#define CRYPTO_ALG_REQ_SEG\t\t0x00100000\n+\n /* The high bits 0xff000000 are reserved for type-specific flags. */\n \n /*\n@@ -489,7 +492,7 @@ static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm)\n \treturn tfm-\u003e__crt_alg-\u003ecra_flags \u0026 CRYPTO_ALG_ASYNC;\n }\n \n-static inline bool crypto_req_on_stack(struct crypto_async_request *req)\n+static __always_inline bool crypto_req_on_stack(struct crypto_async_request *req)\n {\n \treturn req-\u003eflags \u0026 CRYPTO_TFM_REQ_ON_STACK;\n }\ndiff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h\nindex 980358bc6f49c9..09a6b9a419016b 100644\n--- a/include/linux/memcontrol.h\n+++ b/include/linux/memcontrol.h\n@@ -1921,6 +1921,8 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,\n #if defined(CONFIG_MEMCG) \u0026\u0026 defined(CONFIG_ZSWAP)\n bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);\n void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);\n+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,\n+\t\t\t\t  u8 nr_pages, u8 nr_incomp_pages);\n void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);\n bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);\n #else\n@@ -1932,6 +1934,11 @@ static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,\n \t\t\t\t\t   size_t size)\n {\n }\n+static inline void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg,\n+\t\t\t\t\t\tsize_t size, u8 nr_pages,\n+\t\t\t\t\t\tu8 nr_incomp_pages)\n+{\n+}\n static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,\n \t\t\t\t\t     size_t size)\n {\ndiff --git a/mm/memcontrol.c b/mm/memcontrol.c\nindex 1ebc9330aeae51..48c216e621daa6 100644\n--- a/mm/memcontrol.c\n+++ b/mm/memcontrol.c\n@@ -6361,6 +6361,42 @@ void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)\n \trcu_read_unlock();\n }\n \n+/**\n+ * obj_cgroup_charge_zswap_many - bulk charge compression backend memory\n+ *                                from compressing a batch of pages.\n+ * @objcg: the object cgroup\n+ * @size: total compressed length of a batch of compressed objects\n+ * @nr_pages: # of pages that were compressed to store in zswap\n+ * @nr_incomp_pages: # of incompressible pages to store in zswap\n+ *\n+ * This forces the charge after obj_cgroup_may_zswap() allowed\n+ * compression and storage in zswap for this cgroup to go ahead.\n+ */\n+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,\n+\t\t\t\t  u8 nr_pages, u8 nr_incomp_pages)\n+{\n+\tstruct mem_cgroup *memcg;\n+\n+\tif (!cgroup_subsys_on_dfl(memory_cgrp_subsys))\n+\t\treturn;\n+\n+\tif (obj_cgroup_is_root(objcg))\n+\t\treturn;\n+\n+\tVM_WARN_ON_ONCE(!(current-\u003eflags \u0026 PF_MEMALLOC));\n+\n+\t/* PF_MEMALLOC context, charging must succeed */\n+\tif (obj_cgroup_charge(objcg, GFP_KERNEL, size))\n+\t\tVM_WARN_ON_ONCE(1);\n+\n+\trcu_read_lock();\n+\tmemcg = obj_cgroup_memcg(objcg);\n+\tmod_memcg_state(memcg, MEMCG_ZSWAP_B, size);\n+\tmod_memcg_state(memcg, MEMCG_ZSWAPPED, nr_pages);\n+\tmod_memcg_state(memcg, MEMCG_ZSWAP_INCOMP, nr_incomp_pages);\n+\trcu_read_unlock();\n+}\n+\n /**\n  * obj_cgroup_uncharge_zswap - uncharge compression backend memory\n  * @objcg: the object cgroup\ndiff --git a/mm/zswap.c b/mm/zswap.c\nindex 37d217860a6b2e..67181d4fe7e4de 100644\n--- a/mm/zswap.c\n+++ b/mm/zswap.c\n@@ -85,6 +85,9 @@ static bool zswap_pool_reached_full;\n \n #define ZSWAP_PARAM_UNSET \"\"\n \n+/* Limit the batch size to limit per-CPU memory usage for dst buffers. */\n+#define ZSWAP_MAX_BATCH_SIZE 4U\n+\n static int zswap_setup(void);\n \n /* Enable/disable zswap */\n@@ -144,10 +147,17 @@ struct zswap_acomp_req {\n \tstruct mutex mutex;\n };\n \n-/* The compression mutex also protects the output buffer. */\n+/*\n+ * The compression mutex also protects the output buffer(s).\n+ *\n+ * @compr_batch_size: The max batch size of the compression algorithm,\n+ *                    bounded by ZSWAP_MAX_BATCH_SIZE.\n+ */\n struct zswap_comp_ctx {\n \tstruct zswap_acomp_req areq;\n-\tu8 *buffer;\n+\tu8 compr_batch_size;\n+\tstruct sg_table *sg_table;\n+\tu8 **buffers;\n };\n \n /* Separate requests, so that decompression does not wait for compression. */\n@@ -162,6 +172,7 @@ struct crypto_acomp_ctx {\n  * The only case where lru_lock is not acquired while holding tree.lock is\n  * when a zswap_entry is taken off the lru for writeback, in that case it\n  * needs to be verified that it's still valid in the tree.\n+ *\n  */\n struct zswap_pool {\n \tstruct zs_pool *zs_pool;\n@@ -181,6 +192,7 @@ struct zswap_pool {\n  */\n #define ZSWAP_FIRST_POOL_ID 1\n #define ZSWAP_MAX_POOL_ID U8_MAX\n+#define ZSWAP_MAX_COMPR_LENGTH_SHIFT 19U\n static DEFINE_XARRAY_FLAGS(zswap_pools, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH);\n static struct zswap_pool __rcu *zswap_current_pool;\n \n@@ -207,20 +219,37 @@ static struct shrinker *zswap_shrinker;\n  *              logic if referenced is unset. See comments in the shrinker\n  *              section for context.\n  * pool_idx - id of the zswap_pool that the entry's data is in.\n+ * nid - NUMA node id of the page for which this is the zswap entry.\n  * handle - zsmalloc allocation handle that stores the compressed page data\n  * objcg - the obj_cgroup that the compressed memory is charged to\n  * lru - handle to the pool's lru used to evict pages.\n  */\n struct zswap_entry {\n \tswp_entry_t swpentry;\n-\tunsigned int length;\n-\tbool referenced;\n-\tu8 pool_idx;\n+\tstruct {\n+\t\tunsigned int length:ZSWAP_MAX_COMPR_LENGTH_SHIFT;\n+\t\tbool referenced:1U;\n+\t\tu8 pool_idx;\n+\t};\n+\tint nid;\n \tunsigned long handle;\n \tstruct obj_cgroup *objcg;\n \tstruct list_head lru;\n };\n \n+struct zswap_store_context {\n+\tstruct folio *folio;\n+\tlong folio_start;\n+\tunsigned int slen;\n+\tu8 nr_batch_pages;\n+\tbool wb_enabled;\n+\tint nid;\n+\tu8 nr_pages;\n+\tstruct zs_pool *zs_pool;\n+\tstruct zswap_comp_ctx *comp_ctx;\n+\tstruct zswap_entry *entries[ZSWAP_MAX_BATCH_SIZE];\n+};\n+\n /*\n  * No RCU section is needed around the returned pointer: a stored entry pins\n  * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be\n@@ -279,6 +308,8 @@ static void __zswap_pool_empty(struct percpu_ref *ref);\n \n static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)\n {\n+\tu8 i;\n+\n \tif (!acomp_ctx)\n \t\treturn;\n \n@@ -297,8 +328,19 @@ static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)\n \n \tacomp_ctx-\u003eacomp = NULL;\n \n-\tkfree(acomp_ctx-\u003ecomp.buffer);\n-\tacomp_ctx-\u003ecomp.buffer = NULL;\n+\tif (acomp_ctx-\u003ecomp.buffers) {\n+\t\tfor (i = 0; i \u003c ZSWAP_MAX_BATCH_SIZE; ++i)\n+\t\t\tkfree(acomp_ctx-\u003ecomp.buffers[i]);\n+\t\tkfree(acomp_ctx-\u003ecomp.buffers);\n+\t}\n+\tacomp_ctx-\u003ecomp.buffers = NULL;\n+\tacomp_ctx-\u003ecomp.compr_batch_size = 0;\n+\n+\tif (acomp_ctx-\u003ecomp.sg_table) {\n+\t\tsg_free_table(acomp_ctx-\u003ecomp.sg_table);\n+\t\tkfree(acomp_ctx-\u003ecomp.sg_table);\n+\t}\n+\tacomp_ctx-\u003ecomp.sg_table = NULL;\n }\n \n static struct zswap_pool *zswap_pool_create(char *compressor)\n@@ -383,6 +425,7 @@ static struct zswap_pool *zswap_pool_create(char *compressor)\n cpuhp_add_fail:\n \tfor_each_possible_cpu(cpu)\n \t\tacomp_ctx_free(per_cpu_ptr(pool-\u003eacomp_ctx, cpu));\n+\n error:\n \tif (pool-\u003eacomp_ctx)\n \t\tfree_percpu(pool-\u003eacomp_ctx);\n@@ -464,12 +507,12 @@ static int __must_check zswap_pool_tryget(struct zswap_pool *pool)\n }\n \n /* The caller must already have a reference. */\n-static void zswap_pool_get(struct zswap_pool *pool)\n+static inline void zswap_pool_get_many(struct zswap_pool *pool, u8 nr)\n {\n-\tpercpu_ref_get(\u0026pool-\u003eref);\n+\tpercpu_ref_get_many(\u0026pool-\u003eref, nr);\n }\n \n-static void zswap_pool_put(struct zswap_pool *pool)\n+static inline void zswap_pool_put(struct zswap_pool *pool)\n {\n \tpercpu_ref_put(\u0026pool-\u003eref);\n }\n@@ -680,14 +723,8 @@ static int zswap_enabled_param_set(const char *val,\n * lru functions\n **********************************/\n \n-static inline int entry_to_nid(struct zswap_entry *entry)\n-{\n-\treturn page_to_nid(virt_to_page(entry));\n-}\n-\n static void zswap_lru_add(struct zswap_entry *entry)\n {\n-\tint nid = entry_to_nid(entry);\n \tstruct mem_cgroup *memcg;\n \n \t/*\n@@ -704,19 +741,18 @@ static void zswap_lru_add(struct zswap_entry *entry)\n \trcu_read_lock();\n \tmemcg = obj_cgroup_memcg(entry-\u003eobjcg);\n \t/* will always succeed */\n-\tlist_lru_add(\u0026zswap_list_lru, \u0026entry-\u003elru, nid, memcg);\n+\tlist_lru_add(\u0026zswap_list_lru, \u0026entry-\u003elru, entry-\u003enid, memcg);\n \trcu_read_unlock();\n }\n \n static void zswap_lru_del(struct zswap_entry *entry)\n {\n-\tint nid = entry_to_nid(entry);\n \tstruct mem_cgroup *memcg;\n \n \trcu_read_lock();\n \tmemcg = obj_cgroup_memcg(entry-\u003eobjcg);\n \t/* will always succeed */\n-\tlist_lru_del(\u0026zswap_list_lru, \u0026entry-\u003elru, nid, memcg);\n+\tlist_lru_del(\u0026zswap_list_lru, \u0026entry-\u003elru, entry-\u003enid, memcg);\n \trcu_read_unlock();\n }\n \n@@ -764,10 +800,11 @@ void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg)\n **********************************/\n static struct kmem_cache *zswap_entry_cache;\n \n-static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid)\n+static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)\n {\n \tstruct zswap_entry *entry;\n-\tentry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid);\n+\n+\tentry = kmem_cache_alloc(zswap_entry_cache, gfp);\n \tif (!entry)\n \t\treturn NULL;\n \treturn entry;\n@@ -778,6 +815,41 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)\n \tkmem_cache_free(zswap_entry_cache, entry);\n }\n \n+static inline void zswap_entries_cache_free_batch(\n+\tstruct zswap_entry **entries,\n+\tu8 nr_entries)\n+{\n+\t/*\n+\t * It is okay to use this to free entries allocated separately\n+\t * by zswap_entry_cache_alloc().\n+\t */\n+\tkmem_cache_free_bulk(zswap_entry_cache, nr_entries, (void **)entries);\n+}\n+\n+static bool zswap_entries_cache_alloc_batch(\n+\tstruct zswap_store_context *zc,\n+\tgfp_t gfp)\n+{\n+\tbool ret = kmem_cache_alloc_bulk(zswap_entry_cache, gfp,\n+\t\t\t\t\t zc-\u003enr_pages, (void **)zc-\u003eentries);\n+\n+\tif (unlikely(!ret)) {\n+\t\tu8 i;\n+\n+\t\tfor (i = 0; i \u003c zc-\u003enr_pages; ++i) {\n+\t\t\tzc-\u003eentries[i] = zswap_entry_cache_alloc(GFP_KERNEL);\n+\n+\t\t\tif (unlikely(!zc-\u003eentries[i])) {\n+\t\t\t\tzswap_reject_kmemcache_fail++;\n+\t\t\t\tzswap_entries_cache_free_batch(zc-\u003eentries, i);\n+\t\t\t\treturn false;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\treturn true;\n+}\n+\n /*\n  * Carries out the common pattern of freeing an entry's zsmalloc allocation,\n  * freeing the entry itself, and decrementing the number of stored pages.\n@@ -787,7 +859,7 @@ static void zswap_entry_free(struct zswap_entry *entry)\n \tstruct zswap_pool *pool = zswap_entry_pool(entry);\n \n \tzswap_lru_del(entry);\n-\tif (!WARN_ON_ONCE(!pool)) {\n+\tif (likely(!WARN_ON_ONCE(!pool))) {\n \t\tzs_free(pool-\u003ezs_pool, entry-\u003ehandle);\n \t\tzswap_pool_put(pool);\n \t}\n@@ -827,6 +899,12 @@ static int zswap_acomp_req_init(struct zswap_acomp_req *areq,\n \tcrypto_init_wait(\u0026areq-\u003ewait);\n \tzswap_set_acomp_req_callback(areq-\u003ereq, \u0026areq-\u003ewait);\n \n+\t/*\n+\t * This should be called after acomp_request_set_callback(),\n+\t * which initializes req-\u003eunit_size to 0.\n+\t */\n+\tacomp_request_set_unit_size(areq-\u003ereq, PAGE_SIZE);\n+\n \tmutex_init(\u0026areq-\u003emutex);\n \treturn 0;\n }\n@@ -835,7 +913,10 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)\n {\n \tstruct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);\n \tstruct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool-\u003eacomp_ctx, cpu);\n+\tint nid = cpu_to_node(cpu);\n+\tstruct scatterlist *sg;\n \tint ret = -ENOMEM;\n+\tu8 i;\n \n \t/*\n \t * To handle cases where the CPU goes through online-offline-online\n@@ -846,15 +927,11 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)\n \t\treturn 0;\n \t}\n \n-\tacomp_ctx-\u003ecomp.buffer = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu));\n-\tif (!acomp_ctx-\u003ecomp.buffer)\n-\t\treturn ret;\n-\n \t/*\n \t * In case of an error, crypto_alloc_acomp_node() returns an\n \t * error pointer, never NULL.\n \t */\n-\tacomp_ctx-\u003eacomp = crypto_alloc_acomp_node(pool-\u003etfm_name, 0, 0, cpu_to_node(cpu));\n+\tacomp_ctx-\u003eacomp = crypto_alloc_acomp_node(pool-\u003etfm_name, 0, 0, nid);\n \tif (IS_ERR(acomp_ctx-\u003eacomp)) {\n \t\tpr_err(\"could not alloc crypto acomp %s : %pe\\n\",\n \t\t\t\tpool-\u003etfm_name, acomp_ctx-\u003eacomp);\n@@ -862,10 +939,55 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)\n \t\tgoto fail;\n \t}\n \n+\t/*\n+\t * Constrain the crypto algorithm's batch size by ZSWAP_MAX_BATCH_SIZE,\n+\t * store this in the per-CPU zswap_comp_ctx's @compr_batch_size.\n+\t */\n+\tacomp_ctx-\u003ecomp.compr_batch_size = min(ZSWAP_MAX_BATCH_SIZE,\n+\t\t\t\t\t       crypto_acomp_batch_size(acomp_ctx-\u003eacomp));\n+\n \tret = zswap_acomp_req_init(\u0026acomp_ctx-\u003ecomp.areq, acomp_ctx-\u003eacomp);\n \tif (ret)\n \t\tgoto req_fail;\n \n+\t/*\n+\t * Allocate up to ZSWAP_MAX_BATCH_SIZE dst buffers for all compressors.\n+\t */\n+\tacomp_ctx-\u003ecomp.buffers = kcalloc_node(ZSWAP_MAX_BATCH_SIZE,\n+\t\t\t\t\t       sizeof(u8 *), GFP_KERNEL, nid);\n+\tif (!acomp_ctx-\u003ecomp.buffers) {\n+\t\tret = -ENOMEM;\n+\t\tgoto req_fail;\n+\t}\n+\n+\tfor (i = 0; i \u003c ZSWAP_MAX_BATCH_SIZE; ++i) {\n+\t\tacomp_ctx-\u003ecomp.buffers[i] = kmalloc_node(PAGE_SIZE, GFP_KERNEL, nid);\n+\t\tif (!acomp_ctx-\u003ecomp.buffers[i]) {\n+\t\t\tret = -ENOMEM;\n+\t\t\tgoto req_fail;\n+\t\t}\n+\t}\n+\n+\tacomp_ctx-\u003ecomp.sg_table = kmalloc(sizeof(*acomp_ctx-\u003ecomp.sg_table),\n+\t\t\t\t\t   GFP_KERNEL);\n+\tif (!acomp_ctx-\u003ecomp.sg_table) {\n+\t\tret = -ENOMEM;\n+\t\tgoto req_fail;\n+\t}\n+\n+\tif (sg_alloc_table(acomp_ctx-\u003ecomp.sg_table, ZSWAP_MAX_BATCH_SIZE,\n+\t\t\t   GFP_KERNEL)) {\n+\t\tret = -ENOMEM;\n+\t\tgoto req_fail;\n+\t}\n+\n+\t/*\n+\t * Statically map the per-CPU destination buffers to the per-CPU\n+\t * SG lists; crucial for performance.\n+\t */\n+\tfor_each_sg(acomp_ctx-\u003ecomp.sg_table-\u003esgl, sg, ZSWAP_MAX_BATCH_SIZE, i)\n+\t\tsg_set_buf(sg, acomp_ctx-\u003ecomp.buffers[i], PAGE_SIZE);\n+\n \t/* Skip the per-CPU request when decompression can use the stack. */\n \tif (acomp_is_async(acomp_ctx-\u003eacomp)) {\n \t\tret = zswap_acomp_req_init(\u0026acomp_ctx-\u003edecomp, acomp_ctx-\u003eacomp);\n@@ -882,95 +1004,180 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)\n \treturn ret;\n }\n \n-static bool zswap_compress(struct folio *folio, long index,\n-\t\t\t   struct zswap_entry *entry, struct zswap_pool *pool)\n+/*\n+ * zswap_update_entries() is called only when all pages in a store batch have\n+ * been successfully compressed (subsumes the writeback_enabled fallback in case\n+ * of compress errors). The only error that can occur in this phase is zs_malloc\n+ * allocation failure, upon which an error status is returned to\n+ * zswap_store_pages().\n+ */\n+static inline bool zswap_update_entries(struct zswap_store_context *zc)\n {\n-\tstruct zswap_comp_ctx *comp_ctx;\n-\tstruct scatterlist input, output;\n-\tint comp_ret = 0, alloc_ret = 0;\n-\tunsigned int dlen = PAGE_SIZE;\n+\tgfp_t gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;\n+\tstruct zswap_entry *entry;\n+\tstruct scatterlist *sg;\n \tunsigned long handle;\n-\tgfp_t gfp;\n-\tu8 *dst;\n-\tbool mapped = false;\n+\tunsigned int dlen;\n+\tu8 batch_iter;\n+\tu8 *buf;\n \n-\tcomp_ctx = \u0026raw_cpu_ptr(pool-\u003eacomp_ctx)-\u003ecomp;\n-\tmutex_lock(\u0026comp_ctx-\u003eareq.mutex);\n+\t/*\n+\t * For each output SG list in @acomp_ctx-\u003ereq-\u003esg_table-\u003esgl,\n+\t * the @sg-\u003elength should be set to either the page's compressed\n+\t * length (success), or it's negative value compression error.\n+\t */\n+\tfor_each_sg(zc-\u003ecomp_ctx-\u003esg_table-\u003esgl, sg, zc-\u003enr_pages, batch_iter) {\n+\t\tentry = zc-\u003eentries[batch_iter];\n+\t\tdlen = sg-\u003elength;\n+\n+\t\tprefetchw(entry);\n \n-\tdst = comp_ctx-\u003ebuffer;\n-\tsg_init_table(\u0026input, 1);\n-\tsg_set_folio(\u0026input, folio, PAGE_SIZE, index * PAGE_SIZE);\n+\t\thandle = zs_malloc(zc-\u003ezs_pool, dlen, gfp, zc-\u003enid);\n+\n+\t\tif (unlikely(IS_ERR_VALUE(handle))) {\n+\t\t\tif (PTR_ERR((void *)handle) == -ENOSPC)\n+\t\t\t\tzswap_reject_compress_poor++;\n+\t\t\telse\n+\t\t\t\tzswap_reject_alloc_fail++;\n+\n+\t\t\treturn false;\n+\t\t}\n+\n+\t\tentry-\u003elength = dlen;\n+\t\tentry-\u003ehandle = handle;\n+\n+\t\tif (unlikely(dlen == PAGE_SIZE)) {\n+\t\t\tbuf = kmap_local_folio(zc-\u003efolio,\n+\t\t\t\t\t(zc-\u003efolio_start + batch_iter) * PAGE_SIZE);\n+\t\t\tzs_obj_write(zc-\u003ezs_pool, handle, buf, dlen);\n+\t\t\tkunmap_local(buf);\n+\t\t} else {\n+\t\t\tbuf = zc-\u003ecomp_ctx-\u003ebuffers[batch_iter];\n+\t\t\tzs_obj_write(zc-\u003ezs_pool, handle, buf, dlen);\n+\t\t}\n+\t}\n+\n+\treturn true;\n+}\n \n-\tsg_init_one(\u0026output, dst, PAGE_SIZE);\n-\tacomp_request_set_params(comp_ctx-\u003eareq.req, \u0026input, \u0026output,\n-\t\t\t\t PAGE_SIZE, dlen);\n+/*\n+ * zswap_compress() batching implementation for sequential and batching\n+ * compressors.\n+ *\n+ * Description:\n+ * ============\n+ * Compress multiple @zc-\u003enr_pages in @zc-\u003efolio starting from the\n+ * @zc-\u003efolio_start index in batches of @zc-\u003enr_batch_pages; the latter\n+ * representing the lesser of the @zc-\u003enr_pages to be batch compressed and the\n+ * compressor's batch size.\n+ *\n+ * @zc-\u003enr_pages can be in (1, ZSWAP_MAX_BATCH_SIZE] even if the compressor does\n+ * not support batching.\n+ *\n+ * If @nr_batch_pages is 1, each page is compressed sequentially.\n+ *\n+ * If @nr_batch_pages is \u003e 1, the compressor may use batching to optimize\n+ * compression.\n+ *\n+ * In all cases, the store processing that needs to happen post-compression\n+ * occurs in batches of @zc-\u003enr_pages. Error handling is minimal, mostly to\n+ * update zswap statistics, with catch-all cleanup for the batch occuring in the\n+ * @store_pages_failed label in zswap_store_pages().\n+ */\n+static bool zswap_compress(struct zswap_store_context *zc)\n+{\n+\tu8 batch_start, comp_batch_iter;\n+\tstruct scatterlist *sg, *dst_sg;\n+\tint err;\n \n \t/*\n-\t * it maybe looks a little bit silly that we send an asynchronous request,\n-\t * then wait for its completion synchronously. This makes the process look\n-\t * synchronous in fact.\n-\t * Theoretically, acomp supports users send multiple acomp requests in one\n-\t * acomp instance, then get those requests done simultaneously. but in this\n-\t * case, zswap actually does store and load page by page, there is no\n-\t * existing method to send the second page before the first page is done\n-\t * in one thread doing zswap.\n-\t * but in different threads running on different cpu, we have different\n-\t * acomp instance, and compression and decompression use separate\n-\t * requests, so multiple threads can do (de)compression in parallel.\n+\t * @nr_batch_pages is constrained by the per-CPU comp_ctx's compressor\n+\t * batch size and the @zc-\u003enr_pages in this store batch. @slen indicates\n+\t * the total source length bytes for @nr_batch_pages.\n \t */\n-\tcomp_ret = crypto_wait_req(crypto_acomp_compress(comp_ctx-\u003eareq.req),\n-\t\t\t\t   \u0026comp_ctx-\u003eareq.wait);\n-\tdlen = comp_ctx-\u003eareq.req-\u003edlen;\n-\tif (!comp_ret) {\n-\t\tif (dlen == PAGE_SIZE)\n-\t\t\tcomp_ret = -ENOSPC;\n-\t\telse if (WARN_ON_ONCE(!dlen || dlen \u003e PAGE_SIZE))\n-\t\t\tcomp_ret = -EINVAL;\n-\t}\n+\tzc-\u003enr_batch_pages = min(zc-\u003ecomp_ctx-\u003ecompr_batch_size, zc-\u003enr_pages);\n+\tzc-\u003eslen = zc-\u003enr_batch_pages * PAGE_SIZE;\n \n \t/*\n-\t * If a page cannot be compressed into a size smaller than PAGE_SIZE,\n-\t * save the content as is without a compression, to keep the LRU order\n-\t * of writebacks.  If writeback is disabled, reject the page since it\n-\t * only adds metadata overhead.  swap_writeout() will put the page back\n-\t * to the active LRU list in the case.\n+\t * Compress the @nr_pages in @folio starting at index @folio_start\n+\t * in batches of @nr_batch_pages.\n \t */\n-\tif (comp_ret) {\n-\t\trcu_read_lock();\n-\t\tif (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {\n-\t\t\trcu_read_unlock();\n-\t\t\tgoto unlock;\n+\tfor (batch_start = 0; batch_start \u003c zc-\u003enr_pages;\n+\t     batch_start += zc-\u003enr_batch_pages) {\n+\t\t/*\n+\t\t * Send @nr_batch_pages to crypto_acomp for compression:\n+\t\t *\n+\t\t * These pages are in @folio's range of indices in the interval\n+\t\t *     [@folio_start + @batch_start,\n+\t\t *      @folio_start + @batch_start + @nr_batch_pages).\n+\t\t *\n+\t\t * The source folio pages in the batch are directly submitted to\n+\t\t * crypto_acomp via acomp_request_set_src_folio().\n+\t\t */\n+\t\tacomp_request_set_src_folio(zc-\u003ecomp_ctx-\u003eareq.req, zc-\u003efolio,\n+\t\t\t\t(zc-\u003efolio_start + batch_start) * PAGE_SIZE,\n+\t\t\t\tzc-\u003eslen);\n+\n+\t\tdst_sg = zc-\u003ecomp_ctx-\u003esg_table-\u003esgl + batch_start;\n+\t\tdst_sg-\u003elength = zc-\u003eslen;\n+\n+\t\t/*\n+\t\t * The per-CPU @zc-\u003ecomp_ctx-\u003esg_table scatterlists are statically\n+\t\t * mapped to the per-CPU dst @buffers at pool creation time.\n+\t\t * There are ZSWAP_MAX_BATCH_SIZE per-CPU SG lists and dst\n+\t\t * buffers allocated for all compressors.\n+\t\t */\n+\t\tacomp_request_set_dst_sg(zc-\u003ecomp_ctx-\u003eareq.req,\n+\t\t\t\t\t dst_sg, zc-\u003eslen);\n+\n+\t\terr = crypto_wait_req(crypto_acomp_compress(zc-\u003ecomp_ctx-\u003eareq.req),\n+\t\t\t\t      \u0026zc-\u003ecomp_ctx-\u003eareq.wait);\n+\n+\t\t/*\n+\t\t * If a page cannot be compressed into a size smaller than\n+\t\t * PAGE_SIZE, save the content as is without a compression, to\n+\t\t * keep the LRU order of writebacks.  If writeback is disabled,\n+\t\t * reject the page since it only adds metadata overhead.\n+\t\t * swap_writeout() will put the page back to the active LRU list\n+\t\t * in the case.\n+\t\t */\n+\t\tif (err \u0026\u0026 !zc-\u003ewb_enabled)\n+\t\t\tgoto compress_error;\n+\n+\t\t/*\n+\t\t * For each output SG list in @acomp_ctx-\u003ereq-\u003esg_table-\u003esgl,\n+\t\t * the @sg-\u003elength should be set to either the page's compressed\n+\t\t * length (success), or it's negative value compression error.\n+\t\t */\n+\t\tfor_each_sg(dst_sg, sg, zc-\u003enr_batch_pages, comp_batch_iter) {\n+\n+\t\t\tif (sg-\u003elength \u003c= 0 || sg-\u003elength \u003e= PAGE_SIZE) {\n+\t\t\t\tif (!zc-\u003ewb_enabled) {\n+\t\t\t\t\tif (sg-\u003elength == PAGE_SIZE)\n+\t\t\t\t\t\tsg-\u003elength = -ENOSPC;\n+\t\t\t\t\telse if (WARN_ON_ONCE(!sg-\u003elength || sg-\u003elength \u003e PAGE_SIZE))\n+\t\t\t\t\t\tsg-\u003elength = -EINVAL;\n+\t\t\t\t\tgoto compress_error;\n+\t\t\t\t}\n+\n+\t\t\t\tsg-\u003elength = PAGE_SIZE;\n+\t\t\t}\n \t\t}\n-\t\trcu_read_unlock();\n-\t\tcomp_ret = 0;\n-\t\tdlen = PAGE_SIZE;\n-\t\tdst = kmap_local_folio(folio, index * PAGE_SIZE);\n-\t\tmapped = true;\n \t}\n \n-\tgfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;\n-\thandle = zs_malloc(pool-\u003ezs_pool, dlen, gfp, folio_nid(folio));\n-\tif (IS_ERR_VALUE(handle)) {\n-\t\talloc_ret = PTR_ERR((void *)handle);\n-\t\tgoto unlock;\n-\t}\n+\treturn zswap_update_entries(zc);\n \n-\tzs_obj_write(pool-\u003ezs_pool, handle, dst, dlen);\n-\tentry-\u003ehandle = handle;\n-\tentry-\u003elength = dlen;\n+compress_error:\n+\tfor_each_sg(dst_sg, sg, zc-\u003enr_batch_pages, comp_batch_iter) {\n+\t\tif ((int)sg-\u003elength \u003c 0) {\n+\t\t\tif ((int)sg-\u003elength == -ENOSPC)\n+\t\t\t\tzswap_reject_compress_poor++;\n+\t\t\telse\n+\t\t\t\tzswap_reject_compress_fail++;\n+\t\t}\n+\t}\n \n-unlock:\n-\tif (mapped)\n-\t\tkunmap_local(dst);\n-\tif (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)\n-\t\tzswap_reject_compress_poor++;\n-\telse if (comp_ret)\n-\t\tzswap_reject_compress_fail++;\n-\telse if (alloc_ret)\n-\t\tzswap_reject_alloc_fail++;\n-\n-\tmutex_unlock(\u0026comp_ctx-\u003eareq.mutex);\n-\treturn comp_ret == 0 \u0026\u0026 alloc_ret == 0;\n+\treturn false;\n }\n \n static bool __zswap_decompress(struct zswap_entry *entry,\n@@ -1527,89 +1734,194 @@ static void shrink_worker(struct work_struct *w)\n * main API\n **********************************/\n \n-static bool zswap_store_page(struct folio *folio, long index,\n-\t\t\t     struct obj_cgroup *objcg,\n-\t\t\t     struct zswap_pool *pool)\n+/* Functions called by zswap_store(), that operate on batches of pages. */\n+\n+/*\n+ * zswap_init_entries() co-locates most of the zswap entry fields' setup under\n+ * the assumption that all phases of store processing will succeed. An important\n+ * marker to simplify catch-all cleanup in zswap_store_pages() is done by\n+ * initializing each entry's @handle to ERR_PTR(-EINVAL).\n+ */\n+static inline void zswap_init_entries(struct zswap_store_context *zc,\n+\t\t\t\t      struct zswap_pool *pool,\n+\t\t\t\t      struct obj_cgroup *objcg)\n {\n-\tswp_entry_t page_swpentry = folio_swap_entry(folio, index);\n-\tstruct zswap_entry *entry, *old;\n+\tstruct zswap_entry *entry;\n+\tu8 i;\n \n-\t/* allocate entry */\n-\tentry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));\n-\tif (!entry) {\n-\t\tzswap_reject_kmemcache_fail++;\n-\t\treturn false;\n+\t/*\n+\t * We co-locate entry initialization as much as possible here to\n+\t * minimize potential cache misses.\n+\t */\n+\tfor (i = 0; i \u003c zc-\u003enr_pages; ++i) {\n+\t\tentry = zc-\u003eentries[i];\n+\t\tentry-\u003ehandle = (unsigned long)ERR_PTR(-EINVAL);\n+\t\t/*\n+\t\t * Set pool_idx before the xa_store() in zswap_tree_stores()\n+\t\t * publishes the entry, or a concurrent reader could resolve a\n+\t\t * stale pool_idx left by slab reuse to an unrelated live pool.\n+\t\t */\n+\t\tentry-\u003epool_idx = pool-\u003eidx;\n+\t\tentry-\u003eswpentry = folio_swap_entry(zc-\u003efolio,\n+\t\t\t\t\t\t\tzc-\u003efolio_start + i);\n+\t\tentry-\u003eobjcg = objcg;\n+\t\tentry-\u003ereferenced = true;\n+\t\tentry-\u003enid = zc-\u003enid;\n+\t\tINIT_LIST_HEAD(\u0026entry-\u003elru);\n \t}\n+}\n \n-\tif (!zswap_compress(folio, index, entry, pool))\n-\t\tgoto compress_failed;\n+/*\n+ * zswap_tree_stores() stores the batch entries in xarray and adds them to the\n+ * zswap LRU list. If an error occurs during xa_store(), localized state\n+ * unwinding is done to erase prior batch entries from the xarray and delete\n+ * them from the zswap LRU list. Remaining batch cleanup occurs in\n+ * zswap_store_pages(). As batch entries are processed, we maintain aggregate\n+ * statistics to do the accounting in one bulk operation.\n+ */\n+static bool zswap_tree_stores(struct zswap_store_context *zc,\n+\t\t\t      struct zswap_pool *pool,\n+\t\t\t      struct obj_cgroup *objcg)\n+{\n+\tu8 i, store_fail_idx = 0, nr_incomp = 0;\n+\tsize_t total_batch_comp_length = 0;\n+\tstruct zswap_entry *old, *entry;\n+\n+\tfor (i = 0; i \u003c zc-\u003enr_pages; ++i) {\n+\t\tentry = zc-\u003eentries[i];\n+\n+\t\told = xa_store(swap_zswap_tree(entry-\u003eswpentry),\n+\t\t\t       swp_offset(entry-\u003eswpentry),\n+\t\t\t       entry, GFP_KERNEL);\n+\t\tif (unlikely(xa_is_err(old))) {\n+\t\t\tint err = xa_err(old);\n+\n+\t\t\tWARN_ONCE(err != -ENOMEM, \"unexpected xarray error: %d\\n\", err);\n+\t\t\tzswap_reject_alloc_fail++;\n+\t\t\t/*\n+\t\t\t * Entries up to this point have been stored in the\n+\t\t\t * xarray. Local cleanup in 'tree_store_failed' needs to\n+\t\t\t * erase entries up until now.\n+\t\t\t */\n+\t\t\tstore_fail_idx = i;\n+\t\t\tgoto tree_store_failed;\n+\t\t}\n \n-\t/*\n-\t * Set pool_idx before the xa_store() below publishes the entry, or a\n-\t * concurrent reader could resolve a stale pool_idx left by slab reuse\n-\t * to an unrelated live pool.\n-\t */\n-\tentry-\u003epool_idx = pool-\u003eidx;\n+\t\t/*\n+\t\t * We may have had an existing entry that became stale when\n+\t\t * the folio was redirtied and now the new version is being\n+\t\t * swapped out. Get rid of the old.\n+\t\t */\n+\t\tif (unlikely(old))\n+\t\t\tzswap_entry_free(old);\n \n-\told = xa_store(swap_zswap_tree(page_swpentry),\n-\t\t       swp_offset(page_swpentry),\n-\t\t       entry, GFP_KERNEL);\n-\tif (xa_is_err(old)) {\n-\t\tint err = xa_err(old);\n+\t\ttotal_batch_comp_length += entry-\u003elength;\n \n-\t\tWARN_ONCE(err != -ENOMEM, \"unexpected xarray error: %d\\n\", err);\n-\t\tzswap_reject_alloc_fail++;\n-\t\tgoto store_failed;\n-\t}\n+\t\tif (unlikely(entry-\u003elength == PAGE_SIZE))\n+\t\t\t++nr_incomp;\n \n-\t/*\n-\t * We may have had an existing entry that became stale when\n-\t * the folio was redirtied and now the new version is being\n-\t * swapped out. Get rid of the old.\n-\t */\n-\tif (old)\n-\t\tzswap_entry_free(old);\n+\t\t/*\n+\t\t * We finish by adding the entry to the LRU while it's already\n+\t\t * in xarray. This is safe because:\n+\t\t *\n+\t\t * 1. Concurrent stores and invalidations are excluded by folio lock.\n+\t\t *\n+\t\t * 2. Writeback is excluded by the entry not being on the LRU yet.\n+\t\t *    The publishing order matters to prevent writeback from seeing\n+\t\t *    an incoherent entry.\n+\t\t */\n+\t\tif (likely(entry-\u003elength))\n+\t\t\tzswap_lru_add(entry);\n+\t}\n \n \t/*\n-\t * The entry is successfully compressed and stored in the tree, there is\n-\t * no further possibility of failure. Grab refs to the pool and objcg,\n-\t * charge zswap memory, and increment zswap_stored_pages.\n-\t * The opposite actions will be performed by zswap_entry_free()\n-\t * when the entry is removed from the tree.\n+\t * Each batch entry is successfully compressed and stored in the tree,\n+\t * and further failures will be cleaned up in zswap_store().\n+\t * Next, we do bulk accounting for the batch: grab refs to the pool and\n+\t * objcg, charge zswap memory, increment zswap_stored_pages and\n+\t * zswap_stored_incompressible_pages. The opposite actions will be\n+\t * performed by zswap_entry_free() when the entry is removed from the\n+\t * tree.\n \t */\n-\tzswap_pool_get(pool);\n+\tzswap_pool_get_many(pool, zc-\u003enr_pages);\n \tif (objcg) {\n-\t\tobj_cgroup_get(objcg);\n-\t\tobj_cgroup_charge_zswap(objcg, entry-\u003elength);\n+\t\tobj_cgroup_get_many(objcg, zc-\u003enr_pages);\n+\t\tobj_cgroup_charge_zswap_many(objcg, total_batch_comp_length,\n+\t\t\t\t\t     zc-\u003enr_pages, nr_incomp);\n \t}\n-\tatomic_long_inc(\u0026zswap_stored_pages);\n-\tif (entry-\u003elength == PAGE_SIZE)\n-\t\tatomic_long_inc(\u0026zswap_stored_incompressible_pages);\n+\tatomic_long_add(zc-\u003enr_pages, \u0026zswap_stored_pages);\n+\tatomic_long_add(nr_incomp, \u0026zswap_stored_incompressible_pages);\n+\n+\treturn true;\n+\n+tree_store_failed:\n+\tfor (i = 0; i \u003c store_fail_idx; ++i) {\n+\t\txa_erase(swap_zswap_tree(zc-\u003eentries[i]-\u003eswpentry),\n+\t\t\t swp_offset(zc-\u003eentries[i]-\u003eswpentry));\n+\t\tzswap_lru_del(zc-\u003eentries[i]);\n+\t}\n+\n+\treturn false;\n+}\n+\n+/*\n+ * Store multiple pages in @zc-\u003efolio, starting from the page at index @start up\n+ * to the page at index @end-1, i.e. @zc-\u003enr_pages. Each phase of\n+ * zswap_store_pages() does batch processing of @zc-\u003enr_pages in the folio.\n+ * State unwinding is kept to a minimum as a result of the batch processing: a\n+ * batch proceeds to the next phase of store processing only if it passes this\n+ * stage. The batch cleanup is consolidated under the @store_pages_failed\n+ * label. Errors in storing a batch result in folio level cleanup occurring in\n+ * zswap_store().\n+ */\n+static bool zswap_store_pages(struct zswap_store_context *zc,\n+\t\t\t      long start,\n+\t\t\t      long end,\n+\t\t\t      struct zswap_pool *pool,\n+\t\t\t      struct obj_cgroup *objcg)\n+{\n+\tbool ret;\n+\tu8 i;\n+\n+\tzc-\u003enr_pages = end - start;\n+\tzc-\u003efolio_start = start;\n+\n+\tVM_WARN_ON_ONCE(zc-\u003enr_pages \u003c= 0 || zc-\u003enr_pages \u003e ZSWAP_MAX_BATCH_SIZE);\n+\n+\tret = zswap_entries_cache_alloc_batch(zc, GFP_KERNEL);\n+\tif (unlikely(!ret))\n+\t\treturn false;\n+\n+\tzswap_init_entries(zc, pool, objcg);\n \n \t/*\n-\t * We finish initializing the entry while it's already in xarray.\n-\t * This is safe because:\n-\t *\n-\t * 1. Concurrent stores and invalidations are excluded by folio lock.\n-\t *\n-\t * 2. Writeback is excluded by the entry not being on the LRU yet.\n-\t *    The publishing order matters to prevent writeback from seeing\n-\t *    an incoherent entry.\n+\t * Locking the comp_ctx mutex once per store batch results in better\n+\t * performance as compared to locking per compress batch.\n \t */\n-\tentry-\u003eswpentry = page_swpentry;\n-\tentry-\u003eobjcg = objcg;\n-\tentry-\u003ereferenced = true;\n-\tif (entry-\u003elength) {\n-\t\tINIT_LIST_HEAD(\u0026entry-\u003elru);\n-\t\tzswap_lru_add(entry);\n-\t}\n+\tzc-\u003ecomp_ctx = \u0026raw_cpu_ptr(pool-\u003eacomp_ctx)-\u003ecomp;\n+\tmutex_lock(\u0026zc-\u003ecomp_ctx-\u003eareq.mutex);\n+\n+\tret = zswap_compress(zc);\n+\n+\tmutex_unlock(\u0026zc-\u003ecomp_ctx-\u003eareq.mutex);\n+\n+\tif (unlikely(!ret))\n+\t\tgoto store_pages_failed;\n+\n+\tret = zswap_tree_stores(zc, pool, objcg);\n+\n+\tif (unlikely(!ret))\n+\t\tgoto store_pages_failed;\n \n \treturn true;\n \n-store_failed:\n-\tzs_free(pool-\u003ezs_pool, entry-\u003ehandle);\n-compress_failed:\n-\tzswap_entry_cache_free(entry);\n+store_pages_failed:\n+\tfor (i = 0; i \u003c zc-\u003enr_pages; ++i) {\n+\t\tif (!IS_ERR_VALUE(zc-\u003eentries[i]-\u003ehandle))\n+\t\t\tzs_free(pool-\u003ezs_pool, zc-\u003eentries[i]-\u003ehandle);\n+\t}\n+\n+\tzswap_entries_cache_free_batch(zc-\u003eentries, zc-\u003enr_pages);\n \treturn false;\n }\n \n@@ -1619,9 +1931,14 @@ bool zswap_store(struct folio *folio)\n \tswp_entry_t swp = folio-\u003eswap;\n \tstruct obj_cgroup *objcg = NULL;\n \tstruct mem_cgroup *memcg = NULL;\n-\tstruct zswap_pool *pool;\n+\tint nid = folio_nid(folio);\n \tbool ret = false;\n-\tlong index;\n+\tstruct zswap_pool *pool;\n+\tlong start, end;\n+\tstruct zswap_store_context zc = {\n+\t\t.folio = folio,\n+\t\t.nid = nid,\n+\t};\n \n \tVM_WARN_ON_ONCE(!folio_test_locked(folio));\n \tVM_WARN_ON_ONCE(!folio_test_swapcache(folio));\n@@ -1655,8 +1972,24 @@ bool zswap_store(struct folio *folio)\n \t\tmem_cgroup_put(memcg);\n \t}\n \n-\tfor (index = 0; index \u003c nr_pages; ++index) {\n-\t\tif (!zswap_store_page(folio, index, objcg, pool))\n+\tzc.zs_pool = pool-\u003ezs_pool;\n+\trcu_read_lock();\n+\tzc.wb_enabled = mem_cgroup_zswap_writeback_enabled(folio_memcg(folio));\n+\trcu_read_unlock();\n+\n+\t/*\n+\t * For batching and non-batching compressors, store the folio in\n+\t * batches of ZSWAP_MAX_BATCH_SIZE, where the pages in the batch\n+\t * are compressed in batches of the compressor's batch_size. This gives\n+\t * better performance than invoking zswap_store_pages() per-page,\n+\t * due to cache locality of working set structures.\n+\t */\n+\tfor (start = 0; start \u003c nr_pages; start += ZSWAP_MAX_BATCH_SIZE) {\n+\t\tend = min(start + ZSWAP_MAX_BATCH_SIZE, nr_pages);\n+\n+\t\tret = zswap_store_pages(\u0026zc, start, end, pool, objcg);\n+\n+\t\tif (unlikely(!ret))\n \t\t\tgoto put_pool;\n \t}\n \n@@ -1926,6 +2259,8 @@ static int zswap_setup(void)\n \tstruct zswap_pool *pool;\n \tint ret;\n \n+\tBUILD_BUG_ON(ZSWAP_MAX_COMPR_LENGTH_SHIFT \u003c= PAGE_SHIFT);\n+\n \tzswap_entry_cache = KMEM_CACHE(zswap_entry, 0);\n \tif (!zswap_entry_cache) {\n \t\tpr_err(\"entry cache creation failed\\n\");\n"
}

2/1 2026/11/09 05:46 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "ZSWAP",
    "SWAP",
    "MEMCG",
    "CRYPTO_ACOMP2",
    "ZSWAP_DEFAULT_ON"
  ],
  "FocusSymbols": [
    "zswap_store",
    "zswap_store_pages",
    "zswap_compress",
    "zswap_tree_stores",
    "obj_cgroup_charge_zswap_many",
    "crypto_acomp_compress",
    "crypto_acomp_batch_size"
  ],
  "Reasoning": "The patch introduces batch compression and storing functionality into zswap and crypto acompress, modifying core memory management and crypto subsystems. It restructures folio compression and xarray/LRU insertion in zswap, adjusts reference counting and error unwinding, adds bulk charging for memory cgroups (obj_cgroup_charge_zswap_many) with new VM_WARN_ON_ONCE assertions, and modifies request chaining in crypto acompress. This code is fully reachable in virtualized environments under memory pressure when swap/zswap is enabled.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 8c3cd021912957225a6d79bc78e90ca864c09e4b
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Nov 9 05:46:48 2026 +0000

    syz-cluster: applied patch under review

diff --git a/crypto/acompress.c b/crypto/acompress.c
index c8b4b8589769e3..df9e42f519c358 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -166,14 +166,18 @@ 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;
+
 	req->base.complete = cplt;
 	req->base.data = req;
+	req->unit_size = 0;
 }
 
 static void acomp_restore_req(struct acomp_req *req)
 {
 	req->base.complete = req->chain.compl;
 	req->base.data = req->chain.data;
+	req->unit_size = req->chain.unit_size;
 }
 
 static void acomp_reqchain_virt(struct acomp_req *req)
@@ -242,6 +246,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 = unlikely(err) ? err : req->dlen;
+
 	return err;
 }
 
@@ -266,10 +274,13 @@ static int acomp_do_req_chain(struct acomp_req *req, bool comp)
 {
 	int err;
 
+	if (unlikely(req->unit_size && req->slen > req->unit_size))
+		return -ENOSYS;
+
 	acomp_save_req(req, acomp_reqchain_done);
 
 	err = acomp_do_one_req(req, comp);
-	if (err == -EBUSY || err == -EINPROGRESS)
+	if (unlikely(err == -EBUSY || err == -EINPROGRESS))
 		return err;
 
 	return acomp_reqchain_finish(req, err);
@@ -279,10 +290,27 @@ int crypto_acomp_compress(struct acomp_req *req)
 {
 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
 
+	if (likely((req->unit_size == req->slen) && acomp_request_issg(req) &&
+		    !crypto_acomp_req_seg(tfm) && !acomp_is_async(tfm))) {
+		int err = tfm->compress(req);
+
+		req->dst->length = unlikely(err) ? err : req->dlen;
+		return err;
+	}
+
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+
+	if (req->unit_size && acomp_request_issg(req)) {
+		if (!crypto_acomp_req_seg(tfm))
+			return acomp_do_req_chain(req, true);
+
+		return tfm->compress(req);
+	}
+
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
-		return crypto_acomp_reqtfm(req)->compress(req);
+		return tfm->compress(req);
+
 	return acomp_do_req_chain(req, true);
 }
 EXPORT_SYMBOL_GPL(crypto_acomp_compress);
@@ -294,11 +322,25 @@ int crypto_acomp_decompress(struct acomp_req *req)
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
-		return crypto_acomp_reqtfm(req)->decompress(req);
+		return tfm->decompress(req);
 	return acomp_do_req_chain(req, false);
 }
 EXPORT_SYMBOL_GPL(crypto_acomp_decompress);
 
+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm)
+{
+	if (acomp_is_async(tfm) &&
+	    (crypto_comp_alg_common(tfm)->base.cra_type == &crypto_acomp_type)) {
+		struct acomp_alg *alg = crypto_acomp_alg(tfm);
+
+		if (alg && alg->batch_size > 1)
+			return alg->batch_size;
+	}
+
+	return 1;
+}
+EXPORT_SYMBOL_GPL(crypto_acomp_batch_size);
+
 void comp_prepare_alg(struct comp_alg_common *alg)
 {
 	struct crypto_alg *base = &alg->base;
diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h
index 5d5358dfab7345..3ce4704dc8795d 100644
--- a/include/crypto/acompress.h
+++ b/include/crypto/acompress.h
@@ -67,6 +67,7 @@ struct acomp_req_chain {
 		struct folio *dfolio;
 	};
 	u32 flags;
+	u32 unit_size;
 };
 
 /**
@@ -79,6 +80,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 +96,7 @@ struct acomp_req {
 	};
 	unsigned int slen;
 	unsigned int dlen;
+	unsigned int unit_size;
 
 	struct acomp_req_chain chain;
 
@@ -197,7 +200,7 @@ static inline void acomp_request_set_tfm(struct acomp_req *req,
 	crypto_request_set_tfm(&req->base, crypto_acomp_tfm(tfm));
 }
 
-static inline bool acomp_is_async(struct crypto_acomp *tfm)
+static __always_inline bool acomp_is_async(struct crypto_acomp *tfm)
 {
 	return crypto_comp_alg_common(tfm)->base.cra_flags &
 	       CRYPTO_ALG_ASYNC;
@@ -292,7 +295,7 @@ static inline void *acomp_request_extra(struct acomp_req *req)
 	return (void *)((char *)req + len);
 }
 
-static inline bool acomp_req_on_stack(struct acomp_req *req)
+static __always_inline bool acomp_req_on_stack(struct acomp_req *req)
 {
 	return crypto_req_on_stack(&req->base);
 }
@@ -329,9 +332,56 @@ 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.
+ *
+ * This is a helper function that enables batching for zswap, IPComp, etc.
+ * It allows multiple independent compression (or decompression) operations to
+ * be submitted in a single request's SG lists, where each SG list ("segment")
+ * is processed independently. The unit size helps derive segments from a
+ * single request. crypto_acomp does not expect the segments to be related in
+ * any way.
+ *
+ * Example usage model:
+ *
+ * A module such as zswap that's configured to use a batching compressor, can
+ * accomplish batch compression of "nr_pages" with crypto_acomp by creating 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. Depending on whether the @req is used for batch compress/decompress,
+ * zswap must set the @req's source/destination length to be
+ * "nr_pages * @req->unit_size" respectively.
+ *
+ * An acomp_alg can implement batch compression by using the @req->unit_size
+ * to break down the SG lists passed in via @req->dst to submit individual
+ * "@req->slen/@req->unit_size" compress jobs to be processed as a batch.
+ *
+ * Similarly, zswap can implement batch decompression by passing an
+ * SG table with "nr_pages" SG lists via @req->src to process
+ * "@req->dlen/@req->unit_size" decompress jobs as a batch.
+ *
+ * This API must be called after acomp_request_set_callback(),
+ * which sets @req->unit_size to 0. This makes it easy for users of
+ * crypto_acomp to rely on a default of not opting in to batching.
+ * Users such as zswap opt in to batching by defining @req->unit_size
+ * to a non-zero value for use by acomp_algs supporting batching.
+ *
+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.
+ *
+ * @req:	asynchronous compress/decompress request
+ * @du:		data unit size of the input/output 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
  *
@@ -543,6 +593,18 @@ 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:	@tfm's acomp_alg's @batch_size, if it has defined a
+ *		@batch_size greater than 1; else return 1.
+ */
+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm);
+
 static inline struct acomp_req *acomp_request_on_stack_init(
 	char *buf, struct crypto_acomp *tfm)
 {
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h
index 05deea9dac5e88..7d406cfe57519f 100644
--- a/include/crypto/algapi.h
+++ b/include/crypto/algapi.h
@@ -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;
diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h
index 9cd37df32dc40a..2d4d302d1b3f18 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
+ * @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
@@ -47,6 +49,7 @@
 struct acomp_alg {
 	int (*compress)(struct acomp_req *req);
 	int (*decompress)(struct acomp_req *req);
+	unsigned int batch_size;
 	int (*init)(struct crypto_acomp *tfm);
 	void (*exit)(struct crypto_acomp *tfm);
 
@@ -146,7 +149,7 @@ void crypto_unregister_acomp(struct acomp_alg *alg);
 int crypto_register_acomps(struct acomp_alg *algs, int count);
 void crypto_unregister_acomps(struct acomp_alg *algs, int count);
 
-static inline bool acomp_request_issg(struct acomp_req *req)
+static __always_inline bool acomp_request_issg(struct acomp_req *req)
 {
 	return !(req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
 				    CRYPTO_ACOMP_REQ_DST_VIRT));
@@ -189,6 +192,11 @@ static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm)
 	return crypto_tfm_req_virt(&tfm->base);
 }
 
+static __always_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);
 
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index b7c97f1c47c924..0628b62e494668 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -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. */
 
 /*
@@ -489,7 +492,7 @@ static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm)
 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
 }
 
-static inline bool crypto_req_on_stack(struct crypto_async_request *req)
+static __always_inline bool crypto_req_on_stack(struct crypto_async_request *req)
 {
 	return req->flags & CRYPTO_TFM_REQ_ON_STACK;
 }
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 980358bc6f49c9..09a6b9a419016b 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1921,6 +1921,8 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,
 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,
+				  u8 nr_pages, u8 nr_incomp_pages);
 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
 bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);
 #else
@@ -1932,6 +1934,11 @@ static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
 					   size_t size)
 {
 }
+static inline void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg,
+						size_t size, u8 nr_pages,
+						u8 nr_incomp_pages)
+{
+}
 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
 					     size_t size)
 {
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1ebc9330aeae51..48c216e621daa6 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6361,6 +6361,42 @@ void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
 	rcu_read_unlock();
 }
 
+/**
+ * obj_cgroup_charge_zswap_many - bulk charge compression backend memory
+ *                                from compressing a batch of pages.
+ * @objcg: the object cgroup
+ * @size: total compressed length of a batch of compressed objects
+ * @nr_pages: # of pages that were compressed to store in zswap
+ * @nr_incomp_pages: # of incompressible pages to store in zswap
+ *
+ * This forces the charge after obj_cgroup_may_zswap() allowed
+ * compression and storage in zswap for this cgroup to go ahead.
+ */
+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,
+				  u8 nr_pages, u8 nr_incomp_pages)
+{
+	struct mem_cgroup *memcg;
+
+	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
+		return;
+
+	if (obj_cgroup_is_root(objcg))
+		return;
+
+	VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
+
+	/* PF_MEMALLOC context, charging must succeed */
+	if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
+		VM_WARN_ON_ONCE(1);
+
+	rcu_read_lock();
+	memcg = obj_cgroup_memcg(objcg);
+	mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
+	mod_memcg_state(memcg, MEMCG_ZSWAPPED, nr_pages);
+	mod_memcg_state(memcg, MEMCG_ZSWAP_INCOMP, nr_incomp_pages);
+	rcu_read_unlock();
+}
+
 /**
  * obj_cgroup_uncharge_zswap - uncharge compression backend memory
  * @objcg: the object cgroup
diff --git a/mm/zswap.c b/mm/zswap.c
index 37d217860a6b2e..67181d4fe7e4de 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -85,6 +85,9 @@ static bool zswap_pool_reached_full;
 
 #define ZSWAP_PARAM_UNSET ""
 
+/* Limit the batch size to limit per-CPU memory usage for dst buffers. */
+#define ZSWAP_MAX_BATCH_SIZE 4U
+
 static int zswap_setup(void);
 
 /* Enable/disable zswap */
@@ -144,10 +147,17 @@ struct zswap_acomp_req {
 	struct mutex mutex;
 };
 
-/* The compression mutex also protects the output buffer. */
+/*
+ * The compression mutex also protects the output buffer(s).
+ *
+ * @compr_batch_size: The max batch size of the compression algorithm,
+ *                    bounded by ZSWAP_MAX_BATCH_SIZE.
+ */
 struct zswap_comp_ctx {
 	struct zswap_acomp_req areq;
-	u8 *buffer;
+	u8 compr_batch_size;
+	struct sg_table *sg_table;
+	u8 **buffers;
 };
 
 /* Separate requests, so that decompression does not wait for compression. */
@@ -162,6 +172,7 @@ struct crypto_acomp_ctx {
  * The only case where lru_lock is not acquired while holding tree.lock is
  * when a zswap_entry is taken off the lru for writeback, in that case it
  * needs to be verified that it's still valid in the tree.
+ *
  */
 struct zswap_pool {
 	struct zs_pool *zs_pool;
@@ -181,6 +192,7 @@ struct zswap_pool {
  */
 #define ZSWAP_FIRST_POOL_ID 1
 #define ZSWAP_MAX_POOL_ID U8_MAX
+#define ZSWAP_MAX_COMPR_LENGTH_SHIFT 19U
 static DEFINE_XARRAY_FLAGS(zswap_pools, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH);
 static struct zswap_pool __rcu *zswap_current_pool;
 
@@ -207,20 +219,37 @@ static struct shrinker *zswap_shrinker;
  *              logic if referenced is unset. See comments in the shrinker
  *              section for context.
  * pool_idx - id of the zswap_pool that the entry's data is in.
+ * nid - NUMA node id of the page for which this is the zswap entry.
  * handle - zsmalloc allocation handle that stores the compressed page data
  * objcg - the obj_cgroup that the compressed memory is charged to
  * lru - handle to the pool's lru used to evict pages.
  */
 struct zswap_entry {
 	swp_entry_t swpentry;
-	unsigned int length;
-	bool referenced;
-	u8 pool_idx;
+	struct {
+		unsigned int length:ZSWAP_MAX_COMPR_LENGTH_SHIFT;
+		bool referenced:1U;
+		u8 pool_idx;
+	};
+	int nid;
 	unsigned long handle;
 	struct obj_cgroup *objcg;
 	struct list_head lru;
 };
 
+struct zswap_store_context {
+	struct folio *folio;
+	long folio_start;
+	unsigned int slen;
+	u8 nr_batch_pages;
+	bool wb_enabled;
+	int nid;
+	u8 nr_pages;
+	struct zs_pool *zs_pool;
+	struct zswap_comp_ctx *comp_ctx;
+	struct zswap_entry *entries[ZSWAP_MAX_BATCH_SIZE];
+};
+
 /*
  * No RCU section is needed around the returned pointer: a stored entry pins
  * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be
@@ -279,6 +308,8 @@ static void __zswap_pool_empty(struct percpu_ref *ref);
 
 static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
 {
+	u8 i;
+
 	if (!acomp_ctx)
 		return;
 
@@ -297,8 +328,19 @@ static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
 
 	acomp_ctx->acomp = NULL;
 
-	kfree(acomp_ctx->comp.buffer);
-	acomp_ctx->comp.buffer = NULL;
+	if (acomp_ctx->comp.buffers) {
+		for (i = 0; i < ZSWAP_MAX_BATCH_SIZE; ++i)
+			kfree(acomp_ctx->comp.buffers[i]);
+		kfree(acomp_ctx->comp.buffers);
+	}
+	acomp_ctx->comp.buffers = NULL;
+	acomp_ctx->comp.compr_batch_size = 0;
+
+	if (acomp_ctx->comp.sg_table) {
+		sg_free_table(acomp_ctx->comp.sg_table);
+		kfree(acomp_ctx->comp.sg_table);
+	}
+	acomp_ctx->comp.sg_table = NULL;
 }
 
 static struct zswap_pool *zswap_pool_create(char *compressor)
@@ -383,6 +425,7 @@ static struct zswap_pool *zswap_pool_create(char *compressor)
 cpuhp_add_fail:
 	for_each_possible_cpu(cpu)
 		acomp_ctx_free(per_cpu_ptr(pool->acomp_ctx, cpu));
+
 error:
 	if (pool->acomp_ctx)
 		free_percpu(pool->acomp_ctx);
@@ -464,12 +507,12 @@ static int __must_check zswap_pool_tryget(struct zswap_pool *pool)
 }
 
 /* The caller must already have a reference. */
-static void zswap_pool_get(struct zswap_pool *pool)
+static inline void zswap_pool_get_many(struct zswap_pool *pool, u8 nr)
 {
-	percpu_ref_get(&pool->ref);
+	percpu_ref_get_many(&pool->ref, nr);
 }
 
-static void zswap_pool_put(struct zswap_pool *pool)
+static inline void zswap_pool_put(struct zswap_pool *pool)
 {
 	percpu_ref_put(&pool->ref);
 }
@@ -680,14 +723,8 @@ static int zswap_enabled_param_set(const char *val,
 * lru functions
 **********************************/
 
-static inline int entry_to_nid(struct zswap_entry *entry)
-{
-	return page_to_nid(virt_to_page(entry));
-}
-
 static void zswap_lru_add(struct zswap_entry *entry)
 {
-	int nid = entry_to_nid(entry);
 	struct mem_cgroup *memcg;
 
 	/*
@@ -704,19 +741,18 @@ static void zswap_lru_add(struct zswap_entry *entry)
 	rcu_read_lock();
 	memcg = obj_cgroup_memcg(entry->objcg);
 	/* will always succeed */
-	list_lru_add(&zswap_list_lru, &entry->lru, nid, memcg);
+	list_lru_add(&zswap_list_lru, &entry->lru, entry->nid, memcg);
 	rcu_read_unlock();
 }
 
 static void zswap_lru_del(struct zswap_entry *entry)
 {
-	int nid = entry_to_nid(entry);
 	struct mem_cgroup *memcg;
 
 	rcu_read_lock();
 	memcg = obj_cgroup_memcg(entry->objcg);
 	/* will always succeed */
-	list_lru_del(&zswap_list_lru, &entry->lru, nid, memcg);
+	list_lru_del(&zswap_list_lru, &entry->lru, entry->nid, memcg);
 	rcu_read_unlock();
 }
 
@@ -764,10 +800,11 @@ void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg)
 **********************************/
 static struct kmem_cache *zswap_entry_cache;
 
-static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid)
+static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
 {
 	struct zswap_entry *entry;
-	entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid);
+
+	entry = kmem_cache_alloc(zswap_entry_cache, gfp);
 	if (!entry)
 		return NULL;
 	return entry;
@@ -778,6 +815,41 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)
 	kmem_cache_free(zswap_entry_cache, entry);
 }
 
+static inline void zswap_entries_cache_free_batch(
+	struct zswap_entry **entries,
+	u8 nr_entries)
+{
+	/*
+	 * It is okay to use this to free entries allocated separately
+	 * by zswap_entry_cache_alloc().
+	 */
+	kmem_cache_free_bulk(zswap_entry_cache, nr_entries, (void **)entries);
+}
+
+static bool zswap_entries_cache_alloc_batch(
+	struct zswap_store_context *zc,
+	gfp_t gfp)
+{
+	bool ret = kmem_cache_alloc_bulk(zswap_entry_cache, gfp,
+					 zc->nr_pages, (void **)zc->entries);
+
+	if (unlikely(!ret)) {
+		u8 i;
+
+		for (i = 0; i < zc->nr_pages; ++i) {
+			zc->entries[i] = zswap_entry_cache_alloc(GFP_KERNEL);
+
+			if (unlikely(!zc->entries[i])) {
+				zswap_reject_kmemcache_fail++;
+				zswap_entries_cache_free_batch(zc->entries, i);
+				return false;
+			}
+		}
+	}
+
+	return true;
+}
+
 /*
  * Carries out the common pattern of freeing an entry's zsmalloc allocation,
  * freeing the entry itself, and decrementing the number of stored pages.
@@ -787,7 +859,7 @@ static void zswap_entry_free(struct zswap_entry *entry)
 	struct zswap_pool *pool = zswap_entry_pool(entry);
 
 	zswap_lru_del(entry);
-	if (!WARN_ON_ONCE(!pool)) {
+	if (likely(!WARN_ON_ONCE(!pool))) {
 		zs_free(pool->zs_pool, entry->handle);
 		zswap_pool_put(pool);
 	}
@@ -827,6 +899,12 @@ static int zswap_acomp_req_init(struct zswap_acomp_req *areq,
 	crypto_init_wait(&areq->wait);
 	zswap_set_acomp_req_callback(areq->req, &areq->wait);
 
+	/*
+	 * This should be called after acomp_request_set_callback(),
+	 * which initializes req->unit_size to 0.
+	 */
+	acomp_request_set_unit_size(areq->req, PAGE_SIZE);
+
 	mutex_init(&areq->mutex);
 	return 0;
 }
@@ -835,7 +913,10 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 {
 	struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
 	struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
+	int nid = cpu_to_node(cpu);
+	struct scatterlist *sg;
 	int ret = -ENOMEM;
+	u8 i;
 
 	/*
 	 * To handle cases where the CPU goes through online-offline-online
@@ -846,15 +927,11 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 		return 0;
 	}
 
-	acomp_ctx->comp.buffer = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu));
-	if (!acomp_ctx->comp.buffer)
-		return ret;
-
 	/*
 	 * In case of an error, crypto_alloc_acomp_node() returns an
 	 * error pointer, never NULL.
 	 */
-	acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
+	acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, nid);
 	if (IS_ERR(acomp_ctx->acomp)) {
 		pr_err("could not alloc crypto acomp %s : %pe\n",
 				pool->tfm_name, acomp_ctx->acomp);
@@ -862,10 +939,55 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 		goto fail;
 	}
 
+	/*
+	 * Constrain the crypto algorithm's batch size by ZSWAP_MAX_BATCH_SIZE,
+	 * store this in the per-CPU zswap_comp_ctx's @compr_batch_size.
+	 */
+	acomp_ctx->comp.compr_batch_size = min(ZSWAP_MAX_BATCH_SIZE,
+					       crypto_acomp_batch_size(acomp_ctx->acomp));
+
 	ret = zswap_acomp_req_init(&acomp_ctx->comp.areq, acomp_ctx->acomp);
 	if (ret)
 		goto req_fail;
 
+	/*
+	 * Allocate up to ZSWAP_MAX_BATCH_SIZE dst buffers for all compressors.
+	 */
+	acomp_ctx->comp.buffers = kcalloc_node(ZSWAP_MAX_BATCH_SIZE,
+					       sizeof(u8 *), GFP_KERNEL, nid);
+	if (!acomp_ctx->comp.buffers) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	for (i = 0; i < ZSWAP_MAX_BATCH_SIZE; ++i) {
+		acomp_ctx->comp.buffers[i] = kmalloc_node(PAGE_SIZE, GFP_KERNEL, nid);
+		if (!acomp_ctx->comp.buffers[i]) {
+			ret = -ENOMEM;
+			goto req_fail;
+		}
+	}
+
+	acomp_ctx->comp.sg_table = kmalloc(sizeof(*acomp_ctx->comp.sg_table),
+					   GFP_KERNEL);
+	if (!acomp_ctx->comp.sg_table) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	if (sg_alloc_table(acomp_ctx->comp.sg_table, ZSWAP_MAX_BATCH_SIZE,
+			   GFP_KERNEL)) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	/*
+	 * Statically map the per-CPU destination buffers to the per-CPU
+	 * SG lists; crucial for performance.
+	 */
+	for_each_sg(acomp_ctx->comp.sg_table->sgl, sg, ZSWAP_MAX_BATCH_SIZE, i)
+		sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE);
+
 	/* Skip the per-CPU request when decompression can use the stack. */
 	if (acomp_is_async(acomp_ctx->acomp)) {
 		ret = zswap_acomp_req_init(&acomp_ctx->decomp, acomp_ctx->acomp);
@@ -882,95 +1004,180 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	return ret;
 }
 
-static bool zswap_compress(struct folio *folio, long index,
-			   struct zswap_entry *entry, struct zswap_pool *pool)
+/*
+ * zswap_update_entries() is called only when all pages in a store batch have
+ * been successfully compressed (subsumes the writeback_enabled fallback in case
+ * of compress errors). The only error that can occur in this phase is zs_malloc
+ * allocation failure, upon which an error status is returned to
+ * zswap_store_pages().
+ */
+static inline bool zswap_update_entries(struct zswap_store_context *zc)
 {
-	struct zswap_comp_ctx *comp_ctx;
-	struct scatterlist input, output;
-	int comp_ret = 0, alloc_ret = 0;
-	unsigned int dlen = PAGE_SIZE;
+	gfp_t gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
+	struct zswap_entry *entry;
+	struct scatterlist *sg;
 	unsigned long handle;
-	gfp_t gfp;
-	u8 *dst;
-	bool mapped = false;
+	unsigned int dlen;
+	u8 batch_iter;
+	u8 *buf;
 
-	comp_ctx = &raw_cpu_ptr(pool->acomp_ctx)->comp;
-	mutex_lock(&comp_ctx->areq.mutex);
+	/*
+	 * For each output SG list in @acomp_ctx->req->sg_table->sgl,
+	 * the @sg->length should be set to either the page's compressed
+	 * length (success), or it's negative value compression error.
+	 */
+	for_each_sg(zc->comp_ctx->sg_table->sgl, sg, zc->nr_pages, batch_iter) {
+		entry = zc->entries[batch_iter];
+		dlen = sg->length;
+
+		prefetchw(entry);
 
-	dst = comp_ctx->buffer;
-	sg_init_table(&input, 1);
-	sg_set_folio(&input, folio, PAGE_SIZE, index * PAGE_SIZE);
+		handle = zs_malloc(zc->zs_pool, dlen, gfp, zc->nid);
+
+		if (unlikely(IS_ERR_VALUE(handle))) {
+			if (PTR_ERR((void *)handle) == -ENOSPC)
+				zswap_reject_compress_poor++;
+			else
+				zswap_reject_alloc_fail++;
+
+			return false;
+		}
+
+		entry->length = dlen;
+		entry->handle = handle;
+
+		if (unlikely(dlen == PAGE_SIZE)) {
+			buf = kmap_local_folio(zc->folio,
+					(zc->folio_start + batch_iter) * PAGE_SIZE);
+			zs_obj_write(zc->zs_pool, handle, buf, dlen);
+			kunmap_local(buf);
+		} else {
+			buf = zc->comp_ctx->buffers[batch_iter];
+			zs_obj_write(zc->zs_pool, handle, buf, dlen);
+		}
+	}
+
+	return true;
+}
 
-	sg_init_one(&output, dst, PAGE_SIZE);
-	acomp_request_set_params(comp_ctx->areq.req, &input, &output,
-				 PAGE_SIZE, dlen);
+/*
+ * zswap_compress() batching implementation for sequential and batching
+ * compressors.
+ *
+ * Description:
+ * ============
+ * Compress multiple @zc->nr_pages in @zc->folio starting from the
+ * @zc->folio_start index in batches of @zc->nr_batch_pages; the latter
+ * representing the lesser of the @zc->nr_pages to be batch compressed and the
+ * compressor's batch size.
+ *
+ * @zc->nr_pages can be in (1, ZSWAP_MAX_BATCH_SIZE] even if the compressor does
+ * not support batching.
+ *
+ * If @nr_batch_pages is 1, each page is compressed sequentially.
+ *
+ * If @nr_batch_pages is > 1, the compressor may use batching to optimize
+ * compression.
+ *
+ * In all cases, the store processing that needs to happen post-compression
+ * occurs in batches of @zc->nr_pages. Error handling is minimal, mostly to
+ * update zswap statistics, with catch-all cleanup for the batch occuring in the
+ * @store_pages_failed label in zswap_store_pages().
+ */
+static bool zswap_compress(struct zswap_store_context *zc)
+{
+	u8 batch_start, comp_batch_iter;
+	struct scatterlist *sg, *dst_sg;
+	int err;
 
 	/*
-	 * it maybe looks a little bit silly that we send an asynchronous request,
-	 * then wait for its completion synchronously. This makes the process look
-	 * synchronous in fact.
-	 * Theoretically, acomp supports users send multiple acomp requests in one
-	 * acomp instance, then get those requests done simultaneously. but in this
-	 * case, zswap actually does store and load page by page, there is no
-	 * existing method to send the second page before the first page is done
-	 * in one thread doing zswap.
-	 * but in different threads running on different cpu, we have different
-	 * acomp instance, and compression and decompression use separate
-	 * requests, so multiple threads can do (de)compression in parallel.
+	 * @nr_batch_pages is constrained by the per-CPU comp_ctx's compressor
+	 * batch size and the @zc->nr_pages in this store batch. @slen indicates
+	 * the total source length bytes for @nr_batch_pages.
 	 */
-	comp_ret = crypto_wait_req(crypto_acomp_compress(comp_ctx->areq.req),
-				   &comp_ctx->areq.wait);
-	dlen = comp_ctx->areq.req->dlen;
-	if (!comp_ret) {
-		if (dlen == PAGE_SIZE)
-			comp_ret = -ENOSPC;
-		else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE))
-			comp_ret = -EINVAL;
-	}
+	zc->nr_batch_pages = min(zc->comp_ctx->compr_batch_size, zc->nr_pages);
+	zc->slen = zc->nr_batch_pages * PAGE_SIZE;
 
 	/*
-	 * If a page cannot be compressed into a size smaller than PAGE_SIZE,
-	 * save the content as is without a compression, to keep the LRU order
-	 * of writebacks.  If writeback is disabled, reject the page since it
-	 * only adds metadata overhead.  swap_writeout() will put the page back
-	 * to the active LRU list in the case.
+	 * Compress the @nr_pages in @folio starting at index @folio_start
+	 * in batches of @nr_batch_pages.
 	 */
-	if (comp_ret) {
-		rcu_read_lock();
-		if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
-			rcu_read_unlock();
-			goto unlock;
+	for (batch_start = 0; batch_start < zc->nr_pages;
+	     batch_start += zc->nr_batch_pages) {
+		/*
+		 * Send @nr_batch_pages to crypto_acomp for compression:
+		 *
+		 * These pages are in @folio's range of indices in the interval
+		 *     [@folio_start + @batch_start,
+		 *      @folio_start + @batch_start + @nr_batch_pages).
+		 *
+		 * The source folio pages in the batch are directly submitted to
+		 * crypto_acomp via acomp_request_set_src_folio().
+		 */
+		acomp_request_set_src_folio(zc->comp_ctx->areq.req, zc->folio,
+				(zc->folio_start + batch_start) * PAGE_SIZE,
+				zc->slen);
+
+		dst_sg = zc->comp_ctx->sg_table->sgl + batch_start;
+		dst_sg->length = zc->slen;
+
+		/*
+		 * The per-CPU @zc->comp_ctx->sg_table scatterlists are statically
+		 * mapped to the per-CPU dst @buffers at pool creation time.
+		 * There are ZSWAP_MAX_BATCH_SIZE per-CPU SG lists and dst
+		 * buffers allocated for all compressors.
+		 */
+		acomp_request_set_dst_sg(zc->comp_ctx->areq.req,
+					 dst_sg, zc->slen);
+
+		err = crypto_wait_req(crypto_acomp_compress(zc->comp_ctx->areq.req),
+				      &zc->comp_ctx->areq.wait);
+
+		/*
+		 * If a page cannot be compressed into a size smaller than
+		 * PAGE_SIZE, save the content as is without a compression, to
+		 * keep the LRU order of writebacks.  If writeback is disabled,
+		 * reject the page since it only adds metadata overhead.
+		 * swap_writeout() will put the page back to the active LRU list
+		 * in the case.
+		 */
+		if (err && !zc->wb_enabled)
+			goto compress_error;
+
+		/*
+		 * For each output SG list in @acomp_ctx->req->sg_table->sgl,
+		 * the @sg->length should be set to either the page's compressed
+		 * length (success), or it's negative value compression error.
+		 */
+		for_each_sg(dst_sg, sg, zc->nr_batch_pages, comp_batch_iter) {
+
+			if (sg->length <= 0 || sg->length >= PAGE_SIZE) {
+				if (!zc->wb_enabled) {
+					if (sg->length == PAGE_SIZE)
+						sg->length = -ENOSPC;
+					else if (WARN_ON_ONCE(!sg->length || sg->length > PAGE_SIZE))
+						sg->length = -EINVAL;
+					goto compress_error;
+				}
+
+				sg->length = PAGE_SIZE;
+			}
 		}
-		rcu_read_unlock();
-		comp_ret = 0;
-		dlen = PAGE_SIZE;
-		dst = kmap_local_folio(folio, index * PAGE_SIZE);
-		mapped = true;
 	}
 
-	gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
-	handle = zs_malloc(pool->zs_pool, dlen, gfp, folio_nid(folio));
-	if (IS_ERR_VALUE(handle)) {
-		alloc_ret = PTR_ERR((void *)handle);
-		goto unlock;
-	}
+	return zswap_update_entries(zc);
 
-	zs_obj_write(pool->zs_pool, handle, dst, dlen);
-	entry->handle = handle;
-	entry->length = dlen;
+compress_error:
+	for_each_sg(dst_sg, sg, zc->nr_batch_pages, comp_batch_iter) {
+		if ((int)sg->length < 0) {
+			if ((int)sg->length == -ENOSPC)
+				zswap_reject_compress_poor++;
+			else
+				zswap_reject_compress_fail++;
+		}
+	}
 
-unlock:
-	if (mapped)
-		kunmap_local(dst);
-	if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)
-		zswap_reject_compress_poor++;
-	else if (comp_ret)
-		zswap_reject_compress_fail++;
-	else if (alloc_ret)
-		zswap_reject_alloc_fail++;
-
-	mutex_unlock(&comp_ctx->areq.mutex);
-	return comp_ret == 0 && alloc_ret == 0;
+	return false;
 }
 
 static bool __zswap_decompress(struct zswap_entry *entry,
@@ -1527,89 +1734,194 @@ static void shrink_worker(struct work_struct *w)
 * main API
 **********************************/
 
-static bool zswap_store_page(struct folio *folio, long index,
-			     struct obj_cgroup *objcg,
-			     struct zswap_pool *pool)
+/* Functions called by zswap_store(), that operate on batches of pages. */
+
+/*
+ * zswap_init_entries() co-locates most of the zswap entry fields' setup under
+ * the assumption that all phases of store processing will succeed. An important
+ * marker to simplify catch-all cleanup in zswap_store_pages() is done by
+ * initializing each entry's @handle to ERR_PTR(-EINVAL).
+ */
+static inline void zswap_init_entries(struct zswap_store_context *zc,
+				      struct zswap_pool *pool,
+				      struct obj_cgroup *objcg)
 {
-	swp_entry_t page_swpentry = folio_swap_entry(folio, index);
-	struct zswap_entry *entry, *old;
+	struct zswap_entry *entry;
+	u8 i;
 
-	/* allocate entry */
-	entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));
-	if (!entry) {
-		zswap_reject_kmemcache_fail++;
-		return false;
+	/*
+	 * We co-locate entry initialization as much as possible here to
+	 * minimize potential cache misses.
+	 */
+	for (i = 0; i < zc->nr_pages; ++i) {
+		entry = zc->entries[i];
+		entry->handle = (unsigned long)ERR_PTR(-EINVAL);
+		/*
+		 * Set pool_idx before the xa_store() in zswap_tree_stores()
+		 * publishes the entry, or a concurrent reader could resolve a
+		 * stale pool_idx left by slab reuse to an unrelated live pool.
+		 */
+		entry->pool_idx = pool->idx;
+		entry->swpentry = folio_swap_entry(zc->folio,
+							zc->folio_start + i);
+		entry->objcg = objcg;
+		entry->referenced = true;
+		entry->nid = zc->nid;
+		INIT_LIST_HEAD(&entry->lru);
 	}
+}
 
-	if (!zswap_compress(folio, index, entry, pool))
-		goto compress_failed;
+/*
+ * zswap_tree_stores() stores the batch entries in xarray and adds them to the
+ * zswap LRU list. If an error occurs during xa_store(), localized state
+ * unwinding is done to erase prior batch entries from the xarray and delete
+ * them from the zswap LRU list. Remaining batch cleanup occurs in
+ * zswap_store_pages(). As batch entries are processed, we maintain aggregate
+ * statistics to do the accounting in one bulk operation.
+ */
+static bool zswap_tree_stores(struct zswap_store_context *zc,
+			      struct zswap_pool *pool,
+			      struct obj_cgroup *objcg)
+{
+	u8 i, store_fail_idx = 0, nr_incomp = 0;
+	size_t total_batch_comp_length = 0;
+	struct zswap_entry *old, *entry;
+
+	for (i = 0; i < zc->nr_pages; ++i) {
+		entry = zc->entries[i];
+
+		old = xa_store(swap_zswap_tree(entry->swpentry),
+			       swp_offset(entry->swpentry),
+			       entry, GFP_KERNEL);
+		if (unlikely(xa_is_err(old))) {
+			int err = xa_err(old);
+
+			WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
+			zswap_reject_alloc_fail++;
+			/*
+			 * Entries up to this point have been stored in the
+			 * xarray. Local cleanup in 'tree_store_failed' needs to
+			 * erase entries up until now.
+			 */
+			store_fail_idx = i;
+			goto tree_store_failed;
+		}
 
-	/*
-	 * Set pool_idx before the xa_store() below publishes the entry, or a
-	 * concurrent reader could resolve a stale pool_idx left by slab reuse
-	 * to an unrelated live pool.
-	 */
-	entry->pool_idx = pool->idx;
+		/*
+		 * We may have had an existing entry that became stale when
+		 * the folio was redirtied and now the new version is being
+		 * swapped out. Get rid of the old.
+		 */
+		if (unlikely(old))
+			zswap_entry_free(old);
 
-	old = xa_store(swap_zswap_tree(page_swpentry),
-		       swp_offset(page_swpentry),
-		       entry, GFP_KERNEL);
-	if (xa_is_err(old)) {
-		int err = xa_err(old);
+		total_batch_comp_length += entry->length;
 
-		WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
-		zswap_reject_alloc_fail++;
-		goto store_failed;
-	}
+		if (unlikely(entry->length == PAGE_SIZE))
+			++nr_incomp;
 
-	/*
-	 * We may have had an existing entry that became stale when
-	 * the folio was redirtied and now the new version is being
-	 * swapped out. Get rid of the old.
-	 */
-	if (old)
-		zswap_entry_free(old);
+		/*
+		 * We finish by adding the entry to the LRU while it's already
+		 * in xarray. This is safe because:
+		 *
+		 * 1. Concurrent stores and invalidations are excluded by folio lock.
+		 *
+		 * 2. Writeback is excluded by the entry not being on the LRU yet.
+		 *    The publishing order matters to prevent writeback from seeing
+		 *    an incoherent entry.
+		 */
+		if (likely(entry->length))
+			zswap_lru_add(entry);
+	}
 
 	/*
-	 * The entry is successfully compressed and stored in the tree, there is
-	 * no further possibility of failure. Grab refs to the pool and objcg,
-	 * charge zswap memory, and increment zswap_stored_pages.
-	 * The opposite actions will be performed by zswap_entry_free()
-	 * when the entry is removed from the tree.
+	 * Each batch entry is successfully compressed and stored in the tree,
+	 * and further failures will be cleaned up in zswap_store().
+	 * Next, we do bulk accounting for the batch: grab refs to the pool and
+	 * objcg, charge zswap memory, increment zswap_stored_pages and
+	 * zswap_stored_incompressible_pages. The opposite actions will be
+	 * performed by zswap_entry_free() when the entry is removed from the
+	 * tree.
 	 */
-	zswap_pool_get(pool);
+	zswap_pool_get_many(pool, zc->nr_pages);
 	if (objcg) {
-		obj_cgroup_get(objcg);
-		obj_cgroup_charge_zswap(objcg, entry->length);
+		obj_cgroup_get_many(objcg, zc->nr_pages);
+		obj_cgroup_charge_zswap_many(objcg, total_batch_comp_length,
+					     zc->nr_pages, nr_incomp);
 	}
-	atomic_long_inc(&zswap_stored_pages);
-	if (entry->length == PAGE_SIZE)
-		atomic_long_inc(&zswap_stored_incompressible_pages);
+	atomic_long_add(zc->nr_pages, &zswap_stored_pages);
+	atomic_long_add(nr_incomp, &zswap_stored_incompressible_pages);
+
+	return true;
+
+tree_store_failed:
+	for (i = 0; i < store_fail_idx; ++i) {
+		xa_erase(swap_zswap_tree(zc->entries[i]->swpentry),
+			 swp_offset(zc->entries[i]->swpentry));
+		zswap_lru_del(zc->entries[i]);
+	}
+
+	return false;
+}
+
+/*
+ * Store multiple pages in @zc->folio, starting from the page at index @start up
+ * to the page at index @end-1, i.e. @zc->nr_pages. Each phase of
+ * zswap_store_pages() does batch processing of @zc->nr_pages in the folio.
+ * State unwinding is kept to a minimum as a result of the batch processing: a
+ * batch proceeds to the next phase of store processing only if it passes this
+ * stage. The batch cleanup is consolidated under the @store_pages_failed
+ * label. Errors in storing a batch result in folio level cleanup occurring in
+ * zswap_store().
+ */
+static bool zswap_store_pages(struct zswap_store_context *zc,
+			      long start,
+			      long end,
+			      struct zswap_pool *pool,
+			      struct obj_cgroup *objcg)
+{
+	bool ret;
+	u8 i;
+
+	zc->nr_pages = end - start;
+	zc->folio_start = start;
+
+	VM_WARN_ON_ONCE(zc->nr_pages <= 0 || zc->nr_pages > ZSWAP_MAX_BATCH_SIZE);
+
+	ret = zswap_entries_cache_alloc_batch(zc, GFP_KERNEL);
+	if (unlikely(!ret))
+		return false;
+
+	zswap_init_entries(zc, pool, objcg);
 
 	/*
-	 * We finish initializing the entry while it's already in xarray.
-	 * This is safe because:
-	 *
-	 * 1. Concurrent stores and invalidations are excluded by folio lock.
-	 *
-	 * 2. Writeback is excluded by the entry not being on the LRU yet.
-	 *    The publishing order matters to prevent writeback from seeing
-	 *    an incoherent entry.
+	 * Locking the comp_ctx mutex once per store batch results in better
+	 * performance as compared to locking per compress batch.
 	 */
-	entry->swpentry = page_swpentry;
-	entry->objcg = objcg;
-	entry->referenced = true;
-	if (entry->length) {
-		INIT_LIST_HEAD(&entry->lru);
-		zswap_lru_add(entry);
-	}
+	zc->comp_ctx = &raw_cpu_ptr(pool->acomp_ctx)->comp;
+	mutex_lock(&zc->comp_ctx->areq.mutex);
+
+	ret = zswap_compress(zc);
+
+	mutex_unlock(&zc->comp_ctx->areq.mutex);
+
+	if (unlikely(!ret))
+		goto store_pages_failed;
+
+	ret = zswap_tree_stores(zc, pool, objcg);
+
+	if (unlikely(!ret))
+		goto store_pages_failed;
 
 	return true;
 
-store_failed:
-	zs_free(pool->zs_pool, entry->handle);
-compress_failed:
-	zswap_entry_cache_free(entry);
+store_pages_failed:
+	for (i = 0; i < zc->nr_pages; ++i) {
+		if (!IS_ERR_VALUE(zc->entries[i]->handle))
+			zs_free(pool->zs_pool, zc->entries[i]->handle);
+	}
+
+	zswap_entries_cache_free_batch(zc->entries, zc->nr_pages);
 	return false;
 }
 
@@ -1619,9 +1931,14 @@ bool zswap_store(struct folio *folio)
 	swp_entry_t swp = folio->swap;
 	struct obj_cgroup *objcg = NULL;
 	struct mem_cgroup *memcg = NULL;
-	struct zswap_pool *pool;
+	int nid = folio_nid(folio);
 	bool ret = false;
-	long index;
+	struct zswap_pool *pool;
+	long start, end;
+	struct zswap_store_context zc = {
+		.folio = folio,
+		.nid = nid,
+	};
 
 	VM_WARN_ON_ONCE(!folio_test_locked(folio));
 	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
@@ -1655,8 +1972,24 @@ bool zswap_store(struct folio *folio)
 		mem_cgroup_put(memcg);
 	}
 
-	for (index = 0; index < nr_pages; ++index) {
-		if (!zswap_store_page(folio, index, objcg, pool))
+	zc.zs_pool = pool->zs_pool;
+	rcu_read_lock();
+	zc.wb_enabled = mem_cgroup_zswap_writeback_enabled(folio_memcg(folio));
+	rcu_read_unlock();
+
+	/*
+	 * For batching and non-batching compressors, store the folio in
+	 * batches of ZSWAP_MAX_BATCH_SIZE, where the pages in the batch
+	 * are compressed in batches of the compressor's batch_size. This gives
+	 * better performance than invoking zswap_store_pages() per-page,
+	 * due to cache locality of working set structures.
+	 */
+	for (start = 0; start < nr_pages; start += ZSWAP_MAX_BATCH_SIZE) {
+		end = min(start + ZSWAP_MAX_BATCH_SIZE, nr_pages);
+
+		ret = zswap_store_pages(&zc, start, end, pool, objcg);
+
+		if (unlikely(!ret))
 			goto put_pool;
 	}
 
@@ -1926,6 +2259,8 @@ static int zswap_setup(void)
 	struct zswap_pool *pool;
 	int ret;
 
+	BUILD_BUG_ON(ZSWAP_MAX_COMPR_LENGTH_SHIFT <= PAGE_SHIFT);
+
 	zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
 	if (!zswap_entry_cache) {
 		pr_err("entry cache creation failed\n");

3/2 2026/11/09 05:46 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 19157
							output: 27
							thoughts: 680
						

Thoughts:
**Analyzing the Patch for Syzkaller Fuzzing Potential**

Okay, let's break this down. My initial assessment is that this patch *could* be a great target for fuzzing with syzkaller. Here's my thought process:

First, let's dissect the changes. It looks like we're dealing with significant modifications across a few key areas: `crypto/acompress.c` and associated header files, and the `mm/zswap.c` subsystem. The crypto changes introduce batch processing capabilities and adjustments to request management, particularly around compression and decompression. The `zswap` changes are much more extensive, clearly implementing batch compression and storage within zswap itself. There are also smaller changes to memory control, but they tie into the zswap changes.

Crucially, the zswap stuff involves new data structures like `zswap_comp_ctx`, `zswap_entry`, and changes to critical functions: `zswap_compress`, `zswap_store`, `zswap_update_entries`, etc. This level of modification, especially with new data structures and batch processing, gets my attention. This screams "potential for interesting edge cases and subtle bugs."

Next, I need to check for reachability. This code *is* reachable, and that's a big tick. Both `mm/zswap.c` and `crypto/acompress.c` are core kernel subsystems. Zswap, in particular, is commonly enabled, so we can probably hit this code in our standard virtualized environments (QEMU, amd64). I'll need to double-check the Kconfig options, but I'm expecting `CONFIG_ZSWAP`, `CONFIG_CRYPTO_ACOMP2`, `CONFIG_MEMCG`, and `CONFIG_ZSMALLOC` to be relevant. Grepper should confirm those.

The memory management aspect is a huge win. Kernel memory management is almost *always* an interesting target for syzkaller, and the fact that we have crypto mixed in, and batch processing... This could get complex quickly! I'm thinking, "memory corruption potential" with these changes, especially with new batching logic. I like this patch. I'll flag it for further investigation.



4/2 2026/11/09 05:47 tool grepper
0m Args:
{
  "Expression": "config ZSWAP",
  "PathPrefixes": [
    "mm/"
  ]
}

Results:
{
  "Output": "mm/Kconfig=12=menuconfig SWAP\n--\nmm/Kconfig-21-\nmm/Kconfig:22:config ZSWAP\nmm/Kconfig-23-\tbool \"Compressed cache for swap pages\"\n--\nmm/Kconfig-34-\nmm/Kconfig:35:config ZSWAP_DEFAULT_ON\nmm/Kconfig-36-\tbool \"Enable the compressed cache for swap pages by default\"\n--\nmm/Kconfig-44-\nmm/Kconfig:45:config ZSWAP_SHRINKER_DEFAULT_ON\nmm/Kconfig-46-\tbool \"Shrink the zswap pool on memory pressure\"\n--\nmm/Kconfig=59=choice\n--\nmm/Kconfig-76-\nmm/Kconfig:77:config ZSWAP_COMPRESSOR_DEFAULT_DEFLATE\nmm/Kconfig-78-\tbool \"Deflate\"\n--\nmm/Kconfig-82-\nmm/Kconfig:83:config ZSWAP_COMPRESSOR_DEFAULT_LZO\nmm/Kconfig-84-\tbool \"LZO\"\n--\nmm/Kconfig-88-\nmm/Kconfig:89:config ZSWAP_COMPRESSOR_DEFAULT_842\nmm/Kconfig-90-\tbool \"842\"\n--\nmm/Kconfig-94-\nmm/Kconfig:95:config ZSWAP_COMPRESSOR_DEFAULT_LZ4\nmm/Kconfig-96-\tbool \"LZ4\"\n--\nmm/Kconfig-100-\nmm/Kconfig:101:config ZSWAP_COMPRESSOR_DEFAULT_LZ4HC\nmm/Kconfig-102-\tbool \"LZ4HC\"\n--\nmm/Kconfig-106-\nmm/Kconfig:107:config ZSWAP_COMPRESSOR_DEFAULT_ZSTD\nmm/Kconfig-108-\tbool \"zstd\"\n--\nmm/Kconfig=112=endchoice\nmm/Kconfig-113-\nmm/Kconfig:114:config ZSWAP_COMPRESSOR_DEFAULT\nmm/Kconfig-115-       string\n"
}

5/2 2026/11/09 05:47 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20872
							output: 31
							thoughts: 21
						

Thoughts:
Here's my thought process, as it is:

**Examining Kconfig for Module Management**

Okay, time to dive in. I need to take a look at the `mm/Kconfig` file. Specifically, I'm going to zero in on the area around line 22 using a `read-file` command. I'm guessing that this region likely concerns module management within the kernel's memory management subsystem. Given my expertise, I'm expecting to find options related to features, debugging, or potentially configurations that govern how memory modules behave. It could be settings for dynamic loading or unloading of modules, module-specific memory allocation parameters, or even options related to how the kernel interacts with modular memory managers. Let's see what's actually in that area of the Kconfig file!



6/2 2026/11/09 05:47 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 20,
  "LineCount": 40
}

Results:
{
  "Contents": "  20:\t\t  in your computer.  If unsure say Y.\n  21:\t\n  22:\tconfig ZSWAP\n  23:\t\tbool \"Compressed cache for swap pages\"\n  24:\t\tdepends on SWAP\n  25:\t\tselect CRYPTO\n  26:\t\tselect ZSMALLOC\n  27:\t\thelp\n  28:\t\t  A lightweight compressed cache for swap pages.  It takes\n  29:\t\t  pages that are in the process of being swapped out and attempts to\n  30:\t\t  compress them into a dynamically allocated RAM-based memory pool.\n  31:\t\t  This can result in a significant I/O reduction on swap device and,\n  32:\t\t  in the case where decompressing from RAM is faster than swap device\n  33:\t\t  reads, can also improve workload performance.\n  34:\t\n  35:\tconfig ZSWAP_DEFAULT_ON\n  36:\t\tbool \"Enable the compressed cache for swap pages by default\"\n  37:\t\tdepends on ZSWAP\n  38:\t\thelp\n  39:\t\t  If selected, the compressed cache for swap pages will be enabled\n  40:\t\t  at boot, otherwise it will be disabled.\n  41:\t\n  42:\t\t  The selection made here can be overridden by using the kernel\n  43:\t\t  command line 'zswap.enabled=' option.\n  44:\t\n  45:\tconfig ZSWAP_SHRINKER_DEFAULT_ON\n  46:\t\tbool \"Shrink the zswap pool on memory pressure\"\n  47:\t\tdepends on ZSWAP\n  48:\t\tdefault n\n  49:\t\thelp\n  50:\t\t  If selected, the zswap shrinker will be enabled, and the pages\n  51:\t\t  stored in the zswap pool will become available for reclaim (i.e\n  52:\t\t  written back to the backing swap device) on memory pressure.\n  53:\t\n  54:\t\t  This means that zswap writeback could happen even if the pool is\n  55:\t\t  not yet full, or the cgroup zswap limit has not been reached,\n  56:\t\t  reducing the chance that cold pages will reside in the zswap pool\n  57:\t\t  and consume memory indefinitely.\n  58:\t\n  59:\tchoice\n"
}

7/2 2026/11/09 05:47 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 21663
							output: 27
							thoughts: 24
						

Thoughts:
Okay, here's my summary, tailored for an expert audience, as if I'm thinking these things myself:

**Investigating the Build Process for `crypto/acompress.c`**

Alright, so I need to figure out how `crypto/acompress.c` gets built. My first instinct, as always, is to dive straight into the build system. This isn't a complex module, so likely I'm looking at a relatively straightforward configuration. I'll start with the obvious places: `crypto/Kconfig` and the `Makefile` in that directory. The `Kconfig` file will tell me any configuration options that enable or disable this particular code, which is important to identify. The `Makefile`, of course, contains the build rules, so I'm expecting to find either an explicit compilation entry for `acompress.c` there, or, more likely, some inclusion in a larger target along with dependencies.



8/2 2026/11/09 05:47 tool grepper
0m Args:
{
  "Expression": "acompress\\.o",
  "PathPrefixes": [
    "crypto/"
  ]
}

Results:
{
  "Output": "crypto/Makefile=61=obj-$(CONFIG_CRYPTO_MLDSA) += mldsa.o\ncrypto/Makefile-62-\ncrypto/Makefile:63:crypto_acompress-y := acompress.o\ncrypto/Makefile-64-crypto_acompress-y += scompress.o\ncrypto/Makefile:65:obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o\ncrypto/Makefile-66-\n"
}

9/2 2026/11/09 05:47 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22029
							output: 34
							thoughts: 16
						

Thoughts:
Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert:

**Investigating the Configuration Dependency on `CONFIG_CRYPTO_ACOMP2`**

Alright, let's dive in. My immediate thought is, "What depends on `CONFIG_CRYPTO_ACOMP2`?" I need to understand the wider system's configuration dependencies. I'm already formulating a mental checklist: I need to quickly determine where this option is being pulled in, and why. Is it a direct dependency of a specific algorithm or driver? Or is it a more indirect dependency triggered by a broader feature selection? Understanding the "why" is crucial for any potential modifications or optimizations down the line. I’m thinking I'll start by tracing the kconfig references, but I'll be prepared for more in-depth kernel code analysis if the dependencies aren't immediately obvious from the config itself. This is my starting point for understanding how this specific cryptographic component fits into the big picture.



10/2 2026/11/09 05:47 tool grepper
0m Args:
{
  "Expression": "CRYPTO_ACOMP2",
  "PathPrefixes": [
    "crypto/",
    "mm/"
  ]
}

Results:
{
  "Output": "crypto/Kconfig=123=config CRYPTO_KPP\n--\ncrypto/Kconfig-127-\ncrypto/Kconfig:128:config CRYPTO_ACOMP2\ncrypto/Kconfig-129-\ttristate\n--\ncrypto/Kconfig=133=config CRYPTO_ACOMP\n--\ncrypto/Kconfig-135-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:136:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-137-\n--\ncrypto/Kconfig=146=config CRYPTO_MANAGER2\ncrypto/Kconfig-147-\tdef_tristate CRYPTO_MANAGER || (CRYPTO_MANAGER!=n \u0026\u0026 CRYPTO_ALGAPI=y)\ncrypto/Kconfig:148:\tselect CRYPTO_ACOMP2 if CRYPTO_SELFTESTS\ncrypto/Kconfig-149-\tselect CRYPTO_AEAD2 if CRYPTO_SELFTESTS\n--\ncrypto/Kconfig=1039=config CRYPTO_DEFLATE\n--\ncrypto/Kconfig-1041-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1042:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1043-\tselect ZLIB_INFLATE\n--\ncrypto/Kconfig=1050=config CRYPTO_LZO\n--\ncrypto/Kconfig-1052-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1053:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1054-\tselect LZO_COMPRESS\n--\ncrypto/Kconfig=1061=config CRYPTO_842\n--\ncrypto/Kconfig-1063-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1064:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1065-\tselect 842_COMPRESS\n--\ncrypto/Kconfig=1072=config CRYPTO_LZ4\n--\ncrypto/Kconfig-1074-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1075:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1076-\tselect LZ4_COMPRESS\n--\ncrypto/Kconfig=1083=config CRYPTO_LZ4HC\n--\ncrypto/Kconfig-1085-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1086:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1087-\tselect LZ4HC_COMPRESS\n--\ncrypto/Kconfig=1094=config CRYPTO_ZSTD\n--\ncrypto/Kconfig-1096-\tselect CRYPTO_ALGAPI\ncrypto/Kconfig:1097:\tselect CRYPTO_ACOMP2\ncrypto/Kconfig-1098-\tselect ZSTD_COMPRESS\n--\ncrypto/Makefile=64=crypto_acompress-y += scompress.o\ncrypto/Makefile:65:obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o\ncrypto/Makefile-66-\n"
}

11/2 2026/11/09 05:47 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23053
							output: 27
							thoughts: 1273
						

Thoughts:
**Determining the Fuzzing Strategy for This Patch**

Okay, let's break this down systematically. My initial assessment is that this patch is definitely a candidate for fuzzing. I can see that it involves changes across memory management (specifically zswap), memory control with memcg, and the crypto subsystem with compression algorithms. This is all core functionality.

First, the use of compression algorithms like LZ4, LZO, ZSTD, or DEFLATE, all of which use `CRYPTO_ACOMP2`, immediately flags crypto as relevant. In zswap's allocation of compression contexts (`crypto_alloc_acomp_node`), this dependency on `CRYPTO_ACOMP2` is very clear. Moreover, the presence of `CONFIG_MEMCG` and its influence on `zswap` via `obj_cgroup_charge_zswap_many` shows that memory control with cgroups is integral to this patch, if zswap is active.

Moving on to the triage criteria, this patch definitely ticks the boxes. It touches code in `mm/zswap.c`, `mm/memcontrol.c`, and `crypto/acompress.c`, all of which are core subsystems, involving logic modifications in memory management, BPF, core networking, and even syscall handling (indirectly, via memory pressure). Plus, there are refactorings involving data structures, algorithms, and defensive checks (the `VM_WARN_ON_ONCE` assertions are significant). It's also making major changes by adding batch compression, and re-writing the storage and error-handling in zswap.

The next question: is this reachable? Absolutely! Zswap is designed to be used when memory is under pressure. Swapping pages to disk or using zswap, which is the faster option, can easily be triggered by any standard program. Syzkaller can easily generate memory pressure with `mmap`, `madvise`, allocations, or even using sysfs or cgroup memory limits, and target zswap directly. Furthermore, the crypto aspect is reachable through AF_ALG sockets or via crypto test modules or zswap itself. Therefore, "WorthFuzzing" is undeniably `true`.

Now, the crucial part: identifying the `FocusSymbols` to prevent dilution of the fuzzing effort. I need to avoid listing overly general functions (allocators, synchronization primitives). Instead, I must target the specific logic being altered or the feature entrypoints.

Here's my breakdown of the functions, separating static inlines from exported ones and functions called by the swap code:

In `crypto/acompress.c`:
-   Focus on: `crypto_acomp_compress`, `crypto_acomp_decompress`, `crypto_acomp_batch_size`. Static helper functions such as `acomp_save_req`, `acomp_restore_req`, `acomp_reqchain_finish`, and `acomp_do_req_chain` will be indirectly fuzzed by the exported functions.

In `mm/memcontrol.c`:
-   Focus on: `obj_cgroup_charge_zswap_many`.

In `mm/zswap.c`:
-   Focus on: `zswap_store`, and `zswap_setup`. The static helper functions, like `acomp_ctx_free`, `zswap_cpu_comp_prepare`, `zswap_tree_stores` and `zswap_store_pages` are critical to this logic. Furthermore, `zswap_compress` can be seen as an important helper. Static inline functions will be called from other functions, so there is no need to add them.



12/2 2026/11/09 05:47 tool grepper
0m Args:
{
  "Expression": "zswap_store",
  "PathPrefixes": [
    "mm/"
  ]
}

Results:
{
  "Output": "mm/page_io.c=204=int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)\n--\nmm/page_io.c-247-\nmm/page_io.c:248:\tif (zswap_store(folio)) {\nmm/page_io.c-249-\t\tcount_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);\n--\nmm/zswap.c-48-/* The number of pages currently stored in zswap */\nmm/zswap.c:49:atomic_long_t zswap_stored_pages = ATOMIC_LONG_INIT(0);\nmm/zswap.c-50-/* The number of incompressible pages currently stored in zswap */\nmm/zswap.c:51:static atomic_long_t zswap_stored_incompressible_pages = ATOMIC_LONG_INIT(0);\nmm/zswap.c-52-\n--\nmm/zswap.c=227=struct zswap_entry {\n--\nmm/zswap.c-239-\nmm/zswap.c:240:struct zswap_store_context {\nmm/zswap.c-241-\tstruct folio *folio;\n--\nmm/zswap.c-254- * No RCU section is needed around the returned pointer: a stored entry pins\nmm/zswap.c:255: * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be\nmm/zswap.c-256- * reused under us.  Callers WARN and handle a NULL from a corrupt pool_idx.\n--\nmm/zswap.c=829=static bool zswap_entries_cache_alloc_batch(\nmm/zswap.c:830:\tstruct zswap_store_context *zc,\nmm/zswap.c-831-\tgfp_t gfp)\n--\nmm/zswap.c=857=static void zswap_entry_free(struct zswap_entry *entry)\n--\nmm/zswap.c-870-\tif (entry-\u003elength == PAGE_SIZE)\nmm/zswap.c:871:\t\tatomic_long_dec(\u0026zswap_stored_incompressible_pages);\nmm/zswap.c-872-\tzswap_entry_cache_free(entry);\nmm/zswap.c:873:\tatomic_long_dec(\u0026zswap_stored_pages);\nmm/zswap.c-874-}\n--\nmm/zswap.c=912=static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)\n--\nmm/zswap.c-1011- * allocation failure, upon which an error status is returned to\nmm/zswap.c:1012: * zswap_store_pages().\nmm/zswap.c-1013- */\nmm/zswap.c:1014:static inline bool zswap_update_entries(struct zswap_store_context *zc)\nmm/zswap.c-1015-{\n--\nmm/zswap.c-1084- * update zswap statistics, with catch-all cleanup for the batch occuring in the\nmm/zswap.c:1085: * @store_pages_failed label in zswap_store_pages().\nmm/zswap.c-1086- */\nmm/zswap.c:1087:static bool zswap_compress(struct zswap_store_context *zc)\nmm/zswap.c-1088-{\n--\nmm/zswap.c=1260=struct zswap_shrink_ctl {\n--\nmm/zswap.c-1280- * to the swap device.  We are basically resuming the same swap\nmm/zswap.c:1281: * writeback path that was intercepted with the zswap_store()\nmm/zswap.c-1282- * in the first place.  After the folio has been decompressed into\n--\nmm/zswap.c=1515=static unsigned long zswap_shrinker_count(struct shrinker *shrinker,\n--\nmm/zswap.c-1549-\t\tnr_backing = zswap_total_pages();\nmm/zswap.c:1550:\t\tnr_stored = atomic_long_read(\u0026zswap_stored_pages);\nmm/zswap.c-1551-\t}\n--\nmm/zswap.c=1646=static void shrink_worker(struct work_struct *w)\n--\nmm/zswap.c-1736-\nmm/zswap.c:1737:/* Functions called by zswap_store(), that operate on batches of pages. */\nmm/zswap.c-1738-\n--\nmm/zswap.c-1741- * the assumption that all phases of store processing will succeed. An important\nmm/zswap.c:1742: * marker to simplify catch-all cleanup in zswap_store_pages() is done by\nmm/zswap.c-1743- * initializing each entry's @handle to ERR_PTR(-EINVAL).\nmm/zswap.c-1744- */\nmm/zswap.c:1745:static inline void zswap_init_entries(struct zswap_store_context *zc,\nmm/zswap.c-1746-\t\t\t\t      struct zswap_pool *pool,\n--\nmm/zswap.c-1778- * them from the zswap LRU list. Remaining batch cleanup occurs in\nmm/zswap.c:1779: * zswap_store_pages(). As batch entries are processed, we maintain aggregate\nmm/zswap.c-1780- * statistics to do the accounting in one bulk operation.\nmm/zswap.c-1781- */\nmm/zswap.c:1782:static bool zswap_tree_stores(struct zswap_store_context *zc,\nmm/zswap.c-1783-\t\t\t      struct zswap_pool *pool,\n--\nmm/zswap.c-1838-\t * Each batch entry is successfully compressed and stored in the tree,\nmm/zswap.c:1839:\t * and further failures will be cleaned up in zswap_store().\nmm/zswap.c-1840-\t * Next, we do bulk accounting for the batch: grab refs to the pool and\nmm/zswap.c:1841:\t * objcg, charge zswap memory, increment zswap_stored_pages and\nmm/zswap.c:1842:\t * zswap_stored_incompressible_pages. The opposite actions will be\nmm/zswap.c-1843-\t * performed by zswap_entry_free() when the entry is removed from the\n--\nmm/zswap.c-1851-\t}\nmm/zswap.c:1852:\tatomic_long_add(zc-\u003enr_pages, \u0026zswap_stored_pages);\nmm/zswap.c:1853:\tatomic_long_add(nr_incomp, \u0026zswap_stored_incompressible_pages);\nmm/zswap.c-1854-\n--\nmm/zswap.c-1869- * to the page at index @end-1, i.e. @zc-\u003enr_pages. Each phase of\nmm/zswap.c:1870: * zswap_store_pages() does batch processing of @zc-\u003enr_pages in the folio.\nmm/zswap.c-1871- * State unwinding is kept to a minimum as a result of the batch processing: a\n--\nmm/zswap.c-1874- * label. Errors in storing a batch result in folio level cleanup occurring in\nmm/zswap.c:1875: * zswap_store().\nmm/zswap.c-1876- */\nmm/zswap.c:1877:static bool zswap_store_pages(struct zswap_store_context *zc,\nmm/zswap.c-1878-\t\t\t      long start,\n--\nmm/zswap.c-1927-\nmm/zswap.c:1928:bool zswap_store(struct folio *folio)\nmm/zswap.c-1929-{\n--\nmm/zswap.c-1937-\tlong start, end;\nmm/zswap.c:1938:\tstruct zswap_store_context zc = {\nmm/zswap.c-1939-\t\t.folio = folio,\n--\nmm/zswap.c-1983-\t * are compressed in batches of the compressor's batch_size. This gives\nmm/zswap.c:1984:\t * better performance than invoking zswap_store_pages() per-page,\nmm/zswap.c-1985-\t * due to cache locality of working set structures.\n--\nmm/zswap.c-1989-\nmm/zswap.c:1990:\t\tret = zswap_store_pages(\u0026zc, start, end, pool, objcg);\nmm/zswap.c-1991-\n--\nmm/zswap.c=2199=static int debugfs_get_stored_pages(void *data, u64 *val)\nmm/zswap.c-2200-{\nmm/zswap.c:2201:\t*val = atomic_long_read(\u0026zswap_stored_pages);\nmm/zswap.c-2202-\treturn 0;\n--\nmm/zswap.c=2206=static int debugfs_get_stored_incompressible_pages(void *data, u64 *val)\nmm/zswap.c-2207-{\nmm/zswap.c:2208:\t*val = atomic_long_read(\u0026zswap_stored_incompressible_pages);\nmm/zswap.c-2209-\treturn 0;\n"
}

13/2 2026/11/09 05:47 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27597
							output: 31
							thoughts: 1389
						

Thoughts:
**Analyzing Zswap and Crypto Compression for Coverage**

Okay, let's break this down systematically. I'm focusing on these functions to determine potential areas for fuzzing and coverage: `zswap_store`, `zswap_store_pages`, `zswap_compress`, `zswap_tree_stores`, `crypto_acomp_compress`, `crypto_acomp_batch_size`, and `obj_cgroup_charge_zswap_many`. First, `zswap_store`: it's non-static and not a core system call. Good, that narrows it down. It's called specifically within `swap_writeout` when swapping out folios, so it's zswap specific and not in the hot path. `zswap_store_pages`, `zswap_compress`, and `zswap_tree_stores` are static and reside in `mm/zswap.c`. The compiler *might* inline these, but given their substantial nature, they are likely to be separate symbols in the vmlinux.

I see `crypto_acomp_compress` and `crypto_acomp_decompress` in `crypto/acompress.c`, and `crypto_acomp_batch_size` which seems to be a new addition from the patch. Similarly, `obj_cgroup_charge_zswap_many` is a new function related to memcg and zswap. `zswap_store_pages` is the core batch logic in `mm/zswap.c`, and `zswap_compress` seems to be modified in the same file. `zswap_tree_stores` is also new.

Now, let's consider what's being modified within `crypto/acompress.c`. It's not just the functions I listed before. I also see `acomp_reqchain_finish` and `acomp_do_req_chain`. I'll keep that in mind. Reviewing `crypto_acomp_compress`, it appears to handle several cases, from simple compression to chained requests, and it has some async handling.

Next, I need to understand the configuration dependencies. `ZSWAP` depends on `SWAP`, naturally. `SWAP` needs to be enabled for paging. `ZSWAP_DEFAULT_ON` or setting `zswap.enabled=1` at runtime are also important. For `obj_cgroup_charge_zswap_many`, `MEMCG` must be enabled. I see that `ZSWAP` also selects `CRYPTO` and `ZSMALLOC`.

Finally, for `ZSWAP` the default compressor is set, and it's either `ZSWAP_COMPRESSOR_DEFAULT_ZSTD` or `ZSWAP_COMPRESSOR_DEFAULT_LZO`. I need to review `mm/Kconfig` to confirm the actual default. My focus should be to check the default crypto compressor.



14/2 2026/11/09 05:48 tool read-file
0m Args:
{
  "File": "mm/Kconfig",
  "FirstLine": 58,
  "LineCount": 30
}

Results:
{
  "Contents": "  58:\t\n  59:\tchoice\n  60:\t\tprompt \"Default compressor\"\n  61:\t\tdepends on ZSWAP\n  62:\t\tdefault ZSWAP_COMPRESSOR_DEFAULT_LZO\n  63:\t\thelp\n  64:\t\t  Selects the default compression algorithm for the compressed cache\n  65:\t\t  for swap pages.\n  66:\t\n  67:\t\t  For an overview what kind of performance can be expected from\n  68:\t\t  a particular compression algorithm please refer to the benchmarks\n  69:\t\t  available at the following LWN page:\n  70:\t\t  https://lwn.net/Articles/751795/\n  71:\t\n  72:\t\t  If in doubt, select 'LZO'.\n  73:\t\n  74:\t\t  The selection made here can be overridden by using the kernel\n  75:\t\t  command line 'zswap.compressor=' option.\n  76:\t\n  77:\tconfig ZSWAP_COMPRESSOR_DEFAULT_DEFLATE\n  78:\t\tbool \"Deflate\"\n  79:\t\tselect CRYPTO_DEFLATE\n  80:\t\thelp\n  81:\t\t  Use the Deflate algorithm as the default compression algorithm.\n  82:\t\n  83:\tconfig ZSWAP_COMPRESSOR_DEFAULT_LZO\n  84:\t\tbool \"LZO\"\n  85:\t\tselect CRYPTO_LZO\n  86:\t\thelp\n  87:\t\t  Use the LZO algorithm as the default compression algorithm.\n"
}

15/2 2026/11/09 05:48 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30032
							output: 239
							thoughts: 679
						

Thoughts:
**Analyzing the Scope and Testability of a Zswap Patch**

Okay, let's break this down. My initial observation is that selecting `ZSWAP_COMPRESSOR_DEFAULT_LZO` implicitly enables a cascade of dependencies: `CRYPTO_LZO`, which in turn pulls in `CRYPTO_ACOMP2`. So, to ensure everything's properly enabled for this patch, I need to make sure `ZSWAP`, `ZSWAP_DEFAULT_ON`, `MEMCG`, `SWAP`, and `CRYPTO_ACOMP2` are all compiled in. I can confirm this by explicitly verifying those five configurations.

To be extra careful, I'll strip the `"CONFIG_"` prefix and explicitly track these within `"EnableConfigs"`.

Now, the real question is, what specific symbols are most relevant here? I'm going to look at `"FocusSymbols"`: `"zswap_store"`, `"zswap_store_pages"`, `"zswap_compress"`, `"zswap_tree_stores"`, `"obj_cgroup_charge_zswap_many"`, `"crypto_acomp_compress"`, and `"crypto_acomp_batch_size"`.

I need to analyze these individually to determine if they're generic, ubiquitous functions, or specific to the new logic. Let's see: `zswap_store` is only called during page swap-out. `zswap_store_pages` is a helper for batch storing. `zswap_compress` handles page compression. `zswap_tree_stores` handles storage in the xarray and LRU. `obj_cgroup_charge_zswap_many` only gets called when charging zswap memory. And, of course, `crypto_acomp_compress` and `crypto_acomp_batch_size` are the crypto acomp specific functions. None of these appear to be general-purpose memory allocators, page fault handlers, locks, or lifecycle primitives. They all seem directly tied to this patch's new features.

Okay, let's do a sanity check on my reasoning. This patch is modifying core memory management and crypto code, adding batch compression and batch storing to zswap and adding batching support to crypto acomp. It adds new functions like `obj_cgroup_charge_zswap_many`, which has assertions. It also touches zswap storage logic, entry layout, and resource cleanup. Given that, it is directly reachable in standard QEMU environments, when swap and zswap are active under memory pressure. Based on all of this, I believe it's definitely worth fuzzing. I'll call `set-results`.



16/2 2026/11/09 05:48 tool set-results
0m Args:
{
  "EnableConfigs": [
    "ZSWAP",
    "SWAP",
    "MEMCG",
    "CRYPTO_ACOMP2",
    "ZSWAP_DEFAULT_ON"
  ],
  "FocusSymbols": [
    "zswap_store",
    "zswap_store_pages",
    "zswap_compress",
    "zswap_tree_stores",
    "obj_cgroup_charge_zswap_many",
    "crypto_acomp_compress",
    "crypto_acomp_batch_size"
  ],
  "Reasoning": "The patch introduces batch compression and storing functionality into zswap and crypto acompress, modifying core memory management and crypto subsystems. It restructures folio compression and xarray/LRU insertion in zswap, adjusts reference counting and error unwinding, adds bulk charging for memory cgroups (obj_cgroup_charge_zswap_many) with new VM_WARN_ON_ONCE assertions, and modifies request chaining in crypto acompress. This code is fully reachable in virtualized environments under memory pressure when swap/zswap is enabled.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "ZSWAP",
    "SWAP",
    "MEMCG",
    "CRYPTO_ACOMP2",
    "ZSWAP_DEFAULT_ON"
  ],
  "FocusSymbols": [
    "zswap_store",
    "zswap_store_pages",
    "zswap_compress",
    "zswap_tree_stores",
    "obj_cgroup_charge_zswap_many",
    "crypto_acomp_compress",
    "crypto_acomp_batch_size"
  ],
  "Reasoning": "The patch introduces batch compression and storing functionality into zswap and crypto acompress, modifying core memory management and crypto subsystems. It restructures folio compression and xarray/LRU insertion in zswap, adjusts reference counting and error unwinding, adds bulk charging for memory cgroups (obj_cgroup_charge_zswap_many) with new VM_WARN_ON_ONCE assertions, and modifies request chaining in crypto acompress. This code is fully reachable in virtualized environments under memory pressure when swap/zswap is enabled.",
  "WorthFuzzing": true
}

17/1 2026/11/09 05:48 action If
5m Args:
{
  "WorthFuzzing": true
}

Results:
null

18/2 2026/11/09 05:48 agent kmsan-evaluator
5m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series introduces batching support for crypto asynchronous compression (acomp) and leverages it in zswap to store pages in batches (bounded by ZSWAP_MAX_BATCH_SIZE).\n\n1. Data structures and initialization:\n- `struct acomp_req` gains a `unit_size` field, which is explicitly initialized to 0 in `acomp_request_set_callback()` and in `acomp_save_req()`, and set by the caller via `acomp_request_set_unit_size()`. When allocated dynamically, `acomp_request_alloc()` uses `kzalloc()`.\n- In `zswap`, `struct zswap_entry` is packed with bitfields. In `zswap_init_entries()`, all fields (`handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, `lru`) are initialized; `length` and `handle` are updated upon successful compression in `zswap_update_entries()`. On failure before completion, only `handle` (initialized to `ERR_PTR(-EINVAL)`) is checked to determine if zsmalloc freeing is necessary before freeing the entries back to the cache. No uninitialized struct fields are read.\n- The per-CPU compression destination buffers `buffers[i]` are written to by the compression backend and read up to `dlen` (the compressed output size) into zsmalloc via `zs_obj_write()`. Incompressible pages read directly from the source folio.\n\n2. Userspace exposure and control flow:\n- No kernel memory or padding is exposed or copied to userspace (no `copy_to_user`, ioctl, netlink, or socket options).\n- No conditional branching depends on uninitialized memory or uninitialized struct padding.\n\n3. KASAN vs KMSAN applicability:\n- Potential risks in this patch series involve buffer indexing (`ZSWAP_MAX_BATCH_SIZE`), scatterlist iteration bounds, memory allocation failure handling, and concurrency/locking around `comp_ctx-\u003eareq.mutex`. These are all covered by KASAN, LOCKDEP, and standard kernel debugging facilities.\n- There are no uninitialized memory usage risks or info-leaks that would require a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 8c3cd021912957225a6d79bc78e90ca864c09e4b
Author: syz-cluster <triage@syzkaller.com>
Date:   Mon Nov 9 05:46:48 2026 +0000

    syz-cluster: applied patch under review

diff --git a/crypto/acompress.c b/crypto/acompress.c
index c8b4b8589769e3..df9e42f519c358 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -166,14 +166,18 @@ 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;
+
 	req->base.complete = cplt;
 	req->base.data = req;
+	req->unit_size = 0;
 }
 
 static void acomp_restore_req(struct acomp_req *req)
 {
 	req->base.complete = req->chain.compl;
 	req->base.data = req->chain.data;
+	req->unit_size = req->chain.unit_size;
 }
 
 static void acomp_reqchain_virt(struct acomp_req *req)
@@ -242,6 +246,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 = unlikely(err) ? err : req->dlen;
+
 	return err;
 }
 
@@ -266,10 +274,13 @@ static int acomp_do_req_chain(struct acomp_req *req, bool comp)
 {
 	int err;
 
+	if (unlikely(req->unit_size && req->slen > req->unit_size))
+		return -ENOSYS;
+
 	acomp_save_req(req, acomp_reqchain_done);
 
 	err = acomp_do_one_req(req, comp);
-	if (err == -EBUSY || err == -EINPROGRESS)
+	if (unlikely(err == -EBUSY || err == -EINPROGRESS))
 		return err;
 
 	return acomp_reqchain_finish(req, err);
@@ -279,10 +290,27 @@ int crypto_acomp_compress(struct acomp_req *req)
 {
 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
 
+	if (likely((req->unit_size == req->slen) && acomp_request_issg(req) &&
+		    !crypto_acomp_req_seg(tfm) && !acomp_is_async(tfm))) {
+		int err = tfm->compress(req);
+
+		req->dst->length = unlikely(err) ? err : req->dlen;
+		return err;
+	}
+
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
+
+	if (req->unit_size && acomp_request_issg(req)) {
+		if (!crypto_acomp_req_seg(tfm))
+			return acomp_do_req_chain(req, true);
+
+		return tfm->compress(req);
+	}
+
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
-		return crypto_acomp_reqtfm(req)->compress(req);
+		return tfm->compress(req);
+
 	return acomp_do_req_chain(req, true);
 }
 EXPORT_SYMBOL_GPL(crypto_acomp_compress);
@@ -294,11 +322,25 @@ int crypto_acomp_decompress(struct acomp_req *req)
 	if (acomp_req_on_stack(req) && acomp_is_async(tfm))
 		return -EAGAIN;
 	if (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))
-		return crypto_acomp_reqtfm(req)->decompress(req);
+		return tfm->decompress(req);
 	return acomp_do_req_chain(req, false);
 }
 EXPORT_SYMBOL_GPL(crypto_acomp_decompress);
 
+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm)
+{
+	if (acomp_is_async(tfm) &&
+	    (crypto_comp_alg_common(tfm)->base.cra_type == &crypto_acomp_type)) {
+		struct acomp_alg *alg = crypto_acomp_alg(tfm);
+
+		if (alg && alg->batch_size > 1)
+			return alg->batch_size;
+	}
+
+	return 1;
+}
+EXPORT_SYMBOL_GPL(crypto_acomp_batch_size);
+
 void comp_prepare_alg(struct comp_alg_common *alg)
 {
 	struct crypto_alg *base = &alg->base;
diff --git a/include/crypto/acompress.h b/include/crypto/acompress.h
index 5d5358dfab7345..3ce4704dc8795d 100644
--- a/include/crypto/acompress.h
+++ b/include/crypto/acompress.h
@@ -67,6 +67,7 @@ struct acomp_req_chain {
 		struct folio *dfolio;
 	};
 	u32 flags;
+	u32 unit_size;
 };
 
 /**
@@ -79,6 +80,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 +96,7 @@ struct acomp_req {
 	};
 	unsigned int slen;
 	unsigned int dlen;
+	unsigned int unit_size;
 
 	struct acomp_req_chain chain;
 
@@ -197,7 +200,7 @@ static inline void acomp_request_set_tfm(struct acomp_req *req,
 	crypto_request_set_tfm(&req->base, crypto_acomp_tfm(tfm));
 }
 
-static inline bool acomp_is_async(struct crypto_acomp *tfm)
+static __always_inline bool acomp_is_async(struct crypto_acomp *tfm)
 {
 	return crypto_comp_alg_common(tfm)->base.cra_flags &
 	       CRYPTO_ALG_ASYNC;
@@ -292,7 +295,7 @@ static inline void *acomp_request_extra(struct acomp_req *req)
 	return (void *)((char *)req + len);
 }
 
-static inline bool acomp_req_on_stack(struct acomp_req *req)
+static __always_inline bool acomp_req_on_stack(struct acomp_req *req)
 {
 	return crypto_req_on_stack(&req->base);
 }
@@ -329,9 +332,56 @@ 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.
+ *
+ * This is a helper function that enables batching for zswap, IPComp, etc.
+ * It allows multiple independent compression (or decompression) operations to
+ * be submitted in a single request's SG lists, where each SG list ("segment")
+ * is processed independently. The unit size helps derive segments from a
+ * single request. crypto_acomp does not expect the segments to be related in
+ * any way.
+ *
+ * Example usage model:
+ *
+ * A module such as zswap that's configured to use a batching compressor, can
+ * accomplish batch compression of "nr_pages" with crypto_acomp by creating 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. Depending on whether the @req is used for batch compress/decompress,
+ * zswap must set the @req's source/destination length to be
+ * "nr_pages * @req->unit_size" respectively.
+ *
+ * An acomp_alg can implement batch compression by using the @req->unit_size
+ * to break down the SG lists passed in via @req->dst to submit individual
+ * "@req->slen/@req->unit_size" compress jobs to be processed as a batch.
+ *
+ * Similarly, zswap can implement batch decompression by passing an
+ * SG table with "nr_pages" SG lists via @req->src to process
+ * "@req->dlen/@req->unit_size" decompress jobs as a batch.
+ *
+ * This API must be called after acomp_request_set_callback(),
+ * which sets @req->unit_size to 0. This makes it easy for users of
+ * crypto_acomp to rely on a default of not opting in to batching.
+ * Users such as zswap opt in to batching by defining @req->unit_size
+ * to a non-zero value for use by acomp_algs supporting batching.
+ *
+ * @du would be PAGE_SIZE for zswap, it could be the MTU for IPsec.
+ *
+ * @req:	asynchronous compress/decompress request
+ * @du:		data unit size of the input/output 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
  *
@@ -543,6 +593,18 @@ 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:	@tfm's acomp_alg's @batch_size, if it has defined a
+ *		@batch_size greater than 1; else return 1.
+ */
+unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm);
+
 static inline struct acomp_req *acomp_request_on_stack_init(
 	char *buf, struct crypto_acomp *tfm)
 {
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h
index 05deea9dac5e88..7d406cfe57519f 100644
--- a/include/crypto/algapi.h
+++ b/include/crypto/algapi.h
@@ -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;
diff --git a/include/crypto/internal/acompress.h b/include/crypto/internal/acompress.h
index 9cd37df32dc40a..2d4d302d1b3f18 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
+ * @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
@@ -47,6 +49,7 @@
 struct acomp_alg {
 	int (*compress)(struct acomp_req *req);
 	int (*decompress)(struct acomp_req *req);
+	unsigned int batch_size;
 	int (*init)(struct crypto_acomp *tfm);
 	void (*exit)(struct crypto_acomp *tfm);
 
@@ -146,7 +149,7 @@ void crypto_unregister_acomp(struct acomp_alg *alg);
 int crypto_register_acomps(struct acomp_alg *algs, int count);
 void crypto_unregister_acomps(struct acomp_alg *algs, int count);
 
-static inline bool acomp_request_issg(struct acomp_req *req)
+static __always_inline bool acomp_request_issg(struct acomp_req *req)
 {
 	return !(req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT |
 				    CRYPTO_ACOMP_REQ_DST_VIRT));
@@ -189,6 +192,11 @@ static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm)
 	return crypto_tfm_req_virt(&tfm->base);
 }
 
+static __always_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);
 
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index b7c97f1c47c924..0628b62e494668 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -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. */
 
 /*
@@ -489,7 +492,7 @@ static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm)
 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
 }
 
-static inline bool crypto_req_on_stack(struct crypto_async_request *req)
+static __always_inline bool crypto_req_on_stack(struct crypto_async_request *req)
 {
 	return req->flags & CRYPTO_TFM_REQ_ON_STACK;
 }
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 980358bc6f49c9..09a6b9a419016b 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1921,6 +1921,8 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,
 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,
+				  u8 nr_pages, u8 nr_incomp_pages);
 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
 bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);
 #else
@@ -1932,6 +1934,11 @@ static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
 					   size_t size)
 {
 }
+static inline void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg,
+						size_t size, u8 nr_pages,
+						u8 nr_incomp_pages)
+{
+}
 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg,
 					     size_t size)
 {
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1ebc9330aeae51..48c216e621daa6 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6361,6 +6361,42 @@ void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
 	rcu_read_unlock();
 }
 
+/**
+ * obj_cgroup_charge_zswap_many - bulk charge compression backend memory
+ *                                from compressing a batch of pages.
+ * @objcg: the object cgroup
+ * @size: total compressed length of a batch of compressed objects
+ * @nr_pages: # of pages that were compressed to store in zswap
+ * @nr_incomp_pages: # of incompressible pages to store in zswap
+ *
+ * This forces the charge after obj_cgroup_may_zswap() allowed
+ * compression and storage in zswap for this cgroup to go ahead.
+ */
+void obj_cgroup_charge_zswap_many(struct obj_cgroup *objcg, size_t size,
+				  u8 nr_pages, u8 nr_incomp_pages)
+{
+	struct mem_cgroup *memcg;
+
+	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
+		return;
+
+	if (obj_cgroup_is_root(objcg))
+		return;
+
+	VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
+
+	/* PF_MEMALLOC context, charging must succeed */
+	if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
+		VM_WARN_ON_ONCE(1);
+
+	rcu_read_lock();
+	memcg = obj_cgroup_memcg(objcg);
+	mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
+	mod_memcg_state(memcg, MEMCG_ZSWAPPED, nr_pages);
+	mod_memcg_state(memcg, MEMCG_ZSWAP_INCOMP, nr_incomp_pages);
+	rcu_read_unlock();
+}
+
 /**
  * obj_cgroup_uncharge_zswap - uncharge compression backend memory
  * @objcg: the object cgroup
diff --git a/mm/zswap.c b/mm/zswap.c
index 37d217860a6b2e..67181d4fe7e4de 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -85,6 +85,9 @@ static bool zswap_pool_reached_full;
 
 #define ZSWAP_PARAM_UNSET ""
 
+/* Limit the batch size to limit per-CPU memory usage for dst buffers. */
+#define ZSWAP_MAX_BATCH_SIZE 4U
+
 static int zswap_setup(void);
 
 /* Enable/disable zswap */
@@ -144,10 +147,17 @@ struct zswap_acomp_req {
 	struct mutex mutex;
 };
 
-/* The compression mutex also protects the output buffer. */
+/*
+ * The compression mutex also protects the output buffer(s).
+ *
+ * @compr_batch_size: The max batch size of the compression algorithm,
+ *                    bounded by ZSWAP_MAX_BATCH_SIZE.
+ */
 struct zswap_comp_ctx {
 	struct zswap_acomp_req areq;
-	u8 *buffer;
+	u8 compr_batch_size;
+	struct sg_table *sg_table;
+	u8 **buffers;
 };
 
 /* Separate requests, so that decompression does not wait for compression. */
@@ -162,6 +172,7 @@ struct crypto_acomp_ctx {
  * The only case where lru_lock is not acquired while holding tree.lock is
  * when a zswap_entry is taken off the lru for writeback, in that case it
  * needs to be verified that it's still valid in the tree.
+ *
  */
 struct zswap_pool {
 	struct zs_pool *zs_pool;
@@ -181,6 +192,7 @@ struct zswap_pool {
  */
 #define ZSWAP_FIRST_POOL_ID 1
 #define ZSWAP_MAX_POOL_ID U8_MAX
+#define ZSWAP_MAX_COMPR_LENGTH_SHIFT 19U
 static DEFINE_XARRAY_FLAGS(zswap_pools, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH);
 static struct zswap_pool __rcu *zswap_current_pool;
 
@@ -207,20 +219,37 @@ static struct shrinker *zswap_shrinker;
  *              logic if referenced is unset. See comments in the shrinker
  *              section for context.
  * pool_idx - id of the zswap_pool that the entry's data is in.
+ * nid - NUMA node id of the page for which this is the zswap entry.
  * handle - zsmalloc allocation handle that stores the compressed page data
  * objcg - the obj_cgroup that the compressed memory is charged to
  * lru - handle to the pool's lru used to evict pages.
  */
 struct zswap_entry {
 	swp_entry_t swpentry;
-	unsigned int length;
-	bool referenced;
-	u8 pool_idx;
+	struct {
+		unsigned int length:ZSWAP_MAX_COMPR_LENGTH_SHIFT;
+		bool referenced:1U;
+		u8 pool_idx;
+	};
+	int nid;
 	unsigned long handle;
 	struct obj_cgroup *objcg;
 	struct list_head lru;
 };
 
+struct zswap_store_context {
+	struct folio *folio;
+	long folio_start;
+	unsigned int slen;
+	u8 nr_batch_pages;
+	bool wb_enabled;
+	int nid;
+	u8 nr_pages;
+	struct zs_pool *zs_pool;
+	struct zswap_comp_ctx *comp_ctx;
+	struct zswap_entry *entries[ZSWAP_MAX_BATCH_SIZE];
+};
+
 /*
  * No RCU section is needed around the returned pointer: a stored entry pins
  * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be
@@ -279,6 +308,8 @@ static void __zswap_pool_empty(struct percpu_ref *ref);
 
 static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
 {
+	u8 i;
+
 	if (!acomp_ctx)
 		return;
 
@@ -297,8 +328,19 @@ static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
 
 	acomp_ctx->acomp = NULL;
 
-	kfree(acomp_ctx->comp.buffer);
-	acomp_ctx->comp.buffer = NULL;
+	if (acomp_ctx->comp.buffers) {
+		for (i = 0; i < ZSWAP_MAX_BATCH_SIZE; ++i)
+			kfree(acomp_ctx->comp.buffers[i]);
+		kfree(acomp_ctx->comp.buffers);
+	}
+	acomp_ctx->comp.buffers = NULL;
+	acomp_ctx->comp.compr_batch_size = 0;
+
+	if (acomp_ctx->comp.sg_table) {
+		sg_free_table(acomp_ctx->comp.sg_table);
+		kfree(acomp_ctx->comp.sg_table);
+	}
+	acomp_ctx->comp.sg_table = NULL;
 }
 
 static struct zswap_pool *zswap_pool_create(char *compressor)
@@ -383,6 +425,7 @@ static struct zswap_pool *zswap_pool_create(char *compressor)
 cpuhp_add_fail:
 	for_each_possible_cpu(cpu)
 		acomp_ctx_free(per_cpu_ptr(pool->acomp_ctx, cpu));
+
 error:
 	if (pool->acomp_ctx)
 		free_percpu(pool->acomp_ctx);
@@ -464,12 +507,12 @@ static int __must_check zswap_pool_tryget(struct zswap_pool *pool)
 }
 
 /* The caller must already have a reference. */
-static void zswap_pool_get(struct zswap_pool *pool)
+static inline void zswap_pool_get_many(struct zswap_pool *pool, u8 nr)
 {
-	percpu_ref_get(&pool->ref);
+	percpu_ref_get_many(&pool->ref, nr);
 }
 
-static void zswap_pool_put(struct zswap_pool *pool)
+static inline void zswap_pool_put(struct zswap_pool *pool)
 {
 	percpu_ref_put(&pool->ref);
 }
@@ -680,14 +723,8 @@ static int zswap_enabled_param_set(const char *val,
 * lru functions
 **********************************/
 
-static inline int entry_to_nid(struct zswap_entry *entry)
-{
-	return page_to_nid(virt_to_page(entry));
-}
-
 static void zswap_lru_add(struct zswap_entry *entry)
 {
-	int nid = entry_to_nid(entry);
 	struct mem_cgroup *memcg;
 
 	/*
@@ -704,19 +741,18 @@ static void zswap_lru_add(struct zswap_entry *entry)
 	rcu_read_lock();
 	memcg = obj_cgroup_memcg(entry->objcg);
 	/* will always succeed */
-	list_lru_add(&zswap_list_lru, &entry->lru, nid, memcg);
+	list_lru_add(&zswap_list_lru, &entry->lru, entry->nid, memcg);
 	rcu_read_unlock();
 }
 
 static void zswap_lru_del(struct zswap_entry *entry)
 {
-	int nid = entry_to_nid(entry);
 	struct mem_cgroup *memcg;
 
 	rcu_read_lock();
 	memcg = obj_cgroup_memcg(entry->objcg);
 	/* will always succeed */
-	list_lru_del(&zswap_list_lru, &entry->lru, nid, memcg);
+	list_lru_del(&zswap_list_lru, &entry->lru, entry->nid, memcg);
 	rcu_read_unlock();
 }
 
@@ -764,10 +800,11 @@ void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg)
 **********************************/
 static struct kmem_cache *zswap_entry_cache;
 
-static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp, int nid)
+static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
 {
 	struct zswap_entry *entry;
-	entry = kmem_cache_alloc_node(zswap_entry_cache, gfp, nid);
+
+	entry = kmem_cache_alloc(zswap_entry_cache, gfp);
 	if (!entry)
 		return NULL;
 	return entry;
@@ -778,6 +815,41 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)
 	kmem_cache_free(zswap_entry_cache, entry);
 }
 
+static inline void zswap_entries_cache_free_batch(
+	struct zswap_entry **entries,
+	u8 nr_entries)
+{
+	/*
+	 * It is okay to use this to free entries allocated separately
+	 * by zswap_entry_cache_alloc().
+	 */
+	kmem_cache_free_bulk(zswap_entry_cache, nr_entries, (void **)entries);
+}
+
+static bool zswap_entries_cache_alloc_batch(
+	struct zswap_store_context *zc,
+	gfp_t gfp)
+{
+	bool ret = kmem_cache_alloc_bulk(zswap_entry_cache, gfp,
+					 zc->nr_pages, (void **)zc->entries);
+
+	if (unlikely(!ret)) {
+		u8 i;
+
+		for (i = 0; i < zc->nr_pages; ++i) {
+			zc->entries[i] = zswap_entry_cache_alloc(GFP_KERNEL);
+
+			if (unlikely(!zc->entries[i])) {
+				zswap_reject_kmemcache_fail++;
+				zswap_entries_cache_free_batch(zc->entries, i);
+				return false;
+			}
+		}
+	}
+
+	return true;
+}
+
 /*
  * Carries out the common pattern of freeing an entry's zsmalloc allocation,
  * freeing the entry itself, and decrementing the number of stored pages.
@@ -787,7 +859,7 @@ static void zswap_entry_free(struct zswap_entry *entry)
 	struct zswap_pool *pool = zswap_entry_pool(entry);
 
 	zswap_lru_del(entry);
-	if (!WARN_ON_ONCE(!pool)) {
+	if (likely(!WARN_ON_ONCE(!pool))) {
 		zs_free(pool->zs_pool, entry->handle);
 		zswap_pool_put(pool);
 	}
@@ -827,6 +899,12 @@ static int zswap_acomp_req_init(struct zswap_acomp_req *areq,
 	crypto_init_wait(&areq->wait);
 	zswap_set_acomp_req_callback(areq->req, &areq->wait);
 
+	/*
+	 * This should be called after acomp_request_set_callback(),
+	 * which initializes req->unit_size to 0.
+	 */
+	acomp_request_set_unit_size(areq->req, PAGE_SIZE);
+
 	mutex_init(&areq->mutex);
 	return 0;
 }
@@ -835,7 +913,10 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 {
 	struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
 	struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
+	int nid = cpu_to_node(cpu);
+	struct scatterlist *sg;
 	int ret = -ENOMEM;
+	u8 i;
 
 	/*
 	 * To handle cases where the CPU goes through online-offline-online
@@ -846,15 +927,11 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 		return 0;
 	}
 
-	acomp_ctx->comp.buffer = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu));
-	if (!acomp_ctx->comp.buffer)
-		return ret;
-
 	/*
 	 * In case of an error, crypto_alloc_acomp_node() returns an
 	 * error pointer, never NULL.
 	 */
-	acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
+	acomp_ctx->acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, nid);
 	if (IS_ERR(acomp_ctx->acomp)) {
 		pr_err("could not alloc crypto acomp %s : %pe\n",
 				pool->tfm_name, acomp_ctx->acomp);
@@ -862,10 +939,55 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 		goto fail;
 	}
 
+	/*
+	 * Constrain the crypto algorithm's batch size by ZSWAP_MAX_BATCH_SIZE,
+	 * store this in the per-CPU zswap_comp_ctx's @compr_batch_size.
+	 */
+	acomp_ctx->comp.compr_batch_size = min(ZSWAP_MAX_BATCH_SIZE,
+					       crypto_acomp_batch_size(acomp_ctx->acomp));
+
 	ret = zswap_acomp_req_init(&acomp_ctx->comp.areq, acomp_ctx->acomp);
 	if (ret)
 		goto req_fail;
 
+	/*
+	 * Allocate up to ZSWAP_MAX_BATCH_SIZE dst buffers for all compressors.
+	 */
+	acomp_ctx->comp.buffers = kcalloc_node(ZSWAP_MAX_BATCH_SIZE,
+					       sizeof(u8 *), GFP_KERNEL, nid);
+	if (!acomp_ctx->comp.buffers) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	for (i = 0; i < ZSWAP_MAX_BATCH_SIZE; ++i) {
+		acomp_ctx->comp.buffers[i] = kmalloc_node(PAGE_SIZE, GFP_KERNEL, nid);
+		if (!acomp_ctx->comp.buffers[i]) {
+			ret = -ENOMEM;
+			goto req_fail;
+		}
+	}
+
+	acomp_ctx->comp.sg_table = kmalloc(sizeof(*acomp_ctx->comp.sg_table),
+					   GFP_KERNEL);
+	if (!acomp_ctx->comp.sg_table) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	if (sg_alloc_table(acomp_ctx->comp.sg_table, ZSWAP_MAX_BATCH_SIZE,
+			   GFP_KERNEL)) {
+		ret = -ENOMEM;
+		goto req_fail;
+	}
+
+	/*
+	 * Statically map the per-CPU destination buffers to the per-CPU
+	 * SG lists; crucial for performance.
+	 */
+	for_each_sg(acomp_ctx->comp.sg_table->sgl, sg, ZSWAP_MAX_BATCH_SIZE, i)
+		sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE);
+
 	/* Skip the per-CPU request when decompression can use the stack. */
 	if (acomp_is_async(acomp_ctx->acomp)) {
 		ret = zswap_acomp_req_init(&acomp_ctx->decomp, acomp_ctx->acomp);
@@ -882,95 +1004,180 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	return ret;
 }
 
-static bool zswap_compress(struct folio *folio, long index,
-			   struct zswap_entry *entry, struct zswap_pool *pool)
+/*
+ * zswap_update_entries() is called only when all pages in a store batch have
+ * been successfully compressed (subsumes the writeback_enabled fallback in case
+ * of compress errors). The only error that can occur in this phase is zs_malloc
+ * allocation failure, upon which an error status is returned to
+ * zswap_store_pages().
+ */
+static inline bool zswap_update_entries(struct zswap_store_context *zc)
 {
-	struct zswap_comp_ctx *comp_ctx;
-	struct scatterlist input, output;
-	int comp_ret = 0, alloc_ret = 0;
-	unsigned int dlen = PAGE_SIZE;
+	gfp_t gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
+	struct zswap_entry *entry;
+	struct scatterlist *sg;
 	unsigned long handle;
-	gfp_t gfp;
-	u8 *dst;
-	bool mapped = false;
+	unsigned int dlen;
+	u8 batch_iter;
+	u8 *buf;
 
-	comp_ctx = &raw_cpu_ptr(pool->acomp_ctx)->comp;
-	mutex_lock(&comp_ctx->areq.mutex);
+	/*
+	 * For each output SG list in @acomp_ctx->req->sg_table->sgl,
+	 * the @sg->length should be set to either the page's compressed
+	 * length (success), or it's negative value compression error.
+	 */
+	for_each_sg(zc->comp_ctx->sg_table->sgl, sg, zc->nr_pages, batch_iter) {
+		entry = zc->entries[batch_iter];
+		dlen = sg->length;
+
+		prefetchw(entry);
 
-	dst = comp_ctx->buffer;
-	sg_init_table(&input, 1);
-	sg_set_folio(&input, folio, PAGE_SIZE, index * PAGE_SIZE);
+		handle = zs_malloc(zc->zs_pool, dlen, gfp, zc->nid);
+
+		if (unlikely(IS_ERR_VALUE(handle))) {
+			if (PTR_ERR((void *)handle) == -ENOSPC)
+				zswap_reject_compress_poor++;
+			else
+				zswap_reject_alloc_fail++;
+
+			return false;
+		}
+
+		entry->length = dlen;
+		entry->handle = handle;
+
+		if (unlikely(dlen == PAGE_SIZE)) {
+			buf = kmap_local_folio(zc->folio,
+					(zc->folio_start + batch_iter) * PAGE_SIZE);
+			zs_obj_write(zc->zs_pool, handle, buf, dlen);
+			kunmap_local(buf);
+		} else {
+			buf = zc->comp_ctx->buffers[batch_iter];
+			zs_obj_write(zc->zs_pool, handle, buf, dlen);
+		}
+	}
+
+	return true;
+}
 
-	sg_init_one(&output, dst, PAGE_SIZE);
-	acomp_request_set_params(comp_ctx->areq.req, &input, &output,
-				 PAGE_SIZE, dlen);
+/*
+ * zswap_compress() batching implementation for sequential and batching
+ * compressors.
+ *
+ * Description:
+ * ============
+ * Compress multiple @zc->nr_pages in @zc->folio starting from the
+ * @zc->folio_start index in batches of @zc->nr_batch_pages; the latter
+ * representing the lesser of the @zc->nr_pages to be batch compressed and the
+ * compressor's batch size.
+ *
+ * @zc->nr_pages can be in (1, ZSWAP_MAX_BATCH_SIZE] even if the compressor does
+ * not support batching.
+ *
+ * If @nr_batch_pages is 1, each page is compressed sequentially.
+ *
+ * If @nr_batch_pages is > 1, the compressor may use batching to optimize
+ * compression.
+ *
+ * In all cases, the store processing that needs to happen post-compression
+ * occurs in batches of @zc->nr_pages. Error handling is minimal, mostly to
+ * update zswap statistics, with catch-all cleanup for the batch occuring in the
+ * @store_pages_failed label in zswap_store_pages().
+ */
+static bool zswap_compress(struct zswap_store_context *zc)
+{
+	u8 batch_start, comp_batch_iter;
+	struct scatterlist *sg, *dst_sg;
+	int err;
 
 	/*
-	 * it maybe looks a little bit silly that we send an asynchronous request,
-	 * then wait for its completion synchronously. This makes the process look
-	 * synchronous in fact.
-	 * Theoretically, acomp supports users send multiple acomp requests in one
-	 * acomp instance, then get those requests done simultaneously. but in this
-	 * case, zswap actually does store and load page by page, there is no
-	 * existing method to send the second page before the first page is done
-	 * in one thread doing zswap.
-	 * but in different threads running on different cpu, we have different
-	 * acomp instance, and compression and decompression use separate
-	 * requests, so multiple threads can do (de)compression in parallel.
+	 * @nr_batch_pages is constrained by the per-CPU comp_ctx's compressor
+	 * batch size and the @zc->nr_pages in this store batch. @slen indicates
+	 * the total source length bytes for @nr_batch_pages.
 	 */
-	comp_ret = crypto_wait_req(crypto_acomp_compress(comp_ctx->areq.req),
-				   &comp_ctx->areq.wait);
-	dlen = comp_ctx->areq.req->dlen;
-	if (!comp_ret) {
-		if (dlen == PAGE_SIZE)
-			comp_ret = -ENOSPC;
-		else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE))
-			comp_ret = -EINVAL;
-	}
+	zc->nr_batch_pages = min(zc->comp_ctx->compr_batch_size, zc->nr_pages);
+	zc->slen = zc->nr_batch_pages * PAGE_SIZE;
 
 	/*
-	 * If a page cannot be compressed into a size smaller than PAGE_SIZE,
-	 * save the content as is without a compression, to keep the LRU order
-	 * of writebacks.  If writeback is disabled, reject the page since it
-	 * only adds metadata overhead.  swap_writeout() will put the page back
-	 * to the active LRU list in the case.
+	 * Compress the @nr_pages in @folio starting at index @folio_start
+	 * in batches of @nr_batch_pages.
 	 */
-	if (comp_ret) {
-		rcu_read_lock();
-		if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
-			rcu_read_unlock();
-			goto unlock;
+	for (batch_start = 0; batch_start < zc->nr_pages;
+	     batch_start += zc->nr_batch_pages) {
+		/*
+		 * Send @nr_batch_pages to crypto_acomp for compression:
+		 *
+		 * These pages are in @folio's range of indices in the interval
+		 *     [@folio_start + @batch_start,
+		 *      @folio_start + @batch_start + @nr_batch_pages).
+		 *
+		 * The source folio pages in the batch are directly submitted to
+		 * crypto_acomp via acomp_request_set_src_folio().
+		 */
+		acomp_request_set_src_folio(zc->comp_ctx->areq.req, zc->folio,
+				(zc->folio_start + batch_start) * PAGE_SIZE,
+				zc->slen);
+
+		dst_sg = zc->comp_ctx->sg_table->sgl + batch_start;
+		dst_sg->length = zc->slen;
+
+		/*
+		 * The per-CPU @zc->comp_ctx->sg_table scatterlists are statically
+		 * mapped to the per-CPU dst @buffers at pool creation time.
+		 * There are ZSWAP_MAX_BATCH_SIZE per-CPU SG lists and dst
+		 * buffers allocated for all compressors.
+		 */
+		acomp_request_set_dst_sg(zc->comp_ctx->areq.req,
+					 dst_sg, zc->slen);
+
+		err = crypto_wait_req(crypto_acomp_compress(zc->comp_ctx->areq.req),
+				      &zc->comp_ctx->areq.wait);
+
+		/*
+		 * If a page cannot be compressed into a size smaller than
+		 * PAGE_SIZE, save the content as is without a compression, to
+		 * keep the LRU order of writebacks.  If writeback is disabled,
+		 * reject the page since it only adds metadata overhead.
+		 * swap_writeout() will put the page back to the active LRU list
+		 * in the case.
+		 */
+		if (err && !zc->wb_enabled)
+			goto compress_error;
+
+		/*
+		 * For each output SG list in @acomp_ctx->req->sg_table->sgl,
+		 * the @sg->length should be set to either the page's compressed
+		 * length (success), or it's negative value compression error.
+		 */
+		for_each_sg(dst_sg, sg, zc->nr_batch_pages, comp_batch_iter) {
+
+			if (sg->length <= 0 || sg->length >= PAGE_SIZE) {
+				if (!zc->wb_enabled) {
+					if (sg->length == PAGE_SIZE)
+						sg->length = -ENOSPC;
+					else if (WARN_ON_ONCE(!sg->length || sg->length > PAGE_SIZE))
+						sg->length = -EINVAL;
+					goto compress_error;
+				}
+
+				sg->length = PAGE_SIZE;
+			}
 		}
-		rcu_read_unlock();
-		comp_ret = 0;
-		dlen = PAGE_SIZE;
-		dst = kmap_local_folio(folio, index * PAGE_SIZE);
-		mapped = true;
 	}
 
-	gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
-	handle = zs_malloc(pool->zs_pool, dlen, gfp, folio_nid(folio));
-	if (IS_ERR_VALUE(handle)) {
-		alloc_ret = PTR_ERR((void *)handle);
-		goto unlock;
-	}
+	return zswap_update_entries(zc);
 
-	zs_obj_write(pool->zs_pool, handle, dst, dlen);
-	entry->handle = handle;
-	entry->length = dlen;
+compress_error:
+	for_each_sg(dst_sg, sg, zc->nr_batch_pages, comp_batch_iter) {
+		if ((int)sg->length < 0) {
+			if ((int)sg->length == -ENOSPC)
+				zswap_reject_compress_poor++;
+			else
+				zswap_reject_compress_fail++;
+		}
+	}
 
-unlock:
-	if (mapped)
-		kunmap_local(dst);
-	if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)
-		zswap_reject_compress_poor++;
-	else if (comp_ret)
-		zswap_reject_compress_fail++;
-	else if (alloc_ret)
-		zswap_reject_alloc_fail++;
-
-	mutex_unlock(&comp_ctx->areq.mutex);
-	return comp_ret == 0 && alloc_ret == 0;
+	return false;
 }
 
 static bool __zswap_decompress(struct zswap_entry *entry,
@@ -1527,89 +1734,194 @@ static void shrink_worker(struct work_struct *w)
 * main API
 **********************************/
 
-static bool zswap_store_page(struct folio *folio, long index,
-			     struct obj_cgroup *objcg,
-			     struct zswap_pool *pool)
+/* Functions called by zswap_store(), that operate on batches of pages. */
+
+/*
+ * zswap_init_entries() co-locates most of the zswap entry fields' setup under
+ * the assumption that all phases of store processing will succeed. An important
+ * marker to simplify catch-all cleanup in zswap_store_pages() is done by
+ * initializing each entry's @handle to ERR_PTR(-EINVAL).
+ */
+static inline void zswap_init_entries(struct zswap_store_context *zc,
+				      struct zswap_pool *pool,
+				      struct obj_cgroup *objcg)
 {
-	swp_entry_t page_swpentry = folio_swap_entry(folio, index);
-	struct zswap_entry *entry, *old;
+	struct zswap_entry *entry;
+	u8 i;
 
-	/* allocate entry */
-	entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));
-	if (!entry) {
-		zswap_reject_kmemcache_fail++;
-		return false;
+	/*
+	 * We co-locate entry initialization as much as possible here to
+	 * minimize potential cache misses.
+	 */
+	for (i = 0; i < zc->nr_pages; ++i) {
+		entry = zc->entries[i];
+		entry->handle = (unsigned long)ERR_PTR(-EINVAL);
+		/*
+		 * Set pool_idx before the xa_store() in zswap_tree_stores()
+		 * publishes the entry, or a concurrent reader could resolve a
+		 * stale pool_idx left by slab reuse to an unrelated live pool.
+		 */
+		entry->pool_idx = pool->idx;
+		entry->swpentry = folio_swap_entry(zc->folio,
+							zc->folio_start + i);
+		entry->objcg = objcg;
+		entry->referenced = true;
+		entry->nid = zc->nid;
+		INIT_LIST_HEAD(&entry->lru);
 	}
+}
 
-	if (!zswap_compress(folio, index, entry, pool))
-		goto compress_failed;
+/*
+ * zswap_tree_stores() stores the batch entries in xarray and adds them to the
+ * zswap LRU list. If an error occurs during xa_store(), localized state
+ * unwinding is done to erase prior batch entries from the xarray and delete
+ * them from the zswap LRU list. Remaining batch cleanup occurs in
+ * zswap_store_pages(). As batch entries are processed, we maintain aggregate
+ * statistics to do the accounting in one bulk operation.
+ */
+static bool zswap_tree_stores(struct zswap_store_context *zc,
+			      struct zswap_pool *pool,
+			      struct obj_cgroup *objcg)
+{
+	u8 i, store_fail_idx = 0, nr_incomp = 0;
+	size_t total_batch_comp_length = 0;
+	struct zswap_entry *old, *entry;
+
+	for (i = 0; i < zc->nr_pages; ++i) {
+		entry = zc->entries[i];
+
+		old = xa_store(swap_zswap_tree(entry->swpentry),
+			       swp_offset(entry->swpentry),
+			       entry, GFP_KERNEL);
+		if (unlikely(xa_is_err(old))) {
+			int err = xa_err(old);
+
+			WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
+			zswap_reject_alloc_fail++;
+			/*
+			 * Entries up to this point have been stored in the
+			 * xarray. Local cleanup in 'tree_store_failed' needs to
+			 * erase entries up until now.
+			 */
+			store_fail_idx = i;
+			goto tree_store_failed;
+		}
 
-	/*
-	 * Set pool_idx before the xa_store() below publishes the entry, or a
-	 * concurrent reader could resolve a stale pool_idx left by slab reuse
-	 * to an unrelated live pool.
-	 */
-	entry->pool_idx = pool->idx;
+		/*
+		 * We may have had an existing entry that became stale when
+		 * the folio was redirtied and now the new version is being
+		 * swapped out. Get rid of the old.
+		 */
+		if (unlikely(old))
+			zswap_entry_free(old);
 
-	old = xa_store(swap_zswap_tree(page_swpentry),
-		       swp_offset(page_swpentry),
-		       entry, GFP_KERNEL);
-	if (xa_is_err(old)) {
-		int err = xa_err(old);
+		total_batch_comp_length += entry->length;
 
-		WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
-		zswap_reject_alloc_fail++;
-		goto store_failed;
-	}
+		if (unlikely(entry->length == PAGE_SIZE))
+			++nr_incomp;
 
-	/*
-	 * We may have had an existing entry that became stale when
-	 * the folio was redirtied and now the new version is being
-	 * swapped out. Get rid of the old.
-	 */
-	if (old)
-		zswap_entry_free(old);
+		/*
+		 * We finish by adding the entry to the LRU while it's already
+		 * in xarray. This is safe because:
+		 *
+		 * 1. Concurrent stores and invalidations are excluded by folio lock.
+		 *
+		 * 2. Writeback is excluded by the entry not being on the LRU yet.
+		 *    The publishing order matters to prevent writeback from seeing
+		 *    an incoherent entry.
+		 */
+		if (likely(entry->length))
+			zswap_lru_add(entry);
+	}
 
 	/*
-	 * The entry is successfully compressed and stored in the tree, there is
-	 * no further possibility of failure. Grab refs to the pool and objcg,
-	 * charge zswap memory, and increment zswap_stored_pages.
-	 * The opposite actions will be performed by zswap_entry_free()
-	 * when the entry is removed from the tree.
+	 * Each batch entry is successfully compressed and stored in the tree,
+	 * and further failures will be cleaned up in zswap_store().
+	 * Next, we do bulk accounting for the batch: grab refs to the pool and
+	 * objcg, charge zswap memory, increment zswap_stored_pages and
+	 * zswap_stored_incompressible_pages. The opposite actions will be
+	 * performed by zswap_entry_free() when the entry is removed from the
+	 * tree.
 	 */
-	zswap_pool_get(pool);
+	zswap_pool_get_many(pool, zc->nr_pages);
 	if (objcg) {
-		obj_cgroup_get(objcg);
-		obj_cgroup_charge_zswap(objcg, entry->length);
+		obj_cgroup_get_many(objcg, zc->nr_pages);
+		obj_cgroup_charge_zswap_many(objcg, total_batch_comp_length,
+					     zc->nr_pages, nr_incomp);
 	}
-	atomic_long_inc(&zswap_stored_pages);
-	if (entry->length == PAGE_SIZE)
-		atomic_long_inc(&zswap_stored_incompressible_pages);
+	atomic_long_add(zc->nr_pages, &zswap_stored_pages);
+	atomic_long_add(nr_incomp, &zswap_stored_incompressible_pages);
+
+	return true;
+
+tree_store_failed:
+	for (i = 0; i < store_fail_idx; ++i) {
+		xa_erase(swap_zswap_tree(zc->entries[i]->swpentry),
+			 swp_offset(zc->entries[i]->swpentry));
+		zswap_lru_del(zc->entries[i]);
+	}
+
+	return false;
+}
+
+/*
+ * Store multiple pages in @zc->folio, starting from the page at index @start up
+ * to the page at index @end-1, i.e. @zc->nr_pages. Each phase of
+ * zswap_store_pages() does batch processing of @zc->nr_pages in the folio.
+ * State unwinding is kept to a minimum as a result of the batch processing: a
+ * batch proceeds to the next phase of store processing only if it passes this
+ * stage. The batch cleanup is consolidated under the @store_pages_failed
+ * label. Errors in storing a batch result in folio level cleanup occurring in
+ * zswap_store().
+ */
+static bool zswap_store_pages(struct zswap_store_context *zc,
+			      long start,
+			      long end,
+			      struct zswap_pool *pool,
+			      struct obj_cgroup *objcg)
+{
+	bool ret;
+	u8 i;
+
+	zc->nr_pages = end - start;
+	zc->folio_start = start;
+
+	VM_WARN_ON_ONCE(zc->nr_pages <= 0 || zc->nr_pages > ZSWAP_MAX_BATCH_SIZE);
+
+	ret = zswap_entries_cache_alloc_batch(zc, GFP_KERNEL);
+	if (unlikely(!ret))
+		return false;
+
+	zswap_init_entries(zc, pool, objcg);
 
 	/*
-	 * We finish initializing the entry while it's already in xarray.
-	 * This is safe because:
-	 *
-	 * 1. Concurrent stores and invalidations are excluded by folio lock.
-	 *
-	 * 2. Writeback is excluded by the entry not being on the LRU yet.
-	 *    The publishing order matters to prevent writeback from seeing
-	 *    an incoherent entry.
+	 * Locking the comp_ctx mutex once per store batch results in better
+	 * performance as compared to locking per compress batch.
 	 */
-	entry->swpentry = page_swpentry;
-	entry->objcg = objcg;
-	entry->referenced = true;
-	if (entry->length) {
-		INIT_LIST_HEAD(&entry->lru);
-		zswap_lru_add(entry);
-	}
+	zc->comp_ctx = &raw_cpu_ptr(pool->acomp_ctx)->comp;
+	mutex_lock(&zc->comp_ctx->areq.mutex);
+
+	ret = zswap_compress(zc);
+
+	mutex_unlock(&zc->comp_ctx->areq.mutex);
+
+	if (unlikely(!ret))
+		goto store_pages_failed;
+
+	ret = zswap_tree_stores(zc, pool, objcg);
+
+	if (unlikely(!ret))
+		goto store_pages_failed;
 
 	return true;
 
-store_failed:
-	zs_free(pool->zs_pool, entry->handle);
-compress_failed:
-	zswap_entry_cache_free(entry);
+store_pages_failed:
+	for (i = 0; i < zc->nr_pages; ++i) {
+		if (!IS_ERR_VALUE(zc->entries[i]->handle))
+			zs_free(pool->zs_pool, zc->entries[i]->handle);
+	}
+
+	zswap_entries_cache_free_batch(zc->entries, zc->nr_pages);
 	return false;
 }
 
@@ -1619,9 +1931,14 @@ bool zswap_store(struct folio *folio)
 	swp_entry_t swp = folio->swap;
 	struct obj_cgroup *objcg = NULL;
 	struct mem_cgroup *memcg = NULL;
-	struct zswap_pool *pool;
+	int nid = folio_nid(folio);
 	bool ret = false;
-	long index;
+	struct zswap_pool *pool;
+	long start, end;
+	struct zswap_store_context zc = {
+		.folio = folio,
+		.nid = nid,
+	};
 
 	VM_WARN_ON_ONCE(!folio_test_locked(folio));
 	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
@@ -1655,8 +1972,24 @@ bool zswap_store(struct folio *folio)
 		mem_cgroup_put(memcg);
 	}
 
-	for (index = 0; index < nr_pages; ++index) {
-		if (!zswap_store_page(folio, index, objcg, pool))
+	zc.zs_pool = pool->zs_pool;
+	rcu_read_lock();
+	zc.wb_enabled = mem_cgroup_zswap_writeback_enabled(folio_memcg(folio));
+	rcu_read_unlock();
+
+	/*
+	 * For batching and non-batching compressors, store the folio in
+	 * batches of ZSWAP_MAX_BATCH_SIZE, where the pages in the batch
+	 * are compressed in batches of the compressor's batch_size. This gives
+	 * better performance than invoking zswap_store_pages() per-page,
+	 * due to cache locality of working set structures.
+	 */
+	for (start = 0; start < nr_pages; start += ZSWAP_MAX_BATCH_SIZE) {
+		end = min(start + ZSWAP_MAX_BATCH_SIZE, nr_pages);
+
+		ret = zswap_store_pages(&zc, start, end, pool, objcg);
+
+		if (unlikely(!ret))
 			goto put_pool;
 	}
 
@@ -1926,6 +2259,8 @@ static int zswap_setup(void)
 	struct zswap_pool *pool;
 	int ret;
 
+	BUILD_BUG_ON(ZSWAP_MAX_COMPR_LENGTH_SHIFT <= PAGE_SHIFT);
+
 	zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
 	if (!zswap_entry_cache) {
 		pr_err("entry cache creation failed\n");

19/3 2026/11/09 05:48 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 18369
							output: 34
							thoughts: 4670
						

Thoughts:
**KMSAN Fuzzing Evaluation for Zswap Batching Support**

Alright, let's break this down. My goal here is to determine if this patch series, which introduces batching support to crypto_acomp and zswap, warrants a dedicated KMSAN fuzzing session. I need to pinpoint any potential uninitialized memory reads or exposure of such issues due to the changes.

I've carefully read through the patch, noting the files touched and the key modifications. It seems we're dealing with the addition of batching in `crypto/acompress.c` and within the zswap implementation. Specifically, changes in `crypto/acompress.c` like `acomp_save_req`, `acomp_restore_req`, `acomp_reqchain_finish`, `acomp_do_req_chain` and `crypto_acomp_compress` along with the structures added in the associated headers. Also the changes made to `mm/memcontrol.c`, `mm/zswap.c` and the associated headers.

The critical area of concern involves the structures being allocated and the lengths and other properties associated with those structures. So let's look at the buffers and lengths and all those pieces involved.

I'm focused on potential uninitialized memory issues. `zswap_entry` is allocated using `kmem_cache_alloc_bulk`. My concern here is `kmem_cache_alloc_bulk` is not using `__GFP_ZERO`, meaning we need to carefully track when the fields are initialized and used.

Specifically, I see that within `zswap_init_entries` fields like `handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, and `lru` are initialized, but `length` is not. This is where I started to see some important stuff... I am looking for the initialization of `entry->length`.

However, `length` is initialized later in `zswap_update_entries`. This is only reached when compression succeeds and then data is written out. It looks like the program should be safe in this situation. It looks like if compression fails, the `length` field in the cache entry is never read, so there are no issues.

Now, checking the bitfield packing of `struct zswap_entry`, I have to make sure the compiler handles the potential read/write of uninitialized memory in an adjacent field to `length` correctly. This is one of the more common causes of confusion here. However, KMSAN's instrumentation for bitfields should prevent false positives in this case. Also the `entry->referenced = true` is safe because it is written to after the other fields are initialized.

I'm looking closely now at `crypto_acomp_compress` and how it interacts with the batching. I see a condition where `req->unit_size != req->slen` causes `acomp_do_req_chain` to be called. However, `acomp_do_req_chain` returns `-ENOSYS` when `req->unit_size && req->slen > req->unit_size`. This happens when `nr_batch_pages > 1`. However, in this case, the for loop is set to iterate through all of the destination scatter/gather list entries.

The for loop iterates over each element in the `dst_sg` list. The length field in the sg list is then checked, and depending on whether the white-box is enabled, the length field is set to either a specific error code or the `PAGE_SIZE`. In either case, after this point, the length field is initialized, and it is safe to proceed.

Ultimately, I'm going to say that I don't see any obvious uninitialized memory reads in this patch series. The bitfield handling, while tricky, is handled correctly by KMSAN. The logic surrounding `zswap_entry->length` is safe in all execution paths that I could see. The patch introduces a new code path that could trigger a KMSAN bug, but only under conditions that should be very rare. So I think it's safe to say that this patch is low priority, for KMSAN fuzzing.



20/3 2026/11/09 05:48 tool read-file
0m Args:
{
  "File": "mm/zswap.c",
  "FirstLine": 960,
  "LineCount": 40
}

Results:
{
  "Contents": " 960:\t\t\tgoto req_fail;\n 961:\t\t}\n 962:\t\n 963:\t\tfor (i = 0; i \u003c ZSWAP_MAX_BATCH_SIZE; ++i) {\n 964:\t\t\tacomp_ctx-\u003ecomp.buffers[i] = kmalloc_node(PAGE_SIZE, GFP_KERNEL, nid);\n 965:\t\t\tif (!acomp_ctx-\u003ecomp.buffers[i]) {\n 966:\t\t\t\tret = -ENOMEM;\n 967:\t\t\t\tgoto req_fail;\n 968:\t\t\t}\n 969:\t\t}\n 970:\t\n 971:\t\tacomp_ctx-\u003ecomp.sg_table = kmalloc(sizeof(*acomp_ctx-\u003ecomp.sg_table),\n 972:\t\t\t\t\t\t   GFP_KERNEL);\n 973:\t\tif (!acomp_ctx-\u003ecomp.sg_table) {\n 974:\t\t\tret = -ENOMEM;\n 975:\t\t\tgoto req_fail;\n 976:\t\t}\n 977:\t\n 978:\t\tif (sg_alloc_table(acomp_ctx-\u003ecomp.sg_table, ZSWAP_MAX_BATCH_SIZE,\n 979:\t\t\t\t   GFP_KERNEL)) {\n 980:\t\t\tret = -ENOMEM;\n 981:\t\t\tgoto req_fail;\n 982:\t\t}\n 983:\t\n 984:\t\t/*\n 985:\t\t * Statically map the per-CPU destination buffers to the per-CPU\n 986:\t\t * SG lists; crucial for performance.\n 987:\t\t */\n 988:\t\tfor_each_sg(acomp_ctx-\u003ecomp.sg_table-\u003esgl, sg, ZSWAP_MAX_BATCH_SIZE, i)\n 989:\t\t\tsg_set_buf(sg, acomp_ctx-\u003ecomp.buffers[i], PAGE_SIZE);\n 990:\t\n 991:\t\t/* Skip the per-CPU request when decompression can use the stack. */\n 992:\t\tif (acomp_is_async(acomp_ctx-\u003eacomp)) {\n 993:\t\t\tret = zswap_acomp_req_init(\u0026acomp_ctx-\u003edecomp, acomp_ctx-\u003eacomp);\n 994:\t\t\tif (ret)\n 995:\t\t\t\tgoto req_fail;\n 996:\t\t}\n 997:\t\n 998:\t\treturn 0;\n 999:\t\n"
}

21/3 2026/11/09 05:48 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24602
							output: 24
							thoughts: 1357
						

Thoughts:
**Analyzing the zswap Compression Path with Error Handling**

Okay, so I'm digging into this zswap code and trying to figure out what happens when the compression step returns an error, but writeback is enabled (`zc->wb_enabled` is true). My initial thought is, what if `err` is something like `-ENOSYS` or any error, and `zc->wb_enabled` is true?

Let's trace it. Lines 1072-1088 are crucial. We call `crypto_wait_req` on the compression request. If it errors (`err != 0`), the code checks `if (err && !zc->wb_enabled)`. Now, the important thing here is, because `zc->wb_enabled` *is* true, that condition is false, and we *don't* jump to `compress_error`.

Then, we hit the `for_each_sg` loop on the destination scatterlist (`dst_sg`). Here's where it gets interesting:

If `sg->length <= 0 || sg->length >= PAGE_SIZE`, we set `sg->length = PAGE_SIZE;`! This happens *because* `zc->wb_enabled` is true.

The loop finishes, and we `return zswap_update_entries(zc)`. Now, let's look at `zswap_update_entries`:

Inside `zswap_update_entries`, we have *another* `for_each_sg` loop.  The `dlen` (data length) is set to `sg->length`. Now, if `dlen == PAGE_SIZE`, we're reading from `zc->folio` and writing it! And `zc->folio` is the swap page being swapped out, which means initialized page content.

*However* - wait! If `dlen < PAGE_SIZE`, we *should* be reading from the compressed buffer (`comp_ctx->buffers`). And then it hits me: When could `dlen < PAGE_SIZE`? Only if the compression *succeeded*. The return is the *compressed* size!

So, can `sg->length` ever be uninitialized? No. Where does `sg->length` come from in the first place? Let's trace that.

1.  In `zswap_cpu_comp_prepare`, `sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE)` is called! `sg_set_buf` sets `sg->length = PAGE_SIZE`.
2.  In `zswap_compress`, `dst_sg->length` is set to `zc->slen`, which is *the* compressed length.

Okay, so now I get it. If the compression *fails* but `wb_enabled` is true, the `sg->length` (which, by default, is initialized with `PAGE_SIZE`) is *not* overwritten if an error occurs. But, if there *was* no error, then that `dst_sg->length` *is* the size of the compressed data.

Okay, and then the critical question - what about `dst_sg[1]`, `dst_sg[2]`, etc. if `nr_batch_pages > 1`?

Wait! `acomp_request_set_dst_sg` seems key to how the batch compression works. Let's find out how that function works and figure out the full picture.



22/3 2026/11/09 05:49 tool grepper
0m Args:
{
  "Expression": "acomp_request_set_dst_sg"
}

Results:
{
  "Output": "crypto/acompress.c=195=static void acomp_virt_to_sg(struct acomp_req *req)\n--\ncrypto/acompress.c-216-\t\tsg_init_one(\u0026state-\u003edsg, dvirt, dlen);\ncrypto/acompress.c:217:\t\tacomp_request_set_dst_sg(req, \u0026state-\u003edsg, dlen);\ncrypto/acompress.c-218-\t}\n--\ninclude/crypto/acompress.h=486=static inline void acomp_request_set_src_folio(struct acomp_req *req,\n--\ninclude/crypto/acompress.h-495-/**\ninclude/crypto/acompress.h:496: * acomp_request_set_dst_sg() -- Sets destination scatterlist\ninclude/crypto/acompress.h-497- *\n--\ninclude/crypto/acompress.h-503- */\ninclude/crypto/acompress.h:504:static inline void acomp_request_set_dst_sg(struct acomp_req *req,\ninclude/crypto/acompress.h-505-\t\t\t\t\t    struct scatterlist *dst,\n--\ninclude/crypto/acompress.h=565=static inline void acomp_request_set_dst_folio(struct acomp_req *req,\n--\ninclude/crypto/acompress.h-570-\tsg_set_folio(\u0026req-\u003echain.dsg, folio, len, off);\ninclude/crypto/acompress.h:571:\tacomp_request_set_dst_sg(req, \u0026req-\u003echain.dsg, len);\ninclude/crypto/acompress.h-572-}\n--\nmm/zswap.c=1087=static bool zswap_compress(struct zswap_store_context *zc)\n--\nmm/zswap.c-1129-\t\t */\nmm/zswap.c:1130:\t\tacomp_request_set_dst_sg(zc-\u003ecomp_ctx-\u003eareq.req,\nmm/zswap.c-1131-\t\t\t\t\t dst_sg, zc-\u003eslen);\n"
}

23/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27266
							output: 36
							thoughts: 37
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach this task, as if these were my internal thoughts:

**Analyzing `acomp_request_set_dst_sg` within `acompress.h`**

Alright, I need to understand what `acomp_request_set_dst_sg` does, specifically within the context of the ACOMP compression framework. I'm going to jump to `include/crypto/acompress.h` around line 504. The function's name hints at setting the destination scatterlist (sg), which is standard for data transfer operations within the kernel. It’s likely involved in directing the compressed output data. I'll need to examine the surrounding code around line 504 to see exactly how it's used – what arguments it takes, the types of data structures it interacts with (probably `struct acomp_request`, and likely related scatterlists), and any internal manipulations it performs on the sg list. This will give me a better understanding of its role in the compression process and how it handles destination memory management.



24/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "include/crypto/acompress.h",
  "FirstLine": 500,
  "LineCount": 40
}

Results:
{
  "Contents": " 500:\t * @req:\tasynchronous compress request\n 501:\t * @dst:\tpointer to output buffer scatterlist\n 502:\t * @dlen:\tsize of the output buffer\n 503:\t */\n 504:\tstatic inline void acomp_request_set_dst_sg(struct acomp_req *req,\n 505:\t\t\t\t\t\t    struct scatterlist *dst,\n 506:\t\t\t\t\t\t    unsigned int dlen)\n 507:\t{\n 508:\t\treq-\u003edst = dst;\n 509:\t\treq-\u003edlen = dlen;\n 510:\t\n 511:\t\treq-\u003ebase.flags \u0026= ~CRYPTO_ACOMP_REQ_DST_NONDMA;\n 512:\t\treq-\u003ebase.flags \u0026= ~CRYPTO_ACOMP_REQ_DST_VIRT;\n 513:\t}\n 514:\t\n 515:\t/**\n 516:\t * acomp_request_set_dst_dma() -- Sets DMA destination virtual address\n 517:\t *\n 518:\t * Sets destination virtual address required by an acomp operation.\n 519:\t * The address must be usable for DMA.\n 520:\t *\n 521:\t * @req:\tasynchronous compress request\n 522:\t * @dst:\tvirtual address pointer to output buffer\n 523:\t * @dlen:\tsize of the output buffer\n 524:\t */\n 525:\tstatic inline void acomp_request_set_dst_dma(struct acomp_req *req,\n 526:\t\t\t\t\t\t     u8 *dst, unsigned int dlen)\n 527:\t{\n 528:\t\treq-\u003edvirt = dst;\n 529:\t\treq-\u003edlen = dlen;\n 530:\t\n 531:\t\treq-\u003ebase.flags \u0026= ~CRYPTO_ACOMP_REQ_DST_NONDMA;\n 532:\t\treq-\u003ebase.flags |= CRYPTO_ACOMP_REQ_DST_VIRT;\n 533:\t}\n 534:\t\n 535:\t/**\n 536:\t * acomp_request_set_dst_nondma() -- Sets non-DMA destination virtual address\n 537:\t *\n 538:\t * Sets destination virtual address required by an acomp operation.\n 539:\t * The address can not be used for DMA.\n"
}

25/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28200
							output: 36
							thoughts: 25
						

Thoughts:
Okay, here's my take on those lines of code, presented as a first-person internal monologue, assuming I'm an expert in this field:

**Digging into `acompress.h`**

Alright, let's circle back to that specific section within `include/crypto/acompress.h`, specifically lines 480 to 500. I remember that part. It’s likely dealing with the core data structures or function prototypes related to adaptive compression. I want to zero in on how it handles memory allocation, error management, and any potentially subtle threading considerations, assuming this is a multithreaded environment. I'm hoping to understand how that particular implementation interacts with the overall crypto library, especially if there's any cleverness in handling memory alignment or buffer sizing. I'm also watching out for any potential vulnerabilities, especially buffer overflows or integer overflows, considering this is a compression algorithm which can be tricky. I'll be looking to see if there are proper checks. I'll start by making sure any function declarations make sense and the variable types are what I expect. I'll be looking for anything I might have missed in my previous pass, any small details that could point towards a performance optimization or a security vulnerability.



26/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "include/crypto/acompress.h",
  "FirstLine": 480,
  "LineCount": 25
}

Results:
{
  "Contents": " 480:\t *\n 481:\t * @req:\tasynchronous compress request\n 482:\t * @folio:\tpointer to input folio\n 483:\t * @off:\tinput folio offset\n 484:\t * @len:\tsize of the input buffer\n 485:\t */\n 486:\tstatic inline void acomp_request_set_src_folio(struct acomp_req *req,\n 487:\t\t\t\t\t\t       struct folio *folio, size_t off,\n 488:\t\t\t\t\t\t       unsigned int len)\n 489:\t{\n 490:\t\tsg_init_table(\u0026req-\u003echain.ssg, 1);\n 491:\t\tsg_set_folio(\u0026req-\u003echain.ssg, folio, len, off);\n 492:\t\tacomp_request_set_src_sg(req, \u0026req-\u003echain.ssg, len);\n 493:\t}\n 494:\t\n 495:\t/**\n 496:\t * acomp_request_set_dst_sg() -- Sets destination scatterlist\n 497:\t *\n 498:\t * Sets destination scatterlist required by an acomp operation.\n 499:\t *\n 500:\t * @req:\tasynchronous compress request\n 501:\t * @dst:\tpointer to output buffer scatterlist\n 502:\t * @dlen:\tsize of the output buffer\n 503:\t */\n 504:\tstatic inline void acomp_request_set_dst_sg(struct acomp_req *req,\n"
}

27/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28933
							output: 34
							thoughts: 851
						

Thoughts:
**Deconstructing Compression Request Handling**

Okay, let's see what's happening under the hood when a compression request comes in. First, I'm looking at `crypto_acomp_compress` in `crypto/acompress.c`. It seems to have a few main paths based on a series of checks.

The first `if` statement is pretty straightforward: if the request is single-segment, the unit size matches the source length, and a few other conditions are met (not segmented, not async), just call the `compress` function of the transform (`tfm`) directly. Then, set the destination length and return.

If it's async and on the stack, it returns `-EAGAIN`. Otherwise, if the `unit_size` is present, and it's a single segment request, another `if` checks if it’s segmented. If not, I call `acomp_do_req_chain` which seems interesting... otherwise the transform is applied. Finally, if the transform is virtual or it's a single segment request I can call the transform, otherwise I call that `acomp_do_req_chain`.

I need to dive into `acomp_do_req_chain` to understand the flow for chain processing. Looks like this function checks for an invalid `unit_size` situation and then calls `acomp_save_req`. After this it calls `acomp_do_one_req`, and finally `acomp_reqchain_finish`.

Right, `acomp_reqchain_finish` calls `acomp_reqchain_virt` and then `acomp_restore_req`. After that, if the `unit_size` is not zero, the destination length is set. This makes me curious about `acomp_save_req`.

Looking at `acomp_save_req`, I see that the original completion function and associated data are stored, and then the request's completion function is replaced by `acomp_reqchain_done` and data is set to be the request itself. Most importantly, `unit_size` is set to zero!

Then `acomp_restore_req` is the inverse of `acomp_save_req` as expected. It restores the original completion function, associated data, and `unit_size` from the chain-specific storage. Aha! Notice how it restores `req->chain.unit_size` *before* the check that uses `req->unit_size` to set the destination length. That's good.

To finish this, I need to know what `acomp_reqchain_done` does, which is called after `acomp_do_one_req` from `acomp_save_req`. It is probably where the real work happens.



28/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "crypto/acompress.c",
  "FirstLine": 220,
  "LineCount": 40
}

Results:
{
  "Contents": " 220:\t\n 221:\tstatic int acomp_do_nondma(struct acomp_req *req, bool comp)\n 222:\t{\n 223:\t\tACOMP_FBREQ_ON_STACK(fbreq, req);\n 224:\t\tint err;\n 225:\t\n 226:\t\tif (comp)\n 227:\t\t\terr = crypto_acomp_compress(fbreq);\n 228:\t\telse\n 229:\t\t\terr = crypto_acomp_decompress(fbreq);\n 230:\t\n 231:\t\treq-\u003edlen = fbreq-\u003edlen;\n 232:\t\treturn err;\n 233:\t}\n 234:\t\n 235:\tstatic int acomp_do_one_req(struct acomp_req *req, bool comp)\n 236:\t{\n 237:\t\tif (acomp_request_isnondma(req))\n 238:\t\t\treturn acomp_do_nondma(req, comp);\n 239:\t\n 240:\t\tacomp_virt_to_sg(req);\n 241:\t\treturn comp ? crypto_acomp_reqtfm(req)-\u003ecompress(req) :\n 242:\t\t\t      crypto_acomp_reqtfm(req)-\u003edecompress(req);\n 243:\t}\n 244:\t\n 245:\tstatic int acomp_reqchain_finish(struct acomp_req *req, int err)\n 246:\t{\n 247:\t\tacomp_reqchain_virt(req);\n 248:\t\tacomp_restore_req(req);\n 249:\t\n 250:\t\tif (req-\u003eunit_size)\n 251:\t\t\treq-\u003edst-\u003elength = unlikely(err) ? err : req-\u003edlen;\n 252:\t\n 253:\t\treturn err;\n 254:\t}\n 255:\t\n 256:\tstatic void acomp_reqchain_done(void *data, int err)\n 257:\t{\n 258:\t\tstruct acomp_req *req = data;\n 259:\t\tcrypto_completion_t compl;\n"
}

29/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30997
							output: 34
							thoughts: 33
						

Thoughts:
Okay, let's dive into this. I need to get a handle on the `acomp_reqchain_done` function, and the context around it. Specifically, I should look at `crypto/acompress.c`, lines 255 through 270. This section is key because it likely handles the completion of a request chain related to compression. I need to understand how the system manages the asynchronous completion of these requests. This means I'll be looking for things like: how the completion is signaled, any error handling related to these compressed data transfers, and what data structures are being used to track the progress and eventual finalization of a compression operation chain. I'm going to pay close attention to the data flow; seeing how the data moves, how the state is managed, and how the completion is communicated to the caller. Understanding this will be crucial for any performance analysis or optimization efforts within this compression subsystem, and its interaction with other parts of the cryptographic framework. I expect to see some interesting use of locks or atomic operations to ensure thread safety during the process. I'm going in with a clear, focused intention.



30/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "crypto/acompress.c",
  "FirstLine": 255,
  "LineCount": 20
}

Results:
{
  "Contents": " 255:\t\n 256:\tstatic void acomp_reqchain_done(void *data, int err)\n 257:\t{\n 258:\t\tstruct acomp_req *req = data;\n 259:\t\tcrypto_completion_t compl;\n 260:\t\n 261:\t\tcompl = req-\u003echain.compl;\n 262:\t\tdata = req-\u003echain.data;\n 263:\t\n 264:\t\tif (err == -EINPROGRESS)\n 265:\t\t\tgoto notify;\n 266:\t\n 267:\t\terr = acomp_reqchain_finish(req, err);\n 268:\t\n 269:\tnotify:\n 270:\t\tcompl(data, err);\n 271:\t}\n 272:\t\n 273:\tstatic int acomp_do_req_chain(struct acomp_req *req, bool comp)\n 274:\t{\n"
}

31/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 31561
							output: 35
							thoughts: 662
						

Thoughts:
**Analyzing Zswap Compression and Decompression**

Okay, let's dive into this `acomp_ctx_free` and `zswap_cpu_comp_prepare` code, specifically focusing on how data flows during compression and decompression in `zswap`. I need to ensure these memory buffers are being managed correctly.

First, within `zswap_cpu_comp_prepare`, I see the batch size is set, and a request is initialized. Good. Then, a crucial allocation: `acomp_ctx->comp.buffers = kcalloc_node(ZSWAP_MAX_BATCH_SIZE, sizeof(u8 *), GFP_KERNEL, nid)`. This allocates an array of pointers to `u8` buffers. Then, within that, a loop is allocating individual `PAGE_SIZE` buffers using `kmalloc_node`. Aha! So, these are the destination buffers where the compressed data will live. Excellent, but... wait, are these `buffers` actually initialized with data at this point? This is key.

Now, fast forward to `zswap_update_entries`. This is where the compressed data is actually handled. I see that `entry->length` gets set to `dlen` and `entry->handle` is set. Then there's an `if` statement. If `dlen` is equal to `PAGE_SIZE`, something different happens than if it is not. If `dlen` is not equal to `PAGE_SIZE`, the code retrieves the `buf` from `zc->comp_ctx->buffers[batch_iter]`. Then, `zs_obj_write` is called with the buffer, handle, and `dlen`. Wait a second! I think I'm seeing it now! `zs_obj_write` copies `dlen` bytes from the `buf` (which is `zc->comp_ctx->buffers[batch_iter]`) to `handle` within the zsmalloc pool!

Did I get this right? The compression algorithm is writing `dlen` bytes to `buf`, and then `zs_obj_write` is using this compressed data and writing it to handle! Yes, because `dlen` is the number of compressed bytes! Perfect!

Okay, so the compressed data is written to these allocated buffers in `zswap_cpu_comp_prepare`, and then this is later picked up by the `zs_obj_write` function in `zswap_update_entries` and written to the zsmalloc pool for storage.

Now, let's trace this back when decompression happens... I need to understand how the data gets read back. Let's start with how `__zswap_decompress` works, and I'll jump into the source code in `mm/zswap.c`.



32/3 2026/11/09 05:49 tool grepper
0m Args:
{
  "Expression": "static bool __zswap_decompress",
  "PathPrefixes": [
    "mm/zswap.c"
  ]
}

Results:
{
  "Output": "mm/zswap.c=1087=static bool zswap_compress(struct zswap_store_context *zc)\n--\nmm/zswap.c-1182-\nmm/zswap.c:1183:static bool __zswap_decompress(struct zswap_entry *entry,\nmm/zswap.c-1184-\t\t\t       struct zswap_pool *pool, struct acomp_req *req,\n"
}

33/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32966
							output: 35
							thoughts: 12
						

Thoughts:
Okay, let's dive into this. Here's my take on understanding this `__zswap_decompress` function:

**Analyzing the Decompression Process in zswap**

Alright, I'm going to dig into the `__zswap_decompress` function. My initial thought is to understand its role in zswap. I know zswap is a kernel feature to improve system responsiveness by using a compressed swap cache. Therefore, this function is central to that process. It's the point where compressed data that has been stored in swap is brought back into memory. I need to figure out the exact steps of this decompression: what algorithms are being used? Are there any optimizations, especially given the kernel's real-time constraints? How is it integrated within the larger zswap framework? I'll be looking for aspects such as the source of the compressed data (from the swap device), any error handling in case the decompression fails or the data is corrupted, and how it deals with page allocation and management after the data is decompressed. I'll need to identify the data structures it uses, how it interacts with the VM, and the mechanisms it uses to prevent resource exhaustion. And, of course, I'm curious about the specific compression algorithm used here. Is it LZ4, or something else, and how does the choice affect performance? Time to analyze the code and the surrounding context!



34/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "mm/zswap.c",
  "FirstLine": 1183,
  "LineCount": 45
}

Results:
{
  "Contents": "1183:\tstatic bool __zswap_decompress(struct zswap_entry *entry,\n1184:\t\t\t\t       struct zswap_pool *pool, struct acomp_req *req,\n1185:\t\t\t\t       struct crypto_wait *wait, struct folio *folio)\n1186:\t{\n1187:\t\tstruct scatterlist input[2]; /* zsmalloc returns an SG list 1-2 entries */\n1188:\t\tstruct scatterlist output;\n1189:\t\tint ret = 0, dlen;\n1190:\t\n1191:\t\tzs_obj_read_sg_begin(pool-\u003ezs_pool, entry-\u003ehandle, input, entry-\u003elength);\n1192:\t\n1193:\t\t/* zswap entries of length PAGE_SIZE are not compressed. */\n1194:\t\tif (entry-\u003elength == PAGE_SIZE) {\n1195:\t\t\tvoid *dst;\n1196:\t\n1197:\t\t\tWARN_ON_ONCE(input-\u003elength != PAGE_SIZE);\n1198:\t\n1199:\t\t\tdst = kmap_local_folio(folio, 0);\n1200:\t\t\tmemcpy_from_sglist(dst, input, 0, PAGE_SIZE);\n1201:\t\t\tdlen = PAGE_SIZE;\n1202:\t\t\tkunmap_local(dst);\n1203:\t\t\tflush_dcache_folio(folio);\n1204:\t\t} else {\n1205:\t\t\tsg_init_table(\u0026output, 1);\n1206:\t\t\tsg_set_folio(\u0026output, folio, PAGE_SIZE, 0);\n1207:\t\t\tacomp_request_set_params(req, input, \u0026output, entry-\u003elength, PAGE_SIZE);\n1208:\t\t\tret = crypto_acomp_decompress(req);\n1209:\t\t\tret = crypto_wait_req(ret, wait);\n1210:\t\t\tdlen = req-\u003edlen;\n1211:\t\t}\n1212:\t\n1213:\t\tzs_obj_read_sg_end(pool-\u003ezs_pool, entry-\u003ehandle);\n1214:\t\n1215:\t\tif (!ret \u0026\u0026 dlen == PAGE_SIZE)\n1216:\t\t\treturn true;\n1217:\t\n1218:\t\tzswap_decompress_fail++;\n1219:\t\tpr_alert_ratelimited(\"Decompression error from zswap (%d:%lu %s %u-\u003e%d)\\n\",\n1220:\t\t\t\t\t\t\tswp_type(entry-\u003eswpentry),\n1221:\t\t\t\t\t\t\tswp_offset(entry-\u003eswpentry),\n1222:\t\t\t\t\t\t\tpool-\u003etfm_name,\n1223:\t\t\t\t\t\t\tentry-\u003elength, dlen);\n1224:\t\treturn false;\n1225:\t}\n1226:\t\n1227:\tstatic bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)\n"
}

35/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 34058
							output: 35
							thoughts: 100
						

Thoughts:
Here's my summary, as though I'm thinking through this process:

**Diving into Zswap Decompression and Compression**

Okay, hold on a second. I need to take a closer look at `__zswap_decompress`. Let's break this down. It seems to be pulling `entry->length` bytes directly from the zsmalloc pool and then decompressing those bytes into a `folio`. That's the basic workflow, but I need to understand the data movement more deeply.

Now, let's switch gears and go over to the compression side, specifically, `zswap_compress`. I want to zero in on how `dst_sg` is being utilized here. The setup and usage of this variable are key to understanding the overall performance characteristics.

Right, I'm going to pull up lines 1115 to 1175 in `mm/zswap.c`. That's where the heart of the matter lies. Time to dig in and clarify the nuances of the memory handling and data flow.



36/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "mm/zswap.c",
  "FirstLine": 1115,
  "LineCount": 60
}

Results:
{
  "Contents": "1115:\t\t\t * crypto_acomp via acomp_request_set_src_folio().\n1116:\t\t\t */\n1117:\t\t\tacomp_request_set_src_folio(zc-\u003ecomp_ctx-\u003eareq.req, zc-\u003efolio,\n1118:\t\t\t\t\t(zc-\u003efolio_start + batch_start) * PAGE_SIZE,\n1119:\t\t\t\t\tzc-\u003eslen);\n1120:\t\n1121:\t\t\tdst_sg = zc-\u003ecomp_ctx-\u003esg_table-\u003esgl + batch_start;\n1122:\t\t\tdst_sg-\u003elength = zc-\u003eslen;\n1123:\t\n1124:\t\t\t/*\n1125:\t\t\t * The per-CPU @zc-\u003ecomp_ctx-\u003esg_table scatterlists are statically\n1126:\t\t\t * mapped to the per-CPU dst @buffers at pool creation time.\n1127:\t\t\t * There are ZSWAP_MAX_BATCH_SIZE per-CPU SG lists and dst\n1128:\t\t\t * buffers allocated for all compressors.\n1129:\t\t\t */\n1130:\t\t\tacomp_request_set_dst_sg(zc-\u003ecomp_ctx-\u003eareq.req,\n1131:\t\t\t\t\t\t dst_sg, zc-\u003eslen);\n1132:\t\n1133:\t\t\terr = crypto_wait_req(crypto_acomp_compress(zc-\u003ecomp_ctx-\u003eareq.req),\n1134:\t\t\t\t\t      \u0026zc-\u003ecomp_ctx-\u003eareq.wait);\n1135:\t\n1136:\t\t\t/*\n1137:\t\t\t * If a page cannot be compressed into a size smaller than\n1138:\t\t\t * PAGE_SIZE, save the content as is without a compression, to\n1139:\t\t\t * keep the LRU order of writebacks.  If writeback is disabled,\n1140:\t\t\t * reject the page since it only adds metadata overhead.\n1141:\t\t\t * swap_writeout() will put the page back to the active LRU list\n1142:\t\t\t * in the case.\n1143:\t\t\t */\n1144:\t\t\tif (err \u0026\u0026 !zc-\u003ewb_enabled)\n1145:\t\t\t\tgoto compress_error;\n1146:\t\n1147:\t\t\t/*\n1148:\t\t\t * For each output SG list in @acomp_ctx-\u003ereq-\u003esg_table-\u003esgl,\n1149:\t\t\t * the @sg-\u003elength should be set to either the page's compressed\n1150:\t\t\t * length (success), or it's negative value compression error.\n1151:\t\t\t */\n1152:\t\t\tfor_each_sg(dst_sg, sg, zc-\u003enr_batch_pages, comp_batch_iter) {\n1153:\t\n1154:\t\t\t\tif (sg-\u003elength \u003c= 0 || sg-\u003elength \u003e= PAGE_SIZE) {\n1155:\t\t\t\t\tif (!zc-\u003ewb_enabled) {\n1156:\t\t\t\t\t\tif (sg-\u003elength == PAGE_SIZE)\n1157:\t\t\t\t\t\t\tsg-\u003elength = -ENOSPC;\n1158:\t\t\t\t\t\telse if (WARN_ON_ONCE(!sg-\u003elength || sg-\u003elength \u003e PAGE_SIZE))\n1159:\t\t\t\t\t\t\tsg-\u003elength = -EINVAL;\n1160:\t\t\t\t\t\tgoto compress_error;\n1161:\t\t\t\t\t}\n1162:\t\n1163:\t\t\t\t\tsg-\u003elength = PAGE_SIZE;\n1164:\t\t\t\t}\n1165:\t\t\t}\n1166:\t\t}\n1167:\t\n1168:\t\treturn zswap_update_entries(zc);\n1169:\t\n1170:\tcompress_error:\n1171:\t\tfor_each_sg(dst_sg, sg, zc-\u003enr_batch_pages, comp_batch_iter) {\n1172:\t\t\tif ((int)sg-\u003elength \u003c 0) {\n1173:\t\t\t\tif ((int)sg-\u003elength == -ENOSPC)\n1174:\t\t\t\t\tzswap_reject_compress_poor++;\n"
}

37/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 35428
							output: 33
							thoughts: 693
						

Thoughts:
**Analyzing Scatterlist Lengths in Compression**

Okay, hold on a second! I need to take a closer look at lines 1121-1122. Specifically:

dst_sg = zc->comp_ctx->sg_table->sgl + batch_start;
dst_sg->length = zc->slen;

Right, `zc->comp_ctx->sg_table->sgl` is clearly an array of `struct scatterlist`! When `batch_start` increments, that `dst_sg` pointer effectively steps through the `sgl` array. Okay, I'm with it so far. Let me make sure I'm following this.

Now, `dst_sg->length = zc->slen`. But... what was that `sgl[0]->length` initialized to in `zswap_cpu_comp_prepare`? Oh, yes! `sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE);`. So, it was set to `PAGE_SIZE` (4096 bytes) initially.

Now, if `zc->nr_batch_pages > 1`, `zc->slen = zc->nr_batch_pages * PAGE_SIZE`. So `dst_sg->length = zc->slen` then sets `dst_sg[0].length = zc->slen`, which could be 8192 or 16384 bytes, for example. The crucial thing is what happens to `dst_sg[1].length`? It remains `PAGE_SIZE`! Not good!

Now, the next line is `acomp_request_set_dst_sg(req, dst_sg, zc->slen);`. Alright, so the destination scatterlist is set up. And then `crypto_acomp_compress(req)` is called. Let's dig into that...

Okay, focus! When `req->unit_size == req->slen`, it looks like a single page is compressed. Specifically, if the condition is met, then `int err = tfm->compress(req);` is called and then `req->dst->length = unlikely(err) ? err : req->dlen;`. So, `req->dst->length` (which, again, is `dst_sg[0].length` in our case) is either set to `err` (if it's not zero), or to `req->dlen` which is the compressed length. But what if `err` isn't zero? Then `req->dst->length` becomes `err`, which is a negative integer! That could be a problem!

Okay, time to check what the type of `length` in `struct scatterlist` actually is. Time to check its definition!


38/3 2026/11/09 05:49 tool grepper
0m Args:
{
  "Expression": "struct scatterlist \\{",
  "PathPrefixes": [
    "include/linux/scatterlist.h"
  ]
}

Results:
{
  "Output": "include/linux/scatterlist.h-10-\ninclude/linux/scatterlist.h:11:struct scatterlist {\ninclude/linux/scatterlist.h-12-\tunsigned long\tpage_link;\n"
}

39/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 36829
							output: 35
							thoughts: 14
						

Thoughts:
Okay, here's my interpretation of that prompt, and my response:

**Examining the Scatter/Gather List Implementation**

Alright, let's dive in. The instruction is clear: I need to get familiar with the scatter/gather list implementation in the Linux kernel, specifically by reviewing `include/linux/scatterlist.h`. My immediate thought is, "Right, good place to start. That's where the core data structures and likely the foundational definitions reside." I'm expecting to see the `sg_table` struct, the `sg_entry`, and any relevant macros or inline functions for manipulating these structures. This is the bedrock of DMA operations, so understanding how data is described as non-contiguous blocks is crucial for my work. I'll be looking for things like how physical addresses are represented, how length information is stored, and what considerations are given for platform-specific details. Hopefully, it's well-commented. Let's get to it!



40/3 2026/11/09 05:49 tool read-file
0m Args:
{
  "File": "include/linux/scatterlist.h",
  "FirstLine": 11,
  "LineCount": 20
}

Results:
{
  "Contents": "  11:\tstruct scatterlist {\n  12:\t\tunsigned long\tpage_link;\n  13:\t\tunsigned int\toffset;\n  14:\t\tunsigned int\tlength;\n  15:\t\tdma_addr_t\tdma_address;\n  16:\t#ifdef CONFIG_NEED_SG_DMA_LENGTH\n  17:\t\tunsigned int\tdma_length;\n  18:\t#endif\n  19:\t#ifdef CONFIG_NEED_SG_DMA_FLAGS\n  20:\t\tunsigned int    dma_flags;\n  21:\t#endif\n  22:\t};\n  23:\t\n  24:\t/*\n  25:\t * These macros should be used after a dma_map_sg call has been done\n  26:\t * to get bus addresses of each of the SG entries and their lengths.\n  27:\t * You should only work with the number of sg entries dma_map_sg\n  28:\t * returns, or alternatively stop on the first sg_dma_len(sg) which\n  29:\t * is 0.\n  30:\t */\n"
}

41/3 2026/11/09 05:49 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 37367
							output: 19
							thoughts: 1352
						

Thoughts:
**Analyzing the Data Flow and Potential Integer Overflow in Zswap Compression**

Okay, let's break this down. My initial thought process is centered around how `length` is being used within the `scatterlist` structure, and I'm immediately concerned about potential integer overflow issues, especially considering that `unsigned int length;` might not adequately represent all possible values in the context of compressed data.

I see that `dst_sg->length` (which is part of the scatterlist) is assigned either an error code (`(unsigned int)err`) or the compressed data length (`dlen`). The size of an `unsigned int` is going to be platform specific, but large enough that negative values are impossible, however, I'm thinking I should keep a keen eye out for negative values because an integer underflow could cause the `unsigned int` to have a large positive value.

Now, in `zswap_compress`, I spot this check: `if (sg->length <= 0 || sg->length >= PAGE_SIZE)`. My immediate reaction is, "Wait a minute! Is that `sg->length <= 0` intended, or should it be casting to `(int)sg->length < 0` to properly handle potential negative values?" This is a key area where an integer overflow could lead to incorrect behavior. A small `dlen` value might not pass this check, but an integer underflow could make it a large positive value, thus allowing a non-compressed buffer to pass the check.

Then I move to `zswap_update_entries`. This is where the compressed data is actually handled. I see a loop that iterates through the scatterlist entries.  `dlen = sg->length;` So `sg->length` is read. `sg->length` is set to the compressed `dlen`. Then the code allocates memory using `zs_malloc`, sets the `entry->length` to `dlen`, and writes to the allocated handle using `zs_obj_write`. If `dlen` is equal to `PAGE_SIZE`, the code writes directly to the page. Otherwise, the data is written to a buffer. At this stage I'm still feeling okay, the logic seems correct.

Returning to the `zswap_compress` loop, I'm seeing an even deeper dive into the batch processing. Each iteration sets `dst_sg->length` to `zc->slen`. But I'm also seeing `acomp_request_set_dst_sg` being called with `dst_sg` (which is a pointer to one of the scatterlist entries) and `zc->slen`.  `zc->slen` is the size of the compressed data.

Aha! `zswap_update_entries` uses `for_each_sg` on the `zc->comp_ctx->sg_table->sgl` scatterlist. This suggests that the same scatterlist is used in both compression and updating entries.

Let's do some math: Suppose `zc->nr_pages = 4` and `zc->nr_batch_pages = 1`, and therefore the batch starts are [0, 1, 2, 3]. In each iteration of the loop in `zswap_compress`, the `dst_sg->length` is set to `PAGE_SIZE`, *before* the compression happens. So initially, the `sgl` has `PAGE_SIZE` entries.  After compression, the length of each scatter list element (`sg->length`) will be set to the *compressed* size, `dlen`.

So, for each page, the corresponding `sg->length` in `sgl` is overwritten with the compressed data length, `dlen`.

Then, `zswap_update_entries` reads each `sgl[i]->length` value.

Okay, so where is `sgl[i]` defined? Back to `zswap_cpu_comp_prepare`, I see that `sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE)` is called! This means that each `sgl` entry initially points to a buffer in `acomp_ctx->comp.buffers`. So each `sgl` originally has a pointer to a preallocated buffer and the size of `PAGE_SIZE`.  Then, the compression happens, the scatter list entry's length is changed to `dlen`, but does the buffer that scatter list entry points to get written to? I see that `acomp_request_set_dst_sg` is called. So what exactly does this do? I need to check how the *actual* acomp implementations use `req->dst` to understand what's actually being written! I need to search for acomp implementations, like `scomp_acomp_comp`, to understand how


42/3 2026/11/09 05:50 tool grepper
0m Args:
{
  "Expression": "scomp_acomp"
}

Results:
{
  "Output": "crypto/scompress.c=160=static inline void scomp_unlock_scratch(struct scomp_scratch *scratch)\n--\ncrypto/scompress.c-165-\ncrypto/scompress.c:166:static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)\ncrypto/scompress.c-167-{\n--\ncrypto/scompress.c-275-\ncrypto/scompress.c:276:static int scomp_acomp_compress(struct acomp_req *req)\ncrypto/scompress.c-277-{\ncrypto/scompress.c:278:\treturn scomp_acomp_comp_decomp(req, 1);\ncrypto/scompress.c-279-}\ncrypto/scompress.c-280-\ncrypto/scompress.c:281:static int scomp_acomp_decompress(struct acomp_req *req)\ncrypto/scompress.c-282-{\ncrypto/scompress.c:283:\treturn scomp_acomp_comp_decomp(req, 0);\ncrypto/scompress.c-284-}\n--\ncrypto/scompress.c=299=int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)\n--\ncrypto/scompress.c-317-\ncrypto/scompress.c:318:\tcrt-\u003ecompress = scomp_acomp_compress;\ncrypto/scompress.c:319:\tcrt-\u003edecompress = scomp_acomp_decompress;\ncrypto/scompress.c-320-\n"
}

43/3 2026/11/09 05:50 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 40172
							output: 34
							thoughts: 19
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines of code, presented as if they were my own internal thought process:

**Diving into `crypto/scompress.c` around Line 166**

Alright, I'm heading over to `crypto/scompress.c` and specifically focusing on that section around line 166. I know this code pretty well, so I'm expecting to see something related to the core compression or decompression logic, most likely the data processing loop. I’m thinking we're likely going to find the heart of the algorithm at that point. I'm mentally preparing to analyze how it handles input buffers, output buffers, and any potential state management involved. I'm anticipating some bit-level manipulation as well. I'm ready to get down to the nitty-gritty of the data transformations. I'm focusing on identifying any potential vulnerabilities or efficiency bottlenecks. Let's see what we've got!



44/3 2026/11/09 05:50 tool read-file
0m Args:
{
  "File": "crypto/scompress.c",
  "FirstLine": 166,
  "LineCount": 70
}

Results:
{
  "Contents": " 166:\tstatic int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)\n 167:\t{\n 168:\t\tstruct crypto_acomp *tfm = crypto_acomp_reqtfm(req);\n 169:\t\tstruct crypto_scomp **tfm_ctx = acomp_tfm_ctx(tfm);\n 170:\t\tbool src_isvirt = acomp_request_src_isvirt(req);\n 171:\t\tbool dst_isvirt = acomp_request_dst_isvirt(req);\n 172:\t\tstruct crypto_scomp *scomp = *tfm_ctx;\n 173:\t\tunsigned int slen = req-\u003eslen;\n 174:\t\tunsigned int dlen = req-\u003edlen;\n 175:\t\tstruct page *spage, *dpage;\n 176:\t\tunsigned int n;\n 177:\t\tconst u8 *src;\n 178:\t\tsize_t soff;\n 179:\t\tsize_t doff;\n 180:\t\tu8 *dst;\n 181:\t\tint ret;\n 182:\t\n 183:\t\tif (!req-\u003esrc || !slen)\n 184:\t\t\treturn -EINVAL;\n 185:\t\n 186:\t\tif (!req-\u003edst || !dlen)\n 187:\t\t\treturn -EINVAL;\n 188:\t\n 189:\t\tif (dst_isvirt)\n 190:\t\t\tdst = req-\u003edvirt;\n 191:\t\telse {\n 192:\t\t\tif (dlen \u003c= req-\u003edst-\u003elength) {\n 193:\t\t\t\tdpage = sg_page(req-\u003edst);\n 194:\t\t\t\tdoff = req-\u003edst-\u003eoffset;\n 195:\t\t\t} else\n 196:\t\t\t\treturn -ENOSYS;\n 197:\t\n 198:\t\t\tdpage += doff / PAGE_SIZE;\n 199:\t\t\tdoff = offset_in_page(doff);\n 200:\t\n 201:\t\t\tn = (dlen - 1) / PAGE_SIZE;\n 202:\t\t\tn += (offset_in_page(dlen - 1) + doff) / PAGE_SIZE;\n 203:\t\t\tif (PageHighMem(dpage + n) \u0026\u0026\n 204:\t\t\t    size_add(doff, dlen) \u003e PAGE_SIZE)\n 205:\t\t\t\treturn -ENOSYS;\n 206:\t\t\tdst = kmap_local_page(dpage) + doff;\n 207:\t\t}\n 208:\t\n 209:\t\tif (src_isvirt)\n 210:\t\t\tsrc = req-\u003esvirt;\n 211:\t\telse {\n 212:\t\t\tsrc = NULL;\n 213:\t\t\tdo {\n 214:\t\t\t\tif (slen \u003c= req-\u003esrc-\u003elength) {\n 215:\t\t\t\t\tspage = sg_page(req-\u003esrc);\n 216:\t\t\t\t\tsoff = req-\u003esrc-\u003eoffset;\n 217:\t\t\t\t} else\n 218:\t\t\t\t\tbreak;\n 219:\t\n 220:\t\t\t\tspage = spage + soff / PAGE_SIZE;\n 221:\t\t\t\tsoff = offset_in_page(soff);\n 222:\t\n 223:\t\t\t\tn = (slen - 1) / PAGE_SIZE;\n 224:\t\t\t\tn += (offset_in_page(slen - 1) + soff) / PAGE_SIZE;\n 225:\t\t\t\tif (PageHighMem(spage + n) \u0026\u0026\n 226:\t\t\t\t    size_add(soff, slen) \u003e PAGE_SIZE)\n 227:\t\t\t\t\tbreak;\n 228:\t\t\t\tsrc = kmap_local_page(spage) + soff;\n 229:\t\t\t} while (0);\n 230:\t\t}\n 231:\t\n 232:\t\tstruct crypto_acomp_stream *stream = crypto_acomp_lock_stream_bh(\u0026crypto_scomp_alg(scomp)-\u003estreams);\n 233:\t\n 234:\t\tif (!src_isvirt \u0026\u0026 !src) {\n 235:\t\t\tstruct scomp_scratch *scratch = scomp_lock_scratch();\n"
}

45/3 2026/11/09 05:50 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 41536
							output: 34
							thoughts: 27
						

Thoughts:
Okay, here's my summary, presented as a stream of thought:

**Diving into `scompress.c` - Lines 235-275**

Alright, let's get down to brass tacks. I need to understand what's unfolding in the `crypto/scompress.c` file, specifically from lines 235 through 275. I'm focusing in on this particular section because it's probably handling some crucial aspect of the compression or decompression routine, judging by its location within the broader crypto context. I'm expecting to see calls to some low-level compression/decompression primitives at this point. I'll be keeping an eye out for potential optimizations, error handling strategies, and any interesting algorithmic choices being made. I wonder if there are any obvious vulnerabilities, or if it seems well-defended. My expertise tells me to pay close attention to any bitwise operations or memory management, as those are often the hotspots for subtle but impactful bugs in compression algorithms. Time to dig in and see what treasures, or potential pitfalls, await!



46/3 2026/11/09 05:50 tool read-file
0m Args:
{
  "File": "crypto/scompress.c",
  "FirstLine": 235,
  "LineCount": 40
}

Results:
{
  "Contents": " 235:\t\t\tstruct scomp_scratch *scratch = scomp_lock_scratch();\n 236:\t\t\tconst u8 *src = scratch-\u003esrc;\n 237:\t\n 238:\t\t\tmemcpy_from_sglist(scratch-\u003esrc, req-\u003esrc, 0, slen);\n 239:\t\n 240:\t\t\tif (dir)\n 241:\t\t\t\tret = crypto_scomp_compress(scomp, src, slen,\n 242:\t\t\t\t\t\t\t    dst, \u0026dlen, stream-\u003ectx);\n 243:\t\t\telse\n 244:\t\t\t\tret = crypto_scomp_decompress(scomp, src, slen,\n 245:\t\t\t\t\t\t\t      dst, \u0026dlen, stream-\u003ectx);\n 246:\t\n 247:\t\t\tscomp_unlock_scratch(scratch);\n 248:\t\t} else if (dir)\n 249:\t\t\tret = crypto_scomp_compress(scomp, src, slen,\n 250:\t\t\t\t\t\t    dst, \u0026dlen, stream-\u003ectx);\n 251:\t\telse\n 252:\t\t\tret = crypto_scomp_decompress(scomp, src, slen,\n 253:\t\t\t\t\t\t      dst, \u0026dlen, stream-\u003ectx);\n 254:\t\n 255:\t\tcrypto_acomp_unlock_stream_bh(stream);\n 256:\t\n 257:\t\treq-\u003edlen = dlen;\n 258:\t\n 259:\t\tif (!src_isvirt \u0026\u0026 src)\n 260:\t\t\tkunmap_local(src);\n 261:\t\tif (!dst_isvirt) {\n 262:\t\t\tkunmap_local(dst);\n 263:\t\t\tdlen += doff;\n 264:\t\t\tfor (;;) {\n 265:\t\t\t\tflush_dcache_page(dpage);\n 266:\t\t\t\tif (dlen \u003c= PAGE_SIZE)\n 267:\t\t\t\t\tbreak;\n 268:\t\t\t\tdlen -= PAGE_SIZE;\n 269:\t\t\t\tdpage++;\n 270:\t\t\t}\n 271:\t\t}\n 272:\t\n 273:\t\treturn ret;\n 274:\t}\n"
}

47/3 2026/11/09 05:50 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 42399
							output: 22
							thoughts: 322
						

Thoughts:
**My Analysis of Compression Data Flow**

Okay, let's break this down. From what I'm seeing, `scomp_acomp_compress` is responsible for writing compressed data to `dst`, which, if I understand correctly, is essentially a page mapped within the request's destination (`req->dst`). It then updates `req->dlen` with the compressed data length.

Then, within `crypto_acomp_compress`, there's this crucial check: If the unit size equals the source length, and the request uses scatter-gather, and we're *not* using a segmented request *and* the transform function isn't asynchronous, then the core compression happens directly via `tfm->compress(req)`. Crucially, `req->dst->length` is set to either the error code if an error happened, or, if not, to the compressed data length, `req->dlen`. This is where the output length is defined.

Next, looking at `zswap_update_entries`, it seems `dlen` is assigned the `sg->length`. Now, `zs_obj_write` is invoked, writing `dlen` bytes of the compressed data into the `handle`.

But here's where my alarm bells are going off. It all works fine so far, but what if we're dealing with hardware compression or, more specifically, a *batched* compression scenario? I'm thinking of something like the Intel IAA, or any other crypto hardware that supports batching (`batch_size > 1`) and segmented requests (`CRYPTO_ALG_REQ_SEG`). This could change the length of the data on the destination. How does the data length get updated correctly in this scenario? I need to verify how `CRYPTO_ALG_REQ_SEG` is handled within the kernel and how the compressed length is propagated. I need to track how the length of the compressed data might be modified in a batching scenario. Time to do a full search and see what the kernel tree reveals.



48/3 2026/11/09 05:50 tool grepper
0m Args:
{
  "Expression": "CRYPTO_ALG_REQ_SEG"
}

Results:
{
  "Output": "include/crypto/algapi.h=283=static inline bool crypto_tfm_req_seg(struct crypto_tfm *tfm)\ninclude/crypto/algapi.h-284-{\ninclude/crypto/algapi.h:285:\treturn tfm-\u003e__crt_alg-\u003ecra_flags \u0026 CRYPTO_ALG_REQ_SEG;\ninclude/crypto/algapi.h-286-}\n--\ninclude/linux/crypto.h-142-/* Set if the algorithm supports segmentation. */\ninclude/linux/crypto.h:143:#define CRYPTO_ALG_REQ_SEG\t\t0x00100000\ninclude/linux/crypto.h-144-\n"
}

49/3 2026/11/09 05:50 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43353
							output: 34
							thoughts: 3269
						

Thoughts:
**KMSAN and Uninitialized Memory in zswap and acomp Analysis**

Okay, let's break this down systematically. My primary concern is ensuring this patch doesn't introduce vulnerabilities related to uninitialized memory, particularly in light of KMSAN. The core of this seems to be related to changes in compression batching within the zswap subsystem, and the crypto acomp API.

First, I see the introduction of `CRYPTO_ALG_REQ_SEG` and related structures, but the critical point is where this might interact with user-space data. The initial checks rule out direct interactions like `copy_to_user`, `put_user`, netlink, etc., so that's good. The modifications primarily touch internal crypto handling, and zswap batching.

My immediate red flag is uninitialized memory getting stored in zswap. If we're batching and potentially compressing and decompressing pages, could uninitialized content creep in? The code flow is clear: a page, coming from either user space or the page cache, is stored. It might be compressed and the compressed data is written to a zsmalloc pool, or stored uncompressed. It is loaded back, either compressed or uncompressed. When a page is read back in to memory, `memcpy_from_sglist` copies the contents into the folio.

The `zswap_store_context` struct appears fully initialized, which is a key part of how the batching works. `zswap_store_context` and other related contexts are initialized, which helps to mitigate a lot of risks. The structures that are used, such as `zc.comp_ctx`, and `zc.nr_batch_pages` are initialized properly.

The real point of concern is the `zswap_entry` structure. This is internal to zswap, but it holds key metadata about the swapped page (length, handle, etc.). It is never copied to user space, but it's crucial that its fields are properly initialized. The checks within the code show that in `zswap_init_entries`, the fields like `handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, and `lru` are initialized!

The `zswap_update_entries` function is where we actually deal with compressed length and its potential initialization issues. Here, `zs_malloc` is called, potentially failing. If it fails, that particular entry is skipped (and its `handle` is invalidated). If allocation succeeds, `entry->length` and `entry->handle` are initialized. Importantly, the code *does* seem to handle the case of allocation failure gracefully, freeing any allocated memory, and returning an error, thus making sure nothing uninitialized leaks.

The key to the *length* field's safety is its bitfield. But, bitfields *can* have alignment issues. So, I checked how those bits interact with adjacent members, in particular the `referenced` bit and `pool_idx` member. `pool_idx` is a `u8` and therefore would be byte aligned, while length is a bitfield of 19 bits, and is not necessarily byte aligned. The `length` bitfield should not be read *before* it's written. Given the logic of the code, that seems to be the case.

Now, KMSAN is important in this discussion because of bitfields. I understand that KMSAN specifically tracks shadow memory at the byte level.  This is a critical point! It’s designed to handle bitfields without flagging false positives due to uninitialized sibling bits within a byte/word. The kernel code makes extensive use of bitfields, so this is a key design consideration. This means that KMSAN is explicitly designed to handle this, which is good.

The code clearly initializes the `entry->length` field before it's read. So, after it's been initialized via `entry->length = dlen;`, the KMSAN instrumentation *should* not report any uninitialized reads.

Also, it looks like `ZSWAP_MAX_COMPR_LENGTH_SHIFT` (which determines the length of the bitfield) is properly sized to ensure the `length` field can accommodate the page size.

Finally, looking briefly at `crypto/acompress.c`, the changes to `acomp_save_req` and `acomp_restore_req`, and the `acomp_reqchain_virt` look reasonable. I don't see any immediate uninitialized memory issues based on what I have reviewed. The save and restore mechanisms correctly handle `req->unit_size` to store and restore internal states.

In summary: I'm confident that the code handles memory initialization correctly, and that KMSAN and bitfield usage here should not introduce any new vulnerabilities related to uninitialized memory. The code has been written safely in that regard.


50/3 2026/11/09 05:51 tool read-file
0m Args:
{
  "File": "crypto/acompress.c",
  "FirstLine": 175,
  "LineCount": 45
}

Results:
{
  "Contents": " 175:\t\n 176:\tstatic void acomp_restore_req(struct acomp_req *req)\n 177:\t{\n 178:\t\treq-\u003ebase.complete = req-\u003echain.compl;\n 179:\t\treq-\u003ebase.data = req-\u003echain.data;\n 180:\t\treq-\u003eunit_size = req-\u003echain.unit_size;\n 181:\t}\n 182:\t\n 183:\tstatic void acomp_reqchain_virt(struct acomp_req *req)\n 184:\t{\n 185:\t\tstruct acomp_req_chain *state = \u0026req-\u003echain;\n 186:\t\tunsigned int slen = req-\u003eslen;\n 187:\t\tunsigned int dlen = req-\u003edlen;\n 188:\t\n 189:\t\tif (state-\u003eflags \u0026 CRYPTO_ACOMP_REQ_SRC_VIRT)\n 190:\t\t\tacomp_request_set_src_dma(req, state-\u003esrc, slen);\n 191:\t\tif (state-\u003eflags \u0026 CRYPTO_ACOMP_REQ_DST_VIRT)\n 192:\t\t\tacomp_request_set_dst_dma(req, state-\u003edst, dlen);\n 193:\t}\n 194:\t\n 195:\tstatic void acomp_virt_to_sg(struct acomp_req *req)\n 196:\t{\n 197:\t\tstruct acomp_req_chain *state = \u0026req-\u003echain;\n 198:\t\n 199:\t\tstate-\u003eflags = req-\u003ebase.flags \u0026 (CRYPTO_ACOMP_REQ_SRC_VIRT |\n 200:\t\t\t\t\t\t  CRYPTO_ACOMP_REQ_DST_VIRT);\n 201:\t\n 202:\t\tif (acomp_request_src_isvirt(req)) {\n 203:\t\t\tunsigned int slen = req-\u003eslen;\n 204:\t\t\tconst u8 *svirt = req-\u003esvirt;\n 205:\t\n 206:\t\t\tstate-\u003esrc = svirt;\n 207:\t\t\tsg_init_one(\u0026state-\u003essg, svirt, slen);\n 208:\t\t\tacomp_request_set_src_sg(req, \u0026state-\u003essg, slen);\n 209:\t\t}\n 210:\t\n 211:\t\tif (acomp_request_dst_isvirt(req)) {\n 212:\t\t\tunsigned int dlen = req-\u003edlen;\n 213:\t\t\tu8 *dvirt = req-\u003edvirt;\n 214:\t\n 215:\t\t\tstate-\u003edst = dvirt;\n 216:\t\t\tsg_init_one(\u0026state-\u003edsg, dvirt, dlen);\n 217:\t\t\tacomp_request_set_dst_sg(req, \u0026state-\u003edsg, dlen);\n 218:\t\t}\n 219:\t}\n"
}

51/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 48442
							output: 34
							thoughts: 22
						

Thoughts:
Here's a summary of my thought process, as requested:

**Diving into `acompress.c`**

Okay, time to get specific. Instead of broad strokes, I need to pinpoint the relevant section. I'm going to jump into `crypto/acompress.c` directly. My intuition tells me that the relevant information is likely within lines 280-325. This section could contain the heart of the compression algorithm, or perhaps crucial interactions with a specific hardware element. I need to quickly scan this range, looking for key function calls, data structures, and comments that might illuminate the performance characteristics or potential bottlenecks. This requires a targeted approach - I’m not after a general understanding of the file, just the specifics of this segment. This level of granularity is essential for diagnosis.



52/3 2026/11/09 05:51 tool read-file
0m Args:
{
  "File": "crypto/acompress.c",
  "FirstLine": 280,
  "LineCount": 45
}

Results:
{
  "Contents": " 280:\t\tacomp_save_req(req, acomp_reqchain_done);\n 281:\t\n 282:\t\terr = acomp_do_one_req(req, comp);\n 283:\t\tif (unlikely(err == -EBUSY || err == -EINPROGRESS))\n 284:\t\t\treturn err;\n 285:\t\n 286:\t\treturn acomp_reqchain_finish(req, err);\n 287:\t}\n 288:\t\n 289:\tint crypto_acomp_compress(struct acomp_req *req)\n 290:\t{\n 291:\t\tstruct crypto_acomp *tfm = crypto_acomp_reqtfm(req);\n 292:\t\n 293:\t\tif (likely((req-\u003eunit_size == req-\u003eslen) \u0026\u0026 acomp_request_issg(req) \u0026\u0026\n 294:\t\t\t    !crypto_acomp_req_seg(tfm) \u0026\u0026 !acomp_is_async(tfm))) {\n 295:\t\t\tint err = tfm-\u003ecompress(req);\n 296:\t\n 297:\t\t\treq-\u003edst-\u003elength = unlikely(err) ? err : req-\u003edlen;\n 298:\t\t\treturn err;\n 299:\t\t}\n 300:\t\n 301:\t\tif (acomp_req_on_stack(req) \u0026\u0026 acomp_is_async(tfm))\n 302:\t\t\treturn -EAGAIN;\n 303:\t\n 304:\t\tif (req-\u003eunit_size \u0026\u0026 acomp_request_issg(req)) {\n 305:\t\t\tif (!crypto_acomp_req_seg(tfm))\n 306:\t\t\t\treturn acomp_do_req_chain(req, true);\n 307:\t\n 308:\t\t\treturn tfm-\u003ecompress(req);\n 309:\t\t}\n 310:\t\n 311:\t\tif (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))\n 312:\t\t\treturn tfm-\u003ecompress(req);\n 313:\t\n 314:\t\treturn acomp_do_req_chain(req, true);\n 315:\t}\n 316:\tEXPORT_SYMBOL_GPL(crypto_acomp_compress);\n 317:\t\n 318:\tint crypto_acomp_decompress(struct acomp_req *req)\n 319:\t{\n 320:\t\tstruct crypto_acomp *tfm = crypto_acomp_reqtfm(req);\n 321:\t\n 322:\t\tif (acomp_req_on_stack(req) \u0026\u0026 acomp_is_async(tfm))\n 323:\t\t\treturn -EAGAIN;\n 324:\t\tif (crypto_acomp_req_virt(tfm) || acomp_request_issg(req))\n"
}

53/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 49426
							output: 20
							thoughts: 99
						

Thoughts:
**Investigating the Allocation of acomp_request Structures**

Okay, let's dive into how the `req` variable is allocated and initialized within the context of crypto acomp users. I need to understand this better. My initial thought is to start with the core header file, `include/crypto/acompress.h`, to find the relevant declarations. Specifically, I'm looking for either `acomp_request_alloc`, `acomp_request_init`, or indications of stack allocation.

My next step is to examine the behavior when `acomp_request_alloc` is invoked. I need to trace its path to understand what exactly happens during the allocation process. To achieve this, I'll examine the definition of `acomp_request_alloc`. I will look in either `include/crypto/acompress.h` to see the declaration details, or if it is a function, in `crypto/acompress.c` to understand the low-level workings.



54/3 2026/11/09 05:51 tool grepper
0m Args:
{
  "Expression": "acomp_request_alloc"
}

Results:
{
  "Output": "crypto/testmgr.c=3269=static int test_acomp(struct crypto_acomp *tfm,\n--\ncrypto/testmgr.c-3307-\ncrypto/testmgr.c:3308:\t\treq = acomp_request_alloc(tfm);\ncrypto/testmgr.c-3309-\t\tif (!req) {\n--\ncrypto/testmgr.c-3384-\ncrypto/testmgr.c:3385:\t\treq = acomp_request_alloc(tfm);\ncrypto/testmgr.c-3386-\t\tif (!req) {\n--\nfs/erofs/decompressor_crypto.c=6=static int __z_erofs_crypto_decompress(struct z_erofs_decompress_req *rq,\n--\nfs/erofs/decompressor_crypto.c-23-\nfs/erofs/decompressor_crypto.c:24:\treq = acomp_request_alloc(tfm);\nfs/erofs/decompressor_crypto.c-25-\tif (!req)\n--\ninclude/crypto/acompress.h=240=static inline const char *crypto_acomp_driver_name(struct crypto_acomp *tfm)\n--\ninclude/crypto/acompress.h-245-/**\ninclude/crypto/acompress.h:246: * acomp_request_alloc_extra() -- allocates asynchronous (de)compression request\ninclude/crypto/acompress.h-247- *\n--\ninclude/crypto/acompress.h-253- */\ninclude/crypto/acompress.h:254:static inline struct acomp_req *acomp_request_alloc_extra_noprof(\ninclude/crypto/acompress.h-255-\tstruct crypto_acomp *tfm, size_t extra, gfp_t gfp)\n--\ninclude/crypto/acompress.h-268-}\ninclude/crypto/acompress.h:269:#define acomp_request_alloc_noprof(tfm, ...) \\\ninclude/crypto/acompress.h:270:\tCONCATENATE(acomp_request_alloc_noprof_, COUNT_ARGS(__VA_ARGS__))( \\\ninclude/crypto/acompress.h-271-\t\ttfm, ##__VA_ARGS__)\ninclude/crypto/acompress.h:272:#define acomp_request_alloc_noprof_0(tfm) \\\ninclude/crypto/acompress.h:273:\tacomp_request_alloc_noprof_1(tfm, GFP_KERNEL)\ninclude/crypto/acompress.h:274:#define acomp_request_alloc_noprof_1(tfm, gfp) \\\ninclude/crypto/acompress.h:275:\tacomp_request_alloc_extra_noprof(tfm, 0, gfp)\ninclude/crypto/acompress.h:276:#define acomp_request_alloc(...)\talloc_hooks(acomp_request_alloc_noprof(__VA_ARGS__))\ninclude/crypto/acompress.h-277-\ninclude/crypto/acompress.h-278-/**\ninclude/crypto/acompress.h:279: * acomp_request_alloc_extra() -- allocate acomp request with extra memory\ninclude/crypto/acompress.h-280- *\n--\ninclude/crypto/acompress.h-286- */\ninclude/crypto/acompress.h:287:#define acomp_request_alloc_extra(...)\talloc_hooks(acomp_request_alloc_extra_noprof(__VA_ARGS__))\ninclude/crypto/acompress.h-288-\n--\nkernel/power/swap.c=694=static int save_compressed_image(struct swap_map_handle *handle,\n--\nkernel/power/swap.c-756-\nkernel/power/swap.c:757:\t\tdata[thr].cr = acomp_request_alloc(data[thr].cc);\nkernel/power/swap.c-758-\t\tif (!data[thr].cr) {\n--\nkernel/power/swap.c=1195=static int load_compressed_image(struct swap_map_handle *handle,\n--\nkernel/power/swap.c-1260-\nkernel/power/swap.c:1261:\t\tdata[thr].cr = acomp_request_alloc(data[thr].cc);\nkernel/power/swap.c-1262-\t\tif (!data[thr].cr) {\n--\nmm/zswap.c=891=static int zswap_acomp_req_init(struct zswap_acomp_req *areq,\n--\nmm/zswap.c-893-{\nmm/zswap.c:894:\t/* acomp_request_alloc() returns NULL in case of an error. */\nmm/zswap.c:895:\tareq-\u003ereq = acomp_request_alloc(acomp);\nmm/zswap.c-896-\tif (!areq-\u003ereq)\n--\nnet/xfrm/xfrm_ipcomp.c=118=static struct acomp_req *ipcomp_setup_req(struct xfrm_state *x,\n--\nnet/xfrm/xfrm_ipcomp.c-164-\ttfm = ipcd-\u003etfm;\nnet/xfrm/xfrm_ipcomp.c:165:\treq = acomp_request_alloc_extra(\nnet/xfrm/xfrm_ipcomp.c-166-\t\ttfm, sizeof(*extra) + sizeof(*sg) * (nfrags + dnfrags),\n"
}

55/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 51147
							output: 36
							thoughts: 29
						

Thoughts:
Here's my take on the thought process, as requested:

**Diving into `acompress.h`**

Alright, let's get right to it. I need to take a look at the `acomp_request_alloc_extra_noprof` function. My instinct tells me it's located in `include/crypto/acompress.h`, so I'm heading there first. Given the naming convention, it likely handles the allocation of some extra data associated with asynchronous compression requests, and the `noprof` suffix suggests this version doesn't involve performance profiling, which is useful to know. I'm curious about the specific allocation logic and what extra data this is. Time to dig in and see the details.



56/3 2026/11/09 05:51 tool read-file
0m Args:
{
  "File": "include/crypto/acompress.h",
  "FirstLine": 246,
  "LineCount": 25
}

Results:
{
  "Contents": " 246:\t * acomp_request_alloc_extra() -- allocates asynchronous (de)compression request\n 247:\t *\n 248:\t * @tfm:\tACOMPRESS tfm handle allocated with crypto_alloc_acomp()\n 249:\t * @extra:\tamount of extra memory\n 250:\t * @gfp:\tgfp to pass to kzalloc (defaults to GFP_KERNEL)\n 251:\t *\n 252:\t * Return:\tallocated handle in case of success or NULL in case of an error\n 253:\t */\n 254:\tstatic inline struct acomp_req *acomp_request_alloc_extra_noprof(\n 255:\t\tstruct crypto_acomp *tfm, size_t extra, gfp_t gfp)\n 256:\t{\n 257:\t\tstruct acomp_req *req;\n 258:\t\tsize_t len;\n 259:\t\n 260:\t\tlen = ALIGN(sizeof(*req) + crypto_acomp_reqsize(tfm), CRYPTO_MINALIGN);\n 261:\t\tif (check_add_overflow(len, extra, \u0026len))\n 262:\t\t\treturn NULL;\n 263:\t\n 264:\t\treq = kzalloc_noprof(len, gfp);\n 265:\t\tif (likely(req))\n 266:\t\t\tacomp_request_set_tfm(req, tfm);\n 267:\t\treturn req;\n 268:\t}\n 269:\t#define acomp_request_alloc_noprof(tfm, ...) \\\n 270:\t\tCONCATENATE(acomp_request_alloc_noprof_, COUNT_ARGS(__VA_ARGS__))( \\\n"
}

57/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 51829
							output: 36
							thoughts: 299
						

Thoughts:
**Analyzing the `acomp_req` Structure Initialization**

Okay, so I'm digging into how `acomp_req` structures are initialized, specifically when working with the compression framework. I'm focusing on ensuring no unexpected values are in the struct. I see `acomp_request_alloc_extra_noprof` uses `kzalloc_noprof`. That's good, because `kzalloc_noprof` will ensure the allocated `req` is completely zeroed out upon allocation. This is a crucial first step for safety and determinism.

Next, I need to understand how the callback and other fields are set up. I see `acomp_request_set_callback` is responsible for this. It looks like it manipulates the `flgs` field, clears `CRYPTO_ACOMP_REQ_PRIVATE` then sets it again from the `req->base.flags`, and importantly, sets `req->unit_size` to 0 initially. Then `crypto_request_set_callback` is called, likely setting up the standard cryptographic request structure.

Now, how is `unit_size` changed? It looks like `acomp_request_set_unit_size` sets `req->unit_size` to a value `du`. That makes sense; it's the size we're dealing with.

I want to understand the stack request initialization. I'll need to check `acomp_request_on_stack_init`. Let me check that function in `include/crypto/acompress.h`, specifically around line 605, to understand how this differs from dynamic allocation.



58/3 2026/11/09 05:51 tool read-file
0m Args:
{
  "File": "include/crypto/acompress.h",
  "FirstLine": 605,
  "LineCount": 30
}

Results:
{
  "Contents": " 605:\t */\n 606:\tunsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm);\n 607:\t\n 608:\tstatic inline struct acomp_req *acomp_request_on_stack_init(\n 609:\t\tchar *buf, struct crypto_acomp *tfm)\n 610:\t{\n 611:\t\tstruct acomp_req *req = (void *)buf;\n 612:\t\n 613:\t\tcrypto_stack_request_init(\u0026req-\u003ebase, crypto_acomp_tfm(tfm));\n 614:\t\treturn req;\n 615:\t}\n 616:\t\n 617:\tstruct acomp_req *acomp_request_clone(struct acomp_req *req,\n 618:\t\t\t\t\t      size_t total, gfp_t gfp);\n 619:\t\n 620:\t#endif\n"
}

59/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 52767
							output: 33
							thoughts: 25
						

Thoughts:
Okay, here's my attempt at summarizing that thought process, tailored for an expert audience:

**Investigating `acomp_request_on_stack_init` Usage**

Alright, let's dive in. I need to understand the lifecycle of this `acomp_request_on_stack_init` function. The first thing I'm doing is tracing its origins. Where is this thing called? Is it a direct function call, or perhaps it's hidden behind a macro instantiation? I need to quickly identify the points of initialization. That'll give me a good handle on its expected usage patterns and context within the larger system. The goal is to figure out the likely purpose and relationship to other components within the design.



60/3 2026/11/09 05:51 tool grepper
0m Args:
{
  "Expression": "acomp_request_on_stack_init|ACOMP_REQUEST_ON_STACK"
}

Results:
{
  "Output": "fs/ubifs/compress.c=76=static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,\n--\nfs/ubifs/compress.c-93-\tdo {\nfs/ubifs/compress.c:94:\t\tACOMP_REQUEST_ON_STACK(req, compr-\u003ecc);\nfs/ubifs/compress.c-95-\t\tDECLARE_CRYPTO_WAIT(wait);\n--\nfs/ubifs/compress.c=191=static int ubifs_decompress_common(const struct ubifs_info *c,\n--\nfs/ubifs/compress.c-222-\tdo {\nfs/ubifs/compress.c:223:\t\tACOMP_REQUEST_ON_STACK(req, compr-\u003ecc);\nfs/ubifs/compress.c-224-\t\tDECLARE_CRYPTO_WAIT(wait);\n--\ninclude/crypto/acompress.h-43-\ninclude/crypto/acompress.h:44:#define ACOMP_REQUEST_ON_STACK(name, tfm) \\\ninclude/crypto/acompress.h-45-        char __##name##_req[sizeof(struct acomp_req) + \\\ninclude/crypto/acompress.h-46-                            MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR; \\\ninclude/crypto/acompress.h:47:        struct acomp_req *name = acomp_request_on_stack_init( \\\ninclude/crypto/acompress.h-48-                __##name##_req, (tfm))\n--\ninclude/crypto/acompress.h=606=unsigned int crypto_acomp_batch_size(struct crypto_acomp *tfm);\ninclude/crypto/acompress.h-607-\ninclude/crypto/acompress.h:608:static inline struct acomp_req *acomp_request_on_stack_init(\ninclude/crypto/acompress.h-609-\tchar *buf, struct crypto_acomp *tfm)\n--\nmm/zswap.c=1227=static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)\n--\nmm/zswap.c-1241-\tif (!acomp_ctx-\u003edecomp.req) {\nmm/zswap.c:1242:\t\tACOMP_REQUEST_ON_STACK(req, acomp_ctx-\u003eacomp);\nmm/zswap.c-1243-\t\tDECLARE_CRYPTO_WAIT(wait);\n"
}

61/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 53606
							output: 21
							thoughts: 311
						

Thoughts:
**Analyzing the `ACOMP_REQUEST_ON_STACK` Macro**

Okay, let's dive into this `ACOMP_REQUEST_ON_STACK` thing. First, I need to check how it's defined. Right, when it expands, it looks like it declares a `char __##name##_req[...]` on the stack. My initial concern is whether this is initialized or not. I'd hate to see uninitialized memory, that's just asking for trouble.

Let me see… looking at `include/crypto/acompress.h`, the macro definition reveals that it does initialize the request, good. It's declaring `char __##name##_req[sizeof(struct acomp_req) + MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR;` and then initializing a pointer `struct acomp_req *name` to point at it with a call to `acomp_request_on_stack_init`.

Alright, what does `acomp_request_on_stack_init` actually do? Let me see the definition. Ah, it takes a `char *buf` and a `struct crypto_acomp *tfm`. It casts the buffer to a `struct acomp_req *req`, and then calls `crypto_stack_request_init` on the base of that request. And then, it returns the request. That's starting to make sense, but now I'm curious about the details of what `crypto_stack_request_init` does. I need to check that out, let's go look at `include/linux/crypto.h`.



62/3 2026/11/09 05:51 tool grepper
0m Args:
{
  "Expression": "crypto_stack_request_init"
}

Results:
{
  "Output": "include/crypto/acompress.h=608=static inline struct acomp_req *acomp_request_on_stack_init(\n--\ninclude/crypto/acompress.h-612-\ninclude/crypto/acompress.h:613:\tcrypto_stack_request_init(\u0026req-\u003ebase, crypto_acomp_tfm(tfm));\ninclude/crypto/acompress.h-614-\treturn req;\n--\ninclude/crypto/hash.h=1031=static inline struct ahash_request *ahash_request_on_stack_init(\n--\ninclude/crypto/hash.h-1035-\ninclude/crypto/hash.h:1036:\tcrypto_stack_request_init(\u0026req-\u003ebase, crypto_ahash_tfm(tfm));\ninclude/crypto/hash.h-1037-\treturn req;\n--\ninclude/crypto/internal/acompress.h=235=static inline struct acomp_req *acomp_fbreq_on_stack_init(\n--\ninclude/crypto/internal/acompress.h-240-\ninclude/crypto/internal/acompress.h:241:\tcrypto_stack_request_init(\u0026req-\u003ebase,\ninclude/crypto/internal/acompress.h-242-\t\t\t\t  crypto_acomp_tfm(crypto_acomp_fb(tfm)));\n--\ninclude/crypto/internal/hash.h=328=static inline struct ahash_request *ahash_fbreq_on_stack_init(\n--\ninclude/crypto/internal/hash.h-333-\ninclude/crypto/internal/hash.h:334:\tcrypto_stack_request_init(\u0026req-\u003ebase,\ninclude/crypto/internal/hash.h-335-\t\t\t\t  crypto_ahash_tfm(crypto_ahash_fb(tfm)));\n--\ninclude/linux/crypto.h=519=struct crypto_async_request *crypto_request_clone(\n--\ninclude/linux/crypto.h-521-\ninclude/linux/crypto.h:522:static inline void crypto_stack_request_init(struct crypto_async_request *req,\ninclude/linux/crypto.h-523-\t\t\t\t\t     struct crypto_tfm *tfm)\n"
}

63/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 54852
							output: 35
							thoughts: 23
						

Thoughts:
Here's my summary, as though I'm reading and processing the provided code context:

**Analyzing `crypto_stack_request_init`**

Alright, let's dive into this `crypto_stack_request_init` function, located in `include/linux/crypto.h`. I'm immediately interested in the structure and purpose of this initialization routine. Given the name, I'd expect it deals with setting up requests for cryptographic operations, likely within some sort of stacked or layered crypto framework. The "stack" part suggests there might be chaining of algorithms, or potentially some kind of transport wrapper functionality involved. I'll need to look at the surrounding code to determine how it integrates with the overall crypto API. I'll be focused on understanding what data structures are being initialized and how this function prepares a request for submission to the underlying crypto hardware or software. Is this function responsible for setting up context, allocating resources, or simply zeroing out data? Those are the immediate questions I have. I anticipate this will be a crucial piece in understanding how requests are built and processed in the kernel's cryptographic subsystem.



64/3 2026/11/09 05:51 tool read-file
0m Args:
{
  "File": "include/linux/crypto.h",
  "FirstLine": 520,
  "LineCount": 20
}

Results:
{
  "Contents": " 520:\t\tstruct crypto_async_request *req, size_t total, gfp_t gfp);\n 521:\t\n 522:\tstatic inline void crypto_stack_request_init(struct crypto_async_request *req,\n 523:\t\t\t\t\t\t     struct crypto_tfm *tfm)\n 524:\t{\n 525:\t\treq-\u003eflags = 0;\n 526:\t\tcrypto_request_set_tfm(req, tfm);\n 527:\t\treq-\u003eflags |= CRYPTO_TFM_REQ_ON_STACK;\n 528:\t}\n 529:\t\n 530:\t#endif\t/* _LINUX_CRYPTO_H */\n 531:\t\n"
}

65/3 2026/11/09 05:51 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 55343
							output: 35
							thoughts: 509
						

Thoughts:
**Analyzing Potential Security Implications in Crypto Compression**

Okay, hold on a minute. Let's dig into this `crypto_stack_request_init` function. I see it initializes the `req->flags` and specifically sets the `CRYPTO_TFM_REQ_ON_STACK` flag. The problem is, it *doesn't* zero out the `struct acomp_req` itself. Now, I need to look at what's been added to that struct – aha! An `unsigned int unit_size;` field. This is interesting...

Now, let's look at `crypto_acomp_compress`. The logic in there relies heavily on the `req` structure. Specifically, line 293 catches my eye: `if (likely((req->unit_size == req->slen) && ...`. That entire block has a critical vulnerability. What happens if the `req` is allocated on the stack? The `unit_size` may have garbage in it!

I need to see where `ACOMP_REQUEST_ON_STACK` is actually *used* with `crypto_acomp_compress`. I'm going to look at the implementation for `fs/ubifs/compress.c`. Let's analyze lines 90-110 to see if this is actually a problem and how it's handled, or if it's even utilized there. My expertise is now required to assess the potential vulnerability, based on whether the assumption that the `unit_size` field is zeroed out is valid or not.



66/3 2026/11/09 05:52 tool read-file
0m Args:
{
  "File": "fs/ubifs/compress.c",
  "FirstLine": 85,
  "LineCount": 40
}

Results:
{
  "Contents": "  85:\t\t\tgoto no_compr;\n  86:\t\n  87:\t\t/* If the input data is small, do not even try to compress it */\n  88:\t\tif (in_len \u003c UBIFS_MIN_COMPR_LEN)\n  89:\t\t\tgoto no_compr;\n  90:\t\n  91:\t\tdlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF);\n  92:\t\n  93:\t\tdo {\n  94:\t\t\tACOMP_REQUEST_ON_STACK(req, compr-\u003ecc);\n  95:\t\t\tDECLARE_CRYPTO_WAIT(wait);\n  96:\t\n  97:\t\t\tacomp_request_set_callback(req, 0, NULL, NULL);\n  98:\t\t\tif (in_folio)\n  99:\t\t\t\tacomp_request_set_src_folio(req, in_ptr.folio,\n 100:\t\t\t\t\t\t\t    in_offset, in_len);\n 101:\t\t\telse\n 102:\t\t\t\tacomp_request_set_src_dma(req, in_ptr.buf, in_len);\n 103:\t\t\tacomp_request_set_dst_dma(req, out_buf, dlen);\n 104:\t\t\terr = crypto_acomp_compress(req);\n 105:\t\t\tdlen = req-\u003edlen;\n 106:\t\t\tif (err != -EAGAIN)\n 107:\t\t\t\tbreak;\n 108:\t\n 109:\t\t\treq = ACOMP_REQUEST_CLONE(req, GFP_NOFS | __GFP_NOWARN);\n 110:\t\t\tacomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,\n 111:\t\t\t\t\t\t   crypto_req_done, \u0026wait);\n 112:\t\t\terr = crypto_acomp_compress(req);\n 113:\t\t\terr = crypto_wait_req(err, \u0026wait);\n 114:\t\t\tdlen = req-\u003edlen;\n 115:\t\t\tacomp_request_free(req);\n 116:\t\t} while (0);\n 117:\t\n 118:\t\t*out_len = dlen;\n 119:\t\tif (err)\n 120:\t\t\tgoto no_compr;\n 121:\t\n 122:\t\treturn;\n 123:\t\n 124:\tno_compr:\n"
}

67/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 56843
							output: 24
							thoughts: 302
						

Thoughts:
**Analyzing a Potential Regression**

Okay, here's what I'm thinking. I need to dive into this. Line 97, specifically `acomp_request_set_callback(req, 0, NULL, NULL)`, caught my eye. The immediate question is, what changes did that patch bring to `acomp_request_set_callback`? I know I need to check the patch diff, so let's pull that up in `include/crypto/acompress.h`. Ah, right, that `req->unit_size = 0;` line. That's the key. Now, the potential problem is whether all callers of `acomp_req` go through this `acomp_request_set_callback` function, or if the initialization of `req->unit_size` may be missed in some cases. To fully assess this, I need a comprehensive view. I'm going to grep for `crypto_acomp_compress` and `crypto_acomp_decompress` throughout the entire kernel codebase. That will give me a clear picture of all the potential usage points and tell me if my concern is valid.



68/3 2026/11/09 05:52 tool grepper
0m Args:
{
  "Expression": "crypto_acomp_(compress|decompress)"
}

Results:
{
  "Output": "crypto/acompress.c=221=static int acomp_do_nondma(struct acomp_req *req, bool comp)\n--\ncrypto/acompress.c-226-\tif (comp)\ncrypto/acompress.c:227:\t\terr = crypto_acomp_compress(fbreq);\ncrypto/acompress.c-228-\telse\ncrypto/acompress.c:229:\t\terr = crypto_acomp_decompress(fbreq);\ncrypto/acompress.c-230-\n--\ncrypto/acompress.c=273=static int acomp_do_req_chain(struct acomp_req *req, bool comp)\n--\ncrypto/acompress.c-288-\ncrypto/acompress.c:289:int crypto_acomp_compress(struct acomp_req *req)\ncrypto/acompress.c-290-{\n--\ncrypto/acompress.c-315-}\ncrypto/acompress.c:316:EXPORT_SYMBOL_GPL(crypto_acomp_compress);\ncrypto/acompress.c-317-\ncrypto/acompress.c:318:int crypto_acomp_decompress(struct acomp_req *req)\ncrypto/acompress.c-319-{\n--\ncrypto/acompress.c-327-}\ncrypto/acompress.c:328:EXPORT_SYMBOL_GPL(crypto_acomp_decompress);\ncrypto/acompress.c-329-\n--\ncrypto/testmgr.c=3269=static int test_acomp(struct crypto_acomp *tfm,\n--\ncrypto/testmgr.c-3320-\ncrypto/testmgr.c:3321:\t\tret = crypto_wait_req(crypto_acomp_compress(req), \u0026wait);\ncrypto/testmgr.c-3322-\t\tif (ret) {\n--\ncrypto/testmgr.c-3336-\ncrypto/testmgr.c:3337:\t\tret = crypto_wait_req(crypto_acomp_decompress(req), \u0026wait);\ncrypto/testmgr.c-3338-\t\tif (ret) {\n--\ncrypto/testmgr.c-3397-\ncrypto/testmgr.c:3398:\t\tret = crypto_wait_req(crypto_acomp_decompress(req), \u0026wait);\ncrypto/testmgr.c-3399-\t\tif (ret) {\n--\ndrivers/crypto/hisilicon/zip/zip_crypto.c=120=static int hisi_zip_fallback_do_work(struct acomp_req *acomp_req, bool is_decompress)\n--\ndrivers/crypto/hisilicon/zip/zip_crypto.c-125-\tif (!is_decompress)\ndrivers/crypto/hisilicon/zip/zip_crypto.c:126:\t\tret = crypto_acomp_compress(fbreq);\ndrivers/crypto/hisilicon/zip/zip_crypto.c-127-\telse\ndrivers/crypto/hisilicon/zip/zip_crypto.c:128:\t\tret = crypto_acomp_decompress(fbreq);\ndrivers/crypto/hisilicon/zip/zip_crypto.c-129-\tif (ret) {\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c=1020=static int deflate_generic_decompress(struct acomp_req *req)\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1024-\ndrivers/crypto/intel/iaa/iaa_crypto_main.c:1025:\tret = crypto_acomp_decompress(fbreq);\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1026-\treq-\u003edlen = fbreq-\u003edlen;\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c=1033=static int deflate_generic_compress(struct acomp_req *req)\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1037-\ndrivers/crypto/intel/iaa/iaa_crypto_main.c:1038:\tret = crypto_acomp_compress(fbreq);\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1039-\treq-\u003edlen = fbreq-\u003edlen;\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=360=static int qat_comp_alg_zstd_decompress(struct acomp_req *req)\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-386-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:387:\t\tret = crypto_acomp_decompress(nreq);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-388-\t\treq-\u003edlen = nreq-\u003edlen;\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=551=static int qat_comp_alg_lz4s_zstd_compress(struct acomp_req *req)\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-564-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:565:\tret = crypto_acomp_compress(nreq);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-566-\treq-\u003edlen = nreq-\u003edlen;\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=571=static int qat_comp_alg_sw_decompress(struct acomp_req *req)\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-580-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:581:\tret = crypto_acomp_decompress(nreq);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-582-\treq-\u003edlen = nreq-\u003edlen;\n--\nfs/erofs/decompressor_crypto.c=6=static int __z_erofs_crypto_decompress(struct z_erofs_decompress_req *rq,\n--\nfs/erofs/decompressor_crypto.c-44-\nfs/erofs/decompressor_crypto.c:45:\tret = crypto_wait_req(crypto_acomp_decompress(req), \u0026wait);\nfs/erofs/decompressor_crypto.c-46-\tif (ret) {\n--\nfs/ubifs/compress.c=76=static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,\n--\nfs/ubifs/compress.c-103-\t\tacomp_request_set_dst_dma(req, out_buf, dlen);\nfs/ubifs/compress.c:104:\t\terr = crypto_acomp_compress(req);\nfs/ubifs/compress.c-105-\t\tdlen = req-\u003edlen;\n--\nfs/ubifs/compress.c-111-\t\t\t\t\t   crypto_req_done, \u0026wait);\nfs/ubifs/compress.c:112:\t\terr = crypto_acomp_compress(req);\nfs/ubifs/compress.c-113-\t\terr = crypto_wait_req(err, \u0026wait);\n--\nfs/ubifs/compress.c=191=static int ubifs_decompress_common(const struct ubifs_info *c,\n--\nfs/ubifs/compress.c-233-\t\t\tacomp_request_set_dst_dma(req, out_ptr, dlen);\nfs/ubifs/compress.c:234:\t\terr = crypto_acomp_decompress(req);\nfs/ubifs/compress.c-235-\t\tdlen = req-\u003edlen;\n--\nfs/ubifs/compress.c-239-\t\treq = ACOMP_REQUEST_CLONE(req, GFP_NOFS | __GFP_NOWARN);\nfs/ubifs/compress.c:240:\t\terr = crypto_acomp_decompress(req);\nfs/ubifs/compress.c-241-\t\terr = crypto_wait_req(err, \u0026wait);\n--\ninclude/crypto/acompress.h=565=static inline void acomp_request_set_dst_folio(struct acomp_req *req,\n--\ninclude/crypto/acompress.h-574-/**\ninclude/crypto/acompress.h:575: * crypto_acomp_compress() -- Invoke asynchronous compress operation\ninclude/crypto/acompress.h-576- *\n--\ninclude/crypto/acompress.h-582- */\ninclude/crypto/acompress.h:583:int crypto_acomp_compress(struct acomp_req *req);\ninclude/crypto/acompress.h-584-\ninclude/crypto/acompress.h-585-/**\ninclude/crypto/acompress.h:586: * crypto_acomp_decompress() -- Invoke asynchronous decompress operation\ninclude/crypto/acompress.h-587- *\n--\ninclude/crypto/acompress.h-593- */\ninclude/crypto/acompress.h:594:int crypto_acomp_decompress(struct acomp_req *req);\ninclude/crypto/acompress.h-595-\n--\nkernel/power/swap.c=663=static int compress_threadfn(void *data)\n--\nkernel/power/swap.c-683-\t\t\t\t\t     CMP_SIZE - CMP_HEADER);\nkernel/power/swap.c:684:\t\td-\u003eret = crypto_acomp_compress(d-\u003ecr);\nkernel/power/swap.c-685-\t\td-\u003ecmp_len = d-\u003ecr-\u003edlen;\n--\nkernel/power/swap.c=1161=static int decompress_threadfn(void *data)\n--\nkernel/power/swap.c-1181-\t\tacomp_request_set_dst_nondma(d-\u003ecr, d-\u003eunc, UNC_SIZE);\nkernel/power/swap.c:1182:\t\td-\u003eret = crypto_acomp_decompress(d-\u003ecr);\nkernel/power/swap.c-1183-\t\td-\u003eunc_len = d-\u003ecr-\u003edlen;\n--\nmm/zswap.c=1087=static bool zswap_compress(struct zswap_store_context *zc)\n--\nmm/zswap.c-1132-\nmm/zswap.c:1133:\t\terr = crypto_wait_req(crypto_acomp_compress(zc-\u003ecomp_ctx-\u003eareq.req),\nmm/zswap.c-1134-\t\t\t\t      \u0026zc-\u003ecomp_ctx-\u003eareq.wait);\n--\nmm/zswap.c=1183=static bool __zswap_decompress(struct zswap_entry *entry,\n--\nmm/zswap.c-1207-\t\tacomp_request_set_params(req, input, \u0026output, entry-\u003elength, PAGE_SIZE);\nmm/zswap.c:1208:\t\tret = crypto_acomp_decompress(req);\nmm/zswap.c-1209-\t\tret = crypto_wait_req(ret, wait);\n--\nnet/xfrm/xfrm_ipcomp.c=203=static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)\n--\nnet/xfrm/xfrm_ipcomp.c-213-\tacomp_request_set_callback(req, 0, ipcomp_input_done, skb);\nnet/xfrm/xfrm_ipcomp.c:214:\terr = crypto_acomp_decompress(req);\nnet/xfrm/xfrm_ipcomp.c-215-\tif (err == -EINPROGRESS)\n--\nnet/xfrm/xfrm_ipcomp.c=276=static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)\n--\nnet/xfrm/xfrm_ipcomp.c-288-\tacomp_request_set_callback(req, 0, ipcomp_output_done, skb);\nnet/xfrm/xfrm_ipcomp.c:289:\terr = crypto_acomp_compress(req);\nnet/xfrm/xfrm_ipcomp.c-290-\tif (err == -EINPROGRESS)\n"
}

69/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 60593
							output: 24
							thoughts: 322
						

Thoughts:
**Analyzing `acomp_request` Usage in the Kernel**

Okay, let's dive into this `acomp_request` situation. It seems we have a potential issue related to the `unit_size` field, which I need to get to the bottom of. First, I'm going to run through some of the callers of `acomp_request` to get an idea of where this might come from:

1.  `kernel/power/swap.c`: This uses `acomp_request_alloc`, which means `kzalloc` is being used. Consequently, the initial value of `req->unit_size` is 0. Standard behavior.
2.  `crypto/testmgr.c`: Similar to the previous, it also calls `acomp_request_alloc` with `kzalloc`, leaving `req->unit_size` at 0. No surprises.
3.  `fs/erofs/decompressor_crypto.c`: Again, `acomp_request_alloc` and `kzalloc`, leading to `req->unit_size` being initialized to 0. Noted.
4.  `fs/ubifs/compress.c`: Hmm, here we have `ACOMP_REQUEST_ON_STACK`, which might be a different situation. But it *does* call `acomp_request_set_callback`, which means `req->unit_size` is explicitly set to 0. Worth keeping an eye on.
5.  `net/xfrm/xfrm_ipcomp.c`: This one also utilizes `acomp_request_set_callback`, resulting in `req->unit_size` being set to 0. Okay, that's two instances of explicit zeroing.
6.  `mm/zswap.c`: This one is interesting; it's a combination of `acomp_request_alloc` (which uses kzalloc), then `acomp_request_set_callback` and also *sets* `acomp_request_set_unit_size` explicitly. This likely means it does it in 2 stages.

Alright, now, before I move on, let me check `drivers/crypto/hisilicon/zip/zip_crypto.c`. I need to figure out how it handles `fbreq`. I see a macro: `ACOMP_FBREQ_ON_STACK(fbreq, req)`. So, let's track down where `ACOMP_FBREQ_ON_STACK` is defined. I'll need to check `include/crypto/internal/acompress.h` to see exactly what's going on there. I need to understand this macro to continue the investigation.



70/3 2026/11/09 05:52 tool grepper
0m Args:
{
  "Expression": "ACOMP_FBREQ_ON_STACK"
}

Results:
{
  "Output": "crypto/acompress.c=221=static int acomp_do_nondma(struct acomp_req *req, bool comp)\ncrypto/acompress.c-222-{\ncrypto/acompress.c:223:\tACOMP_FBREQ_ON_STACK(fbreq, req);\ncrypto/acompress.c-224-\tint err;\n--\ndrivers/crypto/hisilicon/zip/zip_crypto.c=120=static int hisi_zip_fallback_do_work(struct acomp_req *acomp_req, bool is_decompress)\ndrivers/crypto/hisilicon/zip/zip_crypto.c-121-{\ndrivers/crypto/hisilicon/zip/zip_crypto.c:122:\tACOMP_FBREQ_ON_STACK(fbreq, acomp_req);\ndrivers/crypto/hisilicon/zip/zip_crypto.c-123-\tint ret;\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c=1020=static int deflate_generic_decompress(struct acomp_req *req)\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1021-{\ndrivers/crypto/intel/iaa/iaa_crypto_main.c:1022:\tACOMP_FBREQ_ON_STACK(fbreq, req);\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1023-\tint ret;\n--\ndrivers/crypto/intel/iaa/iaa_crypto_main.c=1033=static int deflate_generic_compress(struct acomp_req *req)\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1034-{\ndrivers/crypto/intel/iaa/iaa_crypto_main.c:1035:\tACOMP_FBREQ_ON_STACK(fbreq, req);\ndrivers/crypto/intel/iaa/iaa_crypto_main.c-1036-\tint ret;\n--\ninclude/crypto/internal/acompress.h-19-\ninclude/crypto/internal/acompress.h:20:#define ACOMP_FBREQ_ON_STACK(name, req) \\\ninclude/crypto/internal/acompress.h-21-        char __##name##_req[sizeof(struct acomp_req) + \\\n"
}

71/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 62121
							output: 37
							thoughts: 29
						

Thoughts:
Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:

**Analyzing `ACOMP_FBREQ_ON_STACK` in `include/crypto/internal/acompress.h`**

Alright, time to get into the weeds. My first thought is to zero in on `ACOMP_FBREQ_ON_STACK` within the `include/crypto/internal/acompress.h` file. I'm immediately curious about what this macro represents. Based on the naming convention and the inclusion location, I'm already forming some preliminary hypotheses: it's likely related to compression and decompression within the kernel's crypto subsystem. The "FB" could stand for "Frame Buffer" or perhaps "Fixed Buffer", but I'll need to dig deeper to confirm. The "REQ" strongly suggests it deals with resource requests or perhaps the characteristics of a compression/decompression operation. Finally, the "ON_STACK" component is particularly intriguing - I'm wondering if this implies memory allocation directly on the stack for the compression process, which can have significant implications for performance and, crucially, for stack size limitations, especially in kernel environments. Given my background, I understand the inherent tradeoffs between stack-based memory and heap-based allocation – stack-based allocation is generally faster but more limited in size and can introduce stack overflow risks. I'll be looking for clues on how the size is managed and any guardrails implemented, as this is something that I always scrutinize when assessing kernel code. I'm now ready to understand its definition and associated usage.



72/3 2026/11/09 05:52 tool read-file
0m Args:
{
  "File": "include/crypto/internal/acompress.h",
  "FirstLine": 18,
  "LineCount": 30
}

Results:
{
  "Contents": "  18:\t#include \u003clinux/workqueue_types.h\u003e\n  19:\t\n  20:\t#define ACOMP_FBREQ_ON_STACK(name, req) \\\n  21:\t        char __##name##_req[sizeof(struct acomp_req) + \\\n  22:\t                            MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR; \\\n  23:\t        struct acomp_req *name = acomp_fbreq_on_stack_init( \\\n  24:\t                __##name##_req, (req))\n  25:\t\n  26:\t/**\n  27:\t * struct acomp_alg - asynchronous compression algorithm\n  28:\t *\n  29:\t * @compress:\tFunction performs a compress operation\n  30:\t * @decompress:\tFunction performs a de-compress operation\n  31:\t * @batch_size:\tMaximum batch-size for batching compress/decompress\n  32:\t *\t\toperations.\n  33:\t * @init:\tInitialize the cryptographic transformation object.\n  34:\t *\t\tThis function is used to initialize the cryptographic\n  35:\t *\t\ttransformation object. This function is called only once at\n  36:\t *\t\tthe instantiation time, right after the transformation context\n  37:\t *\t\twas allocated. In case the cryptographic hardware has some\n  38:\t *\t\tspecial requirements which need to be handled by software, this\n  39:\t *\t\tfunction shall check for the precise requirement of the\n  40:\t *\t\ttransformation and put any software fallbacks in place.\n  41:\t * @exit:\tDeinitialize the cryptographic transformation object. This is a\n  42:\t *\t\tcounterpart to @init, used to remove various changes set in\n  43:\t *\t\t@init.\n  44:\t *\n  45:\t * @base:\tCommon crypto API algorithm data structure\n  46:\t * @calg:\tCmonn algorithm data structure shared with scomp\n  47:\t * @COMP_ALG_COMMON: see struct comp_alg_common\n"
}

73/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 63046
							output: 38
							thoughts: 43
						

Thoughts:
Okay, let's dive into this `acomp_fbreq_on_stack_init` function. I need to understand what this does, specifically looking at lines 235-255 of `include/crypto/internal/acompress.h`. Let's see... I'm guessing this function is related to the initialization of something, perhaps related to frequency-based compression requests. The "on stack" part suggests that it is related to allocations that reside on the stack. I need to confirm my initial thoughts, let me examine the code. I anticipate that this will be where the basic data structures related to such requests are set up. Knowing this function is used internally, I'm expecting some initialization or setup, but not a full-blown request processing function.



74/3 2026/11/09 05:52 tool read-file
0m Args:
{
  "File": "include/crypto/internal/acompress.h",
  "FirstLine": 235,
  "LineCount": 25
}

Results:
{
  "Contents": " 235:\tstatic inline struct acomp_req *acomp_fbreq_on_stack_init(\n 236:\t\tchar *buf, struct acomp_req *old)\n 237:\t{\n 238:\t\tstruct crypto_acomp *tfm = crypto_acomp_reqtfm(old);\n 239:\t\tstruct acomp_req *req = (void *)buf;\n 240:\t\n 241:\t\tcrypto_stack_request_init(\u0026req-\u003ebase,\n 242:\t\t\t\t\t  crypto_acomp_tfm(crypto_acomp_fb(tfm)));\n 243:\t\tacomp_request_set_callback(req, acomp_request_flags(old), NULL, NULL);\n 244:\t\treq-\u003ebase.flags \u0026= ~CRYPTO_ACOMP_REQ_PRIVATE;\n 245:\t\treq-\u003ebase.flags |= old-\u003ebase.flags \u0026 CRYPTO_ACOMP_REQ_PRIVATE;\n 246:\t\treq-\u003esrc = old-\u003esrc;\n 247:\t\treq-\u003edst = old-\u003edst;\n 248:\t\treq-\u003eslen = old-\u003eslen;\n 249:\t\treq-\u003edlen = old-\u003edlen;\n 250:\t\n 251:\t\treturn req;\n 252:\t}\n 253:\t\n 254:\t#endif\n"
}

75/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 63647
							output: 41
							thoughts: 157
						

Thoughts:
Okay, let's break this down.

**Investigating a Potential Memory Footprint Issue in Compression/Decompression**

Alright, I'm diving into this `acomp_fbreq_on_stack_init` function. Line 243, specifically that call to `acomp_request_set_callback`... hang on, *that* call sets `req->unit_size` to *zero*?  Even when we're dealing with `ACOMP_FBREQ_ON_STACK`? That seems... counterintuitive, at least. Why are we zeroing out `unit_size` when it's supposed to represent the unit size that should be compressed? We would not be able to determine the size of the buffer that needs to be allocated, causing a likely crash.

Okay, let's shift gears. I need to understand if this behavior is consistent across other compression algorithms. I should cross-reference this behavior against `qat_comp_algs.c`. I will start by checking the `qat_comp_alg_sw_decompress` and `qat_comp_alg_lz4s_zstd_compress` functions. I will need to understand what this `unit_size` is and if they are properly set.



76/3 2026/11/09 05:52 tool grepper
0m Args:
{
  "Expression": "qat_comp_alg_.*compress",
  "PathPrefixes": [
    "drivers/crypto/intel/qat/qat_common/"
  ]
}

Results:
{
  "Output": "drivers/crypto/intel/qat/qat_common/qat_comp_algs.c=278=static void qat_comp_alg_exit_tfm(struct crypto_acomp *acomp_tfm)\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-285-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:286:static int qat_comp_alg_compress_decompress(struct acomp_req *areq, enum direction dir,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-287-\t\t\t\t\t    unsigned int shdr, unsigned int sftr,\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-349-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:350:static int qat_comp_alg_compress(struct acomp_req *req)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-351-{\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:352:\treturn qat_comp_alg_compress_decompress(req, COMPRESSION, 0, 0, 0, 0);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-353-}\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-354-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:355:static int qat_comp_alg_decompress(struct acomp_req *req)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-356-{\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:357:\treturn qat_comp_alg_compress_decompress(req, DECOMPRESSION, 0, 0, 0, 0);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-358-}\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-359-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:360:static int qat_comp_alg_zstd_decompress(struct acomp_req *req)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-361-{\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-392-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:393:\treturn qat_comp_alg_compress_decompress(req, DECOMPRESSION, 0, 0, 0, 0);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-394-}\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=541=static void qat_comp_alg_zstd_exit_tfm(struct crypto_acomp *acomp_tfm)\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-550-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:551:static int qat_comp_alg_lz4s_zstd_compress(struct acomp_req *req)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-552-{\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-559-\t    req-\u003eslen \u003c= QAT_LZ4S_MAX_OUTPUT_SIZE \u0026\u0026 req-\u003edlen \u003c= QAT_LZ4S_MAX_OUTPUT_SIZE)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:560:\t\treturn qat_comp_alg_compress(req);\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-561-\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-570-\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:571:static int qat_comp_alg_sw_decompress(struct acomp_req *req)\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-572-{\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=587=static struct acomp_alg qat_acomp_deflate[] = { {\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-598-\t.exit = qat_comp_alg_exit_tfm,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:599:\t.compress = qat_comp_alg_compress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:600:\t.decompress = qat_comp_alg_decompress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-601-}};\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=603=static struct acomp_alg qat_acomp_zstd_lz4s = {\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-615-\t.exit = qat_comp_alg_zstd_exit_tfm,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:616:\t.compress = qat_comp_alg_lz4s_zstd_compress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:617:\t.decompress = qat_comp_alg_sw_decompress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-618-};\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c=620=static struct acomp_alg qat_acomp_zstd_native = {\n--\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-632-\t.exit = qat_comp_alg_zstd_exit_tfm,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:633:\t.compress = qat_comp_alg_compress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c:634:\t.decompress = qat_comp_alg_zstd_decompress,\ndrivers/crypto/intel/qat/qat_common/qat_comp_algs.c-635-};\n"
}

77/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 65882
							output: 48
							thoughts: 12
						

Thoughts:
Here's a summary of my current thought process, tailored for an expert audience:

**Analyzing QAT's nreq Allocation**

Okay, so I need to dig into how the QAT (QuickAssist Technology) handles `nreq` allocation. I know the general principles, but I want to get granular. My focus here is going to be on the *exact* mechanism – how it determines the number of requests it can handle concurrently. Are we talking about a fixed buffer size, dynamic allocation based on resource availability, or something more complex? I need to understand if the process is predictable enough for optimal performance tuning, and if there are any potential bottlenecks I should be aware of. I'm especially interested in how this interacts with different workload types and the overall system configuration. Time to start tracing the code and looking for the defining factors that contribute to how this is managed.



78/3 2026/11/09 05:52 tool read-file
0m Args:
{
  "File": "drivers/crypto/intel/qat/qat_common/qat_comp_algs.c",
  "FirstLine": 550,
  "LineCount": 35
}

Results:
{
  "Contents": " 550:\t\n 551:\tstatic int qat_comp_alg_lz4s_zstd_compress(struct acomp_req *req)\n 552:\t{\n 553:\t\tstruct crypto_acomp *acomp_tfm = crypto_acomp_reqtfm(req);\n 554:\t\tstruct qat_compression_ctx *ctx = acomp_tfm_ctx(acomp_tfm);\n 555:\t\tstruct acomp_req *nreq = acomp_request_ctx(req);\n 556:\t\tint ret;\n 557:\t\n 558:\t\tif (req-\u003eslen \u003e= QAT_LZ4S_MIN_INPUT_SIZE \u0026\u0026 req-\u003edlen \u003e= QAT_LZ4S_MIN_INPUT_SIZE \u0026\u0026\n 559:\t\t    req-\u003eslen \u003c= QAT_LZ4S_MAX_OUTPUT_SIZE \u0026\u0026 req-\u003edlen \u003c= QAT_LZ4S_MAX_OUTPUT_SIZE)\n 560:\t\t\treturn qat_comp_alg_compress(req);\n 561:\t\n 562:\t\tmemcpy(nreq, req, sizeof(*req));\n 563:\t\tacomp_request_set_tfm(nreq, ctx-\u003eftfm);\n 564:\t\n 565:\t\tret = crypto_acomp_compress(nreq);\n 566:\t\treq-\u003edlen = nreq-\u003edlen;\n 567:\t\n 568:\t\treturn ret;\n 569:\t}\n 570:\t\n 571:\tstatic int qat_comp_alg_sw_decompress(struct acomp_req *req)\n 572:\t{\n 573:\t\tstruct crypto_acomp *acomp_tfm = crypto_acomp_reqtfm(req);\n 574:\t\tstruct qat_compression_ctx *ctx = acomp_tfm_ctx(acomp_tfm);\n 575:\t\tstruct acomp_req *nreq = acomp_request_ctx(req);\n 576:\t\tint ret;\n 577:\t\n 578:\t\tmemcpy(nreq, req, sizeof(*req));\n 579:\t\tacomp_request_set_tfm(nreq, ctx-\u003eftfm);\n 580:\t\n 581:\t\tret = crypto_acomp_decompress(nreq);\n 582:\t\treq-\u003edlen = nreq-\u003edlen;\n 583:\t\n 584:\t\treturn ret;\n"
}

79/3 2026/11/09 05:52 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 66766
							output: 509
							thoughts: 3814
						

Thoughts:
**Analysis of Memory Safety and KMSAN Applicability**

Okay, let's break this down. My initial assessment focused on the `memcpy` call and the initialization of the `req` structure. Then, I dug into `memcontrol.c` and confirmed the scalar parameters passed to `obj_cgroup_charge_zswap_many` are all initialized and there's no memory corruption potential there. Next, the deep dive into `mm/zswap.c` began. I followed the execution path of `zswap_store`, focusing on the `zswap_store_context` (`zc`) and its interaction with `zswap_store_pages`. I traced the value of `nr_pages` and its use in batching operations, specifically scrutinizing potential out-of-bounds access in the `zc->entries` array, especially in scenarios where the last batch is smaller than `ZSWAP_MAX_BATCH_SIZE`. I confirmed that, because of the use of designated initializers, the other fields are correctly initialized to zero. I carefully went through the flow of `zswap_init_entries`, `zswap_compress`, `zswap_update_entries` and finally `store_pages_failed` to analyze all the loops in there and made sure that there were no potential for memory access out of the bound. Then I checked how `acomp_ctx_free` frees allocated memory in case there are any bugs with allocating memory. I analyzed the impact of failures during buffer allocation in `zswap_cpu_comp_prepare`, specifically focusing on `kcalloc_node` and its potential implications. If any error had occurred, it would have been picked up by KASAN.

Next, I shifted gears to the crucial distinction between KASAN and KMSAN. I considered the conditions that would warrant setting `NeedsKMSAN=true`. I systematically examined the patch against points related to kernel-to-user structure transfers, conditional logic with potentially uninitialized variables, allocation of complex data structures, and how bounds checks might expose uninitialized data.

Let's carefully examine the arguments. I went through all the conditions introduced and modified by the patch to identify possible sources of uninitialized values. Specifically, I went through all conditional statements that involve the various structures in the code. I then looked at how `struct zswap_entry` is initialized, making sure all the fields get set before being used. And then, I analyzed the source of potentially uninitialized bytes in the `zswap_compress` function and also the `zswap_update_entries` function and the usage of the per-CPU buffers (allocated with `kmalloc_node`), and examined how `zs_obj_write` ensures the safety. I concluded that no uninitialized bytes are read or written here.

Based on this, I concluded that the primary risks are not related to uninitialized memory reads or information leaks. Instead, possible risks are standard memory corruption (out-of-bounds access in the `zc->entries` and per-CPU buffers), locking issues, and resource leaks which are all checked by the standard bug detectors.

Therefore, because the patch doesn't introduce any new risks related to uninitialized memory, `NeedsKMSAN` should be `false`. I rechecked the scenarios and edge cases to ensure the analysis is correct, including the usage of `acomp_request_set_callback`, `acomp_save_req`, `acomp_restore_req` and `obj_cgroup_charge_zswap_many`, and also `zswap_entries_cache_alloc_batch`. No issues were found.

Finally, I am confident that the current code is safe and that KMSAN is not required. I am ready to formulate the final response and call `set-results`.



80/3 2026/11/09 05:53 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series introduces batching support for crypto asynchronous compression (acomp) and leverages it in zswap to store pages in batches (bounded by ZSWAP_MAX_BATCH_SIZE).\n\n1. Data structures and initialization:\n- `struct acomp_req` gains a `unit_size` field, which is explicitly initialized to 0 in `acomp_request_set_callback()` and in `acomp_save_req()`, and set by the caller via `acomp_request_set_unit_size()`. When allocated dynamically, `acomp_request_alloc()` uses `kzalloc()`.\n- In `zswap`, `struct zswap_entry` is packed with bitfields. In `zswap_init_entries()`, all fields (`handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, `lru`) are initialized; `length` and `handle` are updated upon successful compression in `zswap_update_entries()`. On failure before completion, only `handle` (initialized to `ERR_PTR(-EINVAL)`) is checked to determine if zsmalloc freeing is necessary before freeing the entries back to the cache. No uninitialized struct fields are read.\n- The per-CPU compression destination buffers `buffers[i]` are written to by the compression backend and read up to `dlen` (the compressed output size) into zsmalloc via `zs_obj_write()`. Incompressible pages read directly from the source folio.\n\n2. Userspace exposure and control flow:\n- No kernel memory or padding is exposed or copied to userspace (no `copy_to_user`, ioctl, netlink, or socket options).\n- No conditional branching depends on uninitialized memory or uninitialized struct padding.\n\n3. KASAN vs KMSAN applicability:\n- Potential risks in this patch series involve buffer indexing (`ZSWAP_MAX_BATCH_SIZE`), scatterlist iteration bounds, memory allocation failure handling, and concurrency/locking around `comp_ctx-\u003eareq.mutex`. These are all covered by KASAN, LOCKDEP, and standard kernel debugging facilities.\n- There are no uninitialized memory usage risks or info-leaks that would require a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series introduces batching support for crypto asynchronous compression (acomp) and leverages it in zswap to store pages in batches (bounded by ZSWAP_MAX_BATCH_SIZE).\n\n1. Data structures and initialization:\n- `struct acomp_req` gains a `unit_size` field, which is explicitly initialized to 0 in `acomp_request_set_callback()` and in `acomp_save_req()`, and set by the caller via `acomp_request_set_unit_size()`. When allocated dynamically, `acomp_request_alloc()` uses `kzalloc()`.\n- In `zswap`, `struct zswap_entry` is packed with bitfields. In `zswap_init_entries()`, all fields (`handle`, `pool_idx`, `swpentry`, `objcg`, `referenced`, `nid`, `lru`) are initialized; `length` and `handle` are updated upon successful compression in `zswap_update_entries()`. On failure before completion, only `handle` (initialized to `ERR_PTR(-EINVAL)`) is checked to determine if zsmalloc freeing is necessary before freeing the entries back to the cache. No uninitialized struct fields are read.\n- The per-CPU compression destination buffers `buffers[i]` are written to by the compression backend and read up to `dlen` (the compressed output size) into zsmalloc via `zs_obj_write()`. Incompressible pages read directly from the source folio.\n\n2. Userspace exposure and control flow:\n- No kernel memory or padding is exposed or copied to userspace (no `copy_to_user`, ioctl, netlink, or socket options).\n- No conditional branching depends on uninitialized memory or uninitialized struct padding.\n\n3. KASAN vs KMSAN applicability:\n- Potential risks in this patch series involve buffer indexing (`ZSWAP_MAX_BATCH_SIZE`), scatterlist iteration bounds, memory allocation failure handling, and concurrency/locking around `comp_ctx-\u003eareq.mutex`. These are all covered by KASAN, LOCKDEP, and standard kernel debugging facilities.\n- There are no uninitialized memory usage risks or info-leaks that would require a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)