From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Minchan Kim <minchan@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Nitin Gupta <ngupta@vflare.org>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: [PATCHv3 2/8] zram: Add recompression algorithm sysfs knob
Date: Sun, 9 Oct 2022 18:07:14 +0900 [thread overview]
Message-ID: <20221009090720.1040633-3-senozhatsky@chromium.org> (raw)
In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org>
Introduce recomp_algorithm sysfs knob that controls
secondary algorithm selection used for recompression.
This device attribute works in a similar way with
comp_algorithm attribute.
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
drivers/block/zram/zram_drv.c | 111 +++++++++++++++++++++++++++-------
1 file changed, 90 insertions(+), 21 deletions(-)
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 770ea3489eb6..a8ef3c0c3dae 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -41,7 +41,12 @@ static DEFINE_IDR(zram_index_idr);
static DEFINE_MUTEX(zram_index_mutex);
static int zram_major;
-static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
+static const char *default_comp_algs[ZRAM_MAX_ZCOMPS] = {
+ CONFIG_ZRAM_DEF_COMP,
+#ifdef CONFIG_ZRAM_MULTI_COMP
+ "zstd",
+#endif
+};
/* Module params (documentation at end) */
static unsigned int num_devices = 1;
@@ -1000,31 +1005,37 @@ static ssize_t max_comp_streams_store(struct device *dev,
return len;
}
-static ssize_t comp_algorithm_show(struct device *dev,
- struct device_attribute *attr, char *buf)
+static void comp_algorithm_set(struct zram *zram, u32 idx, const char *alg)
{
- size_t sz;
- struct zram *zram = dev_to_zram(dev);
+ bool default_alg = false;
+ int i;
- down_read(&zram->init_lock);
- sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_ZCOMP], buf);
- up_read(&zram->init_lock);
+ /* Do not kfree() algs that we didn't allocate, IOW the default ones */
+ for (i = 0; i < ZRAM_MAX_ZCOMPS; i++) {
+ if (zram->comp_algs[idx] == default_comp_algs[i]) {
+ default_alg = true;
+ break;
+ }
+ }
- return sz;
+ if (!default_alg)
+ kfree(zram->comp_algs[idx]);
+ zram->comp_algs[idx] = alg;
}
-static void comp_algorithm_set(struct zram *zram, u32 idx, const char *alg)
+static ssize_t __comp_algorithm_show(struct zram *zram, u32 idx, char *buf)
{
- /* Do not kfree() algs that we didn't allocate, IOW the default ones */
- if (zram->comp_algs[idx] != default_compressor)
- kfree(zram->comp_algs[idx]);
- zram->comp_algs[idx] = alg;
+ ssize_t sz;
+
+ down_read(&zram->init_lock);
+ sz = zcomp_available_show(zram->comp_algs[idx], buf);
+ up_read(&zram->init_lock);
+
+ return sz;
}
-static ssize_t comp_algorithm_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t len)
+static int __comp_algorithm_store(struct zram *zram, u32 idx, const char *buf)
{
- struct zram *zram = dev_to_zram(dev);
char *compressor;
size_t sz;
@@ -1053,11 +1064,55 @@ static ssize_t comp_algorithm_store(struct device *dev,
return -EBUSY;
}
- comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, compressor);
+ comp_algorithm_set(zram, idx, compressor);
up_write(&zram->init_lock);
- return len;
+ return 0;
+}
+
+static ssize_t comp_algorithm_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct zram *zram = dev_to_zram(dev);
+
+ return __comp_algorithm_show(zram, ZRAM_PRIMARY_ZCOMP, buf);
+}
+
+static ssize_t comp_algorithm_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t len)
+{
+ struct zram *zram = dev_to_zram(dev);
+ int ret;
+
+ ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_ZCOMP, buf);
+ return ret ? ret : len;
}
+#ifdef CONFIG_ZRAM_MULTI_COMP
+static ssize_t recomp_algorithm_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct zram *zram = dev_to_zram(dev);
+
+ return __comp_algorithm_show(zram, ZRAM_SECONDARY_ZCOMP, buf);
+}
+
+static ssize_t recomp_algorithm_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t len)
+{
+ struct zram *zram = dev_to_zram(dev);
+ int ret;
+
+ ret = __comp_algorithm_store(zram, ZRAM_SECONDARY_ZCOMP, buf);
+ return ret ? ret : len;
+}
+#endif
+
static ssize_t compact_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t len)
{
@@ -1762,7 +1817,11 @@ static void zram_reset_device(struct zram *zram)
memset(&zram->stats, 0, sizeof(zram->stats));
reset_bdev(zram);
- comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, default_compressor);
+ comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP,
+ default_comp_algs[ZRAM_PRIMARY_ZCOMP]);
+ if (IS_ENABLED(CONFIG_ZRAM_MULTI_COMP))
+ comp_algorithm_set(zram, ZRAM_SECONDARY_ZCOMP,
+ default_comp_algs[ZRAM_SECONDARY_ZCOMP]);
up_write(&zram->init_lock);
}
@@ -1895,6 +1954,9 @@ static DEVICE_ATTR_WO(writeback);
static DEVICE_ATTR_RW(writeback_limit);
static DEVICE_ATTR_RW(writeback_limit_enable);
#endif
+#ifdef CONFIG_ZRAM_MULTI_COMP
+static DEVICE_ATTR_RW(recomp_algorithm);
+#endif
static struct attribute *zram_disk_attrs[] = {
&dev_attr_disksize.attr,
@@ -1918,6 +1980,9 @@ static struct attribute *zram_disk_attrs[] = {
&dev_attr_bd_stat.attr,
#endif
&dev_attr_debug_stat.attr,
+#ifdef CONFIG_ZRAM_MULTI_COMP
+ &dev_attr_recomp_algorithm.attr,
+#endif
NULL,
};
@@ -1997,7 +2062,11 @@ static int zram_add(void)
if (ret)
goto out_cleanup_disk;
- zram->comp_algs[ZRAM_PRIMARY_ZCOMP] = default_compressor;
+ zram->comp_algs[ZRAM_PRIMARY_ZCOMP] =
+ default_comp_algs[ZRAM_PRIMARY_ZCOMP];
+ if (IS_ENABLED(CONFIG_ZRAM_MULTI_COMP))
+ zram->comp_algs[ZRAM_SECONDARY_ZCOMP] =
+ default_comp_algs[ZRAM_SECONDARY_ZCOMP];
zram_debugfs_register(zram);
pr_info("Added device: %s\n", zram->disk->disk_name);
--
2.38.0.rc1.362.ged0d419d3c-goog
next prev parent reply other threads:[~2022-10-09 9:07 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-09 9:07 [PATCHv3 0/8] zram: Support multiple compression streams Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 1/8] zram: Preparation for multi-zcomp support Sergey Senozhatsky
2022-10-09 9:07 ` Sergey Senozhatsky [this message]
2022-10-09 9:07 ` [PATCHv3 3/8] zram: Factor out WB and non-WB zram read functions Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 4/8] zram: Introduce recompress sysfs knob Sergey Senozhatsky
2022-10-18 0:08 ` Andrew Morton
2022-10-18 2:12 ` Sergey Senozhatsky
2022-10-18 2:42 ` Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 5/8] documentation: Add recompression documentation Sergey Senozhatsky
2022-10-18 2:22 ` Sergey Senozhatsky
2022-10-18 3:11 ` Andrew Morton
2022-10-18 2:50 ` Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 6/8] zram: Add recompression algorithm choice to Kconfig Sergey Senozhatsky
2022-10-18 3:07 ` Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 7/8] zram: Add recompress flag to read_block_state() Sergey Senozhatsky
2022-10-09 9:07 ` [PATCHv3 8/8] zram: correct typos Sergey Senozhatsky
2022-10-18 0:08 ` Andrew Morton
2022-10-18 2:10 ` Sergey Senozhatsky
2022-10-18 3:11 ` [PATCHv3 0/8] zram: Support multiple compression streams Sergey Senozhatsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221009090720.1040633-3-senozhatsky@chromium.org \
--to=senozhatsky@chromium.org \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=ngupta@vflare.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).