tty: make tty_class a static const structure
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 2 Apr 2023 17:58:50 +0000 (19:58 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 3 Apr 2023 19:43:07 +0000 (21:43 +0200)
Now that the driver core allows for struct class to be in read-only
memory, move the tty_class structure to be declared at build time
placing it into read-only memory, instead of having to be dynamically
allocated at boot time.

Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/2023040250-landowner-unfitted-11f4@gregkh
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/pty.c
drivers/tty/tty_io.c
drivers/tty/vt/vt.c
include/linux/tty.h

index 07394fd..2b1c8ab 100644 (file)
@@ -931,7 +931,7 @@ static void __init unix98_pty_init(void)
        if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
            register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
                panic("Couldn't register /dev/ptmx driver");
-       device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
+       device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
 }
 
 #else
index 1382d90..44e0d53 100644 (file)
@@ -3070,7 +3070,7 @@ static struct device *tty_get_device(struct tty_struct *tty)
 {
        dev_t devt = tty_devnum(tty);
 
-       return class_find_device_by_devt(tty_class, devt);
+       return class_find_device_by_devt(&tty_class, devt);
 }
 
 
@@ -3143,8 +3143,6 @@ int tty_put_char(struct tty_struct *tty, unsigned char ch)
 }
 EXPORT_SYMBOL_GPL(tty_put_char);
 
-struct class *tty_class;
-
 static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
                unsigned int index, unsigned int count)
 {
@@ -3239,7 +3237,7 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
                return ERR_PTR(-ENOMEM);
 
        dev->devt = devt;
-       dev->class = tty_class;
+       dev->class = &tty_class;
        dev->parent = device;
        dev->release = tty_device_create_release;
        dev_set_name(dev, "%s", name);
@@ -3294,8 +3292,7 @@ EXPORT_SYMBOL_GPL(tty_register_device_attr);
  */
 void tty_unregister_device(struct tty_driver *driver, unsigned index)
 {
-       device_destroy(tty_class,
-               MKDEV(driver->major, driver->minor_start) + index);
+       device_destroy(&tty_class, MKDEV(driver->major, driver->minor_start) + index);
        if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
                cdev_del(driver->cdevs[index]);
                driver->cdevs[index] = NULL;
@@ -3510,13 +3507,14 @@ static char *tty_devnode(const struct device *dev, umode_t *mode)
        return NULL;
 }
 
+const struct class tty_class = {
+       .name           = "tty",
+       .devnode        = tty_devnode,
+};
+
 static int __init tty_class_init(void)
 {
-       tty_class = class_create("tty");
-       if (IS_ERR(tty_class))
-               return PTR_ERR(tty_class);
-       tty_class->devnode = tty_devnode;
-       return 0;
+       return class_register(&tty_class);
 }
 
 postcore_initcall(tty_class_init);
@@ -3643,13 +3641,13 @@ int __init tty_init(void)
        if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
            register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
                panic("Couldn't register /dev/tty driver\n");
-       device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
+       device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
 
        cdev_init(&console_cdev, &console_fops);
        if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
            register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
                panic("Couldn't register /dev/console driver\n");
-       consdev = device_create_with_groups(tty_class, NULL,
+       consdev = device_create_with_groups(&tty_class, NULL,
                                            MKDEV(TTYAUX_MAJOR, 1), NULL,
                                            cons_dev_groups, "console");
        if (IS_ERR(consdev))
index 81c1480..40e4928 100644 (file)
@@ -3539,7 +3539,7 @@ int __init vty_init(const struct file_operations *console_fops)
        if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
            register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
                panic("Couldn't register /dev/tty0 driver\n");
-       tty0dev = device_create_with_groups(tty_class, NULL,
+       tty0dev = device_create_with_groups(&tty_class, NULL,
                                            MKDEV(TTY_MAJOR, 0), NULL,
                                            vt_dev_groups, "tty0");
        if (IS_ERR(tty0dev))
index 093935e..121eb4b 100644 (file)
@@ -387,7 +387,7 @@ extern struct ktermios tty_std_termios;
 
 int vcs_init(void);
 
-extern struct class *tty_class;
+extern const struct class tty_class;
 
 /**
  *     tty_kref_get            -       get a tty reference