USB: make usb class a const structure
PATCH v2 1/1 USB: make usb class a const structure
Now that the driver core allows for struct class to be in read-only memory, remove the usb_class structure and create the usbmisc_class const class structure declared at build time which places it into read-only memory, instead of having it to be dynamically allocated at load time.
Additionally, now we register usb class at startup and unregister it when shutting down, so we don't have to count uses of the class. Therefore we don't need the 'usb_class' structure anymore. Due to this fact, remove all static functions related to class initialization and deinitialization. We can't use them in 'usb.c' since they are static and we don't really need them anymore.
Since we have to register the class in usb_init function in 'usb.c' and use it in 'file.c' as well, declare the usbmisc_class structure as 'export' in the 'usb.h' file.
Debatable moment: the class registration and unregistration functions could be extracted to the 'file.c'. I think we don't want to do this since it would be one-line functions. They would make the code paths more confusing and add calling overhead.
---
V1 -> V2:
- Remove kref field of usb_class structure
- Remove usb_class structure
- Remove class initialization and cleaning functions
- Add usbmisc_class declaration to usb.h file
- Move class registration to usb init function
- Move class unregistration to usb exit function
drivers/usb/core/file.c | 63 ++++------------------------------------- drivers/usb/core/usb.c | 6 ++++ drivers/usb/core/usb.h | 1 + 3 files changed, 13 insertions(+), 57 deletions(-)
drivers/usb/core/file.c +6 -57
@@ -29,7 +29,6 @@#define MAX_USB_MINORS 256static const struct file_operations *usb_minors[MAX_USB_MINORS ];static DECLARE_RWSEM (minor_rwsem); -static DEFINE_MUTEX (init_usb_class_mutex);static int usb_open (struct inode *inode,struct file *file) {
@@ -57,11 +56,6 @@ static const struct file_operations usb_fops = { .llseek = noop_llseek, }; -static struct usb_class { -struct kref kref; -struct class *class; -} *usb_class; -static char *usb_devnode (const struct device *dev,umode_t *mode) {struct usb_class_driver *drv;
@@ -72,50 +66,10 @@ static char *usb_devnode(const struct device *dev, umode_t *mode)return drv->devnode (dev, mode); } -static int init_usb_class (void ) -{ -int result =0 ; - -if (usb_class !=NULL ) { -kref_get (&usb_class->kref); - goto exit; - } - - usb_class =kmalloc (sizeof (*usb_class),GFP_KERNEL ); -if (!usb_class) { - result = -ENOMEM ; - goto exit; - } - -kref_init (&usb_class->kref); - usb_class->class =class_create ("usbmisc" ); -if (IS_ERR (usb_class->class)) { - result =PTR_ERR (usb_class->class); -printk (KERN_ERR "class_create failed for usb devices\n" ); -kfree (usb_class); - usb_class =NULL ; - goto exit; - } - usb_class->class->devnode = usb_devnode; - -exit: -return result; -} - -static void release_usb_class (struct kref *kref) -{ -/* Ok, we cheat as we know we only have one usb_class */ -class_destroy (usb_class->class); -kfree (usb_class); - usb_class =NULL ; -} - -static void destroy_usb_class (void ) -{ -mutex_lock (&init_usb_class_mutex); -kref_put (&usb_class->kref, release_usb_class); -mutex_unlock (&init_usb_class_mutex); -} +const struct class usbmisc_class = { + .name ="usbmisc" , + .devnode = usb_devnode, +};int usb_major_init (void ) {
@@ -175,10 +129,6 @@ int usb_register_dev(struct usb_interface *intf,if (intf->minor >=0 )return -EADDRINUSE ; -mutex_lock (&init_usb_class_mutex); - retval =init_usb_class (); -mutex_unlock (&init_usb_class_mutex); -if (retval)return retval;
@@ -200,7 +150,7 @@ int usb_register_dev(struct usb_interface *intf,/* create a usb class device for this usb interface */ snprintf (name,sizeof (name), class_driver->name, minor - minor_base); - intf->usb_dev =device_create (usb_class->class, &intf->dev, + intf->usb_dev =device_create (&usbmisc_class, &intf->dev,MKDEV (USB_MAJOR , minor), class_driver,"%s" ,kbasename (name));if (IS_ERR (intf->usb_dev)) {
@@ -234,7 +184,7 @@ void usb_deregister_dev(struct usb_interface *intf,return ;dev_dbg (&intf->dev,"removing %d minor\n" , intf->minor); -device_destroy (usb_class->class,MKDEV (USB_MAJOR , intf->minor)); +device_destroy (&usbmisc_class,MKDEV (USB_MAJOR , intf->minor));down_write (&minor_rwsem); usb_minors[intf->minor] =NULL ;
@@ -242,6 +192,5 @@ void usb_deregister_dev(struct usb_interface *intf, intf->usb_dev =NULL ; intf->minor =-1 ; -destroy_usb_class (); }EXPORT_SYMBOL_GPL (usb_deregister_dev);
drivers/usb/core/usb.c +6 -0
@@ -1101,6 +1101,9 @@ static int __init usb_init(void) retval =usb_major_init ();if (retval) goto major_init_failed; + retval =class_register (&usbmisc_class); +if (retval) + goto class_register_failed; retval =usb_register (&usbfs_driver);if (retval) goto driver_register_failed;
@@ -1120,6 +1123,8 @@ static int __init usb_init(void) usb_devio_init_failed:usb_deregister (&usbfs_driver); driver_register_failed: +class_unregister (&usbmisc_class); +class_register_failed:usb_major_cleanup (); major_init_failed:bus_unregister_notifier (&usb_bus_type, &usb_bus_nb);
@@ -1147,6 +1152,7 @@ static void __exit usb_exit(void)usb_deregister (&usbfs_driver);usb_devio_cleanup ();usb_hub_cleanup (); +class_unregister (&usbmisc_class);bus_unregister_notifier (&usb_bus_type, &usb_bus_nb);bus_unregister (&usb_bus_type);usb_acpi_unregister ();
drivers/usb/core/usb.h +1 -0
@@ -141,6 +141,7 @@ static inline int usb_disable_usb2_hardware_lpm(struct usb_device *udev)#endif +extern const struct class usbmisc_class;extern const struct bus_type usb_bus_type;extern struct mutex usb_port_peer_mutex;extern struct device_type usb_device_type;
Signature · 1 line
2.34.1
Greg KH
maintainerQuoted 13 lines · Ivan Orlov
Now that the driver core allows for struct class to be in read-only memory, remove the
usb_classstructure and create theusbmisc_classconst class structure declared at build time which places it into read-only memory, instead of having it to be dynamically allocated at load time.Additionally, now we register usb class at startup and unregister it when shutting down, so we don't have to count uses of the class. Therefore we don't need the '
usb_class' structure anymore. Due to this fact, remove all static functions related to class initialization and deinitialization. We can't use them in 'usb.c' since they are static and we don't really need them anymore.
Quoted 8 lines
Since we have to register the class in
usb_initfunction in 'usb.c' and use it in 'file.c' as well, declare theusbmisc_classstructure as 'export' in the 'usb.h' file.Debatable moment: the class registration and unregistration functions could be extracted to the '
file.c'. I think we don't want to do this since it would be one-line functions. They would make the code paths more confusing and add calling overhead.
Nah, your cleanup here was much nicer overall:
Quoted 4 lines
drivers/usb/core/file.c | 63 ++++------------------------------------- drivers/usb/core/usb.c | 6 ++++ drivers/usb/core/usb.h | 1 + 3 files changed, 13 insertions(+), 57 deletions(-)
Deleting more lines than added is always a good thing, thanks for the revision!
greg k-h
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Ivan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.4-rc7 next-20230621] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ivan-Orlov/USB-make-usb-class-a-const-structure/20230621-231418 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing patch link: https://lore.kernel.org/r/20230621150903.1219439-1-ivan.orlov0322%40gmail.com patch subject: [PATCH v2] USB: make usb class a const structure config: hexagon-randconfig-r041-20230621 (https://download.01.org/0day-ci/archive/20230622/[email protected]/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project.git f28c006a5895fc0e329fe15fead81e37457cb1d1) reproduce: (https://download.01.org/0day-ci/archive/20230622/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from drivers/usb/core/file.c:25:
In file included from include/linux/usb.h:16:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
^
In file included from drivers/usb/core/file.c:25:
In file included from include/linux/usb.h:16:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
^
In file included from drivers/usb/core/file.c:25:
In file included from include/linux/usb.h:16:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
>> drivers/usb/core/file.c:132:6: warning: variable 'retval' is uninitialized when used here [-Wuninitialized]
if (retval)
^~~~~~
drivers/usb/core/file.c:113:12: note: initialize the variable 'retval' to silence this warning
int retval;
^
= 07 warnings generated.
vim +/retval +132 drivers/usb/core/file.c
^1da177e4c3f41 Linus Torvalds 2005-04-16 126
^1da177e4c3f41 Linus Torvalds 2005-04-16 127 if (class_driver->fops == NULL)
0026e00523a85b Alan Stern 2010-09-21 128 return -EINVAL;
0026e00523a85b Alan Stern 2010-09-21 129 if (intf->minor >= 0)
0026e00523a85b Alan Stern 2010-09-21 130 return -EADDRINUSE;
0026e00523a85b Alan Stern 2010-09-21 131
0026e00523a85b Alan Stern 2010-09-21 @132 if (retval)
0026e00523a85b Alan Stern 2010-09-21 133 return retval;
0026e00523a85b Alan Stern 2010-09-21 134
079d440e103da3 Greg Kroah-Hartman 2012-05-01 135 dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
^1da177e4c3f41 Linus Torvalds 2005-04-16 136
d4ead16f50f9ad Alan Stern 2007-05-22 137 down_write(&minor_rwsem);
^1da177e4c3f41 Linus Torvalds 2005-04-16 138 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
^1da177e4c3f41 Linus Torvalds 2005-04-16 139 if (usb_minors[minor])
^1da177e4c3f41 Linus Torvalds 2005-04-16 140 continue;
^1da177e4c3f41 Linus Torvalds 2005-04-16 141
^1da177e4c3f41 Linus Torvalds 2005-04-16 142 usb_minors[minor] = class_driver->fops;
0026e00523a85b Alan Stern 2010-09-21 143 intf->minor = minor;
^1da177e4c3f41 Linus Torvalds 2005-04-16 144 break;
^1da177e4c3f41 Linus Torvalds 2005-04-16 145 }
303911cfc5b95d Alan Stern 2019-08-12 146 if (intf->minor < 0) {
d4ead16f50f9ad Alan Stern 2007-05-22 147 up_write(&minor_rwsem);
0026e00523a85b Alan Stern 2010-09-21 148 return -EXFULL;
303911cfc5b95d Alan Stern 2019-08-12 149 }
^1da177e4c3f41 Linus Torvalds 2005-04-16 150
^1da177e4c3f41 Linus Torvalds 2005-04-16 151 /* create a usb class device for this usb interface */
7071a3ce0ca058 Kay Sievers 2008-05-02 152 snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
56a929ad72b02b Ivan Orlov 2023-06-21 153 intf->usb_dev = device_create(&usbmisc_class, &intf->dev,
f7a386c5b8ff34 Kay Sievers 2009-04-30 154 MKDEV(USB_MAJOR, minor), class_driver,
f8868ed0078a84 Andy Shevchenko 2015-12-10 155 "%s", kbasename(name));
0873c76485c126 Greg Kroah-Hartman 2006-06-20 156 if (IS_ERR(intf->usb_dev)) {
0026e00523a85b Alan Stern 2010-09-21 157 usb_minors[minor] = NULL;
0026e00523a85b Alan Stern 2010-09-21 158 intf->minor = -1;
0873c76485c126 Greg Kroah-Hartman 2006-06-20 159 retval = PTR_ERR(intf->usb_dev);
^1da177e4c3f41 Linus Torvalds 2005-04-16 160 }
303911cfc5b95d Alan Stern 2019-08-12 161 up_write(&minor_rwsem);
^1da177e4c3f41 Linus Torvalds 2005-04-16 162 return retval;
^1da177e4c3f41 Linus Torvalds 2005-04-16 163 }
782e70c6fc2290 Greg Kroah-Hartman 2008-01-25 164 EXPORT_SYMBOL_GPL(usb_register_dev);
^1da177e4c3f41 Linus Torvalds 2005-04-16 165 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki