[PATCH v3 04/31] USB: serial: Register USB drivers with their owning module

From: Leon Romanovsky

Date: Wed Nov 18 2026 - 14:28:25 EST


From: Leon Romanovsky <leonro@xxxxxxxxxx>

The USB-serial core allocates each USB driver on behalf of its serial
module, which deregisters and frees it during module exit. Registering
through usb_register() instead identifies the USB-serial core as its
owner, so a module reference does not retain the driver's allocation.

Pass the serial module's owner and KBUILD_MODNAME to usb_register_driver().
Keep the module name separate from the USB driver name so built-in drivers
also link to the correct module.

Fixes: 68e24113457e ("USB: serial: rework usb_serial_register/deregister_drivers()")
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxx>
---
drivers/usb/serial/usb-serial.c | 4 +++-
include/linux/usb/serial.h | 4 +++-
2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 4a19621f08ba2..ccbd0875339a3 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -1513,6 +1513,7 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
* @serial_drivers: NULL-terminated array of pointers to drivers to be registered
* @owner: owning module
* @name: name of the usb_driver for this set of @serial_drivers
+ * @mod_name: name of the module containing the 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
@@ -1520,6 +1521,7 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
*/
int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
struct module *owner, const char *name,
+ const char *mod_name,
const struct usb_device_id *id_table)
{
int rc;
@@ -1559,7 +1561,7 @@ int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers
}
}

- rc = usb_register(udriver);
+ rc = usb_register_driver(udriver, owner, mod_name);
if (rc)
goto err_free_driver;

diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 5866f19d3c7b8..fcfd0e26e0ffb 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -321,9 +321,11 @@ struct usb_serial_driver {
container_of(d, struct usb_serial_driver, driver)

#define usb_serial_register_drivers(serial_drivers, name, id_table) \
- __usb_serial_register_drivers(serial_drivers, THIS_MODULE, name, id_table)
+ __usb_serial_register_drivers(serial_drivers, THIS_MODULE, name, \
+ KBUILD_MODNAME, id_table)
int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
struct module *owner, const char *name,
+ const char *mod_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);

--
2.56.1