From nobody Thu Apr 16 19:11:14 2026 Received: from mta20.hihonor.com (mta20.hihonor.com [81.70.206.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68D4738E5E4; Thu, 26 Feb 2026 08:06:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.206.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772093211; cv=none; b=F2aOFEFtB3Ii/uXlBN3k0mvzT3DK+QrZwdl0TggFbbYrI5iPYifVnywbIsK+ROVQLWDHNPQ/6Ztq46UxsubgE3fZYOL7SLzLQMUzepqIlA+2YfhrZMBeKB5beBaeiyTP4IQkyC+KW7tDW/UvFK8t5elKgELRk6j7/eSCsZu1n/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772093211; c=relaxed/simple; bh=qElxrGmCY3qzQ2pQXyYa/oLzWEfK61HbA3slC9WOweI=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=bOku32rtbyJlB/ZyW9r4rgbIg9k/Y5126NGhIaGfA5sxYlR47/z+W7fa5xjIqaYkzRnCZu3dPJT4AC8BkPiwYWS+4goeSxs1snOqN4fPR7Doq5xsS/rczRVwL17t8t75VgISrR47la/EJva6nYEuRv0TXgt2PKmyN3VMQcLkN0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.206.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w001.hihonor.com (unknown [10.68.25.235]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4fM3TD2LtzzYkxQc; Thu, 26 Feb 2026 15:47:28 +0800 (CST) Received: from a008.hihonor.com (10.68.30.56) by w001.hihonor.com (10.68.25.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.27; Thu, 26 Feb 2026 15:50:39 +0800 Received: from a008.hihonor.com (10.68.30.56) by a008.hihonor.com (10.68.30.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.27; Thu, 26 Feb 2026 15:50:39 +0800 Received: from a008.hihonor.com ([fe80::b6bf:fc6a:207:6851]) by a008.hihonor.com ([fe80::b6bf:fc6a:207:6851%6]) with mapi id 15.02.2562.027; Thu, 26 Feb 2026 15:50:39 +0800 From: gao xu To: Minchan Kim , Sergey Senozhatsky , Jens Axboe CC: "linux-kernel@vger.kernel.org" , "linux-block@vger.kernel.org" , Andrew Morton , "surenb@google.com" , zhouxiaolong Subject: [PATCH] zram: compression algorithms name use static fields Thread-Topic: [PATCH] zram: compression algorithms name use static fields Thread-Index: Adym8lMkejBmbNzMQs2D5/5nPYBYaA== Date: Thu, 26 Feb 2026 07:50:39 +0000 Message-ID: <32d733a0719c479f85d06f6fec3cc533@honor.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Signed-off-by: gao xu --- drivers/block/zram/zcomp.c | 9 +++++++-- drivers/block/zram/zcomp.h | 2 +- drivers/block/zram/zram_drv.c | 19 ++++++------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index a771a8ecc..974c46918 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]; } =20 -bool zcomp_available_algorithm(const char *comp) +const char *zcomp_lookup_backend_name(const char *comp) { - return lookup_backend_ops(comp) !=3D NULL; + const struct zcomp_ops *backend =3D lookup_backend_ops(comp); + + if (backend) + return backend->name; + + return NULL; } =20 /* show available compressors */ diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index eacfd3f7d..81a0f3f6f 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); =20 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 bca33403f..2472d3e5c 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1637,10 +1637,6 @@ static void zram_debugfs_unregister(struct zram *zra= m) {}; =20 static void comp_algorithm_set(struct zram *zram, u32 prio, const char *al= g) { - /* Do not free statically defined compression algorithms */ - if (zram->comp_algs[prio] !=3D default_compressor) - kfree(zram->comp_algs[prio]); - zram->comp_algs[prio] =3D alg; } =20 @@ -1648,6 +1644,7 @@ static int __comp_algorithm_store(struct zram *zram, = u32 prio, const char *buf) { char *compressor; size_t sz; + const char *alg; =20 sz =3D strlen(buf); if (sz >=3D ZRAM_MAX_ALGO_NAME_SZ) @@ -1661,10 +1658,10 @@ static int __comp_algorithm_store(struct zram *zram= , u32 prio, const char *buf) if (sz > 0 && compressor[sz - 1] =3D=3D '\n') compressor[sz - 1] =3D 0x00; =20 - if (!zcomp_available_algorithm(compressor)) { - kfree(compressor); + alg =3D zcomp_lookup_backend_name(compressor); + kfree(compressor); + if (!alg) return -EINVAL; - } =20 guard(rwsem_write)(&zram->dev_lock); if (init_done(zram)) { @@ -1673,7 +1670,7 @@ static int __comp_algorithm_store(struct zram *zram, = u32 prio, const char *buf) return -EBUSY; } =20 - comp_algorithm_set(zram, prio, compressor); + comp_algorithm_set(zram, prio, alg); return 0; } =20 @@ -2851,12 +2848,8 @@ static void zram_destroy_comps(struct zram *zram) zram->num_active_comps--; } =20 - for (prio =3D ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) { - /* Do not free statically defined compression algorithms */ - if (zram->comp_algs[prio] !=3D default_compressor) - kfree(zram->comp_algs[prio]); + for (prio =3D ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) zram->comp_algs[prio] =3D NULL; - } =20 zram_comp_params_reset(zram); } --