Skip to contentSkip to conversation

lib: zstd: update to latest Linux zstd 1.5.2

PATCH In review Brandon Maier 2 participants 4 messages Jan 12, 2023 – Jan 19, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 lib: zstd: update to latest Linux zstd 1.5.2

Update the zstd implementation to match Linux zstd 1.5.2 from commit 2aa14b1ab2.

This was motivated by running into decompression corruption issues when trying to uncompress files compressed with newer versions of zstd. zstd users also claim significantly improved decompression times with newer zstd versions which is a side benefit.

Original zstd code was copied from Linux commit 2aa14b1ab2 which is a custom-built implementation based on zstd 1.3.1. Linux switched to an implementation that is a copy of the upstream zstd code in Linux commit e0c1b49f5b, this results in a large code diff. However this should make future updates easier along with other benefits[1].

This commit is a straight mirror of the Linux zstd code, except to:
- update a few #include that do not translate cleanly
- linux/swab.h -> asm/byteorder.h
- linux/limits.h -> linux/kernel.h
- linux/module.h -> linux/compat.h
- remove assert() from debug.h so it doesn't conflict with u-boot's assert()
- strip out the compressor code as was done in the previous u-boot zstd
- update existing zstd users to the new Linux zstd API
- change the #define for MEM_STATIC to use INLINE_KEYWORD for codesize
- add a new KConfig option that sets zstd build options to minify code based on zstd's ZSTD_LIB_MINIFY[2].

These changes were tested by booting a zstd 1.5.2 compressed kernel inside a FIT. And the squashfs changes by loading a file from zstd compressed squashfs with sqfsload. buildman was used to compile test other boards and check for binary bloat, as follows:

Quoted 8 lines
$ buildman -b zstd2 --boards dh_imx6,m53menlo,mvebu_espressobin-88f3720,sandbox,sandbox64,stm32mp15_dhcom_basic,stm32mp15_dhcor_basic,turris_mox,turris_omnia -sS
Summary of 6 commits for 9 boards (8 threads, 1 job per thread)
01: Merge branch '2023-01-10-platform-updates'
       arm:  w+   m53menlo dh_imx6
02: lib: zstd: update to latest Linux zstd 1.5.2
   aarch64: (for 2/2 boards) all -3186.0 rodata +920.0 text -4106.0
       arm: (for 5/5 boards) all +1254.4 rodata +940.0 text +314.4
   sandbox: (for 2/2 boards) all -4452.0 data -16.0 rodata +640.0 text -5076.0

[1] https://github.com/torvalds/linux/commit/e0c1b49f5b674cca7b10549c53b3791d0bbc90a8
[2] https://github.com/facebook/zstd/blob/f302ad8811643c428c4e3498e28f53a0578020d3/lib/libzstd.mk#L31

---
v2:
- zstd_decompress(): Handle junk at the end of the decompression buffer by checking actual size with zstd_find_frame_compressed_size().
v3:
- minor formatting change to sqfs_decompressor.c to drop an unnecessary change in a printf error string.
v4:
- add DYNAMIC_BMI2=0 to shrink codesize of x86
- make is possible to disable inline for MEM_STATIC functions

