Merge tag 'mm-stable-2024-09-20-02-31' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / block / zram / zram_drv.c
index 84cd62efac0f640d21c364e55b2dc14eebadee6a..c3d245617083df1f20b36f94e698c1e5c8a0a694 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/debugfs.h>
 #include <linux/cpuhotplug.h>
 #include <linux/part_stat.h>
+#include <linux/kernel_read_file.h>
 
 #include "zram_drv.h"
 
@@ -998,6 +999,103 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
        return 0;
 }
 
+static void comp_params_reset(struct zram *zram, u32 prio)
+{
+       struct zcomp_params *params = &zram->params[prio];
+
+       vfree(params->dict);
+       params->level = ZCOMP_PARAM_NO_LEVEL;
+       params->dict_sz = 0;
+       params->dict = NULL;
+}
+
+static int comp_params_store(struct zram *zram, u32 prio, s32 level,
+                            const char *dict_path)
+{
+       ssize_t sz = 0;
+
+       comp_params_reset(zram, prio);
+
+       if (dict_path) {
+               sz = kernel_read_file_from_path(dict_path, 0,
+                                               &zram->params[prio].dict,
+                                               INT_MAX,
+                                               NULL,
+                                               READING_POLICY);
+               if (sz < 0)
+                       return -EINVAL;
+       }
+
+       zram->params[prio].dict_sz = sz;
+       zram->params[prio].level = level;
+       return 0;
+}
+
+static ssize_t algorithm_params_store(struct device *dev,
+                                     struct device_attribute *attr,
+                                     const char *buf,
+                                     size_t len)
+{
+       s32 prio = ZRAM_PRIMARY_COMP, level = ZCOMP_PARAM_NO_LEVEL;
+       char *args, *param, *val, *algo = NULL, *dict_path = NULL;
+       struct zram *zram = dev_to_zram(dev);
+       int ret;
+
+       args = skip_spaces(buf);
+       while (*args) {
+               args = next_arg(args, &param, &val);
+
+               if (!val || !*val)
+                       return -EINVAL;
+
+               if (!strcmp(param, "priority")) {
+                       ret = kstrtoint(val, 10, &prio);
+                       if (ret)
+                               return ret;
+                       continue;
+               }
+
+               if (!strcmp(param, "level")) {
+                       ret = kstrtoint(val, 10, &level);
+                       if (ret)
+                               return ret;
+                       continue;
+               }
+
+               if (!strcmp(param, "algo")) {
+                       algo = val;
+                       continue;
+               }
+
+               if (!strcmp(param, "dict")) {
+                       dict_path = val;
+                       continue;
+               }
+       }
+
+       /* Lookup priority by algorithm name */
+       if (algo) {
+               s32 p;
+
+               prio = -EINVAL;
+               for (p = ZRAM_PRIMARY_COMP; p < ZRAM_MAX_COMPS; p++) {
+                       if (!zram->comp_algs[p])
+                               continue;
+
+                       if (!strcmp(zram->comp_algs[p], algo)) {
+                               prio = p;
+                               break;
+                       }
+               }
+       }
+
+       if (prio < ZRAM_PRIMARY_COMP || prio >= ZRAM_MAX_COMPS)
+               return -EINVAL;
+
+       ret = comp_params_store(zram, prio, level, dict_path);
+       return ret ? ret : len;
+}
+
 static ssize_t comp_algorithm_show(struct device *dev,
                                   struct device_attribute *attr,
                                   char *buf)
@@ -1330,7 +1428,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]);
        }
@@ -1417,7 +1516,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)) {
@@ -1604,7 +1704,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) {
@@ -1757,6 +1858,18 @@ static ssize_t recompress_store(struct device *dev,
                        algo = val;
                        continue;
                }
+
+               if (!strcmp(param, "priority")) {
+                       ret = kstrtouint(val, 10, &prio);
+                       if (ret)
+                               return ret;
+
+                       if (prio == ZRAM_PRIMARY_COMP)
+                               prio = ZRAM_SECONDARY_COMP;
+
+                       prio_max = min(prio + 1, ZRAM_MAX_COMPS);
+                       continue;
+               }
        }
 
        if (threshold >= huge_class_size)
@@ -1979,6 +2092,15 @@ static void zram_slot_free_notify(struct block_device *bdev,
        zram_slot_unlock(zram, index);
 }
 
+static void zram_comp_params_reset(struct zram *zram)
+{
+       u32 prio;
+
+       for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
+               comp_params_reset(zram, prio);
+       }
+}
+
 static void zram_destroy_comps(struct zram *zram)
 {
        u32 prio;
@@ -1992,6 +2114,13 @@ static void zram_destroy_comps(struct zram *zram)
                zcomp_destroy(comp);
                zram->num_active_comps--;
        }
+
+       for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
+               kfree(zram->comp_algs[prio]);
+               zram->comp_algs[prio] = NULL;
+       }
+
+       zram_comp_params_reset(zram);
 }
 
 static void zram_reset_device(struct zram *zram)
@@ -2049,7 +2178,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->params[prio]);
                if (IS_ERR(comp)) {
                        pr_err("Cannot initialise %s compressing backend\n",
                               zram->comp_algs[prio]);
@@ -2152,6 +2282,7 @@ static DEVICE_ATTR_RW(writeback_limit_enable);
 static DEVICE_ATTR_RW(recomp_algorithm);
 static DEVICE_ATTR_WO(recompress);
 #endif
+static DEVICE_ATTR_WO(algorithm_params);
 
 static struct attribute *zram_disk_attrs[] = {
        &dev_attr_disksize.attr,
@@ -2179,6 +2310,7 @@ static struct attribute *zram_disk_attrs[] = {
        &dev_attr_recomp_algorithm.attr,
        &dev_attr_recompress.attr,
 #endif
+       &dev_attr_algorithm_params.attr,
        NULL,
 };
 
@@ -2254,6 +2386,7 @@ static int zram_add(void)
        if (ret)
                goto out_cleanup_disk;
 
+       zram_comp_params_reset(zram);
        comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
 
        zram_debugfs_register(zram);
This page took 0.042897 seconds and 4 git commands to generate.