Skip to content

Commit f47c0be

Browse files
t-8chpetrpavlu
authored andcommitted
module: sysfs: Simplify section attribute allocation
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 <[email protected]> Reviewed-by: Petr Pavlu <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Link: https://lore.kernel.org/r/20241227-sysfs-const-bin_attr-module-v2-4-e267275f0f37@weissschuh.net Signed-off-by: Petr Pavlu <[email protected]>
1 parent 34f5ec0 commit f47c0be

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

kernel/module/sysfs.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,37 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
5858

5959
for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
6060
kfree((*bin_attr)->attr.name);
61+
kfree(sect_attrs->grp.bin_attrs);
6162
kfree(sect_attrs);
6263
}
6364

6465
static int add_sect_attrs(struct module *mod, const struct load_info *info)
6566
{
66-
unsigned int nloaded = 0, i, size[2];
6767
struct module_sect_attrs *sect_attrs;
6868
struct bin_attribute **gattr;
6969
struct bin_attribute *sattr;
70+
unsigned int nloaded = 0, i;
7071
int ret;
7172

7273
/* Count loaded sections and allocate structures */
7374
for (i = 0; i < info->hdr->e_shnum; i++)
7475
if (!sect_empty(&info->sechdrs[i]))
7576
nloaded++;
76-
size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
77-
sizeof(sect_attrs->grp.bin_attrs[0]));
78-
size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
79-
sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
77+
sect_attrs = kzalloc(struct_size(sect_attrs, attrs, nloaded), GFP_KERNEL);
8078
if (!sect_attrs)
8179
return -ENOMEM;
8280

81+
gattr = kcalloc(nloaded + 1, sizeof(*gattr), GFP_KERNEL);
82+
if (!gattr) {
83+
kfree(sect_attrs);
84+
return -ENOMEM;
85+
}
86+
8387
/* Setup section attributes. */
8488
sect_attrs->grp.name = "sections";
85-
sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
89+
sect_attrs->grp.bin_attrs = gattr;
8690

8791
sattr = &sect_attrs->attrs[0];
88-
gattr = &sect_attrs->grp.bin_attrs[0];
8992
for (i = 0; i < info->hdr->e_shnum; i++) {
9093
Elf_Shdr *sec = &info->sechdrs[i];
9194

@@ -104,7 +107,6 @@ static int add_sect_attrs(struct module *mod, const struct load_info *info)
104107
sattr->attr.mode = 0400;
105108
*(gattr++) = sattr++;
106109
}
107-
*gattr = NULL;
108110

109111
ret = sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp);
110112
if (ret)

0 commit comments

Comments
 (0)