char: misc: make misc_class a static const structure
authorIvan Orlov <ivan.orlov0322@gmail.com>
Tue, 20 Jun 2023 14:37:56 +0000 (16:37 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 23 Jun 2023 08:27:15 +0000 (10:27 +0200)
Now that the driver core allows for struct class to be in read-only
memory, move the misc_class structure to be declared at build time
placing it into read-only memory, instead of having to be dynamically
allocated at load time.

Cc: Arnd Bergmann <arnd@arndb.de>
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Link: https://lore.kernel.org/r/20230620143751.578239-14-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/misc.c

index 1c44c29a666eb94956f1c2efab44b63082684027..541edc26ec89a14ef3735da111452c78d553b79e 100644 (file)
@@ -168,7 +168,21 @@ fail:
        return err;
 }
 
-static struct class *misc_class;
+static char *misc_devnode(const struct device *dev, umode_t *mode)
+{
+       const struct miscdevice *c = dev_get_drvdata(dev);
+
+       if (mode && c->mode)
+               *mode = c->mode;
+       if (c->nodename)
+               return kstrdup(c->nodename, GFP_KERNEL);
+       return NULL;
+}
+
+static const struct class misc_class = {
+       .name           = "misc",
+       .devnode        = misc_devnode,
+};
 
 static const struct file_operations misc_fops = {
        .owner          = THIS_MODULE,
@@ -226,7 +240,7 @@ int misc_register(struct miscdevice *misc)
        dev = MKDEV(MISC_MAJOR, misc->minor);
 
        misc->this_device =
-               device_create_with_groups(misc_class, misc->parent, dev,
+               device_create_with_groups(&misc_class, misc->parent, dev,
                                          misc, misc->groups, "%s", misc->name);
        if (IS_ERR(misc->this_device)) {
                if (is_dynamic) {
@@ -263,43 +277,30 @@ void misc_deregister(struct miscdevice *misc)
 
        mutex_lock(&misc_mtx);
        list_del(&misc->list);
-       device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
+       device_destroy(&misc_class, MKDEV(MISC_MAJOR, misc->minor));
        misc_minor_free(misc->minor);
        mutex_unlock(&misc_mtx);
 }
 EXPORT_SYMBOL(misc_deregister);
 
-static char *misc_devnode(const struct device *dev, umode_t *mode)
-{
-       const struct miscdevice *c = dev_get_drvdata(dev);
-
-       if (mode && c->mode)
-               *mode = c->mode;
-       if (c->nodename)
-               return kstrdup(c->nodename, GFP_KERNEL);
-       return NULL;
-}
-
 static int __init misc_init(void)
 {
        int err;
        struct proc_dir_entry *ret;
 
        ret = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
-       misc_class = class_create("misc");
-       err = PTR_ERR(misc_class);
-       if (IS_ERR(misc_class))
+       err = class_register(&misc_class);
+       if (err)
                goto fail_remove;
 
        err = -EIO;
        if (register_chrdev(MISC_MAJOR, "misc", &misc_fops))
                goto fail_printk;
-       misc_class->devnode = misc_devnode;
        return 0;
 
 fail_printk:
        pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
-       class_destroy(misc_class);
+       class_unregister(&misc_class);
 fail_remove:
        if (ret)
                remove_proc_entry("misc", NULL);