[PATCH v3 12/18] lib/lz4: replace lz4_kernel_api.h's prototypes with <linux/lz4.h>
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:35:16 EST
lz4_kernel_api.h carried its own copy of the seventeen public prototypes
because it could not include <linux/lz4.h>, which defined the stream
types that upstream's headers also define.
The types are incomplete now, so it can. Two prototype lists for one
ABI is a drift hazard: the entry points were built against one and their
callers against the other, so no compiler ever saw both, and MODVERSIONS
would not catch a mismatch either, since it hashes the exporter's view
only. Now there is one list.
It also puts the public constants in scope of the entry points, so the
size assertions can name them, and the HC entry points can size states
by lz4hc_mem_compress(), the helper their callers allocate by, with
LZ4HC_MEM_COMPRESS_OPT and LZ4HC_OPT_MIN_CLEVEL static_asserted against
upstream's LZ4_STREAMHC_EXTSIZE and LZ4HC_CLEVEL_OPT_MIN.
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
lib/lz4/lz4_compress.c | 2 +-
lib/lz4/lz4_decompress.c | 2 +-
lib/lz4/lz4_kernel_api.h | 66 ++++--------------------------------------------
lib/lz4/lz4hc_compress.c | 18 +++++--------
4 files changed, 13 insertions(+), 75 deletions(-)
diff --git a/lib/lz4/lz4_compress.c b/lib/lz4/lz4_compress.c
index 941e32a19ac972aef543c9a03b447a8a98bf5215..3e272584ea0c905b48b6bf0b7e77acfbad131bd1 100644
--- a/lib/lz4/lz4_compress.c
+++ b/lib/lz4/lz4_compress.c
@@ -30,7 +30,7 @@
/* Catch any divergence from upstream's layout at build time. */
static_assert(sizeof(LZ4_stream_t) == LZ4_STREAM_MINSIZE);
-static_assert(LZ4_STREAM_MINSIZE == 16416); /* LZ4_MEM_COMPRESS */
+static_assert(LZ4_STREAM_MINSIZE == LZ4_MEM_COMPRESS);
/* LZ4_initStream() returns NULL for a misaligned state, which
* LZ4_compress_fast_extState() dereferences and
diff --git a/lib/lz4/lz4_decompress.c b/lib/lz4/lz4_decompress.c
index 60b57862a2e387aa89fa26f6f8e2b5485b5d9995..5d68a6f59a62ddb3701dd3531cf606c05ef4e607 100644
--- a/lib/lz4/lz4_decompress.c
+++ b/lib/lz4/lz4_decompress.c
@@ -31,7 +31,7 @@
#endif
static_assert(sizeof(LZ4_streamDecode_t) == LZ4_STREAMDECODE_MINSIZE);
-static_assert(LZ4_STREAMDECODE_MINSIZE == 32); /* LZ4_MEM_DECOMPRESS */
+static_assert(LZ4_STREAMDECODE_MINSIZE == LZ4_MEM_DECOMPRESS);
int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
int maxDecompressedSize)
diff --git a/lib/lz4/lz4_kernel_api.h b/lib/lz4/lz4_kernel_api.h
index a36ecdcebac5fe6efc3fc1758b7e503a04814772..e748d773aa665b988ada92219a837d544beafe61 100644
--- a/lib/lz4/lz4_kernel_api.h
+++ b/lib/lz4/lz4_kernel_api.h
@@ -3,20 +3,11 @@
* Copyright (c) 2026 Samsung Electronics Co., Ltd.
* Author: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
*
- * lz4_kernel_api.h -- the LZ4 API this directory exports
+ * lz4_kernel_api.h -- hand the LZ4 names back to the kernel
*
- * lz4_deps.h renames upstream's entry points to __lz4_* so that the kernel's
- * own definitions can use the real names; this header undoes that renaming,
- * the optimize() override and the #undef of current, and declares what
- * lib/lz4 exports.
- *
- * The same functions are declared to callers in <linux/lz4.h>, in terms of
- * opaque stream types. We cannot include that header here -- it and
- * upstream's lz4.h describe the same library and collide -- so the
- * declarations are repeated, expressed in upstream's own types. Keep the two
- * in step; <linux/lz4.h> is the one callers compile against.
- *
- * Include after upstream/lz4.c or upstream/lz4hc.c.
+ * Undo lz4_deps.h's renaming, its optimize() override and its #undef of
+ * current, and pull in <linux/lz4.h>. Include after upstream/lz4.c or
+ * upstream/lz4hc.c.
*/
#undef LZ4_compress_fast
@@ -42,51 +33,4 @@
#undef optimize
#pragma pop_macro("current")
-/*
- * The objects that do not build lz4hc.c never see this type, and it is only
- * ever used through a pointer here. Upstream declares it the same way.
- */
-typedef union LZ4_streamHC_u LZ4_streamHC_t;
-
-/* Compression. wrkmem is LZ4_MEM_COMPRESS bytes, supplied by the caller. */
-int LZ4_compress_default(const char *source, char *dest, int inputSize,
- int maxOutputSize, void *wrkmem);
-int LZ4_compress_fast(const char *source, char *dest, int inputSize,
- int maxOutputSize, int acceleration, void *wrkmem);
-int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr,
- int targetDestSize, void *wrkmem);
-
-/* Streaming compression. */
-void LZ4_resetStream(LZ4_stream_t *LZ4_stream);
-int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary,
- int dictSize);
-int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize);
-int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src,
- char *dst, int srcSize, int maxDstSize,
- int acceleration);
-
-/* Decompression. */
-int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
- int maxDecompressedSize);
-int LZ4_decompress_safe_partial(const char *source, char *dest,
- int compressedSize, int targetOutputSize,
- int maxDecompressedSize);
-int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
- const char *dictionary, int dictSize);
-int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
- const char *source, char *dest,
- int compressedSize, int maxDecompressedSize);
-int LZ4_decompress_safe_usingDict(const char *source, char *dest,
- int compressedSize, int maxDecompressedSize,
- const char *dictStart, int dictSize);
-
-/* HC compression. wrkmem is LZ4HC_MEM_COMPRESS bytes. */
-int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
- int compressionLevel, void *wrkmem);
-void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel);
-int LZ4_loadDictHC(LZ4_streamHC_t *streamHCPtr, const char *dictionary,
- int dictSize);
-int LZ4_compress_HC_continue(LZ4_streamHC_t *streamHCPtr, const char *src,
- char *dst, int srcSize, int maxDstSize);
-int LZ4_saveDictHC(LZ4_streamHC_t *streamHCPtr, char *safeBuffer,
- int maxDictSize);
+#include <linux/lz4.h>
diff --git a/lib/lz4/lz4hc_compress.c b/lib/lz4/lz4hc_compress.c
index d16b937b2129f0b4d8e1917f6f7ded4af663b44f..9165f0d7b5a745566b18072e3615274053e2ad24 100644
--- a/lib/lz4/lz4hc_compress.c
+++ b/lib/lz4/lz4hc_compress.c
@@ -10,7 +10,7 @@
* The compressor is upstream's, in the verbatim lz4hc.c included below.
* LZ4_compress_HC() takes the kernel's trailing wrkmem argument and forwards
* to LZ4_compress_HC_extStateHC_fastReset(); the level-taking entry points
- * initialise the state at the size its level needs (see lz4hc_state_size()).
+ * initialise the state at the size its level needs, lz4hc_mem_compress().
*/
#include "lz4_deps.h"
@@ -39,8 +39,8 @@ static int LZ4_compressBound(int isize)
/* Catch any divergence from upstream's layout at build time. */
static_assert(sizeof(LZ4_streamHC_t) == LZ4_STREAMHC_MINSIZE);
-static_assert(LZ4_STREAMHC_MINSIZE == 262200); /* LZ4HC_MEM_COMPRESS */
-static_assert(LZ4_STREAMHC_EXTSIZE == 327784); /* LZ4HC_MEM_COMPRESS_OPT */
+static_assert(LZ4_STREAMHC_MINSIZE == LZ4HC_MEM_COMPRESS);
+static_assert(LZ4_STREAMHC_EXTSIZE == LZ4HC_MEM_COMPRESS_OPT);
/* Levels >= LZ4HC_CLEVEL_OPT_MIN (10) use the optimal parser, which needs
* a ~64K workspace: too big for a kernel stack, and the heap mode lz4_deps.h
@@ -51,13 +51,7 @@ static_assert(LZ4_STREAMHC_EXTSIZE == 327784); /* LZ4HC_MEM_COMPRESS_OPT */
* weaker than the shallow hash chain the fork used for them, and levels
* above 12 compress as 12.
*/
-static size_t lz4hc_state_size(int compressionLevel)
-{
- if (compressionLevel >= LZ4HC_CLEVEL_OPT_MIN)
- return LZ4_STREAMHC_EXTSIZE;
-
- return sizeof(LZ4_streamHC_t);
-}
+static_assert(LZ4HC_OPT_MIN_CLEVEL == LZ4HC_CLEVEL_OPT_MIN);
/* LZ4_initStreamHC() returns NULL for a misaligned state, and leaves it
* uninitialised. Warn, and fail the call or skip the reset.
@@ -75,7 +69,7 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
if (lz4hc_state_misaligned(wrkmem))
return 0;
- state = LZ4_initStreamHC(wrkmem, lz4hc_state_size(compressionLevel));
+ state = LZ4_initStreamHC(wrkmem, lz4hc_mem_compress(compressionLevel));
if (!state)
return 0;
@@ -90,7 +84,7 @@ void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel)
if (lz4hc_state_misaligned(streamHCPtr))
return;
- if (LZ4_initStreamHC(streamHCPtr, lz4hc_state_size(compressionLevel)))
+ if (LZ4_initStreamHC(streamHCPtr, lz4hc_mem_compress(compressionLevel)))
LZ4_setCompressionLevel(streamHCPtr, compressionLevel);
}
EXPORT_SYMBOL(LZ4_resetStreamHC);
--
2.34.1