2020-11-03 06:02:49

by Nick Terrell

[permalink] [raw]
Subject: [GIT PULL][PATCH v5 0/9] Update to zstd-1.4.6

From: Nick Terrell <[email protected]>

Please pull from

[email protected]:terrelln/linux.git tags/v5-zstd-1.4.6

to get these changes. Alternatively the patchset is included.

This patchset upgrades the zstd library to the latest upstream release. The
current zstd version in the kernel is a modified version of upstream zstd-1.3.1.
At the time it was integrated, zstd wasn't ready to be used in the kernel as-is.
But, it is now possible to use upstream zstd directly in the kernel.

I have not yet release zstd-1.4.6 upstream. I want the zstd version in the kernel
to match up with a known upstream release, so we know exactly what code is
running. Whenever this patchset is ready for merge, I will cut a release at the
upstream commit that gets merged. This should not be necessary for future
releases.

The kernel zstd library is automatically generated from upstream zstd. A script
makes the necessary changes and imports it into the kernel. The changes are:

1. Replace all libc dependencies with kernel replacements and rewrite includes.
2. Remove unncessary portability macros like: #if defined(_MSC_VER).
3. Use the kernel xxhash instead of bundling it.

This automation gets tested every commit by upstream's continuous integration.
When we cut a new zstd release, we will submit a patch to the kernel to update
the zstd version in the kernel.

I've updated zstd to upstream with one big patch because every commit must build,
so that precludes partial updates. Since the commit is 100% generated, I hope the
review burden is lightened. I considered replaying upstream commits, but that is
not possible because there have been ~3500 upstream commits since the last zstd
import, and the commits don't all build individually. The bulk update preserves
bisectablity because bugs can be bisected to the zstd version update. At that
point the update can be reverted, and we can work with upstream to find and fix
the bug. After this big switch in how the kernel consumes zstd, future patches
will be smaller, because they will only have one upstream release worth of
changes each.

This patchset changes the zstd API from a custom kernel API to the upstream API.
I considered wrapping the upstream API with a wrapper that is closer to the
kernel style guide. Following advise from https://lkml.org/lkml/2020/9/17/814
I've chosen to use the upstream API directly, to minimize opportunities to
introduce bugs, and because using the upstream API directly makes debugging and
communication with upstream easier.

This patchset comes in 3 parts:
1. The first 2 patches prepare for the zstd upgrade. The first patch adds a
compatibility wrapper so zstd can be upgraded without modifying any callers.
The second patch adds an indirection for the lib/decompress_unzstd.c including
of all decompression source files.
2. Import zstd-1.4.6. This patch is completely generated from upstream using
automated tooling.
3. Update all callers to the zstd-1.4.6 API then delete the compatibility
wrapper.

I tested every caller of zstd on x86_64. I tested both after the 1.4.6 upgrade
using the compatibility wrapper, and after the final patch in this series.

I tested kernel and initramfs decompression in i386 and arm.

I ran benchmarks to compare the current zstd in the kernel with zstd-1.4.6.
I benchmarked on x86_64 using QEMU with KVM enabled on an Intel i9-9900k.
I found:
* BtrFS zstd compression at levels 1 and 3 is 5% faster
* BtrFS zstd decompression+read is 15% faster
* SquashFS zstd decompression+read is 15% faster
* F2FS zstd compression+write at level 3 is 8% faster
* F2FS zstd decompression+read is 20% faster
* ZRAM decompression+read is 30% faster
* Kernel zstd decompression is 35% faster
* Initramfs zstd decompression+build is 5% faster

The latest zstd also offers bug fixes and a 1 KB reduction in stack uage during
compression. For example the recent problem with large kernel decompression has
been fixed upstream for over 2 years https://lkml.org/lkml/2020/9/29/27.

Please let me know if there is anything that I can do to ease the way for these
patches. I think it is important because it gets large performance improvements,
contains bug fixes, and is switching to a more maintainable model of consuming
upstream zstd directly, making it easy to keep up to date.

Best,
Nick Terrell

v1 -> v2:
* Successfully tested F2FS with help from Chao Yu to fix my test.
* (1/9) Fix ZSTD_initCStream() wrapper to handle pledged_src_size=0 means unknown.
This fixes F2FS with the zstd-1.4.6 compatibility wrapper, exposed by the test.

v2 -> v3:
* (3/9) Silence warnings by Kernel Test Robot:
https://github.com/facebook/zstd/pull/2324
Stack size warnings remain, but these aren't new, and the functions it warns on
are either unused or not in the maximum stack path. This patchset reduces zstd
compression stack usage by 1 KB overall. I've gotten the low hanging fruit, and
more stack reduction would require significant changes that have the potential
to introduce new bugs. However, I do hope to continue to reduce zstd stack
usage in future versions.

v3 -> v4:
* (3/9) Fix errors and warnings reported by Kernel Test Robot:
https://github.com/facebook/zstd/pull/2326
- Replace mem.h with a custom kernel implementation that matches the current
lib/zstd/mem.h in the kernel. This avoids calls to __builtin_bswap*() which
don't work on certain architectures, as exposed by the Kernel Test Robot.
- Remove ASAN/MSAN (un)poisoning code which doesn't work in the kernel, as
exposed by the Kernel Test Robot.
- I've fixed all of the valid cppcheck warnings reported, but there were many
false positives, where cppcheck was incorrectly analyzing the situation,
which I did not fix. I don't believe it is reasonable to expect that upstream
zstd silences all the static analyzer false positives. Upstream zstd uses
clang scan-build for its static analysis. We find that supporting multiple
static analysis tools multiplies the burden of silencing false positives,
without providing enough marginal value over running a single static analysis
tool.

v4 -> v5:
* Rebase onto v5.10-rc2
* (6/9) Merge with other F2FS changes (no functional change in patch).

Nick Terrell (9):
lib: zstd: Add zstd compatibility wrapper
lib: zstd: Add decompress_sources.h for decompress_unzstd
lib: zstd: Upgrade to latest upstream zstd version 1.4.6
crypto: zstd: Switch to zstd-1.4.6 API
btrfs: zstd: Switch to the zstd-1.4.6 API
f2fs: zstd: Switch to the zstd-1.4.6 API
squashfs: zstd: Switch to the zstd-1.4.6 API
lib: unzstd: Switch to the zstd-1.4.6 API
lib: zstd: Remove zstd compatibility wrapper

crypto/zstd.c | 22 +-
fs/btrfs/zstd.c | 46 +-
fs/f2fs/compress.c | 99 +-
fs/squashfs/zstd_wrapper.c | 7 +-
include/linux/zstd.h | 3021 ++++++++----
include/linux/zstd_errors.h | 76 +
lib/decompress_unzstd.c | 44 +-
lib/zstd/Makefile | 35 +-
lib/zstd/bitstream.h | 379 --
lib/zstd/common/bitstream.h | 437 ++
lib/zstd/common/compiler.h | 150 +
lib/zstd/common/cpu.h | 194 +
lib/zstd/common/debug.c | 24 +
lib/zstd/common/debug.h | 101 +
lib/zstd/common/entropy_common.c | 355 ++
lib/zstd/common/error_private.c | 55 +
lib/zstd/common/error_private.h | 66 +
lib/zstd/common/fse.h | 709 +++
lib/zstd/common/fse_decompress.c | 380 ++
lib/zstd/common/huf.h | 352 ++
lib/zstd/common/mem.h | 258 +
lib/zstd/common/zstd_common.c | 83 +
lib/zstd/common/zstd_deps.h | 124 +
lib/zstd/common/zstd_internal.h | 438 ++
lib/zstd/compress.c | 3485 --------------
lib/zstd/compress/fse_compress.c | 625 +++
lib/zstd/compress/hist.c | 165 +
lib/zstd/compress/hist.h | 75 +
lib/zstd/compress/huf_compress.c | 764 +++
lib/zstd/compress/zstd_compress.c | 4157 +++++++++++++++++
lib/zstd/compress/zstd_compress_internal.h | 1103 +++++
lib/zstd/compress/zstd_compress_literals.c | 158 +
lib/zstd/compress/zstd_compress_literals.h | 29 +
lib/zstd/compress/zstd_compress_sequences.c | 433 ++
lib/zstd/compress/zstd_compress_sequences.h | 54 +
lib/zstd/compress/zstd_compress_superblock.c | 849 ++++
lib/zstd/compress/zstd_compress_superblock.h | 32 +
lib/zstd/compress/zstd_cwksp.h | 465 ++
lib/zstd/compress/zstd_double_fast.c | 521 +++
lib/zstd/compress/zstd_double_fast.h | 32 +
lib/zstd/compress/zstd_fast.c | 496 ++
lib/zstd/compress/zstd_fast.h | 31 +
lib/zstd/compress/zstd_lazy.c | 1138 +++++
lib/zstd/compress/zstd_lazy.h | 61 +
lib/zstd/compress/zstd_ldm.c | 619 +++
lib/zstd/compress/zstd_ldm.h | 104 +
lib/zstd/compress/zstd_opt.c | 1200 +++++
lib/zstd/compress/zstd_opt.h | 50 +
lib/zstd/decompress.c | 2531 ----------
lib/zstd/decompress/huf_decompress.c | 1205 +++++
lib/zstd/decompress/zstd_ddict.c | 241 +
lib/zstd/decompress/zstd_ddict.h | 44 +
lib/zstd/decompress/zstd_decompress.c | 1836 ++++++++
lib/zstd/decompress/zstd_decompress_block.c | 1540 ++++++
lib/zstd/decompress/zstd_decompress_block.h | 62 +
.../decompress/zstd_decompress_internal.h | 195 +
lib/zstd/decompress_sources.h | 18 +
lib/zstd/entropy_common.c | 243 -
lib/zstd/error_private.h | 53 -
lib/zstd/fse.h | 575 ---
lib/zstd/fse_compress.c | 795 ----
lib/zstd/fse_decompress.c | 325 --
lib/zstd/huf.h | 212 -
lib/zstd/huf_compress.c | 772 ---
lib/zstd/huf_decompress.c | 960 ----
lib/zstd/mem.h | 151 -
lib/zstd/zstd_common.c | 75 -
lib/zstd/zstd_compress_module.c | 79 +
lib/zstd/zstd_decompress_module.c | 79 +
lib/zstd/zstd_internal.h | 273 --
lib/zstd/zstd_opt.h | 1014 ----
71 files changed, 24367 insertions(+), 13012 deletions(-)
create mode 100644 include/linux/zstd_errors.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/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/compress.c
create mode 100644 lib/zstd/compress/fse_compress.c
create mode 100644 lib/zstd/compress/hist.c
create mode 100644 lib/zstd/compress/hist.h
create mode 100644 lib/zstd/compress/huf_compress.c
create mode 100644 lib/zstd/compress/zstd_compress.c
create mode 100644 lib/zstd/compress/zstd_compress_internal.h
create mode 100644 lib/zstd/compress/zstd_compress_literals.c
create mode 100644 lib/zstd/compress/zstd_compress_literals.h
create mode 100644 lib/zstd/compress/zstd_compress_sequences.c
create mode 100644 lib/zstd/compress/zstd_compress_sequences.h
create mode 100644 lib/zstd/compress/zstd_compress_superblock.c
create mode 100644 lib/zstd/compress/zstd_compress_superblock.h
create mode 100644 lib/zstd/compress/zstd_cwksp.h
create mode 100644 lib/zstd/compress/zstd_double_fast.c
create mode 100644 lib/zstd/compress/zstd_double_fast.h
create mode 100644 lib/zstd/compress/zstd_fast.c
create mode 100644 lib/zstd/compress/zstd_fast.h
create mode 100644 lib/zstd/compress/zstd_lazy.c
create mode 100644 lib/zstd/compress/zstd_lazy.h
create mode 100644 lib/zstd/compress/zstd_ldm.c
create mode 100644 lib/zstd/compress/zstd_ldm.h
create mode 100644 lib/zstd/compress/zstd_opt.c
create mode 100644 lib/zstd/compress/zstd_opt.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_compress.c
delete mode 100644 lib/zstd/fse_decompress.c
delete mode 100644 lib/zstd/huf.h
delete mode 100644 lib/zstd/huf_compress.c
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_compress_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

--
2.28.0


2020-11-03 06:03:02

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 2/9] lib: zstd: Add decompress_sources.h for decompress_unzstd

From: Nick Terrell <[email protected]>

Adds decompress_sources.h which includes every .c file necessary for
zstd decompression. This is used in decompress_unzstd.c so the internal
structure of the library isn't exposed.

This allows us to upgrade the zstd library version without modifying any
callers. Instead we just need to update decompress_sources.h.

Signed-off-by: Nick Terrell <[email protected]>
---
lib/decompress_unzstd.c | 6 +-----
lib/zstd/decompress_sources.h | 14 ++++++++++++++
2 files changed, 15 insertions(+), 5 deletions(-)
create mode 100644 lib/zstd/decompress_sources.h

diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
index 6bb805aeec08..3c6ad01ffcd5 100644
--- a/lib/decompress_unzstd.c
+++ b/lib/decompress_unzstd.c
@@ -68,11 +68,7 @@
#ifdef STATIC
# define UNZSTD_PREBOOT
# include "xxhash.c"
-# include "zstd/entropy_common.c"
-# include "zstd/fse_decompress.c"
-# include "zstd/huf_decompress.c"
-# include "zstd/zstd_common.c"
-# include "zstd/decompress.c"
+# include "zstd/decompress_sources.h"
#endif

#include <linux/decompress/mm.h>
diff --git a/lib/zstd/decompress_sources.h b/lib/zstd/decompress_sources.h
new file mode 100644
index 000000000000..ccb4960ea0cd
--- /dev/null
+++ b/lib/zstd/decompress_sources.h
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * This file includes every .c file needed for decompression.
+ * It is used by lib/decompress_unzstd.c to include the decompression
+ * source into the translation-unit, so it can be used for kernel
+ * decompression.
+ */
+
+#include "entropy_common.c"
+#include "fse_decompress.c"
+#include "huf_decompress.c"
+#include "zstd_common.c"
+#include "decompress.c"
--
2.28.0

2020-11-03 06:03:19

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 4/9] crypto: zstd: Switch to zstd-1.4.6 API

From: Nick Terrell <[email protected]>

Move away from the compatibility wrapper to the zstd-1.4.6 API. This
code is functionally equivalent.

Signed-off-by: Nick Terrell <[email protected]>
---
crypto/zstd.c | 24 +++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/crypto/zstd.c b/crypto/zstd.c
index dcda3cad3b5c..767fe2fbe009 100644
--- a/crypto/zstd.c
+++ b/crypto/zstd.c
@@ -11,7 +11,7 @@
#include <linux/module.h>
#include <linux/net.h>
#include <linux/vmalloc.h>
-#include <linux/zstd_compat.h>
+#include <linux/zstd.h>
#include <crypto/internal/scompress.h>


@@ -24,16 +24,15 @@ struct zstd_ctx {
void *dwksp;
};

-static ZSTD_parameters zstd_params(void)
-{
- return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0);
-}
-
static int zstd_comp_init(struct zstd_ctx *ctx)
{
int ret = 0;
- const ZSTD_parameters params = zstd_params();
- const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams);
+ const size_t wksp_size = ZSTD_estimateCCtxSize(ZSTD_DEF_LEVEL);
+
+ if (ZSTD_isError(wksp_size)) {
+ ret = -EINVAL;
+ goto out_free;
+ }

ctx->cwksp = vzalloc(wksp_size);
if (!ctx->cwksp) {
@@ -41,7 +40,7 @@ static int zstd_comp_init(struct zstd_ctx *ctx)
goto out;
}

- ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size);
+ ctx->cctx = ZSTD_initStaticCCtx(ctx->cwksp, wksp_size);
if (!ctx->cctx) {
ret = -EINVAL;
goto out_free;
@@ -56,7 +55,7 @@ static int zstd_comp_init(struct zstd_ctx *ctx)
static int zstd_decomp_init(struct zstd_ctx *ctx)
{
int ret = 0;
- const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
+ const size_t wksp_size = ZSTD_estimateDCtxSize();

ctx->dwksp = vzalloc(wksp_size);
if (!ctx->dwksp) {
@@ -64,7 +63,7 @@ static int zstd_decomp_init(struct zstd_ctx *ctx)
goto out;
}

- ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size);
+ ctx->dctx = ZSTD_initStaticDCtx(ctx->dwksp, wksp_size);
if (!ctx->dctx) {
ret = -EINVAL;
goto out_free;
@@ -152,9 +151,8 @@ static int __zstd_compress(const u8 *src, unsigned int slen,
{
size_t out_len;
struct zstd_ctx *zctx = ctx;
- const ZSTD_parameters params = zstd_params();

- out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params);
+ out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, ZSTD_DEF_LEVEL);
if (ZSTD_isError(out_len))
return -EINVAL;
*dlen = out_len;
--
2.28.0

2020-11-03 06:03:21

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

From: Nick Terrell <[email protected]>

Adds zstd_compat.h which provides the necessary functions from the
current zstd.h API. It is only active for zstd versions 1.4.6 and newer.
That means it is disabled currently, but will become active when a later
patch in this series updates the zstd library in the kernel to 1.4.6.

This header allows the zstd upgrade to 1.4.6 without changing any
callers, since they all include zstd through the compatibility wrapper.
Later patches in this series transition each caller away from the
compatibility wrapper. After all the callers have been transitioned away
from the compatibility wrapper, the final patch in this series deletes
it.

Signed-off-by: Nick Terrell <[email protected]>
---
crypto/zstd.c | 2 +-
fs/btrfs/zstd.c | 2 +-
fs/f2fs/compress.c | 2 +-
fs/squashfs/zstd_wrapper.c | 2 +-
include/linux/zstd_compat.h | 116 ++++++++++++++++++++++++++++++++++++
lib/decompress_unzstd.c | 2 +-
6 files changed, 121 insertions(+), 5 deletions(-)
create mode 100644 include/linux/zstd_compat.h

diff --git a/crypto/zstd.c b/crypto/zstd.c
index 1a3309f066f7..dcda3cad3b5c 100644
--- a/crypto/zstd.c
+++ b/crypto/zstd.c
@@ -11,7 +11,7 @@
#include <linux/module.h>
#include <linux/net.h>
#include <linux/vmalloc.h>
-#include <linux/zstd.h>
+#include <linux/zstd_compat.h>
#include <crypto/internal/scompress.h>


diff --git a/fs/btrfs/zstd.c b/fs/btrfs/zstd.c
index 9a4871636c6c..a7367ff573d4 100644
--- a/fs/btrfs/zstd.c
+++ b/fs/btrfs/zstd.c
@@ -16,7 +16,7 @@
#include <linux/refcount.h>
#include <linux/sched.h>
#include <linux/slab.h>
-#include <linux/zstd.h>
+#include <linux/zstd_compat.h>
#include "misc.h"
#include "compression.h"
#include "ctree.h"
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 14262e0f1cd6..57a6360b9827 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -11,7 +11,7 @@
#include <linux/backing-dev.h>
#include <linux/lzo.h>
#include <linux/lz4.h>
-#include <linux/zstd.h>
+#include <linux/zstd_compat.h>

#include "f2fs.h"
#include "node.h"
diff --git a/fs/squashfs/zstd_wrapper.c b/fs/squashfs/zstd_wrapper.c
index b7cb1faa652d..f8c512a6204e 100644
--- a/fs/squashfs/zstd_wrapper.c
+++ b/fs/squashfs/zstd_wrapper.c
@@ -11,7 +11,7 @@
#include <linux/mutex.h>
#include <linux/bio.h>
#include <linux/slab.h>
-#include <linux/zstd.h>
+#include <linux/zstd_compat.h>
#include <linux/vmalloc.h>

#include "squashfs_fs.h"
diff --git a/include/linux/zstd_compat.h b/include/linux/zstd_compat.h
new file mode 100644
index 000000000000..cda9208bf04a
--- /dev/null
+++ b/include/linux/zstd_compat.h
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2016-present, Facebook, Inc.
+ * All rights reserved.
+ *
+ * This source code is licensed under the BSD-style license found in the
+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
+ * An additional grant of patent rights can be found in the PATENTS file in the
+ * same directory.
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation. This program is dual-licensed; you may select
+ * either version 2 of the GNU General Public License ("GPL") or BSD license
+ * ("BSD").
+ */
+
+#ifndef ZSTD_COMPAT_H
+#define ZSTD_COMPAT_H
+
+#include <linux/zstd.h>
+
+#if defined(ZSTD_VERSION_NUMBER) && (ZSTD_VERSION_NUMBER >= 10406)
+/*
+ * This header provides backwards compatibility for the zstd-1.4.6 library
+ * upgrade. This header allows us to upgrade the zstd library version without
+ * modifying any callers. Then we will migrate callers from the compatibility
+ * wrapper one at a time until none remain. At which point we will delete this
+ * header.
+ *
+ * It is temporary and will be deleted once the upgrade is complete.
+ */
+
+#include <linux/zstd_errors.h>
+
+static inline size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters compression_params)
+{
+ return ZSTD_estimateCCtxSize_usingCParams(compression_params);
+}
+
+static inline size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters compression_params)
+{
+ return ZSTD_estimateCStreamSize_usingCParams(compression_params);
+}
+
+static inline size_t ZSTD_DCtxWorkspaceBound(void)
+{
+ return ZSTD_estimateDCtxSize();
+}
+
+static inline size_t ZSTD_DStreamWorkspaceBound(unsigned long long window_size)
+{
+ return ZSTD_estimateDStreamSize(window_size);
+}
+
+static inline ZSTD_CCtx* ZSTD_initCCtx(void* wksp, size_t wksp_size)
+{
+ if (wksp == NULL)
+ return NULL;
+ return ZSTD_initStaticCCtx(wksp, wksp_size);
+}
+
+static inline ZSTD_CStream* ZSTD_initCStream_compat(ZSTD_parameters params, uint64_t pledged_src_size, void* wksp, size_t wksp_size)
+{
+ ZSTD_CStream* cstream;
+ size_t ret;
+
+ if (wksp == NULL)
+ return NULL;
+
+ cstream = ZSTD_initStaticCStream(wksp, wksp_size);
+ if (cstream == NULL)
+ return NULL;
+
+ /* 0 means unknown in old API but means 0 in new API */
+ if (pledged_src_size == 0)
+ pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
+
+ ret = ZSTD_initCStream_advanced(cstream, NULL, 0, params, pledged_src_size);
+ if (ZSTD_isError(ret))
+ return NULL;
+
+ return cstream;
+}
+#define ZSTD_initCStream ZSTD_initCStream_compat
+
+static inline ZSTD_DCtx* ZSTD_initDCtx(void* wksp, size_t wksp_size)
+{
+ if (wksp == NULL)
+ return NULL;
+ return ZSTD_initStaticDCtx(wksp, wksp_size);
+}
+
+static inline ZSTD_DStream* ZSTD_initDStream_compat(unsigned long long window_size, void* wksp, size_t wksp_size)
+{
+ if (wksp == NULL)
+ return NULL;
+ (void)window_size;
+ return ZSTD_initStaticDStream(wksp, wksp_size);
+}
+#define ZSTD_initDStream ZSTD_initDStream_compat
+
+typedef ZSTD_frameHeader ZSTD_frameParams;
+
+static inline size_t ZSTD_getFrameParams(ZSTD_frameParams* frame_params, const void* src, size_t src_size)
+{
+ return ZSTD_getFrameHeader(frame_params, src, src_size);
+}
+
+static inline size_t ZSTD_compressCCtx_compat(ZSTD_CCtx* cctx, void* dst, size_t dst_capacity, const void* src, size_t src_size, ZSTD_parameters params)
+{
+ return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, params);
+}
+#define ZSTD_compressCCtx ZSTD_compressCCtx_compat
+
+#endif /* ZSTD_VERSION_NUMBER >= 10406 */
+#endif /* ZSTD_COMPAT_H */
diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
index 790abc472f5b..6bb805aeec08 100644
--- a/lib/decompress_unzstd.c
+++ b/lib/decompress_unzstd.c
@@ -77,7 +77,7 @@

