Netdev List
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Andrew Lunn <andrew@lunn.ch>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	Russell King <linux@armlinux.org.uk>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	"Joerg Roedel (AMD)" <joro@8bytes.org>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Vasant Hegde <vasant.hegde@amd.com>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	David Woodhouse <dwmw2@infradead.org>,
	Lu Baolu <baolu.lu@linux.intel.com>,
	Johan Hovold <johan@kernel.org>,
	Eric Dumazet <edumazet@kernel.org>,
	Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Nipun Gupta <nipun.gupta@amd.com>,
	Nikhil Agarwal <nikhil.agarwal@amd.com>,
	Pieter Jansen van Vuuren <pieter.jansen-van-vuuren@amd.com>,
	Chris Lu <chris.lu@mediatek.com>, Mans Rullgard <mans@mansr.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Cornelia Huck <cornelia.huck@de.ibm.com>,
	Andrew Morton <akpm@osdl.org>, Jeff Garzik <jeff@garzik.org>
Cc: linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
	linux-input@vger.kernel.org, netdev@vger.kernel.org,
	linux-wireless@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-bluetooth@vger.kernel.org, linux-acpi@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-pci@vger.kernel.org,
	iommu@lists.linux.dev, linux-coco@lists.linux.dev,
	Alexey Kardashevskiy <aik@amd.com>,
	Luiz Augusto von Dentz <luiz.von.dentz@intel.com>,
	Greg Kroah-Hartman <gregkh@suse.de>,
	linux-sound@vger.kernel.org, Jason Gunthorpe <jgg@ziepe.ca>
Subject: [PATCH v3 09/31] USB: Pin modules for unregistered interface claims
Date: Wed, 18 Nov 2026 21:26:39 +0200	[thread overview]
Message-ID: <20261118-device-trust-core-v3-9-d655875c4bfb@nvidia.com> (raw)
In-Reply-To: <20261118-device-trust-core-v3-0-d655875c4bfb@nvidia.com>

From: Leon Romanovsky <leonro@nvidia.com>

A successful CDC-ACM control probe can claim its data interface before
registration. If data registration fails, cleanup can race with module
unload when no tty is open:

Thread A (cleanup)                            Thread B (unload)

usb_driver_release_refused_claim(data)
 usb_unbind_interface(data)
  acm_disconnect(data)
   usb_driver_release_interface(control)
    device_release_driver(control)
     device_release_driver_internal()
      __device_release_driver()
       klist_remove() [last bound interface]
                                              delete_module()
                                               acm_exit()
                                                usb_deregister()
                                                 driver_unregister()
                                                  bus_remove_driver()
                                                   driver_detach()
                                                    [empty list]
                                               free_module()
   [resumes in freed acm_disconnect()]
   tty_port_put()

The empty list lets unload skip the USB device lock and free the module
before the callback returns. Hold a module reference through registration
or cleanup, preparing for the cleanup added next.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/driver.c  | 17 +++++++++++++++++
 drivers/usb/core/message.c |  6 ++++++
 drivers/usb/core/usb.h     |  1 +
 include/linux/usb.h        |  2 ++
 4 files changed, 26 insertions(+)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 0a6fdf2e88f4e..b7bd9226d2627 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -529,6 +529,13 @@ static void usb_shutdown_interface(struct device *dev)
 		driver->shutdown(intf);
 }
 
+void usb_put_claim_owner(struct usb_interface *intf)
+{
+	struct module *owner = xchg(&intf->claim_owner, NULL);
+
+	module_put(owner);
+}
+
 /**
  * usb_driver_claim_interface - bind a driver to an interface
  * @driver: the driver to be bound
@@ -567,6 +574,14 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 		goto unlock;
 	}
 
+	if (!device_is_registered(dev)) {
+		if (!try_module_get(driver->driver.owner)) {
+			retval = -ENODEV;
+			goto unlock;
+		}
+		iface->claim_owner = driver->driver.owner;
+	}
+
 	dev->driver = &driver->driver;
 	usb_set_intfdata(iface, data);
 	iface->needs_binding = 0;
@@ -619,6 +634,7 @@ static void usb_unbind_claim(struct usb_interface *iface)
 	usb_unbind_interface(dev);
 	dev_set_drvdata(dev, NULL);
 	dev->driver = NULL;
+	usb_put_claim_owner(iface);
 }
 
 /**
@@ -653,6 +669,7 @@ void usb_driver_release_interface(struct usb_driver *driver,
 	if (device_is_bound(dev)) {
 		device_unlock(dev);
 		device_release_driver(dev);
+		usb_put_claim_owner(iface);
 		return;
 	}
 
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index e56079398d26e..306978caaf1ef 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1845,6 +1845,7 @@ static void usb_release_interface(struct device *dev)
 	struct usb_interface_cache *intfc =
 			altsetting_to_usb_interface_cache(intf->altsetting);
 
+	usb_put_claim_owner(intf);
 	kref_put(&intfc->ref, usb_release_interface_cache);
 	usb_put_dev(interface_to_usbdev(intf));
 	of_node_put(dev->of_node);
@@ -2269,6 +2270,7 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 	 */
 	for (i = 0; i < nintf; ++i) {
 		struct usb_interface *intf = cp->interface[i];
+		struct module *claim_owner;
 
 		if (intf->dev.of_node &&
 		    !of_device_is_available(intf->dev.of_node)) {
@@ -2282,12 +2284,16 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 			dev_name(&intf->dev), configuration,
 			intf->cur_altsetting->desc.bInterfaceNumber);
 		device_enable_async_suspend(&intf->dev);
+		device_lock(&intf->dev);
+		claim_owner = xchg(&intf->claim_owner, NULL);
+		device_unlock(&intf->dev);
 		ret = device_add(&intf->dev);
 		if (ret) {
 			device_lock(&intf->dev);
 			intf->unregistering = 1;
 			device_unlock(&intf->dev);
 		}
+		module_put(claim_owner);
 		if (ret != 0) {
 			dev_err(&dev->dev, "device_add(%s) --> %d\n",
 				dev_name(&intf->dev), ret);
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index a9b37aeb515be..dfa48f43046d4 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -49,6 +49,7 @@ extern int usb_set_isoch_delay(struct usb_device *dev);
 extern int usb_get_bos_descriptor(struct usb_device *dev);
 extern void usb_release_bos_descriptor(struct usb_device *dev);
 extern int usb_set_configuration(struct usb_device *dev, int configuration);
+void usb_put_claim_owner(struct usb_interface *intf);
 extern int usb_choose_configuration(struct usb_device *udev);
 extern int usb_generic_driver_probe(struct usb_device *udev);
 extern void usb_generic_driver_disconnect(struct usb_device *udev);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 825f03b290670..a5e706f67f33a 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -213,6 +213,7 @@ enum usb_wireless_status {
  * @dev: driver model's view of this device
  * @usb_dev: if an interface is bound to the USB major, this will point
  *	to the sysfs representation for that device.
+ * @claim_owner: Module reference for an unpublished claim.
  * @reset_ws: Used for scheduling resets from atomic context.
  * @resetting_device: USB core reset the device, so use alt setting 0 as
  *	current; needs bandwidth alloc after reset.
@@ -271,6 +272,7 @@ struct usb_interface {
 
 	struct device dev;		/* interface specific device info */
 	struct device *usb_dev;
+	struct module *claim_owner;
 	struct work_struct reset_ws;	/* for resets in atomic context */
 };
 

-- 
2.56.1


  parent reply	other threads:[~2026-11-18 19:27 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 01/31] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 02/31] Bluetooth: btusb: Fix MediaTek ISO claim locking Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 03/31] USB: Lock interfaces during manual driver binding Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 04/31] USB: serial: Register USB drivers with their owning module Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 05/31] USB: Reject claims of DT-disabled interfaces Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 06/31] USB: Release interface claims according to binding state Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 07/31] USB: Reject claims after interface registration failure Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 08/31] ALSA: usb-audio: Release interface claims when probe fails Leon Romanovsky
2026-11-18 19:26 ` Leon Romanovsky [this message]
2026-11-18 19:26 ` [PATCH v3 10/31] USB: Add helpers to release unpublished interface claims Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 11/31] USB: Unwind interface claims after registration or binding failure Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 12/31] PNP: Attach card devices through the driver core Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 13/31] cdx: Propagate DMA configuration errors Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 14/31] driver core: Report deferred probes through sysfs drivers_probe Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 15/31] driver core: Add a compatibility API for manual driver attachment Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 16/31] Input: gameport - use the compatibility driver attachment API Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 17/31] Input: serio " Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 18/31] net: phy: Use " Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 19/31] driver core: Introduce a generic device trust level Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 20/31] driver core: Expose device trust authorization interfaces Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 21/31] driver core: Accept device trust policy on the kernel command line Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 22/31] driver core: Accept device trust policy by driver name Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 23/31] driver core: Expose committed device trust level Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 24/31] driver core: Cover trust authorization and binding with KUnit Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 25/31] driver core: Document trust admission and initrd authorization Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 26/31] PCI: Use device trust for platform restrictions Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 27/31] iommu: Use device trust for DMA restrictions Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 28/31] driver core: Allow driver attachment with the device lock held Leon Romanovsky
2026-11-18 19:26 ` [PATCH v3 29/31] USB: Retain the result of configuration requests Leon Romanovsky
2026-11-18 19:27 ` [PATCH v3 30/31] USB: Authorize interfaces through device trust Leon Romanovsky
2026-11-18 19:27 ` [PATCH v3 31/31] USB: Authorize devices " Leon Romanovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118-device-trust-core-v3-9-d655875c4bfb@nvidia.com \
    --to=leon@kernel.org \
    --cc=aik@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=akpm@osdl.org \
    --cc=andrew@lunn.ch \
    --cc=baolu.lu@linux.intel.com \
    --cc=bhelgaas@google.com \
    --cc=chris.lu@mediatek.com \
    --cc=corbet@lwn.net \
    --cc=cornelia.huck@de.ibm.com \
    --cc=dakr@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=dwmw2@infradead.org \
    --cc=edumazet@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=gregkh@suse.de \
    --cc=hkallweit1@gmail.com \
    --cc=iommu@lists.linux.dev \
    --cc=jeff@garzik.org \
    --cc=jgg@ziepe.ca \
    --cc=johan@kernel.org \
    --cc=johannes@sipsolutions.net \
    --cc=joro@8bytes.org \
    --cc=kuba@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=luiz.dentz@gmail.com \
    --cc=luiz.von.dentz@intel.com \
    --cc=mans@mansr.com \
    --cc=marcel@holtmann.org \
    --cc=netdev@vger.kernel.org \
    --cc=nikhil.agarwal@amd.com \
    --cc=nipun.gupta@amd.com \
    --cc=pabeni@redhat.com \
    --cc=perex@perex.cz \
    --cc=pieter.jansen-van-vuuren@amd.com \
    --cc=rafael@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=robin.murphy@arm.com \
    --cc=skhan@linuxfoundation.org \
    --cc=stern@rowland.harvard.edu \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tiwai@suse.com \
    --cc=vasant.hegde@amd.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox