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: [PATCH RFC 2/7] zram: Add recompression algorithm sysfs knob
Date: Mon, 5 Sep 2022 17:23:18 +0900 [thread overview]
Message-ID: <20220905082323.2742399-3-senozhatsky@chromium.org> (raw)
In-Reply-To: <20220905082323.2742399-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 4ad1daa1283e..694c8c426cb2 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;
@@ -1011,31 +1016,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;
@@ -1064,11 +1075,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)
{
@@ -1770,7 +1825,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);
}
@@ -1912,6 +1971,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,
@@ -1935,6 +1997,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,
};
@@ -2014,7 +2079,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.37.2.789.g6183377224-goog
next prev parent reply other threads:[~2022-09-05 8:23 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-05 8:23 [PATCH RFC 0/7] zram: Support multiple compression streams Sergey Senozhatsky
2022-09-05 8:23 ` [PATCH RFC 1/7] zram: Preparation for multi-zcomp support Sergey Senozhatsky
2022-09-05 8:23 ` Sergey Senozhatsky [this message]
2022-09-05 8:23 ` [PATCH RFC 3/7] zram: Factor out WB and non-WB zram read functions Sergey Senozhatsky
2022-09-05 8:23 ` [PATCH RFC 4/7] zram: Introduce recompress sysfs knob Sergey Senozhatsky
2022-09-12 10:05 ` Sergey Senozhatsky
2022-09-05 8:23 ` [PATCH RFC 5/7] documentation: Add recompression documentation Sergey Senozhatsky
2022-09-05 8:23 ` [PATCH RFC 6/7] zram: Add recompression algorithm choice to Kconfig Sergey Senozhatsky
2022-09-05 8:23 ` [PATCH RFC 7/7] zram: Add recompress flag to read_block_state() Sergey Senozhatsky
-- strict thread matches above, loose matches on Subject: below --
2022-09-05 8:15 [PATCH RFC 0/7] zram: Support multiple compression streams Sergey Senozhatsky
2022-09-05 8:15 ` [PATCH RFC 2/7] zram: Add recompression algorithm sysfs knob 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=20220905082323.2742399-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