Skip to content

Commit bf8a536

Browse files
gao xukawasaki
authored andcommitted
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. Signed-off-by: gao xu <gaoxu2@honor.com>
1 parent bbb3394 commit bf8a536

3 files changed

Lines changed: 13 additions & 26 deletions

File tree

drivers/block/zram/zcomp.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ static const struct zcomp_ops *lookup_backend_ops(const char *comp)
8484
return backends[i];
8585
}
8686

87-
bool zcomp_available_algorithm(const char *comp)
87+
const char *zcomp_lookup_backend_name(const char *comp)
8888
{
89-
return lookup_backend_ops(comp) != NULL;
89+
const struct zcomp_ops *backend = lookup_backend_ops(comp);
90+
91+
if (backend)
92+
return backend->name;
93+
94+
return NULL;
9095
}
9196

9297
/* show available compressors */

drivers/block/zram/zcomp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct zcomp {
8080
int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
8181
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
8282
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at);
83-
bool zcomp_available_algorithm(const char *comp);
83+
const char *zcomp_lookup_backend_name(const char *comp);
8484

8585
struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
8686
void zcomp_destroy(struct zcomp *comp);

drivers/block/zram/zram_drv.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,43 +1637,29 @@ static void zram_debugfs_unregister(struct zram *zram) {};
16371637

16381638
static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
16391639
{
1640-
/* Do not free statically defined compression algorithms */
1641-
if (zram->comp_algs[prio] != default_compressor)
1642-
kfree(zram->comp_algs[prio]);
1643-
16441640
zram->comp_algs[prio] = alg;
16451641
}
16461642

16471643
static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
16481644
{
1649-
char *compressor;
1645+
const char *alg;
16501646
size_t sz;
16511647

16521648
sz = strlen(buf);
16531649
if (sz >= ZRAM_MAX_ALGO_NAME_SZ)
16541650
return -E2BIG;
16551651

1656-
compressor = kstrdup(buf, GFP_KERNEL);
1657-
if (!compressor)
1658-
return -ENOMEM;
1659-
1660-
/* ignore trailing newline */
1661-
if (sz > 0 && compressor[sz - 1] == '\n')
1662-
compressor[sz - 1] = 0x00;
1663-
1664-
if (!zcomp_available_algorithm(compressor)) {
1665-
kfree(compressor);
1652+
alg = zcomp_lookup_backend_name(buf);
1653+
if (!alg)
16661654
return -EINVAL;
1667-
}
16681655

16691656
guard(rwsem_write)(&zram->dev_lock);
16701657
if (init_done(zram)) {
1671-
kfree(compressor);
16721658
pr_info("Can't change algorithm for initialized device\n");
16731659
return -EBUSY;
16741660
}
16751661

1676-
comp_algorithm_set(zram, prio, compressor);
1662+
comp_algorithm_set(zram, prio, alg);
16771663
return 0;
16781664
}
16791665

@@ -2851,12 +2837,8 @@ static void zram_destroy_comps(struct zram *zram)
28512837
zram->num_active_comps--;
28522838
}
28532839

2854-
for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
2855-
/* Do not free statically defined compression algorithms */
2856-
if (zram->comp_algs[prio] != default_compressor)
2857-
kfree(zram->comp_algs[prio]);
2840+
for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++)
28582841
zram->comp_algs[prio] = NULL;
2859-
}
28602842

28612843
zram_comp_params_reset(zram);
28622844
}

0 commit comments

Comments
 (0)