USB: roles: make role_class a static const structure
Greg Kroah-Hartman
maintainerPATCH 1/6 USB: roles: make role_class a static const structure
From: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, move the role_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time.
--- drivers/usb/roles/class.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/usb/roles/class.c +9 -8
@@ -14,7 +14,9 @@#include <linux/mutex.h> #include <linux/slab.h> -static struct class *role_class; +static const struct class role_class = { + .name ="usb_role" , +};struct usb_role_switch {struct device dev;
@@ -95,7 +97,7 @@ static void *usb_role_switch_match(const struct fwnode_handle *fwnode, const chaif (id && !fwnode_property_present (fwnode, id))return NULL ; - dev =class_find_device_by_fwnode (role_class, fwnode); + dev =class_find_device_by_fwnode (&role_class, fwnode);return dev ?to_role_switch (dev) :ERR_PTR (-EPROBE_DEFER ); }
@@ -111,7 +113,7 @@ usb_role_switch_is_parent(struct fwnode_handle *fwnode)return NULL ; } - dev =class_find_device_by_fwnode (role_class, parent); + dev =class_find_device_by_fwnode (&role_class, parent);fwnode_handle_put (parent);return dev ?to_role_switch (dev) :ERR_PTR (-EPROBE_DEFER ); }
@@ -191,7 +193,7 @@ usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)if (!fwnode)return NULL ; - dev =class_find_device_by_fwnode (role_class, fwnode); + dev =class_find_device_by_fwnode (&role_class, fwnode);if (dev)WARN_ON (!try_module_get (dev->parent->driver->owner));
@@ -338,7 +340,7 @@ usb_role_switch_register(struct device *parent, sw->dev.parent = parent; sw->dev.fwnode = desc->fwnode; - sw->dev.class = role_class; + sw->dev.class = &role_class; sw->dev.type = &usb_role_dev_type;dev_set_drvdata (&sw->dev, desc->driver_data); dev_set_name(&sw->dev, "%s-role-switch",
@@ -392,14 +394,13 @@ EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);static int __init usb_roles_init (void ) { - role_class =class_create ("usb_role" ); -return PTR_ERR_OR_ZERO (role_class); +return class_register (&role_class); }subsys_initcall (usb_roles_init); static void __exit usb_roles_exit(void) { -class_destroy (role_class); +class_unregister (&role_class); }module_exit (usb_roles_exit);
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerPATCH 2/6 USB: gadget: udc: core: make udc_class a static const structure
From: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, move the udc_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time.
--- drivers/usb/gadget/udc/core.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/usb/gadget/udc/core.c +12 -12
@@ -50,7 +50,7 @@ struct usb_udc {bool started; }; -static struct class *udc_class; +static const struct class udc_class;static LIST_HEAD (udc_list);/* Protects udc_list, udc->driver, driver->is_bound, and related calls */
@@ -1312,7 +1312,7 @@ int usb_add_gadget(struct usb_gadget *gadget)device_initialize (&udc->dev); udc->dev.release = usb_udc_release; - udc->dev.class = udc_class; + udc->dev.class = &udc_class; udc->dev.groups = usb_udc_attr_groups; udc->dev.parent = gadget->dev.parent; ret = dev_set_name(&udc->dev, "%s",
@@ -1774,6 +1774,11 @@ static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env)return 0 ; } +static const struct class udc_class = { + .name ="udc" , + .dev_uevent = usb_udc_uevent, +}; +static const struct bus_type gadget_bus_type = { .name ="gadget" , .probe = gadget_bind_driver,
@@ -1785,18 +1790,13 @@ static int __init usb_udc_init(void) {int rc; - udc_class =class_create ("udc" ); -if (IS_ERR (udc_class)) { -pr_err ("failed to create udc class --> %ld\n" , -PTR_ERR (udc_class)); -return PTR_ERR (udc_class); - } - - udc_class->dev_uevent = usb_udc_uevent; + rc =class_register (&udc_class); +if (rc) +return rc; rc =bus_register (&gadget_bus_type);if (rc) -class_destroy (udc_class); +class_unregister (&udc_class);return rc; }subsys_initcall (usb_udc_init);
@@ -1804,7 +1804,7 @@ subsys_initcall(usb_udc_init); static void __exit usb_udc_exit(void) {bus_unregister (&gadget_bus_type); -class_destroy (udc_class); +class_unregister (&udc_class); }module_exit (usb_udc_exit);
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerPATCH 3/6 USB: mon: make mon_bin_class a static const structure
In reply to Greg Kroah-HartmanFrom: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, move the mon_bin_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time.
--- drivers/usb/mon/mon_bin.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/usb/mon/mon_bin.c +10 -9
@@ -213,7 +213,10 @@ static unsigned char xfer_to_pipe[4] = { PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT }; -static struct class *mon_bin_class; +static const struct class mon_bin_class = { + .name ="usbmon" , +}; +static dev_t mon_bin_dev0;static struct cdev mon_bin_cdev;
@@ -1360,7 +1363,7 @@ int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)if (minor >=MON_BIN_MAX_MINOR )return 0 ; - dev =device_create (mon_bin_class, ubus ? ubus->controller :NULL , + dev =device_create (&mon_bin_class, ubus ? ubus->controller :NULL ,MKDEV (MAJOR (mon_bin_dev0), minor),NULL ,"usbmon%d" , minor);if (IS_ERR (dev))
@@ -1372,18 +1375,16 @@ int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)void mon_bin_del (struct mon_bus *mbus) { -device_destroy (mon_bin_class, mbus->classdev->devt); +device_destroy (&mon_bin_class, mbus->classdev->devt); }int __init mon_bin_init (void ) {int rc; - mon_bin_class =class_create ("usbmon" ); -if (IS_ERR (mon_bin_class)) { - rc =PTR_ERR (mon_bin_class); + rc =class_register (&mon_bin_class); +if (rc) goto err_class; - } rc =alloc_chrdev_region (&mon_bin_dev0,0 ,MON_BIN_MAX_MINOR ,"usbmon" );if (rc <0 )
@@ -1401,7 +1402,7 @@ int __init mon_bin_init(void) err_add:unregister_chrdev_region (mon_bin_dev0,MON_BIN_MAX_MINOR ); err_dev: -class_destroy (mon_bin_class); +class_unregister (&mon_bin_class); err_class:return rc; }
@@ -1410,5 +1411,5 @@ void mon_bin_exit(void) {cdev_del (&mon_bin_cdev);unregister_chrdev_region (mon_bin_dev0,MON_BIN_MAX_MINOR ); -class_destroy (mon_bin_class); +class_unregister (&mon_bin_class); }
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerPATCH 4/6 USB: gadget: f_printer: make usb_gadget_class a static const structure
In reply to Greg Kroah-HartmanFrom: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, move the usb_gadget_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time.
--- drivers/usb/gadget/function/f_printer.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/usb/gadget/function/f_printer.c +11 -14
@@ -54,7 +54,10 @@#define DEFAULT_Q_LEN 10/* same as legacy g_printer gadget */ static int major, minors; -static struct class *usb_gadget_class; +static const struct class usb_gadget_class = { + .name ="usb_printer_gadget" , +}; +static DEFINE_IDA (printer_ida);static DEFINE_MUTEX (printer_ida_lock);/* protects access do printer_ida */
@@ -1120,7 +1123,7 @@ static int printer_func_bind(struct usb_configuration *c,/* Setup the sysfs files for the printer gadget. */ devt =MKDEV (major, dev->minor); - pdev =device_create (usb_gadget_class,NULL , devt, + pdev =device_create (&usb_gadget_class,NULL , devt,NULL ,"g_printer%d" , dev->minor);if (IS_ERR (pdev)) {ERROR (dev,"Failed to create device: g_printer\n" );
@@ -1143,7 +1146,7 @@ static int printer_func_bind(struct usb_configuration *c,return 0 ; fail_cdev_add: -device_destroy (usb_gadget_class, devt); +device_destroy (&usb_gadget_class, devt); fail_rx_reqs:while (!list_empty (&dev->rx_reqs)) {
@@ -1410,7 +1413,7 @@ static void printer_func_unbind(struct usb_configuration *c, dev =func_to_printer (f); -device_destroy (usb_gadget_class,MKDEV (major, dev->minor)); +device_destroy (&usb_gadget_class,MKDEV (major, dev->minor));/* Remove Character Device */ cdev_del (&dev->printer_cdev);
@@ -1512,19 +1515,14 @@ static int gprinter_setup(int count)int status;dev_t devt; - usb_gadget_class =class_create ("usb_printer_gadget" ); -if (IS_ERR (usb_gadget_class)) { - status =PTR_ERR (usb_gadget_class); - usb_gadget_class =NULL ; -pr_err ("unable to create usb_gadget class %d\n" , status); + status =class_register (&usb_gadget_class); +if (status)return status; - } status =alloc_chrdev_region (&devt,0 , count,"USB printer gadget" );if (status) {pr_err ("alloc_chrdev_region %d\n" , status); -class_destroy (usb_gadget_class); - usb_gadget_class =NULL ; +class_unregister (&usb_gadget_class);return status; }
@@ -1540,6 +1538,5 @@ static void gprinter_cleanup(void)unregister_chrdev_region (MKDEV (major,0 ), minors); major = minors =0 ; } -class_destroy (usb_gadget_class); - usb_gadget_class =NULL ; +class_unregister (&usb_gadget_class); }
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerPATCH 5/6 USB: gadget: f_hid: make hidg_class a static const structure
In reply to Greg Kroah-HartmanFrom: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, move the hidg_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time.
--- drivers/usb/gadget/function/f_hid.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/usb/gadget/function/f_hid.c +10 -11
@@ -23,7 +23,11 @@#define HIDG_MINORS 4static int major, minors; -static struct class *hidg_class; + +static const struct class hidg_class = { + .name ="hidg" , +}; +static DEFINE_IDA (hidg_ida);static DEFINE_MUTEX (hidg_ida_lock);/* protects access to hidg_ida */
@@ -1272,7 +1276,7 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)device_initialize (&hidg->dev); hidg->dev.release = hidg_release; - hidg->dev.class = hidg_class; + hidg->dev.class = &hidg_class; hidg->dev.devt =MKDEV (major, opts->minor); ret =dev_set_name (&hidg->dev,"hidg%d" , opts->minor);if (ret)
@@ -1325,17 +1329,13 @@ int ghid_setup(struct usb_gadget *g, int count)int status;dev_t dev; - hidg_class =class_create ("hidg" ); -if (IS_ERR (hidg_class)) { - status =PTR_ERR (hidg_class); - hidg_class =NULL ; + status =class_register (&hidg_class); +if (status)return status; - } status =alloc_chrdev_region (&dev,0 , count,"hidg" );if (status) { -class_destroy (hidg_class); - hidg_class =NULL ; +class_unregister (&hidg_class);return status; }
@@ -1352,6 +1352,5 @@ void ghid_cleanup(void) major = minors =0 ; } -class_destroy (hidg_class); - hidg_class =NULL ; +class_unregister (&hidg_class); }
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerPATCH 6/6 USB: file.c: make usb class a static const structure
In reply to Greg Kroah-HartmanFrom: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, remove the class field of the usb_class structure and create the usbmisc_class static class structure declared at build time which places it into read-only memory, instead of having it to be dynamically allocated at load time.
--- drivers/usb/core/file.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/usb/core/file.c +10 -9
@@ -59,7 +59,6 @@ static const struct file_operations usb_fops = {static struct usb_class {struct kref kref; -struct class *class; } *usb_class;static char *usb_devnode (const struct device *dev,umode_t *mode)
@@ -72,6 +71,11 @@ static char *usb_devnode(const struct device *dev, umode_t *mode)return drv->devnode (dev, mode); } +static struct class usbmisc_class = { + .name ="usbmisc" , + .devnode = usb_devnode, +}; +static int init_usb_class (void ) {int result =0 ;
@@ -88,15 +92,12 @@ static int init_usb_class(void) }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" ); + result =class_register (&usbmisc_class); +if (result) {kfree (usb_class); usb_class =NULL ; goto exit; } - usb_class->class->devnode = usb_devnode; exit:return result;
@@ -105,7 +106,7 @@ static int init_usb_class(void)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); +class_unregister (&usbmisc_class);kfree (usb_class); usb_class =NULL ; }
@@ -200,7 +201,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 +235,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 ;
Signature · 1 line
2.41.0
Greg Kroah-Hartman
maintainerRe: [PATCH 6/6] USB: file.c: make usb class a static const structure
Quoted 20 lines · Greg Kroah-Hartman
From: Ivan Orlov <[email protected]>
Now that the driver core allows for struct class to be in read-only memory, remove the class field of the
usb_classstructure and create theusbmisc_classstatic class structure declared at build time which places it into read-only memory, instead of having it to be dynamically allocated at load time.Suggested-by Greg Kroah-Hartman <[email protected]>Signed-off-by Ivan Orlov <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- drivers/usb/core/file.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)drivers/usb/core/file.c +0
-0
drivers/usb/core/file.c static const struct file_operations usb_fops = { · line 62
Quoted 5 lines
drivers/usb/core/file.c +0
-1@@ -59,7 +59,6 @@ static const struct file_operations usb_fops = {static struct usb_class {struct kref kref; -struct class *class; } *usb_class;
Is this structure needed anymore at all now that the thing the kref was "protecting" is gone? I think it can be dropped entirely, right?
thanks,
greg k-h
Re: [PATCH 6/6] USB: file.c: make usb class a static const structure
Quoted 6 lines · Greg Kroah-Hartman
Is this structure needed anymore at all now that the thing the kref was "protecting" is gone? I think it can be dropped entirely, right?
thanks,
greg k-h
As I understood after reading the code, this kref is used for checking how many devices uses our class and testing when we can unregister it.
As we register our class only when the first device is registered, I think the best solution is to move this kref structure out of the usb_class structure as we still need it for detecting when we could finally unregister our class. What do you think about it?
Greg Kroah-Hartman
maintainerRe: [PATCH 6/6] USB: file.c: make usb class a static const structure
Quoted 8 lines · Ivan Orlov
Quoted 6 lines · Greg Kroah-Hartman
Is this structure needed anymore at all now that the thing the kref was "protecting" is gone? I think it can be dropped entirely, right?
thanks,
greg k-h
Quoted 7 lines
As I understood after reading the code, this kref is used for checking how many devices uses our class and testing when we can unregister it.
As we register our class only when the first device is registered, I think the best solution is to move this kref structure out of the
usb_classstructure as we still need it for detecting when we could finally unregister our class. What do you think about it?
I think we should make it simpler, allocate the class when we start up, and free it when we shut down, which guarantees that all users of the class are removed at that time as this is part of the usb core code.
No need to be fancy anymore with the dynamic creation/removal of the class, it's just not worth it :)
thanks,
greg k-h
Re: [PATCH 6/6] USB: file.c: make usb class a static const structure
Quoted 10 lines · Greg Kroah-Hartman
I think we should make it simpler, allocate the class when we start up, and free it when we shut down, which guarantees that all users of the class are removed at that time as this is part of the usb core code.
No need to be fancy anymore with the dynamic creation/removal of the class, it's just not worth it :)
thanks,
greg k-h
Alright, it sounds really reasonable, let's do it in this way :)
I'll add init_usb_class call to the 'usb_init' function and the corresponding releasing function call to the 'usb_exit' function in the 'drivers/usb/core/usb.c' file. So we would register class at startup and unregister it when shutting down.
Greg Kroah-Hartman
maintainerRe: [PATCH 6/6] USB: file.c: make usb class a static const structure
Quoted 10 lines · Ivan Orlov
Quoted 9 lines · Greg Kroah-Hartman
I think we should make it simpler, allocate the class when we start up, and free it when we shut down, which guarantees that all users of the class are removed at that time as this is part of the usb core code.
No need to be fancy anymore with the dynamic creation/removal of the class, it's just not worth it :)
thanks,
Quoted 9 lines
Quoted 1 line
greg k-h
Alright, it sounds really reasonable, let's do it in this way :)
I'll add
init_usb_classcall to the 'usb_init' function and the corresponding releasing function call to the 'usb_exit' function in the 'drivers/usb/core/usb.c' file. So we would register class at startup and unregister it when shutting down.
Totally reasonable, thanks for doing this work.
greg k-h