#include <linux/decompress/mm.h>
#include <linux/kernel.h>
-#include <linux/zstd.h>
+#include <linux/zstd_compat.h>

/* 128MB is the maximum window size supported by zstd. */
#define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WINDOWLOG_MAX)
--
2.28.0

2020-11-03 06:04:16

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 8/9] lib: unzstd: Switch to the zstd-1.4.6 API

From: Nick Terrell <[email protected]>

Move away from the compatibility wrapper to the zstd-1.4.6 API. This
code is functionally equivalent.

Signed-off-by: Nick Terrell <[email protected]>
---
lib/decompress_unzstd.c | 40 ++++++++++++++--------------------------
1 file changed, 14 insertions(+), 26 deletions(-)

diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
index 3c6ad01ffcd5..efbe66501b34 100644
--- a/lib/decompress_unzstd.c
+++ b/lib/decompress_unzstd.c
@@ -73,7 +73,8 @@

#include <linux/decompress/mm.h>
#include <linux/kernel.h>
-#include <linux/zstd_compat.h>
+#include <linux/zstd.h>
+#include <linux/zstd_errors.h>

/* 128MB is the maximum window size supported by zstd. */
#define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WINDOWLOG_MAX)
@@ -120,9 +121,9 @@ static int INIT decompress_single(const u8 *in_buf, long in_len, u8 *out_buf,
long out_len, long *in_pos,
void (*error)(char *x))
{
- const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
+ const size_t wksp_size = ZSTD_estimateDCtxSize();
void *wksp = large_malloc(wksp_size);
- ZSTD_DCtx *dctx = ZSTD_initDCtx(wksp, wksp_size);
+ ZSTD_DCtx *dctx = ZSTD_initStaticDCtx(wksp, wksp_size);
int err;
size_t ret;

@@ -165,7 +166,6 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
{
ZSTD_inBuffer in;
ZSTD_outBuffer out;
- ZSTD_frameParams params;
void *in_allocated = NULL;
void *out_allocated = NULL;
void *wksp = NULL;
@@ -234,36 +234,24 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
out.size = out_len;

/*
- * We need to know the window size to allocate the ZSTD_DStream.
- * Since we are streaming, we need to allocate a buffer for the sliding
- * window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX
- * (8 MB), so it is important to use the actual value so as not to
- * waste memory when it is smaller.
+ * Zstd determines the workspace size from the window size written
+ * into the frame header. This ensures that we use the minimum value
+ * possible, since the window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX
+ * (1 GB), so it is very important to use the actual value.
*/
- ret = ZSTD_getFrameParams(&params, in.src, in.size);
+ wksp_size = ZSTD_estimateDStreamSize_fromFrame(in.src, in.size);
err = handle_zstd_error(ret, error);
if (err)
goto out;
- if (ret != 0) {
- error("ZSTD-compressed data has an incomplete frame header");
- err = -1;
- goto out;
- }
- if (params.windowSize > ZSTD_WINDOWSIZE_MAX) {
- error("ZSTD-compressed data has too large a window size");
+ wksp = large_malloc(wksp_size);
+ if (wksp == NULL) {
+ error("Out of memory while allocating ZSTD_DStream");
err = -1;
goto out;
}
-
- /*
- * Allocate the ZSTD_DStream now that we know how much memory is
- * required.
- */
- wksp_size = ZSTD_DStreamWorkspaceBound(params.windowSize);
- wksp = large_malloc(wksp_size);
- dstream = ZSTD_initDStream(params.windowSize, wksp, wksp_size);
+ dstream = ZSTD_initStaticDStream(wksp, wksp_size);
if (dstream == NULL) {
- error("Out of memory while allocating ZSTD_DStream");
+ error("ZSTD_initStaticDStream failed");
err = -1;
goto out;
}
--
2.28.0

2020-11-03 06:04:28

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 9/9] lib: zstd: Remove zstd compatibility wrapper

From: Nick Terrell <[email protected]>

All callers have been transitioned to the new zstd-1.4.6 API. There are
no more callers of the zstd compatibility wrapper, so delete it.

Signed-off-by: Nick Terrell <[email protected]>
---
include/linux/zstd_compat.h | 116 ------------------------------------
1 file changed, 116 deletions(-)
delete mode 100644 include/linux/zstd_compat.h

diff --git a/include/linux/zstd_compat.h b/include/linux/zstd_compat.h
deleted file mode 100644
index cda9208bf04a..000000000000
--- a/include/linux/zstd_compat.h
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright (c) 2016-present, Facebook, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of https://github.com/facebook/zstd.
- * An additional grant of patent rights can be found in the PATENTS file in the
- * same directory.
- *
- * This program is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License version 2 as published by the
- * Free Software Foundation. This program is dual-licensed; you may select
- * either version 2 of the GNU General Public License ("GPL") or BSD license
- * ("BSD").
- */
-
-#ifndef ZSTD_COMPAT_H
-#define ZSTD_COMPAT_H
-
-#include <linux/zstd.h>
-
-#if defined(ZSTD_VERSION_NUMBER) && (ZSTD_VERSION_NUMBER >= 10406)
-/*
- * This header provides backwards compatibility for the zstd-1.4.6 library
- * upgrade. This header allows us to upgrade the zstd library version without
- * modifying any callers. Then we will migrate callers from the compatibility
- * wrapper one at a time until none remain. At which point we will delete this
- * header.
- *
- * It is temporary and will be deleted once the upgrade is complete.
- */
-
-#include <linux/zstd_errors.h>
-
-static inline size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters compression_params)
-{
- return ZSTD_estimateCCtxSize_usingCParams(compression_params);
-}
-
-static inline size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters compression_params)
-{
- return ZSTD_estimateCStreamSize_usingCParams(compression_params);
-}
-
-static inline size_t ZSTD_DCtxWorkspaceBound(void)
-{
- return ZSTD_estimateDCtxSize();
-}
-
-static inline size_t ZSTD_DStreamWorkspaceBound(unsigned long long window_size)
-{
- return ZSTD_estimateDStreamSize(window_size);
-}
-
-static inline ZSTD_CCtx* ZSTD_initCCtx(void* wksp, size_t wksp_size)
-{
- if (wksp == NULL)
- return NULL;
- return ZSTD_initStaticCCtx(wksp, wksp_size);
-}
-
-static inline ZSTD_CStream* ZSTD_initCStream_compat(ZSTD_parameters params, uint64_t pledged_src_size, void* wksp, size_t wksp_size)
-{
- ZSTD_CStream* cstream;
- size_t ret;
-
- if (wksp == NULL)
- return NULL;
-
- cstream = ZSTD_initStaticCStream(wksp, wksp_size);
- if (cstream == NULL)
- return NULL;
-
- /* 0 means unknown in old API but means 0 in new API */
- if (pledged_src_size == 0)
- pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
-
- ret = ZSTD_initCStream_advanced(cstream, NULL, 0, params, pledged_src_size);
- if (ZSTD_isError(ret))
- return NULL;
-
- return cstream;
-}
-#define ZSTD_initCStream ZSTD_initCStream_compat
-
-static inline ZSTD_DCtx* ZSTD_initDCtx(void* wksp, size_t wksp_size)
-{
- if (wksp == NULL)
- return NULL;
- return ZSTD_initStaticDCtx(wksp, wksp_size);
-}
-
-static inline ZSTD_DStream* ZSTD_initDStream_compat(unsigned long long window_size, void* wksp, size_t wksp_size)
-{
- if (wksp == NULL)
- return NULL;
- (void)window_size;
- return ZSTD_initStaticDStream(wksp, wksp_size);
-}
-#define ZSTD_initDStream ZSTD_initDStream_compat
-
-typedef ZSTD_frameHeader ZSTD_frameParams;
-
-static inline size_t ZSTD_getFrameParams(ZSTD_frameParams* frame_params, const void* src, size_t src_size)
-{
- return ZSTD_getFrameHeader(frame_params, src, src_size);
-}
-
-static inline size_t ZSTD_compressCCtx_compat(ZSTD_CCtx* cctx, void* dst, size_t dst_capacity, const void* src, size_t src_size, ZSTD_parameters params)
-{
- return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, params);
-}
-#define ZSTD_compressCCtx ZSTD_compressCCtx_compat
-
-#endif /* ZSTD_VERSION_NUMBER >= 10406 */
-#endif /* ZSTD_COMPAT_H */
--
2.28.0

