Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

gao xu and committed by
Andrew Morton
c09fb53d 511f04aa

+13 -26
+7 -2
drivers/block/zram/zcomp.c
··· 84 84 return backends[i]; 85 85 } 86 86 87 - bool zcomp_available_algorithm(const char *comp) 87 + const char *zcomp_lookup_backend_name(const char *comp) 88 88 { 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; 90 95 } 91 96 92 97 /* show available compressors */
+1 -1
drivers/block/zram/zcomp.h
··· 80 80 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node); 81 81 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); 82 82 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); 84 84 85 85 struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params); 86 86 void zcomp_destroy(struct zcomp *comp);
+5 -23
drivers/block/zram/zram_drv.c
··· 1621 1621 1622 1622 static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg) 1623 1623 { 1624 - /* Do not free statically defined compression algorithms */ 1625 - if (zram->comp_algs[prio] != default_compressor) 1626 - kfree(zram->comp_algs[prio]); 1627 - 1628 1624 zram->comp_algs[prio] = alg; 1629 1625 } 1630 1626 1631 1627 static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) 1632 1628 { 1633 - char *compressor; 1629 + const char *alg; 1634 1630 size_t sz; 1635 1631 1636 1632 sz = strlen(buf); 1637 1633 if (sz >= ZRAM_MAX_ALGO_NAME_SZ) 1638 1634 return -E2BIG; 1639 1635 1640 - compressor = kstrdup(buf, GFP_KERNEL); 1641 - if (!compressor) 1642 - return -ENOMEM; 1643 - 1644 - /* ignore trailing newline */ 1645 - if (sz > 0 && compressor[sz - 1] == '\n') 1646 - compressor[sz - 1] = 0x00; 1647 - 1648 - if (!zcomp_available_algorithm(compressor)) { 1649 - kfree(compressor); 1636 + alg = zcomp_lookup_backend_name(buf); 1637 + if (!alg) 1650 1638 return -EINVAL; 1651 - } 1652 1639 1653 1640 guard(rwsem_write)(&zram->dev_lock); 1654 1641 if (init_done(zram)) { 1655 - kfree(compressor); 1656 1642 pr_info("Can't change algorithm for initialized device\n"); 1657 1643 return -EBUSY; 1658 1644 } 1659 1645 1660 - comp_algorithm_set(zram, prio, compressor); 1646 + comp_algorithm_set(zram, prio, alg); 1661 1647 return 0; 1662 1648 } 1663 1649 ··· 2826 2840 zram->num_active_comps--; 2827 2841 } 2828 2842 2829 - for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) { 2830 - /* Do not free statically defined compression algorithms */ 2831 - if (zram->comp_algs[prio] != default_compressor) 2832 - kfree(zram->comp_algs[prio]); 2843 + for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) 2833 2844 zram->comp_algs[prio] = NULL; 2834 - } 2835 2845 2836 2846 zram_comp_params_reset(zram); 2837 2847 }