[PATCH v3 04/18] zram: lz4/lz4hc: size the streams by the library's constants
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:34:24 EST
<linux/lz4.h> gives the size of a compression stream as
LZ4_MEM_COMPRESS and LZ4HC_MEM_COMPRESS, and lz4_create() and
lz4hc_create() allocate by them. The dictionary streams and the per-page
memcpy() use sizeof() instead, and BUILD_BUG_ON()s tie the two together.
Use the constants throughout. The vendored library that replaces the
fork later in this series declares LZ4_stream_t and LZ4_streamHC_t as
incomplete types, as upstream does, so sizeof() stops compiling; the
library asserts the constants against upstream's layout at build time
instead.
lz4hc allocates its streams by lz4hc_mem_compress(params->level): with
the vendored library, levels 10 and up need a workspace after the state.
The per-page memcpy() keeps copying LZ4HC_MEM_COMPRESS bytes, the state
alone, since the workspace holds nothing between calls.
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
drivers/block/zram/backend_lz4.c | 6 ++----
drivers/block/zram/backend_lz4hc.c | 13 +++++++------
2 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c
index bad648c158da5011c1fb1af450139d3ed599294b..11c8a9c3b6f66c80241b1b99c5c1056bf97b8a51 100644
--- a/drivers/block/zram/backend_lz4.c
+++ b/drivers/block/zram/backend_lz4.c
@@ -35,7 +35,7 @@ static int lz4_setup_params(struct zcomp_params *params)
if (!params->dict || !params->dict_sz)
return 0;
- dict_stream = vzalloc(sizeof(*dict_stream));
+ dict_stream = vzalloc(LZ4_MEM_COMPRESS);
if (!dict_stream)
return -ENOMEM;
@@ -57,8 +57,6 @@ static void lz4_destroy(struct zcomp_ctx *ctx)
static int lz4_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
{
- BUILD_BUG_ON(sizeof(LZ4_stream_t) != LZ4_MEM_COMPRESS);
-
ctx->context = vmalloc(LZ4_MEM_COMPRESS);
if (!ctx->context)
return -ENOMEM;
@@ -76,7 +74,7 @@ static int lz4_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
ctx->context);
} else {
/* Cstrm needs to be reset */
- memcpy(ctx->context, params->drv_data, sizeof(LZ4_stream_t));
+ memcpy(ctx->context, params->drv_data, LZ4_MEM_COMPRESS);
ret = LZ4_compress_fast_continue(ctx->context, req->src,
req->dst, req->src_len,
req->dst_len, params->level);
diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c
index 5825c8ba78dac0faa42a8adfa5dabe6362d7240e..0d86bc4091c1fa167168d14f61fec253165d009c 100644
--- a/drivers/block/zram/backend_lz4hc.c
+++ b/drivers/block/zram/backend_lz4hc.c
@@ -41,7 +41,7 @@ static int lz4hc_setup_params(struct zcomp_params *params)
if (!params->dict || !params->dict_sz)
return 0;
- dict_stream = vzalloc(sizeof(*dict_stream));
+ dict_stream = vzalloc(lz4hc_mem_compress(params->level));
if (!dict_stream)
return -ENOMEM;
@@ -64,9 +64,7 @@ static void lz4hc_destroy(struct zcomp_ctx *ctx)
static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
{
- BUILD_BUG_ON(sizeof(LZ4_streamHC_t) != LZ4HC_MEM_COMPRESS);
-
- ctx->context = vmalloc(LZ4HC_MEM_COMPRESS);
+ ctx->context = vmalloc(lz4hc_mem_compress(params->level));
if (!ctx->context)
return -ENOMEM;
return 0;
@@ -82,8 +80,11 @@ static int lz4hc_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
req->dst_len, params->level,
ctx->context);
} else {
- /* Cstrm needs to be reset */
- memcpy(ctx->context, params->drv_data, sizeof(LZ4_streamHC_t));
+ /*
+ * Cstrm needs to be reset. Copy the state only: a
+ * workspace after it holds nothing between calls.
+ */
+ memcpy(ctx->context, params->drv_data, LZ4HC_MEM_COMPRESS);
ret = LZ4_compress_HC_continue(ctx->context, req->src, req->dst,
req->src_len, req->dst_len);
}
--
2.34.1