summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgao xu <gaoxu2@honor.com>2026-02-26 15:37:22 +0300
committerAndrew Morton <akpm@linux-foundation.org>2026-04-05 23:53:07 +0300
commitc09fb53d293a05adf1b53c800273273e59413f39 (patch)
tree4bfcdd72992903c620ae1b6490390883a065bbac
parent511f04aac469a3ae04f7f2588101020aebb19c90 (diff)
downloadlinux-c09fb53d293a05adf1b53c800273273e59413f39.tar.xz
zram: use statically allocated compression algorithm names
Currently, zram dynamically allocates memory for compressor algorithm names when they are set by the user. This requires careful memory management, including explicit `kfree` calls and special handling to avoid freeing statically defined default compressor names. This patch refactors the way zram handles compression algorithm names. Instead of storing dynamically allocated copies, `zram->comp_algs` will now store pointers directly to the static name strings defined within the `zcomp_ops` backend structures, thereby removing the need for conditional `kfree` calls. Link: https://lkml.kernel.org/r/5bb2e9318d124dbcb2b743dcdce6a950@honor.com Signed-off-by: gao xu <gaoxu2@honor.com> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org> Cc: Jens Axboe <axboe@kernel.dk> Cc: Minchan Kim <minchan@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--drivers/block/zram/zcomp.c9
-rw-r--r--drivers/block/zram/zcomp.h2
-rw-r--r--drivers/block/zram/zram_drv.c28
3 files changed, 13 insertions, 26 deletions
diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index a771a8ecc540..974c4691887e 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -84,9 +84,14 @@ static const struct zcomp_ops *lookup_backend_ops(const char *comp)
return backends[i];
}
-bool zcomp_available_algorithm(const char *comp)
+const char *zcomp_lookup_backend_name(const char *comp)
{
- return lookup_backend_ops(comp) != NULL;
+ const struct zcomp_ops *backend = lookup_backend_ops(comp);
+
+ if (backend)
+ return backend->name;
+
+ return NULL;
}
/* show available compressors */
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index eacfd3f7d61d..81a0f3f6ff48 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -80,7 +80,7 @@ struct zcomp {
int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at);
-bool zcomp_available_algorithm(const char *comp);
+const char *zcomp_lookup_backend_name(const char *comp);
struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
void zcomp_destroy(struct zcomp *comp);
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index af679375b193..990d391847f4 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -1621,43 +1621,29 @@ static void zram_debugfs_unregister(struct zram *zram) {};
static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
{
- /* Do not free statically defined compression algorithms */
- if (zram->comp_algs[prio] != default_compressor)
- kfree(zram->comp_algs[prio]);
-
zram->comp_algs[prio] = alg;
}
static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
{
- char *compressor;
+ const char *alg;
size_t sz;
sz = strlen(buf);
if (sz >= ZRAM_MAX_ALGO_NAME_SZ)
return -E2BIG;
- compressor = kstrdup(buf, GFP_KERNEL);
- if (!compressor)
- return -ENOMEM;
-
- /* ignore trailing newline */
- if (sz > 0 && compressor[sz - 1] == '\n')
- compressor[sz - 1] = 0x00;
-
- if (!zcomp_available_algorithm(compressor)) {
- kfree(compressor);
+ alg = zcomp_lookup_backend_name(buf);
+ if (!alg)
return -EINVAL;
- }
guard(rwsem_write)(&zram->dev_lock);
if (init_done(zram)) {
- kfree(compressor);
pr_info("Can't change algorithm for initialized device\n");
return -EBUSY;
}
- comp_algorithm_set(zram, prio, compressor);
+ comp_algorithm_set(zram, prio, alg);
return 0;
}
@@ -2840,12 +2826,8 @@ static void zram_destroy_comps(struct zram *zram)
zram->num_active_comps--;
}
- for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
- /* Do not free statically defined compression algorithms */
- if (zram->comp_algs[prio] != default_compressor)
- kfree(zram->comp_algs[prio]);
+ for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++)
zram->comp_algs[prio] = NULL;
- }
zram_comp_params_reset(zram);
}