Skip to content

Commit 4723f16

Browse files
t-8chpetrpavlu
authored andcommitted
module: sysfs: Add notes attributes through attribute_group
A kobject is meant to manage the lifecycle of some resource. However the module sysfs code only creates a kobject to get a "notes" subdirectory in sysfs. This can be achieved easier and cheaper by using a sysfs group. Switch the notes attribute code to such a group, similar to how the section allocation in the same file already works. 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-5-e267275f0f37@weissschuh.net Signed-off-by: Petr Pavlu <[email protected]>
1 parent f47c0be commit 4723f16

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

kernel/module/sysfs.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,21 @@ static void remove_sect_attrs(struct module *mod)
138138
*/
139139

140140
struct module_notes_attrs {
141-
struct kobject *dir;
142-
unsigned int notes;
143-
struct bin_attribute attrs[] __counted_by(notes);
141+
struct attribute_group grp;
142+
struct bin_attribute attrs[];
144143
};
145144

146-
static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
147-
unsigned int i)
145+
static void free_notes_attrs(struct module_notes_attrs *notes_attrs)
148146
{
149-
if (notes_attrs->dir) {
150-
while (i-- > 0)
151-
sysfs_remove_bin_file(notes_attrs->dir,
152-
&notes_attrs->attrs[i]);
153-
kobject_put(notes_attrs->dir);
154-
}
147+
kfree(notes_attrs->grp.bin_attrs);
155148
kfree(notes_attrs);
156149
}
157150

158151
static int add_notes_attrs(struct module *mod, const struct load_info *info)
159152
{
160153
unsigned int notes, loaded, i;
161154
struct module_notes_attrs *notes_attrs;
155+
struct bin_attribute **gattr;
162156
struct bin_attribute *nattr;
163157
int ret;
164158

@@ -177,7 +171,15 @@ static int add_notes_attrs(struct module *mod, const struct load_info *info)
177171
if (!notes_attrs)
178172
return -ENOMEM;
179173

180-
notes_attrs->notes = notes;
174+
gattr = kcalloc(notes + 1, sizeof(*gattr), GFP_KERNEL);
175+
if (!gattr) {
176+
kfree(notes_attrs);
177+
return -ENOMEM;
178+
}
179+
180+
notes_attrs->grp.name = "notes";
181+
notes_attrs->grp.bin_attrs = gattr;
182+
181183
nattr = &notes_attrs->attrs[0];
182184
for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
183185
if (sect_empty(&info->sechdrs[i]))
@@ -189,35 +191,35 @@ static int add_notes_attrs(struct module *mod, const struct load_info *info)
189191
nattr->size = info->sechdrs[i].sh_size;
190192
nattr->private = (void *)info->sechdrs[i].sh_addr;
191193
nattr->read = sysfs_bin_attr_simple_read;
192-
++nattr;
194+
*(gattr++) = nattr++;
193195
}
194196
++loaded;
195197
}
196198

197-
notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
198-
if (!notes_attrs->dir) {
199-
ret = -ENOMEM;
199+
ret = sysfs_create_group(&mod->mkobj.kobj, &notes_attrs->grp);
200+
if (ret)
200201
goto out;
201-
}
202-
203-
for (i = 0; i < notes; ++i) {
204-
ret = sysfs_create_bin_file(notes_attrs->dir, &notes_attrs->attrs[i]);
205-
if (ret)
206-
goto out;
207-
}
208202

209203
mod->notes_attrs = notes_attrs;
210204
return 0;
211205

212206
out:
213-
free_notes_attrs(notes_attrs, i);
207+
free_notes_attrs(notes_attrs);
214208
return ret;
215209
}
216210

217211
static void remove_notes_attrs(struct module *mod)
218212
{
219-
if (mod->notes_attrs)
220-
free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
213+
if (mod->notes_attrs) {
214+
sysfs_remove_group(&mod->mkobj.kobj,
215+
&mod->notes_attrs->grp);
216+
/*
217+
* We are positive that no one is using any notes attrs
218+
* at this point. Deallocate immediately.
219+
*/
220+
free_notes_attrs(mod->notes_attrs);
221+
mod->notes_attrs = NULL;
222+
}
221223
}
222224

223225
#else /* !CONFIG_KALLSYMS */

0 commit comments

Comments
 (0)