---
 fs/squashfs/sqfs_decompressor.c               |   15 +-
 include/linux/zstd.h                          | 1244 ++------
 include/linux/zstd_errors.h                   |   77 +
 include/linux/zstd_lib.h                      | 2551 +++++++++++++++++
 lib/Kconfig                                   |   15 +
 lib/zstd/Makefile                             |   36 +-
 lib/zstd/bitstream.h                          |  344 ---
 lib/zstd/common/bitstream.h                   |  446 +++
 lib/zstd/common/compiler.h                    |  184 ++
 lib/zstd/common/cpu.h                         |  194 ++
 lib/zstd/common/debug.c                       |   24 +
 lib/zstd/common/debug.h                       |   92 +
 lib/zstd/common/entropy_common.c              |  357 +++
 lib/zstd/common/error_private.c               |   56 +
 lib/zstd/common/error_private.h               |  145 +
 lib/zstd/common/fse.h                         |  711 +++++
 lib/zstd/common/fse_decompress.c              |  390 +++
 lib/zstd/common/huf.h                         |  358 +++
 lib/zstd/common/mem.h                         |  262 ++
 lib/zstd/common/portability_macros.h          |   93 +
 lib/zstd/common/zstd_common.c                 |   83 +
 lib/zstd/common/zstd_deps.h                   |  125 +
 lib/zstd/common/zstd_internal.h               |  443 +++
 lib/zstd/decompress.c                         | 2516 ----------------
 lib/zstd/decompress/huf_decompress.c          | 1740 +++++++++++
 lib/zstd/decompress/zstd_ddict.c              |  241 ++
 lib/zstd/decompress/zstd_ddict.h              |   44 +
 lib/zstd/decompress/zstd_decompress.c         | 2131 ++++++++++++++
 lib/zstd/decompress/zstd_decompress_block.c   | 2072 +++++++++++++
 lib/zstd/decompress/zstd_decompress_block.h   |   68 +
 .../decompress/zstd_decompress_internal.h     |  228 ++
 lib/zstd/decompress_sources.h                 |   34 +
 lib/zstd/entropy_common.c                     |  213 --
 lib/zstd/error_private.h                      |   43 -
 lib/zstd/fse.h                                |  545 ----
 lib/zstd/fse_decompress.c                     |  302 --
 lib/zstd/huf.h                                |  182 --
 lib/zstd/huf_decompress.c                     |  930 ------
 lib/zstd/mem.h                                |  142 -
 lib/zstd/zstd.c                               |   52 +-
 lib/zstd/zstd_common.c                        |   66 -
 lib/zstd/zstd_common_module.c                 |   32 +
 lib/zstd/zstd_decompress_module.c             |  105 +
 lib/zstd/zstd_internal.h                      |  253 --
 lib/zstd/zstd_opt.h                           | 1004 -------
 45 files changed, 13640 insertions(+), 7548 deletions(-)
 create mode 100644 include/linux/zstd_errors.h
 create mode 100644 include/linux/zstd_lib.h
 delete mode 100644 lib/zstd/bitstream.h
 create mode 100644 lib/zstd/common/bitstream.h
 create mode 100644 lib/zstd/common/compiler.h
 create mode 100644 lib/zstd/common/cpu.h
 create mode 100644 lib/zstd/common/debug.c
 create mode 100644 lib/zstd/common/debug.h
 create mode 100644 lib/zstd/common/entropy_common.c
 create mode 100644 lib/zstd/common/error_private.c
 create mode 100644 lib/zstd/common/error_private.h
 create mode 100644 lib/zstd/common/fse.h
 create mode 100644 lib/zstd/common/fse_decompress.c
 create mode 100644 lib/zstd/common/huf.h
 create mode 100644 lib/zstd/common/mem.h
 create mode 100644 lib/zstd/common/portability_macros.h
 create mode 100644 lib/zstd/common/zstd_common.c
 create mode 100644 lib/zstd/common/zstd_deps.h
 create mode 100644 lib/zstd/common/zstd_internal.h
 delete mode 100644 lib/zstd/decompress.c
 create mode 100644 lib/zstd/decompress/huf_decompress.c
 create mode 100644 lib/zstd/decompress/zstd_ddict.c
 create mode 100644 lib/zstd/decompress/zstd_ddict.h
 create mode 100644 lib/zstd/decompress/zstd_decompress.c
 create mode 100644 lib/zstd/decompress/zstd_decompress_block.c
 create mode 100644 lib/zstd/decompress/zstd_decompress_block.h
 create mode 100644 lib/zstd/decompress/zstd_decompress_internal.h
 create mode 100644 lib/zstd/decompress_sources.h
 delete mode 100644 lib/zstd/entropy_common.c
 delete mode 100644 lib/zstd/error_private.h
 delete mode 100644 lib/zstd/fse.h
 delete mode 100644 lib/zstd/fse_decompress.c
 delete mode 100644 lib/zstd/huf.h
 delete mode 100644 lib/zstd/huf_decompress.c
 delete mode 100644 lib/zstd/mem.h
 delete mode 100644 lib/zstd/zstd_common.c
 create mode 100644 lib/zstd/zstd_common_module.c
 create mode 100644 lib/zstd/zstd_decompress_module.c
 delete mode 100644 lib/zstd/zstd_internal.h
 delete mode 100644 lib/zstd/zstd_opt.h
fs/squashfs/sqfs_decompressor.c +9 -6
index d69ddb24a41..6b3e01cdad7 100644
@@ -40,7 +40,7 @@ int sqfs_decompressor_init(struct squashfs_ctxt *ctxt)
 #endif
 #if IS_ENABLED(CONFIG_ZSTD)
 	case SQFS_COMP_ZSTD:
-		ctxt->zstd_workspace = malloc(ZSTD_DCtxWorkspaceBound());
+		ctxt->zstd_workspace = malloc(zstd_dctx_workspace_bound());
 		if (!ctxt->zstd_workspace)
 			return -ENOMEM;
 		break;
