Skip to contentSkip to conversation

USB: make usb class a const structure

PATCH Superseded Ivan Orlov 2 participants 2 messages Jun 21, 2023 USB

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH v3 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 V2 -> V3:
- Remove redundant retval comparison found by 0-day bot

 drivers/usb/core/file.c | 66 ++++-------------------------------------
 drivers/usb/core/usb.c  |  6 ++++
 drivers/usb/core/usb.h  |  1 +
 3 files changed, 13 insertions(+), 60 deletions(-)
drivers/usb/core/file.c +6 -60
index c4ed3310e069..f7627cd3f647 100644
@@ -29,7 +29,6 @@
 #define MAX_USB_MINORS	256
 static 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,13 +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;
-
 	dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
 
 	down_write(&minor_rwsem);
@@ -200,7 +147,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 +181,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 +189,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
index 901ec732321c..39bdf9589b06 100644
@@ -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
index ffe3f6818e9c..69ca59841083 100644
@@ -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
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Ivan,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-next]
[also build test WARNING on usb/usb-linus linus/master v6.4-rc7 next-20230621] [cannot apply to usb/usb-testing]
[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/20230622-015606
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-next
patch link:    https://lore.kernel.org/r/20230621175259.1221852-1-ivan.orlov0322%40gmail.com
patch subject: [PATCH v3] USB: make usb class a const structure
config: hexagon-randconfig-r005-20230622 (https://download.01.org/0day-ci/archive/20230622/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
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:153:2: warning: variable 'retval' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
           if (IS_ERR(intf->usb_dev)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:55:28: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:57:30: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/usb/core/file.c:159:9: note: uninitialized use occurs here
           return retval;
                  ^~~~~~
   drivers/usb/core/file.c:153:2: note: remove the 'if' if its condition is always true
           if (IS_ERR(intf->usb_dev)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:55:23: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                         ^
   drivers/usb/core/file.c:113:12: note: initialize the variable 'retval' to silence this warning
           int retval;
                     ^
                      = 0

7 warnings generated.

vim +153 drivers/usb/core/file.c

^1da177e4c3f415 Linus Torvalds     2005-04-16  126  
^1da177e4c3f415 Linus Torvalds     2005-04-16  127  	if (class_driver->fops == NULL)
0026e00523a85b9 Alan Stern         2010-09-21  128  		return -EINVAL;
0026e00523a85b9 Alan Stern         2010-09-21  129  	if (intf->minor >= 0)
0026e00523a85b9 Alan Stern         2010-09-21  130  		return -EADDRINUSE;
0026e00523a85b9 Alan Stern         2010-09-21  131  
079d440e103da3e Greg Kroah-Hartman 2012-05-01  132  	dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
^1da177e4c3f415 Linus Torvalds     2005-04-16  133  
d4ead16f50f9ad3 Alan Stern         2007-05-22  134  	down_write(&minor_rwsem);
^1da177e4c3f415 Linus Torvalds     2005-04-16  135  	for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
^1da177e4c3f415 Linus Torvalds     2005-04-16  136  		if (usb_minors[minor])
^1da177e4c3f415 Linus Torvalds     2005-04-16  137  			continue;
^1da177e4c3f415 Linus Torvalds     2005-04-16  138  
^1da177e4c3f415 Linus Torvalds     2005-04-16  139  		usb_minors[minor] = class_driver->fops;
0026e00523a85b9 Alan Stern         2010-09-21  140  		intf->minor = minor;
^1da177e4c3f415 Linus Torvalds     2005-04-16  141  		break;
^1da177e4c3f415 Linus Torvalds     2005-04-16  142  	}
303911cfc5b95d3 Alan Stern         2019-08-12  143  	if (intf->minor < 0) {
d4ead16f50f9ad3 Alan Stern         2007-05-22  144  		up_write(&minor_rwsem);
0026e00523a85b9 Alan Stern         2010-09-21  145  		return -EXFULL;
303911cfc5b95d3 Alan Stern         2019-08-12  146  	}
^1da177e4c3f415 Linus Torvalds     2005-04-16  147  
^1da177e4c3f415 Linus Torvalds     2005-04-16  148  	/* create a usb class device for this usb interface */
7071a3ce0ca058a Kay Sievers        2008-05-02  149  	snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
c235e8495e967ed Ivan Orlov         2023-06-21  150  	intf->usb_dev = device_create(&usbmisc_class, &intf->dev,
f7a386c5b8ff34c Kay Sievers        2009-04-30  151  				      MKDEV(USB_MAJOR, minor), class_driver,
f8868ed0078a845 Andy Shevchenko    2015-12-10  152  				      "%s", kbasename(name));
0873c76485c126a Greg Kroah-Hartman 2006-06-20 @153  	if (IS_ERR(intf->usb_dev)) {
0026e00523a85b9 Alan Stern         2010-09-21  154  		usb_minors[minor] = NULL;
0026e00523a85b9 Alan Stern         2010-09-21  155  		intf->minor = -1;
0873c76485c126a Greg Kroah-Hartman 2006-06-20  156  		retval = PTR_ERR(intf->usb_dev);
^1da177e4c3f415 Linus Torvalds     2005-04-16  157  	}
303911cfc5b95d3 Alan Stern         2019-08-12  158  	up_write(&minor_rwsem);
^1da177e4c3f415 Linus Torvalds     2005-04-16  159  	return retval;
^1da177e4c3f415 Linus Torvalds     2005-04-16  160  }
782e70c6fc2290a Greg Kroah-Hartman 2008-01-25  161  EXPORT_SYMBOL_GPL(usb_register_dev);

^1da177e4c3f415 Linus Torvalds 2005-04-16 162

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki