diff mbox series

[01/52] USB: serial: store owner from modules with usb_serial_register_drivers()

Message ID 20240328-module-owner-usb-serial-v1-1-bc46c9ffbf56@linaro.org
State Superseded
Headers show
Series USB: store owner from modules with usb_serial_register_drivers() | expand

Commit Message

Krzysztof Kozlowski March 28, 2024, 10:05 p.m. UTC
Modules registering driver with usb_serial_register_drivers() might
forget to set .owner field.  The field is used by some of other kernel
parts for reference counting (try_module_get()), so it is expected that
drivers will set it.

Solve the problem by moving this task away from the drivers to the core
amba bus code, just like we did for platform_driver in
commit 9447057eaff8 ("platform_device: use a macro instead of
platform_driver_register").

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 drivers/usb/serial/usb-serial.c | 12 +++++++-----
 include/linux/usb/serial.h      |  7 +++++--
 2 files changed, 12 insertions(+), 7 deletions(-)

Comments

Johan Hovold April 15, 2024, 8:56 a.m. UTC | #1
On Thu, Mar 28, 2024 at 11:05:39PM +0100, Krzysztof Kozlowski wrote:
> Modules registering driver with usb_serial_register_drivers() might
> forget to set .owner field.  The field is used by some of other kernel
> parts for reference counting (try_module_get()), so it is expected that
> drivers will set it.
> 
> Solve the problem by moving this task away from the drivers to the core
> amba bus code, just like we did for platform_driver in

"amba"

> commit 9447057eaff8 ("platform_device: use a macro instead of
> platform_driver_register").
> 
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>  drivers/usb/serial/usb-serial.c | 12 +++++++-----
>  include/linux/usb/serial.h      |  7 +++++--
>  2 files changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
> index f1e91eb7f8a4..a659f2096a1a 100644
> --- a/drivers/usb/serial/usb-serial.c
> +++ b/drivers/usb/serial/usb-serial.c
> @@ -1459,17 +1459,18 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
>  }
>  
>  /**
> - * usb_serial_register_drivers - register drivers for a usb-serial module
> + * __usb_serial_register_drivers - register drivers for a usb-serial module
>   * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
> + * @owner: owning module/driver

Just "module"

>   * @name: name of the usb_driver for this set of @serial_drivers
>   * @id_table: list of all devices this @serial_drivers set binds to
>   *
>   * Registers all the drivers in the @serial_drivers array, and dynamically
>   * creates a struct usb_driver with the name @name and id_table of @id_table.
>   */
> -int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
> -				const char *name,
> -				const struct usb_device_id *id_table)
> +int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
> +				  struct module *owner, const char *name,
> +				  const struct usb_device_id *id_table)

Johan
Krzysztof Kozlowski May 3, 2024, 9:54 a.m. UTC | #2
On 15/04/2024 10:56, Johan Hovold wrote:
> On Thu, Mar 28, 2024 at 11:05:39PM +0100, Krzysztof Kozlowski wrote:
>> Modules registering driver with usb_serial_register_drivers() might
>> forget to set .owner field.  The field is used by some of other kernel
>> parts for reference counting (try_module_get()), so it is expected that
>> drivers will set it.
>>
>> Solve the problem by moving this task away from the drivers to the core
>> amba bus code, just like we did for platform_driver in
> 
> "amba"
> 
>> commit 9447057eaff8 ("platform_device: use a macro instead of
>> platform_driver_register").
>>
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>> ---
>>  drivers/usb/serial/usb-serial.c | 12 +++++++-----
>>  include/linux/usb/serial.h      |  7 +++++--
>>  2 files changed, 12 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
>> index f1e91eb7f8a4..a659f2096a1a 100644
>> --- a/drivers/usb/serial/usb-serial.c
>> +++ b/drivers/usb/serial/usb-serial.c
>> @@ -1459,17 +1459,18 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
>>  }
>>  
>>  /**
>> - * usb_serial_register_drivers - register drivers for a usb-serial module
>> + * __usb_serial_register_drivers - register drivers for a usb-serial module
>>   * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
>> + * @owner: owning module/driver
> 
> Just "module"

Ack for both.

Best regards,
Krzysztof
diff mbox series

Patch

diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index f1e91eb7f8a4..a659f2096a1a 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -1459,17 +1459,18 @@  static void usb_serial_deregister(struct usb_serial_driver *device)
 }
 
 /**
- * usb_serial_register_drivers - register drivers for a usb-serial module
+ * __usb_serial_register_drivers - register drivers for a usb-serial module
  * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
+ * @owner: owning module/driver
  * @name: name of the usb_driver for this set of @serial_drivers
  * @id_table: list of all devices this @serial_drivers set binds to
  *
  * Registers all the drivers in the @serial_drivers array, and dynamically
  * creates a struct usb_driver with the name @name and id_table of @id_table.
  */
-int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
-				const char *name,
-				const struct usb_device_id *id_table)
+int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
+				  struct module *owner, const char *name,
+				  const struct usb_device_id *id_table)
 {
 	int rc;
 	struct usb_driver *udriver;
@@ -1514,6 +1515,7 @@  int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[]
 
 	for (sd = serial_drivers; *sd; ++sd) {
 		(*sd)->usb_driver = udriver;
+		(*sd)->driver.owner = owner;
 		rc = usb_serial_register(*sd);
 		if (rc)
 			goto err_deregister_drivers;
@@ -1532,7 +1534,7 @@  int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[]
 	kfree(udriver);
 	return rc;
 }
-EXPORT_SYMBOL_GPL(usb_serial_register_drivers);
+EXPORT_SYMBOL_GPL(__usb_serial_register_drivers);
 
 /**
  * usb_serial_deregister_drivers - deregister drivers for a usb-serial module
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 1a0a4dc87980..75b2b763f1ba 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -311,8 +311,11 @@  struct usb_serial_driver {
 #define to_usb_serial_driver(d) \
 	container_of(d, struct usb_serial_driver, driver)
 
-int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
-		const char *name, const struct usb_device_id *id_table);
+#define usb_serial_register_drivers(serial_drivers, name, id_table) \
+	__usb_serial_register_drivers(serial_drivers, THIS_MODULE, name, id_table)
+int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
+				  struct module *owner, const char *name,
+				  const struct usb_device_id *id_table);
 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[]);
 void usb_serial_port_softint(struct usb_serial_port *port);