Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

comedi: make all 'class' structures const

Now that the driver core allows for struct class to be in read-only
memory, making all 'class' structures to be declared at build time
placing them into read-only memory, instead of having to be dynamically
allocated at load time.

Cc: Ian Abbott <abbotti@mev.co.uk>
Cc: H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cc: Ivan Orlov <ivan.orlov0322@gmail.com>
Cc: Xuezhi Zhang <zhangxuezhi1@coolpad.com>
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Link: https://lore.kernel.org/r/20230620144137.581406-2-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ivan Orlov and committed by
Greg Kroah-Hartman
3b7a628d 9ee202e6

+35 -35
+24 -23
drivers/comedi/comedi_fops.c
··· 97 97 static struct comedi_subdevice 98 98 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS]; 99 99 100 - static struct class *comedi_class; 101 100 static struct cdev comedi_cdev; 102 101 103 102 static void comedi_device_init(struct comedi_device *dev) ··· 184 185 comedi_board_minor_table[minor] = NULL; 185 186 mutex_unlock(&comedi_board_minor_table_lock); 186 187 return dev; 187 - } 188 - 189 - static void comedi_free_board_dev(struct comedi_device *dev) 190 - { 191 - if (dev) { 192 - comedi_device_cleanup(dev); 193 - if (dev->class_dev) { 194 - device_destroy(comedi_class, 195 - MKDEV(COMEDI_MAJOR, dev->minor)); 196 - } 197 - comedi_dev_put(dev); 198 - } 199 188 } 200 189 201 190 static struct comedi_subdevice * ··· 597 610 NULL, 598 611 }; 599 612 ATTRIBUTE_GROUPS(comedi_dev); 613 + 614 + static const struct class comedi_class = { 615 + .name = "comedi", 616 + .dev_groups = comedi_dev_groups, 617 + }; 618 + 619 + static void comedi_free_board_dev(struct comedi_device *dev) 620 + { 621 + if (dev) { 622 + comedi_device_cleanup(dev); 623 + if (dev->class_dev) { 624 + device_destroy(&comedi_class, 625 + MKDEV(COMEDI_MAJOR, dev->minor)); 626 + } 627 + comedi_dev_put(dev); 628 + } 629 + } 600 630 601 631 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s, 602 632 unsigned int bits) ··· 3267 3263 return ERR_PTR(-EBUSY); 3268 3264 } 3269 3265 dev->minor = i; 3270 - csdev = device_create(comedi_class, hardware_device, 3266 + csdev = device_create(&comedi_class, hardware_device, 3271 3267 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i); 3272 3268 if (!IS_ERR(csdev)) 3273 3269 dev->class_dev = get_device(csdev); ··· 3316 3312 } 3317 3313 i += COMEDI_NUM_BOARD_MINORS; 3318 3314 s->minor = i; 3319 - csdev = device_create(comedi_class, dev->class_dev, 3315 + csdev = device_create(&comedi_class, dev->class_dev, 3320 3316 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i", 3321 3317 dev->minor, s->index); 3322 3318 if (!IS_ERR(csdev)) ··· 3341 3337 comedi_subdevice_minor_table[i] = NULL; 3342 3338 mutex_unlock(&comedi_subdevice_minor_table_lock); 3343 3339 if (s->class_dev) { 3344 - device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor)); 3340 + device_destroy(&comedi_class, MKDEV(COMEDI_MAJOR, s->minor)); 3345 3341 s->class_dev = NULL; 3346 3342 } 3347 3343 } ··· 3387 3383 if (retval) 3388 3384 goto out_unregister_chrdev_region; 3389 3385 3390 - comedi_class = class_create("comedi"); 3391 - if (IS_ERR(comedi_class)) { 3392 - retval = PTR_ERR(comedi_class); 3386 + retval = class_register(&comedi_class); 3387 + if (retval) { 3393 3388 pr_err("failed to create class\n"); 3394 3389 goto out_cdev_del; 3395 3390 } 3396 - 3397 - comedi_class->dev_groups = comedi_dev_groups; 3398 3391 3399 3392 /* create devices files for legacy/manual use */ 3400 3393 for (i = 0; i < comedi_num_legacy_minors; i++) { ··· 3414 3413 3415 3414 out_cleanup_board_minors: 3416 3415 comedi_cleanup_board_minors(); 3417 - class_destroy(comedi_class); 3416 + class_unregister(&comedi_class); 3418 3417 out_cdev_del: 3419 3418 cdev_del(&comedi_cdev); 3420 3419 out_unregister_chrdev_region: ··· 3426 3425 static void __exit comedi_cleanup(void) 3427 3426 { 3428 3427 comedi_cleanup_board_minors(); 3429 - class_destroy(comedi_class); 3428 + class_unregister(&comedi_class); 3430 3429 cdev_del(&comedi_cdev); 3431 3430 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS); 3432 3431
+11 -12
drivers/comedi/drivers/comedi_test.c
··· 60 60 static bool config_mode; 61 61 static unsigned int set_amplitude; 62 62 static unsigned int set_period; 63 - static struct class *ctcls; 63 + static const struct class ctcls = { 64 + .name = CLASS_NAME, 65 + }; 64 66 static struct device *ctdev; 65 67 66 68 module_param_named(noauto, config_mode, bool, 0444); ··· 797 795 } 798 796 799 797 if (!config_mode) { 800 - ctcls = class_create(CLASS_NAME); 801 - if (IS_ERR(ctcls)) { 798 + ret = class_register(&ctcls); 799 + if (ret) { 802 800 pr_warn("comedi_test: unable to create class\n"); 803 801 goto clean3; 804 802 } 805 803 806 - ctdev = device_create(ctcls, NULL, MKDEV(0, 0), NULL, DEV_NAME); 804 + ctdev = device_create(&ctcls, NULL, MKDEV(0, 0), NULL, DEV_NAME); 807 805 if (IS_ERR(ctdev)) { 808 806 pr_warn("comedi_test: unable to create device\n"); 809 807 goto clean2; ··· 819 817 return 0; 820 818 821 819 clean: 822 - device_destroy(ctcls, MKDEV(0, 0)); 820 + device_destroy(&ctcls, MKDEV(0, 0)); 823 821 clean2: 824 - class_destroy(ctcls); 825 - ctdev = NULL; 822 + class_unregister(&ctcls); 826 823 clean3: 827 - ctcls = NULL; 828 - 829 824 return 0; 830 825 } 831 826 module_init(comedi_test_init); ··· 832 833 if (ctdev) 833 834 comedi_auto_unconfig(ctdev); 834 835 835 - if (ctcls) { 836 - device_destroy(ctcls, MKDEV(0, 0)); 837 - class_destroy(ctcls); 836 + if (class_is_registered(&ctcls)) { 837 + device_destroy(&ctcls, MKDEV(0, 0)); 838 + class_unregister(&ctcls); 838 839 } 839 840 840 841 comedi_driver_unregister(&waveform_driver);