]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
module: sysfs: Simplify section attribute allocation
authorThomas Weißschuh <linux@weissschuh.net>
Fri, 27 Dec 2024 13:23:23 +0000 (14:23 +0100)
committerPetr Pavlu <petr.pavlu@suse.com>
Sun, 26 Jan 2025 12:05:24 +0000 (13:05 +0100)
The existing allocation logic manually stuffs two allocations into one.
This is hard to understand and of limited value, given that all the
section names are allocated on their own anyways.
Une one allocation per datastructure.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/r/20241227-sysfs-const-bin_attr-module-v2-4-e267275f0f37@weissschuh.net
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
kernel/module/sysfs.c

index 89797c556e8c51141ae095eb7004f997bf791e80..05b1e3a6b644e047bf516b735da6fc0af98a99b8 100644 (file)
@@ -58,34 +58,37 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
 
        for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
                kfree((*bin_attr)->attr.name);
+       kfree(sect_attrs->grp.bin_attrs);
        kfree(sect_attrs);
 }
 
 static int add_sect_attrs(struct module *mod, const struct load_info *info)
 {
-       unsigned int nloaded = 0, i, size[2];
        struct module_sect_attrs *sect_attrs;
        struct bin_attribute **gattr;
        struct bin_attribute *sattr;
+       unsigned int nloaded = 0, i;
        int ret;
 
        /* Count loaded sections and allocate structures */
        for (i = 0; i < info->hdr->e_shnum; i++)
                if (!sect_empty(&info->sechdrs[i]))
                        nloaded++;
-       size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
-                       sizeof(sect_attrs->grp.bin_attrs[0]));
-       size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
-       sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
+       sect_attrs = kzalloc(struct_size(sect_attrs, attrs, nloaded), GFP_KERNEL);
        if (!sect_attrs)
                return -ENOMEM;
 
+       gattr = kcalloc(nloaded + 1, sizeof(*gattr), GFP_KERNEL);
+       if (!gattr) {
+               kfree(sect_attrs);
+               return -ENOMEM;
+       }
+
        /* Setup section attributes. */
        sect_attrs->grp.name = "sections";
-       sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
+       sect_attrs->grp.bin_attrs = gattr;
 
        sattr = &sect_attrs->attrs[0];
-       gattr = &sect_attrs->grp.bin_attrs[0];
        for (i = 0; i < info->hdr->e_shnum; i++) {
                Elf_Shdr *sec = &info->sechdrs[i];
 
@@ -104,7 +107,6 @@ static int add_sect_attrs(struct module *mod, const struct load_info *info)
                sattr->attr.mode = 0400;
                *(gattr++) = sattr++;
        }
-       *gattr = NULL;
 
        ret = sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp);
        if (ret)