Skip to content

Commit fb46d05

Browse files
terrellnIngo Molnar
authored andcommitted
x86: Add support for ZSTD compressed kernel
- Add support for zstd compressed kernel - Define __DISABLE_EXPORTS in Makefile - Remove __DISABLE_EXPORTS definition from kaslr.c - Bump the heap size for zstd. - Update the documentation. Integrates the ZSTD decompression code to the x86 pre-boot code. Zstandard requires slightly more memory during the kernel decompression on x86 (192 KB vs 64 KB), and the memory usage is independent of the window size. __DISABLE_EXPORTS is now defined in the Makefile, which covers both the existing use in kaslr.c, and the use needed by the zstd decompressor in misc.c. This patch has been boot tested with both a zstd and gzip compressed kernel on i386 and x86_64 using buildroot and QEMU. Additionally, this has been tested in production on x86_64 devices. We saw a 2 second boot time reduction by switching kernel compression from xz to zstd. 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 0fe4f4e commit fb46d05

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

Documentation/x86/boot.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,9 @@ Protocol: 2.08+
782782
uncompressed data should be determined using the standard magic
783783
numbers. The currently supported compression formats are gzip
784784
(magic numbers 1F 8B or 1F 9E), bzip2 (magic number 42 5A), LZMA
785-
(magic number 5D 00), XZ (magic number FD 37), and LZ4 (magic number
786-
02 21). The uncompressed payload is currently always ELF (magic
787-
number 7F 45 4C 46).
785+
(magic number 5D 00), XZ (magic number FD 37), LZ4 (magic number
786+
02 21) and ZSTD (magic number 28 B5). The uncompressed payload is
787+
currently always ELF (magic number 7F 45 4C 46).
788788

789789
============ ==============
790790
Field name: payload_length

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ config X86
188188
select HAVE_KERNEL_LZMA
189189
select HAVE_KERNEL_LZO
190190
select HAVE_KERNEL_XZ
191+
select HAVE_KERNEL_ZSTD
191192
select HAVE_KPROBES
192193
select HAVE_KPROBES_ON_FTRACE
193194
select HAVE_FUNCTION_ERROR_INJECTION

arch/x86/boot/compressed/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ OBJECT_FILES_NON_STANDARD := y
2626
KCOV_INSTRUMENT := n
2727

2828
targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \
29-
vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4
29+
vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4 vmlinux.bin.zst
3030

3131
KBUILD_CFLAGS := -m$(BITS) -O2
3232
KBUILD_CFLAGS += -fno-strict-aliasing $(call cc-option, -fPIE, -fPIC)
@@ -42,6 +42,7 @@ KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
4242
KBUILD_CFLAGS += -Wno-pointer-sign
4343
KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
4444
KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
45+
KBUILD_CFLAGS += -D__DISABLE_EXPORTS
4546

4647
KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__
4748
GCOV_PROFILE := n
@@ -145,13 +146,16 @@ $(obj)/vmlinux.bin.lzo: $(vmlinux.bin.all-y) FORCE
145146
$(call if_changed,lzo)
146147
$(obj)/vmlinux.bin.lz4: $(vmlinux.bin.all-y) FORCE
147148
$(call if_changed,lz4)
149+
$(obj)/vmlinux.bin.zst: $(vmlinux.bin.all-y) FORCE
150+
$(call if_changed,zstd22)
148151

149152
suffix-$(CONFIG_KERNEL_GZIP) := gz
150153
suffix-$(CONFIG_KERNEL_BZIP2) := bz2
151154
suffix-$(CONFIG_KERNEL_LZMA) := lzma
152155
suffix-$(CONFIG_KERNEL_XZ) := xz
153156
suffix-$(CONFIG_KERNEL_LZO) := lzo
154157
suffix-$(CONFIG_KERNEL_LZ4) := lz4
158+
suffix-$(CONFIG_KERNEL_ZSTD) := zst
155159

156160
quiet_cmd_mkpiggy = MKPIGGY $@
157161
cmd_mkpiggy = $(obj)/mkpiggy $< > $@

arch/x86/boot/compressed/kaslr.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
*/
2020
#define BOOT_CTYPE_H
2121

22-
/*
23-
* _ctype[] in lib/ctype.c is needed by isspace() of linux/ctype.h.
24-
* While both lib/ctype.c and lib/cmdline.c will bring EXPORT_SYMBOL
25-
* which is meaningless and will cause compiling error in some cases.
26-
*/
27-
#define __DISABLE_EXPORTS
28-
2922
#include "misc.h"
3023
#include "error.h"
3124
#include "../string.h"

arch/x86/boot/compressed/misc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ static int lines, cols;
7777
#ifdef CONFIG_KERNEL_LZ4
7878
#include "../../../../lib/decompress_unlz4.c"
7979
#endif
80+
81+
#ifdef CONFIG_KERNEL_ZSTD
82+
#include "../../../../lib/decompress_unzstd.c"
83+
#endif
8084
/*
8185
* NOTE: When adding a new decompressor, please update the analysis in
8286
* ../header.S.

arch/x86/include/asm/boot.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
# error "Invalid value for CONFIG_PHYSICAL_ALIGN"
2525
#endif
2626

27-
#ifdef CONFIG_KERNEL_BZIP2
27+
#if defined(CONFIG_KERNEL_BZIP2)
2828
# define BOOT_HEAP_SIZE 0x400000
29-
#else /* !CONFIG_KERNEL_BZIP2 */
29+
#elif defined(CONFIG_KERNEL_ZSTD)
30+
/*
31+
* Zstd needs to allocate the ZSTD_DCtx in order to decompress the kernel.
32+
* The ZSTD_DCtx is ~160KB, so set the heap size to 192KB because it is a
33+
* round number and to allow some slack.
34+
*/
35+
# define BOOT_HEAP_SIZE 0x30000
36+
#else
3037
# define BOOT_HEAP_SIZE 0x10000
3138
#endif
3239

0 commit comments

Comments
 (0)