From: Sergey Senozhatsky Date: Fri, 12 Jul 2024 05:18:27 +0000 (+0900) Subject: zram: add support for dict comp config X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=5c0142b5d8c68afa25c4e9f5e32da6f559fbfba8;p=users%2Fjedix%2Flinux-maple.git zram: add support for dict comp config 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 Cc: Minchan Kim Cc: Nhat Pham Cc: Nick Terrell Cc: Nitin Gupta Signed-off-by: Andrew Morton --- diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index a2c23ca033b59..d471ec14c76a8 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -33,6 +33,7 @@ #include #include #include +#include #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);