From patchwork Mon May 6 07:58:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655038 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6092140E3C for ; Mon, 6 May 2024 07:58:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982329; cv=none; b=Wlcn58LXvNMOhDxr2RPbCSDWB0hNNejVqxKlzUqCx21gXx4QMhBpO0AIdxtpHIsULHnEBPIPolybWifL+3ZUsA+OQ/dxMCcSYasXreAZj642a9FownfOL5jokLDx8oNSqzbTb5tAzpJCP/8PIwWbxm3Ev9bq+GzJ4PIN/8LJqKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982329; c=relaxed/simple; bh=8qI5x0BGUJs1JbjZreFI8Kvjdzxo5r4XEf1ADBnlo2w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RgXOiGnS/6zpJScoSY/R+rkvYWIrxy24Rrj4EqNfX5vWyQbO8KfgDZTwZ+UnZ6Q5S8Tivlt6n9z3haXFWLBUz3XvFfgXAkC7fcmJE9jgzLdaSh3mrIzFkT7uYHYozoqCTdVPu6YzjOKAkivYHPqM3bSwKCDc722E5O62dgvPrfg= 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=BZBz+OwT; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BZBz+OwT" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2b33d011e5dso998775a91.0 for ; Mon, 06 May 2024 00:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982327; x=1715587127; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cJ4glZLOzUHMCYOraHYCv1mLDw/pz7BmHlcE0TlYg0c=; b=BZBz+OwThD6D/zU9/9nwv+BNNYgiwc9i9siruGCOZAlnSoddro/SVI0lZmyjZvHYXV RJD9WnHfuZe25jxa1uXuTY3frQJ7Aioz4blp2J1uawzZ6p54AY370ETMW/+DGzNoUytk WM50+ZKeQWotxmTJp5qHSovJzABMvqbPfe8mE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982327; x=1715587127; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cJ4glZLOzUHMCYOraHYCv1mLDw/pz7BmHlcE0TlYg0c=; b=h2yj4ANIp4QJXrz+FizcRL8vbFqqgTb2lGS7FZeRjG8zP+/Fv6jMAolYKOuVLQ4CrF 1egvJmqEW7J57QQbSS35eTGy8Z8U/RjmccZ3tc/w941bBaYQWNeWduujy3iQ7ucbskkw M0IDCi2nFmUw56emAUuDFXw6rr0vAO0+IX/eg5fo/H337jtZNUzmUWZPZRicGmBQ3RQz iDPHPqQgjAry98XevaryGHJWb/+XFUpnLQ+JsZ+Ai9leAs7DN9JAZDL9v98hX9zzLQYC ldRjAtzOyr+7QRpwR3JWXb6AYeMjkW3S6TcDnOOlCvHOGwZdgPi4YAs7onUzTg3itNXP sxjw== X-Forwarded-Encrypted: i=1; AJvYcCUAJAccezj0Io0ZsXods9oO2mTTgjXkeG1Qr8WMeATzUVmAgC4YOxsEHFNNlH5qdaHHpIwYW6ig9dRu7vrw8woxbfZ3m59T/nXigpY= X-Gm-Message-State: AOJu0YxsDC/FN/gLl+EfkndA66/ePpNO1lCnzlW0CGlZZSPVRv8j+CxZ 0i9z0vKJ8XiFOUvUZx/Z8HOCG3jxNbUWAvwGEgLwaYHtgHM6DzNH0Vs98/ZRfQ== X-Google-Smtp-Source: AGHT+IE5iyQHAjHukQ7ulnyFaOjBB9hRZUTXAdL9e3p3WYQegSS+1BGu18UeI33TeJ5YXYjBVRoEIQ== X-Received: by 2002:a17:90a:880b:b0:2b1:be71:15d0 with SMTP id s11-20020a17090a880b00b002b1be7115d0mr17119940pjn.22.1714982326860; Mon, 06 May 2024 00:58:46 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58: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: [PATCHv2 01/17] zram: move from crypto API to custom comp backends API Date: Mon, 6 May 2024 16:58:14 +0900 Message-ID: <20240506075834.302472-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Crypto API is beautiful and powerful, however, being a generic API, it lacks support for fine-grained per-algorithm configuration. A number of compression algorithms provide various knobs to tune characteristics for particular data patterns. The simplest case is "compression level". A more complicated and interesting case is user-space trained dictionaries (e.g. lz4 and zstd). Moving to custom backends implementation gives us ability to have our own minimalistic and extendable API, and algorithms tunings becomes possible. The list of compression backends is empty at this point, we will add backends in the followup patches. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 39 +---------- drivers/block/zram/zcomp.c | 127 +++++++++++++--------------------- drivers/block/zram/zcomp.h | 26 +++++-- drivers/block/zram/zram_drv.c | 9 ++- 4 files changed, 73 insertions(+), 128 deletions(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 7b29cce60ab2..8ecb74f83a5e 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -2,7 +2,6 @@ config ZRAM tristate "Compressed RAM block device support" depends on BLOCK && SYSFS && MMU - depends on CRYPTO_LZO || CRYPTO_ZSTD || CRYPTO_LZ4 || CRYPTO_LZ4HC || CRYPTO_842 select ZSMALLOC help Creates virtual block devices called /dev/zramX (X = 0, 1, ...). @@ -15,45 +14,9 @@ config ZRAM See Documentation/admin-guide/blockdev/zram.rst for more information. -choice - prompt "Default zram compressor" - default ZRAM_DEF_COMP_LZORLE - depends on ZRAM - -config ZRAM_DEF_COMP_LZORLE - bool "lzo-rle" - depends on CRYPTO_LZO - -config ZRAM_DEF_COMP_ZSTD - bool "zstd" - depends on CRYPTO_ZSTD - -config ZRAM_DEF_COMP_LZ4 - bool "lz4" - depends on CRYPTO_LZ4 - -config ZRAM_DEF_COMP_LZO - bool "lzo" - depends on CRYPTO_LZO - -config ZRAM_DEF_COMP_LZ4HC - bool "lz4hc" - depends on CRYPTO_LZ4HC - -config ZRAM_DEF_COMP_842 - bool "842" - depends on CRYPTO_842 - -endchoice - config ZRAM_DEF_COMP string - default "lzo-rle" if ZRAM_DEF_COMP_LZORLE - default "zstd" if ZRAM_DEF_COMP_ZSTD - default "lz4" if ZRAM_DEF_COMP_LZ4 - default "lzo" if ZRAM_DEF_COMP_LZO - default "lz4hc" if ZRAM_DEF_COMP_LZ4HC - default "842" if ZRAM_DEF_COMP_842 + default "unset-value" config ZRAM_WRITEBACK bool "Write back incompressible or idle page to backing device" diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 8237b08c49d8..0d0d2e6dbaa9 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -15,31 +15,16 @@ #include "zcomp.h" -static const char * const backends[] = { -#if IS_ENABLED(CONFIG_CRYPTO_LZO) - "lzo", - "lzo-rle", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_LZ4) - "lz4", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC) - "lz4hc", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_842) - "842", -#endif -#if IS_ENABLED(CONFIG_CRYPTO_ZSTD) - "zstd", -#endif +static struct zcomp_backend *backends[] = { + NULL }; -static void zcomp_strm_free(struct zcomp_strm *zstrm) +static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) { - if (!IS_ERR_OR_NULL(zstrm->tfm)) - crypto_free_comp(zstrm->tfm); + if (zstrm->ctx) + comp->backend->destroy_ctx(zstrm->ctx); vfree(zstrm->buffer); - zstrm->tfm = NULL; + zstrm->ctx = NULL; zstrm->buffer = NULL; } @@ -47,60 +32,55 @@ static void zcomp_strm_free(struct zcomp_strm *zstrm) * Initialize zcomp_strm structure with ->tfm initialized by backend, and * ->buffer. Return a negative value on error. */ -static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp) +static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) { - zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0); + zstrm->ctx = comp->backend->create_ctx(); + /* * allocate 2 pages. 1 for compressed data, plus 1 extra for the * case when compressed size is larger than the original one */ zstrm->buffer = vzalloc(2 * PAGE_SIZE); - if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) { - zcomp_strm_free(zstrm); + if (!zstrm->ctx || !zstrm->buffer) { + zcomp_strm_free(comp, zstrm); return -ENOMEM; } return 0; } +static struct zcomp_backend *lookup_backend(const char *comp) +{ + int i = 0; + + while (backends[i]) { + if (sysfs_streq(comp, backends[i]->name)) + break; + i++; + } + return backends[i]; +} + bool zcomp_available_algorithm(const char *comp) { - /* - * Crypto does not ignore a trailing new line symbol, - * so make sure you don't supply a string containing - * one. - * This also means that we permit zcomp initialisation - * with any compressing algorithm known to crypto api. - */ - return crypto_has_comp(comp, 0, 0) == 1; + return lookup_backend(comp) != NULL; } /* show available compressors */ ssize_t zcomp_available_show(const char *comp, char *buf) { - bool known_algorithm = false; ssize_t sz = 0; int i; - for (i = 0; i < ARRAY_SIZE(backends); i++) { - if (!strcmp(comp, backends[i])) { - known_algorithm = true; + for (i = 0; i < ARRAY_SIZE(backends) - 1; i++) { + if (!strcmp(comp, backends[i]->name)) { sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "[%s] ", backends[i]); + "[%s] ", backends[i]->name); } else { sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "%s ", backends[i]); + "%s ", backends[i]->name); } } - /* - * Out-of-tree module known to crypto api or a missing - * entry in `backends'. - */ - if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1) - sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "[%s] ", comp); - - sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n"); return sz; } @@ -115,8 +95,8 @@ void zcomp_stream_put(struct zcomp *comp) local_unlock(&comp->stream->lock); } -int zcomp_compress(struct zcomp_strm *zstrm, - const void *src, unsigned int *dst_len) +int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int *dst_len) { /* * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized @@ -132,21 +112,19 @@ int zcomp_compress(struct zcomp_strm *zstrm, * the dst buffer, zram_drv will take care of the fact that * compressed buffer is too big. */ - *dst_len = PAGE_SIZE * 2; + size_t dlen = PAGE_SIZE * 2; + int ret; - return crypto_comp_compress(zstrm->tfm, - src, PAGE_SIZE, - zstrm->buffer, dst_len); + ret = comp->backend->compress(zstrm->ctx, src, zstrm->buffer, &dlen); + if (!ret) + *dst_len = dlen; + return ret; } -int zcomp_decompress(struct zcomp_strm *zstrm, - const void *src, unsigned int src_len, void *dst) +int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int src_len, void *dst) { - unsigned int dst_len = PAGE_SIZE; - - return crypto_comp_decompress(zstrm->tfm, - src, src_len, - dst, &dst_len); + return comp->backend->decompress(zstrm->ctx, src, src_len, dst); } int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) @@ -158,7 +136,7 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) zstrm = per_cpu_ptr(comp->stream, cpu); local_lock_init(&zstrm->lock); - ret = zcomp_strm_init(zstrm, comp); + ret = zcomp_strm_init(comp, zstrm); if (ret) pr_err("Can't allocate a compression stream\n"); return ret; @@ -170,7 +148,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node) struct zcomp_strm *zstrm; zstrm = per_cpu_ptr(comp->stream, cpu); - zcomp_strm_free(zstrm); + zcomp_strm_free(comp, zstrm); return 0; } @@ -199,32 +177,21 @@ void zcomp_destroy(struct zcomp *comp) kfree(comp); } -/* - * search available compressors for requested algorithm. - * allocate new zcomp and initialize it. return compressing - * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL) - * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in - * case of allocation error, or any other error potentially - * returned by zcomp_init(). - */ struct zcomp *zcomp_create(const char *alg) { struct zcomp *comp; int error; - /* - * Crypto API will execute /sbin/modprobe if the compression module - * is not loaded yet. We must do it here, otherwise we are about to - * call /sbin/modprobe under CPU hot-plug lock. - */ - if (!zcomp_available_algorithm(alg)) - return ERR_PTR(-EINVAL); - comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL); if (!comp) return ERR_PTR(-ENOMEM); - comp->name = alg; + comp->backend = lookup_backend(alg); + if (!comp->backend) { + kfree(comp); + return ERR_PTR(-EINVAL); + } + error = zcomp_init(comp); if (error) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index e9fe63da0e9b..757b85017e23 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -12,13 +12,26 @@ struct zcomp_strm { local_lock_t lock; /* compression/decompression buffer */ void *buffer; - struct crypto_comp *tfm; + void *ctx; +}; + +struct zcomp_backend { + int (*compress)(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len); + + int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst); + + void *(*create_ctx)(void); + void (*destroy_ctx)(void *ctx); + + const char *name; }; /* dynamic per-device compression frontend */ struct zcomp { struct zcomp_strm __percpu *stream; - const char *name; + struct zcomp_backend *backend; struct hlist_node node; }; @@ -33,10 +46,9 @@ void zcomp_destroy(struct zcomp *comp); struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); void zcomp_stream_put(struct zcomp *comp); -int zcomp_compress(struct zcomp_strm *zstrm, - const void *src, unsigned int *dst_len); - -int zcomp_decompress(struct zcomp_strm *zstrm, - const void *src, unsigned int src_len, void *dst); +int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int *dst_len); +int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, + const void *src, unsigned int src_len, void *dst); #endif /* _ZCOMP_H_ */ diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 4cf38f7d3e0a..298ef0e97e03 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1342,7 +1342,8 @@ static int zram_read_from_zspool(struct zram *zram, struct page *page, ret = 0; } else { dst = kmap_local_page(page); - ret = zcomp_decompress(zstrm, src, size, dst); + ret = zcomp_decompress(zram->comps[prio], zstrm, + src, size, dst); kunmap_local(dst); zcomp_stream_put(zram->comps[prio]); } @@ -1429,7 +1430,8 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) compress_again: zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]); src = kmap_local_page(page); - ret = zcomp_compress(zstrm, src, &comp_len); + ret = zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm, + src, &comp_len); kunmap_local(src); if (unlikely(ret)) { @@ -1616,7 +1618,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, num_recomps++; zstrm = zcomp_stream_get(zram->comps[prio]); src = kmap_local_page(page); - ret = zcomp_compress(zstrm, src, &comp_len_new); + ret = zcomp_compress(zram->comps[prio], zstrm, + src, &comp_len_new); kunmap_local(src); if (ret) { From patchwork Mon May 6 07:58:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655039 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 48D631411CF for ; Mon, 6 May 2024 07:58:50 +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=1714982332; cv=none; b=VzIta+L+o64DbVt2MfumihsfZZ9KY49SRS+W757c0HNtilWrJphd5vreZ3dzlc8FxLQ7NW80whsEUkaFE7p4Rw3U1OCMiECogKgJIBFbsE6P2e5QLfeu+QdPjnzTDwkmsaY1HwgEmBKl07dA5/iKTrlGJ5oO1CSsSOXXzT0f5dw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982332; c=relaxed/simple; bh=fwqjI0oJhsJ6IWcPj2TUi+YtZmx3QQeidZjvgSy1/xc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rl9Zns5Ul9Z9ipSjnu99IQ1hDkRJ2ivEipgHkZcri8ZYGrTvWiNT+Aq6GIX88H8n4h9gDNyCvehma3DUstAiulEMzJ4e1wlGtQEaK8/n55ahTGEEU4ks4D3chTey9NszwhqijbmO76QhnwB484/plGjigEbyPdbFvYJ5DfTETt8= 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=KzkdujRZ; 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="KzkdujRZ" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1ec69e3dbcfso9764775ad.0 for ; Mon, 06 May 2024 00:58:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982329; x=1715587129; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wUr6LRaWPPkzD2etszVDMza5JPEXKiMrJmq3EUCQIHI=; b=KzkdujRZdmF//QoBITEX4jredBIYFBEWx7AIDoJOHaLAZyqU9eno5yKpVXu5v2MmvO v2wTXafGOnMweH6YcUijDKbID5n4ESrZn73QGQ3n/gG284fcAevQxsq5KX1KpqGOUiC8 e4T1Xt0bKMuXjsxdqsvazFCUgyzUzf9XIb8cA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982329; x=1715587129; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wUr6LRaWPPkzD2etszVDMza5JPEXKiMrJmq3EUCQIHI=; b=CChjqIRQJ9edcqNjanuzY9loBLuWlUZyuHer4S2+qBtpfsqALsnDmEOEz17F85u0xe 65xV0CscPs0yJd1lBbVDz6z1xjpG3xP84lqFy2mYeCkeSDGy6or8SyKfdapCIfSMr0GE lCZw/EOJNZ3/wdwg4LnjQEnw6Bm9ijMQ1TwTL130W4rtJaU4HknZg2K510Vet8nlCEIm hLrmmmchkF/9pTtf5cAxmvqEm9zJjoqPfcdzijlBU6WqTSWOb1rCFdvcnrQZkvEWv/Zy aSde0IaIAibTRP5QNHwA6tIJMt9Oa0PPAQCqctbENnXC8yZeq4X7nZnTlnQjUPD6cjiR sehg== X-Forwarded-Encrypted: i=1; AJvYcCUMuXiEnpxtb2p6xi4KckA4zCi5siJorSWkCcDYgD7fdHhYxWPvF1e2/Lgg0dKI2Lay13+k+iLtMl7O3iWM3RpLyJTeQ1ptFzcmiik= X-Gm-Message-State: AOJu0YzosD56sUcsSSEkj9UM1wmyqk9PvHfo0RqC4dGNprNr4SmcaLcb OqE19A9g4YWWhLNjij6w6Lsg5GxE9ZOAi0O2bRYzAXOus1rJY1BwYRuAXYqnRg== X-Google-Smtp-Source: AGHT+IE750YP2LajwswRiaw4bJrU4J8mGAKK7f8a+FAwgPbMcbI935faiFbWo4I7VSXEC0hWyn5oFw== X-Received: by 2002:a17:902:ecc5:b0:1eb:d7ce:1946 with SMTP id a5-20020a170902ecc500b001ebd7ce1946mr9742490plh.52.1714982329560; Mon, 06 May 2024 00:58:49 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58:49 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 02/17] zram: add lzo and lzorle compression backends support Date: Mon, 6 May 2024 16:58:15 +0900 Message-ID: <20240506075834.302472-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 23 +++++++++++++++ drivers/block/zram/Makefile | 3 ++ drivers/block/zram/backend_lzo.c | 44 +++++++++++++++++++++++++++++ drivers/block/zram/backend_lzo.h | 10 +++++++ drivers/block/zram/backend_lzorle.c | 44 +++++++++++++++++++++++++++++ drivers/block/zram/backend_lzorle.h | 10 +++++++ drivers/block/zram/zcomp.c | 7 +++++ 7 files changed, 141 insertions(+) create mode 100644 drivers/block/zram/backend_lzo.c create mode 100644 drivers/block/zram/backend_lzo.h create mode 100644 drivers/block/zram/backend_lzorle.c create mode 100644 drivers/block/zram/backend_lzorle.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 8ecb74f83a5e..5d329a887b12 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -14,8 +14,31 @@ config ZRAM See Documentation/admin-guide/blockdev/zram.rst for more information. +config ZRAM_BACKEND_LZO + bool "lzo and lzo-rle compression support" + depends on ZRAM + select LZO_COMPRESS + select LZO_DECOMPRESS + +choice + prompt "Default zram compressor" + default ZRAM_DEF_COMP_LZORLE + depends on ZRAM + +config ZRAM_DEF_COMP_LZORLE + bool "lzo-rle" + depends on ZRAM_BACKEND_LZO + +config ZRAM_DEF_COMP_LZO + bool "lzo" + depends on ZRAM_BACKEND_LZO + +endchoice + config ZRAM_DEF_COMP string + default "lzo-rle" if ZRAM_DEF_COMP_LZORLE + default "lzo" if ZRAM_DEF_COMP_LZO default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index de9e457907b1..2dcbc9b75d91 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o + zram-y := zcomp.o zram_drv.o obj-$(CONFIG_ZRAM) += zram.o diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c new file mode 100644 index 000000000000..b88b408964cd --- /dev/null +++ b/drivers/block/zram/backend_lzo.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include "backend_lzo.h" + +static void *lzo_create(void) +{ + return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); +} + +static void lzo_destroy(void *ctx) +{ + kfree(ctx); +} + +static int lzo_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, ctx); + return ret == LZO_E_OK ? 0 : ret; +} + +static int lzo_decompress(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + size_t dst_len = PAGE_SIZE; + int ret; + + ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len); + return ret == LZO_E_OK ? 0 : ret; +} + +struct zcomp_backend backend_lzo = { + .compress = lzo_compress, + .decompress = lzo_decompress, + .create_ctx = lzo_create, + .destroy_ctx = lzo_destroy, + .name = "lzo", +}; diff --git a/drivers/block/zram/backend_lzo.h b/drivers/block/zram/backend_lzo.h new file mode 100644 index 000000000000..377ccb7389e2 --- /dev/null +++ b/drivers/block/zram/backend_lzo.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZO_H__ +#define __BACKEND_LZO_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lzo; + +#endif /* __BACKEND_LZO_H__ */ diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c new file mode 100644 index 000000000000..9bf1843021b0 --- /dev/null +++ b/drivers/block/zram/backend_lzorle.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include "backend_lzorle.h" + +static void *lzorle_create(void) +{ + return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); +} + +static void lzorle_destroy(void *ctx) +{ + kfree(ctx); +} + +static int lzorle_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = lzorle1x_1_compress(src, PAGE_SIZE, dst, dst_len, ctx); + return ret == LZO_E_OK ? 0 : ret; +} + +static int lzorle_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + size_t dst_len = PAGE_SIZE; + int ret; + + ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len); + return ret == LZO_E_OK ? 0 : ret; +} + +struct zcomp_backend backend_lzorle = { + .compress = lzorle_compress, + .decompress = lzorle_decompress, + .create_ctx = lzorle_create, + .destroy_ctx = lzorle_destroy, + .name = "lzo-rle", +}; diff --git a/drivers/block/zram/backend_lzorle.h b/drivers/block/zram/backend_lzorle.h new file mode 100644 index 000000000000..5c1db65a38a4 --- /dev/null +++ b/drivers/block/zram/backend_lzorle.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZORLE_H__ +#define __BACKEND_LZORLE_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lzorle; + +#endif /* __BACKEND_LZORLE_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 0d0d2e6dbaa9..58fb3ac91f4b 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -15,7 +15,14 @@ #include "zcomp.h" +#include "backend_lzo.h" +#include "backend_lzorle.h" + static struct zcomp_backend *backends[] = { +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) + &backend_lzorle, + &backend_lzo, +#endif NULL }; From patchwork Mon May 6 07:58:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655040 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 7F6151420B0 for ; Mon, 6 May 2024 07:58:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982333; cv=none; b=sJGGwnafB2r/6Sd5/gf/xocOqLktf9NDf38+d2KCStfgfHIMWP2bdW8pea3WSslMmUgsOPwIA3tK/ZFFaMw0wwWyvvacpnOK9Va/HWOa5YNSJzAtB8fnFP0vCooXWozBf/h+oAuBD+6dfp2fuKJcS1fUQ/Z275YwYwZMz+ZQUpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982333; c=relaxed/simple; bh=DHL8kqd+RNQDM+Stwrc0IUTkOPk9/Cbf5eFqQlFrF4o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tRgebMV+SEKgjZMKGBJ+j3DIuEBz8UNU1OkOJcYQ8MCURszTI4QZyMnCBKFW8Jl+imu7I+PLAOCZkzgIR148XydqV1uMkqAiUT8MGTB6Zfq05W+xCVjcL6iFIedUMxlpVyvgV/Q+WzIVs6mKM3GOL87Ps/TUclSP3j4wdAUM0iE= 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=NDAumOY3; arc=none smtp.client-ip=209.85.214.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="NDAumOY3" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1ed96772f92so8352865ad.0 for ; Mon, 06 May 2024 00:58:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982332; x=1715587132; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sjjXoB1vjVFge1iRvQDKKfu041t9IBLi05g+C6qi2oA=; b=NDAumOY3qZxJybc8pZ5s2t/ZA+b1RU5Ed4oCuTCxaiwEerlRJ/c/QHxxy5EllLQb/g +dCK9aGgYaKwj948XMxYWs75RVHL5XpEmLYDHYbZIbXIr7SgmUUig8eD+q+EfUqtKv4m 4uDMYt7AHEAdnotry4HzZrV2HG6exIhKF5SDk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982332; x=1715587132; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sjjXoB1vjVFge1iRvQDKKfu041t9IBLi05g+C6qi2oA=; b=YyZ31iG1mDxiz45faB22dI1DFUoijs5NXAJdCmqUZlEt5tDbskaJxvpLGriVJDf9ef g8ldXpKYNEKvru6jHYKyC/0SRQlbKDuH4tKHkaGjNePNTibCLX7Magm7hQxhK1im/pQe 3LHVqJflAYSwIA1PzD2SBM50H6F/+O2KhJAfhrZfFRsTOo2FmTNXCFpoVITyyXw1xbpE eNX4MX2Q58M4jLWazT3KmHbLIAtCG/wFiWE/tIS218v/bpipGK/KhGl29xxfleHnmizC cj35TtD6DTWzlhpHh5PhVXVhfMi8t0EO3TW6zycCDFpirfHF5hh6IYgXOb+lwWL/j/MC +rUg== X-Forwarded-Encrypted: i=1; AJvYcCX23uMdq9y0lS0wMpEejVXaxjPYWq9h2b0zFE+av850g1kTRE7Ry1iOjvQpxw8fnJ46GHjxiptpA6/IXEnIyc2ZWplL5THf8oNJWCg= X-Gm-Message-State: AOJu0Ywjj++IZfFunfJu7Gd7Gyx/rV6KQBNO7l21h3qRVB8EygtP5tlo Jwg5AS+U7ONumJBcm8IzNzL+ey4shhWQ7xKDUXP+yvvJpSziQD/suOqSTiIxuPeP9bS9cVMN6Vc = X-Google-Smtp-Source: AGHT+IFWV+X5WTs3i4V83BUitVus7BW/4D3casQEhHM7fCsP3ic5Ug3u6WiAUmdFhaCHi+CDp3mW3g== X-Received: by 2002:a17:903:18d:b0:1dd:c288:899f with SMTP id z13-20020a170903018d00b001ddc288899fmr11773369plg.18.1714982331935; Mon, 06 May 2024 00:58:51 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58:51 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 03/17] zram: add lz4 compression backend support Date: Mon, 6 May 2024 16:58:16 +0900 Message-ID: <20240506075834.302472-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_lz4.c | 47 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4.h | 10 +++++++ drivers/block/zram/zcomp.c | 4 +++ 5 files changed, 73 insertions(+) create mode 100644 drivers/block/zram/backend_lz4.c create mode 100644 drivers/block/zram/backend_lz4.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 5d329a887b12..f1e76fc8431a 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -20,6 +20,12 @@ config ZRAM_BACKEND_LZO select LZO_COMPRESS select LZO_DECOMPRESS +config ZRAM_BACKEND_LZ4 + bool "lz4 compression support" + depends on ZRAM + select LZ4_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -33,12 +39,17 @@ config ZRAM_DEF_COMP_LZO bool "lzo" depends on ZRAM_BACKEND_LZO +config ZRAM_DEF_COMP_LZ4 + bool "lz4" + depends on ZRAM_BACKEND_LZ4 + endchoice config ZRAM_DEF_COMP string default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO + default "lz4" if ZRAM_DEF_COMP_LZ4 default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 2dcbc9b75d91..1be5d2657960 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c new file mode 100644 index 000000000000..697592dbabe2 --- /dev/null +++ b/drivers/block/zram/backend_lz4.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "backend_lz4.h" + +static void *lz4_create(void) +{ + return vmalloc(LZ4_MEM_COMPRESS); +} + +static void lz4_destroy(void *ctx) +{ + vfree(ctx); +} + +static int lz4_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + int ret; + + ret = LZ4_compress_default(src, dst, PAGE_SIZE, *dst_len, ctx); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + int dst_len = PAGE_SIZE; + int ret; + + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (ret < 0) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_lz4 = { + .compress = lz4_compress, + .decompress = lz4_decompress, + .create_ctx = lz4_create, + .destroy_ctx = lz4_destroy, + .name = "lz4", +}; diff --git a/drivers/block/zram/backend_lz4.h b/drivers/block/zram/backend_lz4.h new file mode 100644 index 000000000000..a5fb5564835c --- /dev/null +++ b/drivers/block/zram/backend_lz4.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4_H__ +#define __BACKEND_LZ4_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4; + +#endif /* __BACKEND_LZ4_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 58fb3ac91f4b..902bdaf7e299 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -17,11 +17,15 @@ #include "backend_lzo.h" #include "backend_lzorle.h" +#include "backend_lz4.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) &backend_lzorle, &backend_lzo, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) + &backend_lz4, #endif NULL }; From patchwork Mon May 6 07:58:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655041 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 5F971140E2E for ; Mon, 6 May 2024 07:58:55 +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=1714982336; cv=none; b=CBQBs1vfVQ9umuNFWP37nTdt4sy9DTIW6qIUQ2IvRjnCCCR+KVBcxU5jUmQWSXroquGI+2c8c587AqnNCT2WiJnRWx9hxUAfimYy9s6sBshsZwloHEB2lwlWo6TWMvF2RJ0pIOWzQN78cgb4ADrujV9cpfWH9gAzWqMvIFRC2f0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982336; c=relaxed/simple; bh=8iuEInzIc8LpvQjmTn+1sUuxsPZjdX9h9ql/8Ate56k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TomALWQ6FyZr1rFv2D/WcdKiTjhlWGYJhP/Eq6FzC8ZywxYbc4qdxHEQUoSviIe9CjDKGsqBx5vGuu1hQd/VO4Doq+h19mrbfZKdmvZN+1sirLxnrs7IKsOasdpnoF7ldmGIODS8YvMtMnnrvRW/1kkKYYHdRTjdauhFkdKEpw4= 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=V+xbILaB; 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="V+xbILaB" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1edf506b216so3882265ad.2 for ; Mon, 06 May 2024 00:58:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982335; x=1715587135; 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=AqdQ0YWyhNoNXU+2X1vjHodTilUScpd/3IsqcqsqLiU=; b=V+xbILaBJEiuWm2uqTe3IfxciTcIXcRJ+U3p1WCfEzCfKfhDQrmJ/+q1O1u52g2hbP F0fVFN1px9FMONAQZSFuLwi7AfRNpPhlBSpuqAVtda4CCtHCROMal0vnZwjCPLrzuhWf Nfj2Hja+ZVq8PSYgNHAx0Rn2wm6hVcGfuYses= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982335; x=1715587135; 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=AqdQ0YWyhNoNXU+2X1vjHodTilUScpd/3IsqcqsqLiU=; b=fodzobJBjlbk7AJ3a2m+6CJIld9W9BmQ5qA4ovgwW+9A9F8p3m85NzMD8uyAu2wOlL tgGG+2/hAEfTeWtYzRpF04n99fh9Zcdseo/Rm8Dw2rpeJgxgVLJ3lJ/lh1gyL7VF5Kit R4WQnzuVKMCYQJDlpNy23mjtkf1cg7sFQBJ/ypVWlrEAXXmKRdP72pn4U6vMbVApGG+B KFX93uNasNgR2IuKhihWZC/VMnUPP9iV0Dvf5OkSQ9BJcsatkoT1B9okDxIo2QdjjGO5 fwgLhoscX2wyPVODFToeZU2Xq3fFti1oNPdccuvUKuHRU1PxevCTUZBcQIBktiaUJ5OV k+UQ== X-Forwarded-Encrypted: i=1; AJvYcCVNDfX1zGxFy2CiDbeN6fWVebs4QwrsEdrdR0pdIAF8COQYlqQvVL3DM0BXEoqGoHGsWdIKPM72F+5WCu8dSPEiSRDLCrRSqR/eh1c= X-Gm-Message-State: AOJu0YydcZb+CYNpZBVeK7VAIZ/Az7b0u/M2w/mfmbvw54iS8L+j9ui6 DtfaGi2NXhj7h3Xr/knyndgigsJN73Z6xjgqE0rtLD62WMirWC8ds3fxVIiqgw== X-Google-Smtp-Source: AGHT+IHZhQ4b8tdZyzaV0NrXU0v8rY+bsI60IyNZYj8K0IP2NFhpywlMWf7NCyQJdVaOVElcnD70Jw== X-Received: by 2002:a17:902:be08:b0:1e7:b6f4:971 with SMTP id r8-20020a170902be0800b001e7b6f40971mr7734763pls.27.1714982334810; Mon, 06 May 2024 00:58:54 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58:54 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 04/17] zram: add lz4hc compression backend support Date: Mon, 6 May 2024 16:58:17 +0900 Message-ID: <20240506075834.302472-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++++ drivers/block/zram/Makefile | 5 ++- drivers/block/zram/backend_lz4hc.c | 72 ++++++++++++++++++++++++++++++ drivers/block/zram/backend_lz4hc.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 drivers/block/zram/backend_lz4hc.c create mode 100644 drivers/block/zram/backend_lz4hc.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index f1e76fc8431a..0d890a8d2dc4 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -26,6 +26,12 @@ config ZRAM_BACKEND_LZ4 select LZ4_COMPRESS select LZ4_DECOMPRESS +config ZRAM_BACKEND_LZ4HC + bool "lz4hc compression support" + depends on ZRAM + select LZ4HC_COMPRESS + select LZ4_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -43,6 +49,10 @@ config ZRAM_DEF_COMP_LZ4 bool "lz4" depends on ZRAM_BACKEND_LZ4 +config ZRAM_DEF_COMP_LZ4HC + bool "lz4hc" + depends on ZRAM_BACKEND_LZ4HC + endchoice config ZRAM_DEF_COMP @@ -50,6 +60,7 @@ config ZRAM_DEF_COMP default "lzo-rle" if ZRAM_DEF_COMP_LZORLE default "lzo" if ZRAM_DEF_COMP_LZO default "lz4" if ZRAM_DEF_COMP_LZ4 + default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 1be5d2657960..815b45471c7d 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c new file mode 100644 index 000000000000..5c437623aa65 --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include "backend_lz4hc.h" + +struct lz4hc_ctx { + void *mem; + s32 level; +}; + +static void lz4hc_destroy(void *ctx) +{ + struct lz4hc_ctx *zctx = ctx; + + vfree(zctx->mem); + kfree(zctx); +} + +static void *lz4hc_create(void) +{ + struct lz4hc_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx->mem) { + lz4hc_destroy(ctx); + return NULL; + } + + /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ + ctx->level = LZ4HC_DEFAULT_CLEVEL; + return ctx; +} + +static int lz4hc_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct lz4hc_ctx *zctx = ctx; + int ret; + + ret = LZ4_compress_HC(src, dst, PAGE_SIZE, *dst_len, + zctx->level, zctx->mem); + if (!ret) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int lz4hc_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + int dst_len = PAGE_SIZE; + int ret; + + ret = LZ4_decompress_safe(src, dst, src_len, dst_len); + if (ret < 0) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_lz4hc = { + .compress = lz4hc_compress, + .decompress = lz4hc_decompress, + .create_ctx = lz4hc_create, + .destroy_ctx = lz4hc_destroy, + .name = "lz4hc", +}; diff --git a/drivers/block/zram/backend_lz4hc.h b/drivers/block/zram/backend_lz4hc.h new file mode 100644 index 000000000000..29c428a850e2 --- /dev/null +++ b/drivers/block/zram/backend_lz4hc.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_LZ4HC_H__ +#define __BACKEND_LZ4HC_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_lz4hc; + +#endif /* __BACKEND_LZ4HC_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 902bdaf7e299..f04f5844a23c 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -18,6 +18,7 @@ #include "backend_lzo.h" #include "backend_lzorle.h" #include "backend_lz4.h" +#include "backend_lz4hc.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -26,6 +27,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) &backend_lz4, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) + &backend_lz4hc, #endif NULL }; From patchwork Mon May 6 07:58:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655042 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.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 CEEC3142629 for ; Mon, 6 May 2024 07:58:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982341; cv=none; b=oyuo1eGxO9C+y4KYptmo6+55vAQLfa9uNncA1du1ZY/8eu+Zv/Jojfw45kepy32mD6q43NULAfN2I3avP6wVdaM2kzXr3uCNhZmLCIHlv0V8qUwMNgUGPw/UTxKvYhOxRYMLmsSnj41aberQN3Q0TEcf8Lccj9NkHdHyV0rX0lo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982341; c=relaxed/simple; bh=UjfGmtLHYc+ZtfDBltxPDecVYYzszPmAvKAkfS30jKk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nx0YNMX9Lg5IQUNibG/dVCPRXFHZKOFYOl0J/ibB5b9FAOymcZ0dqCF4U/IDIzv6qrAInoeY3dhREjXNYCzpn5+6I9+mueqwb4ubB1aJ78BnPmcTX7vfBVOCm4KmYtqC6vFCi/ZR8R4maZOOYemtzcKc0OWQ7xTUYfjc/wCFpEs= 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=NKQQ2C1h; arc=none smtp.client-ip=209.85.210.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="NKQQ2C1h" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-6f45f1179c3so790374b3a.3 for ; Mon, 06 May 2024 00:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982337; x=1715587137; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xs/oZbuIhbdeWVSnm1Y7/6OB6aLjJFlUrJpqF9jHmLk=; b=NKQQ2C1h5hvGqTvKG1hv2Zwv9i/g+7Kn5RkUgFJXp0o7olst5gJRgXDBy0T6uCZTi+ LlSPvg09PSDuYFdzmHexDsar0An9UmSnJi8De/bIP3EajXAYVpJX+0BAT/IpkAvWY2Hf l2OF2xh3Z4+bRVE1YCbwE5TuajaGSEqwV5AvU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982337; x=1715587137; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xs/oZbuIhbdeWVSnm1Y7/6OB6aLjJFlUrJpqF9jHmLk=; b=DoAucMcYXemN2b7NhTZoABBha+E8T38Vhe4UykrObPZ2TOW/tOZx8OhIvIweyzdp3S hIqrX+f+eThZ7j5Fo3yb7ZbTc/+sWW/6hZSXdTReXc0J301SvAGLLgUWUTsnQP1ybTmc 6xtM0rXQ6vphZTGMEdqo6VEn+GSHLH7sztWav4YYs4otAIQZHoIO2mAztNf6M3aTEr/0 +QLqbd2vSCYZ3vl04gpRS07wQXgt0IRVY/93c/HgQDXnooO2NGjIqaOXJU3Qdzp7XqAM cbakP+n0eDgJCEBUWvqDI8DTGjMG8V7m1knmxiXtkEAlesOLn3zDV84Zw8jg+eNaDU2T Py1g== X-Forwarded-Encrypted: i=1; AJvYcCUFJfvQinsnUoZIvfFQQissCF8nTLIMXswVtqYFf6HfXx9s+zDHJlvSp92lWILGUTsumyxCB4ZYhvPF/uj0BcAA/yBTUbggyxbB9Ps= X-Gm-Message-State: AOJu0YwnmCM4mI/FtUraq32EgJ6CgS1Mev//sRdOMjLMhafoU2bKhwZg rlS64b7L+J+D0LxbxnMX4ByhChJxnIcPBJsU+Gx+UoatmjG1RBHqTeJc1VVkBQ== X-Google-Smtp-Source: AGHT+IGy/4lD/Gu1ZGaiNMwKg7zR1w+EoUSMKcYS2iKj3KJder89nFxCHISjC6pco3raQDM3dE7LMg== X-Received: by 2002:a05:6a20:c90d:b0:1af:9321:8ac3 with SMTP id gx13-20020a056a20c90d00b001af93218ac3mr4829178pzb.36.1714982337219; Mon, 06 May 2024 00:58:57 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58:56 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 05/17] zram: add zstd compression backend support Date: Mon, 6 May 2024 16:58:18 +0900 Message-ID: <20240506075834.302472-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_zstd.c | 97 +++++++++++++++++++++++++++++++ drivers/block/zram/backend_zstd.h | 10 ++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 123 insertions(+) create mode 100644 drivers/block/zram/backend_zstd.c create mode 100644 drivers/block/zram/backend_zstd.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 0d890a8d2dc4..71cd0d5d8f35 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -32,6 +32,12 @@ config ZRAM_BACKEND_LZ4HC select LZ4HC_COMPRESS select LZ4_DECOMPRESS +config ZRAM_BACKEND_ZSTD + bool "zstd compression support" + depends on ZRAM + select ZSTD_COMPRESS + select ZSTD_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -53,6 +59,10 @@ config ZRAM_DEF_COMP_LZ4HC bool "lz4hc" depends on ZRAM_BACKEND_LZ4HC +config ZRAM_DEF_COMP_ZSTD + bool "zstd" + depends on ZRAM_BACKEND_ZSTD + endchoice config ZRAM_DEF_COMP @@ -61,6 +71,7 @@ config ZRAM_DEF_COMP default "lzo" if ZRAM_DEF_COMP_LZO default "lz4" if ZRAM_DEF_COMP_LZ4 default "lz4hc" if ZRAM_DEF_COMP_LZ4HC + default "zstd" if ZRAM_DEF_COMP_ZSTD default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 815b45471c7d..053fe35e346b 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o +obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c new file mode 100644 index 000000000000..4da49626f110 --- /dev/null +++ b/drivers/block/zram/backend_zstd.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_zstd.h" + +struct zstd_ctx { + zstd_cctx *cctx; + zstd_dctx *dctx; + void *cctx_mem; + void *dctx_mem; + s32 level; +}; + +static void zstd_destroy(void *ctx) +{ + struct zstd_ctx *zctx = ctx; + + vfree(zctx->cctx_mem); + vfree(zctx->dctx_mem); + kfree(zctx); +} + +static void *zstd_create(void) +{ + zstd_parameters params; + struct zstd_ctx *ctx; + size_t sz; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->level = ZSTD_defaultCLevel(); + params = zstd_get_params(ctx->level, 0); + sz = zstd_cctx_workspace_bound(¶ms.cParams); + ctx->cctx_mem = vzalloc(sz); + if (!ctx->cctx_mem) + goto error; + + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); + if (!ctx->cctx) + goto error; + + sz = zstd_dctx_workspace_bound(); + ctx->dctx_mem = vzalloc(sz); + if (!ctx->dctx_mem) + goto error; + + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); + if (!ctx->dctx) + goto error; + + return ctx; + +error: + zstd_destroy(ctx); + return NULL; +} + +static int zstd_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct zstd_ctx *zctx = ctx; + const zstd_parameters params = zstd_get_params(zctx->level, 0); + size_t ret; + + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, ¶ms); + if (zstd_is_error(ret)) + return -EINVAL; + *dst_len = ret; + return 0; +} + +static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + struct zstd_ctx *zctx = ctx; + size_t ret; + + ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); + if (zstd_is_error(ret)) + return -EINVAL; + return 0; +} + +struct zcomp_backend backend_zstd = { + .compress = zstd_compress, + .decompress = zstd_decompress, + .create_ctx = zstd_create, + .destroy_ctx = zstd_destroy, + .name = "zstd", +}; diff --git a/drivers/block/zram/backend_zstd.h b/drivers/block/zram/backend_zstd.h new file mode 100644 index 000000000000..75d2d2c02768 --- /dev/null +++ b/drivers/block/zram/backend_zstd.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_ZSTD_H__ +#define __BACKEND_ZSTD_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_zstd; + +#endif /* __BACKEND_ZSTD_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index f04f5844a23c..c16eb038f608 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -19,6 +19,7 @@ #include "backend_lzorle.h" #include "backend_lz4.h" #include "backend_lz4hc.h" +#include "backend_zstd.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -30,6 +31,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) &backend_lz4hc, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) + &backend_zstd, #endif NULL }; From patchwork Mon May 6 07:58:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655043 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 35C671411C1 for ; Mon, 6 May 2024 07:58: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=1714982342; cv=none; b=kKKmHvdry5/Q3QVy5Dzp+pzd8X89BIOFvT9WXErGbGfdZEFOOVIiwL1ZrxRxfLNwI2OjbU4o9JX70dZDKvqECgzmiEbdg1RK9/x8ZAAR+m93A8A9ewYpygfmA4+2aBGnF8QjJuJ5As6nISemN+NG5Za8jNBepdeKSAPgFhxdQFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982342; c=relaxed/simple; bh=FRBhLTt/FHOdPdldPK2JgCScJh/quKT94FOX0NLvGcs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o7vkCDfhrIqfuS11NL0yek092vNC4KbmVX4Ux59ZcIYDpxC1E4tKoBadETtepa+LMkbITl2Q6PYMCbigpDGRcuf4xWcavYyX8cH54nIFrKe7CEJFzxNjgp5WNVpPHUKZSZc/NJjM6V2eQba1qbgIE9FgXQQeWC8IX38aSgR5/yQ= 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=USREE9Ki; 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="USREE9Ki" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1ee0132a6f3so5200345ad.0 for ; Mon, 06 May 2024 00:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982339; x=1715587139; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/EvmWJu/VXtBqzoQye3a2VzbJm8fhR7v5ZjISdbrAiY=; b=USREE9KiozbUeQsQtYFP9rLh8fk4Ns4PqZvLqOy3oo9CudM3ZLfhQDK7fnHSIJf5ZW FD+61fFvZJFVYt1le59UJotSQ5z1fDxyr+uAw/+587HkxABdvxvZqrH0dvxdi98klJx5 yM1gG2/vg3kGKvdLlB15zr+sWaYrUmhQnR5JQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982339; x=1715587139; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/EvmWJu/VXtBqzoQye3a2VzbJm8fhR7v5ZjISdbrAiY=; b=Q+S72C1YKYS8lZbuOwEbrcsPY6Z1bxFhvcqLEA+TCOHP0VjVdDUvYAAoZbcE5SgqEz 2zyBXeb9x0QFnWB/pBip7Kazb3/UBpfqA0vQ8WRm1woANUSVYpZKcT/oijigitKnr7PC ZUAnCJ0SMkimPg3Tntc1i9eXKPEkNmcR/gfI/EX1O6yCMa8RHpFGDVxyKbhym1RUjYDu nXs4Z5jOb2r4iiODFO9mz6AzeglsPAzHBiDRm7xvrwllRwaLpBaNBW/11lU4H9NJoY3B CHUvr8qsgYVjx+dY2aA6g2yyw9X+DGWGRVSRkIBm/gvq0ioIn0BDR7gf/zuVAax+JMQ3 auCg== X-Forwarded-Encrypted: i=1; AJvYcCVWLS3HIaCHVpEnfEAO4WuZYIYyb+Ezdxxyfc7BGx+MZwP/TcSQrEVrs3FN3U7Qd9JDXcx9tbO+1wJ2WkuDr4WIKL6DHzB8/mMf9yk= X-Gm-Message-State: AOJu0Yw5UL8//S5IiTA9lKvEmV2nbrj7y9NCWNlpQ3+TgE5naOKJcnTQ FBCshwKbMqVg5EpPTXCljUa9vztpEFgqn2zL6ZFmhK3NfRUmXojlpIJbQKztn02i34JKGeDfuCU = X-Google-Smtp-Source: AGHT+IHAlAMMb1xOBQ8h5iXt0t3MILo5VpT1GAqE/mOolH78hccZi0QsEujFUvP+FM022nf/kSp60g== X-Received: by 2002:a17:903:2349:b0:1e5:3c5:55a5 with SMTP id c9-20020a170903234900b001e503c555a5mr10089448plh.8.1714982339556; Mon, 06 May 2024 00:58:59 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:58:59 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 06/17] zram: pass estimated src size hint to zstd Date: Mon, 6 May 2024 16:58:19 +0900 Message-ID: <20240506075834.302472-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 zram works with PAGE_SIZE buffers, so we always know exact size of the source buffer and hence can pass estimated_src_size to zstd_get_params(). This hint on x86_64, for example, reduces the size of the work memory buffer from 1303520 bytes down to 90080 bytes. Given that compression streams are per-CPU that's quite some memory saving. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 4da49626f110..4a7734aa1a8a 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -35,7 +35,7 @@ static void *zstd_create(void) return NULL; ctx->level = ZSTD_defaultCLevel(); - params = zstd_get_params(ctx->level, 0); + params = zstd_get_params(ctx->level, PAGE_SIZE); sz = zstd_cctx_workspace_bound(¶ms.cParams); ctx->cctx_mem = vzalloc(sz); if (!ctx->cctx_mem) @@ -65,7 +65,7 @@ static int zstd_compress(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len) { struct zstd_ctx *zctx = ctx; - const zstd_parameters params = zstd_get_params(zctx->level, 0); + const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, From patchwork Mon May 6 07:58:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655044 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 27B801428FD for ; Mon, 6 May 2024 07:59:02 +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=1714982344; cv=none; b=W01VkD7RCiKa2tuON4b5ZDO8vZsn1i9eEWeCduEwbJ3ylFq0LvALuFqLrOaVR5BwkN54HvGwuUBknVtVqGaFCTT1xG4vi2kh45SLltFov6G0ghOzOppjAf3sGkekwJKt5dYrKBfZfQUBALM2pBu9uu95jTCQhim/Mx8+sk8Ltlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982344; c=relaxed/simple; bh=QlfqxCVzGVqjuU78RUVl7laME8V3Egjqi1IedM7APCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XnN8XcIz51jcot9IaAAZbai9FW8F/kdv+mDROqGHFTsFsgIyjrJ86Tl2aq4/DsxSUXI9TT5kOEhg1rKhtMbIzBEPXpR6nIgqOiRvR84RqeNM+y+XphpRPj2mARvt0bhboWvH63s5hRny4XCmQ2FjNmr6krccOmRf9y9Odj4Ghd4= 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=Y+002BZz; 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="Y+002BZz" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1e3ca546d40so10725185ad.3 for ; Mon, 06 May 2024 00:59:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982342; x=1715587142; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=II/I/hgLmJhm0CjvuLYW2Y62FUnxRGP17kgZqYB2Ofs=; b=Y+002BZzzrT6iVTRt/cxg+Zatq7JALmOKexRbqagtXPFJxtHopU1Em9eLX5om8hGvS GvS+/3BxIZwRM/MVycB2OlV6CehbEcpFZRoDxWOabZpbUckcY8rVnN86k9lGLqKNrerd k8U2oOob7nYdoj8GMQlhjRgwCmwfPGbyz5lPQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982342; x=1715587142; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=II/I/hgLmJhm0CjvuLYW2Y62FUnxRGP17kgZqYB2Ofs=; b=p678orv/3oUzYzBsDUWp4r8ADjXXhITUW30yY7fjVkWo96ncwaBOm4om6GAUPN8bxA mbLzRfRFkju0hZn6XmdxG+dT3VFPAluT6bzVjySm4zaXl9R3qXxIXNRJlkIUIUAnwU/l GgT4MElt21YF1waeSjctwsCknD/uAMMwLvSN+EHNnXqHxcOI483ybX7IPd/6WHbIB01t YlnOUGLmTrI75kRYOE4PhvTGBz9WkSzs9ukw5g63+hoP4pTVm3eNilQrjLE235pPZb5u DNcKl2UoY+FwKZ+ffsJzxGPCvl/ujKmzrUlFePIpe7WfD4p2Vl8SiYbc29kOUpf0Wad6 x8yg== X-Forwarded-Encrypted: i=1; AJvYcCW1v+QdPkgNjNeQID3lAJDONQ01MwbTwfLhLBFWQFC3blNOeeGr9Fmg6og72skmqWe55dIyNN58o2lngfGKw1q3Lv9Fd7gyXdx4A7A= X-Gm-Message-State: AOJu0Yxt3/zvLFJ5iIJPlgXTL1/as6VxLYo9kFykF19dGuuWuhjyacxR 1SBuxW2DiMZFvB3QCo3M9ZWWBLeXiDA5cjoN/RTtarltFKMlnNaRkMkaYYcYRg== X-Google-Smtp-Source: AGHT+IG3qlw1x1SePS1V5j/mdkzvd9Zz5/3pjLBOxeH4hydPqS3ZppFbDb5KKv7zeJPRQgEW+0vKZA== X-Received: by 2002:a17:902:cec8:b0:1e2:15a8:e4c6 with SMTP id d8-20020a170902cec800b001e215a8e4c6mr11789247plg.55.1714982342042; Mon, 06 May 2024 00:59:02 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59:01 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 07/17] zram: add zlib compression backend support Date: Mon, 6 May 2024 16:58:20 +0900 Message-ID: <20240506075834.302472-8-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 +++ drivers/block/zram/Makefile | 9 +- drivers/block/zram/backend_deflate.c | 130 +++++++++++++++++++++++++++ drivers/block/zram/backend_deflate.h | 10 +++ drivers/block/zram/zcomp.c | 4 + 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 drivers/block/zram/backend_deflate.c create mode 100644 drivers/block/zram/backend_deflate.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 71cd0d5d8f35..9dedd2edfb28 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -38,6 +38,12 @@ config ZRAM_BACKEND_ZSTD select ZSTD_COMPRESS select ZSTD_DECOMPRESS +config ZRAM_BACKEND_DEFLATE + bool "deflate compression support" + depends on ZRAM + select ZLIB_DEFLATE + select ZLIB_INFLATE + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -63,6 +69,10 @@ config ZRAM_DEF_COMP_ZSTD bool "zstd" depends on ZRAM_BACKEND_ZSTD +config ZRAM_DEF_COMP_DEFLATE + bool "deflate" + depends on ZRAM_BACKEND_DEFLATE + endchoice config ZRAM_DEF_COMP @@ -72,6 +82,7 @@ config ZRAM_DEF_COMP default "lz4" if ZRAM_DEF_COMP_LZ4 default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "zstd" if ZRAM_DEF_COMP_ZSTD + default "deflate" if ZRAM_DEF_COMP_DEFLATE default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 053fe35e346b..91c07595d8b4 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -1,9 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o -obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o -obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o +obj-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o +obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o +obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o +obj-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c new file mode 100644 index 000000000000..949d402ea3dd --- /dev/null +++ b/drivers/block/zram/backend_deflate.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_deflate.h" + +/* Use the same value as crypto API */ +#define DEFLATE_DEF_WINBITS 11 +#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL + +struct deflate_ctx { + struct z_stream_s cctx; + struct z_stream_s dctx; + s32 level; +}; + +static void deflate_destroy(void *ctx) +{ + struct deflate_ctx *zctx = ctx; + + if (zctx->cctx.workspace) { + zlib_deflateEnd(&zctx->cctx); + vfree(zctx->cctx.workspace); + } + if (zctx->dctx.workspace) { + zlib_inflateEnd(&zctx->dctx); + vfree(zctx->dctx.workspace); + } + kfree(zctx); +} + +static void *deflate_create(void) +{ + struct deflate_ctx *ctx; + size_t sz; + int ret; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + /* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ + ctx->level = Z_DEFAULT_COMPRESSION; + sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); + ctx->cctx.workspace = vzalloc(sz); + if (!ctx->cctx.workspace) + goto error; + + ret = zlib_deflateInit2(&ctx->cctx, ctx->level, Z_DEFLATED, + -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL, + Z_DEFAULT_STRATEGY); + if (ret != Z_OK) + goto error; + + sz = zlib_inflate_workspacesize(); + ctx->dctx.workspace = vzalloc(sz); + if (!ctx->dctx.workspace) + goto error; + + ret = zlib_inflateInit2(&ctx->dctx, -DEFLATE_DEF_WINBITS); + if (ret != Z_OK) + goto error; + + return ctx; + +error: + deflate_destroy(ctx); + return NULL; +} + +static int deflate_compress(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct deflate_ctx *zctx = ctx; + struct z_stream_s *deflate; + int ret; + + deflate = &zctx->cctx; + ret = zlib_deflateReset(deflate); + if (ret != Z_OK) + return -EINVAL; + + deflate->next_in = (u8 *)src; + deflate->avail_in = PAGE_SIZE; + deflate->next_out = (u8 *)dst; + deflate->avail_out = *dst_len; + + ret = zlib_deflate(deflate, Z_FINISH); + if (ret != Z_STREAM_END) + return -EINVAL; + + *dst_len = deflate->total_out; + return 0; +} + +static int deflate_decompress(void *ctx, const unsigned char *src, + size_t src_len, unsigned char *dst) +{ + struct deflate_ctx *zctx = ctx; + struct z_stream_s *inflate; + int ret; + + inflate = &zctx->dctx; + + ret = zlib_inflateReset(inflate); + if (ret != Z_OK) + return -EINVAL; + + inflate->next_in = (u8 *)src; + inflate->avail_in = src_len; + inflate->next_out = (u8 *)dst; + inflate->avail_out = PAGE_SIZE; + + ret = zlib_inflate(inflate, Z_SYNC_FLUSH); + if (ret != Z_STREAM_END) + return -EINVAL; + + return 0; +} + +struct zcomp_backend backend_deflate = { + .compress = deflate_compress, + .decompress = deflate_decompress, + .create_ctx = deflate_create, + .destroy_ctx = deflate_destroy, + .name = "deflate", +}; diff --git a/drivers/block/zram/backend_deflate.h b/drivers/block/zram/backend_deflate.h new file mode 100644 index 000000000000..49cef8fc1e77 --- /dev/null +++ b/drivers/block/zram/backend_deflate.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_DEFLATE_H__ +#define __BACKEND_DEFLATE_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_deflate; + +#endif /* __BACKEND_DEFLATE_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index c16eb038f608..9fc5477a6259 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -20,6 +20,7 @@ #include "backend_lz4.h" #include "backend_lz4hc.h" #include "backend_zstd.h" +#include "backend_deflate.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -34,6 +35,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) &backend_zstd, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) + &backend_deflate, #endif NULL }; From patchwork Mon May 6 07:58:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655049 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 2F8F8142E68 for ; Mon, 6 May 2024 07:59:04 +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=1714982356; cv=none; b=bAYi2v06HU2rl9Pm2tOg3BpvKtwvlU1nAz7fqIfN/MRXFZga9zTLF0TBq/xrEHdflPyItVCZGWcPl07ANaSJc/sDEcn6p0Wqu0C1d2Nn3/kVjrAOVpqWc987aU+WgMqAsEp61paXgsARrSUhpjfDHgB+rZhnB7YS/ZvXfgvC4E0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982356; c=relaxed/simple; bh=kNJbXxfK8zdnskpjgM5oyiiq8zq2KQp2aiK9qKIm8Jk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LaLLN45x8uix/18/wrM/0ZmcjjyoLd7h+LP8rBuCzYrgfny+RutjzjECD8tUncvex555LeilwmgidmzRKKBIb0g70SuMQDp924BF0wBz6c8akCIFpkDI4v2ykLwgcv9pHq0ycLC0wiAXs93GdovgWHBSOpkPOsiF0R8/ytY2dYU= 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=XkbjsdWa; 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="XkbjsdWa" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1eb24e3a2d9so15914065ad.1 for ; Mon, 06 May 2024 00:59:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982344; x=1715587144; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EEH83ikFWbHDBUUzWu4h4KsSObV7EItvgFziqF5bCxw=; b=XkbjsdWaI5kxkxIsPYMcVSotFcCqJRHDQuhAsAM1zhvrp6SiZxgh41SgUFPjD+8fOb lbWN4drWqJTZLsUt7PX62p51vTsDJqO5JjVZwCGTNQjKTwbXPyitoNSVcrIxgjx141vW ywzSZayTzvkYpIlAyWCDUxYuObsxDMPx9RXb4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982344; x=1715587144; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EEH83ikFWbHDBUUzWu4h4KsSObV7EItvgFziqF5bCxw=; b=aO8JsHIeiGKWPXr2mQhwKwkiXuYmg6YWp5W6shilA/95luZyCwFyYlKn8ggEVpO0pr vyN2GoJuRyUn6WQDaN4VRim/Ggs+ta+dhzIsiQp+4n5WVbW5Ji1pZynW49Uzjz0QXoC8 kPrURV4Ph5jZIyZcy+rz/CJU4a9/1Wgavk5nwmVF2JkjBXSUBSWzBXPcRayeNFKkN/FY jfJPNILd39RwBTKYAZiAFZq0zm8yOhXrKFF39w+iKDgslPQtrmaweVLCkWydO3dtiBEp 6HxvguycWzOSeVpo9tCsuCT5u3gnDZvuU6BmhYKd5ze7TdDpocjty197Nkkfg9a715O/ C1kQ== X-Forwarded-Encrypted: i=1; AJvYcCVCOYf+28KyFHywrhwcrDn9AeLwHPM31MKMP6wcG+6eAzHhS/RMYvFfyVvxi4UuH3u+npHpvpEJxxYeitC6I1aZYvjs+bC64OP0wL0= X-Gm-Message-State: AOJu0Yzl26hL+AnetTSHNR57UMy+M+UY2hsnMMjFA1vQlAXAyY2nmpvk CRRXtu1gDhjk8tAAuqpy3V+47tEfHVuwQ/88rNxHXagHG9o2mSx4g/Ehem3Dlg== X-Google-Smtp-Source: AGHT+IFvtTM7Cp4XRi4ugjU8maUmX0Y1BJMBzeXwG4uz5NGaWtdCrHH1ePjQEmZ/XZrKEMZrCdrJng== X-Received: by 2002:a17:902:d2cd:b0:1e3:e1d5:c680 with SMTP id n13-20020a170902d2cd00b001e3e1d5c680mr13775583plc.63.1714982344547; Mon, 06 May 2024 00:59:04 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59:04 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 08/17] zram: add 842 compression backend support Date: Mon, 6 May 2024 16:58:21 +0900 Message-ID: <20240506075834.302472-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 11 ++++++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_842.c | 68 ++++++++++++++++++++++++++++++++ drivers/block/zram/backend_842.h | 10 +++++ drivers/block/zram/zcomp.c | 4 ++ 5 files changed, 94 insertions(+) create mode 100644 drivers/block/zram/backend_842.c create mode 100644 drivers/block/zram/backend_842.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 9dedd2edfb28..1e0e7e5910b8 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -44,6 +44,12 @@ config ZRAM_BACKEND_DEFLATE select ZLIB_DEFLATE select ZLIB_INFLATE +config ZRAM_BACKEND_842 + bool "842 compression support" + depends on ZRAM + select 842_COMPRESS + select 842_DECOMPRESS + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE @@ -73,6 +79,10 @@ config ZRAM_DEF_COMP_DEFLATE bool "deflate" depends on ZRAM_BACKEND_DEFLATE +config ZRAM_DEF_COMP_842 + bool "842" + depends on ZRAM_BACKEND_842 + endchoice config ZRAM_DEF_COMP @@ -83,6 +93,7 @@ config ZRAM_DEF_COMP default "lz4hc" if ZRAM_DEF_COMP_LZ4HC default "zstd" if ZRAM_DEF_COMP_ZSTD default "deflate" if ZRAM_DEF_COMP_DEFLATE + default "842" if ZRAM_DEF_COMP_842 default "unset-value" config ZRAM_WRITEBACK diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 91c07595d8b4..029827a6ddac 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o obj-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o obj-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o obj-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o +obj-$(CONFIG_ZRAM_BACKEND_842) += backend_842.o zram-y := zcomp.o zram_drv.o diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c new file mode 100644 index 000000000000..8ea7a230b890 --- /dev/null +++ b/drivers/block/zram/backend_842.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "backend_842.h" + +struct sw842_ctx { + void *mem; +}; + +static void destroy_842(void *ctx) +{ + struct sw842_ctx *zctx = ctx; + + kfree(zctx->mem); + kfree(zctx); +} + +static void *create_842(void) +{ + struct sw842_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return NULL; + + ctx->mem = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); + if (!ctx->mem) + goto error; + + return ctx; + +error: + destroy_842(ctx); + return NULL; +} + +static int compress_842(void *ctx, const unsigned char *src, + unsigned char *dst, size_t *dst_len) +{ + struct sw842_ctx *zctx = ctx; + unsigned int dlen = *dst_len; + int ret; + + ret = sw842_compress(src, PAGE_SIZE, dst, &dlen, zctx->mem); + if (ret == 0) + *dst_len = dlen; + return ret; +} + +static int decompress_842(void *ctx, const unsigned char *src, size_t src_len, + unsigned char *dst) +{ + unsigned int dlen = PAGE_SIZE; + + return sw842_decompress(src, src_len, dst, &dlen); +} + +struct zcomp_backend backend_842 = { + .compress = compress_842, + .decompress = decompress_842, + .create_ctx = create_842, + .destroy_ctx = destroy_842, + .name = "842", +}; diff --git a/drivers/block/zram/backend_842.h b/drivers/block/zram/backend_842.h new file mode 100644 index 000000000000..c03a2396d7b2 --- /dev/null +++ b/drivers/block/zram/backend_842.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_842_H__ +#define __BACKEND_842_H__ + +#include "zcomp.h" + +extern struct zcomp_backend backend_842; + +#endif /* __BACKEND_842_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 9fc5477a6259..2a38126f4da3 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -21,6 +21,7 @@ #include "backend_lz4hc.h" #include "backend_zstd.h" #include "backend_deflate.h" +#include "backend_842.h" static struct zcomp_backend *backends[] = { #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) @@ -38,6 +39,9 @@ static struct zcomp_backend *backends[] = { #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) &backend_deflate, +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_842) + &backend_842, #endif NULL }; From patchwork Mon May 6 07:58:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655045 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 B1887142E79 for ; Mon, 6 May 2024 07:59:07 +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=1714982349; cv=none; b=sVi9lPAfDPnLLTQhycU4rfGLd5912JkTBRk/qe5iH9xpuirjVmTBAoK3Fd0AxKUkaAOI+AXgaSlRretNtIe4ZMNugI6/ZkbFLsiaLRyq3qmF/haOslWJieLGXDhM4GMsa01tGn3Jfp+3siEXGoNTnygaJiRQKCd1Ycs0Hvypd4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982349; c=relaxed/simple; bh=fkmGnp5H4sYk5AcEucbfVYS4PjNz7ma3EYQ0ecKZnDQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g1pIKuS8eKOcaZYezOUe+ZZ2B1BE84XvarMNMbIeeIaBy17mK4SHZW8NIdsUaQTa8TR2+lq4BdaPv+mvshGoTQXMDVqLiQAfOAK3QfkXYJ1qC9L5ZGOWjyLBDtatAUxvyMbQJguHXRBE2PlFScvRDGERJ/EzFQ/iVnaIFi3Y6ww= 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=KgTsJZdP; 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="KgTsJZdP" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1ec69e3dbcfso9766205ad.0 for ; Mon, 06 May 2024 00:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982347; x=1715587147; 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=KgTsJZdPOGCHNeKMRY/ASsztQuJA1aHxZewpKIpaBMEVQZ1Hm6lxgvK24JJZrSxOIW L0pEPc6ulksi6lrBLU49vCe2e87GK9ZggRWa6c1S/Xqmnjzec9ZcqKHx5vFUa0/mgRoG 9rJN4wsRT/yU2pVjGYU//JDekkVQ2yNxVQLik= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982347; x=1715587147; 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=NvHjTUNIjOeCx+Hoq4KLEF1G+mSnwrUdvt21O7L4aZtUgPPK8YWT8p/4rm+uKUp9dh M+hZuq1N7QmvNl+uC8qubQEscwV3BDJrgx1IA2R/+qIEXivFCDH4De8EGympCgHns5Cw dnM+Vq9Fkf0mmlYXY2JfwjLM+OGyAOprq5OTkhanMGrw9AfUv9wIC7pdaHRaahpdTK+c mJpTv5YJv/j8AS//QQsCKWpti/0Py+i/tA7ZQtgmigqOGVa7euwVKrIwnvcuE4R28A6X m3XdKNE4ORKtntQslOoKL8MggGLJQ14ouVfceAWsnTb1UgMxL8VHuWUwviWYsVLuYP7h oX9g== X-Forwarded-Encrypted: i=1; AJvYcCUL/G8+cHI6H4U7UD4xczrOUbVsyVc660DIVTpHEdolCeFV5vp6guC9J1I2+8QbmHcabskPxecxtmyvLplCy9yCvY0RkNcCshDvG7U= X-Gm-Message-State: AOJu0YxT9HgHlH2Oe1zG1vEiRpS0WsGHlopSCaGVS5Vt1sFxJduXhl60 eBHiEqSCcsZMAUWmcJ+uDWaycTvWab641dzW3v+5kdZNNft0yIFQURv2ABlXKw== X-Google-Smtp-Source: AGHT+IGhUbnM5L19HjkMQe7zz6msC494kghrBWv74m5MtSoX20PhtNuMEWFgOVS6HchTjkbJ745/Dw== X-Received: by 2002:a17:902:ea06:b0:1ec:28e4:691f with SMTP id s6-20020a170902ea0600b001ec28e4691fmr10804968plg.63.1714982346998; Mon, 06 May 2024 00:59:06 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 09/17] zram: check that backends array has at least one backend Date: Mon, 6 May 2024 16:58:22 +0900 Message-ID: <20240506075834.302472-10-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-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 Mon May 6 07:58:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655046 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 1DD90142E9D for ; Mon, 6 May 2024 07:59:09 +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=1714982351; cv=none; b=iNREu2O4HzgkIqDx2+dLRUa4ri06I3q+58+hD1oFPcW8x3avyvQbGtjaeuFwRIayk47HtvFKlEiGF4NdUs2YQ4vfqEO8V9plg8ym4hya1G/vlfmrEVE6pr4RKvWa5YFrenu+fHeFYEg8HMrIVasZfFmuaAxS/a/M7IOd1wYJwSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982351; c=relaxed/simple; bh=2Vyk+Ag37u4kDGLIMiSings1Oonk1CeUx8x+t48thY8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qfMtTkDlY/N04BSgeVByZQMD1eLT1302yhd2Px8jaqYTiiUwPbFmsb2ougHLS6D7xp2Ub1wIFsl5+uGY0Uaoyd67IoqY6ordsspmEABCeFbjwvWRNwELA7AZstebvyabW9aQPlOtdUf+IwO0VGQRh6HTpBo6gVnCHgPa/2VLqDc= 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=Zw2Lam0O; 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="Zw2Lam0O" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6f44dd41a5cso1631141b3a.0 for ; Mon, 06 May 2024 00:59:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982349; x=1715587149; 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=pVh6NYo/4zssb8icrCllJJuSBkrSwxoCvV3IlQAP7Ko=; b=Zw2Lam0Os1nG0TuPs2vIV5KPyjJOuUkRK/Jue8vHkRxUEgAV5eCpo/hfrlooipCg7t TOLLCZhhre75c7xA5oJbaFRsZcSVEElz279C1JTX4DVuKp2vP0CAB4Ay9ThF+3w8WfkZ UzWu2ZrBmxk5i/Zv6CO98u56XZF0QhWIAjjC8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982349; x=1715587149; 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=pVh6NYo/4zssb8icrCllJJuSBkrSwxoCvV3IlQAP7Ko=; b=nuuO/U6kkYZO9GYds19haUS4z9Hn+GSQWT/e4Jx7flKe8E9EE0i1JPLHybWqQ8mmc7 e4bl9dtxuVu51CPq3/TIcfA5TcFONFMfO7yAimUW1l55cuhTt9GIQ/W1XDRE/97/AB7a 1Tckj2MAQyRBZPSwtuKBFQ9eT8squo2xAoBMKmf+ruwRStUaOgNO77/NujwsuBhG34qq OOtCm71/C7BcViIQEmlzvJqxoOXney+4gkumqwYkdMleuuUTznpIxxPQleNm8mQEQ/92 f1SXIIr/6caxni5L2Oi6R2X8bPLgTcxhP2RIiY7RQneb2RYLJusyiyuODM/BAuQyAla9 Uv3g== X-Forwarded-Encrypted: i=1; AJvYcCUclPxWbRUMilzeCVMdeOImjQbf+sUiYMb3v/NPSmed0A1dOf+Mi8uB3A9sc8JE7fE4cNKAMc8c+h5c2MVzbszE0J+etB39dZqh7AA= X-Gm-Message-State: AOJu0YwDU3VIfiW3ko11u0dkMhIlxnWWypBsHfRwXV4BUu+Bwo2cBwFH XaPbrrjewvqJzJdOWrgEee9H1a7sehyTrNkfnmG4f8hjmermYCDGnSReoi+scM9leXx3qEtcg04 = X-Google-Smtp-Source: AGHT+IH/mSd3rLEiYYj8dUEjAhdpkeQnt3cdXAUtQ7VcLpCUW6PImX4BG/ZFvXc1Yt3FkqqB5nM56A== X-Received: by 2002:a05:6a21:1798:b0:1af:ac31:9b8b with SMTP id nx24-20020a056a21179800b001afac319b8bmr4497887pzb.10.1714982349467; Mon, 06 May 2024 00:59:09 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59:09 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 10/17] zram: introduce zcomp_config structure Date: Mon, 6 May 2024 16:58:23 +0900 Message-ID: <20240506075834.302472-11-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We will store a per-algorithm parameters there (compression level, dictionary, dictionary size, etc.). Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_842.c | 2 +- drivers/block/zram/backend_deflate.c | 9 ++++++--- drivers/block/zram/backend_lz4.c | 2 +- drivers/block/zram/backend_lz4hc.c | 10 ++++++---- drivers/block/zram/backend_lzo.c | 2 +- drivers/block/zram/backend_lzorle.c | 2 +- drivers/block/zram/backend_zstd.c | 8 ++++++-- drivers/block/zram/zcomp.c | 5 +++-- drivers/block/zram/zcomp.h | 14 ++++++++++++-- drivers/block/zram/zram_drv.c | 20 +++++++++++++++++++- drivers/block/zram/zram_drv.h | 1 + 11 files changed, 57 insertions(+), 18 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c index 8ea7a230b890..12e716deb763 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -19,7 +19,7 @@ static void destroy_842(void *ctx) kfree(zctx); } -static void *create_842(void) +static void *create_842(struct zcomp_config *config) { struct sw842_ctx *ctx; diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c index 949d402ea3dd..83c660adc722 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx) kfree(zctx); } -static void *deflate_create(void) +static void *deflate_create(struct zcomp_config *config) { struct deflate_ctx *ctx; size_t sz; @@ -42,8 +42,11 @@ static void *deflate_create(void) if (!ctx) return NULL; - /* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ - ctx->level = Z_DEFAULT_COMPRESSION; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = Z_DEFAULT_COMPRESSION; + sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); ctx->cctx.workspace = vzalloc(sz); if (!ctx->cctx.workspace) diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 697592dbabe2..98d9c9274149 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -4,7 +4,7 @@ #include "backend_lz4.h" -static void *lz4_create(void) +static void *lz4_create(struct zcomp_config *config) { return vmalloc(LZ4_MEM_COMPRESS); } diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 5c437623aa65..62721a7c82dc 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -18,7 +18,7 @@ static void lz4hc_destroy(void *ctx) kfree(zctx); } -static void *lz4hc_create(void) +static void *lz4hc_create(struct zcomp_config *config) { struct lz4hc_ctx *ctx; @@ -26,14 +26,16 @@ static void *lz4hc_create(void) if (!ctx) return NULL; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = LZ4HC_DEFAULT_CLEVEL; + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); if (!ctx->mem) { lz4hc_destroy(ctx); return NULL; } - - /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ - ctx->level = LZ4HC_DEFAULT_CLEVEL; return ctx; } diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c index b88b408964cd..79ecfae9c4c5 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -6,7 +6,7 @@ #include "backend_lzo.h" -static void *lzo_create(void) +static void *lzo_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c index 9bf1843021b0..b0937103b5fb 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -6,7 +6,7 @@ #include "backend_lzorle.h" -static void *lzorle_create(void) +static void *lzorle_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 4a7734aa1a8a..b2fb94902bef 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -24,7 +24,7 @@ static void zstd_destroy(void *ctx) kfree(zctx); } -static void *zstd_create(void) +static void *zstd_create(struct zcomp_config *config) { zstd_parameters params; struct zstd_ctx *ctx; @@ -34,7 +34,11 @@ static void *zstd_create(void) if (!ctx) return NULL; - ctx->level = ZSTD_defaultCLevel(); + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = ZSTD_defaultCLevel(); + params = zstd_get_params(ctx->level, PAGE_SIZE); sz = zstd_cctx_workspace_bound(¶ms.cParams); ctx->cctx_mem = vzalloc(sz); diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index d49791f724e9..3f990a715487 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -61,7 +61,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) */ static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) { - zstrm->ctx = comp->backend->create_ctx(); + zstrm->ctx = comp->backend->create_ctx(comp->config); /* * allocate 2 pages. 1 for compressed data, plus 1 extra for the @@ -204,7 +204,7 @@ void zcomp_destroy(struct zcomp *comp) kfree(comp); } -struct zcomp *zcomp_create(const char *alg) +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config) { struct zcomp *comp; int error; @@ -221,6 +221,7 @@ struct zcomp *zcomp_create(const char *alg) if (!comp) return ERR_PTR(-ENOMEM); + comp->config = config; comp->backend = lookup_backend(alg); if (!comp->backend) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 757b85017e23..345c78bc76db 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -5,6 +5,7 @@ #ifndef _ZCOMP_H_ #define _ZCOMP_H_ + #include struct zcomp_strm { @@ -15,6 +16,14 @@ struct zcomp_strm { void *ctx; }; +#define ZCOMP_CONFIG_NO_LEVEL INT_MIN + +struct zcomp_config { + s32 level; + size_t dict_sz; + void *dict; +}; + struct zcomp_backend { int (*compress)(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len); @@ -22,7 +31,7 @@ struct zcomp_backend { int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst); - void *(*create_ctx)(void); + void *(*create_ctx)(struct zcomp_config *config); void (*destroy_ctx)(void *ctx); const char *name; @@ -32,6 +41,7 @@ struct zcomp_backend { struct zcomp { struct zcomp_strm __percpu *stream; struct zcomp_backend *backend; + struct zcomp_config *config; struct hlist_node node; }; @@ -40,7 +50,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); ssize_t zcomp_available_show(const char *comp, char *buf); bool zcomp_available_algorithm(const char *comp); -struct zcomp *zcomp_create(const char *alg); +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config); void zcomp_destroy(struct zcomp *comp); struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 298ef0e97e03..6c36cd349431 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1994,6 +1994,20 @@ static void zram_slot_free_notify(struct block_device *bdev, zram_slot_unlock(zram, index); } +static void zram_reset_comp_configs(struct zram *zram) +{ + u32 prio; + + for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) { + struct zcomp_config *config = &zram->configs[prio]; + + vfree(config->dict); + config->level = ZCOMP_CONFIG_NO_LEVEL; + config->dict_sz = 0; + config->dict = NULL; + } +} + static void zram_destroy_comps(struct zram *zram) { u32 prio; @@ -2007,6 +2021,8 @@ static void zram_destroy_comps(struct zram *zram) zcomp_destroy(comp); zram->num_active_comps--; } + + zram_reset_comp_configs(zram); } static void zram_reset_device(struct zram *zram) @@ -2064,7 +2080,8 @@ static ssize_t disksize_store(struct device *dev, if (!zram->comp_algs[prio]) continue; - comp = zcomp_create(zram->comp_algs[prio]); + comp = zcomp_create(zram->comp_algs[prio], + &zram->configs[prio]); if (IS_ERR(comp)) { pr_err("Cannot initialise %s compressing backend\n", zram->comp_algs[prio]); @@ -2271,6 +2288,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; + zram_reset_comp_configs(zram); comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); zram_debugfs_register(zram); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 37bf29f34d26..2c77f3bff166 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -107,6 +107,7 @@ struct zram { struct zram_table_entry *table; struct zs_pool *mem_pool; struct zcomp *comps[ZRAM_MAX_COMPS]; + struct zcomp_config configs[ZRAM_MAX_COMPS]; struct gendisk *disk; /* Prevent concurrent execution of device init */ struct rw_semaphore init_lock; From patchwork Mon May 6 07:58:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655047 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 C5CE61411F8 for ; Mon, 6 May 2024 07:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982354; cv=none; b=iA4zDiZyUEHXiG26KLVm9R7ODxFaAouVMpHF2x1aHCsLUM8FgQGXi1h3ct8XhNoshBigCifgdkV3/K6OwWIgL6yQDQltAXM5wV01TzFvODMHpH7iVVDxm4TqH+Ou3jeCTr49749xtCx+3YxnQvWvgHqp50g8p9yTVXnUSOYRhko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982354; c=relaxed/simple; bh=k8QWr7Ce151M14jHiuPdpQHPaGdK/slKGX2a8ui9kaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G+IHgGO6Ob0O5yGu1NgULNlJgSdTkx0yZPESQqmVGPA/h6EzEsRxbIP88JqN3em/ywULgMg8/qhkouBiXnclC6DEQ+VeeVcM9avI8J/q2zOJg2fhTWF6nIYuqW2C4xBs/bOgPXuT4gBFUvA+jDOIpNdSU9MFdbwKkRkesokeJ/k= 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=awEsttkZ; arc=none smtp.client-ip=209.85.215.172 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="awEsttkZ" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-5ce07cf1e5dso991380a12.2 for ; Mon, 06 May 2024 00:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982352; x=1715587152; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hvvjJAe8l8tDTfecPvMLnwPB1eap8xtvETRDKvyj4SM=; b=awEsttkZoYVD4mYwcv21cz0wNIF0xRxm4gTzCc0eVulvJfhIu1DMhfxjOGwyaibI/y 8kQ4mYg2j2ttMlDSLrJRlDK6Txg5Er6dvndjOA20ozZfUoLofiEsxbxAspRnme/9PAOw g7Yj5gvyf159yaEVtbW1MCabr7vJSu1jSKAi4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982352; x=1715587152; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hvvjJAe8l8tDTfecPvMLnwPB1eap8xtvETRDKvyj4SM=; b=ftRTNl8r2qYXz3MC0wwv9SaO3oaup86OJVfhnXmn8dcPYM0Fh9MiK/va6+GOBLPMNZ MUZ1Q6iMkqT+lDsqkpRACp6oDxTlAxhPDtNJht0qw/PYTJFXoIpSXI+6N1WyQAqXEnVg TdiE1G2iOmkQGbdODtm+Lbt81oAOM73n+YLtMqaR9YqaLs5d8yXkfmy+mdd0Khiboevi Hz7pEDFe2Z4PqUX4o+ZAG/f3Uhc4evZV2ZQumBor4XAhnfZLTvoHAJ5fR8ggMD+iAUz+ 0j6DD7sj566K4Gz7oTam7cdslOCkUmpyYpCFMs+AA23KHRVFx3xpt1DGXInYlTxPGMd+ r7dQ== X-Forwarded-Encrypted: i=1; AJvYcCUyzLTxo0pdQQ1f/vAg+SfgnW8Mt9bF63U5sB69FlaaETQDRJlP8bzPf6+R1eadkmIHDBGB2kReE2aOeKCXkgG+vH2xzy4AQiSGZF0= X-Gm-Message-State: AOJu0YxQMC9pCYVfs0TNSd5MJC2AkG/kmp4Etfol4M1Rk+gAUqAHegIW 7CSDXcr5PRBJwxM6VBv93F2cZ9ecjk+iOJ0jD7SM5aO99HRP+paX8MxN5ivjGA== X-Google-Smtp-Source: AGHT+IFI/ZSB9K3QVOfSMmaBz+TlUwMRiyAXAGYpMQBEi0KptgFE5NvdCMm1zm04lt/IfO56n8Dyng== X-Received: by 2002:a05:6a21:2d8c:b0:1ad:9413:d5bd with SMTP id ty12-20020a056a212d8c00b001ad9413d5bdmr11397267pzb.9.1714982351980; Mon, 06 May 2024 00:59:11 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 11/17] zram: extend comp_algorithm attr write handling Date: Mon, 6 May 2024 16:58:24 +0900 Message-ID: <20240506075834.302472-12-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Previously comp_algorithm device attr would accept only algorithm name param, however in order to enabled comp configuration we need to extend comp_algorithm_store() with param=value support. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 6c36cd349431..bd8433363cbe 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1028,9 +1028,33 @@ static ssize_t comp_algorithm_store(struct device *dev, size_t len) { struct zram *zram = dev_to_zram(dev); + char *args, *param, *val; + char *alg = NULL; int ret; - ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf); + args = skip_spaces(buf); + while (*args) { + args = next_arg(args, ¶m, &val); + + /* + * We need to support 'param' without value, which is an + * old format for this attr (algorithm name only). + */ + if (!val || !*val) { + alg = param; + continue; + } + + if (!strcmp(param, "algo")) { + alg = val; + continue; + } + } + + if (!alg) + return -EINVAL; + + ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; } From patchwork Mon May 6 07:58:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655048 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 1CFAC143888 for ; Mon, 6 May 2024 07:59:14 +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=1714982356; cv=none; b=FPHn541pQMcGUGK9D3XuKjwehkVYwY5ejAqZCiVZi/smZPHpOLXImDgUE710sEvPb3184njBrd81V296jmSQpPl0oAbHGPHcwst4fvdvCZETzTMV+0rQtqBIN5k/IkVl3Utey96rxFZnK0x3+O0rr1MkzDRjwQIIwdgaNnRg2xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982356; c=relaxed/simple; bh=d4UQf/ZsLZS7cvretx2WXvAFWLig4xky110hNJGk3Zc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RlwL5KONHECkKvQC4hbFZckFlB6HkBdyF7lxIXAOMg/48d2i852lUAArEJytXSuCheAbyh3DZ8gE8rzGKT/IqRWO19LOZu3SPk3aXYXG9fLG0MW+Z7rYqJYWlyjtnNi9HlKa8huvu5qbq24rMGtNpRtnIykAr9A7YCdijSRogRE= 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=mURkW0J1; 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="mURkW0J1" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1ee42b97b32so719225ad.2 for ; Mon, 06 May 2024 00:59:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982354; x=1715587154; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wIlqiB/ZJyLpyyyQGB102ierEcV1ZXWEtzUdufZbr+0=; b=mURkW0J12TLZ9IAGFzMJD7zTYRO3Ji6THYvcqwKHdIMigAmVH2G102S1sPzby08G8M CeRG9SSaYjBxJi85pBHskG8rYFSwnLftoJ8Jy5LlCSS1D/rY6UmmGanVNhFx1hu6AaiR BmQwbrULcPsgFOMiAGIo24Fd84LhwSKdIhExg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982354; x=1715587154; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wIlqiB/ZJyLpyyyQGB102ierEcV1ZXWEtzUdufZbr+0=; b=VwbwYrnlvMZHwcam3b1mo8/pTRFhlxzgPGgyy/mPSGPCoEBqJW3ijUXKaDhe3yP+PE qsTiIOwXdAA3Hkdc5KvOGhW/SWWH5UmPJRbZWaYEKJdKaTRhVobnKiJ/kOpZjJH2EsNw rcRdjF6KhDnjMSanVfJuYea7aVJewA39WBubqkui5OCMwGfdPGmvV8DXjrjzQUO7fAkY kiCPeHoBedZaz+OHB7J3+/Li0zPpAVUyJqxEkfzKE0JtSs+tyfLRrECETb/jbz1HRfKe DVSZEwd1Z6xPpd684K14V1Ar1FueQdmlnMqr3KtiJxuvOGmF+4LvhJl4a3Us+bgjzkFj okeA== X-Forwarded-Encrypted: i=1; AJvYcCW36IkJ8c0swP6r9fWFTZiM4kEY8mEra/ylVxP/dNZnFwdyv/cDgZ+zo2j8WDCHRgUOm6nOex8APSl7tY+K5OmonDonimFynSebjEE= X-Gm-Message-State: AOJu0YxHO/zNP8lj6vhy6C+GNtGQC05nuxTqta22LLderbjXjKfzC0wm SdawfsXZubt6iF0r45x6423ywCtdjIEKoFqIEilfvYzRhPHnQl7ZoOvneHy9U9C++FayPd3Ug1Q = X-Google-Smtp-Source: AGHT+IEu8xJh6jdUx6bHxS7wt8XM4/kzzQzw9kZsTgvT5qkc9pX/gCVBcFkVCQ4l1y2x/J0OVqYbhA== X-Received: by 2002:a17:902:6806:b0:1e0:e85f:3882 with SMTP id h6-20020a170902680600b001e0e85f3882mr9682647plk.38.1714982354399; Mon, 06 May 2024 00:59:14 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59:14 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 12/17] zram: support compression level comp config Date: Mon, 6 May 2024 16:58:25 +0900 Message-ID: <20240506075834.302472-13-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add support for compression level level=N comp configuration to comp_algorithm and recomp_algorithm knobs. Note that zram cannot verify ranges, it's a task of corresponding backends to make sure that level makes sense. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index bd8433363cbe..89a2eb37e26c 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1013,6 +1013,12 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) return 0; } +static int comp_config_store(struct zram *zram, u32 prio, s32 level) +{ + zram->configs[prio].level = level; + return 0; +} + static ssize_t comp_algorithm_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -1030,6 +1036,7 @@ static ssize_t comp_algorithm_store(struct device *dev, struct zram *zram = dev_to_zram(dev); char *args, *param, *val; char *alg = NULL; + s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; args = skip_spaces(buf); @@ -1049,12 +1056,21 @@ static ssize_t comp_algorithm_store(struct device *dev, alg = val; continue; } + + if (!strcmp(param, "level")) { + ret = kstrtoint(val, 10, &level); + if (ret) + return ret; + continue; + } } if (!alg) return -EINVAL; - ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); + ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level); + if (!ret) + ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; } @@ -1087,6 +1103,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, int prio = ZRAM_SECONDARY_COMP; char *args, *param, *val; char *alg = NULL; + s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; args = skip_spaces(buf); @@ -1107,6 +1124,13 @@ static ssize_t recomp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "level")) { + ret = kstrtoint(val, 10, &level); + if (ret) + return ret; + continue; + } } if (!alg) @@ -1115,7 +1139,9 @@ static ssize_t recomp_algorithm_store(struct device *dev, if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS) return -EINVAL; - ret = __comp_algorithm_store(zram, prio, alg); + ret = comp_config_store(zram, prio, level); + if (!ret) + ret = __comp_algorithm_store(zram, prio, alg); return ret ? ret : len; } #endif From patchwork Mon May 6 07:58:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655050 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 C82AB143C65 for ; Mon, 6 May 2024 07:59:17 +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=1714982359; cv=none; b=jXLmRwj0iAH/IKHmmIzlsPw2BWhuSAIB47tEvhmt3jQbT22O6sjyyeZ0YBXhrmGWlxPluEr+1exRWK2zrIqSL+uGWCBWojoIAfLPLbSDg07/qtQcye5nqjfNK2UsYlKL6fH1bE/s0V6114V4H/2y5wWZR9R0mVtYApKyXkf4ocg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982359; c=relaxed/simple; bh=hqBaAu2Ha8Gh/abMg4Cg6golRoIbO9BVZYRoWaldmIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cA0FGHUxNVc7tDITlqdetlCckq6YCBL05YYk55oBws4mMyJvO+kC4zw0nyZVu6ie8bB86Mow3lxkNdp+H1hJNGaS4/u5DQzmSNXedOEnPCJOxqvyvduTTFoVaEgw2YCIx+pMmR2bxsdqiTGvtjRnThJxg4KKDFGfJE5xq018nS0= 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=jOQqfVzV; 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="jOQqfVzV" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1e9ffd3f96eso10200195ad.3 for ; Mon, 06 May 2024 00:59:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982357; x=1715587157; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=une8u6O58vSEqdMRQHIC0PiltZGgPobzLxkJ5Uf2uCM=; b=jOQqfVzVUh4iDEKhdp6pilQEjUoYGOGRLd3S5VuYwi9eK48/btkDhINpASI84yYWkN oZ6q3DJXvgpH0KZ3QmJcJJ4Fd1CPlYngX4puFcm/vDFHKemW+YjbJhXhRvP6hGZ6JzMe xUEaZVn06W9jq2sXc/Q1OoSptFTW5pvvt90Ts= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982357; x=1715587157; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=une8u6O58vSEqdMRQHIC0PiltZGgPobzLxkJ5Uf2uCM=; b=T8973kpOe3fKFIN+qRjeVwnAeiIRV0QWC3Yx6t1zqtcmU6SwLFABkTp2hnvgTC4pmV C5cTesLk61BYHbmF77l+HAknFaB124aiiIKe8n72wosE9Snh0dacnNUtnEDkyCCn1Lli H4Yaj7ezNdWvt7fSIM8U5LRCRv19+69iPNX1aES8svQkIkYIoeMGmnnHdy8MqcoNXEVz yeTP9Nv7zN6xMTSvQ3MChwmpaZvGaF1CvX+vDY0Nij91Em0/oNR+yJUgE8aSP4+rJZr8 imYpRGpwh7r1E+J4DRzEtxENuUx7T0q7LYyrBP5NnZYq21VCGe/twQkh9djbGbavU+zB l9ww== X-Forwarded-Encrypted: i=1; AJvYcCWqfg1zq+7ZrmKNcptVeADeBNVzMMdwOTT1UsvTGe9j3MQPIRVDTDbFJnKmfaxidEtQXOYBj4FV1p+bYSAVzj536z0o7cZRwXHJ4Ec= X-Gm-Message-State: AOJu0YyPcBuINW7r2UU+g2Rs1uNwyfMItkn9TMA5S5OcWjDGlZL8arv1 BffMMKCI97nNqFl6Ddc+RctULJna2n6X8r30xKqwjABmqOkiJt1zlmksJsA/9w== X-Google-Smtp-Source: AGHT+IGzsGmfPiTUH/STys9Y8TEuX4yEBZGZlmQlIEqsXRWYr5vbBOXLsW/TZxDNhzdywdQuP5/k1w== X-Received: by 2002:a17:902:c10c:b0:1e1:a54:1fe8 with SMTP id 12-20020a170902c10c00b001e10a541fe8mr9487138pli.53.1714982357067; Mon, 06 May 2024 00:59:17 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 13/17] zram: add support for dict comp config Date: Mon, 6 May 2024 16:58:26 +0900 Message-ID: <20240506075834.302472-14-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Handle dict=path param so that we can read a pre-trained compression algorithm dictionary which we then pass to the backend configuration. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 54 ++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 89a2eb37e26c..863e4e125eaa 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "zram_drv.h" @@ -1013,9 +1014,35 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) return 0; } -static int comp_config_store(struct zram *zram, u32 prio, s32 level) +static void __reset_comp_config(struct zram *zram, u32 prio) { + struct zcomp_config *config = &zram->configs[prio]; + + vfree(config->dict); + config->level = ZCOMP_CONFIG_NO_LEVEL; + config->dict_sz = 0; + config->dict = NULL; +} + +static int comp_config_store(struct zram *zram, u32 prio, s32 level, + const char *dict_path) +{ + ssize_t sz = 0; + + __reset_comp_config(zram, prio); + + if (dict_path) { + sz = kernel_read_file_from_path(dict_path, 0, + &zram->configs[prio].dict, + INT_MAX, + NULL, + READING_POLICY); + if (sz < 0) + return -EINVAL; + } + zram->configs[prio].level = level; + zram->configs[prio].dict_sz = sz; return 0; } @@ -1035,7 +1062,7 @@ static ssize_t comp_algorithm_store(struct device *dev, { struct zram *zram = dev_to_zram(dev); char *args, *param, *val; - char *alg = NULL; + char *alg = NULL, *dict_path = NULL; s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; @@ -1063,12 +1090,17 @@ static ssize_t comp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "dict")) { + dict_path = val; + continue; + } } if (!alg) return -EINVAL; - ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level); + ret = comp_config_store(zram, ZRAM_PRIMARY_COMP, level, dict_path); if (!ret) ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg); return ret ? ret : len; @@ -1102,7 +1134,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, struct zram *zram = dev_to_zram(dev); int prio = ZRAM_SECONDARY_COMP; char *args, *param, *val; - char *alg = NULL; + char *alg = NULL, *dict_path = NULL; s32 level = ZCOMP_CONFIG_NO_LEVEL; int ret; @@ -1131,6 +1163,11 @@ static ssize_t recomp_algorithm_store(struct device *dev, return ret; continue; } + + if (!strcmp(param, "dict")) { + dict_path = val; + continue; + } } if (!alg) @@ -1139,7 +1176,7 @@ static ssize_t recomp_algorithm_store(struct device *dev, if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS) return -EINVAL; - ret = comp_config_store(zram, prio, level); + ret = comp_config_store(zram, prio, level, dict_path); if (!ret) ret = __comp_algorithm_store(zram, prio, alg); return ret ? ret : len; @@ -2049,12 +2086,7 @@ static void zram_reset_comp_configs(struct zram *zram) u32 prio; for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) { - struct zcomp_config *config = &zram->configs[prio]; - - vfree(config->dict); - config->level = ZCOMP_CONFIG_NO_LEVEL; - config->dict_sz = 0; - config->dict = NULL; + __reset_comp_config(zram, prio); } } From patchwork Mon May 6 07:58:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655051 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.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 069651442F0 for ; Mon, 6 May 2024 07:59:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982362; cv=none; b=nhWXN7YlW4JpHAE0mnCfFDJAlUd2WZbJ0WmRLvbcQyQdlC1zMglUchbYSe7HLHdkjPe0SRavYFDneQ7yx/FvmOvzK15rNohvWfNBeOpOOalnBQ/jYib1AK4Ln9jefRv5Tuydsi9sUhGwYLn5dX4kkIH7EZdFB2DYWuSLO0Kka6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982362; c=relaxed/simple; bh=09Ka/tk/DAZP8tzeuC9sVbqY92/WnQ147E6Hm/AJ2rM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EIGSnEa9e2BDmd7rqNTuOR/K1ZdwHEl4mzbN4FuTfD44qBN0ueqXj8XNUj0tE8UeC1MIfl9pDiEeYOabqgeIfUYSL91VIxYn3wsZfZ5AEqfPHkeFhuYHhwwbh/3rlLFs/lpbqNt/y+mhphfnZ4z2M7SGZFE/hGj8CtJfwrnBNIU= 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=K2Lgl7ZY; arc=none smtp.client-ip=209.85.210.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="K2Lgl7ZY" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6f28bb6d747so948256b3a.3 for ; Mon, 06 May 2024 00:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982359; x=1715587159; 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=670BJqB3qXgoYBmu7ZZ2F7xy6czndExLjXYSSS9otVc=; b=K2Lgl7ZYYtDjnluEJHFLSJ6UBxSfbOLFbckpF+REUbxISR5F0e8gF7ddRxBD9L13s+ jJqAa2WW22YbYVkIrxBg5A0xoYdhSnOM78bdaNoAP4SqTqVxq4DepS3yOOJ9pqagZEdC RZ/9oycRECn0oc9obL3Djpcm4oMV6TCZiwNpA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982359; x=1715587159; 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=670BJqB3qXgoYBmu7ZZ2F7xy6czndExLjXYSSS9otVc=; b=tMxhu+V/Ye8sPf3qahdfb6mKTqe1OSzb5xiuqj4xh1jUCQGPpibFkxUkiJSS7HV56S 1mhtApZvtnkU27nJrEMUg7QRB49mqafWUDtpxt4TQiCTFdakwVOlqp+Lmo+EMu0YFBN9 O+WSC/FMIAanQgP5VQR1xXSwR5gyJzMU7mVuT4E2E8SYhWHCkzK21l/8/dRZD3+J34vt wZYarehJOAFbARXvrqsAyV0+agn2A5EgbYRD4mX6xvc2DLxD+LlGFytysPfNct23xSw8 LjOc3sh800ZzRsMFPJq9Ph2aqNWPoEr000u54KOC5VPnTU3hX+OGWlDgWIfh2ukylV+W qbow== X-Forwarded-Encrypted: i=1; AJvYcCUY9h5GW+XO5QSEkZP1+BqHfRufyvnzSofu8Tdi5hGKOplAdgDepSBS/+FaXS4+2nwayubkhchr2/ELaN2mMedn1lInIJGt302Uaag= X-Gm-Message-State: AOJu0YyeXb5zHFTmlN61ahYL8uEZsI2cIMS9JqjXd77bDRVdQD/rjYVc zbcHPz34mJgaBKeWKdwdh5wryZAMu2B0ao7Civ3WQ9UN7iUeXiS2757NKpF61A== X-Google-Smtp-Source: AGHT+IFqCS/72gy51F3U0CaV8OqKEWsZufjlSXQ1lFbeGQE7NGOdqaTAIrBnsi3YpuEUHmqREUCqnA== X-Received: by 2002:a05:6a20:8410:b0:1ae:3f11:328e with SMTP id c16-20020a056a20841000b001ae3f11328emr9298805pzd.20.1714982359296; Mon, 06 May 2024 00:59:19 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 14/17] zram: add dictionary support to zstd backend Date: Mon, 6 May 2024 16:58:27 +0900 Message-ID: <20240506075834.302472-15-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-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. [1] https://github.com/facebook/zstd/blob/dev/programs/zstd.1.md#dictionary-builder Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 126 ++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 25 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index b2fb94902bef..006d0f40617d 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -12,23 +12,52 @@ struct zstd_ctx { zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; + ZSTD_customMem ctx_mem; + ZSTD_CDict *cdict; + ZSTD_DDict *ddict; s32 level; }; +/* + * Cctx allocator.customAlloc() can be called from zcomp_compress() under + * local-lock (per-CPU compression stream), in which case we must use + * GFP_ATOMIC. + */ +static void *zstd_ctx_alloc(void *opaque, size_t size) +{ + if (!preemptible()) + return kvzalloc(size, GFP_ATOMIC); + + return kvzalloc(size, GFP_KERNEL); +} + +static void zstd_ctx_free(void *opaque, void *address) +{ + kvfree(address); +} + static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; - vfree(zctx->cctx_mem); - vfree(zctx->dctx_mem); + if (zctx->cctx_mem) + vfree(zctx->cctx_mem); + else + ZSTD_freeCCtx(zctx->cctx); + + if (zctx->dctx_mem) + vfree(zctx->dctx_mem); + else + ZSTD_freeDCtx(zctx->dctx); + + ZSTD_freeCDict(zctx->cdict); + ZSTD_freeDDict(zctx->ddict); kfree(zctx); } static void *zstd_create(struct zcomp_config *config) { - zstd_parameters params; struct zstd_ctx *ctx; - size_t sz; ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -39,24 +68,62 @@ static void *zstd_create(struct zcomp_config *config) else ctx->level = ZSTD_defaultCLevel(); - params = zstd_get_params(ctx->level, PAGE_SIZE); - sz = zstd_cctx_workspace_bound(¶ms.cParams); - ctx->cctx_mem = vzalloc(sz); - if (!ctx->cctx_mem) - goto error; - - ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); - if (!ctx->cctx) - goto error; - - sz = zstd_dctx_workspace_bound(); - ctx->dctx_mem = vzalloc(sz); - if (!ctx->dctx_mem) - goto error; - - ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); - if (!ctx->dctx) - goto error; + ctx->ctx_mem.customAlloc = zstd_ctx_alloc; + ctx->ctx_mem.customFree = zstd_ctx_free; + + if (config->dict_sz == 0) { + zstd_parameters params; + size_t sz; + + params = zstd_get_params(ctx->level, PAGE_SIZE); + sz = zstd_cctx_workspace_bound(¶ms.cParams); + ctx->cctx_mem = vzalloc(sz); + if (!ctx->cctx_mem) + goto error; + + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); + if (!ctx->cctx) + goto error; + + sz = zstd_dctx_workspace_bound(); + ctx->dctx_mem = vzalloc(sz); + if (!ctx->dctx_mem) + goto error; + + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); + if (!ctx->dctx) + goto error; + } else { + ZSTD_compressionParameters params; + + ctx->cctx = ZSTD_createCCtx_advanced(ctx->ctx_mem); + if (!ctx->cctx) + goto error; + + ctx->dctx = ZSTD_createDCtx_advanced(ctx->ctx_mem); + if (!ctx->dctx) + goto error; + + params = ZSTD_getCParams(ctx->level, PAGE_SIZE, + config->dict_sz); + + ctx->cdict = ZSTD_createCDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx->ctx_mem); + if (!ctx->cdict) + goto error; + + ctx->ddict = ZSTD_createDDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + ctx->ctx_mem); + if (!ctx->ddict) + goto error; + } return ctx; @@ -72,8 +139,12 @@ static int zstd_compress(void *ctx, const unsigned char *src, const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; - ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, - src, PAGE_SIZE, ¶ms); + if (!zctx->cdict) + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, ¶ms); + else + ret = ZSTD_compress_usingCDict(zctx->cctx, dst, *dst_len, + src, PAGE_SIZE, zctx->cdict); if (zstd_is_error(ret)) return -EINVAL; *dst_len = ret; @@ -86,7 +157,12 @@ static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, struct zstd_ctx *zctx = ctx; size_t ret; - ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); + if (!zctx->ddict) + ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, + src, src_len); + else + ret = ZSTD_decompress_usingDDict(zctx->dctx, dst, PAGE_SIZE, + src, src_len, zctx->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; From patchwork Mon May 6 07:58:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655052 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 200A6144306 for ; Mon, 6 May 2024 07:59:22 +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=1714982363; cv=none; b=CrjkCGImziZvzj8/bUV1uy2jw7v6Rd5ENHPTekSHJKMaeViQwIRE1eEnbTOSCm4nbiRVBF7tWUVG8pwMEXEwY96WG1gbnSXcWNQNBtCcxRpUH33ekBW9fWLFBTXVTjeqtfwm7gryShXRa81ygolvrI4x+5V+bCUv45oKhxi8Hmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982363; c=relaxed/simple; bh=hgmirr95sjRq4R2lLO1A5KsG7QrgS6u7qDlMbRWtxt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FMvedY/3XuR6ZIXaX3V5pzDX0ARlQi9ZdAAJRDC+6WIRgOypWzGb4jx70bBh1wzLKXBSwCyWXf+lo5/oX0TgNPXSv/eA6zgxOhxE5aAoGr1Oqs1MjrbHxKgCZAet1CH66WvE9FnY1xhXng03jD8FtzSkCxHr3KlaaFfhhIr/jaE= 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=X1mtT/+Z; 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="X1mtT/+Z" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1e3ca546d40so10727095ad.3 for ; Mon, 06 May 2024 00:59:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982361; x=1715587161; 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=VSKm1cjEfmY4DxWo94ytvPqckCgfxE12U1aF5qaMLGw=; b=X1mtT/+ZYU6mlKgIXoZYqTRnQwptdNaZ+k53wmeX6XjpTFiFU+urXxWZDO5kiHb1AX EObjV08CbRNqA4HM0Zf20Z9cO+SqyPgfs3LVQL2VAWu5ase9csAhFzj0Y6zQGfyA02hG rj6BW/jtOljEm0Z4BmgtDkmqRzAY/q3JR67yQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982361; x=1715587161; 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=VSKm1cjEfmY4DxWo94ytvPqckCgfxE12U1aF5qaMLGw=; b=iEFQwo79gqJpFqnGFa6G+lz9bo4PaZfr7F3COBF08On6WLJpPbtAARoj77abtg7ZvL g27LtZXKXHsp/YrbItylkqEtKK8q0j0WYjt0MZDMad+ojqqObPHrdDgthWdoMYnJ+Z0p pxr4X9maYO95te9NUMXBmVUOBSyKEutCq0Kj/nD0uiKHLBg7VNuop1Uo3mzC6CC+a66X pdNcOkawZtxyBoHmNA7LUxyXcFUwt3dh5WFXDoAn6b3mgs580btvortBN/CxHCfJwEoK y6ycS3MztkgWJUVDQafEYaAffeTO4cLYvfyjAStBEdtLBg0gNUBdenqQKhHcuesp5XKQ 80Rg== X-Forwarded-Encrypted: i=1; AJvYcCW2IpZc5H2Q9TUxOEi4OySE+FbQ46v92Eqh/asQi02MdtJ68t28LD4ZW4L00CEuKUyBSUZQ4pvmlB992dUwWZUMwtWdi4acoMYpSFI= X-Gm-Message-State: AOJu0YwfuIoF2Qs1DvN3oQ48nW+VoDVgZAIulwHimF3v9gbXp/uPmlDU 75dhMMPrySdHagaUPYeK4+cFetz8IV6ZiK/C15jr7M8pgxObV20i7Q25YY1mcw== X-Google-Smtp-Source: AGHT+IH2B2dFPj5B0HG2Sh37UkCiY0lzwsBPBKoQzfd0hcQDmjpeVeY9Oi0u/Lzg5YDZ40Ky0jFHrQ== X-Received: by 2002:a17:903:22d1:b0:1e2:307f:d283 with SMTP id y17-20020a17090322d100b001e2307fd283mr11473620plg.1.1714982361528; Mon, 06 May 2024 00:59:21 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 15/17] zram: add config init/release backend callbacks Date: Mon, 6 May 2024 16:58:28 +0900 Message-ID: <20240506075834.302472-16-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some backends can create a backend-specific private data for comp config, e.g. parse a dictionary and use it to init all of the ctx-s later on. Introduce two zcomp_config callbacks to create and destroy per-config backend private data. This is also the place where config can be validated. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_842.c | 11 +++++++++++ drivers/block/zram/backend_deflate.c | 20 +++++++++++++++----- drivers/block/zram/backend_lz4.c | 11 +++++++++++ drivers/block/zram/backend_lz4hc.c | 20 +++++++++++++++----- drivers/block/zram/backend_lzo.c | 11 +++++++++++ drivers/block/zram/backend_lzorle.c | 11 +++++++++++ drivers/block/zram/backend_zstd.c | 20 +++++++++++++++----- drivers/block/zram/zcomp.c | 6 ++++++ drivers/block/zram/zcomp.h | 4 ++++ drivers/block/zram/zram_drv.c | 3 +++ 10 files changed, 102 insertions(+), 15 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c index 12e716deb763..1522aa88ac35 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -11,6 +11,15 @@ struct sw842_ctx { void *mem; }; +static int init_config_842(struct zcomp_config *config) +{ + return 0; +} + +static void release_config_842(struct zcomp_config *config) +{ +} + static void destroy_842(void *ctx) { struct sw842_ctx *zctx = ctx; @@ -64,5 +73,7 @@ struct zcomp_backend backend_842 = { .decompress = decompress_842, .create_ctx = create_842, .destroy_ctx = destroy_842, + .init_config = init_config_842, + .release_config = release_config_842, .name = "842", }; diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c index 83c660adc722..2e166b507f5a 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -17,6 +17,18 @@ struct deflate_ctx { s32 level; }; +static int deflate_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = Z_DEFAULT_COMPRESSION; + + return 0; +} + +static void deflate_release_config(struct zcomp_config *config) +{ +} + static void deflate_destroy(void *ctx) { struct deflate_ctx *zctx = ctx; @@ -42,11 +54,7 @@ static void *deflate_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = Z_DEFAULT_COMPRESSION; - + ctx->level = config->level; sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); ctx->cctx.workspace = vzalloc(sz); if (!ctx->cctx.workspace) @@ -129,5 +137,7 @@ struct zcomp_backend backend_deflate = { .decompress = deflate_decompress, .create_ctx = deflate_create, .destroy_ctx = deflate_destroy, + .init_config = deflate_init_config, + .release_config = deflate_release_config, .name = "deflate", }; diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 98d9c9274149..560fcf139301 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -4,6 +4,15 @@ #include "backend_lz4.h" +static int lz4_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lz4_release_config(struct zcomp_config *config) +{ +} + static void *lz4_create(struct zcomp_config *config) { return vmalloc(LZ4_MEM_COMPRESS); @@ -43,5 +52,7 @@ struct zcomp_backend backend_lz4 = { .decompress = lz4_decompress, .create_ctx = lz4_create, .destroy_ctx = lz4_destroy, + .init_config = lz4_init_config, + .release_config = lz4_release_config, .name = "lz4", }; diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 62721a7c82dc..ebc88261cb72 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -10,6 +10,18 @@ struct lz4hc_ctx { s32 level; }; +static int lz4hc_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = LZ4HC_DEFAULT_CLEVEL; + + return 0; +} + +static void lz4hc_release_config(struct zcomp_config *config) +{ +} + static void lz4hc_destroy(void *ctx) { struct lz4hc_ctx *zctx = ctx; @@ -26,11 +38,7 @@ static void *lz4hc_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = LZ4HC_DEFAULT_CLEVEL; - + ctx->level = config->level; ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); if (!ctx->mem) { lz4hc_destroy(ctx); @@ -70,5 +78,7 @@ struct zcomp_backend backend_lz4hc = { .decompress = lz4hc_decompress, .create_ctx = lz4hc_create, .destroy_ctx = lz4hc_destroy, + .init_config = lz4hc_init_config, + .release_config = lz4hc_release_config, .name = "lz4hc", }; diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c index 79ecfae9c4c5..75e0e3d297aa 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -6,6 +6,15 @@ #include "backend_lzo.h" +static int lzo_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lzo_release_config(struct zcomp_config *config) +{ +} + static void *lzo_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); @@ -40,5 +49,7 @@ struct zcomp_backend backend_lzo = { .decompress = lzo_decompress, .create_ctx = lzo_create, .destroy_ctx = lzo_destroy, + .init_config = lzo_init_config, + .release_config = lzo_release_config, .name = "lzo", }; diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c index b0937103b5fb..d1dc2e35061b 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -6,6 +6,15 @@ #include "backend_lzorle.h" +static int lzorle_init_config(struct zcomp_config *config) +{ + return 0; +} + +static void lzorle_release_config(struct zcomp_config *config) +{ +} + static void *lzorle_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); @@ -40,5 +49,7 @@ struct zcomp_backend backend_lzorle = { .decompress = lzorle_decompress, .create_ctx = lzorle_create, .destroy_ctx = lzorle_destroy, + .init_config = lzorle_init_config, + .release_config = lzorle_release_config, .name = "lzo-rle", }; diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 006d0f40617d..df59584b0337 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -36,6 +36,18 @@ static void zstd_ctx_free(void *opaque, void *address) kvfree(address); } +static int zstd_init_config(struct zcomp_config *config) +{ + if (config->level == ZCOMP_CONFIG_NO_LEVEL) + config->level = ZSTD_defaultCLevel(); + + return 0; +} + +static void zstd_release_config(struct zcomp_config *config) +{ +} + static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; @@ -63,11 +75,7 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx) return NULL; - if (config->level != ZCOMP_CONFIG_NO_LEVEL) - ctx->level = config->level; - else - ctx->level = ZSTD_defaultCLevel(); - + ctx->level = config->level; ctx->ctx_mem.customAlloc = zstd_ctx_alloc; ctx->ctx_mem.customFree = zstd_ctx_free; @@ -173,5 +181,7 @@ struct zcomp_backend backend_zstd = { .decompress = zstd_decompress, .create_ctx = zstd_create, .destroy_ctx = zstd_destroy, + .init_config = zstd_init_config, + .release_config = zstd_release_config, .name = "zstd", }; diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 3f990a715487..a7013a4b6575 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -187,18 +187,24 @@ static int zcomp_init(struct zcomp *comp) if (!comp->stream) return -ENOMEM; + ret = comp->backend->init_config(comp->config); + if (ret) + goto cleanup; + ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node); if (ret < 0) goto cleanup; return 0; cleanup: + comp->backend->release_config(comp->config); free_percpu(comp->stream); return ret; } void zcomp_destroy(struct zcomp *comp) { + comp->backend->release_config(comp->config); cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node); free_percpu(comp->stream); kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 345c78bc76db..aa604e5db7ad 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -22,6 +22,7 @@ struct zcomp_config { s32 level; size_t dict_sz; void *dict; + void *private; }; struct zcomp_backend { @@ -34,6 +35,9 @@ struct zcomp_backend { void *(*create_ctx)(struct zcomp_config *config); void (*destroy_ctx)(void *ctx); + int (*init_config)(struct zcomp_config *config); + void (*release_config)(struct zcomp_config *config); + const char *name; }; diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 863e4e125eaa..d667fe2d0cd9 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1018,6 +1018,9 @@ static void __reset_comp_config(struct zram *zram, u32 prio) { struct zcomp_config *config = &zram->configs[prio]; + /* config->private should be freed by the backend */ + WARN_ON_ONCE(config->private); + vfree(config->dict); config->level = ZCOMP_CONFIG_NO_LEVEL; config->dict_sz = 0; From patchwork Mon May 6 07:58:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655053 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 594101448D4 for ; Mon, 6 May 2024 07:59:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982365; cv=none; b=oJxmuA6zqgztmWBdShFwF5FtjDY3+T3F8XVHpdr02Yvz1TJUlWvFN2rVgb6XgeO4N5Vlb/ifUYOhJsOxzB/XsxbZAHTdFn917S/Y290kdito1N2rbKQTPVR0k3VUtQ/5yran5EXU4E9EQgwL0yEO21Fbq8NE9NQ+ltBKOunWRXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982365; c=relaxed/simple; bh=d3vFRBqQt4rMVioo0/wvp6HT9RZQh4Y89EivFcAwXXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nsYJemnbEHedgG1hjllNzO+XL8tQfBZq+v/1ga4+4vt8Geh33VdN0hB8qMIGutx+6IBK5Cnx+tDyTWREKlzHaiTu1wLlz42z+gno0GgnSFaGPGzYod8sq3JmSlql5cNVvD57PgcoAArGjfdZnXXb1oXsO8sROHfMZ+ufoXFLSFg= 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=a22O3M7h; arc=none smtp.client-ip=209.85.214.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="a22O3M7h" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1ec4dc64c6cso10587605ad.0 for ; Mon, 06 May 2024 00:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982363; x=1715587163; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UawFcLvcVVuz6l7D9w7wTxKhqDne11bGdAUI9Tc6zcg=; b=a22O3M7hgUiDPYJIGXlyVHQidISMahs7I3evFaWbwdIjJcy83YHo79I1jgi8TvMC0H fhu4fSQuEG1DyBtwmsvUK8n3CD+OnBZYgtv1u8ALYxcyKazYycvEKfAZfi0FqYm9UgqN fXYvpenhLgVgQko/Ay9VJtmD0vIHr7E015k9Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982363; x=1715587163; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UawFcLvcVVuz6l7D9w7wTxKhqDne11bGdAUI9Tc6zcg=; b=fQS8MctCCvOI3lii9bXUjdCVrIPYdNc2sbV6hAi33X5bRiNkbrSnaD6gI/raKxmc3i znvDSW85YClX22/FnCbS8PEnRjig66O/IJ1TTVgfoqtZ0F/wcLccgtSM+GKLuo1UEE3R ROaSk6zz8GUpjX8+OxiQUONfnlhGTHDKe8Bm6KjxV+/pJVaCo3YNu0rhgLdVnBfxs27S h0Nbd2eOVdXtOr38j4Kvf3IOBCDbeAPqPu2QokvmFfEvuVq+F4WeuMpGaIa7VROi4Fgn CU8JLOCcTvHRHDYiViWtdqxauHYKjBROByF6LiV2O5gzGAHRyVCvD7Yih3SgOTy+2ANJ 6ktA== X-Forwarded-Encrypted: i=1; AJvYcCV6TDQ7D+yBgLEfDXF7Bep1f/fVt4Ug0fFRVRf6s5comsnSOshCX5wxm+E3wmmD+zJlbVsf56Q+CfwUADxthlYY0RnDhH82D0OGhQU= X-Gm-Message-State: AOJu0YyXi5tYdDz1ZbvgJOt9bVxbjjtAx9BfzuQ+/RkOJSV6lSK8DJHg gh6WWmZyfjw1NIP3e77qjki2d4RES6NwgQDq3m5+jhUuVmzkVTyZNuXMN8Z9AQ== X-Google-Smtp-Source: AGHT+IHmZ+eYgC3Tq1EfdPkOv57BYc61jK6mkSfnt3zGgOMiuOEVcQPHHosKuKLxkmgHUlN/n5sCSQ== X-Received: by 2002:a17:902:eb91:b0:1ee:2a58:cb7c with SMTP id q17-20020a170902eb9100b001ee2a58cb7cmr1021125plg.35.1714982363738; Mon, 06 May 2024 00:59:23 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 16/17] zram: share dictionaries between per-CPU contexts Date: Mon, 6 May 2024 16:58:29 +0900 Message-ID: <20240506075834.302472-17-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 zstd's CDict and DDict are accessed read-only during compression and decompression, so instead of allocation per-context dictionaries we can create just one CDict and DDict in init_config() and make per-CPU contexts use them. This saves quite a lot of memory: on my system CDict requires 408128 bytes and DDict requires 27352 bytes. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_zstd.c | 106 ++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 36 deletions(-) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index df59584b0337..99115b21c264 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -7,14 +7,18 @@ #include "backend_zstd.h" +struct zstd_ctx_data { + ZSTD_customMem ctx_mem; + ZSTD_CDict *cdict; + ZSTD_DDict *ddict; +}; + struct zstd_ctx { zstd_cctx *cctx; zstd_dctx *dctx; void *cctx_mem; void *dctx_mem; - ZSTD_customMem ctx_mem; - ZSTD_CDict *cdict; - ZSTD_DDict *ddict; + struct zstd_ctx_data *ctx_data; s32 level; }; @@ -38,32 +42,81 @@ static void zstd_ctx_free(void *opaque, void *address) static int zstd_init_config(struct zcomp_config *config) { + struct zstd_ctx_data *ctx_data = config->private; + ZSTD_compressionParameters params; + + /* Already initialized */ + if (ctx_data) + return 0; + if (config->level == ZCOMP_CONFIG_NO_LEVEL) config->level = ZSTD_defaultCLevel(); + if (config->dict_sz == 0) + return 0; + + ctx_data = kzalloc(sizeof(*ctx_data), GFP_KERNEL); + if (!ctx_data) + return -ENOMEM; + + ctx_data->ctx_mem.customAlloc = zstd_ctx_alloc; + ctx_data->ctx_mem.customFree = zstd_ctx_free; + + params = ZSTD_getCParams(config->level, PAGE_SIZE, config->dict_sz); + + ctx_data->cdict = ZSTD_createCDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + params, + ctx_data->ctx_mem); + if (!ctx_data->cdict) + goto error; + + ctx_data->ddict = ZSTD_createDDict_advanced(config->dict, + config->dict_sz, + ZSTD_dlm_byRef, + ZSTD_dct_auto, + ctx_data->ctx_mem); + if (!ctx_data->ddict) + goto error; + + config->private = ctx_data; return 0; + +error: + ZSTD_freeCDict(ctx_data->cdict); + ZSTD_freeDDict(ctx_data->ddict); + kfree(ctx_data); + return -EINVAL; } static void zstd_release_config(struct zcomp_config *config) { + struct zstd_ctx_data *ctx_data = config->private; + + if (!ctx_data) + return; + + config->private = NULL; + ZSTD_freeCDict(ctx_data->cdict); + ZSTD_freeDDict(ctx_data->ddict); + kfree(ctx_data); } static void zstd_destroy(void *ctx) { struct zstd_ctx *zctx = ctx; + /* Don't free zctx->ctx_data, it's done in release_config() */ if (zctx->cctx_mem) vfree(zctx->cctx_mem); else ZSTD_freeCCtx(zctx->cctx); - if (zctx->dctx_mem) vfree(zctx->dctx_mem); else ZSTD_freeDCtx(zctx->dctx); - - ZSTD_freeCDict(zctx->cdict); - ZSTD_freeDDict(zctx->ddict); kfree(zctx); } @@ -75,9 +128,8 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx) return NULL; + ctx->ctx_data = config->private; ctx->level = config->level; - ctx->ctx_mem.customAlloc = zstd_ctx_alloc; - ctx->ctx_mem.customFree = zstd_ctx_free; if (config->dict_sz == 0) { zstd_parameters params; @@ -102,35 +154,15 @@ static void *zstd_create(struct zcomp_config *config) if (!ctx->dctx) goto error; } else { - ZSTD_compressionParameters params; + struct zstd_ctx_data *ctx_data = ctx->ctx_data; - ctx->cctx = ZSTD_createCCtx_advanced(ctx->ctx_mem); + ctx->cctx = ZSTD_createCCtx_advanced(ctx_data->ctx_mem); if (!ctx->cctx) goto error; - ctx->dctx = ZSTD_createDCtx_advanced(ctx->ctx_mem); + ctx->dctx = ZSTD_createDCtx_advanced(ctx_data->ctx_mem); if (!ctx->dctx) goto error; - - params = ZSTD_getCParams(ctx->level, PAGE_SIZE, - config->dict_sz); - - ctx->cdict = ZSTD_createCDict_advanced(config->dict, - config->dict_sz, - ZSTD_dlm_byRef, - ZSTD_dct_auto, - params, - ctx->ctx_mem); - if (!ctx->cdict) - goto error; - - ctx->ddict = ZSTD_createDDict_advanced(config->dict, - config->dict_sz, - ZSTD_dlm_byRef, - ZSTD_dct_auto, - ctx->ctx_mem); - if (!ctx->ddict) - goto error; } return ctx; @@ -144,15 +176,16 @@ static int zstd_compress(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len) { struct zstd_ctx *zctx = ctx; + struct zstd_ctx_data *ctx_data = zctx->ctx_data; const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); size_t ret; - if (!zctx->cdict) + if (!ctx_data) ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, src, PAGE_SIZE, ¶ms); else ret = ZSTD_compress_usingCDict(zctx->cctx, dst, *dst_len, - src, PAGE_SIZE, zctx->cdict); + src, PAGE_SIZE, ctx_data->cdict); if (zstd_is_error(ret)) return -EINVAL; *dst_len = ret; @@ -163,14 +196,15 @@ static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst) { struct zstd_ctx *zctx = ctx; + struct zstd_ctx_data *ctx_data = zctx->ctx_data; size_t ret; - if (!zctx->ddict) + if (!ctx_data) ret = zstd_decompress_dctx(zctx->dctx, dst, PAGE_SIZE, src, src_len); else ret = ZSTD_decompress_usingDDict(zctx->dctx, dst, PAGE_SIZE, - src, src_len, zctx->ddict); + src, src_len, ctx_data->ddict); if (zstd_is_error(ret)) return -EINVAL; return 0; From patchwork Mon May 6 07:58:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655054 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 7E4E11448FA for ; Mon, 6 May 2024 07:59:26 +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=1714982367; cv=none; b=o4UGThhHP7d4WqW7Bk3D2GLtHs0/R0vl+IwIsDWFLvtLgKyCTjjHMXPooiIimNshvszySrYn9+vE433OzrDRK4QboJUJeVyWpgmhlnBETt1HeS5wM9t9luCa4xT+4iZxLxX8ETXPhRwdqXT5RZyRlGJh3vtdsE0XZLSnxi0iaTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982367; c=relaxed/simple; bh=0QKD/VRLqE4UJhTMThwx4W0GPaxzgatlvvK+3EE9Gd0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PzVoeSy/W6fsfQOKHGkDapZmGnAkJ9cOTVv+7MeyCW6gRjUFEllqszc+WjboaLHLmMoHuLSmit1r+ezYRfXbJQMPl3A1qTBVj4FZta5VIJgjFXndF/7CR0GMWJm4NkrFDfU91rFAAAIGsh6pvZcOsBa2J3RX3G9378B4Yk2zw30= 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=HNK33Wdi; 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="HNK33Wdi" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1ec486198b6so11724765ad.1 for ; Mon, 06 May 2024 00:59:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982366; x=1715587166; 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=HNK33WdiJNKGrbAzcZCJfzRSc2z/Ri2zshCOD6DA24APChdR3JA0J5CAzCO7q1SU87 PUfREEHBg1dSMqwo1vbGueiEI+jU798eebatD2M/LbL2cTIp/61JNGN0+ycnumaFZHfX mQ1cPkJlCyCjPahkp/6LsvqX+18eBNHB3+oxk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982366; x=1715587166; 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=GE6xD87ZPngGHWyt8O5+LK9zwk1N35/9jzyHB544S6Y2BbUW6kifhg/pMNlaEH1OEW xA+bJcNTexpRU3VffMtGEVOvJc+ufXSxxUjmA4BMh4M/ksxbSZzP67KMN+X1HNZiYgjd qbOk4lpkedjrFQgUac6vp8Q65qz+3sbN6SGW8LkYdRvsbeSX1pq8C4ZLQ2gyuRg9e8zg msYJViw0jDQN0QhEJ0Fyk7uebDJsP0XlmWCyM3RM/eZfHWcblUO2xT2l+9+EnWhqbPt2 cxLhc8IgPdmeYgZdXTyWfY5dZetRP7YjlmaEjfgX0iR8gC5X2qIZgzkhAqAk1FjKMAd/ enng== X-Forwarded-Encrypted: i=1; AJvYcCU7A1fU1DJ/uyUROpI5kliuiWWZ+9s9yJH0uYs4BgDYzubw7H4w7aCl+Um6F+N1HhWODPYtgWrXaUAqUlgwEOk46g+BNqq2udZhtHI= X-Gm-Message-State: AOJu0YxFYZVAo44VmiPew/MjMC8tlDz0AaCXwyZ0m9uzE/b4HUuGZStb kTiZMW+LTVYfJqiyMd+h1sSPB8D9bPafyHczkheRy1klac4nf+blXEUWPcrrqw== X-Google-Smtp-Source: AGHT+IGtD6df0LGz8qgNCcFIQycCbTSE1kkQVyBBaYoc1cmb4rEblCnjBd4Fq0l6WUO2JRvfpM/M5A== X-Received: by 2002:a17:902:e749:b0:1e3:cf2b:7151 with SMTP id p9-20020a170902e74900b001e3cf2b7151mr12883681plf.59.1714982365968; Mon, 06 May 2024 00:59:25 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59: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: [PATCHv2 17/17] Documentation/zram: add documentation for algorithm parameters Date: Mon, 6 May 2024 16:58:30 +0900 Message-ID: <20240506075834.302472-18-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-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.