]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
zram: add support for dict comp config
authorSergey Senozhatsky <senozhatsky@chromium.org>
Fri, 12 Jul 2024 05:18:27 +0000 (14:18 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 17 Aug 2024 00:52:30 +0000 (17:52 -0700)
Handle dict=path param so that we can read a pre-trained
compression algorithm dictionary which we then pass to the
backend configuration.

Link: https://lkml.kernel.org/r/20240712051850.484318-17-senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Nitin Gupta <ngupta@vflare.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
drivers/block/zram/zram_drv.c

index a2c23ca033b59635eff9237cf70645f9b0e394fd..d471ec14c76a83d5ea15c471ec4bcdaaf6f5500d 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,8 +999,34 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
        return 0;
 }
 
-static int comp_params_store(struct zram *zram, u32 prio, s32 level)
+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;
 }
@@ -1020,7 +1047,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_PARAM_NO_LEVEL;
        int ret;
 
@@ -1048,12 +1075,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_params_store(zram, ZRAM_PRIMARY_COMP, level);
+       ret = comp_params_store(zram, ZRAM_PRIMARY_COMP, level, dict_path);
        if (!ret)
                ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, alg);
        return ret ? ret : len;
@@ -1087,7 +1119,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_PARAM_NO_LEVEL;
        int ret;
 
@@ -1116,6 +1148,11 @@ static ssize_t recomp_algorithm_store(struct device *dev,
                                return ret;
                        continue;
                }
+
+               if (!strcmp(param, "dict")) {
+                       dict_path = val;
+                       continue;
+               }
        }
 
        if (!alg)
@@ -1124,7 +1161,7 @@ static ssize_t recomp_algorithm_store(struct device *dev,
        if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS)
                return -EINVAL;
 
-       ret = comp_params_store(zram, prio, level);
+       ret = comp_params_store(zram, prio, level, dict_path);
        if (!ret)
                ret = __comp_algorithm_store(zram, prio, alg);
        return ret ? ret : len;
@@ -2029,17 +2066,12 @@ static void zram_slot_free_notify(struct block_device *bdev,
        zram_slot_unlock(zram, index);
 }
 
-static void zram_reset_comp_params(struct zram *zram)
+static void zram_comp_params_reset(struct zram *zram)
 {
        u32 prio;
 
        for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
-               struct zcomp_params *params = &zram->params[prio];
-
-               vfree(params->dict);
-               params->level = ZCOMP_PARAM_NO_LEVEL;
-               params->dict_sz = 0;
-               params->dict = NULL;
+               comp_params_reset(zram, prio);
        }
 }
 
@@ -2057,7 +2089,7 @@ static void zram_destroy_comps(struct zram *zram)
                zram->num_active_comps--;
        }
 
-       zram_reset_comp_params(zram);
+       zram_comp_params_reset(zram);
 }
 
 static void zram_reset_device(struct zram *zram)
@@ -2321,7 +2353,7 @@ static int zram_add(void)
        if (ret)
                goto out_cleanup_disk;
 
-       zram_reset_comp_params(zram);
+       zram_comp_params_reset(zram);
        comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
 
        zram_debugfs_register(zram);