Skip to content

Commit 48f7ddf

Browse files
terrellnIngo Molnar
authored andcommitted
init: Add support for zstd compressed kernel
- Add the zstd and zstd22 cmds to scripts/Makefile.lib - Add the HAVE_KERNEL_ZSTD and KERNEL_ZSTD options Architecture specific support is still needed for decompression. Signed-off-by: Nick Terrell <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Tested-by: Sedat Dilek <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4963bb2 commit 48f7ddf

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ KLZOP = lzop
464464
LZMA = lzma
465465
LZ4 = lz4c
466466
XZ = xz
467+
ZSTD = zstd
467468

468469
CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
469470
-Wbitwise -Wno-return-void -Wno-unknown-attribute $(CF)
@@ -512,7 +513,7 @@ CLANG_FLAGS :=
512513
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
513514
export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE LEX YACC AWK INSTALLKERNEL
514515
export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
515-
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ
516+
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
516517
export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
517518

518519
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS

init/Kconfig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ config HAVE_KERNEL_LZO
191191
config HAVE_KERNEL_LZ4
192192
bool
193193

194+
config HAVE_KERNEL_ZSTD
195+
bool
196+
194197
config HAVE_KERNEL_UNCOMPRESSED
195198
bool
196199

197200
choice
198201
prompt "Kernel compression mode"
199202
default KERNEL_GZIP
200-
depends on HAVE_KERNEL_GZIP || HAVE_KERNEL_BZIP2 || HAVE_KERNEL_LZMA || HAVE_KERNEL_XZ || HAVE_KERNEL_LZO || HAVE_KERNEL_LZ4 || HAVE_KERNEL_UNCOMPRESSED
203+
depends on HAVE_KERNEL_GZIP || HAVE_KERNEL_BZIP2 || HAVE_KERNEL_LZMA || HAVE_KERNEL_XZ || HAVE_KERNEL_LZO || HAVE_KERNEL_LZ4 || HAVE_KERNEL_ZSTD || HAVE_KERNEL_UNCOMPRESSED
201204
help
202205
The linux kernel is a kind of self-extracting executable.
203206
Several compression algorithms are available, which differ
@@ -276,6 +279,16 @@ config KERNEL_LZ4
276279
is about 8% bigger than LZO. But the decompression speed is
277280
faster than LZO.
278281

282+
config KERNEL_ZSTD
283+
bool "ZSTD"
284+
depends on HAVE_KERNEL_ZSTD
285+
help
286+
ZSTD is a compression algorithm targeting intermediate compression
287+
with fast decompression speed. It will compress better than GZIP and
288+
decompress around the same speed as LZO, but slower than LZ4. You
289+
will need at least 192 KB RAM or more for booting. The zstd command
290+
line tool is required for compression.
291+
279292
config KERNEL_UNCOMPRESSED
280293
bool "None"
281294
depends on HAVE_KERNEL_UNCOMPRESSED

scripts/Makefile.lib

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,28 @@ quiet_cmd_xzkern = XZKERN $@
413413
quiet_cmd_xzmisc = XZMISC $@
414414
cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
415415

416+
# ZSTD
417+
# ---------------------------------------------------------------------------
418+
# Appends the uncompressed size of the data using size_append. The .zst
419+
# format has the size information available at the beginning of the file too,
420+
# but it's in a more complex format and it's good to avoid changing the part
421+
# of the boot code that reads the uncompressed size.
422+
#
423+
# Note that the bytes added by size_append will make the zstd tool think that
424+
# the file is corrupt. This is expected.
425+
#
426+
# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
427+
# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
428+
# single pass, so zstd doesn't need to allocate a window buffer. When streaming
429+
# decompression is used, like initramfs decompression, zstd22 should likely not
430+
# be used because it would require zstd to allocate a 128 MB buffer.
431+
432+
quiet_cmd_zstd = ZSTD $@
433+
cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@
434+
435+
quiet_cmd_zstd22 = ZSTD22 $@
436+
cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
437+
416438
# ASM offsets
417439
# ---------------------------------------------------------------------------
418440

0 commit comments

Comments
 (0)