From nobody Tue Apr 7 15:26:04 2026 Received: from mta20.hihonor.com (mta20.honor.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 7FB36318131; Thu, 26 Feb 2026 12:37:27 +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=1772109449; cv=none; b=MQ7hxlgkjQY78fkG8kxCOdoAcCKMRthjC+wNDG8fVtPlQXEL2bo9JrnVqrSW6pgvm4Pbkc/SHvp+c6WssfNo2d7Y7Xui/vuL5NKVN3iH1ZY36aPegYyLIoOfPw5uonPPHGbnLZoVGr+/49+T+KFxw0JpQYObJE7SkR6l3C3IDtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772109449; c=relaxed/simple; bh=Na+ewq1zInvYiZUUwbf4n+d+17IoK0dPSUysZrxXWWw=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=THFQmVcjpyApG1rodxJayxfXs92h4N8hUCc80hDjmfssYZKfi/1htkFEG1UEPa/0FdmDN010IKSns26/9ccZDMDh7i+GjvodLwuv0uUphWGuRywvnYFmYv1wzngY4+3Ev0Wk5W+J3xAkPHlFznh+zw7Po2hgWGOMtO0etahZAfk= 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 w012.hihonor.com (unknown [10.68.27.189]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4fM9r34kgxzYkxs9; Thu, 26 Feb 2026 20:34:11 +0800 (CST) Received: from a001.hihonor.com (10.68.28.182) by w012.hihonor.com (10.68.27.189) 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 20:37:22 +0800 Received: from a008.hihonor.com (10.68.30.56) by a001.hihonor.com (10.68.28.182) 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 20:37:22 +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 20:37:22 +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 v2] zram: use statically allocated compression algorithm names Thread-Topic: [PATCH v2] zram: use statically allocated compression algorithm names Thread-Index: AdynGK1vcq4GzljDSRK93Q6Z8tt7XA== Date: Thu, 26 Feb 2026 12:37:22 +0000 Message-ID: <5bb2e9318d124dbcb2b743dcdce6a950@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 --- v1 -> v2:=20 1. Remove compressor and pass buf directly to zcomp_lookup_backend_name(). 2. Update the commit subject. Based on Sergey Senozhatsky's suggestions. https://lore.kernel.org/linux-block/423505aa53314004b0f2ee04ed5a1266@honor.= com/T/#t --- 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(-) 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..6f1ce0317 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1637,43 +1637,29 @@ static void zram_debugfs_unregister(struct zram *zr= am) {}; =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 static int __comp_algorithm_store(struct zram *zram, u32 prio, const char = *buf) { - char *compressor; + const char *alg; size_t sz; =20 sz =3D strlen(buf); if (sz >=3D ZRAM_MAX_ALGO_NAME_SZ) return -E2BIG; =20 - compressor =3D kstrdup(buf, GFP_KERNEL); - if (!compressor) - return -ENOMEM; - - /* ignore trailing newline */ - if (sz > 0 && compressor[sz - 1] =3D=3D '\n') - compressor[sz - 1] =3D 0x00; - - if (!zcomp_available_algorithm(compressor)) { - kfree(compressor); + alg =3D zcomp_lookup_backend_name(buf); + if (!alg) return -EINVAL; - } =20 guard(rwsem_write)(&zram->dev_lock); if (init_done(zram)) { - kfree(compressor); pr_info("Can't change algorithm for initialized device\n"); return -EBUSY; } =20 - comp_algorithm_set(zram, prio, compressor); + comp_algorithm_set(zram, prio, alg); return 0; } =20 @@ -2851,12 +2837,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); } --