@@ -99,11 +99,14 @@ static int sqfs_zstd_decompress(struct squashfs_ctxt *ctxt, void *dest,
 	size_t wsize;
 	int ret;
 
-	wsize = ZSTD_DCtxWorkspaceBound();
-	ctx = ZSTD_initDCtx(ctxt->zstd_workspace, wsize);
-	ret = ZSTD_decompressDCtx(ctx, dest, dest_len, source, src_len);
+	wsize = zstd_dctx_workspace_bound();
 
-	return ZSTD_isError(ret);
+	ctx = zstd_init_dctx(ctxt->zstd_workspace, wsize);
+	if (!ctx)
+		return -EINVAL;
+	ret = zstd_decompress_dctx(ctx, dest, dest_len, source, src_len);
+
+	return zstd_is_error(ret);
 }
 #endif /* CONFIG_ZSTD */
 
@@ -140,7 +143,7 @@ int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
 	case SQFS_COMP_ZSTD:
 		ret = sqfs_zstd_decompress(ctxt, dest, *dest_len, source, src_len);
 		if (ret) {
-			printf("ZSTD Error code: %d\n", ZSTD_getErrorCode(ret));
+			printf("ZSTD Error code: %d\n", zstd_get_error_code(ret));
 			return -EINVAL;
 		}
 
include/linux/zstd.h +272 -972

1,422 lines not on this page · raw message

include/linux/zstd_errors.h new file +77 -0

77 lines not on this page · raw message

include/linux/zstd_lib.h new file +2551 -0

1,467 lines not on this page · raw message

lib/Kconfig +15 -0
index def36f275ce..980bbc45d9e 100644

Lines of this file are not shown. View the raw message.

lib/zstd/Makefile +34 -2
index 12170892923..2003e184258 100644

Lines of this file are not shown. View the raw message.

lib/zstd/bitstream.h deleted +0 -344

344 lines not on this page · raw message

lib/zstd/common/bitstream.h new file +446 -0

446 lines not on this page · raw message

lib/zstd/common/compiler.h new file +184 -0

184 lines not on this page · raw message

lib/zstd/common/cpu.h new file +194 -0

194 lines not on this page · raw message

lib/zstd/common/debug.c new file +24 -0
new file mode 100644
index 00000000000..bb863c9ea61

Lines of this file are not shown. View the raw message.

lib/zstd/common/debug.h new file +92 -0

92 lines not on this page · raw message

lib/zstd/common/entropy_common.c new file +357 -0

357 lines not on this page · raw message

lib/zstd/common/error_private.c new file +56 -0

56 lines not on this page · raw message

lib/zstd/common/error_private.h new file +145 -0

145 lines not on this page · raw message

lib/zstd/common/fse.h new file +711 -0

711 lines not on this page · raw message

lib/zstd/common/fse_decompress.c new file +390 -0

390 lines not on this page · raw message

lib/zstd/common/huf.h new file +358 -0

358 lines not on this page · raw message

lib/zstd/common/mem.h new file +262 -0

262 lines not on this page · raw message

lib/zstd/common/portability_macros.h new file +93 -0

93 lines not on this page · raw message

lib/zstd/common/zstd_common.c new file +83 -0

83 lines not on this page · raw message

lib/zstd/common/zstd_deps.h new file +125 -0

125 lines not on this page · raw message

lib/zstd/common/zstd_internal.h new file +443 -0

443 lines not on this page · raw message

lib/zstd/decompress.c deleted +0 -2516

2,516 lines not on this page · raw message

lib/zstd/decompress/huf_decompress.c new file +1740 -0

1,740 lines not on this page · raw message

lib/zstd/decompress/zstd_ddict.c new file +241 -0

241 lines not on this page · raw message

lib/zstd/decompress/zstd_ddict.h new file +44 -0

44 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress.c new file +2131 -0

2,131 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress_block.c new file +2072 -0

2,072 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress_block.h new file +68 -0

68 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress_internal.h new file +228 -0

228 lines not on this page · raw message

lib/zstd/decompress_sources.h new file +34 -0
new file mode 100644
index 00000000000..a06ca187aab

Lines of this file are not shown. View the raw message.

lib/zstd/entropy_common.c deleted +0 -213

213 lines not on this page · raw message

lib/zstd/error_private.h deleted +0 -43

43 lines not on this page · raw message

lib/zstd/fse.h deleted +0 -545

545 lines not on this page · raw message

lib/zstd/fse_decompress.c deleted +0 -302

