From patchwork Wed May 8 07:41:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658175 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAAA836137 for ; Wed, 8 May 2024 07:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154159; cv=none; b=RKYyYJjgn4iFq8U1Ge0R3F4x/6ha0aVXL90QOuqjUXGypy8IxpPi21oiA6o7b8aYvYASnwQvaJHFK7SJRsws7RhwTI+N38tffAbBJt4h/ahIg1qr2eVfL89G/40HcYbyeWSPPF/FXHbPb/J/Y8VpEEpKYQhvITzmx2LXwsHBYkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154159; c=relaxed/simple; bh=8qI5x0BGUJs1JbjZreFI8Kvjdzxo5r4XEf1ADBnlo2w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VXfjQVILanS6Jtc0d6jfxB5M0ObQaevjxYrYWIWJ1D/fFccYedVIBtJcShynO/hCtEzqkmNsp5fCu5Kcxxd07ZDkkeRuisNwvocnjFpCWW2jIEAARjOG00AOsOg5f7Z/aP9cy+4B1p3O+MDjePx0mOiqwnanbub6j2PvrT1BHlI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=jmm3v1wP; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="jmm3v1wP" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-613a6bb2947so3291559a12.3 for ; Wed, 08 May 2024 00:42:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154157; x=1715758957; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cJ4glZLOzUHMCYOraHYCv1mLDw/pz7BmHlcE0TlYg0c=; b=jmm3v1wPTswp98gKa0FAzNTfLpNoYjNxAZJ9OPfRkLXDNkLZYRrT7sV8GY+db5BZ+h 74oK2DTmnhErs3kDqP4cZCYdlUHSlDeQ+DbjJtg7VjCeKbkOK4w4Pa85HHEZdqmVJih6 AzHsbEOSaPXbUCYJyaDYmc6dqMSFKldQ7i32I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154157; x=1715758957; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cJ4glZLOzUHMCYOraHYCv1mLDw/pz7BmHlcE0TlYg0c=; b=GA4HhSo6DtwZGRunUwyQCDT2qx4auwy5of+90h8PD8YeTscfYsX6i0kp5ftCg5A/0M 8nxUeG8M3lwVDDVe9vWEis0oeJ6YpwcxXr41rf1u5ZDEbnnRqLTdMgeICxty5xLhyS3O /cnywgHSUnPfbRPgHfKLk7MigAU/F6TE1Hv7WujJTYDC8I/QOaApE6RvoLQy0jMHFch9 knYoN+sUkul2FQiZGsSDcyfBG6jI+G0U0E1IIvFrjnUc8dVCLHt5CxMszZt+u0/ltkR+ ztfkTcRZhPEjNpMnkS1q4Yac0VBfcsiGsZXxTXcbvoMnnF4R4BRaPEFMOilT4Oli72Zq 9ksQ== X-Forwarded-Encrypted: i=1; AJvYcCVNZ06pvwPCLloX/6BZtOF4fx7pG8j+0PY2baAIbL6RZu9MS5gZne0g5Y4JedTZie11GS67RGGor9okk4Uot4Z7kTWZ5Ic4P9XWKF4= X-Gm-Message-State: AOJu0YxiL6RVcfe5xrBG+pKCxmQynddlTBhpfEzb3lRI2sa0QFUqt37Z DcdxfzJhw93RbsIV5m+nfyMr6JL99OLovNrh0DQf09pEMtC2ZAkliXyg+v8+aw== X-Google-Smtp-Source: AGHT+IHUK7vI93zA/+lEn1Auf/udklwRoiX5cMMc77qGrx/aNrnZ9Z2lshmtBccQu4T5nrwPaWMOLA== X-Received: by 2002:a17:90a:d196:b0:2b6:2069:f825 with SMTP id 98e67ed59e1d1-2b62069f8fdmr1287390a91.8.1715154157063; Wed, 08 May 2024 00:42:37 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:36 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 01/19] zram: move from crypto API to custom comp backends API Date: Wed, 8 May 2024 16:41:54 +0900 Message-ID: <20240508074223.652784-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Crypto API is beautiful and powerful, however, being a generic API, it lacks support for fine-grained per-algorithm configuration. A number of compression algorithms provide various knobs to tune characteristics for particular data patterns. The simplest case is "compression level". A more complicated and interesting case is user-space trained dictionaries (e.g. lz4 and zstd). Moving to custom backends implementation gives us ability to have our own minimalistic and extendable API, and algorithms tunings becomes possible. The list of compression backends is empty at this point, we will add backends in the followup patches. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 39 +---------- drivers/block/zram/zcomp.c | 127 +++++++++++++--------------------- drivers/block/zram/zcomp.h | 26 +++++-- drivers/block/zram/zram_drv.c | 9 ++- 4 files changed, 73 insertions(+), 128 deletions(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 7b29cce60ab2..8ecb74f83a5e 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -2,7 +2,6 @@ config ZRAM tristate "Compressed RAM block device support" depends on BLOCK && SYSFS && MMU - depends on CRYPTO_LZO || CRYPTO_ZSTD || CRYPTO_LZ4 || CRYPTO_LZ4HC || CRYPTO_842 select ZSMALLOC help Creates virtual block devices called /dev/zramX (X = 0, 1, ...). @@ -15,45 +14,9 @@ config ZRAM See Documentation/admin-guide/blockdev/zram.rst for more information. -choice - prompt "Default zram compressor" - default ZRAM_DEF_COMP_LZORLE - depends on ZRAM - -config ZRAM_DEF_COMP_LZORLE - bool "lzo-rle" - depends on CRYPTO_LZO - -config ZRAM_DEF_COMP_ZSTD - bool "zstd" - depends on CRYPTO_ZSTD - -config ZRAM_DEF_COMP_LZ4 - bool "lz4" - depends on CRYPTO_LZ4 - -config ZRAM_DEF_COMP_LZO - bool "lzo" - depends on CRYPTO_LZO - -config ZRAM_DEF_COMP_LZ4HC - bool "lz4hc" - depends on CRYPTO_LZ4HC - -config ZRAM_DEF_COMP_842 - bool "842" - depends on CRYPTO_842 - -endchoice - config ZRAM_DEF_COMP string - default "lzo-rle" if ZRAM_DEF_COMP_LZORLE - default "zstd" if ZRAM_DEF_COMP_ZSTD - default "lz4" if ZRAM_DEF_COMP_LZ4 - default "lzo" if ZRAM_DEF_COMP_LZO - default "lz4hc" if ZRAM_DEF_COMP_LZ4HC - default "842" if ZRAM_DEF_COMP_842 + default "unset-value" config ZRAM_WRITEBACK bool "Write back incompressible or idle page to backing device" diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 8237b08c49d8..0d0d2e6dbaa9 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -15,31 +15,16 @@ #include "zcomp.h" -static const char * const backends[] = { -#if IS_ENABLED(CONFIG_CRYPTO_LZO) - "lzo", - "lzo-rle", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_LZ4) - "lz4", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC) - "lz4hc", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_842) - "842", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_ZSTD) - "zstd", -#endif +static struct zcomp_backend *backends[] = { + NULL }; -static void zcomp_strm_free(struct zcomp_strm *zstrm) +static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) { - if (!IS_ERR_OR_NULL(zstrm->tfm)) - crypto_free_comp(zstrm->tfm); + if (zstrm->ctx) + comp->backend->destroy_ctx(zstrm->ctx); vfree(zstrm->buffer); - zstrm->tfm = NULL; + zstrm->ctx = NULL; zstrm->buffer = NULL; } @@ -47,60 +32,55 @@ static void zcomp_strm_free(struct zcomp_strm *zstrm) * Initialize zcomp_strm structure with ->tfm initialized by backend, and * ->buffer. Return a negative value on error. */ -static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp) +static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) { - zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0); + zstrm->ctx = comp->backend->create_ctx(); + /* * allocate 2 pages. 1 for compressed data, plus 1 extra for the * case when compressed size is larger than the original one */ zstrm->buffer = vzalloc(2 * PAGE_SIZE); - if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) { - zcomp_strm_free(zstrm); + if (!zstrm->ctx || !zstrm->buffer) { + zcomp_strm_free(comp, zstrm); return -ENOMEM; } return 0; } +static struct zcomp_backend *lookup_backend(const char *comp) +{ + int i = 0; + + while (backends[i]) { + if (sysfs_streq(comp, backends[i]->name)) + break; + i++; + } + return backends[i]; +} + bool zcomp_available_algorithm(const char *comp) { - /* - * Crypto does not ignore a trailing new line symbol, - * so make sure you don't supply a string containing - * one. - * This also means that we permit zcomp initialisation - * with any compressing algorithm known to crypto api. - */ - return crypto_has_comp(comp, 0, 0) == 1; + return lookup_backend(comp) != NULL; } /* show available compressors */ ssize_t zcomp_available_show(const char *comp, char *buf) { - bool known_algorithm = false; ssize_t sz = 0; int i; - for (i = 0; i < ARRAY_SIZE(backends); i++) { - if (!strcmp(comp, backends[i])) { - known_algorithm = true; + for (i = 0; i < ARRAY_SIZE(backends) - 1; i++) { + if (!strcmp(comp, backends[i]->name)) { sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "[%s] ", backends[i]); + "[%s] ", backends[i]->name); } else { sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "%s ", backends[i]); + "%s ", backends[i]->name); } } - /* - * Out-of-tree module known to crypto api or a missing - * entry in `backends'. - */ - if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1) - sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "[%s] ", comp); - - sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n"); return sz; } @@ -115,8 +95,8 @@ void zcomp_stream_put(struct zcomp *comp) local_unlock(&comp->stream->lock); } -int zcomp_compress(struct zcomp_strm *zstrm, - const void *src, unsigned int *dst_len) +int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int *dst_len) { /* * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized @@ -132,21 +112,19 @@ int zcomp_compress(struct zcomp_strm *zstrm, * the dst buffer, zram_drv will take care of the fact that * compressed buffer is too big. */ - *dst_len = PAGE_SIZE * 2; + size_t dlen = PAGE_SIZE * 2; + int ret; - return crypto_comp_compress(zstrm->tfm, - src, PAGE_SIZE, - zstrm->buffer, dst_len); + ret = comp->backend->compress(zstrm->ctx, src, zstrm->buffer, &dlen); + if (!ret) + *dst_len = dlen; + return ret; } -int zcomp_decompress(struct zcomp_strm *zstrm, - const void *src, unsigned int src_len, void *dst) +int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int src_len, void *dst) { - unsigned int dst_len = PAGE_SIZE; - - return crypto_comp_decompress(zstrm->tfm, - src, src_len, - dst, &dst_len); + return comp->backend->decompress(zstrm->ctx, src, src_len, dst); } int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) @@ -158,7 +136,7 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) zstrm = per_cpu_ptr(comp->stream, cpu); local_lock_init(&zstrm->lock); - ret = zcomp_strm_init(zstrm, comp); + ret = zcomp_strm_init(comp, zstrm); if (ret) pr_err("Can't allocate a compression stream\n"); return ret; @@ -170,7 +148,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node) struct zcomp_strm *zstrm; zstrm = per_cpu_ptr(comp->stream, cpu); - zcomp_strm_free(zstrm); + zcomp_strm_free(comp, zstrm); return 0; } @@ -199,32 +177,21 @@ void zcomp_destroy(struct zcomp *comp) kfree(comp); } -/* - * search available compressors for requested algorithm. - * allocate new zcomp and initialize it. return compressing - * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL) - * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in - * case of allocation error, or any other error potentially - * returned by zcomp_init(). - */ struct zcomp *zcomp_create(const char *alg) { struct zcomp *comp; int error; - /* - * Crypto API will execute /sbin/modprobe if the compression module - * is not loaded yet. We must do it here, otherwise we are about to - * call /sbin/modprobe under CPU hot-plug lock. - */ - if (!zcomp_available_algorithm(alg)) - return ERR_PTR(-EINVAL); - comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL); if (!comp) return ERR_PTR(-ENOMEM); - comp->name = alg; + comp->backend = lookup_backend(alg); + if (!comp->backend) { + kfree(comp); + return ERR_PTR(-EINVAL); + } + error = zcomp_init(comp); if (error) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index e9fe63da0e9b..757b85017e23 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -12,13 +12,26 @@ struct zcomp_strm { local_lock_t lock; /* compression/decompression buffer */ void *buffer; - struct crypto_comp *tfm; + void *ctx; +}; + +struct zcomp_backend { + int (*compress)(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len); + + int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst); + + void *(*create_ctx)(void); + void (*destroy_ctx)(void *ctx); + + const char *name; }; /* dynamic per-device compression frontend */ struct zcomp { struct zcomp_strm __percpu *stream; - const char *name; + struct zcomp_backend *backend; struct hlist_node node; }; @@ -33,10 +46,9 @@ void zcomp_destroy(struct zcomp *comp); struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); void zcomp_stream_put(struct zcomp *comp); -int zcomp_compress(struct zcomp_strm *zstrm, - const void *src, unsigned int *dst_len); - -int zcomp_decompress(struct zcomp_strm *zstrm, - const void *src, unsigned int src_len, void *dst); +int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int *dst_len); +int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int src_len, void *dst); #endif /* _ZCOMP_H_ */ diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 4cf38f7d3e0a..298ef0e97e03 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1342,7 +1342,8 @@ static int zram_read_from_zspool(struct zram *zram, struct page *page, ret = 0; } else { dst = kmap_local_page(page); - ret = zcomp_decompress(zstrm, src, size, dst); + ret = zcomp_decompress(zram->comps[prio], zstrm, + src, size, dst); kunmap_local(dst); zcomp_stream_put(zram->comps[prio]); } @@ -1429,7 +1430,8 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) compress_again: zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]); src = kmap_local_page(page); - ret = zcomp_compress(zstrm, src, &comp_len); + ret = zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm, + src, &comp_len); kunmap_local(src); if (unlikely(ret)) { @@ -1616,7 +1618,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, num_recomps++; zstrm = zcomp_stream_get(zram->comps[prio]); src = kmap_local_page(page); - ret = zcomp_compress(zstrm, src, &comp_len_new); + ret = zcomp_compress(zram->comps[prio], zstrm, + src, &comp_len_new); kunmap_local(src); if (ret) { From patchwork Wed May 8 07:41:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658176 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 680A63B1AE for ; Wed, 8 May 2024 07:42:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154163; cv=none; b=UnfvPSrxL111H9Fw/rH5qHM4BoIXWNKrQhZUBvujaqNHu7OYtTdVpUV0VkuyIh6DfSBvoJJwx21PNLiWfUN8ds91gjgKMwltfgJSxTF5xqcC0ljV8dC111vP0+jDobv/4DDxlUj5iAYUXXo7haZxgzBHpMlL20z+ajXUkxF6PQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154163; c=relaxed/simple; bh=fwqjI0oJhsJ6IWcPj2TUi+YtZmx3QQeidZjvgSy1/xc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h45rEsjSgwpPaUD4PdYXS4V5mXPUuIZel+37AAZt0JzYd3AvRdcCuXOg/6mHaNMAchdrvj8Ieey0MSOcAj6QrMsnmazuyTfp8hm4iH1j2qrphsftqLBOTUgYri/fyNaWgQ3qK23LQIabaDFdhksQHoBnXCimW3pB9n+PZslrNDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=O00YgaVt; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="O00YgaVt" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2b2bc7b37bcso3492566a91.2 for ; Wed, 08 May 2024 00:42:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154160; x=1715758960; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wUr6LRaWPPkzD2etszVDMza5JPEXKiMrJmq3EUCQIHI=; b=O00YgaVty94AsK17uKUCbD/JhgikEa8rc0N6DCZ8mnY5qKj6l/e5HC3Xov9iwHksMa 6kn4utRXVlizLXtKRFcm5A03l/Li14cq7SIzcrbGmQPyqsjyJxvokavyEhYPBvNe3bbS S9stMTj3B539NTDhJHgGTZyfe0WiCgqU+WXGs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154160; x=1715758960; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wUr6LRaWPPkzD2etszVDMza5JPEXKiMrJmq3EUCQIHI=; b=NLW+JjI3dIjxmXtiev2/Qq8Wcrfb/YidJFofjHozDuRmLZvOp5eiJ+pyiv7O62uWKj y9FpWrQX5YKTrW9jdMxHWYcUrmXTT39NoqZCOofDlNaJrmDr3NbgvF6uSvGcdcpFQzLO /xP7rDRN/8qmFid7znO3pBB+5tqjeJIFWmDNBiyld7GKGQ5ZL4blewS0h0nGVUp8r08T 9/i52EqY4ThZdKDfkf8eMYg6mhK0hkAZ7YGluYyo8YAHsHD50ZJmMvX6D4LxFaiSla3Q wxWSTTH5dvRe1HVjZVm2jB8JJBd3cLte2/SYkhffQHStELai+4/0eCfV54IduSd6M4wr 0YUg== X-Forwarded-Encrypted: i=1; AJvYcCXWZP3qjJmZ+T9zImBYHsAYKXQaYgC3DAVv9gRrvgz4ByRrdF5hFtD4iE9LiKFOYJsR1q/nZWT1UiwkXVQbM6C3RIcwQJ6WF2Fj8RQ= X-Gm-Message-State: AOJu0Yw/DdohPTpME5irCDxmjqq4U3slkTtjhpGRVd/nIkT3ybZVDI15 b1pC4WUyDAhKDDVWHJYHYn8h6fNjolOg098PC3+es12L9kAHiipmFFgqepcpcA== X-Google-Smtp-Source: AGHT+IFK1adbyQut7TJL1cvR0x0aBrGL77aQAU4STPf/S0CRUyW6lbjvBTtWy12MICu+3tfpN4KMvw== X-Received: by 2002:a17:90b:891:b0:2b4:abc7:d64b with SMTP id 98e67ed59e1d1-2b6163a0616mr1809932a91.6.1715154159769; Wed, 08 May 2024 00:42:39 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:39 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 02/19] zram: add lzo and lzorle compression backends support Date: Wed, 8 May 2024 16:41:55 +0900 Message-ID: <20240508074223.652784-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 23 +++++++++++++++ drivers/block/zram/Makefile | 3 ++ drivers/block/zram/backend_lzo.c | 44 +++++++++++++++++++++++++++++ drivers/block/zram/backend_lzo.h | 10 +++++++ drivers/block/zram/backend_lzorle.c | 44 +++++++++++++++++++++++++++++ drivers/block/zram/backend_lzorle.h | 10 +++++++ drivers/block/zram/zcomp.c | 7 +++++ 7 files changed, 141 insertions(+) create mode 100644 drivers/block/zram/backend_lzo.c create mode 100644 drivers/block/zram/backend_lzo.h create mode 100644 drivers/block/zram/backend_lzorle.c create mode 100644 drivers/block/zram/backend_lzorle.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 8ecb74f83a5e..5d329a887b12 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -14,8 +14,31 @@ config ZRAM See Documentation/admin-guide/blockdev/zram.rst for more information. +config ZRAM_BACKEND_LZO + bool "lzo and lzo-rle compression support" + depends on ZRAM + select LZO_COMPRESS + select LZO_DECOMPRESS + +choice + prompt "Default zram compressor" + default ZRAM_DEF_COMP_LZORLE + depends on ZRAM + +config ZRAM_DEF_COMP_LZORLE + bool "lzo-rle" + depends on ZRAM_BACKEND_LZO + +config ZRAM_DEF_COMP_LZO + bool "lzo" + depends on ZRAM_BACKEND_LZO + +endchoice + config ZRAM_DEF_COMP string + default "lzo-rle" if ZRAM_DEF_COMP_LZORLE + default "lzo" if ZRAM_DEF_COMP_LZO default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index de9e457907b1..2dcbc9b75d91 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o + zram-y := zcomp.o zram_drv.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c new file mode 100644 index 000000000000..b88b408964cd --- /dev/null +++ b/drivers/block/zram/backend_lzo.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include "backend_lzo.h" + +static void *lzo_create(void) +{ + return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); +} + +static void lzo_destroy(void *ctx) +{ + kfree(ctx); +} + +static int lzo_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, ctx); + return ret == LZO_E_OK ? 0 : ret; +} + +static int lzo_decompress(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + size_t dst_len = PAGE_SIZE; + int ret; + + ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len); + return ret == LZO_E_OK ? 0 : ret; +} + +struct zcomp_backend backend_lzo = { + .compress = lzo_compress, + .decompress = lzo_decompress, + .create_ctx = lzo_create, + .destroy_ctx = lzo_destroy, + .name = "lzo", +}; diff --git a/drivers/block/zram/backend_lzo.h b/drivers/block/zram/backend_lzo.h new file mode 100644 index 000000000000..377ccb7389e2 --- /dev/null +++ b/drivers/block/zram/backend_lzo.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZO_H__ +#define __BACKEND_LZO_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lzo; + +#endif /* __BACKEND_LZO_H__ */ diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c new file mode 100644 index 000000000000..9bf1843021b0 --- /dev/null +++ b/drivers/block/zram/backend_lzorle.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include "backend_lzorle.h" + +static void *lzorle_create(void) +{ + return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); +} + +static void lzorle_destroy(void *ctx) +{ + kfree(ctx); +} + +static int lzorle_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = lzorle1x_1_compress(src, PAGE_SIZE, dst, dst_len, ctx); + return ret == LZO_E_OK ? 0 : ret; +} + +static int lzorle_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + size_t dst_len = PAGE_SIZE; + int ret; + + ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len); + return ret == LZO_E_OK ? 0 : ret; +} + +struct zcomp_backend backend_lzorle = { + .compress = lzorle_compress, + .decompress = lzorle_decompress, + .create_ctx = lzorle_create, + .destroy_ctx = lzorle_destroy, + .name = "lzo-rle", +}; diff --git a/drivers/block/zram/backend_lzorle.h b/drivers/block/zram/backend_lzorle.h new file mode 100644 index 000000000000..5c1db65a38a4 --- /dev/null +++ b/drivers/block/zram/backend_lzorle.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZORLE_H__ +#define __BACKEND_LZORLE_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lzorle; + +#endif /* __BACKEND_LZORLE_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 0d0d2e6dbaa9..58fb3ac91f4b 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -15,7 +15,14 @@ #include "zcomp.h" +#include "backend_lzo.h" +#include "backend_lzorle.h" + static struct zcomp_backend *backends[] = { +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) + &backend_lzorle, + &backend_lzo, +#endif NULL }; From patchwork Wed May 8 07:41:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658177 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA0393E485 for ; Wed, 8 May 2024 07:42:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154164; cv=none; b=qDgLLBHsFFjER0QgcdMwQl3JCuWOEp/+UeexbYm/R3/AjEPKudUTz9wkhhqNq3YMm8AwZ9MxM4IRLxkCZvjHaLkjsgwlWxExOiaKhN7lbrr+570uu/EVSTaqgGGme9L+Ck+TgxQG2pWKZYOJVsRZFO6EUZotsXzKTrCyfN9vbrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154164; c=relaxed/simple; bh=DHL8kqd+RNQDM+Stwrc0IUTkOPk9/Cbf5eFqQlFrF4o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GLNW8Ov4Jb9gpaFKXCW1NqvocR7m/re8vOeh7f42Gr7L03GgopDht/eidXKlR3OWv1Rgz9XOSznHNTLN0OyiKsYgH2g7B4vKBegoKtAmlKFYBKqGnRaEpZnwJoxnhtHP0TXnEYvKEQMzGQl6c8RaNRh79gRqZgo1J9Irfxc7vDo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=XFNZJi4H; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XFNZJi4H" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2b37731c118so3114585a91.1 for ; Wed, 08 May 2024 00:42:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154162; x=1715758962; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sjjXoB1vjVFge1iRvQDKKfu041t9IBLi05g+C6qi2oA=; b=XFNZJi4HvIhzDQkvWmhx+ztq8DZ9wYJDktaj5M4DG1OqaAcygfmNWG/jq5Nwanyws0 0OSN7c6jpZtvl+DdV/gsRCrXblo3AVIsbl/J0OhomhyqMKk18gNH22PtMe8ozuS0MZRR glLRxPPwjgf/fXqEIx+zAVu5hup0/gadW1Hbg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154162; x=1715758962; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sjjXoB1vjVFge1iRvQDKKfu041t9IBLi05g+C6qi2oA=; b=ErHHbobuYvWEH2cJg5PhCwMgeRSKhN627qdnoky5ndK3VcXDOsn5bHmbeppqm4bPEA SGk9cGxXO5gEL+8nkBiCuHTJeqiSl4vXyTug8DJKlIls/ebVgplWcRzl10n3LzOg2/0P m/enjac0JCck+w9m3DWi4rZ/GwW1GTpVqDD5yzHsnnCd13OBkOnWDMyNBlCGx9VEZCam Jqko920d3dZqN/DadBdALXs5ZRBY/Di5YA0lu219WsrfrCaCHpqfr+TZ4CW27FJTK7vK dNmZjaQbDdiSWEYQQxC0VVrIfScvLbi04N+5Ks2uhJ0MER6yYJ9RMEc4SeR1Ds7c60nl iGmw== X-Forwarded-Encrypted: i=1; AJvYcCVG8snLm56mXGl3IAfL60rtBSaEj5067dxcPHhH09tActFyNqHhSI9J2ePm4TrZwhEJ7ht0Lz/ihW4KP6OwfxEMhhRvLHUSSPUPewc= X-Gm-Message-State: AOJu0YznoJS5sDqhHiIDSku8BUi99WMv2AFRzVlZ3vhnm7V0QKajQ73V Mlxm99B+SaeqtdV01Pd6owEOthDlHCpp/XAXcqqGbW7ZkgZxA1WZPvL3vMVYSQ== X-Google-Smtp-Source: AGHT+IEP96o73mZK4rztyE8+OYRQzbQHXnDNHKL0xwt4vZPXv9Ss/NhZbOzE9smjMNBdXZ+cLY8buQ== X-Received: by 2002:a17:90a:c001:b0:2b2:b1c7:5fd7 with SMTP id 98e67ed59e1d1-2b616be23b9mr1538063a91.30.1715154162123; Wed, 08 May 2024 00:42:42 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:41 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 03/19] zram: add lz4 compression backend support Date: Wed, 8 May 2024 16:41:56 +0900 Message-ID: <20240508074223.652784-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_lz4.c | 47 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4.h | 10 +++++++ drivers/block/zram/zcomp.c | 4 +++ 5 files changed, 73 insertions(+) create mode 100644 drivers/block/zram/backend_lz4.c create mode 100644 drivers/block/zram/backend_lz4.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 5d329a887b12..f1e76fc8431a 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -20,6 +20,12 @@ config ZRAM_BACKEND_LZO select LZO_COMPRESS select LZO_DECOMPRESS +config ZRAM_BACKEND_LZ4 + bool "lz4 compression support" + depends on ZRAM + select LZ4_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -33,12 +39,17 @@ config ZRAM_DEF_COMP_LZO bool "lzo" depends on ZRAM_BACKEND_LZO +config ZRAM_DEF_COMP_LZ4 + bool "lz4" + depends on ZRAM_BACKEND_LZ4 + endchoice config ZRAM_DEF_COMP string default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO + default "lz4" if ZRAM_DEF_COMP_LZ4 default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 2dcbc9b75d91..1be5d2657960 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c new file mode 100644 index 000000000000..697592dbabe2 --- /dev/null +++ b/drivers/block/zram/backend_lz4.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "backend_lz4.h" + +static void *lz4_create(void) +{ + return vmalloc(LZ4_MEM_COMPRESS); +} + +static void lz4_destroy(void *ctx) +{ + vfree(ctx); +} + +static int lz4_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = LZ4_compress_default(src, dst, PAGE_SIZE, *dst_len, ctx); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + int dst_len = PAGE_SIZE; + int ret; + + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (ret < 0) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_lz4 = { + .compress = lz4_compress, + .decompress = lz4_decompress, + .create_ctx = lz4_create, + .destroy_ctx = lz4_destroy, + .name = "lz4", +}; diff --git a/drivers/block/zram/backend_lz4.h b/drivers/block/zram/backend_lz4.h new file mode 100644 index 000000000000..a5fb5564835c --- /dev/null +++ b/drivers/block/zram/backend_lz4.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4_H__ +#define __BACKEND_LZ4_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4; + +#endif /* __BACKEND_LZ4_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 58fb3ac91f4b..902bdaf7e299 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -17,11 +17,15 @@ #include "backend_lzo.h" #include "backend_lzorle.h" +#include "backend_lz4.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) &backend_lzorle, &backend_lzo, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) + &backend_lz4, #endif NULL }; From patchwork Wed May 8 07:41:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658178 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F37E64594A for ; Wed, 8 May 2024 07:42:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154168; cv=none; b=cKAMLF4KLRHBI/u427kHO5sNJJro/xJnnZFNV3OHQ/gZFoYM903kAYfLn34B0TyySbvCKc2Yrse3n4rWfpA1bGhtnm1JcnBDEbAAslc1afLy91GNXmQo7YZcjMCbfQNSyhM0OXS7uRAld4qGxqBBZuI6Bp1nkmcktXld3YP+D6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154168; c=relaxed/simple; bh=FN4STGuvFZWpAy2rRpFsI/oXrQt4bTeq5tFn+4e9nzM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HpYememkT8/QcaJT0EAtZZASSgXc+T8mVrq9FTZFv+1woiY4b0BZ48dG/kWUyZyZi6edOYTZhQGXGLphTbcoiNnhk9N5ES8UJJDNrBERRaZce7bnfxxRl6CD9V20GM2x4WeSVqewEWEm/wrZ6Q8gazUjJs+ks3VW5gzlFS4iIKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=UTzrDjS1; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="UTzrDjS1" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-61f2dc31be4so408817a12.1 for ; Wed, 08 May 2024 00:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154165; x=1715758965; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=R3nECJbuYUQmjPzVo2jmTce+DVuwgWkYW9TFfzYmbzE=; b=UTzrDjS1wFYkc3K8jYt9SascaEcQEHjJBmAY/tZ0VDY+W2+sOMxxpwzLZdg2wkhwb+ Q75AOVYCRG8atiA+KrBDXQBqsInAIvr2CeCaS6v2GnWO0g00umbKcCqEOBZ7m3Cr4EYh 8HqjkXC6wuGQxKCWsz1MMMxVKDlI6TqfkAFVo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154165; x=1715758965; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R3nECJbuYUQmjPzVo2jmTce+DVuwgWkYW9TFfzYmbzE=; b=t4x9MzOiD8QAwhF/paFXz7avZHHvcJTBVwp7sQjS1+lh4RlIIxrtdzyCT63VHvEcix +OgimoROX8COuL4qBkeTHP5jpJ42eDY/i8Z4ppHkmxy6mjmY0SaMDXZTVzEE2LtIvpIR 9CD5KzLkpMoXABkd5eCEhCefZ2LJmfwJD9QMW81L0oXcWNZrKELDhwIWv+OT8peHk7et x7Ztj3+e031s88ScZmwUDsasrwhcu/Lg/9BOLbW3cHq+hjT43iTzTW6qbCQHcq9xQEpP tjh8ed11PCL7t9X+yjT0N0ulqjqHuxLWCBjiKHDKZ3uAgbrYxCytI0HwC2H6ZXtkgkfI QDAg== X-Forwarded-Encrypted: i=1; AJvYcCVfSLewepPazNAuPiUve2nhUvV/lBkBnB9yQtxpxEXg/Va9Tb5F9QVKanG0tigYmUZWLoQbKtzcjtXAlojtFrZYeS8JwbrGReN86vw= X-Gm-Message-State: AOJu0Yyr3QEmr3kNJQNaNQNvqge6/BOkoT16V/BA15rcIo5yskmP88uh JepF+f7wSpm6HoyEgFUpi9ka0BGUUXQLzq51F9IAJHzllOiNfl/Mt5+ZPyCRm7+rnCdi1Jajvzc = X-Google-Smtp-Source: AGHT+IE0MlFlPwSWQJRjErFW2GMMS4LLdcDfJb8bL7Or5RyoCHAaclgPc25OI2HOCjFzRoaD8dOoHg== X-Received: by 2002:a17:90a:1108:b0:2ab:8324:1b47 with SMTP id 98e67ed59e1d1-2b6119c8e82mr3128891a91.15.1715154165288; Wed, 08 May 2024 00:42:45 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:44 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 04/19] zram: add lz4hc compression backend support Date: Wed, 8 May 2024 16:41:57 +0900 Message-ID: <20240508074223.652784-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 5 ++- drivers/block/zram/backend_lz4hc.c | 72 ++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4hc.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 drivers/block/zram/backend_lz4hc.c create mode 100644 drivers/block/zram/backend_lz4hc.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index f1e76fc8431a..0d890a8d2dc4 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -26,6 +26,12 @@ config ZRAM_BACKEND_LZ4 select LZ4_COMPRESS select LZ4_DECOMPRESS +config ZRAM_BACKEND_LZ4HC + bool "lz4hc compression support" + depends on ZRAM + select LZ4HC_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -43,6 +49,10 @@ config ZRAM_DEF_COMP_LZ4 bool "lz4" depends on ZRAM_BACKEND_LZ4 +config ZRAM_DEF_COMP_LZ4HC + bool "lz4hc" + depends on ZRAM_BACKEND_LZ4HC + endchoice config ZRAM_DEF_COMP @@ -50,6 +60,7 @@ config ZRAM_DEF_COMP default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO default "lz4" if ZRAM_DEF_COMP_LZ4 + default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 1be5d2657960..815b45471c7d 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c new file mode 100644 index 000000000000..1735a2598443 --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include "backend_lz4hc.h" + +struct lz4hc_ctx { + void *mem; + s32 level; +}; + +static void lz4hc_destroy(void *ctx) +{ + struct lz4hc_ctx *zctx = ctx; + + vfree(zctx->mem); + kfree(zctx); +} + +static void *lz4hc_create(void) +{ + struct lz4hc_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx->mem) { + lz4hc_destroy(ctx); + return NULL; + } + + /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ + ctx->level = LZ4HC_DEFAULT_CLEVEL; + return ctx; +} + +static int lz4hc_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct lz4hc_ctx *zctx = ctx; + int ret; + + ret = LZ4_compress_HC(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4hc_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + int dst_len = PAGE_SIZE; + int ret; + + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (ret < 0) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_lz4hc = { + .compress = lz4hc_compress, + .decompress = lz4hc_decompress, + .create_ctx = lz4hc_create, + .destroy_ctx = lz4hc_destroy, + .name = "lz4hc", +}; diff --git a/drivers/block/zram/backend_lz4hc.h b/drivers/block/zram/backend_lz4hc.h new file mode 100644 index 000000000000..29c428a850e2 --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4HC_H__ +#define __BACKEND_LZ4HC_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4hc; + +#endif /* __BACKEND_LZ4HC_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 902bdaf7e299..f04f5844a23c 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -18,6 +18,7 @@ #include "backend_lzo.h" #include "backend_lzorle.h" #include "backend_lz4.h" +#include "backend_lz4hc.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -26,6 +27,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) &backend_lz4, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) + &backend_lz4hc, #endif NULL }; From patchwork Wed May 8 07:41:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658179 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38A3247A7D for ; Wed, 8 May 2024 07:42:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154169; cv=none; b=vGJSg9t5Fb5jfzRmIE8IFfWB+WNVaE0m+0FAyHvu3auaEOicLJH+XhW5yj0+7pVbfj71jJ6X16DPG1DJXoaqydX5dqRpXDrgWUjaGx7j3lDVpRiHZw8yGnwYoz10hHJNPaf0IPgzP0nueF1Dels9FKKfVjRuFb4SPuAHpjQM1V0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154169; c=relaxed/simple; bh=UjfGmtLHYc+ZtfDBltxPDecVYYzszPmAvKAkfS30jKk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dmFfcNcSKjkrX/Pmag2pmlilVPL7PXQmNANH8L4ZyR1S/YOi4xzFxB0Ip8YhvXrG34u9D1izhX3U+gwZ1N3reKOZxzHGL1ac0DOgTIjoemcODuHJYlY4qk3PGVPc2TvZP852f2MGVMbXqrS/FEZRRV2YmnEXEF2GjdRSWO7AILQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=S6VNcUZW; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="S6VNcUZW" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6f44a2d1e3dso3455856b3a.3 for ; Wed, 08 May 2024 00:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154168; x=1715758968; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xs/oZbuIhbdeWVSnm1Y7/6OB6aLjJFlUrJpqF9jHmLk=; b=S6VNcUZWgqpUojvnokrB2g55zLPuYYS+j8+o0zdfbeg5Bkp0ExFlXS02OqqTa98Wa7 sEYaPI7L17DelYbLwUAVseHynoMVyYfhBIyaPffgrE6hXZDgf6LBKulnWLLW3dbt5JxM jL0gewyPNMcUWmxhT38nMIKKI1ifl2SLFewWk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154168; x=1715758968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xs/oZbuIhbdeWVSnm1Y7/6OB6aLjJFlUrJpqF9jHmLk=; b=sJaHJ3GpFSU7oPFY5jxWL9X3fJrYVS5m2Ni8zcrRgU1HbF66XpdWTa0dG6TpBZuMF5 /VqxgtwfzPLpJpcPlDQeXefyv09jQC8CmbR19jBHMuNIndlC33MCCrKQjwyI+mrOSxQM hk28a3w1b4Ydg0M/fnywNUhDJuXzEGeJU7U//vhS9kFQZDo5gm567wFCVz1IRtyapzkW exSca9z9SJBmNS8yimFQ8OmWD5BZANR4dPlhIOUq5bgLMAvbXtHpgXDF621z84MpY1dm No4Jvoj0KGIMUURcF+iZdJgrLytVpOLIB4GK212QhPL1Ta5ONm5QNfmIxF9O2vJpdWoC mIBA== X-Forwarded-Encrypted: i=1; AJvYcCUj6KYyUJCbpzIlnbRsYpUZ667j6rpTh+1LDazTXldlWKTEbc4/8DQCXbWF7weN9GYqfbwSDbFCCLRu3A2A7hYwL36CZVoQRbuz+H8= X-Gm-Message-State: AOJu0Yz6Uw1P9iENFnuvHmQZl/lVAADuwrfl5x+O81DYZRDqpX0tqTbJ OkZ8jZhIs9bmy8a0ObssoDyYfUhncY7cSMzElVPFecTExuORT/Sy2av55K5v3A== X-Google-Smtp-Source: AGHT+IFO2VQzLGGAy8b1vLdPgLtYlvSlasHwDwvTd2+OhnH1jF5ePjrL8wvO3+GWqyNffeo4LSj2IQ== X-Received: by 2002:a05:6a21:6d96:b0:1ad:9adf:febf with SMTP id adf61e73a8af0-1afc8d5be49mr2191762637.31.1715154167678; Wed, 08 May 2024 00:42:47 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:47 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 05/19] zram: add zstd compression backend support Date: Wed, 8 May 2024 16:41:58 +0900 Message-ID: <20240508074223.652784-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_zstd.c | 97 +++++++++++++++++++++++++++++++ drivers/block/zram/backend_zstd.h | 10 ++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 123 insertions(+) create mode 100644 drivers/block/zram/backend_zstd.c create mode 100644 drivers/block/zram/backend_zstd.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 0d890a8d2dc4..71cd0d5d8f35 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -32,6 +32,12 @@ config ZRAM_BACKEND_LZ4HC select LZ4HC_COMPRESS select LZ4_DECOMPRESS +config ZRAM_BACKEND_ZSTD + bool "zstd compression support" + depends on ZRAM + select ZSTD_COMPRESS + select ZSTD_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -53,6 +59,10 @@ config ZRAM_DEF_COMP_LZ4HC bool "lz4hc" depends on ZRAM_BACKEND_LZ4HC +config ZRAM_DEF_COMP_ZSTD + bool "zstd" + depends on ZRAM_BACKEND_ZSTD + endchoice config ZRAM_DEF_COMP @@ -61,6 +71,7 @@ config ZRAM_DEF_COMP default "lzo" if ZRAM_DEF_COMP_LZO default "lz4" if ZRAM_DEF_COMP_LZ4 default "lz4hc" if ZRAM_DEF_COMP_LZ4HC + default "zstd" if ZRAM_DEF_COMP_ZSTD default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 815b45471c7d..053fe35e346b 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o +obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c new file mode 100644 index 000000000000..4da49626f110 --- /dev/null +++ b/drivers/block/zram/backend_zstd.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_zstd.h" + +struct zstd_ctx { + zstd_cctx *cctx; + zstd_dctx *dctx; + void *cctx_mem; + void *dctx_mem; + s32 level; +}; + +static void zstd_destroy(void *ctx) +{ + struct zstd_ctx *zctx = ctx; + + vfree(zctx->cctx_mem); + vfree(zctx->dctx_mem); + kfree(zctx); +} + +static void *zstd_create(void) +{ + zstd_parameters params; + struct zstd_ctx *ctx; + size_t sz; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->level = ZSTD_defaultCLevel(); + params = zstd_get_params(ctx->level, 0); + sz = zstd_cctx_workspace_bound(¶ms.cParams); + ctx->cctx_mem = vzalloc(sz); + if (!ctx->cctx_mem) + goto error; + + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); + if (!ctx->cctx) + goto error; + + sz = zstd_dctx_workspace_bound(); + ctx->dctx_mem = vzalloc(sz); + if (!ctx->dctx_mem) + goto error; + + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); + if (!ctx->dctx) + goto error; + + return ctx; + +error: + zstd_destroy(ctx); + return NULL; +} + +static int zstd_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct zstd_ctx *zctx = ctx; + const zstd_parameters params = zstd_get_params(zctx->level, 0); + size_t ret; + + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, ¶ms); + if (zstd_is_error(ret)) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + struct zstd_ctx *zctx = ctx; + size_t ret; + + ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); + if (zstd_is_error(ret)) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_zstd = { + .compress = zstd_compress, + .decompress = zstd_decompress, + .create_ctx = zstd_create, + .destroy_ctx = zstd_destroy, + .name = "zstd", +}; diff --git a/drivers/block/zram/backend_zstd.h b/drivers/block/zram/backend_zstd.h new file mode 100644 index 000000000000..75d2d2c02768 --- /dev/null +++ b/drivers/block/zram/backend_zstd.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_ZSTD_H__ +#define __BACKEND_ZSTD_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_zstd; + +#endif /* __BACKEND_ZSTD_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index f04f5844a23c..c16eb038f608 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -19,6 +19,7 @@ #include "backend_lzorle.h" #include "backend_lz4.h" #include "backend_lz4hc.h" +#include "backend_zstd.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -30,6 +31,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) &backend_lz4hc, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) + &backend_zstd, #endif NULL }; From patchwork Wed May 8 07:41:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658180 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1CFA376F1 for ; Wed, 8 May 2024 07:42:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154172; cv=none; b=K7X+m/BMch0IItMf2oUiQU5EAsEiRWpQqdTvlFKA6Hp4J9LUwLmWX1ZcPULD4NRHZK/2i76Z5A9nWYysKtglaVx+BXtSJNBJub+oaAMINEN5oHAyaACVqAwfstJFBPYGlb9LBvkPE4k7J7S2WID1kSoahLy0xdfO8IcXWNnokhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154172; c=relaxed/simple; bh=FRBhLTt/FHOdPdldPK2JgCScJh/quKT94FOX0NLvGcs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fHLp4Hz9KX8vZjmqd9DBKQrvyc3DWP+BmhDUUIMy+h7NvNwCb183fCOmWIAjvDxdAbO+o4RuT2K6shSZcL5o7WPzaD4Gu+Ll0kZnkHXD7HYgcvVloNCC5hqwFMqQH5/Lq0AR2UuT23EegUDHKIN6eXsF7HHYMySKBBM3WN7MWb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=dOkrMjQP; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="dOkrMjQP" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2b273cbbdfdso2907147a91.1 for ; Wed, 08 May 2024 00:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154170; x=1715758970; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/EvmWJu/VXtBqzoQye3a2VzbJm8fhR7v5ZjISdbrAiY=; b=dOkrMjQPC8GkW9WsZ44muU5Wiz9dpsk+y4Dgw0h2n72qVYBqPYmhjwTuIrjGH1cgzd bXskbXyx0IYZnpX9EdjSjlRNh72n5h2YRSwgLT+neCyo3AHj36Rvg9iAPtMJedIB2Mpd 0yqVRrAnq/kb5Cgjm5g/9bsddHxLjNWoRmRXk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154170; x=1715758970; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/EvmWJu/VXtBqzoQye3a2VzbJm8fhR7v5ZjISdbrAiY=; b=p/h7PhRKrSB2ol5gEpJzbVhMsX0IA69sNexwyFwuS1PsJ70fpmPnyu0665x3bxaSCx 4djUyDjc3Gdjs1bcpoARcqOg/KYXSvrKJk5HjQJmVlY97Ae8jgQtd8pseA7JoCpm0tja +Sxwt4v1FsWm2uead6uMsHAuUxclovqxxO58gLoZB3ULQQpE6JoLqhssUlSaFaahzBtu MbAKNkjRk/o/X+j12ud2J2+T6SHbTp1u/DGnHDHumRQfv6P6cCe3bCQ6XkdPYWz/VOKM lN6iyxSLWorx8AMfUkdc5VcAYHIVAokhjzUC71geMF2hhuRcRgAr1/LKY6+Ax8kYJPy5 3MMg== X-Forwarded-Encrypted: i=1; AJvYcCVHuNUk6SdJcMhSWv2KOnVuYY/IHHt9BeLGuNZZDu7tIoS9QBibXweM3DhVLpYVWs1Gxi+sik5fXni5EMCPCy2CANKnJkyB2Uu1qI4= X-Gm-Message-State: AOJu0Yy//kii8O0gbITN8ALhA0vitEqfaz5yjMZh2E0L2aaVTUAxjShx iC81w8bBy5LHrxU1vI2qe/ZOxum/sF+JzThvfPnvAvyzkwjREomuYCQf6Mp+Eg== X-Google-Smtp-Source: AGHT+IHVhKTM2v7Uz/Aq4g+yImMIqtZc4Gvz8s0tsH6+Eo+knHPIcdNtEwPD2ijl/sCOPSx9VMynkw== X-Received: by 2002:a17:90a:d781:b0:2b4:fcfd:780e with SMTP id 98e67ed59e1d1-2b616bf08b5mr1618670a91.49.1715154170221; Wed, 08 May 2024 00:42:50 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:49 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 06/19] zram: pass estimated src size hint to zstd Date: Wed, 8 May 2024 16:41:59 +0900 Message-ID: <20240508074223.652784-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 zram works with PAGE_SIZE buffers, so we always know exact size of the source buffer and hence can pass estimated_src_size to zstd_get_params(). This hint on x86_64, for example, reduces the size of the work memory buffer from 1303520 bytes down to 90080 bytes. Given that compression streams are per-CPU that's quite some memory saving. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 4da49626f110..4a7734aa1a8a 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -35,7 +35,7 @@ static void *zstd_create(void) return NULL; ctx->level = ZSTD_defaultCLevel(); - params = zstd_get_params(ctx->level, 0); + params = zstd_get_params(ctx->level, PAGE_SIZE); sz = zstd_cctx_workspace_bound(¶ms.cParams); ctx->cctx_mem = vzalloc(sz); if (!ctx->cctx_mem) @@ -65,7 +65,7 @@ static int zstd_compress(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len) { struct zstd_ctx *zctx = ctx; - const zstd_parameters params = zstd_get_params(zctx->level, 0); + const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, From patchwork Wed May 8 07:42:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658181 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FD17537E0 for ; Wed, 8 May 2024 07:42:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154174; cv=none; b=Ql9f8bnjE+ZSoqRcE98ctLo4+L6E+cQA5BO7SPr4MtE5WukPMCEvF0jGw9YcPE1Y0qBt0XCAgmdC6D5vvvpKTVxw8OMRa+ubDx2Z554J88c5PgzbXe6R/Fl5cnC/Wv514etBJaIbgJDdFyow8pXigk3LNnEseOTRhUMsiSZwI8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154174; c=relaxed/simple; bh=QlfqxCVzGVqjuU78RUVl7laME8V3Egjqi1IedM7APCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YjyWlZtWKgU4iOwdO0lwKlMR4VhVPvQc80iat5TiSmarSRiVbJQeSvgaVpHkjUm3zUMZ8mubNdbOU+mURZCqeFupfQNzEiCEbLVVu0QEO/aqFUhVegBabR0Vyzh5DZg7G802oPvk5QfiKD0uI29k1E/7Qh/rL8PmpN9njdhzqXs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=R7xhMQs+; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="R7xhMQs+" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5ca29c131ebso3033053a12.0 for ; Wed, 08 May 2024 00:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154173; x=1715758973; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=II/I/hgLmJhm0CjvuLYW2Y62FUnxRGP17kgZqYB2Ofs=; b=R7xhMQs+LZQ5W5ems3eTs1wSRGOew4R44hkK2dxllm5gn8QhYRC9mfK/0+NTrheGQE xMa7LIEhfRCGWQbtmy0hDxncvoqDOV7DZVAvPpjpfFUy1/7FQ8wnMdwPy0SpyrqbPM+P GIRC5cVZoTDesK2ydc8FZlVYMd3HgJ8I2YoCM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154173; x=1715758973; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=II/I/hgLmJhm0CjvuLYW2Y62FUnxRGP17kgZqYB2Ofs=; b=RjfCwn4o6nexwh59qXWM1z+vOdOf+jv21d7NIrLVYWSl7tdjHNF3QHSgdGfUfpwJwD LG+Mc6DkmtilGun91C3yKtWnx6F0gDhyYh0zIhZrCJRNkwE4M0S32kd2EMQcUL1M431i 4Q8t9bBGz5MYNZsnio6vyL3FjvVD7fkBt8GNwj1qjKIO7Q1M799l54TwTAiybB0tPOHr K0Bf3txB9HLlX5kPN/fiI1wYogxh+RzMoVC3aC0dmrROR9GoHhbjKzuBwqvEbaBSZJRJ v2W/B4zN7uGmO1kCHM/pPoMuTPtX8ZAjKdvp31HPKYjrSbZTfWbMyGGDmXSs4LZZqIkb lCAg== X-Forwarded-Encrypted: i=1; AJvYcCXrV/wnxIPmPDASHb6fl1l+Mk/f1adJaSqIaw1dnt91uKk2apYGbZsPVlz0TVI8dVSf2a/8LdNyeAUUSeyLJOV5nFGNSPORiJUVRAU= X-Gm-Message-State: AOJu0YwJq0oSC25YuzXQlWgr0fa+l/nud5MeKgw2TKHDZtwPLaYvRCP7 tJkXFl5QYWnuu33oe525xBMmqp92T5PhBCvMh3tY3U0lZ0jr/JfM8ZDgxC1xzA== X-Google-Smtp-Source: AGHT+IECICoA7hcDg23NAlDQOI1208s0oA/MgjMrcQhNGoK5Hsv4ExsAu/YczEHWlGdEBYIalK11CQ== X-Received: by 2002:a17:90a:e011:b0:2ab:e345:4685 with SMTP id 98e67ed59e1d1-2b6165a5634mr1749435a91.17.1715154172664; Wed, 08 May 2024 00:42:52 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:52 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 07/19] zram: add zlib compression backend support Date: Wed, 8 May 2024 16:42:00 +0900 Message-ID: <20240508074223.652784-8-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++ drivers/block/zram/Makefile | 9 +- drivers/block/zram/backend_deflate.c | 130 +++++++++++++++++++++++++++ drivers/block/zram/backend_deflate.h | 10 +++ drivers/block/zram/zcomp.c | 4 + 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 drivers/block/zram/backend_deflate.c create mode 100644 drivers/block/zram/backend_deflate.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 71cd0d5d8f35..9dedd2edfb28 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -38,6 +38,12 @@ config ZRAM_BACKEND_ZSTD select ZSTD_COMPRESS select ZSTD_DECOMPRESS +config ZRAM_BACKEND_DEFLATE + bool "deflate compression support" + depends on ZRAM + select ZLIB_DEFLATE + select ZLIB_INFLATE + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -63,6 +69,10 @@ config ZRAM_DEF_COMP_ZSTD bool "zstd" depends on ZRAM_BACKEND_ZSTD +config ZRAM_DEF_COMP_DEFLATE + bool "deflate" + depends on ZRAM_BACKEND_DEFLATE + endchoice config ZRAM_DEF_COMP @@ -72,6 +82,7 @@ config ZRAM_DEF_COMP default "lz4" if ZRAM_DEF_COMP_LZ4 default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "zstd" if ZRAM_DEF_COMP_ZSTD + default "deflate" if ZRAM_DEF_COMP_DEFLATE default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 053fe35e346b..91c07595d8b4 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,9 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o -obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o +obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o +obj-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c new file mode 100644 index 000000000000..949d402ea3dd --- /dev/null +++ b/drivers/block/zram/backend_deflate.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_deflate.h" + +/* Use the same value as crypto API */ +#define DEFLATE_DEF_WINBITS 11 +#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL + +struct deflate_ctx { + struct z_stream_s cctx; + struct z_stream_s dctx; + s32 level; +}; + +static void deflate_destroy(void *ctx) +{ + struct deflate_ctx *zctx = ctx; + + if (zctx->cctx.workspace) { + zlib_deflateEnd(&zctx->cctx); + vfree(zctx->cctx.workspace); + } + if (zctx->dctx.workspace) { + zlib_inflateEnd(&zctx->dctx); + vfree(zctx->dctx.workspace); + } + kfree(zctx); +} + +static void *deflate_create(void) +{ + struct deflate_ctx *ctx; + size_t sz; + int ret; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + /* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ + ctx->level = Z_DEFAULT_COMPRESSION; + sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); + ctx->cctx.workspace = vzalloc(sz); + if (!ctx->cctx.workspace) + goto error; + + ret = zlib_deflateInit2(&ctx->cctx, ctx->level, Z_DEFLATED, + -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL, + Z_DEFAULT_STRATEGY); + if (ret != Z_OK) + goto error; + + sz = zlib_inflate_workspacesize(); + ctx->dctx.workspace = vzalloc(sz); + if (!ctx->dctx.workspace) + goto error; + + ret = zlib_inflateInit2(&ctx->dctx, -DEFLATE_DEF_WINBITS); + if (ret != Z_OK) + goto error; + + return ctx; + +error: + deflate_destroy(ctx); + return NULL; +} + +static int deflate_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct deflate_ctx *zctx = ctx; + struct z_stream_s *deflate; + int ret; + + deflate = &zctx->cctx; + ret = zlib_deflateReset(deflate); + if (ret != Z_OK) + return -EINVAL; + + deflate->next_in = (u8 *)src; + deflate->avail_in = PAGE_SIZE; + deflate->next_out = (u8 *)dst; + deflate->avail_out = *dst_len; + + ret = zlib_deflate(deflate, Z_FINISH); + if (ret != Z_STREAM_END) + return -EINVAL; + + *dst_len = deflate->total_out; + return 0; +} + +static int deflate_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + struct deflate_ctx *zctx = ctx; + struct z_stream_s *inflate; + int ret; + + inflate = &zctx->dctx; + + ret = zlib_inflateReset(inflate); + if (ret != Z_OK) + return -EINVAL; + + inflate->next_in = (u8 *)src; + inflate->avail_in = src_len; + inflate->next_out = (u8 *)dst; + inflate->avail_out = PAGE_SIZE; + + ret = zlib_inflate(inflate, Z_SYNC_FLUSH); + if (ret != Z_STREAM_END) + return -EINVAL; + + return 0; +} + +struct zcomp_backend backend_deflate = { + .compress = deflate_compress, + .decompress = deflate_decompress, + .create_ctx = deflate_create, + .destroy_ctx = deflate_destroy, + .name = "deflate", +}; diff --git a/drivers/block/zram/backend_deflate.h b/drivers/block/zram/backend_deflate.h new file mode 100644 index 000000000000..49cef8fc1e77 --- /dev/null +++ b/drivers/block/zram/backend_deflate.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_DEFLATE_H__ +#define __BACKEND_DEFLATE_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_deflate; + +#endif /* __BACKEND_DEFLATE_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index c16eb038f608..9fc5477a6259 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -20,6 +20,7 @@ #include "backend_lz4.h" #include "backend_lz4hc.h" #include "backend_zstd.h" +#include "backend_deflate.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -34,6 +35,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) &backend_zstd, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) + &backend_deflate, #endif NULL }; From patchwork Wed May 8 07:42:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658182 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 832F051C4B for ; Wed, 8 May 2024 07:42:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154176; cv=none; b=OESRuo7rsqm6CXxbjeASoZUP50M6jkiBMi/eBWH5W84sn+gHL3Jw5HqHAIRu7asq0FTbZNOGMvn8d4hS151n6ntOiI5Go1/xcuGLF5BODFYsigKfBEg2H0uUvd4SYMt075QZ6CS1H/6MRD+i3B98bRILg8aVKP1D1ip90UEUA8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154176; c=relaxed/simple; bh=kNJbXxfK8zdnskpjgM5oyiiq8zq2KQp2aiK9qKIm8Jk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XmrtBAWS69OGbtQCME9nD1sQXEFgblVi6idOymYKXEqQeE144LvDtJR5ny9lOwhyh2/7LAgp/l77BF8vaIXjO6bRXpP5fN0j6o9QqvRcfFQwJ9gnlom7C0MDE6+wFajhYepTaaqDzOQ8xUbFs1ncWTNFfvFMWdLXFcfItNeaLQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=J8FA69Bd; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="J8FA69Bd" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5d8b887bb0cso3361800a12.2 for ; Wed, 08 May 2024 00:42:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154175; x=1715758975; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EEH83ikFWbHDBUUzWu4h4KsSObV7EItvgFziqF5bCxw=; b=J8FA69BdgXXskEd/BPlAMrf0g4gpVDyflcFnylken9drYDmKP/Lpg8LyNkznHjNa/J 3FPXUW5hhYlykq+ss8QBM1KlofuOYNBKPBy4dLMbC6PYd0jxZ6Ax4OLL52M1wix4GOL5 MVx3xO9szVkQbqYEex3HIbJeRWMrZn/XZLnlM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154175; x=1715758975; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EEH83ikFWbHDBUUzWu4h4KsSObV7EItvgFziqF5bCxw=; b=G8QhlnaWLq94D79VcsVw7DXdJpWLaTLxzWQv+ewykR2CU6baazapIMP2t1wi7uuBjx QtIXTsaNH3S9Ty4VZRdpmWrYVhbBuwhaurtPH6Q6ic8ysUgreq6ApQ0Yn7KDbhuA1BoZ qm85X5KdgyDqCC/laslKYfbkZA+j8wk1sGWFrVMshq/grfYywFykcirOp7BpuHAfNq5G Su5DP9fAfgKOS/EPDs3jdvIuf7kXN7bc0QtfywZ1/qlvYKOuBN52MyWv/i21SBgwS95t qi0pt06wuw3xxSBjjW+baGwNAnlFo9RFrfEdl5U4Gdis42GpncReneMxcH33HUSFogQU wR5Q== X-Forwarded-Encrypted: i=1; AJvYcCW04TBwE1TutS+mhavVqZv8nzawyLSDSORmdAOyPFnRcPnMsuMB9JYYelORG3gNVtsYjANHTex6mqTUemPnT4Fmh5m7DdqU+f9om58= X-Gm-Message-State: AOJu0Yzpp5jl/AMEduE53mF4rxtbJ0kgeJrF03YaZeAkdQySJL81t1rr alDtaV9vTLMKD5bxKAZVlGWlyT7BWAETP96Bb5yjlZA7ObySsFH+IgDcM+PIOQ== X-Google-Smtp-Source: AGHT+IHwE3BmeY633h6z9OdUt2KUPSJ3mKVAE+dA7ACYa7t2C5hLuIWOLWXLTi0oYaqXZgvkqtD7rA== X-Received: by 2002:a17:90a:ab12:b0:2b2:9855:2836 with SMTP id 98e67ed59e1d1-2b616be1f64mr2190414a91.34.1715154174910; Wed, 08 May 2024 00:42:54 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:54 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 08/19] zram: add 842 compression backend support Date: Wed, 8 May 2024 16:42:01 +0900 Message-ID: <20240508074223.652784-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_842.c | 68 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_842.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 94 insertions(+) create mode 100644 drivers/block/zram/backend_842.c create mode 100644 drivers/block/zram/backend_842.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 9dedd2edfb28..1e0e7e5910b8 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -44,6 +44,12 @@ config ZRAM_BACKEND_DEFLATE select ZLIB_DEFLATE select ZLIB_INFLATE +config ZRAM_BACKEND_842 + bool "842 compression support" + depends on ZRAM + select 842_COMPRESS + select 842_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -73,6 +79,10 @@ config ZRAM_DEF_COMP_DEFLATE bool "deflate" depends on ZRAM_BACKEND_DEFLATE +config ZRAM_DEF_COMP_842 + bool "842" + depends on ZRAM_BACKEND_842 + endchoice config ZRAM_DEF_COMP @@ -83,6 +93,7 @@ config ZRAM_DEF_COMP default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "zstd" if ZRAM_DEF_COMP_ZSTD default "deflate" if ZRAM_DEF_COMP_DEFLATE + default "842" if ZRAM_DEF_COMP_842 default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 91c07595d8b4..029827a6ddac 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o obj-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o +obj-$(CONFIG_ZRAM_BACKEND_842) += backend_842.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c new file mode 100644 index 000000000000..8ea7a230b890 --- /dev/null +++ b/drivers/block/zram/backend_842.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_842.h" + +struct sw842_ctx { + void *mem; +}; + +static void destroy_842(void *ctx) +{ + struct sw842_ctx *zctx = ctx; + + kfree(zctx->mem); + kfree(zctx); +} + +static void *create_842(void) +{ + struct sw842_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->mem = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); + if (!ctx->mem) + goto error; + + return ctx; + +error: + destroy_842(ctx); + return NULL; +} + +static int compress_842(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct sw842_ctx *zctx = ctx; + unsigned int dlen = *dst_len; + int ret; + + ret = sw842_compress(src, PAGE_SIZE, dst, &dlen, zctx->mem); + if (ret == 0) + *dst_len = dlen; + return ret; +} + +static int decompress_842(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + unsigned int dlen = PAGE_SIZE; + + return sw842_decompress(src, src_len, dst, &dlen); +} + +struct zcomp_backend backend_842 = { + .compress = compress_842, + .decompress = decompress_842, + .create_ctx = create_842, + .destroy_ctx = destroy_842, + .name = "842", +}; diff --git a/drivers/block/zram/backend_842.h b/drivers/block/zram/backend_842.h new file mode 100644 index 000000000000..c03a2396d7b2 --- /dev/null +++ b/drivers/block/zram/backend_842.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_842_H__ +#define __BACKEND_842_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_842; + +#endif /* __BACKEND_842_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 9fc5477a6259..2a38126f4da3 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -21,6 +21,7 @@ #include "backend_lz4hc.h" #include "backend_zstd.h" #include "backend_deflate.h" +#include "backend_842.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -38,6 +39,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) &backend_deflate, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_842) + &backend_842, #endif NULL }; From patchwork Wed May 8 07:42:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658183 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 392D554756 for ; Wed, 8 May 2024 07:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154179; cv=none; b=APYMdDzHErpcslAQdZ0GCZzHeu2Exx7mwAc7NhsURgwFpacBLBY+eSm0xYr/b3hnKtZ/aJG0vedBwxDL4Uyj6GRCt6iViPbi4eaPhFbqlGShYrKWvgOtLcrN1wLlwKroGvUXzktRCNoAbZRMiilydSAtNlvI8a1XJX0hKVzGnvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154179; c=relaxed/simple; bh=fkmGnp5H4sYk5AcEucbfVYS4PjNz7ma3EYQ0ecKZnDQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mLK/rOScRuWVPSF8o2BZW53zwXhOWnYERyU7RV8wH03TTzfWotpfCW+tB8c3zDpMBgRB84pVblwNYOwf+R6sj7m/nN3Bi/k1SxDv9zfayI50Ii/bnxhmKIXTh1tITSM1gXQjHr/APAkcoeeWfs1rupmnSG23iH7hlxCIUsIffHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=H80IqcB0; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="H80IqcB0" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2b3c1ea9a68so411787a91.1 for ; Wed, 08 May 2024 00:42:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154177; x=1715758977; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0eIYrOQ3BkMXJCk6zuaDE2ECawMW63bMTlaOVOjCrZU=; b=H80IqcB0FwAB6hHdI88P8f4QJKwvMn7A8p8g+5vrIdknVDA/UZYCXoA0/q3VfdU/CB 1+DZjgHppFXLxXZPGrkvqpJ9uPMySdsXHIqx7fS3aesMhyAoiV8/VWnBQb9DXk4EniMg W0RDcxB+4dGF3xwJtv0vHPRqYmofKxCHMjSxs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154177; x=1715758977; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0eIYrOQ3BkMXJCk6zuaDE2ECawMW63bMTlaOVOjCrZU=; b=oi2EdNzII0/yF953Z7/Y6tYrqnP14W2cjtx4HBCy/rGBZBiucEkNy0k7YSdRCh23S7 DO8M3/m9ps21KlfuiG5xcPu5i5Zh1nhVvz3BcGWtydjliPm9LV0ZlOxAlZs10CM92YXQ LnhcWmvOC+fUpvxGrPVt4iITm7rfNQr+0EcVFeVaNUr6AcLDgciToCOZh5bYqggkAsXP Yk9HiOJnnCqAAjwZvTTwFVo0oq6k2rQy9jJmtNvJZN9JKrDPrPLSjce6fPRqAdNe2bdi mNY9PCHD0a4mTVvvWQfIzQma5fik/w7vTrFGaPNv3U5WlKTkK98g/r1WRGuwJ+mbb9GY 3X3A== X-Forwarded-Encrypted: i=1; AJvYcCUwoc8ZQlOyw6MQ1HWWk1HJJb8PNUQEDBonywH/JmZOuvi7c5cqdc6ECVSplOuv0B53ifYHJzvD96lFxSnLWe33HGnLu/EtMubWETM= X-Gm-Message-State: AOJu0YzXointqxtMl5ojcHCFBvVYwhUTjNmdpN7+vULdMwAW8+ymxFNF 9JwIQiKboW9GU0ppGuYJK+mtdnIPg/sbY6kcrLvAAXTN67r8s19lo0PRI+810xvGM5K8yAPXrp4 = X-Google-Smtp-Source: AGHT+IHQgia9i24967sF+3KEw6udtuPb13bMRjWKcpvNAJbkVABQfoU1gdDD8sgKiYoXBBzsmpfN/Q== X-Received: by 2002:a17:90a:bd05:b0:2b4:32c0:d7d7 with SMTP id 98e67ed59e1d1-2b5bc2ad5b0mr7051893a91.16.1715154177470; Wed, 08 May 2024 00:42:57 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:56 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 09/19] zram: check that backends array has at least one backend Date: Wed, 8 May 2024 16:42:02 +0900 Message-ID: <20240508074223.652784-10-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Make sure that backends array has anything apart from the sentinel NULL value. We also select LZO_BACKEND if none backends were selected. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 19 +++++++++++++------ drivers/block/zram/zcomp.c | 8 ++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 1e0e7e5910b8..6aea609b795c 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -14,12 +14,6 @@ config ZRAM See Documentation/admin-guide/blockdev/zram.rst for more information. -config ZRAM_BACKEND_LZO - bool "lzo and lzo-rle compression support" - depends on ZRAM - select LZO_COMPRESS - select LZO_DECOMPRESS - config ZRAM_BACKEND_LZ4 bool "lz4 compression support" depends on ZRAM @@ -50,6 +44,19 @@ config ZRAM_BACKEND_842 select 842_COMPRESS select 842_DECOMPRESS +config ZRAM_BACKEND_FORCE_LZO + depends on ZRAM + def_bool !ZRAM_BACKEND_LZ4 && !ZRAM_BACKEND_LZ4HC && \ + !ZRAM_BACKEND_ZSTD && !ZRAM_BACKEND_DEFLATE && \ + !ZRAM_BACKEND_842 + +config ZRAM_BACKEND_LZO + bool "lzo and lzo-rle compression support" if !ZRAM_BACKEND_FORCE_LZO + depends on ZRAM + default ZRAM_BACKEND_FORCE_LZO + select LZO_COMPRESS + select LZO_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 2a38126f4da3..d49791f724e9 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -209,6 +209,14 @@ struct zcomp *zcomp_create(const char *alg) struct zcomp *comp; int error; + /* + * The backends array has a sentinel NULL value, so the minimum + * size is 1. In order to be valid the array, apart from the + * sentinel NULL element, should have at least one compression + * backend selected. + */ + BUILD_BUG_ON(ARRAY_SIZE(backends) <= 1); + comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL); if (!comp) return ERR_PTR(-ENOMEM); From patchwork Wed May 8 07:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658184 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6111F54BE0 for ; Wed, 8 May 2024 07:43:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154181; cv=none; b=eGLeOeazbRF/OqAjLDCpj9p1HzrbZp0nYzTv9VrLAwUSuJgCCh4Az3ghAklb7PZsrQ+aKqUtvqnmsSSHpxDpvr7bIXbges3UwaI31o7VNmoCdaptvxq1HdSEwyAueS/t7cSe+Gk5u4bTy5Fs+Mu73bFZ7t5qDf1Z52URz0hKEDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154181; c=relaxed/simple; bh=gUBERMicU7WLLq2de8iEQjXt2o8KBPI9FHzu44dkhxg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m/YgNiH1WQ2m5UMi+7uPgLL8dfjBTgHPyz/Iz7RR+iD+fdsvHtUjNJYVydSV32K42rtB5SL7hCoAirblvfaiA47Mt04nqM6Z6X7vqJC+jPb0qo6T5+EouvPkVLzOJHK8s03UEnS5C8eLnW2gGck2z/T5Y9RpsuBA9pp83ru4vVI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BApYeXDE; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BApYeXDE" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2b27eec1eb1so2915474a91.0 for ; Wed, 08 May 2024 00:43:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154180; x=1715758980; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=S3bUdaYb8VukERAvkARptjjY26yVI43doztfWIHDgkU=; b=BApYeXDEjMTKsh9sPhlMdhyCMI3Tz64U3Soqqwi7h5H/rbmmR5CMO/MYmtZhsoRSEZ 8GvnXQAp4LMXcBqp+is/3XDZssIp8AYljPlk/gWgxpcfPSwN2ztHIjvJkOr8/Je4MfQz gaPe+UuM/sv928mxB8kvB7yx02PbLuI7WGvXI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154180; x=1715758980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S3bUdaYb8VukERAvkARptjjY26yVI43doztfWIHDgkU=; b=i1l1+B9ULytehbt1qbtmx/n4UOsnqqmzFLK5+vR7qwNBy/hjr8FZ0/Vt7k54ckkAcX HSXIBAhtovAfmYUYrfAyLhbrcpkGXMBWvR0ORBljeitaS8pbRLE8QcN+Jw3dWGeLBzz8 X5/ms800F2qdb/775CXRPifmRzZzdg3PgAE+Ka8cjiTO9i9TTq2XsK2uoP5iZtjxYaK0 DagI13lqTVkebt9sVhnyo3isGkEGDTZ6x1g/k0IeobdtR39MhqLHaOq+QENOhwcF9fvu 5hwWIn8FVnGS+nshnyvMfb2nKDNp84k6ohOk3ADtFG7E9RfqdSS0JyG07rj7zm3mx6+i RSAg== X-Forwarded-Encrypted: i=1; AJvYcCUo8OnjKbtV55JmpsTzLRGuxPKzxUyDEyhzfHSx5cla/ojEy+Kc5EKIVUfdwBbd1m8Z5bF84FZa3+/0uxjFdjsI53KPup0Ut3dis0M= X-Gm-Message-State: AOJu0YzdsnzHADrcdbDa7MiKzovuUx3JCyTtRwQIPlSA9jNxl9PZakNI paYI3LNn7koUWA9MDSQraJuLQsM/rVE2Sn08J56/mv77N5DDhCE4Knju1SgVtw== X-Google-Smtp-Source: AGHT+IHLDs5Z2qVvaQD54fdwh5bRibjLYhY0MMzGyEEicjkKB8A1A4WJtW4ta3aNVJPQkExUKPlytg== X-Received: by 2002:a17:90b:230e:b0:2b2:ae4b:9e54 with SMTP id 98e67ed59e1d1-2b61659f842mr1829748a91.11.1715154179747; Wed, 08 May 2024 00:42:59 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.42.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:42:59 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 10/19] zram: introduce zcomp_config structure Date: Wed, 8 May 2024 16:42:03 +0900 Message-ID: <20240508074223.652784-11-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We will store a per-algorithm parameters there (compression level, dictionary, dictionary size, etc.). Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_842.c | 2 +- drivers/block/zram/backend_deflate.c | 9 ++++++--- drivers/block/zram/backend_lz4.c | 2 +- drivers/block/zram/backend_lz4hc.c | 10 ++++++---- drivers/block/zram/backend_lzo.c | 2 +- drivers/block/zram/backend_lzorle.c | 2 +- drivers/block/zram/backend_zstd.c | 8 ++++++-- drivers/block/zram/zcomp.c | 5 +++-- drivers/block/zram/zcomp.h | 14 ++++++++++++-- drivers/block/zram/zram_drv.c | 20 +++++++++++++++++++- drivers/block/zram/zram_drv.h | 1 + 11 files changed, 57 insertions(+), 18 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c index 8ea7a230b890..12e716deb763 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -19,7 +19,7 @@ static void destroy_842(void *ctx) kfree(zctx); } -static void *create_842(void) +static void *create_842(struct zcomp_config *config) { struct sw842_ctx *ctx; diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c index 949d402ea3dd..83c660adc722 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx) kfree(zctx); } -static void *deflate_create(void) +static void *deflate_create(struct zcomp_config *config) { struct deflate_ctx *ctx; size_t sz; @@ -42,8 +42,11 @@ static void *deflate_create(void) if (!ctx) return NULL; - /* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ - ctx->level = Z_DEFAULT_COMPRESSION; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = Z_DEFAULT_COMPRESSION; + sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); ctx->cctx.workspace = vzalloc(sz); if (!ctx->cctx.workspace) diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 697592dbabe2..98d9c9274149 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -4,7 +4,7 @@ #include "backend_lz4.h" -static void *lz4_create(void) +static void *lz4_create(struct zcomp_config *config) { return vmalloc(LZ4_MEM_COMPRESS); } diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 1735a2598443..e1069f185437 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -18,7 +18,7 @@ static void lz4hc_destroy(void *ctx) kfree(zctx); } -static void *lz4hc_create(void) +static void *lz4hc_create(struct zcomp_config *config) { struct lz4hc_ctx *ctx; @@ -26,14 +26,16 @@ static void *lz4hc_create(void) if (!ctx) return NULL; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = LZ4HC_DEFAULT_CLEVEL; + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); if (!ctx->mem) { lz4hc_destroy(ctx); return NULL; } - - /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ - ctx->level = LZ4HC_DEFAULT_CLEVEL; return ctx; } diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c index b88b408964cd..79ecfae9c4c5 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -6,7 +6,7 @@ #include "backend_lzo.h" -static void *lzo_create(void) +static void *lzo_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c index 9bf1843021b0..b0937103b5fb 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -6,7 +6,7 @@ #include "backend_lzorle.h" -static void *lzorle_create(void) +static void *lzorle_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 4a7734aa1a8a..b2fb94902bef 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -24,7 +24,7 @@ static void zstd_destroy(void *ctx) kfree(zctx); } -static void *zstd_create(void) +static void *zstd_create(struct zcomp_config *config) { zstd_parameters params; struct zstd_ctx *ctx; @@ -34,7 +34,11 @@ static void *zstd_create(void) if (!ctx) return NULL; - ctx->level = ZSTD_defaultCLevel(); + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = ZSTD_defaultCLevel(); + params = zstd_get_params(ctx->level, PAGE_SIZE); sz = zstd_cctx_workspace_bound(¶ms.cParams); ctx->cctx_mem = vzalloc(sz); diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index d49791f724e9..3f990a715487 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -61,7 +61,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) */ static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) { - zstrm->ctx = comp->backend->create_ctx(); + zstrm->ctx = comp->backend->create_ctx(comp->config); /* * allocate 2 pages. 1 for compressed data, plus 1 extra for the @@ -204,7 +204,7 @@ void zcomp_destroy(struct zcomp *comp) kfree(comp); } -struct zcomp *zcomp_create(const char *alg) +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config) { struct zcomp *comp; int error; @@ -221,6 +221,7 @@ struct zcomp *zcomp_create(const char *alg) if (!comp) return ERR_PTR(-ENOMEM); + comp->config = config; comp->backend = lookup_backend(alg); if (!comp->backend) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 757b85017e23..345c78bc76db 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -5,6 +5,7 @@ #ifndef _ZCOMP_H_ #define _ZCOMP_H_ + #include struct zcomp_strm { @@ -15,6 +16,14 @@ struct zcomp_strm { void *ctx; }; +#define ZCOMP_CONFIG_NO_LEVEL INT_MIN + +struct zcomp_config { + s32 level; + size_t dict_sz; + void *dict; +}; + struct zcomp_backend { int (*compress)(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len); @@ -22,7 +31,7 @@ struct zcomp_backend { int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst); - void *(*create_ctx)(void); + void *(*create_ctx)(struct zcomp_config *config); void (*destroy_ctx)(void *ctx); const char *name; @@ -32,6 +41,7 @@ struct zcomp_backend { struct zcomp { struct zcomp_strm __percpu *stream; struct zcomp_backend *backend; + struct zcomp_config *config; struct hlist_node node; }; @@ -40,7 +50,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); ssize_t zcomp_available_show(const char *comp, char *buf); bool zcomp_available_algorithm(const char *comp); -struct zcomp *zcomp_create(const char *alg); +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config); void zcomp_destroy(struct zcomp *comp); struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 298ef0e97e03..6c36cd349431 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1994,6 +1994,20 @@ static void zram_slot_free_notify(struct block_device *bdev, zram_slot_unlock(zram, index); } +static void zram_reset_comp_configs(struct zram *zram) +{ + u32 prio; + + for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) { + struct zcomp_config *config = &zram->configs[prio]; + + vfree(config->dict); + config->level = ZCOMP_CONFIG_NO_LEVEL; + config->dict_sz = 0; + config->dict = NULL; + } +} + static void zram_destroy_comps(struct zram *zram) { u32 prio; @@ -2007,6 +2021,8 @@ static void zram_destroy_comps(struct zram *zram) zcomp_destroy(comp); zram->num_active_comps--; } + + zram_reset_comp_configs(zram); } static void zram_reset_device(struct zram *zram) @@ -2064,7 +2080,8 @@ static ssize_t disksize_store(struct device *dev, if (!zram->comp_algs[prio]) continue; - comp = zcomp_create(zram->comp_algs[prio]); + comp = zcomp_create(zram->comp_algs[prio], + &zram->configs[prio]); if (IS_ERR(comp)) { pr_err("Cannot initialise %s compressing backend\n", zram->comp_algs[prio]); @@ -2271,6 +2288,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; + zram_reset_comp_configs(zram); comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); zram_debugfs_register(zram); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 37bf29f34d26..2c77f3bff166 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -107,6 +107,7 @@ struct zram { struct zram_table_entry *table; struct zs_pool *mem_pool; struct zcomp *comps[ZRAM_MAX_COMPS]; + struct zcomp_config configs[ZRAM_MAX_COMPS]; struct gendisk *disk; /* Prevent concurrent execution of device init */ struct rw_semaphore init_lock; From patchwork Wed May 8 07:42:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658185 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3262964CC0 for ; Wed, 8 May 2024 07:43:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154184; cv=none; b=ZFtmbBtRFcBSuJ3vlHqy6NKLufU1ElaXpR9NkvbNfBAXQoJ/6tYFyQN/0r0wm3xYhKv3D0nT+5k/jv5UTOOr9uVSXVjSwftTEH/AegijwWICmjKv908PcAEG/yqbItHGvM+NxC0eYCAswrc7hOoE7TF2nRAo4QwHVr0Dlpxi2ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154184; c=relaxed/simple; bh=k8QWr7Ce151M14jHiuPdpQHPaGdK/slKGX2a8ui9kaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tHM2BwYzYrVZVfnpDnuh+z1JEi5EqBi4elJ3tlbhuhm/5Woq+Y0/uphYKYqd3O0LfpSueZ45RiAGp1qk8rOcjK5HUZ7Ub2Xzm6QM7POIVcnbgfzyjn6moTLZsIjRd3qsMzuQgJPffuBpu3ErjCDXf0Ep2pHkxox6MlYLlFZi86c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=NLynwZUY; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="NLynwZUY" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2b6208c88dcso381613a91.3 for ; Wed, 08 May 2024 00:43:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154182; x=1715758982; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hvvjJAe8l8tDTfecPvMLnwPB1eap8xtvETRDKvyj4SM=; b=NLynwZUYFJESBQ16y+8nyJagJoMLKS90eaX099lJBGiQygndJAxb6CBLLJi6k37smi CCOFZiBr4mtPnSAhxjVEai3Nw7zTYERJGWO40WGnyDn42p1sPqAh7Gc/1qn+NbXDglUF 2fOmf5dnGroFENk+KcEGzW7VVhW+K7RJyVBgc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154182; x=1715758982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hvvjJAe8l8tDTfecPvMLnwPB1eap8xtvETRDKvyj4SM=; b=MxDwCLmMpdI6VZyiomh2rsGfbS17Acu9h9Vr0grhSS95KtigLNWv4mc3NIUt/GSXcr fgyI/sNb+m9eh7CGauD0knF4GzYy3NbWrsMJWGOzdOJt5OoWTmzXzhV67UUK8/KKSKW9 kPPaBvAfpMsz+4TztbO1Smy3wG7AItqglCR6ZEDlMEowvls7KBHSL2hxXObYh36I06Cb 04U/0MvFSA+ecYfBspF+CFvCDRqpX5ZDFB5xdTEENLfCvj73oG5obDgdcbH36Xb5rFXz EqvDRMTyIFa1f9doDI+RNKOZbWsbmzbnRBg7Za+kEpLQVlo1mFLTMOIEAU3MAe1cvxxU dFdQ== X-Forwarded-Encrypted: i=1; AJvYcCXdZ4KqD1sIKdryz78Giu73JVMhpsm1S2ZEZb0771V/iDeF5p6wczJmEbteT5U0/rA/Bt3MZkO0NynIr2bhlu9d0L9F3ff1rP2a4A8= X-Gm-Message-State: AOJu0YzjhTz4Zc9ehJ7PA9p1KB+anrLwgz0aoXicdT4bcC/kvmQfhtWT B1Y4zfGE3v94VUrqmO//Nmb60bL1DleD29TziZjyTfL0Z7oajy8ry/DrETjCNg== X-Google-Smtp-Source: AGHT+IGsT20V+REVmZfVALkjzCYeDrdJBAUngwmprLRShQjZLhtblQAF9Tf2N99ShaWbI2RxmWzVLA== X-Received: by 2002:a17:90b:1046:b0:2a7:600e:ce0 with SMTP id 98e67ed59e1d1-2b616bfdb97mr1754458a91.42.1715154182204; Wed, 08 May 2024 00:43:02 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:01 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 11/19] zram: extend comp_algorithm attr write handling Date: Wed, 8 May 2024 16:42:04 +0900 Message-ID: <20240508074223.652784-12-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Previously comp_algorithm device attr would accept only algorithm name param, however in order to enabled comp configuration we need to extend comp_algorithm_store() with param=value support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 6c36cd349431..bd8433363cbe 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1028,9 +1028,33 @@ static ssize_t comp_algorithm_store(struct device *dev, size_t len) { struct zram *zram = dev_to_zram(dev); + char *args, *param, *val; + char *alg = NULL; int ret; - ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf); + args = skip_spaces(buf); + while (*args) { + args = next_arg(args, ¶m, &val); + + /* + * We need to support 'param' without value, which is an + * old format for this attr (algorithm name only). + */ + if (!val || !*val) { + alg = param; + continue; + } + + if (!strcmp(param, "algo")) { + alg = val; + continue; + } + } + + if (!alg) + return -EINVAL; + + ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; } From patchwork Wed May 8 07:42:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658186 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 717206CDBC for ; Wed, 8 May 2024 07:43:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154186; cv=none; b=utxDpQoOhqGl17PB6HOq8n6qfnI7zjH+Y2PCJdgHsY6KMGbsGYbllt2rbjR7x5ll5rFMXUMleO+sktdosku4h2JC/KlTNpd0hZCAduJqmO89/2TDS4NPUX7WWmU8eRQlHkKvrLS+TUvu2QbzVc3GkKK6B6CrFSRHbPalJHyQA5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154186; c=relaxed/simple; bh=d4UQf/ZsLZS7cvretx2WXvAFWLig4xky110hNJGk3Zc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hyITmG7nQED/8miqFILPV+kejt17JN30jzmsy7V8PisxiW1ucxoVOs2DCdtiErIaTZyUC0RbupsJ/WBtYDAIL38/JZmLcug40OPtJruS2yaUItumy7sf+W6J0Uj6oxtmM5wnjjDofI70bsV4ycaOb4nTmzcnjRPIVZdJaZgH4is= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=LBgoz7JA; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="LBgoz7JA" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2b43490e0e2so2723642a91.2 for ; Wed, 08 May 2024 00:43:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154185; x=1715758985; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wIlqiB/ZJyLpyyyQGB102ierEcV1ZXWEtzUdufZbr+0=; b=LBgoz7JAaDMFqYFuIPioP2gVlBeVB6J9DYRtY4AmooUSmgIuYR44FKL3IW4VEu1iiW 0x6d6cxR+buZz13FZ3fNkblRG89yj+kIz05yo6353ydLY+m67WQap6FREyX7weQXuZhQ giZD1fjs6XrscGg2Zk6ePfj3oBW5/pi5ttrAg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154185; x=1715758985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wIlqiB/ZJyLpyyyQGB102ierEcV1ZXWEtzUdufZbr+0=; b=NfUbCaPdXMKUV0blktz8m/KPcH2X/NYXuAY7IIBHdBOfVYFOApaQt+3ttGBQek2DHw oRi7miixnTZcyCnLlpFHRJ8+UhBd3DvbxM8QsPojjMqw0lcYRUa1iB8Uhy6hnTwJk3zb yrgCmQFgJTPLrhjAxsR+fzGqKQkmQrtH7GlKYU3He4C9yb4cDqWnwnZw+ycuGOv8GY8L 8jIdFrbgYGgCwgjMWmeFavMgdRRml+SyMhJMtdLmWLA1jWsSOkt0gf5G4EVfZ/EjLtfh ZI4IpyPGoXSJMXOBuHydh2gs0OaU62G9EGuN5yfOzoRF5nczXUWbJMe61lD3GnNSH9cz Sm4A== X-Forwarded-Encrypted: i=1; AJvYcCWvUKxx4y8MiE37N0Q7nwzfGis7EIRaqHz59nmWIWNKcV9vek8PUjJc2VbUGFGcoH56d0ANmMGT4XPyjMJWQm7pR5MOsfyyzWX+ZSI= X-Gm-Message-State: AOJu0YyRiOksPPQeOvWN+5NQ2G/u3x4lIgQdhEM679/rcX892BfDf3aH AR/c5OSDP3TkHPGbTb1RKv0guQ0ypLkyvU/mVjb16Lw/nk/ZGLeREFY2nOFH7Q== X-Google-Smtp-Source: AGHT+IF092QUexa8q6Se5sPjVpV2rmvHb1BXFAsCgfpPU65gIEcc7ohGaINcYFa5GBGvAPDdjivYpw== X-Received: by 2002:a17:90a:b113:b0:2b1:534f:ea09 with SMTP id 98e67ed59e1d1-2b6166bfd77mr1811833a91.23.1715154184927; Wed, 08 May 2024 00:43:04 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:04 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 12/19] zram: support compression level comp config Date: Wed, 8 May 2024 16:42:05 +0900 Message-ID: <20240508074223.652784-13-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add support for compression level level=N comp configuration to comp_algorithm and recomp_algorithm knobs. Note that zram cannot verify ranges, it's a task of corresponding backends to make sure that level makes sense. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index bd8433363cbe..89a2eb37e26c 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1013,6 +1013,12 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) return 0; } +static int comp_config_store(struct zram *zram, u32 prio, s32 level) +{ + zram->configs[prio].level = level; + return 0; +} + static ssize_t comp_algorithm_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -1030,6 +1036,7 @@ static ssize_t comp_algorithm_store(struct device *dev, struct zram *zram = dev_to_zram(dev); char *args, *param, *val; char *alg = NULL; + s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; args = skip_spaces(buf); @@ -1049,12 +1056,21 @@ static ssize_t comp_algorithm_store(struct device *dev, alg = val; continue; } + + if (!strcmp(param, "level")) { + ret = kstrtoint(val, 10, &level); + if (ret) + return ret; + continue; + } } if (!alg) return -EINVAL; - ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); + ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level); + if (!ret) + ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; } @@ -1087,6 +1103,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, int prio = ZRAM_SECONDARY_COMP; char *args, *param, *val; char *alg = NULL; + s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; args = skip_spaces(buf); @@ -1107,6 +1124,13 @@ static ssize_t recomp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "level")) { + ret = kstrtoint(val, 10, &level); + if (ret) + return ret; + continue; + } } if (!alg) @@ -1115,7 +1139,9 @@ static ssize_t recomp_algorithm_store(struct device *dev, if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS) return -EINVAL; - ret = __comp_algorithm_store(zram, prio, alg); + ret = comp_config_store(zram, prio, level); + if (!ret) + ret = __comp_algorithm_store(zram, prio, alg); return ret ? ret : len; } #endif From patchwork Wed May 8 07:42:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658187 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15C416A33E for ; Wed, 8 May 2024 07:43:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154189; cv=none; b=aLpbTv1mHKWcmPN11GoMpGdj/6OkbnI1FNNc7m01xSrC8FxKNKoTKnSRc3NIIvkM7sbUx9CPl6OA+Wy1SSndeEwXABMkDxPA6Jj4fOcKSATCv/7Ie19IuUYRrnTDvggfA4emFFBKueVEyHUToAL4NxfHTszNQCNmagX9I1Irht0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154189; c=relaxed/simple; bh=hqBaAu2Ha8Gh/abMg4Cg6golRoIbO9BVZYRoWaldmIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EboNn0FWNHW8CUmLZhTJqD5bgObm2HR+BEgDUFvyqa/YpdlsaNp6+bD8L99Vf6Nt2wDd0D+b0eYv6QXw/t4KCfYYmdWw72TJVQpbs5HGE+K2D6UZlB1jXBMI5xgPMCC66I3QX9z3CISncSyWbA+LljLUZjdGT4wgrez/lt4vDXY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=PKiywfTP; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PKiywfTP" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2b432be5cc9so2900717a91.3 for ; Wed, 08 May 2024 00:43:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154187; x=1715758987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=une8u6O58vSEqdMRQHIC0PiltZGgPobzLxkJ5Uf2uCM=; b=PKiywfTP7x35TLvZ5Wl70U3In2E6HROkPCFyFEd5xKwwGj7f/8sfEVVyrk/x2H9yyi d4Fu3xK7VxfPl3Ffj33ueCtkPgxnF0LVPquV9/C8jDa5MW1wUnzkTSRWq4YHvBYToBX/ Aa6i1W3uweqQdcvs4SiSWHbcPqtwDxU3Ag6oc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154187; x=1715758987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=une8u6O58vSEqdMRQHIC0PiltZGgPobzLxkJ5Uf2uCM=; b=F4GC/oV0IDn63Qur203TUg+ZXw+7o6SfNK3S50xCJxTtPV78caQpJjR/hPIB7CZY3S sTpK/P2xZtaiZXxMCPlyOznVcrpWo/dxBM0gv91j9mM3rOIiN0rPayHZE9Pz3H/qyQWx 45cHhKEmntph5niXTfqiTs6C0dlbxGhmMr+MqWM+Z0VYIvdrfJX7eA2sYw9hju+yqzx7 8LQHCry1r92lMTvr8fM+abVYWU0CSzm2YinSoA36oObQqqptpz4VjQB/WlO2HuKinGF1 HroSEKt3BprlvVkmuKL7gCu/xzZmcH+RkAPiWz5OXkroixblDxf1/mHZqLt9e8reZE7C I2PA== X-Forwarded-Encrypted: i=1; AJvYcCWEIf+8KymRR8mFHHYZLSFFrAU2j2vCvii4+k0vJsiyrFkdFJfyBTxY43DQhulfTi3M0purFLQDd+i5DvOE2ZicUIDy5m9mMwDYYtw= X-Gm-Message-State: AOJu0YzIcmgexaxKG+bLM9SS0GkOu97KidxpxT7BEnlchZabdTiGHBoK C4k/RNJwSefBORUJ98wuUAk8M+3fhqQT9luRVmgNTWAljbLlmaVpkc6Ep9YX2g== X-Google-Smtp-Source: AGHT+IHVw5rVeShNRu6xrdN8APaPx44Qmmg3cThkOLagufafTV2ioHSynqyx4HHegBvdxZGaapvtwQ== X-Received: by 2002:a17:90a:aa81:b0:2b6:3792:ea4d with SMTP id 98e67ed59e1d1-2b637932ademr433346a91.5.1715154187479; Wed, 08 May 2024 00:43:07 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:07 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 13/19] zram: add support for dict comp config Date: Wed, 8 May 2024 16:42:06 +0900 Message-ID: <20240508074223.652784-14-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Handle dict=path param so that we can read a pre-trained compression algorithm dictionary which we then pass to the backend configuration. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 54 ++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 89a2eb37e26c..863e4e125eaa 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "zram_drv.h" @@ -1013,9 +1014,35 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) return 0; } -static int comp_config_store(struct zram *zram, u32 prio, s32 level) +static void __reset_comp_config(struct zram *zram, u32 prio) { + struct zcomp_config *config = &zram->configs[prio]; + + vfree(config->dict); + config->level = ZCOMP_CONFIG_NO_LEVEL; + config->dict_sz = 0; + config->dict = NULL; +} + +static int comp_config_store(struct zram *zram, u32 prio, s32 level, + const char *dict_path) +{ + ssize_t sz = 0; + + __reset_comp_config(zram, prio); + + if (dict_path) { + sz = kernel_read_file_from_path(dict_path, 0, + &zram->configs[prio].dict, + INT_MAX, + NULL, + READING_POLICY); + if (sz < 0) + return -EINVAL; + } + zram->configs[prio].level = level; + zram->configs[prio].dict_sz = sz; return 0; } @@ -1035,7 +1062,7 @@ static ssize_t comp_algorithm_store(struct device *dev, { struct zram *zram = dev_to_zram(dev); char *args, *param, *val; - char *alg = NULL; + char *alg = NULL, *dict_path = NULL; s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; @@ -1063,12 +1090,17 @@ static ssize_t comp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "dict")) { + dict_path = val; + continue; + } } if (!alg) return -EINVAL; - ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level); + ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level, dict_path); if (!ret) ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; @@ -1102,7 +1134,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, struct zram *zram = dev_to_zram(dev); int prio = ZRAM_SECONDARY_COMP; char *args, *param, *val; - char *alg = NULL; + char *alg = NULL, *dict_path = NULL; s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; @@ -1131,6 +1163,11 @@ static ssize_t recomp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "dict")) { + dict_path = val; + continue; + } } if (!alg) @@ -1139,7 +1176,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS) return -EINVAL; - ret = comp_config_store(zram, prio, level); + ret = comp_config_store(zram, prio, level, dict_path); if (!ret) ret = __comp_algorithm_store(zram, prio, alg); return ret ? ret : len; @@ -2049,12 +2086,7 @@ static void zram_reset_comp_configs(struct zram *zram) u32 prio; for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) { - struct zcomp_config *config = &zram->configs[prio]; - - vfree(config->dict); - config->level = ZCOMP_CONFIG_NO_LEVEL; - config->dict_sz = 0; - config->dict = NULL; + __reset_comp_config(zram, prio); } } From patchwork Wed May 8 07:42:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658188 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5D1A745D5 for ; Wed, 8 May 2024 07:43:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154192; cv=none; b=IDUKZEgNAE1QxzWypO8TfY84V5fbblxs6aBgvA26mdCIXF3mm78BPYGUHq3kcQ262ubMZ+otWZeEVBVVLjrxaoruIFLbJ2n2tNM4+JJvJqCh1ENmfDXR5TcRQ0nnBwa+IZv8SL0f1URzDimwCBU/l0ZJ+FOQXOMDaP3EAWuh8h8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154192; c=relaxed/simple; bh=S+q9egEJGrJejpCCNByxOEX2jrCMT0cr23KcD3p6dh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NzoYs398ftrFYlljJE3OB+18+rMSv3l2kLLoRmd3PVVoIHoCZ5p0fzQI/Mnj/qSMrfXSa6WhQIDgax87lMzx8iNEOMwS7tMFLIiN0kZPb/xwhz9rvYXdb+DWvRE7zh4uQiCOl0u4OJgoXiA1C5m5x70fsvnhc8opxwxEnl0IXLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=C/fFqpBH; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="C/fFqpBH" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2b4952a1b51so3123393a91.0 for ; Wed, 08 May 2024 00:43:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154190; x=1715758990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=egL7/ZOm/LwTDOSUeZ3gfevqUUa8V/EYy8mdcUtRQbg=; b=C/fFqpBHUFTgOJ+SKvQAlmoH8yvxpTX1JXg+wLvGA9pVuCiL26eEVREbP3x8b+uCtW ifcd6VFQTpK1PWBS0o8/pOff5uXs6aSfz5PvsYiVwtUp0Vlab6VqX9rLcErDcxirChF5 wbBZrTsGI0rjRcFBHte456n9VSKuacQM63HEQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154190; x=1715758990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=egL7/ZOm/LwTDOSUeZ3gfevqUUa8V/EYy8mdcUtRQbg=; b=S76XZxxwClOGYpCICnpDYgzPcmOQ5QZZgvQiTU/VxFUulx8PDk60USXn7GcK7sfFyP iAuqe57dS82H585zEXEv3fuubF6cq2IYD8UQRf6RoSZfpUoSaQ2vQgbJRscXmu3EJh6/ XzhE3WE6R/VetBKkbptdWQ5afKHY3x9iUHJVqUYtfbQSL+nxaRHnHuDBHUZvEnxYCw9e QrtaswXAEHIiF+Np5GLHyr6x73dvngzp9NhXupjXqWaQFaKDepAyjT8FO5XDQ5HTv2rI E8uKKwvbm5I8yafkrchAbuC0h2E83CxgkGhJCV6ekfxegHqjJeJJKhyXNO+IY4GzoQU/ icKQ== X-Forwarded-Encrypted: i=1; AJvYcCUsoEBxf8TJ01G24DJnLW92NWAy2KX0qLszEj0M++HR8Up9rbgYw8zgEGzikpFMAw+o0v96io6y6huSQa3+OYScTroeNeUTR6XWezk= X-Gm-Message-State: AOJu0Yyj785Op07I9zSaakJy/hQAeQxxvoYunhnhaZwnVHv9nOyAZgCP GBgI6Cf3/Su16suGChhw49B6P84VA2NZzPK88h8glPo/edFkqnk54uTq0NqS/g== X-Google-Smtp-Source: AGHT+IFZBqF0jjYvlPfasWPt5+AF2jKv+Gvu8wpV0lbwhiTsU35SOMNKGqwZcxg+Jy7o1OR2mNPBRA== X-Received: by 2002:a17:90a:a592:b0:2a3:be59:e969 with SMTP id 98e67ed59e1d1-2b616c05945mr1560239a91.47.1715154190329; Wed, 08 May 2024 00:43:10 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:10 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 14/19] zram: add dictionary support to zstd backend Date: Wed, 8 May 2024 16:42:07 +0900 Message-ID: <20240508074223.652784-15-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This adds support for pre-trained zstd dictionaries [1] Dictionary is loaded once (per-config) and then loaded to Cctx and Dctx by reference, so we don't allocate extra memory. Regarding GFP_ATOMIC in Cctx customAlloc(), we probably would want to do something about it. Either make sure that we always (somehow) fully setup all Cctx contexts from non-atomic context before we attempt to use them, come up with some sort of custom allocator or stop calling zcomp_compress() from atomic context. TEST ==== - default zstd /sys/block/zram0/mm_stat 1750315008 504602831 514256896 0 514256896 1 0 34204 34204 - zstd level=7 dict=/etc/dictionary /sys/block/zram0/mm_stat 1750310912 432540606 441712640 0 441712640 1 0 34187 34187 [1] https://github.com/facebook/zstd/blob/dev/programs/zstd.1.md#dictionary-builder Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 126 ++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 25 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index b2fb94902bef..006d0f40617d 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -12,23 +12,52 @@ struct zstd_ctx { zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; + ZSTD_customMem ctx_mem; + ZSTD_CDict *cdict; + ZSTD_DDict *ddict; s32 level; }; +/* + * Cctx allocator.customAlloc() can be called from zcomp_compress() under + * local-lock (per-CPU compression stream), in which case we must use + * GFP_ATOMIC. + */ +static void *zstd_ctx_alloc(void *opaque, size_t size) +{ + if (!preemptible()) + return kvzalloc(size, GFP_ATOMIC); + + return kvzalloc(size, GFP_KERNEL); +} + +static void zstd_ctx_free(void *opaque, void *address) +{ + kvfree(address); +} + static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; - vfree(zctx->cctx_mem); - vfree(zctx->dctx_mem); + if (zctx->cctx_mem) + vfree(zctx->cctx_mem); + else + ZSTD_freeCCtx(zctx->cctx); + + if (zctx->dctx_mem) + vfree(zctx->dctx_mem); + else + ZSTD_freeDCtx(zctx->dctx); + + ZSTD_freeCDict(zctx->cdict); + ZSTD_freeDDict(zctx->ddict); kfree(zctx); } static void *zstd_create(struct zcomp_config *config) { - zstd_parameters params; struct zstd_ctx *ctx; - size_t sz; ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -39,24 +68,62 @@ static void *zstd_create(struct zcomp_config *config) else ctx->level = ZSTD_defaultCLevel(); - params = zstd_get_params(ctx->level, PAGE_SIZE); - sz = zstd_cctx_workspace_bound(¶ms.cParams); - ctx->cctx_mem = vzalloc(sz); - if (!ctx->cctx_mem) - goto error; - - ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); - if (!ctx->cctx) - goto error; - - sz = zstd_dctx_workspace_bound(); - ctx->dctx_mem = vzalloc(sz); - if (!ctx->dctx_mem) - goto error; - - ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); - if (!ctx->dctx) - goto error; + ctx->ctx_mem.customAlloc = zstd_ctx_alloc; + ctx->ctx_mem.customFree = zstd_ctx_free; + + if (config->dict_sz == 0) { + zstd_parameters params; + size_t sz; + + params = zstd_get_params(ctx->level, PAGE_SIZE); + sz = zstd_cctx_workspace_bound(¶ms.cParams); + ctx->cctx_mem = vzalloc(sz); + if (!ctx->cctx_mem) + goto error; + + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); + if (!ctx->cctx) + goto error; + + sz = zstd_dctx_workspace_bound(); + ctx->dctx_mem = vzalloc(sz); + if (!ctx->dctx_mem) + goto error; + + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); + if (!ctx->dctx) + goto error; + } else { + ZSTD_compressionParameters params; + + ctx->cctx = ZSTD_createCCtx_advanced(ctx->ctx_mem); + if (!ctx->cctx) + goto error; + + ctx->dctx = ZSTD_createDCtx_advanced(ctx->ctx_mem); + if (!ctx->dctx) + goto error; + + params = ZSTD_getCParams(ctx->level, PAGE_SIZE, + config->dict_sz); + + ctx->cdict = ZSTD_createCDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx->ctx_mem); + if (!ctx->cdict) + goto error; + + ctx->ddict = ZSTD_createDDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + ctx->ctx_mem); + if (!ctx->ddict) + goto error; + } return ctx; @@ -72,8 +139,12 @@ static int zstd_compress(void *ctx, const unsigned char *src, const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; - ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, - src, PAGE_SIZE, ¶ms); + if (!zctx->cdict) + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, ¶ms); + else + ret = ZSTD_compress_usingCDict(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, zctx->cdict); if (zstd_is_error(ret)) return -EINVAL; *dst_len = ret; @@ -86,7 +157,12 @@ static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, struct zstd_ctx *zctx = ctx; size_t ret; - ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); + if (!zctx->ddict) + ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, + src, src_len); + else + ret = ZSTD_decompress_usingDDict(zctx->dctx, dst, PAGE_SIZE, + src, src_len, zctx->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; From patchwork Wed May 8 07:42:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658189 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 763F4768FC for ; Wed, 8 May 2024 07:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154195; cv=none; b=OhB8eNFQFU0OTagrZekEtAvose+qQuNpvd0en8YQBOtOI4tQripDIbhw6EBYkUT5w5eAdIQEPgBMQIxV1RNnkgTQrG2km+P0CcQU/VM3s5qhi2MW/ahRZWd1tjalZSnYt+OpZZe+HiBRn/+zDxtFWMwaP6vgZVYKupCxNG4nRh4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154195; c=relaxed/simple; bh=UKwqjwb9x/OsHkTqcI+97IPeKV8KGD2MI9Ln01YNtM4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r5j52qhNNDoABSvabXLEiaCsTwUqbzmtCln56cTVeDtAi40n7aS65xUFOGksDGoy7g8f9CSlRAEKJ6Zv21uOXUOlA/t4/Cp71nFnWD1vf4gT/mek+UroYQHS6HBrZpF/8NDgITs8sbYlua5jaGiCANurTWSJ+n6wz17ob3KPYuY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=CQyjCJsr; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CQyjCJsr" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5f415fd71f8so3092572a12.3 for ; Wed, 08 May 2024 00:43:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154193; x=1715758993; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=px5tFo3EeYPEdZvOSeLSW/x5RbwO5TsDAN0dXuxOAa4=; b=CQyjCJsrqy/IbH4Z7UU9tMWD2zPEuJ5C+ou+7r7RDVSvD8f/QUHCwJRNMqXSbBVo8g eCMMS/NAS09MmeQj6oYGje9n1R5wmh5CQzAVxuWhROuHTqkxg6Hrs3gTuNeOzBIgsH3U 4yideXo6OYmL9QBEt1T3THUEZEiB72LIPon1o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154193; x=1715758993; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=px5tFo3EeYPEdZvOSeLSW/x5RbwO5TsDAN0dXuxOAa4=; b=qQyNXlI8i3o/a69jOVJH90K4tXOMKSOSC3c1DRFGqSqFgz5TTlTVNR09JFlxYd7VBS L+slsX3v9BKWmlPbUAQ/cYQntnTRva5vupCC0sMbZlL68kE988vADl40tBSKlEmrJILh RQMD+fz7vuwtLn4HmLeV/N2gj80TZzelqxB4SonsMQa/fKavicnRMeYhsOSjR14/KmHo apH0OXqUnZ5U44iYQOx6OWXbUCK0oANG92AquYYKK3MygRDAthS9ZyrW1bu8ZfjFgGBA XNudO1aTAb7dgVoKzWzjlqrCfAE4QdQGhnIC+3tMpe+vDLnQPgjWUfubT0/kmIoDbv7a 9SwA== X-Forwarded-Encrypted: i=1; AJvYcCU8rgP4a3e3935ZK78mdjkJO6CrMueCw3Ez+hMi9B91eH6q3tdc8frqdotTr7A3TORREpx0dfai0hT/NjmZIuo6kDh1ffUSW7Gkw2o= X-Gm-Message-State: AOJu0YxncQDNX4M9WVTQgcXNGQYAek/I+16GvazmVTpIqd7v1YR7wiqG +yty5dlY/FSmybcBY3j0WOYMKAhyIob7jmQWFo0nX2LxykNQFlnE+x7JDbgbgw== X-Google-Smtp-Source: AGHT+IFEUcpcmTHNCeMSDBeBNcfOgk3fQHMS7MIX9XAd+uxg/0r0afRpez4CJ7Do2vUXxVfXGtcYjQ== X-Received: by 2002:a05:6a20:394d:b0:1ac:4219:b817 with SMTP id adf61e73a8af0-1afc8d75e57mr2333745637.16.1715154192917; Wed, 08 May 2024 00:43:12 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:12 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 15/19] zram: add config init/release backend callbacks Date: Wed, 8 May 2024 16:42:08 +0900 Message-ID: <20240508074223.652784-16-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some backends can create a backend-specific private data for comp config, e.g. parse a dictionary and use it to init all of the ctx-s later on. Introduce two zcomp_config callbacks to create and destroy per-config backend private data. This is also the place where config can be validated. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_842.c | 11 +++++++++++ drivers/block/zram/backend_deflate.c | 20 +++++++++++++++----- drivers/block/zram/backend_lz4.c | 11 +++++++++++ drivers/block/zram/backend_lz4hc.c | 20 +++++++++++++++----- drivers/block/zram/backend_lzo.c | 11 +++++++++++ drivers/block/zram/backend_lzorle.c | 11 +++++++++++ drivers/block/zram/backend_zstd.c | 20 +++++++++++++++----- drivers/block/zram/zcomp.c | 6 ++++++ drivers/block/zram/zcomp.h | 4 ++++ drivers/block/zram/zram_drv.c | 3 +++ 10 files changed, 102 insertions(+), 15 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c index 12e716deb763..1522aa88ac35 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -11,6 +11,15 @@ struct sw842_ctx { void *mem; }; +static int init_config_842(struct zcomp_config *config) +{ + return 0; +} + +static void release_config_842(struct zcomp_config *config) +{ +} + static void destroy_842(void *ctx) { struct sw842_ctx *zctx = ctx; @@ -64,5 +73,7 @@ struct zcomp_backend backend_842 = { .decompress = decompress_842, .create_ctx = create_842, .destroy_ctx = destroy_842, + .init_config = init_config_842, + .release_config = release_config_842, .name = "842", }; diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c index 83c660adc722..2e166b507f5a 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -17,6 +17,18 @@ struct deflate_ctx { s32 level; }; +static int deflate_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = Z_DEFAULT_COMPRESSION; + + return 0; +} + +static void deflate_release_config(struct zcomp_config *config) +{ +} + static void deflate_destroy(void *ctx) { struct deflate_ctx *zctx = ctx; @@ -42,11 +54,7 @@ static void *deflate_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = Z_DEFAULT_COMPRESSION; - + ctx->level = config->level; sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); ctx->cctx.workspace = vzalloc(sz); if (!ctx->cctx.workspace) @@ -129,5 +137,7 @@ struct zcomp_backend backend_deflate = { .decompress = deflate_decompress, .create_ctx = deflate_create, .destroy_ctx = deflate_destroy, + .init_config = deflate_init_config, + .release_config = deflate_release_config, .name = "deflate", }; diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 98d9c9274149..560fcf139301 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -4,6 +4,15 @@ #include "backend_lz4.h" +static int lz4_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lz4_release_config(struct zcomp_config *config) +{ +} + static void *lz4_create(struct zcomp_config *config) { return vmalloc(LZ4_MEM_COMPRESS); @@ -43,5 +52,7 @@ struct zcomp_backend backend_lz4 = { .decompress = lz4_decompress, .create_ctx = lz4_create, .destroy_ctx = lz4_destroy, + .init_config = lz4_init_config, + .release_config = lz4_release_config, .name = "lz4", }; diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index e1069f185437..431a44f0fcfd 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -10,6 +10,18 @@ struct lz4hc_ctx { s32 level; }; +static int lz4hc_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = LZ4HC_DEFAULT_CLEVEL; + + return 0; +} + +static void lz4hc_release_config(struct zcomp_config *config) +{ +} + static void lz4hc_destroy(void *ctx) { struct lz4hc_ctx *zctx = ctx; @@ -26,11 +38,7 @@ static void *lz4hc_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = LZ4HC_DEFAULT_CLEVEL; - + ctx->level = config->level; ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); if (!ctx->mem) { lz4hc_destroy(ctx); @@ -70,5 +78,7 @@ struct zcomp_backend backend_lz4hc = { .decompress = lz4hc_decompress, .create_ctx = lz4hc_create, .destroy_ctx = lz4hc_destroy, + .init_config = lz4hc_init_config, + .release_config = lz4hc_release_config, .name = "lz4hc", }; diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c index 79ecfae9c4c5..75e0e3d297aa 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -6,6 +6,15 @@ #include "backend_lzo.h" +static int lzo_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lzo_release_config(struct zcomp_config *config) +{ +} + static void *lzo_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); @@ -40,5 +49,7 @@ struct zcomp_backend backend_lzo = { .decompress = lzo_decompress, .create_ctx = lzo_create, .destroy_ctx = lzo_destroy, + .init_config = lzo_init_config, + .release_config = lzo_release_config, .name = "lzo", }; diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c index b0937103b5fb..d1dc2e35061b 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -6,6 +6,15 @@ #include "backend_lzorle.h" +static int lzorle_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lzorle_release_config(struct zcomp_config *config) +{ +} + static void *lzorle_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); @@ -40,5 +49,7 @@ struct zcomp_backend backend_lzorle = { .decompress = lzorle_decompress, .create_ctx = lzorle_create, .destroy_ctx = lzorle_destroy, + .init_config = lzorle_init_config, + .release_config = lzorle_release_config, .name = "lzo-rle", }; diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 006d0f40617d..df59584b0337 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -36,6 +36,18 @@ static void zstd_ctx_free(void *opaque, void *address) kvfree(address); } +static int zstd_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = ZSTD_defaultCLevel(); + + return 0; +} + +static void zstd_release_config(struct zcomp_config *config) +{ +} + static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; @@ -63,11 +75,7 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = ZSTD_defaultCLevel(); - + ctx->level = config->level; ctx->ctx_mem.customAlloc = zstd_ctx_alloc; ctx->ctx_mem.customFree = zstd_ctx_free; @@ -173,5 +181,7 @@ struct zcomp_backend backend_zstd = { .decompress = zstd_decompress, .create_ctx = zstd_create, .destroy_ctx = zstd_destroy, + .init_config = zstd_init_config, + .release_config = zstd_release_config, .name = "zstd", }; diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 3f990a715487..a7013a4b6575 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -187,18 +187,24 @@ static int zcomp_init(struct zcomp *comp) if (!comp->stream) return -ENOMEM; + ret = comp->backend->init_config(comp->config); + if (ret) + goto cleanup; + ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node); if (ret < 0) goto cleanup; return 0; cleanup: + comp->backend->release_config(comp->config); free_percpu(comp->stream); return ret; } void zcomp_destroy(struct zcomp *comp) { + comp->backend->release_config(comp->config); cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node); free_percpu(comp->stream); kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 345c78bc76db..aa604e5db7ad 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -22,6 +22,7 @@ struct zcomp_config { s32 level; size_t dict_sz; void *dict; + void *private; }; struct zcomp_backend { @@ -34,6 +35,9 @@ struct zcomp_backend { void *(*create_ctx)(struct zcomp_config *config); void (*destroy_ctx)(void *ctx); + int (*init_config)(struct zcomp_config *config); + void (*release_config)(struct zcomp_config *config); + const char *name; }; diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 863e4e125eaa..d667fe2d0cd9 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1018,6 +1018,9 @@ static void __reset_comp_config(struct zram *zram, u32 prio) { struct zcomp_config *config = &zram->configs[prio]; + /* config->private should be freed by the backend */ + WARN_ON_ONCE(config->private); + vfree(config->dict); config->level = ZCOMP_CONFIG_NO_LEVEL; config->dict_sz = 0; From patchwork Wed May 8 07:42:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658190 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF8A97BB1B for ; Wed, 8 May 2024 07:43:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154197; cv=none; b=Txe28ywTlEUh4ZatAAvN4mEfrYK5iVT1bKVy9N6jxJKDVtwYdooaCARoaqRVTSCgIQpPerzMudbmtKkQfCcxA8JlP5Fiu2fjwsLBad1bqdMTfW5llOklEEKSbKMga1UjYtdhC2Xl13yPF/PwbLRe4OxIjwwGwTK0X8fT74Hu1+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154197; c=relaxed/simple; bh=d3vFRBqQt4rMVioo0/wvp6HT9RZQh4Y89EivFcAwXXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tbFvJw0/UanRB0rm5ZhFgZoYKMhnfa2oKQaagvgylVUJ8HoakwV95jZ0Gf9Clavkf5f+eZ2gTNP4M5/OuYmyBnyr94tPOIE5Qt75Bj2DgkCIhOQVc6gbUQs3MObp9NxmeFWhKfgG8Qh0XsjNsRQVIaJmhsG/SfjazUZm/wYjRIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=LapmttX9; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="LapmttX9" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2b27eec1eb1so2915610a91.0 for ; Wed, 08 May 2024 00:43:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154195; x=1715758995; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UawFcLvcVVuz6l7D9w7wTxKhqDne11bGdAUI9Tc6zcg=; b=LapmttX97atai9mkktgyd6PWh7UO+n9kNoZYrhy0fxaJrd7FxhHgl1dOximLiR6pwB i84YOGMhpOu01Wpcsecw2DX6M/AuTPr4ntXdfw3xo14H5zAFXlT5GG2ziisTnQLnQMPv Pca7Zk8nBkr4UlD5QDWBzRdSvyJXZ9iHp6Mdg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154195; x=1715758995; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UawFcLvcVVuz6l7D9w7wTxKhqDne11bGdAUI9Tc6zcg=; b=tLQ+aagClsYUymIzeAIZURPkz3CbMGEpV1VzkS5PcA4UZ+NeSVV17BaRZ7GDV0cw0n J7bWRgtkxVlOGUZ39o+T3b4h3fN8iwNzQSW2lqlPB/F30NtqVhCFkdt9RxMfYLHUmREm P3HKQEzpjbKtDbbIO2wxxbkEEFLbeSUxliPHVzdjV9WcIKLzMuasGHgRRWdjUPut81pn wzbe6L+JN4+WFSl15RQYMpRdXURDgetpfQp1Al7SNHkkdpaYks4Mv4cfT1xex7B/fIsJ jskDG5fO8ryoOLenpGgeSpaifDKTvzc0SPR6s8rBAOeryDOuklDBkxV4BNZYhq+rMqEh FUOw== X-Forwarded-Encrypted: i=1; AJvYcCVBYG1KINfPON+jfwKVOad/RgrXXoPbBoTdn1kOQL6XLfljz7077TgXreY0oDgRlh5MJxO17TSpY9Ql9RjzpQTwhSYYlZcXNzzPUgA= X-Gm-Message-State: AOJu0YyIrKWuLkfj2dOCyWAPaIKnCIYxWMmJUGS29gBcl7WOy8qjJOR6 zvcoCh6sMEcLv+YA9rgOyjPj259IgKmaIvf8CU6Iszg0/dyj71OI278KkRI1jrwut8/AvL5PWPk = X-Google-Smtp-Source: AGHT+IGkpZlHcB9WqUD6yMyxsDlX1n4CcJPrejHi03UqRulpJXLYN7DEIDkfCY04As9LHb5/AWDHEw== X-Received: by 2002:a17:90a:6584:b0:2b3:c011:d28c with SMTP id 98e67ed59e1d1-2b616ae482bmr1652869a91.48.1715154195476; Wed, 08 May 2024 00:43:15 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:15 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 16/19] zram: share dictionaries between per-CPU contexts Date: Wed, 8 May 2024 16:42:09 +0900 Message-ID: <20240508074223.652784-17-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 zstd's CDict and DDict are accessed read-only during compression and decompression, so instead of allocation per-context dictionaries we can create just one CDict and DDict in init_config() and make per-CPU contexts use them. This saves quite a lot of memory: on my system CDict requires 408128 bytes and DDict requires 27352 bytes. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 106 ++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 36 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index df59584b0337..99115b21c264 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -7,14 +7,18 @@ #include "backend_zstd.h" +struct zstd_ctx_data { + ZSTD_customMem ctx_mem; + ZSTD_CDict *cdict; + ZSTD_DDict *ddict; +}; + struct zstd_ctx { zstd_cctx *cctx; zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; - ZSTD_customMem ctx_mem; - ZSTD_CDict *cdict; - ZSTD_DDict *ddict; + struct zstd_ctx_data *ctx_data; s32 level; }; @@ -38,32 +42,81 @@ static void zstd_ctx_free(void *opaque, void *address) static int zstd_init_config(struct zcomp_config *config) { + struct zstd_ctx_data *ctx_data = config->private; + ZSTD_compressionParameters params; + + /* Already initialized */ + if (ctx_data) + return 0; + if (config->level == ZCOMP_CONFIG_NO_LEVEL) config->level = ZSTD_defaultCLevel(); + if (config->dict_sz == 0) + return 0; + + ctx_data = kzalloc(sizeof(*ctx_data), GFP_KERNEL); + if (!ctx_data) + return -ENOMEM; + + ctx_data->ctx_mem.customAlloc = zstd_ctx_alloc; + ctx_data->ctx_mem.customFree = zstd_ctx_free; + + params = ZSTD_getCParams(config->level, PAGE_SIZE, config->dict_sz); + + ctx_data->cdict = ZSTD_createCDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx_data->ctx_mem); + if (!ctx_data->cdict) + goto error; + + ctx_data->ddict = ZSTD_createDDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + ctx_data->ctx_mem); + if (!ctx_data->ddict) + goto error; + + config->private = ctx_data; return 0; + +error: + ZSTD_freeCDict(ctx_data->cdict); + ZSTD_freeDDict(ctx_data->ddict); + kfree(ctx_data); + return -EINVAL; } static void zstd_release_config(struct zcomp_config *config) { + struct zstd_ctx_data *ctx_data = config->private; + + if (!ctx_data) + return; + + config->private = NULL; + ZSTD_freeCDict(ctx_data->cdict); + ZSTD_freeDDict(ctx_data->ddict); + kfree(ctx_data); } static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; + /* Don't free zctx->ctx_data, it's done in release_config() */ if (zctx->cctx_mem) vfree(zctx->cctx_mem); else ZSTD_freeCCtx(zctx->cctx); - if (zctx->dctx_mem) vfree(zctx->dctx_mem); else ZSTD_freeDCtx(zctx->dctx); - - ZSTD_freeCDict(zctx->cdict); - ZSTD_freeDDict(zctx->ddict); kfree(zctx); } @@ -75,9 +128,8 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx) return NULL; + ctx->ctx_data = config->private; ctx->level = config->level; - ctx->ctx_mem.customAlloc = zstd_ctx_alloc; - ctx->ctx_mem.customFree = zstd_ctx_free; if (config->dict_sz == 0) { zstd_parameters params; @@ -102,35 +154,15 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx->dctx) goto error; } else { - ZSTD_compressionParameters params; + struct zstd_ctx_data *ctx_data = ctx->ctx_data; - ctx->cctx = ZSTD_createCCtx_advanced(ctx->ctx_mem); + ctx->cctx = ZSTD_createCCtx_advanced(ctx_data->ctx_mem); if (!ctx->cctx) goto error; - ctx->dctx = ZSTD_createDCtx_advanced(ctx->ctx_mem); + ctx->dctx = ZSTD_createDCtx_advanced(ctx_data->ctx_mem); if (!ctx->dctx) goto error; - - params = ZSTD_getCParams(ctx->level, PAGE_SIZE, - config->dict_sz); - - ctx->cdict = ZSTD_createCDict_advanced(config->dict, - config->dict_sz, - ZSTD_dlm_byRef, - ZSTD_dct_auto, - params, - ctx->ctx_mem); - if (!ctx->cdict) - goto error; - - ctx->ddict = ZSTD_createDDict_advanced(config->dict, - config->dict_sz, - ZSTD_dlm_byRef, - ZSTD_dct_auto, - ctx->ctx_mem); - if (!ctx->ddict) - goto error; } return ctx; @@ -144,15 +176,16 @@ static int zstd_compress(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len) { struct zstd_ctx *zctx = ctx; + struct zstd_ctx_data *ctx_data = zctx->ctx_data; const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; - if (!zctx->cdict) + if (!ctx_data) ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, src, PAGE_SIZE, ¶ms); else ret = ZSTD_compress_usingCDict(zctx->cctx, dst, *dst_len, - src, PAGE_SIZE, zctx->cdict); + src, PAGE_SIZE, ctx_data->cdict); if (zstd_is_error(ret)) return -EINVAL; *dst_len = ret; @@ -163,14 +196,15 @@ static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst) { struct zstd_ctx *zctx = ctx; + struct zstd_ctx_data *ctx_data = zctx->ctx_data; size_t ret; - if (!zctx->ddict) + if (!ctx_data) ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); else ret = ZSTD_decompress_usingDDict(zctx->dctx, dst, PAGE_SIZE, - src, src_len, zctx->ddict); + src, src_len, ctx_data->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; From patchwork Wed May 8 07:42:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658191 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19F097E0E8 for ; Wed, 8 May 2024 07:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154201; cv=none; b=oiaRp/TA87YHIeGBpCEJWWkknvDriknnKZw06zicbPs196XwPipWhU2IZbOwp4LdupHOY45kQ0f41CzIt7iUVAIwKyUBIpy/Lxr806bWMWHhjQgLlHiSjx3FZHMLSVZBwdEqne4QK+zjDvJIDWPY/SJucPuB4lebRSVCswpIh6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154201; c=relaxed/simple; bh=cIuDRIJ4nUfk74gqjSPgbW11lj17tju3W5di3bnGXlI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DkatrMTMEFlKnD+8+BpThxrCgFKVQDhQ1m59cgWtJxcloIJaGavl+urUvBvCvBA/OqWroHrjjG/LFeWihxOorlhinNfhKpKVf/0GYC9gjIORlclNYNdixnvB+z2wHaWcqzaizZmqTUqSEEpd/BfZAkqccjxZyF/pUDgtpQWsELM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BklmlNhU; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BklmlNhU" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2ad8fb779d2so3082957a91.0 for ; Wed, 08 May 2024 00:43:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154198; x=1715758998; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aM4w/1Tu/L1seDE9eZUHI5/Rr3RZ+Tacd81KpjjZOe8=; b=BklmlNhUbOHzYIwk5uFuRWHwsCkrhaPL1t9eZjF6jIR/z6721CLZJoIlT5yO3IQgJs FSmFsFRXWOmjqfCce2GaSJHPzwcCoh8FHGCXl/R17BdxwiFU1N9bhyeZ4JeZ/Kh5fG4J PiPJVvlEgtZfjebR+C/SGFJ0YS5zCnZdhsjm4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154198; x=1715758998; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aM4w/1Tu/L1seDE9eZUHI5/Rr3RZ+Tacd81KpjjZOe8=; b=vYsma+cF4YVCPEV0yMnMiUvJzG9yYxocBrxpBrpvZX/UmlqWl8RQBcXCd2DEAKN4XF pYi96AFXvpcPb4nSad0jRR4wYv5qPLrIHFZtp0g8FMMPuhGn01NCPn4I4Q85te4BEgXx 4HQiDWmwLIZRFt0Dp/BD7TWiLSF2mPlHnRpaUzbkZj/OLNu+hmWw4P6ZzLFH0/vC19DB SjeTAEt5bSnQ1Z5S2qWaiolcqp+lbUhjwcWmY5UfIIeHsI7j2X92xvwJPOb/fiKakGuL lgwVFv9/x3aN4Q++6/8Fea9hFBY4Zfuk/+ndRqNhjXtHVufkb0gSWY5y2FB61PkbgCof bBPQ== X-Forwarded-Encrypted: i=1; AJvYcCWvC2Pd4O3MpUi8/AGD42kHZWdWpqUonQOXD3OdM6kVEwXppVamc5E8XLTEDshl/qI2wBRMgVwud47mIiRRzpy6/E9edGTRffs5pqk= X-Gm-Message-State: AOJu0YyGOC3cedzt5LRgGKpmVzHFTFnQOG7AnmlEhSI3Y+w8r56WN8Si EaF5tGKy79oWMDWk1fqWlIjxVOoHLNTc+bNitKcBtQNdmHbzswURyveBhM6WRQ== X-Google-Smtp-Source: AGHT+IHEfuikcLcEdzIHPVyHM4RdbcbRmnsyhBsB7pjK11xJlOMqxiCR+jIEf8xqbF0MBD0Ty2IxqA== X-Received: by 2002:a17:90a:f008:b0:2b6:208b:ca8e with SMTP id 98e67ed59e1d1-2b6208bcdc3mr1083278a91.30.1715154198462; Wed, 08 May 2024 00:43:18 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:18 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 17/19] zram: add dictionary support to lz4 Date: Wed, 8 May 2024 16:42:10 +0900 Message-ID: <20240508074223.652784-18-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Support pre-trained dictionary param. lz4 doesn't mandate specific format of the dictionary and even zstd --train can be used to train a dictionary for lz4, according to [1]. TEST ==== - default lz4 /sys/block/zram0/mm_stat 1750323200 664258735 676990976 0 676990976 2 0 34288 34288 - lz4 dict=/etc/dictionary /sys/block/zram0/mm_stat 1750310912 620608254 632852480 0 632852480 1 0 34288 34288 [1] https://github.com/lz4/lz4/issues/557 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_lz4.c | 77 +++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 560fcf139301..2c3b64058adf 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -1,9 +1,20 @@ #include #include +#include #include #include "backend_lz4.h" +struct lz4_ctx { + void *mem; + LZ4_streamDecode_t *dstrm; + LZ4_stream_t *cstrm; + + /* Shared between C/D streams */ + void *dict; + size_t dict_sz; +}; + static int lz4_init_config(struct zcomp_config *config) { return 0; @@ -13,22 +24,65 @@ static void lz4_release_config(struct zcomp_config *config) { } -static void *lz4_create(struct zcomp_config *config) +static void lz4_destroy(void *ctx) { - return vmalloc(LZ4_MEM_COMPRESS); + struct lz4_ctx *zctx = ctx; + + kfree(zctx->dstrm); + kfree(zctx->cstrm); + vfree(zctx->mem); + kfree(zctx); } -static void lz4_destroy(void *ctx) +static void *lz4_create(struct zcomp_config *config) { - vfree(ctx); + struct lz4_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + if (!config->dict) { + ctx->mem = vmalloc(LZ4_MEM_COMPRESS); + if (!ctx->mem) + goto error; + } else { + ctx->dstrm = kzalloc(sizeof(*ctx->dstrm), GFP_KERNEL); + if (!ctx->dstrm) + goto error; + + ctx->cstrm = kzalloc(sizeof(*ctx->cstrm), GFP_KERNEL); + if (!ctx->cstrm) + goto error; + + ctx->dict = config->dict; + ctx->dict_sz = config->dict_sz; + } + + return ctx; +error: + lz4_destroy(ctx); + return NULL; } static int lz4_compress(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len) { + struct lz4_ctx *zctx = ctx; int ret; - ret = LZ4_compress_default(src, dst, PAGE_SIZE, *dst_len, ctx); + if (!zctx->cstrm) { + ret = LZ4_compress_default(src, dst, PAGE_SIZE, *dst_len, + zctx->mem); + } else { + /* Cstrm needs to be reset */ + ret = LZ4_loadDict(zctx->cstrm, zctx->dict, zctx->dict_sz); + if (ret != zctx->dict_sz) + return -EINVAL; + ret = LZ4_compress_fast_continue(zctx->cstrm, src, dst, + PAGE_SIZE, *dst_len, + LZ4_ACCELERATION_DEFAULT); + } if (!ret) return -EINVAL; *dst_len = ret; @@ -38,10 +92,21 @@ static int lz4_compress(void *ctx, const unsigned char *src, static int lz4_decompress(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst) { + struct lz4_ctx *zctx = ctx; int dst_len = PAGE_SIZE; int ret; - ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (!zctx->dstrm) { + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + } else { + /* Dstrm needs to be reset */ + ret = LZ4_setStreamDecode(zctx->dstrm, zctx->dict, + zctx->dict_sz); + if (!ret) + return -EINVAL; + ret = LZ4_decompress_safe_continue(zctx->dstrm, src, dst, + src_len, dst_len); + } if (ret < 0) return -EINVAL; return 0; From patchwork Wed May 8 07:42:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658192 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED2817EF04 for ; Wed, 8 May 2024 07:43:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154203; cv=none; b=b3q+GVeLkcYP/sZ218yUMjnPDBIj0qkF06apTmVYCYp19z+MqDjy27rtffrZ8I38Ys5PPsUwIK9U1o3TRECDLjcnkCpvUK1dUBuxekD0n/X4nAfpKZVjF8n+VhOmNxetdlUGBrhf0m7SjYslVJ1e63Lxwuj126SDeBQjDy9rkDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154203; c=relaxed/simple; bh=OdhpOTFPCVgN2eccjlEO6kqeLVB903rqR/qnsmX6Lno=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LyEoOuCs8yVzX7CRi+k2u6KpFbX8UeFleFN2Wi1xFmhyzlHPNh9Vs8GlzaAu0xipIrXUPA6RBcnmIyF/Hoaw09bc6Radd+OW/gpoYVQ5N2yYtiMAZOgLaslmhvDmTVbdaEQ17TUWdgI8xTbXrr/tfv7t89O+gQaSGxPstGeiIFk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=ZVQ50cfY; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ZVQ50cfY" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5f80aa2d4a3so3010227a12.0 for ; Wed, 08 May 2024 00:43:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154201; x=1715759001; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9QxTKxxqM9CCOnK78l26rfPlNmUT2j4/F00viIdsP1A=; b=ZVQ50cfYfOoOS1C06wSLvbD8nXRkfN6+5b/J3wznnKRRPcaFs9JMLJJ1IiG3pFMh/1 8r+wMHNvYF+MyeuElNBzYEdXoRezPpTw1c03b6CCVRolr6JE/1W1/SM8BdcNublhB6h4 +KvkFUo1mMINPGHBzqa3UHIoqHSC/4dUP7NEs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154201; x=1715759001; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9QxTKxxqM9CCOnK78l26rfPlNmUT2j4/F00viIdsP1A=; b=fwG2arBu1zzURPPH/xQE4ocgPvfJAnj4oXOAtAt8250okzO6ocQnJFo8IBBXjyRKnn xznNk8octOppcinWHYkvy6ppO96+Of3ru5WlZAb39dB1u4M1qyWKI2GfLgM8ya7tbjFc X3SGspD+Dr4O3bcKweZRULeqHsbJCde/bWXTWzd2Uh1t6lqq3K0wHUq56+VpBYcYpbaX 172JMrYkGk07Vu67RJyJ3PBd7trzp5d2JIUgDDrnR+CQsrjZINtH18SgfpJKci2w4/2r 7qMar29fWPdUW+w0pzIAwTNDjLIrwwLhenBWslMryidpTWcLbcHvjbTWqFKkdKS0nwoE 0IAA== X-Forwarded-Encrypted: i=1; AJvYcCXWoDbMU0LBZ61JK91IYsxiT6IVY8sTbBWa2EevHBExUqkJWKJQtRL4zX5MGFIlR4SklzcSHe9ozCGVZl+OQwlPIYOxw9jf7opBWTM= X-Gm-Message-State: AOJu0YxIgIGnrKlD0pFoLmy+qgDOkpXKA3Vq2CTU9YyfOEEB36tewtfH O3h/185of8vvDJJediAS4TZeeqf0GsjVoAFjCYXd7i5Rxxw0gMIytIyo+gYJqkGg2WscOXL0jaY = X-Google-Smtp-Source: AGHT+IEzdJrzSXLzatIiUMsCPntn78tgD8O/GIVzTJ0AlqSOg942tNgKxWsOC7FgEZxsD9MYoNfjYQ== X-Received: by 2002:a17:90a:f014:b0:2ae:6e16:da91 with SMTP id 98e67ed59e1d1-2b6165c1513mr1825272a91.29.1715154201328; Wed, 08 May 2024 00:43:21 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:21 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 18/19] zram: add dictionary support to lz4hc Date: Wed, 8 May 2024 16:42:11 +0900 Message-ID: <20240508074223.652784-19-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Support pre-trained dictionary param. Just like lz4, lz4hc doesn't mandate specific format of the dictionary and zstd --train can be used to train a dictionary for lz4, according to [1]. TEST ==== - default lz4hc /sys/block/zram0/mm_stat 1750315008 609010918 621006848 0 621006848 2 0 34288 34288 - lz4hc dict=/etc/dictionary /sys/block/zram0/mm_stat 1750319104 499629401 509485056 0 509485056 1 0 34288 34288 [1] https://github.com/lz4/lz4/issues/557 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_lz4hc.c | 58 ++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 431a44f0fcfd..c928f94f30df 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -8,6 +8,12 @@ struct lz4hc_ctx { void *mem; s32 level; + LZ4_streamDecode_t *dstrm; + LZ4_streamHC_t *cstrm; + + /* Shared between C/D streams */ + void *dict; + size_t dict_sz; }; static int lz4hc_init_config(struct zcomp_config *config) @@ -26,6 +32,8 @@ static void lz4hc_destroy(void *ctx) { struct lz4hc_ctx *zctx = ctx; + kfree(zctx->dstrm); + kfree(zctx->cstrm); vfree(zctx->mem); kfree(zctx); } @@ -39,12 +47,27 @@ static void *lz4hc_create(struct zcomp_config *config) return NULL; ctx->level = config->level; - ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); - if (!ctx->mem) { - lz4hc_destroy(ctx); - return NULL; + if (!config->dict) { + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx->mem) + goto error; + } else { + ctx->dstrm = kzalloc(sizeof(*ctx->dstrm), GFP_KERNEL); + if (!ctx->dstrm) + goto error; + + ctx->cstrm = kzalloc(sizeof(*ctx->cstrm), GFP_KERNEL); + if (!ctx->cstrm) + goto error; + + ctx->dict = config->dict; + ctx->dict_sz = config->dict_sz; } + return ctx; +error: + lz4hc_destroy(ctx); + return NULL; } static int lz4hc_compress(void *ctx, const unsigned char *src, @@ -53,8 +76,18 @@ static int lz4hc_compress(void *ctx, const unsigned char *src, struct lz4hc_ctx *zctx = ctx; int ret; - ret = LZ4_compress_HC(src, dst, PAGE_SIZE, *dst_len, - zctx->level, zctx->mem); + if (!zctx->cstrm) { + ret = LZ4_compress_HC(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + } else { + /* Cstrm needs to be reset */ + LZ4_resetStreamHC(zctx->cstrm, zctx->level); + ret = LZ4_loadDictHC(zctx->cstrm, zctx->dict, zctx->dict_sz); + if (ret != zctx->dict_sz) + return -EINVAL; + ret = LZ4_compress_HC_continue(zctx->cstrm, src, dst, + PAGE_SIZE, *dst_len); + } if (!ret) return -EINVAL; *dst_len = ret; @@ -64,10 +97,21 @@ static int lz4hc_compress(void *ctx, const unsigned char *src, static int lz4hc_decompress(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst) { + struct lz4hc_ctx *zctx = ctx; int dst_len = PAGE_SIZE; int ret; - ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (!zctx->dstrm) { + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + } else { + /* Dstrm needs to be reset */ + ret = LZ4_setStreamDecode(zctx->dstrm, zctx->dict, + zctx->dict_sz); + if (!ret) + return -EINVAL; + ret = LZ4_decompress_safe_continue(zctx->dstrm, src, dst, + src_len, dst_len); + } if (ret < 0) return -EINVAL; return 0; From patchwork Wed May 8 07:42:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13658193 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6D237F7CC for ; Wed, 8 May 2024 07:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154207; cv=none; b=a8iPzIA+jRhPwXZUe3dSEAwQwndqICWe5kSJ9J6vXSU4sCtl03RtevulX3g2Mk27qnotfHddRIWXDVRe/o1notKqQd/IGB88sl9HGdPOHQMPrC7W+RQvXcRyoUnJK7M3TMyl0yK701KPHUWD5yP3+uBJNkgeFQn8/9UapMGGAmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715154207; c=relaxed/simple; bh=0QKD/VRLqE4UJhTMThwx4W0GPaxzgatlvvK+3EE9Gd0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BSfPD5NTAKyeCcKD8ubXUL+JudfjopAiunfn2QEmeaThn18o8l1zzGvVWGkQoJuzJ2tEUqLCpP+PD+zNB3PoaaT7vhtSGyqL4HtUE2rOymIYeVVs73+4yDew0PYe12evhu3HHd41gRT6a4hoa3K/UiZpC/CCEtFEJ99U2Eh6jfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=UGL7O0Si; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="UGL7O0Si" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2b4952a1aecso2722587a91.3 for ; Wed, 08 May 2024 00:43:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715154205; x=1715759005; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ow2rUHFnrcYhlTotFbBj4Gwxyq4hSnuB9nn9e+mMxIY=; b=UGL7O0SiV6EoBU3l0lP0gPA1waybDambjYpwsSAgQUqgoCrPnL+Ua8eE+bWKvouG31 jUfZDMHyVx1ImFpR4VjxdHwJ5Ix+Y2rebf/zuV8BTXY5pdk7JPvmq5SPjZ5CyaZXMjDO 5P76Sb+vvENpJ4XFgtgxGNZl9S5SDghCRGGKQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715154205; x=1715759005; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ow2rUHFnrcYhlTotFbBj4Gwxyq4hSnuB9nn9e+mMxIY=; b=eXBv8HsdfWmkUxjbBkUbq78gEv/As3Pczoc7Zse2J5ZxMSOqitJkRFAHqSRW3ZWtgg AiFJ+QXRoWKDUoF47QY8j+VvXYpUrVra81tVGEbMVV9XSDhUfJ+EQyiW6b93LK3oh0Ib jcQUwQejoDExd+sGJviPUCWJyF84O3tPuU2hWR3ZbFCGhV8cJjixgHnKP0DToOFTMUrt gc4bXAbblrn/j63k8fAVRXy/BNgrrtDwX2QHRr5kGpPjy8Ixy+bOv4+h6mludK3aZ/AC 3tbYBixQ3pfx26/bkogINOsmfSxSA++uR06swwV/2XWUmoaO2413kWbdDd9M9aS827v3 W5jw== X-Forwarded-Encrypted: i=1; AJvYcCV3bMORLi4VZ1za9VwEO9q3dr1riUzyc39lzjLfyTvr27CJWNASAgQess2flPttxPO82pACaIe4XgjylVEugLehvYAr0jeLCrAuSd4= X-Gm-Message-State: AOJu0Yzh57caipaXbieAMHWfW1MAX0lb0arzxCepkvrhzn34jqYQPmje ucEaJxU+tHub0x0zUjLCqvwQwZJlpZPnh+QhtTjJwAqM6EqZA5uPMNpOTcswcg== X-Google-Smtp-Source: AGHT+IE1GlyzbHlgtHh03vLzDAXZBLlDXZN7nerQilU82pSz8k6bsqpHgAvqZzQ1IEihysz0KtfQbA== X-Received: by 2002:a17:90a:440f:b0:2ae:e1e0:3d8f with SMTP id 98e67ed59e1d1-2b6163a22cdmr1679910a91.2.1715154205219; Wed, 08 May 2024 00:43:25 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:ad4d:5f6c:6699:2da4]) by smtp.gmail.com with ESMTPSA id l5-20020a17090aec0500b002b328adaa40sm780011pjy.17.2024.05.08.00.43.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 00:43:24 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 19/19] Documentation/zram: add documentation for algorithm parameters Date: Wed, 8 May 2024 16:42:12 +0900 Message-ID: <20240508074223.652784-20-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240508074223.652784-1-senozhatsky@chromium.org> References: <20240508074223.652784-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Document brief description of compression algorithms' parameters: compression level and pre-trained dictionary. Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 38 ++++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst index 091e8bb38887..58d79f9099e3 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -102,15 +102,26 @@ Examples:: #select lzo compression algorithm echo lzo > /sys/block/zram0/comp_algorithm -For the time being, the `comp_algorithm` content does not necessarily -show every compression algorithm supported by the kernel. We keep this -list primarily to simplify device configuration and one can configure -a new device with a compression algorithm that is not listed in -`comp_algorithm`. The thing is that, internally, ZRAM uses Crypto API -and, if some of the algorithms were built as modules, it's impossible -to list all of them using, for instance, /proc/crypto or any other -method. This, however, has an advantage of permitting the usage of -custom crypto compression modules (implementing S/W or H/W compression). +For the time being, the `comp_algorithm` content shows only compression +algorithms that are supported by zram. + +It is also possible to pass algorithm specific configuration parameters:: + + #set compression level to 8 + echo "zstd level=8" > /sys/block/zram0/comp_algorithm + +Note that `comp_algorithm` also supports `algo=name` format:: + + #set compression level to 8 + echo "algo=zstd level=8" > /sys/block/zram0/comp_algorithm + +Certain compression algorithms support pre-trained dictionaries, which +significantly change algorithms' characteristics. In order to configure +compression algorithm to use external pre-trained dictionary, pass full +path to the dictionary along with other parameters:: + + #pass path to pre-trained dictionary + echo "algo=zstd dict=/etc/dictioary" > /sys/block/zram0/comp_algorithm 4) Set Disksize =============== @@ -442,6 +453,15 @@ configuration::: #select deflate recompression algorithm, priority 2 echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm +The `recomp_algorithm` also supports algorithm configuration parameters, e.g. +compression level and pre-trained dircionary:: + + #pass compression level + echo "algo=zstd level=8" > /sys/block/zramX/recomp_algorithm + + #pass path to pre-trained dictionary + echo "algo=zstd dict=/etc/dictioary" > /sys/block/zramX/recomp_algorithm + Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress, which controls recompression.