]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
module: sysfs: Add notes attributes through attribute_group
authorThomas Weißschuh <linux@weissschuh.net>
Fri, 27 Dec 2024 13:23:24 +0000 (14:23 +0100)
committerPetr Pavlu <petr.pavlu@suse.com>
Sun, 26 Jan 2025 12:05:24 +0000 (13:05 +0100)
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 <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-5-e267275f0f37@weissschuh.net
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
kernel/module/sysfs.c

index 05b1e3a6b644e047bf516b735da6fc0af98a99b8..fc2ad7e17cbd2db802c2a7e27b9d5963bc8961f5 100644 (file)
@@ -138,20 +138,13 @@ static void remove_sect_attrs(struct module *mod)
  */
 
 struct module_notes_attrs {
-       struct kobject *dir;
-       unsigned int notes;
-       struct bin_attribute attrs[] __counted_by(notes);
+       struct attribute_group grp;
+       struct bin_attribute attrs[];
 };
 
-static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
-                            unsigned int i)
+static void free_notes_attrs(struct module_notes_attrs *notes_attrs)
 {
-       if (notes_attrs->dir) {
-               while (i-- > 0)
-                       sysfs_remove_bin_file(notes_attrs->dir,
-                                             &notes_attrs->attrs[i]);
-               kobject_put(notes_attrs->dir);
-       }
+       kfree(notes_attrs->grp.bin_attrs);
        kfree(notes_attrs);
 }
 
@@ -159,6 +152,7 @@ static int add_notes_attrs(struct module *mod, const struct load_info *info)
 {
        unsigned int notes, loaded, i;
        struct module_notes_attrs *notes_attrs;
+       struct bin_attribute **gattr;
        struct bin_attribute *nattr;
        int ret;
 
@@ -177,7 +171,15 @@ static int add_notes_attrs(struct module *mod, const struct load_info *info)
        if (!notes_attrs)
                return -ENOMEM;
 
-       notes_attrs->notes = notes;
+       gattr = kcalloc(notes + 1, sizeof(*gattr), GFP_KERNEL);
+       if (!gattr) {
+               kfree(notes_attrs);
+               return -ENOMEM;
+       }
+
+       notes_attrs->grp.name = "notes";
+       notes_attrs->grp.bin_attrs = gattr;
+
        nattr = &notes_attrs->attrs[0];
        for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
                if (sect_empty(&info->sechdrs[i]))
@@ -189,35 +191,35 @@ static int add_notes_attrs(struct module *mod, const struct load_info *info)
                        nattr->size = info->sechdrs[i].sh_size;
                        nattr->private = (void *)info->sechdrs[i].sh_addr;
                        nattr->read = sysfs_bin_attr_simple_read;
-                       ++nattr;
+                       *(gattr++) = nattr++;
                }
                ++loaded;
        }
 
-       notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
-       if (!notes_attrs->dir) {
-               ret = -ENOMEM;
+       ret = sysfs_create_group(&mod->mkobj.kobj, &notes_attrs->grp);
+       if (ret)
                goto out;
-       }
-
-       for (i = 0; i < notes; ++i) {
-               ret = sysfs_create_bin_file(notes_attrs->dir, &notes_attrs->attrs[i]);
-               if (ret)
-                       goto out;
-       }
 
        mod->notes_attrs = notes_attrs;
        return 0;
 
 out:
-       free_notes_attrs(notes_attrs, i);
+       free_notes_attrs(notes_attrs);
        return ret;
 }
 
 static void remove_notes_attrs(struct module *mod)
 {
-       if (mod->notes_attrs)
-               free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
+       if (mod->notes_attrs) {
+               sysfs_remove_group(&mod->mkobj.kobj,
+                                  &mod->notes_attrs->grp);
+               /*
+                * We are positive that no one is using any notes attrs
+                * at this point.  Deallocate immediately.
+                */
+               free_notes_attrs(mod->notes_attrs);
+               mod->notes_attrs = NULL;
+       }
 }
 
 #else /* !CONFIG_KALLSYMS */