All of lore.kernel.org
 help / color / mirror / Atom feed
* + zram-use-statically-allocated-compression-algorithm-names.patch added to mm-new branch
@ 2026-02-26 16:45 Andrew Morton
  2026-02-27  3:04 ` Sergey Senozhatsky
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Morton @ 2026-02-26 16:45 UTC (permalink / raw)
  To: mm-commits, surenb, senozhatsky, minchan, axboe, gaoxu2, akpm


The patch titled
     Subject: zram: use statically allocated compression algorithm names
has been added to the -mm mm-new branch.  Its filename is
     zram-use-statically-allocated-compression-algorithm-names.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zram-use-statically-allocated-compression-algorithm-names.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: gao xu <gaoxu2@honor.com>
Subject: zram: use statically allocated compression algorithm names
Date: Thu, 26 Feb 2026 12:37:22 +0000

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>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 drivers/block/zram/zcomp.c    |    9 +++++++--
 drivers/block/zram/zcomp.h    |    2 +-
 drivers/block/zram/zram_drv.c |   28 +++++-----------------------
 3 files changed, 13 insertions(+), 26 deletions(-)

--- a/drivers/block/zram/zcomp.c~zram-use-statically-allocated-compression-algorithm-names
+++ a/drivers/block/zram/zcomp.c
@@ -84,9 +84,14 @@ static const struct zcomp_ops *lookup_ba
 	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 */
--- a/drivers/block/zram/zcomp.h~zram-use-statically-allocated-compression-algorithm-names
+++ a/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);
--- a/drivers/block/zram/zram_drv.c~zram-use-statically-allocated-compression-algorithm-names
+++ a/drivers/block/zram/zram_drv.c
@@ -1637,43 +1637,29 @@ static void zram_debugfs_unregister(stru
 
 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;
 }
 
@@ -2851,12 +2837,8 @@ static void zram_destroy_comps(struct zr
 		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);
 }
_

Patches currently in -mm which might be from gaoxu2@honor.com are

zram-use-statically-allocated-compression-algorithm-names.patch


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: + zram-use-statically-allocated-compression-algorithm-names.patch added to mm-new branch
  2026-02-26 16:45 + zram-use-statically-allocated-compression-algorithm-names.patch added to mm-new branch Andrew Morton
@ 2026-02-27  3:04 ` Sergey Senozhatsky
  0 siblings, 0 replies; 2+ messages in thread
From: Sergey Senozhatsky @ 2026-02-27  3:04 UTC (permalink / raw)
  To: Andrew Morton; +Cc: mm-commits, surenb, senozhatsky, minchan, axboe, gaoxu2

On (26/02/26 08:45), Andrew Morton wrote:
> 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>
> Cc: Jens Axboe <axboe@kernel.dk>
> Cc: Minchan Kim <minchan@kernel.org>
> Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
> Cc: Suren Baghdasaryan <surenb@google.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-27  3:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-26 16:45 + zram-use-statically-allocated-compression-algorithm-names.patch added to mm-new branch Andrew Morton
2026-02-27  3:04 ` Sergey Senozhatsky

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.