[PATCH RFC 2/7] zram: Add recompression algorithm sysfs knob

From: Sergey Senozhatsky
Date: Mon Sep 05 2022 - 04:24:06 EST


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@xxxxxxxxxxxx>
---
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