2020-11-03 06:04:56

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 7/9] squashfs: zstd: Switch to the zstd-1.4.6 API

From: Nick Terrell <[email protected]>

Move away from the compatibility wrapper to the zstd-1.4.6 API. This
code is functionally equivalent.

Signed-off-by: Nick Terrell <[email protected]>
---
fs/squashfs/zstd_wrapper.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/fs/squashfs/zstd_wrapper.c b/fs/squashfs/zstd_wrapper.c
index f8c512a6204e..add582409866 100644
--- a/fs/squashfs/zstd_wrapper.c
+++ b/fs/squashfs/zstd_wrapper.c
@@ -11,7 +11,7 @@
#include <linux/mutex.h>
#include <linux/bio.h>
#include <linux/slab.h>
-#include <linux/zstd_compat.h>
+#include <linux/zstd.h>
#include <linux/vmalloc.h>

#include "squashfs_fs.h"
@@ -34,7 +34,7 @@ static void *zstd_init(struct squashfs_sb_info *msblk, void *buff)
goto failed;
wksp->window_size = max_t(size_t,
msblk->block_size, SQUASHFS_METADATA_SIZE);
- wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size);
+ wksp->mem_size = ZSTD_estimateDStreamSize(wksp->window_size);
wksp->mem = vmalloc(wksp->mem_size);
if (wksp->mem == NULL)
goto failed;
@@ -71,7 +71,7 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);

- stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size);
+ stream = ZSTD_initStaticDStream(wksp->mem, wksp->mem_size);

if (!stream) {
ERROR("Failed to initialize zstd decompressor\n");
@@ -122,8 +122,7 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
break;

if (ZSTD_isError(zstd_err)) {
- ERROR("zstd decompression error: %d\n",
- (int)ZSTD_getErrorCode(zstd_err));
+ ERROR("zstd decompression error: %s\n", ZSTD_getErrorName(zstd_err));
error = -EIO;
break;
}
--
2.28.0

2020-11-03 06:06:17

by Nick Terrell

[permalink] [raw]
Subject: [PATCH v5 6/9] f2fs: zstd: Switch to the zstd-1.4.6 API

From: Nick Terrell <[email protected]>

Move away from the compatibility wrapper to the zstd-1.4.6 API. This
code is more efficient because it uses the single-pass API instead of
the streaming API. The streaming API is not necessary because the whole
input and output buffers are available. This saves memory because we
don't need to allocate a buffer for the window. It is also more
efficient because it saves unnecessary memcpy calls.

Compression memory increases from 168 KB to 204 KB because upstream
uses slightly more memory. Decompression memory decreases from 1.4 MB
to 158 KB.

Signed-off-by: Nick Terrell <[email protected]>
---
fs/f2fs/compress.c | 101 +++++++++++++++++----------------------------
1 file changed, 37 insertions(+), 64 deletions(-)

diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 57a6360b9827..8f8234877666 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -11,7 +11,8 @@
#include <linux/backing-dev.h>
#include <linux/lzo.h>
#include <linux/lz4.h>
-#include <linux/zstd_compat.h>
+#include <linux/zstd.h>
+#include <linux/zstd_errors.h>

#include "f2fs.h"
#include "node.h"
@@ -322,21 +323,21 @@ static const struct f2fs_compress_ops f2fs_lz4_ops = {
static int zstd_init_compress_ctx(struct compress_ctx *cc)
{
ZSTD_parameters params;
- ZSTD_CStream *stream;
+ ZSTD_CCtx *ctx;
void *workspace;
unsigned int workspace_size;

params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
- workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
+ workspace_size = ZSTD_estimateCCtxSize_usingCParams(params.cParams);

workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
workspace_size, GFP_NOFS);
if (!workspace)
return -ENOMEM;

- stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
- if (!stream) {
- printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
+ ctx = ZSTD_initStaticCCtx(workspace, workspace_size);
+ if (!ctx) {
+ printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_inittaticCStream failed\n",
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
__func__);
kvfree(workspace);
@@ -344,7 +345,7 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
}

cc->private = workspace;
- cc->private2 = stream;
+ cc->private2 = ctx;

cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
return 0;
@@ -359,66 +360,48 @@ static void zstd_destroy_compress_ctx(struct compress_ctx *cc)

static int zstd_compress_pages(struct compress_ctx *cc)
{
- ZSTD_CStream *stream = cc->private2;
- ZSTD_inBuffer inbuf;
- ZSTD_outBuffer outbuf;
- int src_size = cc->rlen;
- int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
- int ret;
-
- inbuf.pos = 0;
- inbuf.src = cc->rbuf;
- inbuf.size = src_size;
-
- outbuf.pos = 0;
- outbuf.dst = cc->cbuf->cdata;
- outbuf.size = dst_size;
+ ZSTD_CCtx *ctx = cc->private2;
+ const size_t src_size = cc->rlen;
+ const size_t dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
+ ZSTD_parameters params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, src_size, 0);
+ size_t ret;

- ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
+ ret = ZSTD_compress_advanced(
+ ctx, cc->cbuf->cdata, dst_size, cc->rbuf, src_size, NULL, 0, params);
if (ZSTD_isError(ret)) {
- printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
- KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
- __func__, ZSTD_getErrorCode(ret));
- return -EIO;
- }
-
- ret = ZSTD_endStream(stream, &outbuf);
- if (ZSTD_isError(ret)) {
- printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
+ /*
+ * there is compressed data remained in intermediate buffer due to
+ * no more space in cbuf.cdata
+ */
+ if (ZSTD_getErrorCode(ret) == ZSTD_error_dstSize_tooSmall)
+ return -EAGAIN;
+ /* other compression errors return -EIO */
+ printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compress_advanced failed, err: %s\n",
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
- __func__, ZSTD_getErrorCode(ret));
+ __func__, ZSTD_getErrorName(ret));
return -EIO;
}

- /*
- * there is compressed data remained in intermediate buffer due to
- * no more space in cbuf.cdata
- */
- if (ret)
- return -EAGAIN;
-
- cc->clen = outbuf.pos;
+ cc->clen = ret;
return 0;
}

static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
{
- ZSTD_DStream *stream;
+ ZSTD_DCtx *ctx;
void *workspace;
unsigned int workspace_size;
- unsigned int max_window_size =
- MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);

- workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
+ workspace_size = ZSTD_estimateDCtxSize();

workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
workspace_size, GFP_NOFS);
if (!workspace)
return -ENOMEM;

- stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
- if (!stream) {
- printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
+ ctx = ZSTD_initStaticDCtx(workspace, workspace_size);
+ if (!ctx) {
+ printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initStaticDCtx failed\n",
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
__func__);
kvfree(workspace);
@@ -426,7 +409,7 @@ static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
}

dic->private = workspace;
- dic->private2 = stream;
+ dic->private2 = ctx;

return 0;
}
@@ -440,28 +423,18 @@ static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)

static int zstd_decompress_pages(struct decompress_io_ctx *dic)
{
- ZSTD_DStream *stream = dic->private2;
- ZSTD_inBuffer inbuf;
- ZSTD_outBuffer outbuf;
- int ret;
-
- inbuf.pos = 0;
- inbuf.src = dic->cbuf->cdata;
- inbuf.size = dic->clen;
-
- outbuf.pos = 0;
- outbuf.dst = dic->rbuf;
- outbuf.size = dic->rlen;
+ ZSTD_DCtx *ctx = dic->private2;
+ size_t ret;

- ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
+ ret = ZSTD_decompressDCtx(ctx, dic->rbuf, dic->rlen, dic->cbuf->cdata, dic->clen);
if (ZSTD_isError(ret)) {
- printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
+ printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_decompressDCtx failed, err: %s\n",
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
- __func__, ZSTD_getErrorCode(ret));
+ __func__, ZSTD_getErrorName(ret));
return -EIO;
}

- if (dic->rlen != outbuf.pos) {
+ if (dic->rlen != ret) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
"expected:%lu\n", KERN_ERR,
F2FS_I_SB(dic->inode)->sb->s_id,
--
2.28.0

2020-11-06 17:16:04

by Josef Bacik

[permalink] [raw]
Subject: Re: [GIT PULL][PATCH v5 0/9] Update to zstd-1.4.6

On 11/3/20 1:05 AM, Nick Terrell wrote:
> From: Nick Terrell <[email protected]>
>
> Please pull from
>
> [email protected]:terrelln/linux.git tags/v5-zstd-1.4.6
>
> to get these changes. Alternatively the patchset is included.
>

Where did we come down on the code formatting question? Personally I'm of the
mind that as long as the consumers themselves adhere to the proper coding style
I'm fine not maintaining the code style as long as we get the benefit of easily
syncing in code from the upstream project. Thanks,

Josef

2020-11-06 18:39:20

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

You just keep resedning this crap, don't you? Haven't you been told
multiple times to provide a proper kernel API by now?

On Mon, Nov 02, 2020 at 10:05:27PM -0800, Nick Terrell wrote:
> From: Nick Terrell <terre[email protected]>
>
> Adds zstd_compat.h which provides the necessary functions from the
> current zstd.h API. It is only active for zstd versions 1.4.6 and newer.
> That means it is disabled currently, but will become active when a later
> patch in this series updates the zstd library in the kernel to 1.4.6.
>
> This header allows the zstd upgrade to 1.4.6 without changing any
> callers, since they all include zstd through the compatibility wrapper.
> Later patches in this series transition each caller away from the
> compatibility wrapper. After all the callers have been transitioned away
> from the compatibility wrapper, the final patch in this series deletes
> it.
>
> Signed-off-by: Nick Terrell <[email protected]>
> ---
> crypto/zstd.c | 2 +-
> fs/btrfs/zstd.c | 2 +-
> fs/f2fs/compress.c | 2 +-
> fs/squashfs/zstd_wrapper.c | 2 +-
> include/linux/zstd_compat.h | 116 ++++++++++++++++++++++++++++++++++++
> lib/decompress_unzstd.c | 2 +-
> 6 files changed, 121 insertions(+), 5 deletions(-)
> create mode 100644 include/linux/zstd_compat.h
>
> diff --git a/crypto/zstd.c b/crypto/zstd.c
> index 1a3309f066f7..dcda3cad3b5c 100644
> --- a/crypto/zstd.c
> +++ b/crypto/zstd.c
> @@ -11,7 +11,7 @@
> #include <linux/module.h>
> #include <linux/net.h>
> #include <linux/vmalloc.h>
> -#include <linux/zstd.h>
> +#include <linux/zstd_compat.h>
> #include <crypto/internal/scompress.h>
>
>
> diff --git a/fs/btrfs/zstd.c b/fs/btrfs/zstd.c
> index 9a4871636c6c..a7367ff573d4 100644
> --- a/fs/btrfs/zstd.c
> +++ b/fs/btrfs/zstd.c
> @@ -16,7 +16,7 @@
> #include <linux/refcount.h>
> #include <linux/sched.h>
> #include <linux/slab.h>
> -#include <linux/zstd.h>
> +#include <linux/zstd_compat.h>
> #include "misc.h"
> #include "compression.h"
> #include "ctree.h"
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index 14262e0f1cd6..57a6360b9827 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -11,7 +11,7 @@
> #include <linux/backing-dev.h>
> #include <linux/lzo.h>
> #include <linux/lz4.h>
> -#include <linux/zstd.h>
> +#include <linux/zstd_compat.h>
>
> #include "f2fs.h"
> #include "node.h"
> diff --git a/fs/squashfs/zstd_wrapper.c b/fs/squashfs/zstd_wrapper.c
> index b7cb1faa652d..f8c512a6204e 100644
> --- a/fs/squashfs/zstd_wrapper.c
> +++ b/fs/squashfs/zstd_wrapper.c
> @@ -11,7 +11,7 @@
> #include <linux/mutex.h>
> #include <linux/bio.h>
> #include <linux/slab.h>
> -#include <linux/zstd.h>
> +#include <linux/zstd_compat.h>
> #include <linux/vmalloc.h>
>
> #include "squashfs_fs.h"
> diff --git a/include/linux/zstd_compat.h b/include/linux/zstd_compat.h
> new file mode 100644
> index 000000000000..cda9208bf04a
> --- /dev/null
> +++ b/include/linux/zstd_compat.h
> @@ -0,0 +1,116 @@
> +/*
> + * Copyright (c) 2016-present, Facebook, Inc.
> + * All rights reserved.
> + *
> + * This source code is licensed under the BSD-style license found in the
> + * LICENSE file in the root directory of https://github.com/facebook/zstd.
> + * An additional grant of patent rights can be found in the PATENTS file in the
> + * same directory.
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation. This program is dual-licensed; you may select
> + * either version 2 of the GNU General Public License ("GPL") or BSD license
> + * ("BSD").
> + */
> +
> +#ifndef ZSTD_COMPAT_H
> +#define ZSTD_COMPAT_H
> +
> +#include <linux/zstd.h>
> +
> +#if defined(ZSTD_VERSION_NUMBER) && (ZSTD_VERSION_NUMBER >= 10406)
> +/*
> + * This header provides backwards compatibility for the zstd-1.4.6 library
> + * upgrade. This header allows us to upgrade the zstd library version without
> + * modifying any callers. Then we will migrate callers from the compatibility
> + * wrapper one at a time until none remain. At which point we will delete this
> + * header.
> + *
> + * It is temporary and will be deleted once the upgrade is complete.
> + */
> +
> +#include <linux/zstd_errors.h>
> +
> +static inline size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters compression_params)
> +{
> + return ZSTD_estimateCCtxSize_usingCParams(compression_params);
> +}
> +
> +static inline size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters compression_params)
> +{
> + return ZSTD_estimateCStreamSize_usingCParams(compression_params);
> +}
> +
> +static inline size_t ZSTD_DCtxWorkspaceBound(void)
> +{
> + return ZSTD_estimateDCtxSize();
> +}
> +
> +static inline size_t ZSTD_DStreamWorkspaceBound(unsigned long long window_size)
> +{
> + return ZSTD_estimateDStreamSize(window_size);
> +}
> +
> +static inline ZSTD_CCtx* ZSTD_initCCtx(void* wksp, size_t wksp_size)
> +{
> + if (wksp == NULL)
> + return NULL;
> + return ZSTD_initStaticCCtx(wksp, wksp_size);
> +}
> +
> +static inline ZSTD_CStream* ZSTD_initCStream_compat(ZSTD_parameters params, uint64_t pledged_src_size, void* wksp, size_t wksp_size)
> +{
> + ZSTD_CStream* cstream;
> + size_t ret;
> +
> + if (wksp == NULL)
> + return NULL;
> +
> + cstream = ZSTD_initStaticCStream(wksp, wksp_size);
> + if (cstream == NULL)
> + return NULL;
> +
> + /* 0 means unknown in old API but means 0 in new API */
> + if (pledged_src_size == 0)
> + pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
> +
> + ret = ZSTD_initCStream_advanced(cstream, NULL, 0, params, pledged_src_size);
> + if (ZSTD_isError(ret))
> + return NULL;
> +
> + return cstream;
> +}
> +#define ZSTD_initCStream ZSTD_initCStream_compat
> +
> +static inline ZSTD_DCtx* ZSTD_initDCtx(void* wksp, size_t wksp_size)
> +{
> + if (wksp == NULL)
> + return NULL;
> + return ZSTD_initStaticDCtx(wksp, wksp_size);
> +}
> +
> +static inline ZSTD_DStream* ZSTD_initDStream_compat(unsigned long long window_size, void* wksp, size_t wksp_size)
> +{
> + if (wksp == NULL)
> + return NULL;
> + (void)window_size;
> + return ZSTD_initStaticDStream(wksp, wksp_size);
> +}
> +#define ZSTD_initDStream ZSTD_initDStream_compat
> +
> +typedef ZSTD_frameHeader ZSTD_frameParams;
> +
> +static inline size_t ZSTD_getFrameParams(ZSTD_frameParams* frame_params, const void* src, size_t src_size)
> +{
> + return ZSTD_getFrameHeader(frame_params, src, src_size);
> +}
> +
> +static inline size_t ZSTD_compressCCtx_compat(ZSTD_CCtx* cctx, void* dst, size_t dst_capacity, const void* src, size_t src_size, ZSTD_parameters params)
> +{
> + return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, params);
> +}
> +#define ZSTD_compressCCtx ZSTD_compressCCtx_compat
> +
> +#endif /* ZSTD_VERSION_NUMBER >= 10406 */
> +#endif /* ZSTD_COMPAT_H */
> diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
> index 790abc472f5b..6bb805aeec08 100644
> --- a/lib/decompress_unzstd.c
> +++ b/lib/decompress_unzstd.c
> @@ -77,7 +77,7 @@
>
> #include <linux/decompress/mm.h>
> #include <linux/kernel.h>
> -#include <linux/zstd.h>
> +#include <linux/zstd_compat.h>
>
> /* 128MB is the maximum window size supported by zstd. */
> #define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WINDOWLOG_MAX)
> --
> 2.28.0
>
---end quoted text---

2020-11-06 18:51:58

by Nick Terrell

[permalink] [raw]
Subject: Re: [GIT PULL][PATCH v5 0/9] Update to zstd-1.4.6

> On Nov 6, 2020, at 9:15 AM, Josef Bacik <[email protected]> wrote:
>
> On 11/3/20 1:05 AM, Nick Terrell wrote:
>> From: Nick Terrell <[email protected]>
>> Please pull from
>> [email protected]:terrelln/linux.git tags/v5-zstd-1.4.6
>> to get these changes. Alternatively the patchset is included.
>
> Where did we come down on the code formatting question? Personally I'm of the mind that as long as the consumers themselves adhere to the proper coding style I'm fine not maintaining the code style as long as we get the benefit of easily syncing in code from the upstream project. Thanks,

The general consensus of everyone who has been involved in the discussion so far, seems to be that the benefits of keeping zstd in-sync with upstream outweigh the cost of accepting upstream’s API, though not everyone agrees. The alternative is to provide a wrapper around upstream’s API, but this makes it slightly harder to debug, since you have to go through the wrapper whose only purpose is to adapt to the coding style, and allows bugs to sneak into the kernel implementation, which aren’t present upstream.

Additionally, in 2017 LZ4 switched to using upstream LZ4’s API in order to stay up to date with upstream, which sets precedent. I also help maintain LZ4, and once the zstd update is merged, I plan to work on making it easier to update LZ4 in the kernel when upstream updates. That will be a much smaller change, since LZ4 is already nearly using upstream’s code directly.

Best,
Nick

2020-11-09 19:03:42

by Chris Mason

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper



On 6 Nov 2020, at 13:38, Christoph Hellwig wrote:

> You just keep resedning this crap, don't you? Haven't you been told
> multiple times to provide a proper kernel API by now?

You do consistently ask for a shim layer, but you haven’t explained
what we gain by diverging from the documented and tested API of the
upstream zstd project. It’s an important discussion given that we
hope to regularly update the kernel side as they make improvements in
zstd.

The only benefit described so far seems to be camelcase related, but if
there are problems in the API beyond that, I haven’t seen you describe
them. I don’t think the camelcase alone justifies the added costs of
the shim.

-chris

2020-11-10 15:28:06

by David Sterba

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

On Mon, Nov 09, 2020 at 02:01:41PM -0500, Chris Mason wrote:
> On 6 Nov 2020, at 13:38, Christoph Hellwig wrote:
> > You just keep resedning this crap, don't you? Haven't you been told
> > multiple times to provide a proper kernel API by now?
>
> You do consistently ask for a shim layer, but you haven’t explained
> what we gain by diverging from the documented and tested API of the
> upstream zstd project. It’s an important discussion given that we
> hope to regularly update the kernel side as they make improvements in
> zstd.
>
> The only benefit described so far seems to be camelcase related, but if
> there are problems in the API beyond that, I haven’t seen you describe
> them. I don’t think the camelcase alone justifies the added costs of
> the shim.

The API change in this patchset is adding churn that wouldn't be
necessary if there were an upstream<->kernel API from the beginning.

The patch 5/9 is almost entirely renaming just some internal identifiers

- ZSTD_CStreamWorkspaceBound(params.cParams),
- ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
+ ZSTD_estimateCStreamSize_usingCParams(params.cParams),
+ ZSTD_estimateDStreamSize(ZSTD_BTRFS_MAX_INPUT));

plus updating the names in the error strings. The compression API that
filesystems need is simple:

- set up workspace and parameters
- compress buffer
- decompress buffer

We really should not care if upstream has 3 functions for initializing
stream (ZSTD_initCStream/ZSTD_initStaticCStream/ZSTD_initCStream_advanced),
or if the name changes again in the future.

This should not require explicit explanation, this should be a natural
requirement especially for separate projects that don't share the same
coding style but have to be integrated in some way.

2020-11-10 18:40:49

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

On Mon, Nov 09, 2020 at 02:01:41PM -0500, Chris Mason wrote:
> You do consistently ask for a shim layer, but you haven???t explained what
> we gain by diverging from the documented and tested API of the upstream zstd
> project. It???s an important discussion given that we hope to regularly
> update the kernel side as they make improvements in zstd.

An API that looks like every other kernel API, and doesn't cause endless
amount of churn because someone decided they need a new API flavor of
the day. Btw, I'm not asking for a shim layer - that was the compromise
we ended up with.

If zstd folks can't maintain a sane code base maybe we should just drop
this childish churning code base from the tree.

2020-11-10 19:26:44

by Chris Mason

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

On 10 Nov 2020, at 13:39, Christoph Hellwig wrote:

> On Mon, Nov 09, 2020 at 02:01:41PM -0500, Chris Mason wrote:
>> You do consistently ask for a shim layer, but you haven???t explained
>> what
>> we gain by diverging from the documented and tested API of the
>> upstream zstd
>> project. It???s an important discussion given that we hope to
>> regularly
>> update the kernel side as they make improvements in zstd.
>
> An API that looks like every other kernel API, and doesn't cause
> endless
> amount of churn because someone decided they need a new API flavor of
> the day. Btw, I'm not asking for a shim layer - that was the
> compromise
> we ended up with.
>
> If zstd folks can't maintain a sane code base maybe we should just
> drop
> this childish churning code base from the tree.

I think APIs change based on the needs of the project. We do this all
the time in the kernel, and we don’t think twice about updating users
of the API as needed. The zstd changes look awkward and large today
because it’ a long time period, but we’ve all been pretty vocal in
the past about the importance of being able to advance APIs.

-chris

2020-11-10 19:35:41

by Nick Terrell

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

> On Nov 10, 2020, at 7:25 AM, David Sterba <[email protected]> wrote:
>
> On Mon, Nov 09, 2020 at 02:01:41PM -0500, Chris Mason wrote:
>> On 6 Nov 2020, at 13:38, Christoph Hellwig wrote:
>>> You just keep resedning this crap, don't you? Haven't you been told
>>> multiple times to provide a proper kernel API by now?
>>
>> You do consistently ask for a shim layer, but you haven’t explained
>> what we gain by diverging from the documented and tested API of the
>> upstream zstd project. It’s an important discussion given that we
>> hope to regularly update the kernel side as they make improvements in
>> zstd.
>>
>> The only benefit described so far seems to be camelcase related, but if
>> there are problems in the API beyond that, I haven’t seen you describe
>> them. I don’t think the camelcase alone justifies the added costs of
>> the shim.
>
> The API change in this patchset is adding churn that wouldn't be
> necessary if there were an upstream<->kernel API from the beginning.
>
> The patch 5/9 is almost entirely renaming just some internal identifiers
>
> - ZSTD_CStreamWorkspaceBound(params.cParams),
> - ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
> + ZSTD_estimateCStreamSize_usingCParams(params.cParams),
> + ZSTD_estimateDStreamSize(ZSTD_BTRFS_MAX_INPUT));
>
> plus updating the names in the error strings. The compression API that
> filesystems need is simple:
>
> - set up workspace and parameters
> - compress buffer
> - decompress buffer
>
> We really should not care if upstream has 3 functions for initializing
> stream (ZSTD_initCStream/ZSTD_initStaticCStream/ZSTD_initCStream_advanced),
> or if the name changes again in the future.

Upstream will not change these function names. We guarantee the stable
portion of our API has a fixed ABI. The unstable portion doesn’t make this
guarantee, but we guarantee never to change function semantics in an
incompatible way, and to provide long deprecation periods (years) when we
delete functions.

For reference, the only functions we’ve deleted/modified since v1.0.0 when we
stabilized the zstd format 4 years ago are an old streaming API that was
deprecated before v1.0.0. We’ve added new functions, and provided new
recommended ways to use our API that we think are better. But, we highly
value not breaking our users code, so all the older APIs are still supported.

This churn is caused because the current version of zstd inside the kernel is
not upstream zstd. At the time of integration upstream zstd wasn’t ready to
be used as-is in the kernel. When I integrated zstd into the kernel, I should’ve
done more work to use upstream as-is. It was a mistake that I would like to
correct, so the kernel can benefit from the significant performance
improvements that upstream has made in the last few years.

> This should not require explicit explanation, this should be a natural
> requirement especially for separate projects that don't share the same
> coding style but have to be integrated in some way.

I’m not completely against providing a kernel shim. Personally, I don’t believe
it provides much benefit. But if the consensus of kernel developers is that a
shim provides a better API, then I’m happy to provide it. So far, I haven’t seen
a clear consensus either way. That leaves me kind of stuck.

Best,
Nick

2020-11-16 16:55:50

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v5 1/9] lib: zstd: Add zstd compatibility wrapper

On Tue, Nov 10, 2020 at 02:24:35PM -0500, Chris Mason wrote:
> I think APIs change based on the needs of the project. We do this all the
> time in the kernel, and we don???t think twice about updating users of the
> API as needed.

We update kernel APIs when:

- we need additional functionality
- thew new API is clearly better than the old one

None of that seems to be the case here.