[PATCH v3 00/18] lib/lz4: stop forking upstream LZ4, vendor it instead
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:33:58 EST
The in-kernel LZ4 is a fork. The decompressor was last synced with
upstream v1.8.3 in 2018 and the compressor with v1.7.3 in 2017, both by
hand. Upstream has made over 600 commits against lib/ since, and the gap
is maintained one cherry-pick at a time.
That has left real bugs in place: for example, the forked
LZ4_decompress_fast() has no bounds checks on its input, so corrupted
input reads past its end and before the output buffer.
This series vendors the upstream sources unmodified and adapts them at
build time, so a re-sync becomes a directory copy:
upstream/lz4.c, upstream/lz4.h verbatim upstream sources
upstream/lz4hc.c, upstream/lz4hc.h
lz4_deps.h the build environment
lz4_kernel_api.h hands the LZ4 names back
freestanding/ ISO C headers -nostdinc drops
lz4_compress.c the kernel's entry points
lz4_decompress.c
lz4hc_compress.c
Patches 1-5 prepare the fork: the pre-boot decompressor moves off
LZ4_decompress_fast(), which then goes, and zram and f2fs size their
streams by the library's constants and the new lz4hc_mem_compress()
rather than sizeof(). Patches 6-12 import the sources and switch the
compressor, HC compressor and decompressor over one at a time; patch 11
also widens the in-place margin erofs relies on. Patch 13 has zram
compress lz4hc levels above 9 as level 9, patch 14 (S. Biplab Raut)
adds KUnit tests, and patch 15 gives lib/lz4 its first MAINTAINERS
entry.
Patches 16 and 17 take two lz4hc changes I have filed upstream as lz4
pull requests #2216 and #2217, not merged yet, and patch 18 has
lib/lz4 use the second to leave out lz4hc pattern analysis. With them,
lz4hc in zram is faster than today at levels 6 and 9 on program
memory; level 3 stays 0.5-2% slower. They come last so they can be
taken or left on their own: patches 1-15 do not depend on them, and
they go away at the next re-sync to a release that has them.
This is meant to go through Andrew's tree and is based on linux-next,
which carries the zram rework the lz4 backends saw this cycle; patch 8
needs acks from the boot maintainers, and patch 5 from f2fs.
The exported API changes in four ways.
The three stream types become incomplete. <linux/lz4.h> spelled out
their layout for the fork's benefit, upstream headers define the same
types and lib/decompress_unlz4.c ends up with both in one translation
unit. Callers allocate LZ4_MEM_COMPRESS, lz4hc_mem_compress(level) or
LZ4_MEM_DECOMPRESS bytes instead of sizeof(), as every caller but zram
already did. Each conversion patch makes its own type incomplete so no
step leaves the tree broken.
Sixteen macros go with them: the hash geometry (LZ4_HASHLOG,
LZ4_HASHTABLESIZE, LZ4_HASH_SIZE_U32, LZ4HC_DICTIONARY_LOGSIZE,
LZ4HC_MAXD, LZ4HC_MAXD_MASK, LZ4HC_HASH_LOG, LZ4HC_HASHTABLESIZE,
LZ4HC_HASH_MASK) and the sizes derived from it (LZ4_MEMORY_USAGE,
LZ4_STREAMSIZE{,_U64}, LZ4_STREAMHCSIZE{,_SIZET},
LZ4_STREAMDECODESIZE{,_U64}). They existed to lay out structures the
fork defined here; none has an in-tree user outside lib/lz4.
LZ4_compressBound() goes too: a static inline whose only caller was
lib/decompress_unlz4.c, now using the LZ4_COMPRESSBOUND() macro.
LZ4_MEM_DECOMPRESS, LZ4HC_MEM_COMPRESS_OPT, LZ4HC_OPT_MIN_CLEVEL and
lz4hc_mem_compress() are new.
LZ4_decompress_fast(), LZ4_decompress_fast_continue() and
LZ4_decompress_fast_usingDict() go. Upstream has deprecated them since
v1.9, and their last in-tree user, the pre-boot decompressor, now uses
LZ4_decompress_safe().
Three behaviour changes come from upstream. I have kept them rather
than patch the vendored sources, which is the divergence this series
removes.
- LZ4HC levels 10 to 12 select upstream's optimal parser, and levels
above 12 compress as 12. The fork had no optimal parser - level only
scaled search depth - and its levels 10-16 compressed within 0.2% of
its level 9, 0.05% on 4K pages. The parser's ~64K workspace follows
the state (LZ4_STREAMHC_EXTSIZE), so callers allocate
lz4hc_mem_compress(level) bytes: 64K more than LZ4HC_MEM_COMPRESS
from level 10 up, nothing more below. A caller that still allocates
LZ4HC_MEM_COMPRESS and asks for level 10 or more now writes past its
state; the in-tree ones allocate by lz4hc_mem_compress() or, like
crypto's lz4hc, compress at level 9, and patch 14's tests catch it
under KASAN. The parser suits data compressed once and read many
times. Level 12 against the fork's level 16, in independent blocks,
library level with the kernel's code generation flags:
binary kernel source
block size speed size speed
4K -0.07% -80% -0.29% -54%
16K -0.10% -80% -0.45% -44%
64K -0.14% -79% -0.63% -33%
256K -0.18% -78% -0.73% -21%
Level 10 is seldom worth it: on the binary from 64K up it is 1%
larger than level 9, in upstream's own lz4 tool too. f2fs, which
compresses clusters of 16K and more, gets these levels as upstream
has them. zram compresses single pages, so patch 13 has it compress
levels above 9 as level 9 and log it, recompression included, so
existing zram settings do not drop to a quarter or half of their
speed.
- LZ4_decompress_safe_partial() with a zero-length target returns 0
rather than -1, matching upstream since 725cb0aafdf7. No in-tree
caller depends on it.
- The compressors require wrkmem aligned to 8 bytes. Upstream checks
this and LZ4_initStream() returns NULL, which upstream's own callers
then dereference or ignore. The entry points check the alignment
themselves, warn once and fail the call. Every in-tree caller uses
kmalloc() or vmalloc() and is already aligned; <linux/lz4.h> now
documents the requirement.
The layout follows lib/zstd, except that LZ4 has no upstream import
tool or deps header, so the glue lives outside the vendored files.
The import is the v1.11.1 release tag, unmodified. v1.11.0 contains the
three upstream commits v2 backported on top of v1.10.0, and v1.11.1
fixes LZ4_resetStreamHC() on uninitialised memory and turns on the fast
decode loop on ppc64.
checkpatch reports about 3400 errors against patch 6, and a few against
patches 16 and 17: upstream's coding style, and no SPDX tags, in files
kept verbatim.
Measured in the kernel: three kernels differing only in lib/lz4, booted
one core under KVM, each reading 1.3GB of kernel source from an LZ4
erofs image with the page cache dropped before every pass, median of
140 passes on an idle machine. The middle column is upstream v1.11.1
with LZ4_FAST_DEC_LOOP=0, to separate the version bump from the decode
loop upstream added in v1.9.0.
erofs read, MB/s
erofs cluster today v1.11.1 + fast loop net
4K 2131 2155 2356 +11%
64K 2602 2627 2869 +10%
The same three decoders at the library level, each block decoded on
its own as zram does a page: x86_64, the kernel's code generation flags
(no SSE), median of 21 interleaved runs, on upstream's datagen with a
fixed seed (byte-identical anywhere) and on a tar of kernel sources:
decompression, MB/s
corpus block ratio today v1.11.1 + fast loop net
datagen -s0 -P60 4K 1.09x 4451 4446 5660 +27%
16K 1.34x 3835 3825 5022 +31%
64K 1.90x 4620 4651 5617 +22%
256K 1.92x 6665 6767 8160 +22%
kernel source tar 4K 2.31x 3616 3527 4136 +14%
16K 2.60x 3641 3569 4176 +15%
64K 2.85x 3640 3570 4097 +13%
256K 2.98x 4022 4078 4741 +18%
Compression is slower in places without patches 16-18. Measured
in zram under KVM on the anonymous memory of four programs (setup in
the section at the end), write speed and compressed size against
today's kernel, with patches 1-15:
lz4 lz4hc:3 lz4hc:9 lz4hc:9
speed size 16K dict
Firefox +0.5% -1.47% -6.0% -1.0% -32.2%
Chrome +1.6% -0.43% -6.1% +2.5% -31.4%
gcc (cc1) +1.1% -0.43% -6.1% -2.5% -33.8%
Python +0.1% -0.44% -5.0% -5.8% -40.4%
lz4 is within 2% of today and compresses up to 1.5% smaller; on
synthetic data it is within 3%. lz4hc is slower at most levels, up to
40% at zram's default level 9 with a dictionary, and compresses up to
0.3% smaller. That is upstream's own behaviour; the section at the end
explains it. With all 18 patches, the same test gives:
lz4 lz4hc:3 lz4hc:9 lz4hc:9
speed size 16K dict
Firefox -0.1% -1.47% -1.3% +10.9% +10.4%
Chrome +1.1% -0.43% -0.7% +10.8% +8.2%
gcc (cc1) +1.8% -0.43% -1.1% +14.4% +12.5%
Python +0.2% -0.44% -0.5% +12.1% +9.0%
lz4hc's size is then within 0.05% of today's. Level 3 stays 0.5-1.3%
slower, and on the binary and synthetic inputs of the section at the
end levels 3 to 9 stay up to 5% slower. Patches 9 and 10 have
library-level numbers.
The cost is size: lib/lz4 grows from 45K to 89K of text on x86_64,
the compressors most (lz4 21K to 40K, lz4hc 12K to 36K). That is
upstream code the fork never had.
========================================================================
Background: lz4hc on small blocks, and the upstream changes
========================================================================
This explains the lz4hc numbers above and patches 16-18. The table
below is an earlier run, with lz4hc.c from earlier revisions of the
pull requests; its v1.11.1 rows differ from the tables above by up to
3%, run-to-run spread on another day.
The time goes to LZ4HC_InsertAndGetWiderMatch(), which every lz4hc
level runs. Since the fork it has gained a separate maximum match
length (upstream 502b02b44630), an external dictionary branch and the
level 9+ pattern analysis, and it now keeps more values live than
x86_64 has registers; perf shows no extra cache or branch misses. I
have filed two pull requests against upstream's dev branch:
1. https://github.com/lz4/lz4/pull/2216
Check the maximum match length on pointers the search already
holds, and build the hash chain search a second time without the
external dictionary branch, for input without a dictionary. Same
output, no opt-in.
2. https://github.com/lz4/lz4/pull/2217
LZ4HC_PATTERN_ANALYSIS=0 leaves pattern analysis out. The default
builds the same code as today.
zram write speed against today's kernel: one core under KVM, 20 passes
in each of 5 boots per kernel, kernels interleaved, median of the
per-boot ratios. The data is the anonymous memory of four programs
started fresh (Firefox and Chrome headless on local pages, cc1
compiling kernel/sched/core.c, Python running scripts/kernel-doc),
same-filled pages dropped, 27-32MB each, plus four synthetic inputs.
"1" and "1+2" are v1.11.1 with those changes:
no dictionary 16K dictionary
lz4hc:3 :6 :9 lz4hc:3 :6 :9
Firefox v1.11.1 -8% -6% -4% -8% -6% -34%
1 -2% -2% 0% -8% -6% -33%
1+2 -3% +1% +10% -2% 0% +10%
Chrome v1.11.1 -7% -4% 0% -7% -4% -34%
1 -1% +1% +4% -7% -4% -33%
1+2 -1% +2% +10% -1% 0% +8%
cc1 v1.11.1 -7% -4% -5% -7% -4% -36%
1 -2% -1% -3% -7% -5% -35%
1+2 -2% +2% +13% -2% +1% +12%
Python v1.11.1 -7% -4% -8% -7% -5% -42%
1 -2% 0% -6% -7% -5% -41%
1+2 -2% +3% +11% -1% +2% +9%
binary v1.11.1 -9% -7% -14% -8% -7% -26%
1 -2% -1% -9% -9% -7% -25%
1+2 -3% -1% -1% -3% -3% -1%
kernel src v1.11.1 -7% -5% -1% -7% -4% -8%
1 -1% +1% +4% -7% -4% -8%
1+2 -3% +1% +5% -1% +1% +8%
datagen v1.11.1 -14% -15% -15% -12% -12% -11%
1 +3% +3% +3% -12% -11% -11%
1+2 -5% -5% -5% -4% -4% -4%
byte runs v1.11.1 -3% -2% +184% -2% 0% +254%
1 -2% -2% +188% -1% 0% +258%
1+2 -2% 0% +39% -1% +2% +41%
Upstream dev unchanged measures as v1.11.1 within 1% on the synthetic
inputs. 1 does not touch the dictionary path. Without pattern analysis,
byte runs at level 9 drop from +184% to +39%, and level 9 output on the
program memory grows by up to 0.3%; zram never compresses pages of a
single value. Patch 18 sets the macro in lz4_deps.h, so a re-sync to a
release with the changes keeps it, and lz4hc stays at 36K. In upstream's
own -O3 build, 1 adds 13-21K to lz4hc.o and 2 removes 11-16K; the pull
requests have upstream's benchmark numbers.
---
Changes in v3:
- Rebase on linux-next (next-20261201), which carries this cycle's
rework of zram's lz4 backends (Sergey)
- Patch 8 also covers alpha's new pre-boot decompressor stub
- Import upstream v1.11.1 instead of v1.10.0; the three backports are
in the release, so patches 5-7 of v2 go
- Drop v2's patch 3: linux-next's zram resets the lz4hc dictionary
stream once in setup_params (Sergey)
- New patch 4 has zram size its streams by LZ4_MEM_COMPRESS and
lz4hc_mem_compress(), so the lib/lz4 patches no longer touch
drivers/block/zram (Sergey)
- Keep LZ4HC levels 10-12 as upstream has them rather than clamping
them to 9: new patch 3 adds lz4hc_mem_compress(), patches 4 and new 5
have zram and f2fs size their lz4hc states by it
- New patch 13: zram compresses lz4hc levels above 9 as level 9, since
on a page the optimal parser takes 2-5 times as long for under 0.3%
smaller output
- New patch 14: KUnit tests for lib/lz4 (S. Biplab Raut), with the HC
level test sizing its work memory by lz4hc_mem_compress()
- Fold the in-place margin into the decompressor switch (Gao Xiang),
and widen it to upstream dev's (compressedSize >> 8) + 65 (lz4 PR
#2104) for v1.11's 64-byte match copies; +65 is upstream's bound,
where an earlier reply in the v2 thread said +68
- Patch 1 decodes with LZ4_decompress_safe() rather than the partial
variant, and rejects an image that ends before its stated size
- Restore the current macro after the vendored sources: fixes the arc
allmodconfig build (kernel test robot)
- Predefine LZ4_FORCE_NOINLINE, new in v1.11, which the kernel's
noinline macro otherwise expands inside
- Remeasure everything with v1.11.1
- Measure compression in zram; explain the lz4hc slowdown on small
blocks and the upstream changes that would fix it, in a separate
section at the end
- New patches 16-18, optional and last: take the two lz4hc changes
filed upstream as lz4 pull requests #2216 and #2217, and leave out
pattern analysis, so lz4hc in zram is faster than today at levels 6
and 9 on program memory instead of slower
- Link to v2: https://lore.kernel.org/r/20261106-lz4-vendor-upstream-v2-0-ee04c3f0d7dc@xxxxxxxxxxx
Changes in v2:
- Drop the RFC prefix
- New patches 1-2: move the pre-boot decompressor to
LZ4_decompress_safe_partial() and remove LZ4_decompress_fast() and its
variants from the fork, ahead of the import (Sergey)
- New patch 3: zram's lz4hc backend resets its stream once, not per page
- New patches 6-7: backport upstream's arm64 fast decode loop (Sergey)
and its match length limit fix
- New patch 12: widen LZ4_DECOMPRESS_INPLACE_MARGIN for the fast decode
loop
- Compressor entry points check wrkmem alignment, warn and fail (Sergey)
- Patch 13 names the upstream counterpart of each retired local patch
(Sergey)
- MAINTAINERS: also cover lib/decompress_unlz4.c and
include/linux/decompress/unlz4.h (Eric)
- Remeasure erofs reads with more runs on an idle machine
- Link to v1: https://lore.kernel.org/r/20260925-lz4-vendor-upstream-v1-0-1c7ffbe21c4b@xxxxxxxxxxx
---
Michal Wilczynski (17):
lib/decompress_unlz4: use LZ4_decompress_safe() before boot
lib/lz4: remove LZ4_decompress_fast() and its variants
lib/lz4: add lz4hc_mem_compress()
zram: lz4/lz4hc: size the streams by the library's constants
f2fs: size the lz4hc context by the compression level
lib/lz4: import upstream LZ4 sources verbatim
lib/lz4: add the build environment for the vendored sources
arch: boot: put the LZ4 freestanding headers on the decompressor path
lib/lz4: switch the compressor to the vendored sources
lib/lz4: switch the HC compressor to the vendored sources
lib/lz4: switch the decompressor to the vendored sources
lib/lz4: replace lz4_kernel_api.h's prototypes with <linux/lz4.h>
zram: lz4hc: compress levels above 9 as level 9
MAINTAINERS: add an entry for the LZ4 compression library
lib/lz4: take lz4 PR #2216's lz4hc hash chain search speedup
lib/lz4: take lz4 PR #2217's LZ4HC_PATTERN_ANALYSIS build macro
lib/lz4: leave out lz4hc pattern analysis
S. Biplab Raut (1):
lib/lz4: add KUnit tests for compression and decompression
Documentation/admin-guide/blockdev/zram.rst | 4 +
Documentation/filesystems/f2fs.rst | 2 +
MAINTAINERS | 10 +
arch/alpha/boot/compressed/Makefile | 3 +
arch/arm/boot/compressed/Makefile | 3 +
arch/mips/boot/compressed/Makefile | 4 +
arch/parisc/boot/compressed/Makefile | 3 +
arch/s390/boot/Makefile | 3 +
arch/x86/boot/compressed/Makefile | 3 +
drivers/block/zram/backend_lz4.c | 6 +-
drivers/block/zram/backend_lz4hc.c | 23 +-
fs/f2fs/compress.c | 2 +-
include/linux/lz4.h | 265 +--
lib/Kconfig | 15 +
lib/decompress_unlz4.c | 22 +-
lib/lz4/.kunitconfig | 5 +
lib/lz4/Makefile | 17 +-
lib/lz4/freestanding/limits.h | 14 +
lib/lz4/freestanding/stddef.h | 15 +
lib/lz4/freestanding/stdint.h | 14 +
lib/lz4/freestanding/string.h | 14 +
lib/lz4/lz4_compress.c | 946 +--------
lib/lz4/lz4_decompress.c | 724 +------
lib/lz4/lz4_deps.h | 133 ++
lib/lz4/lz4_kernel_api.h | 36 +
lib/lz4/lz4defs.h | 247 ---
lib/lz4/lz4hc_compress.c | 799 +-------
lib/lz4/tests/Makefile | 2 +
lib/lz4/tests/lz4_kunit.c | 912 +++++++++
lib/lz4/upstream/lz4.c | 2955 +++++++++++++++++++++++++++
lib/lz4/upstream/lz4.h | 871 ++++++++
lib/lz4/upstream/lz4hc.c | 2356 +++++++++++++++++++++
lib/lz4/upstream/lz4hc.h | 402 ++++
33 files changed, 8078 insertions(+), 2752 deletions(-)
---
base-commit: d138596e9f822033490b2e577ec2ea64f2aa246c
change-id: 20260904-lz4-vendor-upstream-4bc2202d0ffc
Best regards,
--
Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>