302 lines not on this page · raw message

lib/zstd/huf.h deleted +0 -182

182 lines not on this page · raw message

lib/zstd/huf_decompress.c deleted +0 -930

930 lines not on this page · raw message

lib/zstd/mem.h deleted +0 -142

142 lines not on this page · raw message

lib/zstd/zstd.c +24 -28

52 lines not on this page · raw message

lib/zstd/zstd_common.c deleted +0 -66

66 lines not on this page · raw message

lib/zstd/zstd_common_module.c new file +32 -0
new file mode 100644
index 00000000000..73b97c2778c

Lines of this file are not shown. View the raw message.

lib/zstd/zstd_decompress_module.c new file +105 -0

105 lines not on this page · raw message

lib/zstd/zstd_internal.h deleted +0 -253

253 lines not on this page · raw message

lib/zstd/zstd_opt.h deleted +0 -1004

1,004 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.39.0

Part of the diff is not shown here. View the raw message.

#1 lore raw

PATCH v4 2/2 test: compression: add zstd uncompression test

---
v2:
- added zstd uncompression test
v3:
- use sizeof() instead of magic number for length

---
 test/Kconfig       |  2 +-
 test/compression.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)
test/Kconfig +1 -1
index 9f4641ae6bc..465028265b2 100644
@@ -69,7 +69,7 @@ endif
 config UT_COMPRESSION
 	bool "Unit test for compression"
 	depends on UNIT_TEST
-	depends on CMDLINE && GZIP_COMPRESSED && BZIP2 && LZMA && LZO && LZ4
+	depends on CMDLINE && GZIP_COMPRESSED && BZIP2 && LZMA && LZO && LZ4 && ZSTD
 	default y
 	help
 	  Enables tests for compression and decompression routines for simple
test/compression.c +71 -0

107 lines not on this page · raw message

Signature · 1 line
2.39.0
#3 lore raw
In reply to Brandon Maier
Quoted 41 lines · Brandon Maier

Update the zstd implementation to match Linux zstd 1.5.2 from commit 2aa14b1ab2.

This was motivated by running into decompression corruption issues when trying to uncompress files compressed with newer versions of zstd. zstd users also claim significantly improved decompression times with newer zstd versions which is a side benefit.

Original zstd code was copied from Linux commit 2aa14b1ab2 which is a custom-built implementation based on zstd 1.3.1. Linux switched to an implementation that is a copy of the upstream zstd code in Linux commit e0c1b49f5b, this results in a large code diff. However this should make future updates easier along with other benefits[1].

This commit is a straight mirror of the Linux zstd code, except to:
- update a few #include that do not translate cleanly
- linux/swab.h -> asm/byteorder.h
- linux/limits.h -> linux/kernel.h
- linux/module.h -> linux/compat.h
- remove assert() from debug.h so it doesn't conflict with u-boot's assert()
- strip out the compressor code as was done in the previous u-boot zstd
- update existing zstd users to the new Linux zstd API
- change the #define for MEM_STATIC to use INLINE_KEYWORD for codesize
- add a new KConfig option that sets zstd build options to minify code based on zstd's ZSTD_LIB_MINIFY[2].

These changes were tested by booting a zstd 1.5.2 compressed kernel inside a FIT. And the squashfs changes by loading a file from zstd compressed squashfs with sqfsload. buildman was used to compile test other boards and check for binary bloat, as follows:

Quoted 8 lines
$ buildman -b zstd2 --boards dh_imx6,m53menlo,mvebu_espressobin-88f3720,sandbox,sandbox64,stm32mp15_dhcom_basic,stm32mp15_dhcor_basic,turris_mox,turris_omnia -sS
Summary of 6 commits for 9 boards (8 threads, 1 job per thread)
01: Merge branch '2023-01-10-platform-updates'
       arm:  w+   m53menlo dh_imx6
02: lib: zstd: update to latest Linux zstd 1.5.2
   aarch64: (for 2/2 boards) all -3186.0 rodata +920.0 text -4106.0
       arm: (for 5/5 boards) all +1254.4 rodata +940.0 text +314.4
   sandbox: (for 2/2 boards) all -4452.0 data -16.0 rodata +640.0 text -5076.0
Quoted 4 lines

[1] https://github.com/torvalds/linux/commit/e0c1b49f5b674cca7b10549c53b3791d0bbc90a8
[2] https://github.com/facebook/zstd/blob/f302ad8811643c428c4e3498e28f53a0578020d3/lib/libzstd.mk#L31

Applied to u-boot/master, thanks!

Signature · 1 line
Tom