From patchwork Wed May 15 07:12:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664746 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.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 A769D43AD2 for ; Wed, 15 May 2024 07:16:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757421; cv=none; b=ubHYijljRLSvvq5I0t105c3r//9tFdgOn8/uTGOZhCUWn+hWrNRKTtO10RbfBCohH2NtKsGavxwfF2A6O4iYn/vyq4xBefwgtsenXQfCy4Ol49Mrg633ByrOlpeZeATqXZenRP4i1zMDVvWu9casqmO+E3Jsc9DGpYtzBA38b60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757421; c=relaxed/simple; bh=544LLbSp8/PVrYB0QGq7rRmx9sLqiZpPOCFAPpIoqy0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oQoyC1htOfrYJHjyABjowvB61sm1Cu4xaaKX2EwWx/MfJuPj1cOONY64c342bzqgMyYkXofsqDUsFC9zigU9INH/CM/VTMxcPxdJM5JO3WTLD5D1+/38kbRGyH2IRu3npUJvIcGCoE9L+umbUHY9ZbG5VkwDztMsJ4tRlb8yk2o= 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=GsHICxUG; arc=none smtp.client-ip=209.85.214.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="GsHICxUG" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1ecc23e6c9dso43072425ad.2 for ; Wed, 15 May 2024 00:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757419; x=1716362219; 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=fsLG0zPIDV95vZmzBVa/SvUT8ZYjpbB7N30gWJFx0mg=; b=GsHICxUGAfpKfZMBu3GPpDoVfAE4zE4XWuifCtCGJun31Akyh9KG0wrV84E1mqL8wZ MCN5glkCkvxQ67Aon6C1zHdvzZMkPylWSTD1K0idpAT17LbYaowBsDFEhcBtFTmFxCq2 XdYBddbbHGVn8IFThXJhT4ti+eQusRxyuL7mU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757419; x=1716362219; 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=fsLG0zPIDV95vZmzBVa/SvUT8ZYjpbB7N30gWJFx0mg=; b=P8LvCR6FBJdHiQknW4KndnjpoERqY/K4d0S1aTkXdKbQ8x3Cj03cZlIjcoPHGTIHej DcAMMvqaSCQ8HsEfe+ud635xPYb1dSlQDf/AFYFgBP1bNubKsQsvtdoPN8jQGwaBzcUk VWHXZR/2BhgoxOXkxSA0jRme+YgqPSAnVI+L1yXacOtKbXYWj3X7qZNuHkB6yqHy0Dpx kJIxSAb35uMvcZpXb9B64DSscqX0t+q8xz2+kxCibWrA1zK37pVRrr7Yrpm2Qk2GRr9w GDfE+G7068FYkP1MKPfD+sisc3jlV0aPZSRbnzq6zyFKOHQHHQpmB+TA+PxwFN2FnOAT 6BRA== X-Forwarded-Encrypted: i=1; AJvYcCV6fxyDjbjSHlvCLcDtGQ7pQGP6IYuXkECdyEWkoStRP+CFJzSKTGQ2v3vsUvhYzvrzqavnWJl0zD5fjIZneiyk+ZqmLm5s6XP7Veg= X-Gm-Message-State: AOJu0Ywd7kVkBSzJiDqx0BMiCTMBPhbQDMgwrQwo6rdADFHTX2ehXcPm uTpYDWdoqkJvTXszmaYxZAPho1JIvXf4drXmAqxjSbq6FWrUDORnjMi3JwScIQ== X-Google-Smtp-Source: AGHT+IE4lcnvFwAZn4jYeIZdBmKYEjCZJtmfcPgBKIRB2MhlQ4QeIrzh5k67ZucVVYTYmBniCUBgIg== X-Received: by 2002:a17:902:cf07:b0:1ea:ca03:5d8e with SMTP id d9443c01a7336-1ef4404fbecmr181642705ad.44.1715757418737; Wed, 15 May 2024 00:16:58 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:16:58 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 01/21] zram: move from crypto API to custom comp backends API Date: Wed, 15 May 2024 16:12:38 +0900 Message-ID: <20240515071645.1788128-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 3acd7006ad2c..38afc2c23108 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1327,7 +1327,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]); } @@ -1414,7 +1415,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)) { @@ -1601,7 +1603,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 15 07:12:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664747 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 1B81C46424 for ; Wed, 15 May 2024 07:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757423; cv=none; b=NpZotvW6gu3C2hETQYklJLxM0FjbT5/0bBvWQrlwcxMagcK0K7DGqR0yftzyxs4uTcRVbIPpG+cYOh3smSYqpYX2qKIVB9uPeDnIda/tEdLip+8OSSSh5cRgy1D2HOuuu6dCOwmx7soPP0g+WURtcdPmNLnhSITgHXIl/oRDmdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757423; c=relaxed/simple; bh=SQspApLzR6A+qI3ABji2AwHeyysKKTXjPbdBT7c8Ig8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t5ak9Bxeawj8YY1NxHpcKMKNw4BqCk/21JvRDAu3fQYmw8pjlZFKDIddS84ZIYZfGUzudAEylmZbtfGgXpl2NnZf6jg9mffT9KvNTqY+z5i81DZpAaVkKqUQKcnnDNyWQ+EuMEwfjjUdaHSQOo56LqhC80H3awxOoCRXnQ9pBVE= 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=aGT3GKz7; arc=none smtp.client-ip=209.85.214.181 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="aGT3GKz7" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1edc696df2bso56594915ad.0 for ; Wed, 15 May 2024 00:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757421; x=1716362221; 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=zHiH39OlVbbyYOoV0PVG81/IjMwGnGQnqALJjuzf7+o=; b=aGT3GKz7+tMGUzCxJW/Ezcs3m9E++MTurLqHSz5AOQOrIUipU1671dzGqM1nXcP61w 6r1+TqTp/+GmxYqAV4tmyt1zosJBeNmibXN3l1+Ri7umUkgsXjwM7Rls5MM9ChDSnjVF Wle+YjbC54Kdqd31XWNU4A0v6nDcS1SmHPGeo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757421; x=1716362221; 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=zHiH39OlVbbyYOoV0PVG81/IjMwGnGQnqALJjuzf7+o=; b=pchKL0bpuINbPPEzxseHImzAWOxfDCgCdbNA3Orjlsl69tMQ+R+fLiPwlE6LvDvNlr bsP8pkDINWidKb3UrHndejcn4xNERomV0YDt0Ha/OK96DnpfsUKiIQqXDGs6sYUFc90B 0GXESOyLSqgAp+DerxlrVxJRJ7L8KBXmrkHXaoJfEAjUDc02bsDpI15buj7mWw33gaBU e2BFiBFG6q+QzR5BQGOmSmQ8UstDbZvQr3p1caJpp/KKDuv4ZGvO3qEvi1Z+v081RpcS YsmEjQAfrMDcpfNnEkkeoc04AMnOxf7/6+HdZUIDu+EqNSlGcotCWOZMeTLrSSflWDc9 Xe5A== X-Forwarded-Encrypted: i=1; AJvYcCWfQYlzNTb65ChhPyNWiveUz6YE88prsStPmhyxQZa5hDTW3ymqipIMWZKA7Lh6HtiSIqhdAsd3cKPpDV3oOCSJPeya7/+gBYsGzVM= X-Gm-Message-State: AOJu0YyAxyJClhafD1ar57EFyHXC5CmEyLAy5tinv534yGJbs26pr13K AZpOdOaYWDb7yI9OOGUXDagDH2RwvKns+ujZ0Cbe8XP3EY9r+AHVfnApt1bmzXZhqlkZIp/+e+I = X-Google-Smtp-Source: AGHT+IFL9UyLu26jE4g+X6OYAAQOaCxw87D8rJNv/C2yKEFY7GXkl4F5s/LdrGfAqTySIEcbf+3Imw== X-Received: by 2002:a17:902:7b87:b0:1ea:9585:a1d7 with SMTP id d9443c01a7336-1ef43e293d9mr148335675ad.37.1715757421264; Wed, 15 May 2024 00:17:01 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.16.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:00 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 02/21] zram: add lzo and lzorle compression backends support Date: Wed, 15 May 2024 16:12:39 +0900 Message-ID: <20240515071645.1788128-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w lzo/lzorle compression support. 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..2a3db3368af9 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + zram-y := zcomp.o zram_drv.o +zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.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 15 07:12:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664748 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 971E648CCD for ; Wed, 15 May 2024 07:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757426; cv=none; b=KbQjDigDrOLwgqNSS1Ua/MuGn3Ea7ZWs+bPnJMp24QN5JMPgYXQF4+pxBqB+ALXn4HXLfTAiirJ0bvAVYnmuiLyj7E0wLbiJIQf6t2TVwmWRuYeiodgFFP2FrzjB8b4PAJiRCxJ9n+xJET4Dt/6tMcmRmR0ld6cIBQouBJO/KTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757426; c=relaxed/simple; bh=YnePQvm5yTrRFyDy+nrRta3ObTgqKOo4iqn1FqtURHk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bM6cak/y9wBS4JhB6vbXh+Jyrpnpx7n6zTwtApJlo49GTuCJ1oB/itllgLGBcbjCCzEjRgujY0haWURvpvPOcoc5zKu2QRhUeW23oZF51hmCj5AO0UqkrdlEP0/4OfOeA65a3iVTUptayiNbjjBqL8+Q7a6H89fYZUg9C7M9lf4= 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=Vw+WBCOZ; arc=none smtp.client-ip=209.85.214.169 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="Vw+WBCOZ" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1ec4dc64c6cso46473675ad.0 for ; Wed, 15 May 2024 00:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757424; x=1716362224; 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=5y1JEx/byw+/lYPDinjRUJ1Lo0uZERSy7KI3xtlcbP0=; b=Vw+WBCOZaKo9SWdEdeLIPfENpcr8UAHjh3BRSzFyrj9WX2igxbRzDaDPXR/1kFESa4 ZKQ7sdIlW3SMwiPk8Tlz94WtGScKDAP7jWLTxCY59b1TySsQhU0ML7ZmZcTy+fOlXHK7 T34iemPWtcpVZxQWJemp/wtuUJR6DPepUVw/E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757424; x=1716362224; 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=5y1JEx/byw+/lYPDinjRUJ1Lo0uZERSy7KI3xtlcbP0=; b=OAQV47+mT/uysohgJY6JMhsgKoJrxnh6jNyO+VLEw7eNeVRoOsiBP4tfwkYmcfkdJF GT6JIVQjFKtUyNvJwcV0eeyNAVik5yoiS9g7+CTWtzGgbellm4OWriYhsTy3Bi6JtOCo 7ak4lF/uncgI9ssdlEShseVqWeu8e9VSfmo5x8D7Q8+MRqdYGRQv1kXi0dmK7Yndy/Ch 1Rakr6sjeBuSkeb64wLQIUTKo9ZZgAJE5wFuQoZEYjNUtflPqxco54OFR9fIUPzpwfCi k4UELcIgzfRqqotjZw5UxGx2miDamKiJNyCBwqyeVnp9upajSnO67YH0XFaFulmVbN+4 LVjA== X-Forwarded-Encrypted: i=1; AJvYcCUDNYRS1Stew4p3ULyQu2+j6uZPt4B7wVG+L/1VKJlUFDgxTeXYfSDswXLtn/kLPg0Ra6mWGqhaH0cWWSlIxog/YbuPcFCnNyI2cTA= X-Gm-Message-State: AOJu0YzyuFrjTC/1SAIzCVWDWiYR3LL4lKHjjV7RppGuPwi3ZXaIxaRu Ak35JMeZL5a0/X/e2jyJO08MFojfzKMrAOzJ3WCG/wLfRfMzfAsknFIDCXLl8g== X-Google-Smtp-Source: AGHT+IFpWjl6eRWiof5dtv+ykNbLzv4SU4wXOuUgvzR9ccRXR+//FCr7TGf87zxYfy5eESFA9z9pJw== X-Received: by 2002:a17:902:b111:b0:1eb:d914:64e4 with SMTP id d9443c01a7336-1ef43e25f72mr142390235ad.32.1715757423975; Wed, 15 May 2024 00:17:03 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:03 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 03/21] zram: add lz4 compression backend support Date: Wed, 15 May 2024 16:12:40 +0900 Message-ID: <20240515071645.1788128-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w lz4 compression support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_lz4.c | 73 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 99 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 2a3db3368af9..567f4434aee8 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -3,5 +3,6 @@ zram-y := zcomp.o zram_drv.o zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c new file mode 100644 index 000000000000..6ea67511d782 --- /dev/null +++ b/drivers/block/zram/backend_lz4.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "backend_lz4.h" + +struct lz4_ctx { + void *mem; + s32 level; +}; + +static void lz4_destroy(void *ctx) +{ + struct lz4_ctx *zctx = ctx; + + vfree(zctx->mem); + kfree(zctx); +} + +static void *lz4_create(void) +{ + struct lz4_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + /* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */ + ctx->level = LZ4_ACCELERATION_DEFAULT; + ctx->mem = vmalloc(LZ4_MEM_COMPRESS); + if (!ctx->mem) + goto error; + + 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_fast(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + 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 15 07:12:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664749 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 0C1414EB20 for ; Wed, 15 May 2024 07:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757428; cv=none; b=O7TXD//KD2d/ovb5STPp5SA+dvndTLxv3ajBdsbSygdlm/2C9Sowul0i6xCHExSltFu1QnW/ENQZAleRzGmwpoJCx9YJu9YfH8CFprJbkl2lNRgJMeKF0U59meg5JiLY+Ca3S5jyC4jYpRj315uT69SSDNkQMPhZ4mnUwwLUPpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757428; c=relaxed/simple; bh=QIqKpR6W+iYpBKRdE8LyZOci6XU5F9QsGlMydzcKw90=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GOjIpW6KDxiNu+49WFKyBxuZkDJieOmI5LFZJNpDJSXbLwNB47TGJ5BhE0U8fbF9Eq/ri0/+eQcfZri5m3ElzfEt5CdnFNbrEslBtX5UhdGWB7OLHlaZyLAE0eRN0J01rO/mb6HkWRIQUp+/2a3Z9yu33cS+AAr46K97AMmadmQ= 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=TqKtLYPJ; arc=none smtp.client-ip=209.85.214.170 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="TqKtLYPJ" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1e651a9f3ffso37226155ad.1 for ; Wed, 15 May 2024 00:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757426; x=1716362226; 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=i/3U4lC6hcVdfnHTL8Mc0HJ1E1QNWG67FTnBC8OLuFc=; b=TqKtLYPJJzfGx3mpymohFTwUklUkkKIHSdVKWhJAsSz8aW4dOO+OaKam8x6KfC889x NLSXtm+OXQDkNaOmG0+PcXXlX6ca+RvBY7ej8nMVcNddFoGd/CNY5Tl1TQGv8BlWn/RN 1btDHINGL6t0k0+NDlPVD0D80Zvv9iIzl6og0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757426; x=1716362226; 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=i/3U4lC6hcVdfnHTL8Mc0HJ1E1QNWG67FTnBC8OLuFc=; b=mUAcCHhqcvjyImgxHnfUWkPquJ9gRnfz8sYJhrYN6NIhUWTiFM2+72Gm8UM6jCZyf4 2NRC76A3btViaeRVexIE76uNhc09pC9aSVQVI4mnekRTbfyZmK1NLEee1UL1dxhNxUig +aZy+T076HrkxLdy7QtV1nqUkz5rDSnQpxw1zyXe7iWz+avqLgI8j1scLGIbbYLVccTa S7XLhl3jFY3HYqxgRkUDCjepwAMNB+azAssbS0ZLc+6WgyKQlqW4tswY2W4LVqAkJy5K /IF4X/ZofKa0nGX2svvbjCbLGyPgwenHno/ct6PHf7qlg56L2X87oZTUmR2TxXeOaRqL ++6w== X-Forwarded-Encrypted: i=1; AJvYcCUg37cAW21ML5DWx7vCweRCJHZVn95OqJvzrbyWuAP5W+m85vude+G2L7qteHEqtfvknnPn0ywvW6pOfHNivnoNqwy+0YtXYR8IV4k= X-Gm-Message-State: AOJu0YwUr8rvwmPOulmanf8EjCPmkrPXJRFOSH/gyIy8qGASmaZJd2RS Eyz1om6gbwDETJ8QaBxzFivJyzJJco9cC4K7zlk+BYb3gDM0RQ1adicc/wvE3MsXVk41/D7/vp8 = X-Google-Smtp-Source: AGHT+IFhFHm0P8O5jX9tenmwIWFZOdHgBJqiZN6RIu4mGkRSmngvUyxYc09i0GDOD/kiHYBPTyaQyA== X-Received: by 2002:a17:902:ec89:b0:1eb:58d2:8739 with SMTP id d9443c01a7336-1ef43d0a009mr183267855ad.3.1715757426418; Wed, 15 May 2024 00:17:06 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:06 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 04/21] zram: add lz4hc compression backend support Date: Wed, 15 May 2024 16:12:41 +0900 Message-ID: <20240515071645.1788128-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w lz4hc compression support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 5 +- drivers/block/zram/backend_lz4hc.c | 73 ++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4hc.h | 10 ++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 101 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 567f4434aee8..727c9d68e3e3 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -2,7 +2,8 @@ zram-y := zcomp.o zram_drv.o -zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c new file mode 100644 index 000000000000..9bcc5aa19a2b --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.c @@ -0,0 +1,73 @@ +#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; + + /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ + ctx->level = LZ4HC_DEFAULT_CLEVEL; + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx->mem) + goto error; + + return ctx; +error: + lz4hc_destroy(ctx); + return NULL; +} + +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 15 07:12:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664750 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.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 823E4502A1 for ; Wed, 15 May 2024 07:17:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757431; cv=none; b=n+gLixitmHTBhUSCfiHZpW5VOghUkm2LLbLaFoQKbzTBGm3Uk/j6VX1FRn8X2clnY3qGq3hdYjDj8d3TOjMRCc2SUeHLyZvrBIeAnwflwsb3zGQRDodP2Jyx0kCrVgSzODyPzp81fhbCDyajYYlWRqNXmsR9m23pXv1R3vBIXXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757431; c=relaxed/simple; bh=qp4pzB90m9g55C+fAL/JlDtKkb1iY2MFD8dmhT5+bXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mSnscgW8jhdIt6pynR9GrCIfR7uMNQlTA14Oy45Fbd2bsy2W5pMNZAlWgvb3Umgy05Ugj8BhJOp6OUey24N75yos1ifFRZHGmGkYmE4XDpYYYi+5/f/ZBkdjzmMgC4gNrbRVLqSUer3gI4qhRSDxzRA91Q1jdq1i7AdkxBeMVCk= 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=So7SAHfS; arc=none smtp.client-ip=209.85.214.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="So7SAHfS" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1ed41eb3382so47130625ad.0 for ; Wed, 15 May 2024 00:17:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757429; x=1716362229; 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=J/F4eHWC20T9jERmFYhVQI8aXCSk3r/5sAst+t3cPEA=; b=So7SAHfSG1tRJ+ZYNXTpChZVTSHS1gZDbC3+LlPMQowXPa+fKV+UZ135EB9+edEZgO CjNV2ZbhlxhReSr7BYkRM5wXAXbvTGV/VsWOyWctenZyWaBBQknbZFmQi1wo6He5vkTx vXOyJWOf+UU+GvLbFxaU6mfmr0BKCYtNO2GnE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757429; x=1716362229; 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=J/F4eHWC20T9jERmFYhVQI8aXCSk3r/5sAst+t3cPEA=; b=TmljKX5T/etSqsohXC/1f8sgxMY1WZ1LRBuwk+S7QEJIb45yvO+iRYrTU9Yjix+y3V ugVU5NcBQj4w7dw0MQVp4tKFRMDCrw+PhkFcHteh7lbu7KAHz5CDmUDsRjCKpXsf9JIX TKTK0kMo2s6Hq9fjlOOv+4QWPWHh+SuDeDUhY6JeW9F1tLbLwYaD7cGhTxe/MFG9VArP YIMc4Z0a3+jK7tk+XF6Rrqa6wiU3E2vJPsrXR6FBa3EW1Z+D0F2M4wCiHh8XWkf8q5DB 3vLwHiAqRanCEOihD8BIUSYJ5ihzgA/atm9w5DEndvX2yMSJWwApPJ5CfHmkXPFqCZWD q0Ow== X-Forwarded-Encrypted: i=1; AJvYcCWV/gOdj+lMNjhkP04ivJn2/QklSLP42fmx4GCJqL3yGpdXjzy5/l2qGPBXJp+hcJgbY8Ha9fLzdu5aI97f1h5zHBYERm9cxWtXOIc= X-Gm-Message-State: AOJu0YwFGYV6RRe6VxioSRAW9QWnwuGYz9LaROjwYt3m82bK+DldJYXp 0G4cod7lQjiO46YCcCnqa2bj/16ZrWC6pcoiCZ5DhSGZ2xxn8/QBu2Hn79uMmQ== X-Google-Smtp-Source: AGHT+IFR8beUNV/civmOKa/JzrQT21TYGy2xDo+9rfsbtHXdFVJDdpYHoSOshN3+CoSvN5XhewZdRA== X-Received: by 2002:a17:902:d64c:b0:1e3:e1d5:c680 with SMTP id d9443c01a7336-1ef4404e097mr174785415ad.63.1715757429001; Wed, 15 May 2024 00:17:09 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:08 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 05/21] zram: add zstd compression backend support Date: Wed, 15 May 2024 16:12:42 +0900 Message-ID: <20240515071645.1788128-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w zstd compression. 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 727c9d68e3e3..a2ca227e199c 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -5,5 +5,6 @@ zram-y := zcomp.o zram_drv.o zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o +zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c new file mode 100644 index 000000000000..bfe836844232 --- /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_default_clevel(); + 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 15 07:12:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664751 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 0742D56B79 for ; Wed, 15 May 2024 07:17:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757433; cv=none; b=ddnO6sJwZWdlVBMXJbZMXNEQnF4ENkiZkY09mP2Zza7myt5w60whZo9VUu9KuMHZggDs9bZ4IyxQPmTHULKIxMcOdg7xedMN0ssazGfBLwTLRvocDF2vuQPkiefxvSRv7/8SuRBzfk05Uyc9t09dqpMeUvmqrlDHMa+wPriao74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757433; c=relaxed/simple; bh=NfNIjYe0R8m8g+PZ0SlYD1aU6GoRy1xQlpR4Iu21ap4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XBFOjIOgPSkes+a5gROXPkEd7mi+7xJaZ/vaKfg98e2jb2KwhKBuXRMZfGFOgqJdLPfOmQ5i/gC6NcIIw8z/kUJ0dO/C5JHeREwga20yxO9PHG6K+ERKqBdcZnS04Us+qz3GQoU50/VVa7MxKtperBUWSmmamdaZ3cjoJh98Pso= 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=CN5XvkMc; arc=none smtp.client-ip=209.85.214.170 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="CN5XvkMc" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1f0537e39b3so37033765ad.3 for ; Wed, 15 May 2024 00:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757431; x=1716362231; 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=CKysrwqZlMwVUU8fiuTWrWkRyKYxV6Zxfm3TNnO+6jg=; b=CN5XvkMcvsVZxxf/c50nuKiByew5wDs3YGQ6sG59SAfQ+WqRBaSSgVOkXyaA1t7TED rrvS5DwDzzubQ6VDyEvHG36glPUI//4McE/8PspWacnWMiQgSW2ZuEgj/O6CKZW+bg6/ WE64SRs9ojutSPlsnVY5+y63jm0r3KUASLlXg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757431; x=1716362231; 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=CKysrwqZlMwVUU8fiuTWrWkRyKYxV6Zxfm3TNnO+6jg=; b=Ac520WSDr4mjn/Tg7m7Yu/v8PPwfY09fse7xjjhaSHMv+a0DxrkA2XIAD198f7P4JF jG25monYNrAHXazsaHzOdbedjFcm3QAgHbC3qDlcvFl3Xi2aST9dWLoQ3EeaxhjJU7Xp yR4ZeGH28U1aZK5/AveVpYhqkk10y5gEgu7tidasNvH6u5V+z+W8DSUdfP66cL6uW3Mt 91xUhY1LZn0yx2lTgjRsXixrOQBt2/3PnzWWnvmqqeflefQOfS62hto9+MqLjZkLAXpT 5EHF2Brv8IjffyDKQ//UnmKMgJLSyW9DYIapIr7inWNkaiC0XNyiFRKHeE2PjHOF5WQG JWdA== X-Forwarded-Encrypted: i=1; AJvYcCX6QiUHWnLjq0xskuZBAcq8pZUCVZVrmYtvI7S6QNXDTlJl0NUK9vj5pBxfCHf1R+jal8jZm3ytG65OGuJKd5cbmVsthPifmzPCg/w= X-Gm-Message-State: AOJu0Yz8RQk5n6Vpes+crGU3p4DwZj3Hm3WcmM0ATb90Kg/yK1Iy15w3 CXHaTF8lQFGUUS8eallE2ED2sbcCYoZinrRKxnwB6OxDdqbSVazdkai7iN7IQQ== X-Google-Smtp-Source: AGHT+IGDnOeAF1FL804TtRjppmHScnFvlydpSaTIsnO+VLF+Nn6sN4yHuLdVS3qjMSH7/M91C1F0BQ== X-Received: by 2002:a17:902:c145:b0:1ec:659c:95fb with SMTP id d9443c01a7336-1ef43e25e5cmr138560735ad.32.1715757431472; Wed, 15 May 2024 00:17:11 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:11 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 06/21] zram: pass estimated src size hint to zstd Date: Wed, 15 May 2024 16:12:43 +0900 Message-ID: <20240515071645.1788128-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 bfe836844232..f930e9828be2 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_default_clevel(); - 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 15 07:12:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664752 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 DEB695A0E1 for ; Wed, 15 May 2024 07:17:14 +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=1715757436; cv=none; b=Kvh9Q55teHzwA1TFcuHPJO4iGLdqYk4A6JECEqWFph5hwpvLIzmDBXQvw9v8tmNkWeO+XllL9DCbXpOapDq0HNkpBzkdVTRGoCOdVqJIr4z1tyuIcOMc4+amChBQZySj2yc18p6RPMK4D2bgExtZ0ysBUKSn6Nh6KJEICVj/XRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757436; c=relaxed/simple; bh=KCnNcRrdNNXPCLyOiCquIu29kMdtXA8DR2xEEGBW8rU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Py2cWa2B59pw29e4bNSTFMDMA4EwmqxCbpp2aSw2qu+QX3KVBWs742d524Qtx9gWXnp6vwQoeQRIGhJ9YEPbdRmUaZUPdrErvep04TfQrx3Lq8brRSYEz9Mc2IVc2FYwkVbVS89pdctXuWGoXJUnXgUvyhZh+KuodA2GLoeyDxw= 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=m4Rm2UYo; 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="m4Rm2UYo" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5dca1efad59so4888425a12.2 for ; Wed, 15 May 2024 00:17:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757434; x=1716362234; 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=y95jRkO+GQnVvfMlitMFd5UTtBybaGLJy7YbFRNgOjw=; b=m4Rm2UYo6OPV4yeac4SynDYQZwAT0/5Vvw0IAJLs0ajQf8rKDf5L5wbOpXKnfpTYN1 n9OvV2v+Z8+JO0LkoxZD6q07sMR2TziMr3pNvPJWJQdbmFp2Cz3GT9KcB1eiWq/dHYO/ NidIxRqBxDXe0h+jRSM/KrblHlVZjza1ae+rU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757434; x=1716362234; 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=y95jRkO+GQnVvfMlitMFd5UTtBybaGLJy7YbFRNgOjw=; b=LTsePC0yGvpAPFYsw4ea05L/W9sF325JGTvZUOpbgBEH4xNYpCwnPQEMEXg/zmjvAO Inp4VI8ohQJjY/TY5gMc2ff5KX16CH9JkRcKOHYqHqyYdoRqM6bsy+iaMwScTSoPTirY I1TRoy164GAY4CPSLSk3kWec+EMevsmUUQmIxi9wXSJFWYNHDTNASNwDQPhB5vNvRMeB a/P0jbRQ8r/AnnFOn6z7v9sR7RiCiXpjMkkEHID8farXoDoUYPsX+gqFUBZz1o5s9U1T kd4XmSVgNEPuwt13rUzhqRYgfGsYrtHn0iA9TqkZ51g4bjYwqr2fIULCMpo9t5KJMZAz IK8g== X-Forwarded-Encrypted: i=1; AJvYcCVybRc204S+Kz7REs+RABGIXCXmAG7Yvh/Cw2OWgAg5sBFxB4Q9pI4+gJgRJKaEtJ68nUh+KDoOsQL51teIErbXbS2e8lettdNo4h0= X-Gm-Message-State: AOJu0Yz4/F6v1S1Bv7JcqjvKEIZ6RpIWmG3KugIPtohgHyEmkrp4Ra3T zMjMYGIvyHdgz4C9m4YTkZFLImkWLGq7FEozr1ACG1cXdwUttxuC47VgqOwnwg== X-Google-Smtp-Source: AGHT+IFLzF62if52qJ1KBXdicGFtIwJvmc9KJjo8zwyiW54iJHmUAuxAjnUDL7j7CHzBCzunheLUcg== X-Received: by 2002:a05:6a20:3d84:b0:1a9:84f6:dcb6 with SMTP id adf61e73a8af0-1afde1d91dfmr15784036637.57.1715757434238; Wed, 15 May 2024 00:17:14 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:13 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 07/21] zram: add zlib compression backend support Date: Wed, 15 May 2024 16:12:44 +0900 Message-ID: <20240515071645.1788128-8-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w zlib (inflate/deflate) compression. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_deflate.c | 130 +++++++++++++++++++++++++++ drivers/block/zram/backend_deflate.h | 10 +++ drivers/block/zram/zcomp.c | 4 + 5 files changed, 156 insertions(+) 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 a2ca227e199c..266430548437 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -6,5 +6,6 @@ zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o +zram-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o obj-$(CONFIG_ZRAM) += zram.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 15 07:12:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664753 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 794215B683 for ; Wed, 15 May 2024 07:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757441; cv=none; b=hPi3nZ87k+2hgsky5//1vDffLAgElECx+PNXfV0BjQhPHh6geKi91nwZws+HTxcGL/7rw4Cu753b+c/GtS1TILSpyTrgzVhI1wJd5vYwsIHeIvZQhLcKjEW6XcugWc0w/BT4guYtJwZKpwy8Md1Jxm5bYm4OgT1VMtMxX1lQ+pk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757441; c=relaxed/simple; bh=a+rQHeyHWJr5E88fLAaNVtGpfAm1FcfbIk5kcHXBMNA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Yo0hLaawLDESo1z5fEXcskPuVjkMd4hOiX7mhMQ36784PbRcy1u20m6ibWFavY6jNBvcbhVHxjAWm6I1FDPi9Pb4CSfID3yS+atR6d1fwUMRfnXTydFLJxhpjj6HHUJ5QO4/bjNR4sNd0m5kXfl5E2zxNQgpMNY+HB3X91p63ZM= 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=lJ+dQfGo; arc=none smtp.client-ip=209.85.214.174 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="lJ+dQfGo" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1eecc71311eso54293305ad.3 for ; Wed, 15 May 2024 00:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757437; x=1716362237; 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=R1BvSeNbTbBx82yan2V/RhWZavK2/Nwn3AwN6PoYkyI=; b=lJ+dQfGoknPf202DLVf0jTPVBcrR/7DNOXRUXwWsOokvW/u7EuVnY4BSJBtkugtJal W/vi8b/waDV2pZZdsTq7tQ1fwnxgBRQRpJa968Tx4lpkJ3N5/q2Bv0OFg1+r1G7gxSh9 mM55uHd8l3Ihr2n7khEHvDJUaZg0kBh6vCql0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757437; x=1716362237; 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=R1BvSeNbTbBx82yan2V/RhWZavK2/Nwn3AwN6PoYkyI=; b=X5yGYlMlryQV+qjdSwituKZxJLCwH9Shb2BuArLzArhCFD+JqEbiSKkaQfoIgATTu+ N1v36x8UxEgr5Yq5B1h6dngBFDzbVnvOq3ip5YndjigQC0KRmWofxA5eoHj5gc0nvlYB JcDM7k+fySwH66PYFJThTzODPWExw4ZyKozvC5ZmucjT6du9DcUlXnzXCNY9Wp4DWG/x aTz2VZYyzUoA7xQ3zp86zk/VPnp4P2OKC9AerbN0NEVGg//968j7jZEeVtUjKbmr3dyH rXrdJFg/AWMfFZknRBt1d3NVs6EwxcHedZFolOMCyAyzEdM8VN4E6gx+m4Zdwy4i5hUy yVNg== X-Forwarded-Encrypted: i=1; AJvYcCXeXH+EgJqJLULQ0qQpmApzXiCt3osrB5wY5HB97irE30xYWf+OhKncJUegYEfw8z6luBuq0ifvO/e506g3iVRzwLhS41B1oVPeZnI= X-Gm-Message-State: AOJu0Yw3C+Jf0Lwk/H2hG8NlfuBRUrGVnTaZBqsPbDMuNZG8IdsomYVp 6DuPusrfoRaNY6F4y4ucsxPhjeNNT5XROJfmT24sJpXL8CjcMw9nzG71ait8eg== X-Google-Smtp-Source: AGHT+IHl9yzwzSALZfi0o/HPHNzCxu6wZ+RNh+cMmfX5oJ73uVNFYO9l4NG6YzfeqIT88y6WcZ1L0A== X-Received: by 2002:a17:902:e74a:b0:1e0:2977:9dfc with SMTP id d9443c01a7336-1ef43f4ea08mr235269795ad.55.1715757436880; Wed, 15 May 2024 00:17:16 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:16 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 08/21] zram: add 842 compression backend support Date: Wed, 15 May 2024 16:12:45 +0900 Message-ID: <20240515071645.1788128-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 s/w 842 compression support. 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 266430548437..0fdefd576691 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -7,5 +7,6 @@ zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o zram-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o +zram-$(CONFIG_ZRAM_BACKEND_842) += backend_842.o obj-$(CONFIG_ZRAM) += zram.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 15 07:12:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664754 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 C7A355EE78 for ; Wed, 15 May 2024 07:17:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757441; cv=none; b=kMPVV8t5x67Tnm6xLjWELpGKTm/7OZt6f0JRvOuW+tz7nYu+/sdR5nMnw8P23oj6ud1i3ZvOF7EiCr1sfTsbMn4DNdwJY3bwPd8Hc8//ShRjsy8cKq404tTcB1eyn2rJnwvNCMAzPi5kKy+MCzLAIhMxGV3y5+oSzGHOTD3+/jE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757441; c=relaxed/simple; bh=fkmGnp5H4sYk5AcEucbfVYS4PjNz7ma3EYQ0ecKZnDQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U6WiuMlrJxqt8osnDZt9ev/uq0Ie5/S62aiI5a0qo7UAt886cxJ6jS6UJ+l6Ut+Erl/PUlt0HKsTe5iUNQ7Tjy9bLDncvQ4uVArhBemUAizJ1DtqM8ZwQEWt1mPBsjRBYpMkuXUYat7CmY26ronhHWYBMHcrcJZcIzXzWE6QfEI= 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=XOXQbRcc; arc=none smtp.client-ip=209.85.214.170 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="XOXQbRcc" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1edc696df2bso56596795ad.0 for ; Wed, 15 May 2024 00:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757439; x=1716362239; 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=XOXQbRcc/5Eftf81qGGiSFwKljbAd4Yaidd1Ll+OYJc1YSaOujkIsUt95xcblTUgGa V+P41TKB3jvve5g/CeVM5fTaP+t/MKEeA0RT2uJbXUSzOVHZVgd4zCVkX4ygALurVlqO 1SLZeoSzKdm7kodbWBX5OvYelcO4/ssndEb50= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757439; x=1716362239; 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=WOEPexR0uhkV6XtPuCRBr+SbiSAGnQb2TGM1QIi56HwTSmGTnQwUUp6fcbODJ2Z6MM Q3dXkmpHEwNbffK0j0KsFSk2S3/pIBSUHKzgqRZ4PFooe2lD/huADNNNGb1NyWw5S9g/ SBZEwb17dIIVAqjxFPcq3iChdGqwHT9f+ViD+9xQSCS6vekmpG+0xydLY6ADn4ax2ooL U1GCD61yjc9w+XOO+cYfz1VtTRzOcErYBN0qqqzN96PMAIoFFeekdYnKAeHzWrDlkcTz m8rNLzpRY+W4D3/RgrVwhSkDnZ4M8AWXFBgC9zLF4ZBzhbJHIFabjfhUxhuJmbD89PlV 5L+Q== X-Forwarded-Encrypted: i=1; AJvYcCUFRm2Om/CM+GD0GsBJwWGFJa7itj24crdKoA/vYpaybz44bdbfmh56auba1AvH1r00XRwMWPNm4eDefupzWtd/C04uj5ZH4gP+1po= X-Gm-Message-State: AOJu0YzVy83iiE+Vv1H6z0nW3YfSgfdVoyChSKGUlLzdRJLVoqtISHpG 9Iin9R9EhTpZwjCd6AS22pPg5VhFGw7CN+JWLwljRBh/LQEUgORmuF3nsiq9IF5Qei4NMWi7er4 = X-Google-Smtp-Source: AGHT+IFvQPQbFdswaG8pKMdD1k7IMUK032+vZt0K46ITPMQuokgurkpbNqiMOKMg6mMbkrns6C8dzQ== X-Received: by 2002:a17:902:bcc4:b0:1ee:c491:ab62 with SMTP id d9443c01a7336-1ef43d2ea47mr134926055ad.25.1715757439167; Wed, 15 May 2024 00:17:19 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17: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: [PATCHv4 09/21] zram: check that backends array has at least one backend Date: Wed, 15 May 2024 16:12:46 +0900 Message-ID: <20240515071645.1788128-10-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 15 07:12:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664755 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.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 4FB074438A for ; Wed, 15 May 2024 07:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757444; cv=none; b=MsjRw2UELsfwSwfFzP3R+zxExlCf1YEmaADjheXDKCANLkPz81zve7tQFgBlqX7Z37w/K6RJf1LLvwHCecg2M30O5+3R/IcyXr1PQbmQGD+ng25rwdx2gTcA9LVknRIULDhMvaBLFn3S9pVQyeddxNRIsN90ni/qbyLgJt0M89Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757444; c=relaxed/simple; bh=4TR3qcfJHoDHqbXJJ3W7Q7XPODavCGAvmdqEyE6Irws=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fX1H+f30irZYkPoTpAAVUeDEculjxUrhw/CXhM95Id33wpD/ox7TO2WOxu56yY6c8qPadXObjtMWOytda21/N8Z8zA/hy42nIhEKcoICmdH2p0Z8QK4NBil54rsivg+mcQL7msITLHk7q//15axA214uWQRfnvRg0NnNAaFTieg= 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=dWpMaU/J; arc=none smtp.client-ip=209.85.214.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="dWpMaU/J" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1f082d92864so16526735ad.1 for ; Wed, 15 May 2024 00:17:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757441; x=1716362241; 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=ylr3jp2oOM/Qv+5Rzo3O/r/CyrGBpC7ZOv+1y6H5M+s=; b=dWpMaU/JxykJog3Lv39wkauy1UVePPOWA8BXNLl7PIXEI4T9Cwf8xduBL9e/DlJqiN QFfwNC3WKKOpM3eHdpvrWcAu8ndRU+mw9VuOcyRt+0zV4gwVJz3+31Cbq/9w1T/YxN3v HnabcnyAKGWBmN+F0zfYx1ht4xrLkE66fcLKo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757441; x=1716362241; 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=ylr3jp2oOM/Qv+5Rzo3O/r/CyrGBpC7ZOv+1y6H5M+s=; b=WGQdMSsRbxSAf3OvPy/K5nSnHnsEiiYkMvCC9gDx+qL8D8JGTo3QdpovEh99Pf75Z+ 7jdla97h31Nd2WQ9b5VXyKUjZcjwJxlCBxpxhej9wSmNmL+FbJQUnHSqnfjy8KLtXK6E T0XSzJ2yYMbv6d0UaxB3Ckapdbi694HBAn2+UZ/g+pKpkwwkPp5SdzhjOhnTIMLoak6M uGfnkBWESlCk6QbrzRhF1bM8DN+GUev4+lyShHylGEHWzECQwU4cgyFINMqHy7ONxr1D Bu+voukHLD7W9qCoHo7tGD1OOP9EMRVck3bnnVhpusfgIxIKi/yrdrRMIkWEd15ULbMe 6tkw== X-Forwarded-Encrypted: i=1; AJvYcCWzybRm/PvC4NIO+J2YUyl5TFIzjFgd+AqVuMsP1pd6TeJ/zlcmWxbDG87M0/KgDKS+3cu0QBwznNEBzhMM2VeGrnJb8wf+b1Xfjm4= X-Gm-Message-State: AOJu0YyWi6ZhsBcdkHzfDeucXAqta0SaWw/m9TGcmRIKLjTxKVrUXpPg hUl3793jYeaxuXNGrfCukOdEy/5CSIWyEHzegaAEJsMoEO1qc++ibePm3uufhMAUL1LfGCSyzIg = X-Google-Smtp-Source: AGHT+IEuqUbMH1dVaUqG9mvERkiQ2Ee7dAVCVB672+cKA0XgZvskGuWFwOFiCoOhsDXfHUCvCcgHYA== X-Received: by 2002:a17:903:984:b0:1eb:7746:4248 with SMTP id d9443c01a7336-1ef440495abmr176193975ad.54.1715757441574; Wed, 15 May 2024 00:17:21 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17: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: [PATCHv4 10/21] zram: introduce zcomp_config structure Date: Wed, 15 May 2024 16:12:47 +0900 Message-ID: <20240515071645.1788128-11-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 | 9 ++++++--- drivers/block/zram/backend_lz4hc.c | 9 ++++++--- 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, 62 insertions(+), 19 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 6ea67511d782..df0753fd8180 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -18,7 +18,7 @@ static void lz4_destroy(void *ctx) kfree(zctx); } -static void *lz4_create(void) +static void *lz4_create(struct zcomp_config *config) { struct lz4_ctx *ctx; @@ -26,8 +26,11 @@ static void *lz4_create(void) if (!ctx) return NULL; - /* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */ - ctx->level = LZ4_ACCELERATION_DEFAULT; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = LZ4_ACCELERATION_DEFAULT; + ctx->mem = vmalloc(LZ4_MEM_COMPRESS); if (!ctx->mem) goto error; diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 9bcc5aa19a2b..2f03ca5809c7 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,8 +26,11 @@ static void *lz4hc_create(void) if (!ctx) return NULL; - /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ - ctx->level = LZ4HC_DEFAULT_CLEVEL; + 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) goto error; 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 f930e9828be2..d392d364ad2c 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_default_clevel(); + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = zstd_default_clevel(); + 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 38afc2c23108..75ecdb885d91 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1979,6 +1979,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; @@ -1992,6 +2006,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) @@ -2049,7 +2065,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]); @@ -2256,6 +2273,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 35e322144629..c3c7475d6517 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 15 07:12:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664756 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 B2A5548CCD for ; Wed, 15 May 2024 07:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757446; cv=none; b=rZeghCkvEZJfecMHYBWSLoMFdiW5uev9VngnMVbUR8ZtuCzaHn4tzHAiFq1/bwysSysaGCQ9sgLznCEzMEyQ0dw6DO77fHDXAo4RlbOkQsFF6VmtobcXrgsyZIpRAjn9vR85Y+dydFHDMpyEavfUmkNWPyBYOzl3Bua9tqMdqkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757446; c=relaxed/simple; bh=YnHUtLMZwpVLI0sVuuwwM6o13HO9vMtZxnKMkrUR/KQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q8yAx2i7V3jL7gZ6CjhdNFohi0+xDnftPn8YEeE9bexwYq1w7lZlbb0Jt5oqRGsNSN8jzEkpoaZEZTS4z76cf+f2M5RJQzzZmiDm0hZrgPh+RZ1f1U+ZHBV17M1SdA9MmCIc7EK1uhXH96tzKgrwmCJWj/NNTK51vm/3fJFosu0= 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=LO5Z3Q9t; arc=none smtp.client-ip=209.85.214.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="LO5Z3Q9t" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1ee954e0aa6so52139845ad.3 for ; Wed, 15 May 2024 00:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757444; x=1716362244; 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=EKVs+B+0cyVVZhjjQw1NRD02y131beAoNCpGUQE22Zo=; b=LO5Z3Q9tbMtRLgU4hrriUYiXGWQbatUGRHUqdFQ8GmElEulIilhJdczdqhlD7j24r3 Wg/vfrFw+2UCMJ192IWDIvfpG+JpW2wvHIka38x8ZEDgoMHGJpXMTOQjr61At/weeEUp M46cAzb6F+/xfEPCrEstxU8oArfne0fJ2p8As= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757444; x=1716362244; 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=EKVs+B+0cyVVZhjjQw1NRD02y131beAoNCpGUQE22Zo=; b=KaJl8hd5M+hEkYd4V+klBOlkXOmwNkWXOyvEzuupkqZFCDXZWBL31OdBlBdy6WuBF5 rG/GoQhZNE49ziscf2tgiKePfgDfDdGvICEq8iqNrtjm3SMDwMn6pz28pUHCQQfwKn4p Qnv7sVC/7rkR0BSTrABYckePmyQC1UXslS21c58arwyVu8ZBh550FKfh8r0L1y3luiky 1+RKv9dW5K5iMvHaT/UDgQ4HSEd/rNp6S/GLyTeQLiuoWyOGAyeiLLuubdGYDTCpBRdK BU1EsIiZrid/mdj/k4EkYm/LBelWaio4nLpifHTluftx7VG8IhifaHbItVhfBZ/43pgr O3zA== X-Forwarded-Encrypted: i=1; AJvYcCXEZE1cSvkzDCffJ8FsY/cJc7TJaqht2Gdetc0QFbIoH0QhZHMSPm/loNWmpUpsBp9sBytpFzNMhKfKlupzQ4FCV5CL4zH8jf7dEqw= X-Gm-Message-State: AOJu0YxK2k1bbLIMpP8CfUambx9O05EOOUQh65x0gojoKtEW5fWDDrXs M2Zlxw2zR6ib82eFj7elPiP6TdQAyv1ItVYRjeIkDVrBxjSczC+tDSS5eJVPog== X-Google-Smtp-Source: AGHT+IE8qiXvQd9erZoKg5Z5UhnAOTEuylXJnZ57+lB5IHA55FEIIAx2uTY4ujRkG+u9767ycaP2LQ== X-Received: by 2002:a17:902:cec3:b0:1ee:b23b:ad5 with SMTP id d9443c01a7336-1ef43d2e1e8mr177002205ad.5.1715757443909; Wed, 15 May 2024 00:17:23 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:23 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 11/21] zram: extend comp_algorithm attr write handling Date: Wed, 15 May 2024 16:12:48 +0900 Message-ID: <20240515071645.1788128-12-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 75ecdb885d91..7eb500c8fdaa 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1013,9 +1013,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 15 07:12:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664757 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 E2B2348CCD for ; Wed, 15 May 2024 07:17:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757448; cv=none; b=pviM3fKYOEhkYyweXT2yjB1HtWGVulvaHegGH5kOI3Af04x/osAKVjTiYcNlLrETUxjNONch6ZOiRkTFfNYNQDgtHF7Kz+0+LoWzWGoblkueaqvReGEhMuB6Zt7oWjBKi2IoKLNL8HmO1IO1VhnHKhXYhLiYCmWckTRf30VBDGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757448; c=relaxed/simple; bh=NSsCiPGcIxF0zOQ6vdufDUaR67+LlToiDZLTG5tQTr0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EKQlHEldBCGjZF8Vc0Wfk7+KattgRp7FXzEI5I44DzPh+HYg2Nzf2pAR7yX4u3N4WjYOncpl+ulEU6oI789sUOLIQZ5mfd6OoyNEeDBxIIsi00+/1NlkhP5Tcg555rZCXkhYrum5Uix0hplMRGusFttAK36wfStDYqV783bUWb0= 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=Gx3LeW9i; arc=none smtp.client-ip=209.85.214.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="Gx3LeW9i" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1ed96772f92so51679485ad.0 for ; Wed, 15 May 2024 00:17:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757446; x=1716362246; 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=7bmm+z3Y++HgLKjqUQ1k19xvJfVuFLOPQwKCzZQr8AY=; b=Gx3LeW9iM9+zmkNIGCPmc35XkwEQn2XcILyOSg9atjScgmafLHd2uMc1E3NnjIsjEi CALrMw1t6+7leFD8H2qJj0ZJZ7ikkBrsXgjd7+2BJzsQtlzaD3YOWZfZ3LoLdjrXFz2n efgxwgrHjEHQJ1DjBW4GNolYqmi3u+U/W4kr4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757446; x=1716362246; 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=7bmm+z3Y++HgLKjqUQ1k19xvJfVuFLOPQwKCzZQr8AY=; b=Uv+t9QGJxYudCGI3wL5pwT7D39vweDmfaelWbfRC4EuV78CHqf2l58QTwXbbBfnRvn QIYoU3VYBxn5t5EcmiYNGfdCiiMtoZwLQHCTv52wYe3u+UASyRulusjhGWm1Dgv9Vbyw h59zLEp/e3DkIonlkEZaprKflqrro6Dp07MyQ00ZIxFiSodRpOP0Gygc6X1S+wWEyK7K AQp219rmN/yxmpkdMt7oQKBDADvqUtm7Eo5J5JImwQLuTPhVxp0n36Gj96Gx8P3WRxVK wmfIc/C8XqAYdQYIaFgSvEDrqGi6vCUNCfJ/Or89JaIVdU+FTkm6Ih62uvMMB5x3Rnxc ZCfQ== X-Forwarded-Encrypted: i=1; AJvYcCWQccQWQTmhoWELFX9IpEWl8F506azU+D3bOv885gmq52tRf9jOCK4IjGPqrqRDJ8pwxzFutBwPFeVhyNGwfYcqqf1SDGJlbez8LSw= X-Gm-Message-State: AOJu0YwOQpvHpbg6OZa6eq+GvymT9+e51fUi/98YXw9TNwPf8N24pPLB +vS7Pe3tm+eesab1N/LUmLLlQEE+enr/AXmQuH7uUECzE5n8GeSrjcg0bdC/Ug== X-Google-Smtp-Source: AGHT+IFqYYiz6/dbTR0wwd7ijHcmxpeWPZVSroUIYlUMxCGV540oRKNRsxU3YgCNTEN6VkVK7MOiHA== X-Received: by 2002:a17:902:ce11:b0:1e7:d482:9e32 with SMTP id d9443c01a7336-1ef43c0e8d6mr184387855ad.7.1715757446224; Wed, 15 May 2024 00:17:26 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:25 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 12/21] zram: support compression level comp config Date: Wed, 15 May 2024 16:12:49 +0900 Message-ID: <20240515071645.1788128-13-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 7eb500c8fdaa..91706889b63f 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -998,6 +998,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) @@ -1015,6 +1021,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); @@ -1034,12 +1041,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; } @@ -1072,6 +1088,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); @@ -1092,6 +1109,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) @@ -1100,7 +1124,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 15 07:12:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664758 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.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 346225C5F3 for ; Wed, 15 May 2024 07:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757450; cv=none; b=rIkhbABLA8kQXnbUimkcSgsLsqSHPE23MAbDpB4p/Ed1qV63M/wx7zGjynXVcJ9k2TisTzsCW4SDZCvAgMD5mG7psFRAcWyCMHaZ+/b+h6q3V4TSGLYLjRDorQ/cnJyJf/y2oGfAuUtKiVJKjQSJ4AW4UyFafkWtMRMupvZn4y0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757450; c=relaxed/simple; bh=lOpECWNUVFdB4uflmaL917YB3kCc2WBAsq/OEf5OpTE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TIu1QvC8yHaYZDLUMcDGeqUlZxt0fi8OPcNRlIj/e169plxNom0PFjXYiJC8GYYniQ3N0naFLNHAT5cg3ZngL6ngGEzNlV00qaYDfSUZqGZkWRb77fX3fzM8HOYB5x7yUNXo96c0wNyMDf8qj2nft+KfRlrvMoGvQ5R6JbgB6oQ= 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=GGRlDign; arc=none smtp.client-ip=209.85.214.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="GGRlDign" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1ed96772f92so51679745ad.0 for ; Wed, 15 May 2024 00:17:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757448; x=1716362248; 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=4La/wbPPFJXkOrGYwK5uu1FW9Id7puPhRkViZ/LZt3Q=; b=GGRlDignRfYmnYlC3l25nP0NSXAdeAA0fFaCDTPlRzqmu2R00HMa+xFseWbiAYZecY krqyHz7O1GrlfcD9VCc0DpRxEehCZAANxUucua0wOLGQiB4VPyuZ+ph8H7W9/MqJBKCi SCkKj5SidVFWJR0lkM2Hskh4G6Vm9WRqemLFA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757448; x=1716362248; 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=4La/wbPPFJXkOrGYwK5uu1FW9Id7puPhRkViZ/LZt3Q=; b=wrT2ORIJ5wGonf7NADaiASW38U5DYadGfzor1M6/BhfUvg/m0c3qZ42BtkjuEifypO gitB82PReRCDhn8j0AD2A8E3Sqw7Ywi+SVU6ez9XnFWtUSUlfKhPDj3kGsQf84vKZ2Wi WXAndzQ1il0mA2a5lQFL4CnCfRYkhe643ouW1PDc7qlXQ8TNoBahuGepKyYHS1m5H2Dp Ah6AA8uzeBGPSc7Zbgka3/RSvo0LwjDGCIqhxKlrJIUycrFI+wxtJpF4ITxrJmI9D2cP CdjqRKmRUVuv2YuJ4JgTqxMuwmdHzlndvy0r9etMWbPA326oGWsZQyF1Iz2NVpYOn9GC 1hvA== X-Forwarded-Encrypted: i=1; AJvYcCVeVlkgK5AVLwkr4JZeRopvEK1ka3ouEIYAJ4eT/w7615uWCEgiei7ZjdaLWH+mLdwG19/i1GgG+IEr943HXInYyhB7CD+XEscSCMw= X-Gm-Message-State: AOJu0Yxa9u9Tz6TbevM9tJIxmQRZnlTg6wKEXN8jtg/3eCiTWwzdvRQO ye0/OtsmTT2Vha/Cx/P2L8FAV0pDrDBM1qH/DQxd9sRVCwzTY0uORkVm2cNrrA== X-Google-Smtp-Source: AGHT+IF1uuv1Ow3yvq8pHb/zFsmRaet2oXT4g/kID1xMXGAMF0E6Hhv0qQeFV2atbjmsxgzSnd8SRQ== X-Received: by 2002:a17:902:e889:b0:1eb:c3d4:349c with SMTP id d9443c01a7336-1ef43d2ec0bmr187724195ad.30.1715757448368; Wed, 15 May 2024 00:17:28 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:28 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 13/21] zram: add support for dict comp config Date: Wed, 15 May 2024 16:12:50 +0900 Message-ID: <20240515071645.1788128-14-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 91706889b63f..216686a5f3f0 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" @@ -998,9 +999,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; } @@ -1020,7 +1047,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; @@ -1048,12 +1075,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; @@ -1087,7 +1119,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; @@ -1116,6 +1148,11 @@ static ssize_t recomp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "dict")) { + dict_path = val; + continue; + } } if (!alg) @@ -1124,7 +1161,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; @@ -2034,12 +2071,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 15 07:12:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664759 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.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 C79B162171 for ; Wed, 15 May 2024 07:17:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757453; cv=none; b=Y70Cg8xPlPEgtavmT8HopYX/4X6C5RFSd7PZFa61x45dB9o0kNYCDNDsTfCAa/odgAzz3uWbfMOfCM/Hno262wCji+PA5LbdzdtkDrD+d7lni1mYwrYPTmDjBdeJGk5ItyepaT4oj4yyAs++2x0iRLVwpBzz4VmACGJWnatEhxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757453; c=relaxed/simple; bh=dGZx0+acqv0CZi2wH7InQ4R8O8AcdvZYdUe91hxxiWY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nSN0JJuL4+kqD8wkwQ/xsxDn3US7SofLsmP7QM+ZHA21D5QV0FQmIj0XWdmLrAQzNWcbgtH4C23NYrXOFYCTdUdJbLdiQj8U6F1Ei2QojuTR+fWJOTyCkDeMdcXnQJPSJILG84T1UejYi/fmWt4QW4yCuDMGkytWaW3xOcw241I= 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=XYhwxIUM; arc=none smtp.client-ip=209.85.210.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="XYhwxIUM" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6f4472561f1so5896174b3a.0 for ; Wed, 15 May 2024 00:17:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757451; x=1716362251; 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=/z5a3E5tjsWd2v3yZqDEd2ZFUX29EqdmRojLDBz4OHQ=; b=XYhwxIUM8or1FxDmvVN5glOWFu63z/QCHG1Ml26EstgH79SW1uXwEBvolud6QX6cdr 2+dXRPM+1lza3dA2tPVvNhIQrMIsTCCoKiEZOzBED75+gaQ1/9SWjjsxVuJV0tQVwQOQ n0dmXtvze4CwhLT/6loisp/6N0yo4cD6NuT5s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757451; x=1716362251; 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=/z5a3E5tjsWd2v3yZqDEd2ZFUX29EqdmRojLDBz4OHQ=; b=EodaHsRk803VHjFXm4xpZD9FsENqBmx0PvJEwj/Ash0aUjCTX+MIke9LDwKIo1e4Xu 0xI4KpPYHg45zGkem7wbdMV3pnY/9XExwDVak6oa3DeT0tmd7+o1Uql5vjTYpnh47ERo uaD2e5FV0aos7l4CKpUZKaKAXNurTrK4VVtjCiL6kP2lMkwOQFQtupQX5Jqa9/idFKON Z1FhMOQ41Le4LaHq1cMi3a65H46W6D6rExtjSITqGQCKYLK7NodJ3TCdONi95UccqIZP vQ2WTE+Bw5SA/pwGvgzG/5Y2WdEBVYv7MskbZ7ZT5/3TZ4KHGPJ9PA7IcM2ClBBWR9DC ps7g== X-Forwarded-Encrypted: i=1; AJvYcCUfGagOGYgE8IHwTah0NsP+AAUyvPMwSn1pe0JvFY4qyitIKf99kIXNIcOX9huKt3csf5cLNmwDqcEPdTh0c0KhU77ZqIzg9rhe7Eo= X-Gm-Message-State: AOJu0Yw3tor6pLTk36oSy70J/XmJjwZIQ6YtU0NVdYF0ET/y6U+s+01P tJckAsAKjnldL5eDgJYjko/r8/4ihEJxYhgLvhancNTr1cTU9LD9wbLaGPBUGA== X-Google-Smtp-Source: AGHT+IGv1Omsyk9TRAdIngjWvd5a6uW973xBM8/wHVw3kf6S3NTLue+rsCGPi9x1UgDL0rvIAAuHOQ== X-Received: by 2002:a05:6a20:f38f:b0:1aa:a6cc:39c5 with SMTP id adf61e73a8af0-1afde0824f6mr18701539637.7.1715757451053; Wed, 15 May 2024 00:17:31 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:30 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky , Nick Terrell Subject: [PATCHv4 14/21] lib/zstd: export API needed for dictionary support Date: Wed, 15 May 2024 16:12:51 +0900 Message-ID: <20240515071645.1788128-15-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 need to export a number of API functions that enable advanced zstd usage - C/D dictionaries, dictionaries sharing between contexts, etc. Signed-off-by: Sergey Senozhatsky Cc: Nick Terrell --- include/linux/zstd.h | 165 ++++++++++++++++++++++++++++++ lib/zstd/zstd_compress_module.c | 52 ++++++++++ lib/zstd/zstd_decompress_module.c | 38 +++++++ 3 files changed, 255 insertions(+) diff --git a/include/linux/zstd.h b/include/linux/zstd.h index f109d49f43f8..18260a354231 100644 --- a/include/linux/zstd.h +++ b/include/linux/zstd.h @@ -77,6 +77,30 @@ int zstd_min_clevel(void); */ int zstd_max_clevel(void); +/** + * zstd_default_clevel() - default compression level + * + * Return: Default compression level. + */ +int zstd_default_clevel(void); + +/** + * struct zstd_custom_mem - custom memory allocation + */ +typedef ZSTD_customMem zstd_custom_mem; + +/** + * struct zstd_dict_load_method - Dictionary load method. + * See zstd_lib.h. + */ +typedef ZSTD_dictLoadMethod_e zstd_dict_load_method; + +/** + * struct zstd_dict_content_type - Dictionary context type. + * See zstd_lib.h. + */ +typedef ZSTD_dictContentType_e zstd_dict_content_type; + /* ====== Parameter Selection ====== */ /** @@ -136,6 +160,19 @@ typedef ZSTD_parameters zstd_parameters; zstd_parameters zstd_get_params(int level, unsigned long long estimated_src_size); + +/** + * zstd_get_cparams() - returns zstd_compression_parameters for selected level + * @level: The compression level + * @estimated_src_size: The estimated source size to compress or 0 + * if unknown. + * @dict_size: Dictionary size. + * + * Return: The selected zstd_compression_parameters. + */ +zstd_compression_parameters zstd_get_cparams(int level, + unsigned long long estimated_src_size, size_t dict_size); + /* ====== Single-pass Compression ====== */ typedef ZSTD_CCtx zstd_cctx; @@ -180,6 +217,70 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, const void *src, size_t src_size, const zstd_parameters *parameters); +/** + * zstd_create_cctx_advanced() - Create compression context + * @custom_mem: Custom allocator. + * + * Return: NULL on error, pointer to compression context otherwise. + */ +zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem); + +/** + * zstd_free_cctx() - Free compression context + * @cdict: Pointer to compression context. + * + * Return: Always 0. + */ +size_t zstd_free_cctx(zstd_cctx* cctx); + +/** + * struct zstd_cdict - Compression dictionary. + * See zstd_lib.h. + */ +typedef ZSTD_CDict zstd_cdict; + +/** + * zstd_create_cdict_advanced() - Create compression dictionary + * @dict: Pointer to dictionary buffer. + * @dict_size: Size of the dictionary buffer. + * @dict_load_method: Dictionary load method. + * @dict_content_type: Dictionary content type. + * @custom_mem: Memory allocator. + * + * Return: NULL on error, pointer to compression dictionary + * otherwise. + */ +zstd_cdict *zstd_create_cdict_advanced(const void *dict, size_t dict_size, + zstd_dict_load_method dict_load_method, + zstd_dict_content_type dict_content_type, + zstd_compression_parameters cparams, + zstd_custom_mem custom_mem); + +/** + * zstd_free_cdict() - Free compression dictionary + * @cdict: Pointer to compression dictionary. + * + * Return: Always 0. + */ +size_t zstd_free_cdict(zstd_cdict* cdict); + +/** + * zstd_compress_using_cdict() - compress src into dst using a dictionary + * @cctx: The context. Must have been initialized with zstd_init_cctx(). + * @dst: The buffer to compress src into. + * @dst_capacity: The size of the destination buffer. May be any size, but + * ZSTD_compressBound(srcSize) is guaranteed to be large enough. + * @src: The data to compress. + * @src_size: The size of the data to compress. + * @cdict: The dictionary to be used. + * + * Return: The compressed size or an error, which can be checked using + * zstd_is_error(). + */ +size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, + size_t dst_capacity, const void *src, size_t src_size, + const zstd_cdict *cdict); + /* ====== Single-pass Decompression ====== */ typedef ZSTD_DCtx zstd_dctx; @@ -220,6 +321,70 @@ zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size); size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, const void *src, size_t src_size); +/** + * struct zstd_ddict - Decompression dictionary. + * See zstd_lib.h. + */ +typedef ZSTD_DDict zstd_ddict; + +/** + * zstd_create_ddict_advanced() - Create decompression dictionary + * @dict: Pointer to dictionary buffer. + * @dict_size: Size of the dictionary buffer. + * @dict_load_method: Dictionary load method. + * @dict_content_type: Dictionary content type. + * @custom_mem: Memory allocator. + * + * Return: NULL on error, pointer to decompression dictionary + * otherwise. + */ +zstd_ddict *zstd_create_ddict_advanced(const void *dict, size_t dict_size, + zstd_dict_load_method dict_load_method, + zstd_dict_content_type dict_content_type, + zstd_custom_mem custom_mem); +/** + * zstd_free_ddict() - Free decompression dictionary + * @dict: Pointer to the dictionary. + * + * Return: Always 0. + */ +size_t zstd_free_ddict(zstd_ddict *ddict); + +/** + * zstd_create_dctx_advanced() - Create decompression context + * @custom_mem: Custom allocator. + * + * Return: NULL on error, pointer to decompression context otherwise. + */ +zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem); + +/** + * zstd_free_dctx() -- Free decompression context + * @dctx: Pointer to decompression context. + * Return: Always 0. + */ +size_t zstd_free_dctx(zstd_dctx *dctx); + +/** + * zstd_decompress_using_ddict() - decompress src into dst using a dictionary + * @dctx: The decompression context. + * @dst: The buffer to decompress src into. + * @dst_capacity: The size of the destination buffer. Must be at least as large + * as the decompressed size. If the caller cannot upper bound the + * decompressed size, then it's better to use the streaming API. + * @src: The zstd compressed data to decompress. Multiple concatenated + * frames and skippable frames are allowed. + * @src_size: The exact size of the data to decompress. + * @ddict: The dictionary to be used. + * + * Return: The decompressed size or an error, which can be checked using + * zstd_is_error(). + */ +size_t zstd_decompress_using_ddict(zstd_dctx *dctx, + void *dst, size_t dst_capacity, const void *src, size_t src_size, + const zstd_ddict *ddict); + + /* ====== Streaming Buffers ====== */ /** diff --git a/lib/zstd/zstd_compress_module.c b/lib/zstd/zstd_compress_module.c index 8ecf43226af2..a5edf59334df 100644 --- a/lib/zstd/zstd_compress_module.c +++ b/lib/zstd/zstd_compress_module.c @@ -66,6 +66,12 @@ int zstd_max_clevel(void) } EXPORT_SYMBOL(zstd_max_clevel); +int zstd_default_clevel(void) +{ + return ZSTD_defaultCLevel(); +} +EXPORT_SYMBOL(zstd_default_clevel); + size_t zstd_compress_bound(size_t src_size) { return ZSTD_compressBound(src_size); @@ -79,6 +85,13 @@ zstd_parameters zstd_get_params(int level, } EXPORT_SYMBOL(zstd_get_params); +zstd_compression_parameters zstd_get_cparams(int level, + unsigned long long estimated_src_size, size_t dict_size) +{ + return ZSTD_getCParams(level, estimated_src_size, dict_size); +} +EXPORT_SYMBOL(zstd_get_cparams); + size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams) { return ZSTD_estimateCCtxSize_usingCParams(*cparams); @@ -93,6 +106,36 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size) } EXPORT_SYMBOL(zstd_init_cctx); +zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem) +{ + return ZSTD_createCCtx_advanced(custom_mem); +} +EXPORT_SYMBOL(zstd_create_cctx_advanced); + +size_t zstd_free_cctx(zstd_cctx *cctx) +{ + return ZSTD_freeCCtx(cctx); +} +EXPORT_SYMBOL(zstd_free_cctx); + +zstd_cdict *zstd_create_cdict_advanced(const void *dict, size_t dict_size, + zstd_dict_load_method dict_load_method, + zstd_dict_content_type dict_content_type, + zstd_compression_parameters cparams, + zstd_custom_mem custom_mem) +{ + return ZSTD_createCDict_advanced(dict, dict_size, dict_load_method, + dict_content_type, cparams, + custom_mem); +} +EXPORT_SYMBOL(zstd_create_cdict_advanced); + +size_t zstd_free_cdict(zstd_cdict *cdict) +{ + return ZSTD_freeCDict(cdict); +} +EXPORT_SYMBOL(zstd_free_cdict); + size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, const void *src, size_t src_size, const zstd_parameters *parameters) { @@ -101,6 +144,15 @@ size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, } EXPORT_SYMBOL(zstd_compress_cctx); +size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, + size_t dst_capacity, const void *src, size_t src_size, + const ZSTD_CDict *cdict) +{ + return ZSTD_compress_usingCDict(cctx, dst, dst_capacity, + src, src_size, cdict); +} +EXPORT_SYMBOL(zstd_compress_using_cdict); + size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams) { return ZSTD_estimateCStreamSize_usingCParams(*cparams); diff --git a/lib/zstd/zstd_decompress_module.c b/lib/zstd/zstd_decompress_module.c index 7d31518e9d5a..6a0f464609bc 100644 --- a/lib/zstd/zstd_decompress_module.c +++ b/lib/zstd/zstd_decompress_module.c @@ -44,6 +44,35 @@ size_t zstd_dctx_workspace_bound(void) } EXPORT_SYMBOL(zstd_dctx_workspace_bound); +zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem) +{ + return ZSTD_createDCtx_advanced(custom_mem); +} +EXPORT_SYMBOL(zstd_create_dctx_advanced); + +size_t zstd_free_dctx(zstd_dctx *dctx) +{ + return ZSTD_freeDCtx(dctx); +} +EXPORT_SYMBOL(zstd_free_dctx); + +zstd_ddict *zstd_create_ddict_advanced(const void *dict, size_t dict_size, + zstd_dict_load_method dict_load_method, + zstd_dict_content_type dict_content_type, + zstd_custom_mem custom_mem) +{ + return ZSTD_createDDict_advanced(dict, dict_size, dict_load_method, + dict_content_type, custom_mem); + +} +EXPORT_SYMBOL(zstd_create_ddict_advanced); + +size_t zstd_free_ddict(zstd_ddict *ddict) +{ + return ZSTD_freeDDict(ddict); +} +EXPORT_SYMBOL(zstd_free_ddict); + zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size) { if (workspace == NULL) @@ -59,6 +88,15 @@ size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, } EXPORT_SYMBOL(zstd_decompress_dctx); +size_t zstd_decompress_using_ddict(zstd_dctx *dctx, + void *dst, size_t dst_capacity, const void* src, size_t src_size, + const zstd_ddict* ddict) +{ + return ZSTD_decompress_usingDDict(dctx, dst, dst_capacity, src, + src_size, ddict); +} +EXPORT_SYMBOL(zstd_decompress_using_ddict); + size_t zstd_dstream_workspace_bound(size_t max_window_size) { return ZSTD_estimateDStreamSize(max_window_size); From patchwork Wed May 15 07:12:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664760 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 D2F6B5BAFC for ; Wed, 15 May 2024 07:17:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757456; cv=none; b=hYgJ5Pu6JehKEUxWiN2Cr2FJCcbRflRQKh8o+tWPcXc2RRPAPPLs14vQjIV6TOrAwEUFUlmL5PHyNYTCH7HJOkNmW8LkIY+nsA8PUMgU/lvTdHB2TLjH3kAWUb4FHHdoeQybqE0RLySMvCqmQekTIGM1Xd+zGS9bA5NAn4fW3EE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757456; c=relaxed/simple; bh=qPxpMzrLexeajXOvydJh+v3ubpdPbmLqWgxknedh2Uk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qQN9TTa6mYnCORpmYG6m658GaU8Iw73GsCViJeSIfCkzr7pjEUEOrsZMcR32VDsahHtoz77UrZ+L5Ok8BzDx71k58uNXk375qDNX3eZCSQJor7VGntQun2dD8V2ShHL4JrxWx64QtLLHyq3qIHYO+mbyg04ekIsOEH4VXTGpTLA= 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=Xdt8p9hv; arc=none smtp.client-ip=209.85.210.173 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="Xdt8p9hv" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6f453d2c5a1so5277447b3a.2 for ; Wed, 15 May 2024 00:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757453; x=1716362253; 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=e1Q92/50oEzsFAIXeAasi/lhjVaiDz6wVjSMUCSkV98=; b=Xdt8p9hvR9TFDNsREHSqFGtB4LrvWY6k3grut4Jh2mc67rjUSKE6UncsmCO6Lc5F/q 7q8FhbvOcx+XLoPR+yglbhlOfFlycwo9lgVqS6Mmdfvvvw5icjO9/p1zNM5frFZNuulB QBXRdiJqUWmsdPOCwa8nlNr3UmFwQCV1tiXL4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757453; x=1716362253; 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=e1Q92/50oEzsFAIXeAasi/lhjVaiDz6wVjSMUCSkV98=; b=T7/NVgtMXc21ADPnzklqZIXR3tApbATudmnxjp38C2Me8i7M8kR5OoGMtiEfymVlXM hComt5M8h5fti9Zg2tDRADtdPcVCbtT+MFKzrPf+j0ykZSNV7voFL2lliebcp34TMSOV ZvIax8izGlwO+8or8fEMS6um9G8cFgVaqzwS/c8OnDD6wXv2ID76BNTZd5ECzyym+feh mshdkgXkOSTXgvGwAWMSLevtmiUQonA0F3P/N575yil7jBz9M1pZCbsmFwcNYK2bcf2B gyWoAYZ/5Ml9OiOWuoak9oM5YO1DtpnJIWwzNZfQjFqutpn8NWPLKLSVDOysIYVVdDNo kBNA== X-Forwarded-Encrypted: i=1; AJvYcCWjAsuHwNyDbrORzT9yfCQzvb3y6puUzj2ulSGMeazTxIdqitr+wwyYc3gPUd3/IoK6cHei4rKZaFRQzIFSmgRB7XsTOuAhddYSo6o= X-Gm-Message-State: AOJu0Yw3o3Agdv8j6GsbvGRS0lK93FExIDhTsO6X7A58hxNMR9aObcRM FbP/SlXp3B5um6447f7nXpsWoJU4H5sV8rXDtjBh3azHL5BXo0xe3vaHW/BoRQ== X-Google-Smtp-Source: AGHT+IFoXN+G8OBVIyHl2dhkr+cK7yJTsa+IDe8Y4EgCK8SM3Nz6RhZgJrZp6Vf7WOYHuo5IjVDf2g== X-Received: by 2002:a05:6a20:5504:b0:1a9:8152:5102 with SMTP id adf61e73a8af0-1afde0d54c9mr14256096637.24.1715757453236; Wed, 15 May 2024 00:17:33 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:32 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 15/21] zram: add dictionary support to zstd backend Date: Wed, 15 May 2024 16:12:52 +0900 Message-ID: <20240515071645.1788128-16-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 d392d364ad2c..4be149370e0b 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_custom_mem 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_free_cctx(zctx->cctx); + + if (zctx->dctx_mem) + vfree(zctx->dctx_mem); + else + zstd_free_dctx(zctx->dctx); + + zstd_free_cdict(zctx->cdict); + zstd_free_ddict(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_default_clevel(); - 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_compression_parameters params; + + ctx->cctx = zstd_create_cctx_advanced(ctx->ctx_mem); + if (!ctx->cctx) + goto error; + + ctx->dctx = zstd_create_dctx_advanced(ctx->ctx_mem); + if (!ctx->dctx) + goto error; + + params = zstd_get_cparams(ctx->level, PAGE_SIZE, + config->dict_sz); + + ctx->cdict = zstd_create_cdict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx->ctx_mem); + if (!ctx->cdict) + goto error; + + ctx->ddict = zstd_create_ddict_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_using_cdict(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_using_ddict(zctx->dctx, dst, PAGE_SIZE, + src, src_len, zctx->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; From patchwork Wed May 15 07:12:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13664761 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.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 1809569D2F for ; Wed, 15 May 2024 07:17:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757457; cv=none; b=U4hPrXRslgm0ji2/stnHcYbRCq6hqIJwzF/FOz2EE8jXoHsP93N31BKL+QmOlnKlFPpwxrfID+HRqcX/rMGmbp6CewINUCdZCC5PB/HrQy5Et5oh4ZDafqgc3Xf7RyMbrvdEOVUli7pQqAOxPqz78zm7qNOnNFw1XFJPh3xGGp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757457; c=relaxed/simple; bh=EdcVeRKP/KQ7J2vOPlmfMisYBBpem1rc+IBT0Rrt74I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Trhyx3oBPSJtLft/Hq4s2FDv2IR93XbcuvebTj/PgSIUzJ+rfVGMWL5zGpAQuWnM0svVUTecv8VpFVcaUkDrWp7PxS0rzXNSda5x2UMmVyAYH/E7QRlrEnqooK2k2Hz5+dipjoVClBxf4zQfhzCfD/auvn72a8Cm6hXsgJxE/KM= 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=LYOGCI/y; arc=none smtp.client-ip=209.85.214.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="LYOGCI/y" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1eb0e08bfd2so39285605ad.1 for ; Wed, 15 May 2024 00:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757455; x=1716362255; 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=KqMJS5agkLEOzJWW1AUSO+pWta3iupztSYk/iAWBNyc=; b=LYOGCI/yIuAyRPoBAN9Lhn3v1z52/Tj8mf633vwkwvzNGMv2Jcg7rYe/JyUJA9bgK6 bbCJW4kiYIc5NzxoFGBozGvRP+BHGZa2z6IIXQdTpYCeU9KpuJ2e/7cLFtZOpGcoil5i TY7HgCvmkTtBcpEDI79AQ0Zeb+AOU3uRV0HAE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757455; x=1716362255; 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=KqMJS5agkLEOzJWW1AUSO+pWta3iupztSYk/iAWBNyc=; b=HqvfIzLBaFi4VP4L81qAOHSrNt7JC+n1CJBhaNJgUgZBEwS7DWvS1B2MBhegvoW16p n++ijPkmVGl3/B4fmUUTA8GkCKxeIHxeSh5m4VX9XG9C5PpTfxmDXz0PJdnM+zosjHRq 6GBnQ2VNJLsV8n/6QJRWkkLQfxO2G4IlWFRU4dq+ACfhQ5gZGn8Z5LN7ns1+f6Jrzye6 LRM64qutmJ6fEyQHSKyGtkSuNKpj7+A2Xc6s93xRQhBs7UxeH5qJqeAp6jU/n9/f3Jc8 OK6GHo2iYdmyRrwAil+z5iZPFYYYuSDFmzMv7wFR9Z8O3vfqU57exvYXSxtsCgAO4VQK jkyg== X-Forwarded-Encrypted: i=1; AJvYcCWxaVbtFM0Jet1AsJo6KT949/iZKlMCFZogz3XEey/JgDBIkOOHnHpc0cBNwxwc/3dWPO0qt2Pmy8p871sRni9YfEVdHcP3BUsx50Y= X-Gm-Message-State: AOJu0YwwoUsN5nTxCLO6UBTldnqduqTRS3M6jpJDZdkrRJ8tMhYHFT+c OPs8PJ+GeaNflbGmXkp6+1dB3+iQJCCvEOPzO6qyV6CLajdRg50yXeeg4hCQvju6h4yWRo4q6yI = X-Google-Smtp-Source: AGHT+IEqDaF2jnae7dsuVOOBN2n95AgJH3VLf55Gs8FaRkx1MfHX06Cxat1HEPBqfloibFYY9DeFyg== X-Received: by 2002:a17:903:283:b0:1e3:e081:e7f5 with SMTP id d9443c01a7336-1ef4404bf29mr165136805ad.66.1715757455517; Wed, 15 May 2024 00:17:35 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:35 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 16/21] zram: add config init/release backend callbacks Date: Wed, 15 May 2024 16:12:53 +0900 Message-ID: <20240515071645.1788128-17-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 | 20 +++++++++++++++----- 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, 106 insertions(+), 20 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 df0753fd8180..cf39bfc30f5b 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -10,6 +10,18 @@ struct lz4_ctx { s32 level; }; +static int lz4_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = LZ4_ACCELERATION_DEFAULT; + + return 0; +} + +static void lz4_release_config(struct zcomp_config *config) +{ +} + static void lz4_destroy(void *ctx) { struct lz4_ctx *zctx = ctx; @@ -26,11 +38,7 @@ static void *lz4_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = LZ4_ACCELERATION_DEFAULT; - + ctx->level = config->level; ctx->mem = vmalloc(LZ4_MEM_COMPRESS); if (!ctx->mem) goto error; @@ -72,5 +80,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 2f03ca5809c7..428b393371d7 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) goto error; @@ -72,5 +80,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 4be149370e0b..9d2cc27d4c9d 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_default_clevel(); + + 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_default_clevel(); - + 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 216686a5f3f0..ce63c3f12f58 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1003,6 +1003,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 15 07:12: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: 13664762 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 5D8AF6D1A8 for ; Wed, 15 May 2024 07:17:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757460; cv=none; b=hlqzM1Z4D+AhgLxHUhpVpVULW08v7bI48S05GqkhTEKtTtw7zQRZMrcZceok/ROYC+Rlnel0KwfqByZCbRwsLaxG1X59mYV5CB02uDjjzZ9TozceFC9saUPLQlQ+LLVWjlsPj4C/2CuAJ+D+7FnHZfdUb8VPH7oO9VaBKqOF9nU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757460; c=relaxed/simple; bh=27D84d9fXLJIjEidlYnHwUN44hOlRv9gnV3y+W6WffA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QC7zDsI8j63uK/GHTCy7kTksAtLwvD11MNEsh8zxFD3OxvfUMKqJlWGdtQupZsvwJX2CD0b7PJyPQCBsARyBUMe4J0sJfJ4h1LPMCh9lz84eX+rCp3CR3bp/kz5h0s1tEfPf9zexEAyRdgBjVF94iFhSVopEg54OJ+1yNj5TpNM= 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=cf+6dqrd; arc=none smtp.client-ip=209.85.214.174 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="cf+6dqrd" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1ed835f3c3cso56415985ad.3 for ; Wed, 15 May 2024 00:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757458; x=1716362258; 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=1NDMJY3isqUp1Ew8boWj0krAQJNa5FH6QFiRGHLN6Vo=; b=cf+6dqrdu3Tq1OTezw3qOVjOrFhtLLoHjo3nt3iGZtzq83y9P04BG5WLsVXq1sw/BY aoloOrBTZbEhJ7E08d8fl0YcphjchIRzE6E5XLSxfR8VV03yRWlVCpKrFV2Y81i3xZJG dNdvHwnOAncDJxlRc4+7TDz24qzCqoCDgfiN0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757458; x=1716362258; 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=1NDMJY3isqUp1Ew8boWj0krAQJNa5FH6QFiRGHLN6Vo=; b=tpr02wLEIkGyPAa0BGal5tq2B7xA4FNw0eSzFCRigwYa6dXO796OXk2Fb/EQPe0b4h kXWzzUod2TPp6PPo6c2h4N1pO9CFrOa4wTqZHsgkUaLbW8oi7vaoq08FCzLB7h8XXuWT g43CLH0VVPS5PY7WepbBIlAyMWcXzdPC1Jf7OriBQ8kn8FzY+U6exuHKsyOZ1hg1OLl5 jLLw/cZTO4+cMqaQ4YOsFQsA+uOrYgPnmk3CmodLkuT9yZIB+nFNmnwDRMmupN/1sAkm +IIPX7eVsXR5WPV8ySLTgW5g4r+/K3FHKSCe6aMsfudGxoYdYtRXaR8k73TBkxI82TiZ sCwA== X-Forwarded-Encrypted: i=1; AJvYcCUJc0eScBqA1wDQu0B0DkPqlkpX0DVhUR1oVLa7F9puRYl7lPw5m659GEnTPtDF9BqEaB0/oXz7tVWK/B1Oe9ivqoFmrF6ZH8VrM4w= X-Gm-Message-State: AOJu0YzHbV+pjczxHc2gL90AfgdcPZbnbHbv1ljTG6yk60P0TDSTdc1l 4Zt4qiQb0rQ5VEmciJhMdcm1ptjiMJQxRHdKh3+YWYD3dVjc/YJ1B+slNOLEMA== X-Google-Smtp-Source: AGHT+IHeoSK5yxNBWrJqyEizK8yAwDS+DFO/v6xnM5tOk4j5OWsHulkeJfcNBaP6LY2IvO2BEnwfXg== X-Received: by 2002:a17:902:ecc7:b0:1e8:c994:b55b with SMTP id d9443c01a7336-1ef43d15786mr210891335ad.7.1715757457712; Wed, 15 May 2024 00:17:37 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:37 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 17/21] zram: share dictionaries between per-CPU contexts Date: Wed, 15 May 2024 16:12:54 +0900 Message-ID: <20240515071645.1788128-18-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 | 108 ++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 9d2cc27d4c9d..836de2af7c11 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_custom_mem ctx_mem; + zstd_cdict *cdict; + zstd_ddict *ddict; +}; + struct zstd_ctx { zstd_cctx *cctx; zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; - zstd_custom_mem 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_compression_parameters params; + + /* Already initialized */ + if (ctx_data) + return 0; + if (config->level == ZCOMP_CONFIG_NO_LEVEL) config->level = zstd_default_clevel(); + 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_get_cparams(config->level, PAGE_SIZE, config->dict_sz); + + ctx_data->cdict = zstd_create_cdict_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_create_ddict_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_free_cdict(ctx_data->cdict); + zstd_free_ddict(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_free_cdict(ctx_data->cdict); + zstd_free_ddict(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_free_cctx(zctx->cctx); - if (zctx->dctx_mem) vfree(zctx->dctx_mem); else zstd_free_dctx(zctx->dctx); - - zstd_free_cdict(zctx->cdict); - zstd_free_ddict(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_compression_parameters params; + struct zstd_ctx_data *ctx_data = ctx->ctx_data; - ctx->cctx = zstd_create_cctx_advanced(ctx->ctx_mem); + ctx->cctx = zstd_create_cctx_advanced(ctx_data->ctx_mem); if (!ctx->cctx) goto error; - ctx->dctx = zstd_create_dctx_advanced(ctx->ctx_mem); + ctx->dctx = zstd_create_dctx_advanced(ctx_data->ctx_mem); if (!ctx->dctx) goto error; - - params = zstd_get_cparams(ctx->level, PAGE_SIZE, - config->dict_sz); - - ctx->cdict = zstd_create_cdict_advanced(config->dict, - config->dict_sz, - ZSTD_dlm_byRef, - ZSTD_dct_auto, - params, - ctx->ctx_mem); - if (!ctx->cdict) - goto error; - - ctx->ddict = zstd_create_ddict_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,17 @@ 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_using_cdict(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 +197,16 @@ 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_using_ddict(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 15 07:12: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: 13664763 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 894566F06A for ; Wed, 15 May 2024 07:17:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757462; cv=none; b=Kb7napogX46DWX6qIoYSNEbKHUyIgpB050j1O6oTpgAaSRSpGeYbJ2PDwWP1mrLm+QpItQ7ctrQPNb11H54WcdmVc0OlkrwwoHzJQKhU4cG2uyjmxdz/tuuIlTjafwRulvViJ7Jj7U4cVVvDoGNmwx+qDeHbo7RC1s8/ZlNO+zU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757462; c=relaxed/simple; bh=MpumSGwS57AOBN7ZGr4e9nw0AvNHL1Xtn57eoC3mKTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Swl/hXzIE9Ms79+Y3i3C2HTb/gARdJUAPKX4Kwdi2HJfj/hLxq1oExrjvlX5JFXiBeap/rNMrTQbh4UFIvb7YDNws1PJk9qwlSc6vse0EYYEgvWz5MBOu18j1FMa13LGoBpDx/3E9kWFKVq4AQ0AZdJH1Kxt3WFkybQPxAJhVG0= 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=NXrrCK2d; arc=none smtp.client-ip=209.85.214.174 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="NXrrCK2d" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1ecc23e6c9dso43076735ad.2 for ; Wed, 15 May 2024 00:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757460; x=1716362260; 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=uMJ82RyKF+6qXGhhZplFfcbZEx2r4rtHTVfrRVwN5n0=; b=NXrrCK2dZVHCswEwYlimpfveWN88+wE22uHa+YQaslqB5AS1fJ4BC9AmYGAlDqrzIQ 9SW2vdMVOB/GFpvZDAGhB2Ttgyi3YHtauV5A/XC3ugUSl1yUpJgklY5ZNvygiB3RcF3H 5lED72TAQ8o6X+/fWe7q3nm9BWHhh/1ra3Q8w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757460; x=1716362260; 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=uMJ82RyKF+6qXGhhZplFfcbZEx2r4rtHTVfrRVwN5n0=; b=bMFHd1EOpHQPM29RgvCm2bNfC4xC0V9vMElyvTvcozUGcZfB10xWdECWs6K6NsGooZ Ql3aVa+KaRc1ZiancOfIcFJpHGel7iKt27E/yDIA+utuShdj+yUEN9x1iwdpR5GTxKb+ 7oIodPC3Zc6cs+BFbzQOin4dM15VVu1RcCworTm1zqY2cUfXAvwmLWHBA0Fc3BKgYfLV AKF27+m1RsdixP4nzL4p0vXQy+8FXSSwTYSrLKQRFUOHYIF8cs+33LpCP9q3ZE2MonYo Ldb0GnY6gMtefLHpaoSSCxtMgywrNHR/rSrCBCtWZdPPk1Wd5PP/E6qj/0tRJueiOm8S 3hMg== X-Forwarded-Encrypted: i=1; AJvYcCUPnym0FwMXq5itZw/FvrconTaNUGPd6TKt5A4dzkZaRtA5e067wd/StZdzVrjEhETANby2/cll9b5pkbXjYrxcLtoUaJrHzxEyJAw= X-Gm-Message-State: AOJu0YxLOUY98qvMT+p4jlp3nqiv12YbX6ZMCJg7OlUP8joA059dyFFL BMyX2EnIXhJRV3Eqta/KyOM1AKzMC8Pf3bEeKj+BfQIlt9EZK01/DUOD/BYncPqSu6CmbLjeo8w = X-Google-Smtp-Source: AGHT+IFWK5yzmfV2TgdVGKOyYOrHWxkziGiQydgnTP8gYfOW1Be1F/ocsMetxNXGL/KQ6FtS97/Wvg== X-Received: by 2002:a17:902:f60a:b0:1e7:b6b5:1f05 with SMTP id d9443c01a7336-1ef43d1b00fmr170185825ad.18.1715757459970; Wed, 15 May 2024 00:17:39 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17: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: [PATCHv4 18/21] zram: add dictionary support to lz4 Date: Wed, 15 May 2024 16:12:55 +0900 Message-ID: <20240515071645.1788128-19-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 | 54 ++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index cf39bfc30f5b..47d0e719d5d7 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -8,6 +8,12 @@ struct lz4_ctx { void *mem; s32 level; + 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) @@ -26,6 +32,8 @@ static void lz4_destroy(void *ctx) { struct lz4_ctx *zctx = ctx; + kfree(zctx->dstrm); + kfree(zctx->cstrm); vfree(zctx->mem); kfree(zctx); } @@ -39,9 +47,22 @@ static void *lz4_create(struct zcomp_config *config) return NULL; ctx->level = config->level; - ctx->mem = vmalloc(LZ4_MEM_COMPRESS); - if (!ctx->mem) - goto error; + 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: @@ -55,8 +76,18 @@ static int lz4_compress(void *ctx, const unsigned char *src, struct lz4_ctx *zctx = ctx; int ret; - ret = LZ4_compress_fast(src, dst, PAGE_SIZE, *dst_len, - zctx->level, zctx->mem); + if (!zctx->cstrm) { + ret = LZ4_compress_fast(src, dst, PAGE_SIZE, *dst_len, + zctx->level, 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, + zctx->level); + } if (!ret) return -EINVAL; *dst_len = ret; @@ -66,10 +97,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 15 07:12: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: 13664764 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 EC7EB4AECA for ; Wed, 15 May 2024 07:17:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757464; cv=none; b=maKWos3bCQNoceWpOLXmfaYBkgbzsSbqxF//zr3ibie+YmLS8T2wSLFywVSzohM2iLy0wsyERxfWzupM0AQM6e3UGHa7ka579NL/ejTGs/3CbTqrJXlpg2TRusjqiDQUzYq6TPBkHXIu/XUDjmdBBkdvuHBM9vfqc0npfnyo0g8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757464; c=relaxed/simple; bh=vwOiFI5WYyrjHytmKrfcWdUOay50C5Amcx9THlRsWqQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hyhfop7tvfpvZpowVdbMpidzqELghpLXem+G+ncL4B8v+849V/zS+wnnQobSVWN6swXUOin2a3L7ScBRUftBOcqQMDTu7Da+5foQ1jF39LsDTc+ZVUiwzZgUiWug7jLdauwcegUNHFGJtSUyDoC0iBOZ6VLo0wZJc9g9cZIw1T0= 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=bTXAM2h+; arc=none smtp.client-ip=209.85.214.179 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="bTXAM2h+" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1f05b669b6cso36651885ad.3 for ; Wed, 15 May 2024 00:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757462; x=1716362262; 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=NWMx/siR7FY9UCYUHf6j2VRPjDt1sZ3YrZXGDzQRlXs=; b=bTXAM2h+GhS9bRINMcGOldAw3OpEZEMGRHDRC1FifNDeNhfo3OJYmzpJGHaWJ8Ksei Hu6WsJeiWQjcqMkIPGu2YoDWPxzcytREKOZun2lRdj/zdNs+5ZWnjI27UPhX+RGjon0H X3vaF7/4KDKSRsiBa9CDkRHtUT8I5PcTnLyL0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757462; x=1716362262; 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=NWMx/siR7FY9UCYUHf6j2VRPjDt1sZ3YrZXGDzQRlXs=; b=IVJ8JqZDG9B7qC7RegnTgh4/VuTK+cz4fRZG2nB9LInTEdv9+c+RHFeQDmYdcAScRc 4APaYQEtp/PrU9YDXQc/ysQOPvUETu3+yDova8YHgc4jZHXl23XumVdYLqmfEWJaKCqd /8jk/ASDcUD3l+zxy1hEP7lX8GPnPipWvbpwB+I7Dtv3n7zC8BHOz6n8yo4djObtauEd StvZcb23gmS+5pim2agGUKD4cBh3ah2g8sES8eMt6KqKhkDadMh9vO4T63OmkgADc+Qu GZjZzsG58qiW84lrPw+Lfg3vTL32c1Du3Dk1pafyyCJned8z2JrZYoWsXArUFQfTwpfv HufQ== X-Forwarded-Encrypted: i=1; AJvYcCUPC88dB8c9b52jXOSi043qFY7SE5FffPFBT3h376pHibbVR8CppoS+E6/q8L8LbNDwyuH18OmQJoDU6X0lTw9ml88L3TaZfSQBppI= X-Gm-Message-State: AOJu0Yz3iAByO08IQCtrP/27fGIjQvfTetAS/mTkQsgPr40Miu3Bnzqf 7W7vQf08Q0ZGMrwRnETcSmnoPIqt2ftEhLG+pA3syTHsRH3FMz/kXM8EAijhVA== X-Google-Smtp-Source: AGHT+IEAZvvWI3L3dp2OnVta0kR0smXesfqE4//K2Eg9tSM/PQOb96+KhvywxjLh/WRSanjCEV8rcw== X-Received: by 2002:a17:903:1209:b0:1eb:6477:f2e3 with SMTP id d9443c01a7336-1ef44049595mr180248885ad.49.1715757462340; Wed, 15 May 2024 00:17:42 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:42 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky , Nick Terrell Subject: [PATCHv4 19/21] lib/lz4hc: export LZ4_resetStreamHC symbol Date: Wed, 15 May 2024 16:12:56 +0900 Message-ID: <20240515071645.1788128-20-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 symbol is needed to enable lz4hc dictionary support. Signed-off-by: Sergey Senozhatsky Cc: Nick Terrell --- lib/lz4/lz4hc_compress.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/lz4/lz4hc_compress.c b/lib/lz4/lz4hc_compress.c index e7ac8694b797..bc45594ad2a8 100644 --- a/lib/lz4/lz4hc_compress.c +++ b/lib/lz4/lz4hc_compress.c @@ -621,6 +621,7 @@ void LZ4_resetStreamHC(LZ4_streamHC_t *LZ4_streamHCPtr, int compressionLevel) LZ4_streamHCPtr->internal_donotuse.base = NULL; LZ4_streamHCPtr->internal_donotuse.compressionLevel = (unsigned int)compressionLevel; } +EXPORT_SYMBOL(LZ4_resetStreamHC); int LZ4_loadDictHC(LZ4_streamHC_t *LZ4_streamHCPtr, const char *dictionary, From patchwork Wed May 15 07:12: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: 13664765 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.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 5D5CD76025 for ; Wed, 15 May 2024 07:17:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757466; cv=none; b=p0B9xNeKrx6hmQhanKrN086DYO8uaIj3+HK7uQnk1gn7Xf6Wwj8qXmCheqSKzkgNOUcaWcGNnFK4maT+tP8Ocru8taI18HHI79jsPwmN8UCYKmcviHumzIpWUlFLdIxa0SfHUW8CGlAkPbyrrzHV6SO3MSjg0k5v0sXhpu5yMv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757466; c=relaxed/simple; bh=qtvmUsQREyXmjeLbpuxebnWi3agfG2+3rkkIDgBbtXg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EF5gqnjegv6bsEPA+T6CLGKb1tbJXCVZBZNySBJhhSYzsN7Toh69e+FudzBMr4ojNQh3DiCuHtWnL99xfbqFiy8kY8cOzJr3jWGxuMA/ltcxVmfudTK5D7iuDFl4rXv4GhGvt6T+mDlGW6UWHw6+3ztJfm4/NaC/74aRDUB2gKo= 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=Fdh7B8/Z; arc=none smtp.client-ip=209.85.214.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="Fdh7B8/Z" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1ec41d82b8bso61704155ad.2 for ; Wed, 15 May 2024 00:17:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757465; x=1716362265; 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=fHfG35o7+8/7OU1lSE40PRM3WsVXKsxMJUAQYr8zh6U=; b=Fdh7B8/ZhcGrI2v5MO0se01y9b5gbob6rKAnF1fJY7gwhE8c0SUAhpUFtyHzHOSq3Y 0hugWD0WUKa+/NJByWV/RAad3bt4YtwXF3YVLhXA+F1sFpRKzVymT0gXY6Js+QCyGkkk ZOfnWzBX5WjLBwZVK+j9FzJzfuZvGYydJNZGI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757465; x=1716362265; 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=fHfG35o7+8/7OU1lSE40PRM3WsVXKsxMJUAQYr8zh6U=; b=c8XlYfEnDEsIp8dFBQ0jpkllYdSBWOGaKjfciDspvOkkIGLyLlcmEMIKU6rtAFbTfP AQ4bmCQh+timnixqF6Q5o6DdNb3hR1n9Qcy2cWSeba8qX1isvU2rLEpXmbL9AjNPAvHe fsES+r3Amxik6Pv8rqIpmP0cTjNmSE1IHXSpDc76vfTUOe6Fz6IL6dLf6TKCftajCPk9 qjH6AvnkijmpCWxYLl03yxOosH7TaXqXlBIpmGOlRL8lQMD7hEK9sKv876warb4A6FAf 5mvYn8IaP7GPD/OkxO1mZrmHoHrpJOUmoWMniZRD9c51+DwvJMaEg8zfdHB+j9Gd15hn jVSw== X-Forwarded-Encrypted: i=1; AJvYcCVm2QZcuWUHjVRgwmRWrtCmBL9ovjU1Wo6P1Xb8IHP7TuCMVkFxIGCOFQJ5N7Op8a4a8WPIB6DoZERYfk6krU3J/a1Ml0LOtk/JO+g= X-Gm-Message-State: AOJu0Yx/TOQHkSWR/L44hDEOLGQRNKbeRG8QktM6IenhqmDfRmoscWvE KYK61G/hdST2/Bq7q3Qd0ZUJvU5VK4ykIrZITidcVoiiTOqilr8JU3wv9m4G5w== X-Google-Smtp-Source: AGHT+IFWdGVgCe1JBmXZ3hDnpc6dFO9Vvt71Vj0tCFKh+5tnuApc6QuAnmJpi2vhaAxChIPFAqL2cQ== X-Received: by 2002:a17:902:8542:b0:1e2:be4b:dd9f with SMTP id d9443c01a7336-1ef43d29670mr140437795ad.15.1715757464806; Wed, 15 May 2024 00:17:44 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17: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: [PATCHv4 20/21] zram: add dictionary support to lz4hc Date: Wed, 15 May 2024 16:12:57 +0900 Message-ID: <20240515071645.1788128-21-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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 | 54 ++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 428b393371d7..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,9 +47,22 @@ static void *lz4hc_create(struct zcomp_config *config) return NULL; ctx->level = config->level; - ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); - if (!ctx->mem) - goto error; + 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: @@ -55,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; @@ -66,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 15 07:12: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: 13664766 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.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 BB673763F7 for ; Wed, 15 May 2024 07:17:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757469; cv=none; b=KazZC1BwTv4SGHvoKKAqw0o741wu8oCrhgOV/wtJ3zAh3LD1o02Ypq9vEFqlARkKUsqm3fkX8eFMvei/H2tsRvIoW/aR2ouSvbHGHiGcy50kuWpTb1dDF66x1pHvOUznYj6xQbT+zN+2Zhp/UTAqCJt0if+PBp/U7RI54C0mqjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715757469; c=relaxed/simple; bh=0QKD/VRLqE4UJhTMThwx4W0GPaxzgatlvvK+3EE9Gd0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ictz6hF1GcdpUj9PRkuzQX6jmRtVoFxCkqHAqI7beLai8ojoEvnWrh3uqAfgvRTc9unZ8FdWu/rp2fagtY3VRuFrfNO+uykk5q+6N0PHn0cIGsgTKc2r7rHe36SZGWCQzvpr1ElfDfOVJa/XEsHguYHIpQ3BmamfbQ0s1UyEsas= 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=YHQzgsw2; arc=none smtp.client-ip=209.85.214.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="YHQzgsw2" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1f082d92864so16529405ad.1 for ; Wed, 15 May 2024 00:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715757467; x=1716362267; 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=YHQzgsw2miaw+A94pdrS9zbw6+Yen4uDKAHCSsd+5Pv/WyQAA41PeR3hs1rChZ6Yc9 CPSflOIuyfyGCdPWV0kx5WKzNQ/E8BsOREcSthOX/Vb58PcCkQ7RNy/on1u5NRyRWuxV 10TMPfYFcNtQe4w9M+ZBzIRuCTEv6/IFvkLd8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715757467; x=1716362267; 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=sC/h1lwiQSR9khnnv6VZ4BK3jNhh9I42jGVJ8D1Q7pJaBezsOTAwjqcorCiFcPNyhO AI76sBkQVbKZCOf1/4cFBzQL2S7JsICvkn/p7wzYXsFNXz4/LtrpVGY0sqSUEcJf/Tqb cYMWoFg4Uvoyefuzn3huqWrgNvdzEB3ATge5iBVGBX7g/BmQ2+Aqfl9+sX/fchA8cMYY spSDqeNvUXmaXUH57uqFWuMWCOE4KWX769Q3gBZbg6rpB8V6SAMbeSpMisZMm22hBJfU 9vpNg4x4ytJK5ozr21AcjjHPxnH/UxNuOV14k4BlHWpqSfm73GuPqROkam2o+w5q8CXD ttxg== X-Forwarded-Encrypted: i=1; AJvYcCVWPTEiDd9fk3SbX4y3BbtX/01yrTSQrlkpz4S8pJkG/+Sp7eE1EJ2FAdBMUGPWpz6+EVdRuXpWQYUaUZSi2RZxawbwhHcGsO40fkk= X-Gm-Message-State: AOJu0Ywnt7aic8ahKEJw28o2/mjAhhLuwoIBIt0RQLW0GnmO7Nl0pMrU Sxox5B6/XuZs6uaaNBZQIss1QcGMsSv3ehE8bpKGOap6NSSa+NNiWE8nVCia5w== X-Google-Smtp-Source: AGHT+IFUXSL8PagKambUAn3JNk1vaS0WDwvdWErdRIsdpbLz+NRveFk3xgU/oe2NPDEqLe4EoIXhzg== X-Received: by 2002:a17:903:984:b0:1eb:7746:4248 with SMTP id d9443c01a7336-1ef440495abmr176201825ad.54.1715757466991; Wed, 15 May 2024 00:17:46 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:111d:a618:3172:cd5b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c136d53sm110941605ad.254.2024.05.15.00.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 00:17:46 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv4 21/21] Documentation/zram: add documentation for algorithm parameters Date: Wed, 15 May 2024 16:12:58 +0900 Message-ID: <20240515071645.1788128-22-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240515071645.1788128-1-senozhatsky@chromium.org> References: <20240515071645.1788128-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.