Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp275633pxb; Mon, 2 Nov 2020 22:06:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJysjTcYyK2bv0ZYyTxSs/msS+2asH4ZrNzm6brxH+AWAYJ9rEvL2xWlMWoGjKIiwKaEsbYw X-Received: by 2002:a50:8f61:: with SMTP id 88mr20521564edy.175.1604383576963; Mon, 02 Nov 2020 22:06:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604383576; cv=none; d=google.com; s=arc-20160816; b=knkNgBxYQSH1g4e+jaWPX/cOp3MvQcP9t5+EpVb/FRJ0OBKtcwBejC1K0bA11vZ2Ql I2aApdP8OjbUoWYaYG37lsLjCHgAP5C12G/jfPzZNOkujBlIg4ODLBrCqNkhfNPcui8q shaTMiYxM0hxpSSddG47UGGJ/JOyMVQYPtqUi+2HpJPK7wbYB9MlRlBxI+MSdOmXql2k JbPhNKzYtv9iz17CuLsjHPBya1hAY7o1ZsYOkMCZOQbk6r5yiTprmyHyu9TYN68vdg5V 6PQdj3ypMMKIJPUoQPjsTJfUFouxkUDry5M1tCBMk3kuO3UA2wDwo+JUOhBjGpPyubTp uozg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tfz5JoPFBe+5NZSwhHeml9GZoHiera3KaxD144JrSCQ=; b=UsyLw8+tvTLWE3yrWgHKWykZ7/PiKZ968niSjoBhV9t2nBr0ysf78qigqtyxi+f78c yQPmX8IzWCX/NEjf8aTqfdkuvIct3VEY3vQ+qUcexryWd+AspwqfXZ5izGYYbhZn3j8Y 5yXs46iOCpHA5ynMeBUFnuaq74sA+Mw7X85seLxoOuzb8c2ZqhrK7NzdVDEfCDCLW2Fb UM38s1GiWuHePTGI/LVEEBnJHzjTZa7iXrNVuwWYAad3K/5SYTbWXWhybIsIYjfdzu0h J71goy/4zTKS1QycFmSKwivB98PrwE0vcqXLGaF59t3FvUcPJVItVFZQwle2n6LqTH81 lQ6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gSSV2Azc; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cf25si13268957ejb.733.2020.11.02.22.05.53; Mon, 02 Nov 2020 22:06:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gSSV2Azc; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727589AbgKCGCd (ORCPT + 99 others); Tue, 3 Nov 2020 01:02:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727543AbgKCGCW (ORCPT ); Tue, 3 Nov 2020 01:02:22 -0500 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26982C061A47; Mon, 2 Nov 2020 22:02:21 -0800 (PST) Received: by mail-pg1-x542.google.com with SMTP id g12so12837065pgm.8; Mon, 02 Nov 2020 22:02:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tfz5JoPFBe+5NZSwhHeml9GZoHiera3KaxD144JrSCQ=; b=gSSV2AzcSgKjWbA0/duEx/32yEqKcFXKR17Ctdoe1pcX/lfA9NvUrSBAIAJSl1Z7tj H2ui0Evi/5PTqJlwsDtcpVN419Jbs3OELOwS5RGAVngk2jRvNTKb/ZkAsm8Oe0/fXi4+ ckqV6Ge2q4B+j+A0/CRILjnvPQQHcR9Nw2Igf4qVYN/H3Rf8+LYVEcSdySSXengisu8U 64/db4Qsod9qGVW7k1Py7tK0/mg2MC2id1BrP7MklQRcBwyF3leReGVmsyiWK2RL8ZT/ d3x+aGj5IWmdxpL1LHBxjSXE4bFbeCYbSpdJXO5s+2vkwRlfEyRmCqBIVGgf8SoSrYxP NLxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tfz5JoPFBe+5NZSwhHeml9GZoHiera3KaxD144JrSCQ=; b=H1JevOqAZJa0HGPW2spY5mqIHK12Zja+YOEH4k1uRl505QDhZieL4E2J+QNHK0UHyp HG99BWXzwMo5LIhStkdlHAE7YHItwR5wkxJ4+bs4fUI9LqjQBLaOBB05Sq9N5IdXfOR+ gqndSzaLDv13EWltd5KAP3+5Fk5IkxxkvYz9pGCW2PNQoXABmmr3o1g89dQuript3eOW dkQuRPOOvYqiWPBBR3RxA3lWODb+kUrgBQVPrf3/cDiqSQ1R9D/nIrtdtHieY1w46GQK 4TuA6kSXdURQ+p4afUqQoC1LIkISZBio5eV1VbREJENcFdkLR7Azx8DmpsyNoJsH3T8k E6nw== X-Gm-Message-State: AOAM5315S1gSNEGEBmLY7+bXrZUXyA5YrOusPKPuMr/0n8X6KJE19O8I n6vuvFuYshTGomMpvpZGzgs= X-Received: by 2002:a63:389:: with SMTP id 131mr3966874pgd.128.1604383340660; Mon, 02 Nov 2020 22:02:20 -0800 (PST) Received: from nickserv.localdomain (c-98-33-101-203.hsd1.ca.comcast.net. [98.33.101.203]) by smtp.gmail.com with ESMTPSA id b16sm15647269pfp.195.2020.11.02.22.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 22:02:20 -0800 (PST) From: Nick Terrell To: Herbert Xu Cc: linux-crypto@vger.kernel.org, linux-btrfs@vger.kernel.org, squashfs-devel@lists.sourceforge.net, linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Kernel Team , Nick Terrell , Nick Terrell , Chris Mason , Petr Malat , Johannes Weiner , Niket Agarwal , Yann Collet Subject: [PATCH v5 6/9] f2fs: zstd: Switch to the zstd-1.4.6 API Date: Mon, 2 Nov 2020 22:05:32 -0800 Message-Id: <20201103060535.8460-7-nickrterrell@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201103060535.8460-1-nickrterrell@gmail.com> References: <20201103060535.8460-1-nickrterrell@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Nick Terrell 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 --- 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 #include #include -#include +#include +#include #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