Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v5 00/33] driver core: Authorize device trust before driver binding
@ 2026-12-03 12:03 Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 01/33] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
                   ` (32 more replies)
  0 siblings, 33 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe, Takashi Iwai

Device trust policies need to precede driver setup, including manual
attachment, so userspace can authorize devices before they operate.

This series introduces device trust policy, authorization, and lifetime
commitment. Authorization does not reconfigure ATS capabilities or replace
IOMMU domains selected during discovery and further ATS and IOMMU integration
will follow in a separate series.

The table below tries to summarize the possible kernel command line
options and system behavior after that:

Command line                        Initial behavior
----------------------------------  ---------------------------------------
No device_trust options             General: existing driver binding.
                                    PCI: existing ACPI/topology
                                      restrictions.
                                    USB: existing authorized sysfs flow.

device_trust.default=bus            General: use each bus's initial policy.
                                    PCI: keep the platform classification
                                      and its restrictions; device rules
                                      can replace it.
                                    USB: devices and interfaces denied by
                                      USB defaults wait for authorization
                                      before binding.

device_trust.default=blocked        General: block driver binding until
                                      authorization.
                                    PCI: restrict every device from
                                      discovery; authorization does not
                                      revisit domain and ATS decisions.
                                    USB: leave devices and interfaces
                                      unbound until authorized.

device_trust.default=adversarial    General: allow adversarial binding.
                                    PCI: restrict every device as
                                      adversarial.
                                    USB: allow adversarial binding,
                                      overriding USB authorization
                                      defaults.

device_trust.default=unrestricted   General: allow unrestricted binding;
                                      freeze trust policy changes.
                                    PCI: replace the platform
                                      classification; removable devices
                                      lose their DMA restrictions.
                                    USB: authorize devices and interfaces
                                      regardless of USB defaults.

device_trust=...                    General: matching rules select policy;
or device_trust.driver=...            unmatched devices stay blocked.
without an explicit default         PCI: rules replace the platform
                                      classification; a device rule of
                                      bus resolves to unrestricted.
                                    USB: matching rules select device or
                                      interface policy.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
Changes in v5:
- When several drivers match a device, reject a later candidate whose
  device_trust.driver= rule conflicts with the policy fixed by the first
  candidate's rule or commitment, instead of binding it against its rule.
- Add KUnit cases and documentation for multiple candidate drivers.
- Shorten device-trust.rst.
- Rename the device trust policy default to bus everywhere.
- Link to v4: https://patch.msgid.link/20261124-device-trust-core-v4-0-037eb0d17b1f@nvidia.com

Changes in v4:
- Documentation, commit messages e.t.c
- Don't fail BT device initialization if WiFI USB claim fails/
- Rename "adversary" to "adversarial" as was agreed at the beginning.
- Added new patch to drain usbfs during disconnect.
- Added another new patch to reject usbfs connect requests for
  unregistered interfaces.
- Link to v3: https://patch.msgid.link/20261118-device-trust-core-v3-0-d655875c4bfb@nvidia.com

Changes in v3:
- Rewrote all commit messages
- Split USB patches to smaller chunks
- Introduced special device attach function which works only in
  untrusted mode
- Remove the special PHY -EBUSY IRQ handling.
- Clarify that further ATS and IOMMU integration follows separately.
- Link to v2: https://patch.msgid.link/20261109-device-trust-core-v2-0-3b12d9a9f861@nvidia.com

Changes in v2:
- Replace the serio and PHY attachment conversions with
  device_driver_attach_unrestricted(), restricted to device_trust.default=unrestricted.
- Add USB authorization on top of device trust: export
  device_trust_set_policy() and convert the USB authorized attributes.
- Documentation and commit messages fixes
- Small USB patches rewrote and split
- Link to v1: https://patch.msgid.link/20261105-device-trust-core-v1-0-1e536825b4eb@nvidia.com

---
Leon Romanovsky (33):
      wifi: mac80211_hwsim: lock device for manual driver binding
      Bluetooth: btusb: Fix MediaTek ISO claim locking
      USB: Lock interfaces during manual driver binding
      USB: serial: Register USB drivers with their owning module
      USB: Reject claims of DT-disabled interfaces
      USB: Reject usbfs connect requests for unregistered interfaces
      USB: Release interface claims according to binding state
      USB: Reject claims after interface registration failure
      ALSA: usb-audio: Release interface claims when probe fails
      USB: Pin modules for unregistered interface claims
      USB: Add helpers to release unpublished interface claims
      USB: Unwind interface claims after registration or binding failure
      PNP: Attach card devices through the driver core
      cdx: Propagate DMA configuration errors
      driver core: Report deferred probes through sysfs drivers_probe
      driver core: Add a compatibility API for manual driver attachment
      Input: gameport - use the compatibility driver attachment API
      Input: serio - use the compatibility driver attachment API
      net: phy: Use the compatibility driver attachment API
      USB: Drain usbfs when a device is unregistered
      driver core: Introduce a generic device trust level
      driver core: Expose device trust authorization interfaces
      driver core: Accept device trust policy on the kernel command line
      driver core: Accept device trust policy by driver name
      driver core: Expose committed device trust level
      driver core: Cover trust authorization and binding with KUnit
      driver core: Document trust admission and initrd authorization
      PCI: Use device trust for platform restrictions
      iommu: Use device trust for DMA restrictions
      driver core: Allow driver attachment with the device lock held
      USB: Retain the result of configuration requests
      USB: Authorize interfaces through device trust
      USB: Authorize devices through device trust

 Documentation/ABI/testing/sysfs-bus-drivers-probe  |   14 +
 Documentation/ABI/testing/sysfs-bus-usb            |   18 +
 Documentation/ABI/testing/sysfs-devices-trust      |   37 +
 Documentation/admin-guide/kernel-parameters.txt    |   37 +
 .../driver-api/driver-model/device-trust.rst       |  266 ++
 Documentation/driver-api/driver-model/index.rst    |    1 +
 Documentation/usb/authorization.rst                |   37 +
 MAINTAINERS                                        |    2 +
 drivers/base/Makefile                              |    2 +-
 drivers/base/base.h                                |   58 +
 drivers/base/bus.c                                 |    9 +-
 drivers/base/core.c                                |   52 +-
 drivers/base/dd.c                                  |  192 +-
 drivers/base/test/Makefile                         |    1 +
 drivers/base/test/device-trust-test.c              | 2609 ++++++++++++++++++++
 drivers/base/trust.c                               |  543 ++++
 drivers/bluetooth/btusb_main.c                     |   55 +-
 drivers/cdx/cdx.c                                  |    2 +-
 drivers/input/gameport/gameport.c                  |   22 +-
 drivers/input/serio/serio.c                        |   29 +-
 drivers/iommu/amd/iommu.c                          |    3 +-
 drivers/iommu/dma-iommu.c                          |   14 +-
 drivers/iommu/intel/iommu.c                        |   14 +-
 drivers/iommu/iommu.c                              |    9 +-
 drivers/net/phy/phy_device.c                       |   28 +-
 drivers/net/wireless/virtual/mac80211_hwsim_main.c |    2 +
 drivers/pci/ats.c                                  |    4 +-
 drivers/pci/pci.c                                  |    5 +-
 drivers/pci/probe.c                                |   18 +-
 drivers/pci/quirks.c                               |    7 +-
 drivers/pnp/card.c                                 |   12 +-
 drivers/usb/core/devio.c                           |   12 +-
 drivers/usb/core/driver.c                          |  118 +-
 drivers/usb/core/generic.c                         |    2 +-
 drivers/usb/core/hub.c                             |   42 +-
 drivers/usb/core/message.c                         |  104 +-
 drivers/usb/core/sysfs.c                           |    9 +-
 drivers/usb/core/trace.h                           |    2 +-
 drivers/usb/core/usb.c                             |    6 +
 drivers/usb/core/usb.h                             |    7 +-
 drivers/usb/serial/usb-serial.c                    |    4 +-
 include/linux/device.h                             |   21 +
 include/linux/device/bus.h                         |   10 +
 include/linux/device/trust.h                       |   54 +
 include/linux/pci.h                                |   18 +-
 include/linux/usb.h                                |   25 +-
 include/linux/usb/serial.h                         |    4 +-
 sound/usb/card.c                                   |    7 +
 48 files changed, 4331 insertions(+), 216 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git for-next
change-id: 20260915-device-trust-core-12934e618489

Best regards,
--  
Leon Romanovsky <leonro@nvidia.com>


^ permalink raw reply	[flat|nested] 34+ messages in thread

* [PATCH v5 01/33] wifi: mac80211_hwsim: lock device for manual driver binding
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 02/33] Bluetooth: btusb: Fix MediaTek ISO claim locking Leon Romanovsky
                   ` (31 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

The documented API contract for device_bind_driver() requires the device
lock. mac80211_hwsim uses it for a newly created class device with no bus,
so no automatic driver-core path can bind the device concurrently and the
missing lock was safe.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/net/wireless/virtual/mac80211_hwsim_main.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/wireless/virtual/mac80211_hwsim_main.c b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
index e53714b81ec2..b796da3dd54b 100644
--- a/drivers/net/wireless/virtual/mac80211_hwsim_main.c
+++ b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
@@ -5769,7 +5769,9 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,
 		goto failed_drvdata;
 	}
 	data->dev->driver = &mac80211_hwsim_driver.driver;
+	device_lock(data->dev);
 	err = device_bind_driver(data->dev);
+	device_unlock(data->dev);
 	if (err != 0) {
 		pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
 		       err);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 02/33] Bluetooth: btusb: Fix MediaTek ISO claim locking
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 01/33] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 03/33] USB: Lock interfaces during manual driver binding Leon Romanovsky
                   ` (30 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

MediaTek ISO claims are performed from HCI setup work outside USB probe.
Locking only the interface does not protect the claim against teardown
of the parent device. Unconditionally waiting for the parent lock can
deadlock with disconnect while it drains the setup work.

Resume the interface before acquiring the parent lock and use bounded
retries to keep the interface locked while claiming it. Propagate resume
and lock failures to setup, since controller setup requires the parent
device. Keep a failed claim non-fatal: MT7663 and MT7668 leave interface
2 to Wi-Fi.

Fixes: ceac1cb0259d ("Bluetooth: btusb: mediatek: add ISO data transmission functions")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/bluetooth/btusb_main.c | 53 +++++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 16 deletions(-)

diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 92a78a7e6039..128ec6f7d31f 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2993,40 +2993,54 @@ static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
 	return hci_recv_frame(hdev, skb);
 }
 
-static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
+#define BTUSB_MTK_ISO_LOCK_TRIES	5
+static int btusb_mtk_claim_iso_intf(struct btusb_data *data)
 {
 	struct btmtk_data *btmtk_data;
-	int err;
+	int err, i;
 
 	if (!data->hdev)
-		return;
+		return 0;
 
 	btmtk_data = hci_get_priv(data->hdev);
 	if (!btmtk_data)
-		return;
+		return 0;
 
 	if (!btmtk_data->isopkt_intf) {
 		bt_dev_err(data->hdev, "Can't claim NULL iso interface");
-		return;
+		return 0;
 	}
 
-	/*
-	 * The function usb_driver_claim_interface() is documented to need
-	 * locks held if it's not called from a probe routine. The code here
-	 * is called from the hci_power_on workqueue, so grab the lock.
-	 */
+	err = usb_autopm_get_interface(data->intf);
+	if (err)
+		return err;
+
+	/* Disconnect may hold the parent lock while waiting for this work. */
+	for (i = 0; i < BTUSB_MTK_ISO_LOCK_TRIES; i++) {
+		err = usb_lock_device_for_reset(data->udev, data->intf);
+		if (err != -EBUSY)
+			break;
+	}
+	if (err)
+		goto put_interface;
+
 	device_lock(&btmtk_data->isopkt_intf->dev);
 	err = usb_driver_claim_interface(&btusb_driver,
 					 btmtk_data->isopkt_intf, data);
 	device_unlock(&btmtk_data->isopkt_intf->dev);
-	if (err < 0) {
-		btmtk_data->isopkt_intf = NULL;
+	if (err) {
+		/* MT7663 and MT7668 leave interface 2 to Wi-Fi. */
 		bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
-		return;
+		err = 0;
+	} else {
+		init_usb_anchor(&btmtk_data->isopkt_anchor);
+		set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
 	}
 
-	set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
-	init_usb_anchor(&btmtk_data->isopkt_anchor);
+	usb_unlock_device(data->udev);
+put_interface:
+	usb_autopm_put_interface(data->intf);
+	return err;
 }
 
 static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
@@ -3169,7 +3183,14 @@ static int btusb_mtk_setup(struct hci_dev *hdev)
 	/* Claim ISO data interface and endpoint */
 	if (!test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
 		btmtk_data->isopkt_intf = usb_ifnum_to_if(data->udev, MTK_ISO_IFNUM);
-		btusb_mtk_claim_iso_intf(data);
+		err = btusb_mtk_claim_iso_intf(data);
+		if (!test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags))
+			btmtk_data->isopkt_intf = NULL;
+		if (err) {
+			bt_dev_err(hdev, "Failed to resume or lock iso interface: %d",
+				   err);
+			return err;
+		}
 	}
 
 	err = btmtk_usb_setup(hdev);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 03/33] USB: Lock interfaces during manual driver binding
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 01/33] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 02/33] Bluetooth: btusb: Fix MediaTek ISO claim locking Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 04/33] USB: serial: Register USB drivers with their owning module Leon Romanovsky
                   ` (29 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Driver core requires the claimed interface's device lock during binding.
The parent USB device lock alone does not satisfy this requirement.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/bluetooth/btusb_main.c |  2 --
 drivers/usb/core/driver.c      | 15 ++++++++++-----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 128ec6f7d31f..37f36434c6e9 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -3024,10 +3024,8 @@ static int btusb_mtk_claim_iso_intf(struct btusb_data *data)
 	if (err)
 		goto put_interface;
 
-	device_lock(&btmtk_data->isopkt_intf->dev);
 	err = usb_driver_claim_interface(&btusb_driver,
 					 btmtk_data->isopkt_intf, data);
-	device_unlock(&btmtk_data->isopkt_intf->dev);
 	if (err) {
 		/* MT7663 and MT7668 leave interface 2 to Wi-Fi. */
 		bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d3a61ebc6afb..e1de348cc0ca 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -540,9 +540,8 @@ static void usb_shutdown_interface(struct device *dev)
  * No device driver should directly modify internal usb_interface or
  * usb_device structure members.
  *
- * Callers must own the device lock, so driver probe() entries don't need
- * extra locking, but other call contexts may need to explicitly claim that
- * lock.
+ * Callers must hold the parent USB device lock, but not the claimed
+ * interface's device lock.
  *
  * Return: 0 on success.
  */
@@ -559,9 +558,13 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 	if (dev->driver)
 		return -EBUSY;
 
+	device_lock(dev);
+
 	/* reject claim if interface is not authorized */
-	if (!iface->authorized)
-		return -ENODEV;
+	if (!iface->authorized) {
+		retval = -ENODEV;
+		goto unlock;
+	}
 
 	dev->driver = &driver->driver;
 	usb_set_intfdata(iface, data);
@@ -602,6 +605,8 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 		pm_runtime_set_suspended(dev);
 	}
 
+unlock:
+	device_unlock(dev);
 	return retval;
 }
 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 04/33] USB: serial: Register USB drivers with their owning module
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (2 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 03/33] USB: Lock interfaces during manual driver binding Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 05/33] USB: Reject claims of DT-disabled interfaces Leon Romanovsky
                   ` (28 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

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@nvidia.com>
---
 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 4a19621f08ba..ccbd0875339a 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 5866f19d3c7b..fcfd0e26e0ff 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


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 05/33] USB: Reject claims of DT-disabled interfaces
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (3 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 04/33] USB: serial: Register USB drivers with their owning module Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 06/33] USB: Reject usbfs connect requests for unregistered interfaces Leon Romanovsky
                   ` (27 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

USB core skips registration of DT-disabled interfaces but keeps their
objects available to sibling drivers. A CDC-ACM control interface can
therefore claim a disabled data interface: manual claims check USB
authorization without checking Device Tree availability.

Fixes: 01fdf179f4b0 ("usb: core: skip interfaces disabled in devicetree")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/driver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index e1de348cc0ca..735ae3310b45 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -28,6 +28,7 @@
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/export.h>
+#include <linux/of.h>
 #include <linux/usb.h>
 #include <linux/usb/quirks.h>
 #include <linux/usb/hcd.h>
@@ -560,8 +561,8 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 
 	device_lock(dev);
 
-	/* reject claim if interface is not authorized */
-	if (!iface->authorized) {
+	if (!iface->authorized ||
+	    (dev->of_node && !of_device_is_available(dev->of_node))) {
 		retval = -ENODEV;
 		goto unlock;
 	}

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 06/33] USB: Reject usbfs connect requests for unregistered interfaces
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (4 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 05/33] USB: Reject claims of DT-disabled interfaces Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 07/33] USB: Release interface claims according to binding state Leon Romanovsky
                   ` (26 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

USB core does not register DT-disabled interfaces, and an interface whose
registration failed stays in the active configuration. USBDEVFS_CONNECT
still asks driver core to attach a driver to such an interface:

proc_ioctl()
  device_attach()
    __device_attach()

The interface has no driver-core private data, so the request
dereferences a NULL pointer.

Return -ENODEV for an unregistered interface. An interface claimed by a
sibling driver keeps reporting -EBUSY.

Fixes: ef0ff68351be ("driver core: Probe devices asynchronously instead of the driver")
Fixes: 654888327e9f ("driver core: Avoid binding drivers to dead devices")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/devio.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 101cb9425480..1491ba015bcd 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -2366,10 +2366,12 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
 
 	/* let kernel drivers try to (re)bind to the interface */
 	case USBDEVFS_CONNECT:
-		if (!intf->dev.driver)
-			retval = device_attach(&intf->dev);
-		else
+		if (intf->dev.driver)
 			retval = -EBUSY;
+		else if (!device_is_registered(&intf->dev))
+			retval = -ENODEV;
+		else
+			retval = device_attach(&intf->dev);
 		break;
 
 	/* talk directly to the interface's driver */

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 07/33] USB: Release interface claims according to binding state
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (5 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 06/33] USB: Reject usbfs connect requests for unregistered interfaces Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 08/33] USB: Reject claims after interface registration failure Leon Romanovsky
                   ` (25 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

A driver such as CDC-ACM can claim a sibling interface before it is
registered. Disabling USB bus autoprobing between the claim and
registration leaves dev->driver set without adding the interface to the
driver's bound-device list.

Releasing that interface through driver core then attempts to remove an
unattached list node, causing a NULL pointer dereference.

Fixes: b8c5cec23d5c ("Driver core: udev triggered device-<>driver binding")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/driver.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 735ae3310b45..d902708416d0 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -612,6 +612,15 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 }
 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
 
+static void usb_unbind_claim(struct usb_interface *iface)
+{
+	struct device *dev = &iface->dev;
+
+	usb_unbind_interface(dev);
+	dev_set_drvdata(dev, NULL);
+	dev->driver = NULL;
+}
+
 /**
  * usb_driver_release_interface - unbind a driver from an interface
  * @driver: the driver to be unbound
@@ -631,27 +640,25 @@ void usb_driver_release_interface(struct usb_driver *driver,
 {
 	struct device *dev = &iface->dev;
 
-	/* this should never happen, don't release something that's not ours */
-	if (!dev->driver || dev->driver != &driver->driver)
-		return;
-
-	/* don't release from within disconnect() */
+	/* Avoid recursive locking from disconnect(). */
 	if (iface->condition != USB_INTERFACE_BOUND)
 		return;
+
+	device_lock(dev);
+	if (!dev->driver || dev->driver != &driver->driver ||
+	    iface->condition != USB_INTERFACE_BOUND)
+		goto unlock;
 	iface->condition = USB_INTERFACE_UNBINDING;
 
-	/* Release via the driver core only if the interface
-	 * has already been registered
-	 */
-	if (device_is_registered(dev)) {
-		device_release_driver(dev);
-	} else {
-		device_lock(dev);
-		usb_unbind_interface(dev);
-		dev_set_drvdata(dev, NULL);
-		dev->driver = NULL;
+	if (device_is_bound(dev)) {
 		device_unlock(dev);
+		device_release_driver(dev);
+		return;
 	}
+
+	usb_unbind_claim(iface);
+unlock:
+	device_unlock(dev);
 }
 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 08/33] USB: Reject claims after interface registration failure
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (6 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 07/33] USB: Release interface claims according to binding state Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 09/33] ALSA: usb-audio: Release interface claims when probe fails Leon Romanovsky
                   ` (24 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

After device_add() fails, the interface remains in the active
configuration, so usbfs can still claim it. Configuration removal skips
unregistered interfaces and never calls the claimant's disconnect
callback.

A driver can claim an interface before USB core registers it. If
registration fails, reject further claims because USB core will not retry
registration of that object. Changing the device configuration creates
new interface objects.

Fixes: 86d30741e480 ("[PATCH] USB: Usbcore: Don't try to delete unregistered interfaces")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/driver.c  |  2 +-
 drivers/usb/core/message.c | 12 ++++++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d902708416d0..a50403820285 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -561,7 +561,7 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 
 	device_lock(dev);
 
-	if (!iface->authorized ||
+	if (!iface->authorized || iface->unregistering ||
 	    (dev->of_node && !of_device_is_available(dev->of_node))) {
 		retval = -ENODEV;
 		goto unlock;
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index a738d03c23ea..a645858b25d9 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1468,8 +1468,13 @@ void usb_disable_device(struct usb_device *dev, int skip_ep0)
 		 * bandwidth_mutex, we have to mark all the interfaces
 		 * before unregistering any of them.
 		 */
-		for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
-			dev->actconfig->interface[i]->unregistering = 1;
+		for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
+			struct usb_interface *intf = dev->actconfig->interface[i];
+
+			device_lock(&intf->dev);
+			intf->unregistering = 1;
+			device_unlock(&intf->dev);
+		}
 
 		for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
 			struct usb_interface	*interface;
@@ -2279,6 +2284,9 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 		device_enable_async_suspend(&intf->dev);
 		ret = device_add(&intf->dev);
 		if (ret != 0) {
+			device_lock(&intf->dev);
+			intf->unregistering = 1;
+			device_unlock(&intf->dev);
 			dev_err(&dev->dev, "device_add(%s) --> %d\n",
 				dev_name(&intf->dev), ret);
 			continue;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 09/33] ALSA: usb-audio: Release interface claims when probe fails
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (7 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 08/33] USB: Reject claims after interface registration failure Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 10/33] USB: Pin modules for unregistered interface claims Leon Romanovsky
                   ` (23 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe, Takashi Iwai

From: Leon Romanovsky <leonro@nvidia.com>

If the first control-interface probe fails after claiming streaming or
MIDI interfaces, freeing the card leaves those claims behind. Module
unload can then race with registration of the claimed interfaces and
leave dangling driver pointers.

Release auxiliary claims when discarding a newly created card. Preserve
them when a failed secondary probe leaves an existing card in use.

Fixes: 9ad3d6ccf5ee ("[PATCH] USB: Remove USB private semaphore")
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 sound/usb/card.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/sound/usb/card.c b/sound/usb/card.c
index 59979b793fe9..56ebdd61f72e 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -1090,6 +1090,13 @@ static int usb_audio_probe(struct usb_interface *intf,
 		atomic_dec(&chip->active);
 		if (!chip->num_interfaces) {
 			usb_audio_disconnect_components(chip);
+			for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
+				struct usb_interface *iface = dev->actconfig->interface[i];
+
+				if (iface->dev.driver == &usb_audio_driver.driver &&
+				    usb_get_intfdata(iface) == USB_AUDIO_IFACE_UNUSED)
+					usb_driver_release_interface(&usb_audio_driver, iface);
+			}
 			snd_card_free(chip->card);
 		}
 	}

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 10/33] USB: Pin modules for unregistered interface claims
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (8 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 09/33] ALSA: usb-audio: Release interface claims when probe fails Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 11/33] USB: Add helpers to release unpublished " Leon Romanovsky
                   ` (22 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

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 a50403820285..a03ea60edc50 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 a645858b25d9..cfb4cbdbe76f 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,7 +2284,11 @@ 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);
+		module_put(claim_owner);
 		if (ret != 0) {
 			device_lock(&intf->dev);
 			intf->unregistering = 1;
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index a9b37aeb515b..dfa48f43046d 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 825f03b29067..a5e706f67f33 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


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 11/33] USB: Add helpers to release unpublished interface claims
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (9 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 10/33] USB: Pin modules for unregistered interface claims Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 12/33] USB: Unwind interface claims after registration or binding failure Leon Romanovsky
                   ` (21 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

An early USB claim can require disconnect even though driver core never
bound the interface. Binding failure may also have cleared the claimant
pointer.

Add release helpers so registration rollback and configuration removal
can unwind these claims through USB.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/driver.c | 26 ++++++++++++++++++++++++++
 drivers/usb/core/usb.h    |  3 +++
 2 files changed, 29 insertions(+)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index a03ea60edc50..9dd022c1ca15 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -637,6 +637,14 @@ static void usb_unbind_claim(struct usb_interface *iface)
 	usb_put_claim_owner(iface);
 }
 
+void usb_release_unregistered_interface(struct usb_interface *iface)
+{
+	device_lock(&iface->dev);
+	if (iface->condition == USB_INTERFACE_BOUND)
+		usb_unbind_claim(iface);
+	device_unlock(&iface->dev);
+}
+
 /**
  * usb_driver_release_interface - unbind a driver from an interface
  * @driver: the driver to be unbound
@@ -679,6 +687,24 @@ void usb_driver_release_interface(struct usb_driver *driver,
 }
 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
 
+bool usb_driver_release_refused_claim(struct usb_interface *iface,
+				      struct device_driver *claimant)
+{
+	struct device *dev = &iface->dev;
+
+	device_lock(dev);
+	if (iface->condition != USB_INTERFACE_BOUND || device_is_bound(dev)) {
+		device_unlock(dev);
+		return false;
+	}
+	dev->driver = claimant;
+	usb_unbind_interface(dev);
+	dev_set_drvdata(dev, NULL);
+	dev->driver = NULL;
+	device_unlock(dev);
+	return true;
+}
+
 /* returns 0 if no match, 1 if match */
 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
 {
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index dfa48f43046d..1b2de58fd6ce 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -50,6 +50,9 @@ 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);
+void usb_release_unregistered_interface(struct usb_interface *iface);
+bool usb_driver_release_refused_claim(struct usb_interface *iface,
+				      struct device_driver *claimant);
 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);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 12/33] USB: Unwind interface claims after registration or binding failure
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (10 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 11/33] USB: Add helpers to release unpublished " Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 13/33] PNP: Attach card devices through the driver core Leon Romanovsky
                   ` (20 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

A successful USB-audio control probe can claim unregistered playback and
capture interfaces. If playback registration fails, releasing its claim
alone leaves the card active, its disconnect callback returns immediately.

usb_set_configuration()
  device_add(control)
    usb_audio_probe()
      snd_usb_create_streams()
        snd_usb_create_stream(playback)
          usb_driver_claim_interface(playback)
        snd_usb_create_stream(capture)
          usb_driver_claim_interface(capture)
  device_add(playback) [fails]

Configuration removal skips unregistered claims. Binding failure can
clear dev->driver without disconnecting the claimant, and disabled
autoprobe leaves the claim unbound.

Release every claim that driver core left unbound, remove the
configuration if sibling claims remain, and disconnect unpublished
claims before freeing interfaces or dropping the module reference. A
bound claim is on the driver's device list, so unload waits for its
disconnect.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 86d30741e480f ("[PATCH] USB: Usbcore: Don't try to delete unregistered interfaces")
Fixes: c6a46696f97ff ("driver core: don't fail attaching the device if it cannot be bound")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/message.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index cfb4cbdbe76f..e7a23e406f8a 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1489,6 +1489,9 @@ void usb_disable_device(struct usb_device *dev, int skip_ep0)
 			device_del(&interface->dev);
 		}
 
+		for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
+			usb_release_unregistered_interface(dev->actconfig->interface[i]);
+
 		/* Now that the interfaces are unbound, nobody should
 		 * try to access them.
 		 */
@@ -2020,6 +2023,25 @@ int usb_set_wireless_status(struct usb_interface *iface,
 }
 EXPORT_SYMBOL_GPL(usb_set_wireless_status);
 
+static bool usb_has_claimant(struct usb_device *dev,
+			     const struct device_driver *claimant)
+{
+	struct usb_host_config *config = dev->actconfig;
+	int i;
+
+	for (i = 0; i < config->desc.bNumInterfaces; i++) {
+		struct device *intf = &config->interface[i]->dev;
+		bool claimed;
+
+		device_lock(intf);
+		claimed = intf->driver == claimant;
+		device_unlock(intf);
+		if (claimed)
+			return true;
+	}
+	return false;
+}
+
 /*
  * usb_set_configuration - Makes a particular device setting be current
  * @dev: the device whose configuration is being updated
@@ -2270,6 +2292,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 device_driver *claimant;
 		struct module *claim_owner;
 
 		if (intf->dev.of_node &&
@@ -2285,10 +2308,26 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 			intf->cur_altsetting->desc.bInterfaceNumber);
 		device_enable_async_suspend(&intf->dev);
 		device_lock(&intf->dev);
+		claimant = intf->dev.driver;
 		claim_owner = xchg(&intf->claim_owner, NULL);
 		device_unlock(&intf->dev);
 		ret = device_add(&intf->dev);
-		module_put(claim_owner);
+		if (claimant &&
+		    usb_driver_release_refused_claim(intf, claimant)) {
+			bool unconfigure = usb_has_claimant(dev, claimant);
+
+			get_device(&intf->dev);
+			if (unconfigure)
+				usb_set_configuration(dev, -1);
+			module_put(claim_owner);
+			put_device(&intf->dev);
+			if (unconfigure) {
+				usb_autosuspend_device(dev);
+				return ret ? ret : -ENODEV;
+			}
+		} else {
+			module_put(claim_owner);
+		}
 		if (ret != 0) {
 			device_lock(&intf->dev);
 			intf->unregistering = 1;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 13/33] PNP: Attach card devices through the driver core
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (11 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 12/33] USB: Unwind interface claims after registration or binding failure Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 14/33] cdx: Propagate DMA configuration errors Leon Romanovsky
                   ` (19 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

pnp_request_card_device() calls the PnP bus probe directly, bypassing
the driver core's supplier checks and power management setup. If
device_bind_driver() then fails to create the sysfs links, the device
remains attached and may stay active, but the caller receives NULL and
cannot release it.

Use device_driver_attach() for synchronous probing with the driver
core's setup and error handling. Preserve the card link until binding
completes, so probe cleanup and CONFIG_DEBUG_TEST_DRIVER_REMOVE cannot
clear or free a link still owned by the enclosing card probe.

Fixes: bfc7ee207078 ("[PATCH] PNP: handle sysfs errors")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/pnp/card.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/pnp/card.c b/drivers/pnp/card.c
index df5e3d3cbf6c..9aaaa03d5ffa 100644
--- a/drivers/pnp/card.c
+++ b/drivers/pnp/card.c
@@ -52,13 +52,17 @@ static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
 
 static void card_remove(struct pnp_dev *dev)
 {
-	dev->card_link = NULL;
+	if (device_is_bound(&dev->dev))
+		dev->card_link = NULL;
 }
 
 static void card_remove_first(struct pnp_dev *dev)
 {
 	struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
 
+	/* The card probe still owns its link until this device is bound. */
+	if (!device_is_bound(&dev->dev))
+		return;
 	if (!dev->card || !drv)
 		return;
 	if (drv->remove)
@@ -331,16 +335,12 @@ struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
 
 found:
 	dev->card_link = clink;
-	dev->dev.driver = &drv->link.driver;
-	if (pnp_bus_type.probe(&dev->dev))
-		goto err_out;
-	if (device_bind_driver(&dev->dev))
+	if (device_driver_attach(&drv->link.driver, &dev->dev))
 		goto err_out;
 
 	return dev;
 
 err_out:
-	dev->dev.driver = NULL;
 	dev->card_link = NULL;
 	return NULL;
 }

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 14/33] cdx: Propagate DMA configuration errors
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (12 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 13/33] PNP: Attach card devices through the driver core Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 15/33] driver core: Report deferred probes through sysfs drivers_probe Leon Romanovsky
                   ` (18 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

CDX reports successful DMA configuration when firmware setup defers or
default-domain acquisition fails. Driver probing can then proceed without
the required DMA setup or IOMMU ownership.

Return the configuration result so driver core stops or defers probing
instead of allowing a driver to use a rejected domain.

Fixes: 2959ab247061 ("cdx: add the cdx bus driver")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/cdx/cdx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c
index a118a89a7779..0a030bf52269 100644
--- a/drivers/cdx/cdx.c
+++ b/drivers/cdx/cdx.c
@@ -367,7 +367,7 @@ static int cdx_dma_configure(struct device *dev)
 			arch_teardown_dma_ops(dev);
 	}
 
-	return 0;
+	return ret;
 }
 
 static void cdx_dma_cleanup(struct device *dev)

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 15/33] driver core: Report deferred probes through sysfs drivers_probe
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (13 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 14/33] cdx: Propagate DMA configuration errors Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 16/33] driver core: Add a compatibility API for manual driver attachment Leon Romanovsky
                   ` (17 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

commit b8c5cec23d5c ("Driver core: udev triggered device-<>driver
binding") introduced the concept of "Systems who want to control the
driver binding with udev" can use the drivers_probe sysfs to trigger a
driver bind based on the kernel's match list.

If attaching the driver is deferred before its probe callback runs,
userspace sees EINVAL and cannot tell it should try again, maybe after
loading other drivers.

Map that deferral to EAGAIN and add missed documentation for the
drivers_probe sysfs. A deferral requested by the probe callback itself
is still not reported.

Fixes: b8c5cec23d5c ("Driver core: udev triggered device-<>driver binding")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-bus-drivers-probe | 14 ++++++++++++++
 MAINTAINERS                                       |  1 +
 drivers/base/bus.c                                |  9 +++++++--
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-drivers-probe b/Documentation/ABI/testing/sysfs-bus-drivers-probe
new file mode 100644
index 000000000000..ed8828e55d6b
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-drivers-probe
@@ -0,0 +1,14 @@
+What:		/sys/bus/.../drivers_probe
+Date:		February 2007
+Contact:	driver-core@lists.linux.dev
+Description:
+		(WO) Writing a device name requests driver attachment on this
+		bus, even when drivers_autoprobe is disabled. A successful
+		write does not guarantee that a driver bound. The driver link
+		can appear before probe completes; a bind uevent signals
+		successful binding.
+
+		EAGAIN means attachment was deferred before the driver's probe
+		callback, for example while a supplier or the device itself
+		is not ready. ENODEV means the device was not found. Other
+		attachment errors return EINVAL.
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..b726344362b4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8149,6 +8149,7 @@ M:	Johan Hovold <johan@kernel.org>
 L:	driver-core@lists.linux.dev
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
+F:	Documentation/ABI/testing/sysfs-bus-drivers-probe
 F:	Documentation/core-api/kobject.rst
 F:	Documentation/driver-api/driver-model/
 F:	drivers/base/
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 396097fdb316..ecff32c3704b 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -310,13 +310,18 @@ static ssize_t drivers_probe_store(const struct bus_type *bus,
 				   const char *buf, size_t count)
 {
 	struct device *dev;
-	int err = -EINVAL;
+	int err;
 
 	dev = bus_find_device_by_name(bus, NULL, buf);
 	if (!dev)
 		return -ENODEV;
-	if (bus_rescan_devices_helper(dev, NULL) == 0)
+	err = bus_rescan_devices_helper(dev, NULL);
+	if (!err)
 		err = count;
+	else if (err == -EPROBE_DEFER)
+		err = -EAGAIN;
+	else
+		err = -EINVAL;
 	put_device(dev);
 	return err;
 }

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 16/33] driver core: Add a compatibility API for manual driver attachment
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (14 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 15/33] driver core: Report deferred probes through sysfs drivers_probe Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 17/33] Input: gameport - use the compatibility driver attachment API Leon Romanovsky
                   ` (16 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Manual attachment can race normal probing and removal, releasing another
probe's resources. Callers cannot always acquire the device mutex under
subsystem locks.

Add device_driver_attach_unrestricted() with a binding mutex shared by
attachment, removal and deletion. Trylock it to reject competing
transactions. Supplier-status reads must avoid sysfs lock inversion and
private-state access after registration failure.

This compatibility API must not be used in any new code: it prevents use
of the device trust authorization infrastructure. New code must use
device_driver_attach().

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/base/base.h    |   3 ++
 drivers/base/core.c    |  32 +++++++++++---
 drivers/base/dd.c      | 117 +++++++++++++++++++++++++++++++++++++++++++------
 include/linux/device.h |   2 +
 4 files changed, 134 insertions(+), 20 deletions(-)

diff --git a/drivers/base/base.h b/drivers/base/base.h
index 866b2beb6baa..61f49fc142f4 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -105,6 +105,7 @@ struct driver_private {
  *			   dev_err_probe() for later retrieval via debugfs
  * @device: pointer back to the struct device that this structure is
  *	    associated with.
+ * @driver_lock: serializes binding and removal, including compatibility attach
  * @dead: This device is currently either in the process of or has been
  *	  removed from the system. Any asynchronous events scheduled for this
  *	  device should exit without taking any action.
@@ -121,6 +122,8 @@ struct device_private {
 	const struct device_driver *async_driver;
 	char *deferred_probe_reason;
 	struct device *device;
+	/* Taken after the device mutex, except by compatibility attach. */
+	struct mutex driver_lock;
 	u8 dead:1;
 };
 #define to_device_private_parent(obj)	\
diff --git a/drivers/base/core.c b/drivers/base/core.c
index cbe05197d4a0..92d25bf4f863 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1315,13 +1315,23 @@ static ssize_t waiting_for_supplier_show(struct device *dev,
 					 const struct device_attribute *attr,
 					 char *buf)
 {
+	struct kernfs_node *kn;
+	ssize_t ret = -ENODEV;
 	bool val;
 
+	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
+	if (!kn)
+		return -ENODEV;
+
 	device_lock(dev);
-	scoped_guard(mutex, &fwnode_link_lock)
-		val = !!fwnode_links_check_suppliers(dev->fwnode);
+	if (dev_ready_to_probe(dev) && !dev->p->dead) {
+		scoped_guard(mutex, &fwnode_link_lock)
+			val = !!fwnode_links_check_suppliers(dev->fwnode);
+		ret = sysfs_emit(buf, "%u\n", val);
+	}
 	device_unlock(dev);
-	return sysfs_emit(buf, "%u\n", val);
+	sysfs_unbreak_active_protection(kn);
+	return ret;
 }
 static const DEVICE_ATTR_RO(waiting_for_supplier);
 
@@ -3609,6 +3619,9 @@ static int device_private_init(struct device *dev)
 	if (!dev->p)
 		return -ENOMEM;
 	dev->p->device = dev;
+	mutex_init(&dev->p->driver_lock);
+	/* Driver callbacks can bind or unbind child devices. */
+	lockdep_set_novalidate_class(&dev->p->driver_lock);
 	klist_init(&dev->p->klist_children, klist_children_get,
 		   klist_children_put);
 	INIT_LIST_HEAD(&dev->p->deferred_probe);
@@ -3889,6 +3902,8 @@ EXPORT_SYMBOL_GPL(put_device);
 
 bool kill_device(struct device *dev)
 {
+	bool killed = false;
+
 	/*
 	 * Require the device lock and set the "dead" flag to guarantee that
 	 * the update behavior is consistent with the other bitfields near
@@ -3898,10 +3913,13 @@ bool kill_device(struct device *dev)
 	 */
 	device_lock_assert(dev);
 
-	if (dev->p->dead)
-		return false;
-	dev->p->dead = true;
-	return true;
+	mutex_lock(&dev->p->driver_lock);
+	if (!dev->p->dead) {
+		dev->p->dead = true;
+		killed = true;
+	}
+	mutex_unlock(&dev->p->driver_lock);
+	return killed;
 }
 EXPORT_SYMBOL_GPL(kill_device);
 
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 52123251965d..6ac1486b9fbb 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -526,6 +526,21 @@ static void driver_sysfs_remove(struct device *dev)
 	}
 }
 
+static int __device_bind_driver(struct device *dev)
+{
+	int ret;
+
+	ret = driver_sysfs_add(dev);
+	if (ret) {
+		bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
+		return ret;
+	}
+
+	device_links_force_bind(dev);
+	driver_bound(dev);
+	return 0;
+}
+
 /**
  * device_bind_driver - bind a driver to one device.
  * @dev: device.
@@ -545,16 +560,85 @@ int device_bind_driver(struct device *dev)
 {
 	int ret;
 
-	ret = driver_sysfs_add(dev);
-	if (!ret) {
-		device_links_force_bind(dev);
-		driver_bound(dev);
+	mutex_lock(&dev->p->driver_lock);
+	ret = __device_bind_driver(dev);
+	mutex_unlock(&dev->p->driver_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(device_bind_driver);
+
+/**
+ * device_driver_attach_unrestricted - attach a driver using subsystem serialization
+ * @drv: driver to attach
+ * @dev: device to attach it to
+ *
+ * This compatibility API must not be used in any new code. It prevents
+ * devices from using the device trust authorization infrastructure.
+ * New code must use device_driver_attach().
+ *
+ * Callers must provide their subsystem's existing manual-binding
+ * serialization. This function does not acquire device or parent locks.
+ * Binding and removal are serialized separately; an overlapping transaction
+ * returns -EBUSY instead of waiting under the caller's subsystem locks.
+ * It calls the bus or driver probe callback and publishes the binding without
+ * performing the normal driver-core DMA, pinctrl or power-domain setup.
+ *
+ * Return: 0 on success, -EAGAIN if registration or probing must be retried,
+ * or another negative error before binding.
+ */
+int device_driver_attach_unrestricted(const struct device_driver *drv,
+				      struct device *dev)
+{
+	int ret;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
+	if (!mutex_trylock(&dev->p->driver_lock))
+		return -EBUSY;
+	if (dev->p->dead) {
+		ret = -ENODEV;
+		goto unlock;
 	}
+	if (!dev_ready_to_probe(dev)) {
+		ret = -EAGAIN;
+		goto unlock;
+	}
+	if (dev->driver) {
+		ret = -EBUSY;
+		goto unlock;
+	}
+
+	device_set_driver(dev, drv);
+	if (dev->bus->probe)
+		ret = dev->bus->probe(dev);
 	else
-		bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
+		ret = drv->probe(dev);
+	if (ret)
+		goto clear_driver;
+
+	ret = __device_bind_driver(dev);
+	if (ret)
+		goto bus_remove;
+
+	mutex_unlock(&dev->p->driver_lock);
+	return 0;
+
+bus_remove:
+	if (dev->bus->remove)
+		dev->bus->remove(dev);
+	else
+		drv->remove(dev);
+clear_driver:
+	devres_release_all(dev);
+	device_set_driver(dev, NULL);
+	dev_set_drvdata(dev, NULL);
+	if (ret == -EPROBE_DEFER)
+		ret = -EAGAIN;
+unlock:
+	mutex_unlock(&dev->p->driver_lock);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(device_bind_driver);
+EXPORT_SYMBOL_GPL(device_driver_attach_unrestricted);
 
 static atomic_t probe_count = ATOMIC_INIT(0);
 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
@@ -1035,15 +1119,15 @@ static int __device_attach_driver(struct device_driver *drv, void *_data)
  * @dev: Device we will update driver info for
  * @parent: Parent device. Needed if the device type requires parent lock
  *
- * This function will take the required locks for manipulating dev->drv.
- * Normally this will just be the @dev lock, but when called for a USB
- * interface, @parent lock will be held as well.
+ * Take the device lock and binding lock, together with the parent lock
+ * when the device type requires it.
  */
 static void __device_driver_lock(struct device *dev, struct device *parent)
 {
 	if (dev->type && dev->type->need_parent_lock && parent)
 		device_lock(parent);
 	device_lock(dev);
+	mutex_lock(&dev->p->driver_lock);
 }
 
 /*
@@ -1051,12 +1135,12 @@ static void __device_driver_lock(struct device *dev, struct device *parent)
  * @dev: Device we will update driver info for
  * @parent: Parent device. Needed if the device type requires parent lock
  *
- * This function will release the required locks for manipulating dev->drv.
- * Normally this will just be the @dev lock, but when called for a
- * USB interface, @parent lock will be released as well.
+ * Release the binding and device locks, together with the parent lock
+ * when the device type requires it.
  */
 static void __device_driver_unlock(struct device *dev, struct device *parent)
 {
+	mutex_unlock(&dev->p->driver_lock);
 	device_unlock(dev);
 	if (dev->type && dev->type->need_parent_lock && parent)
 		device_unlock(parent);
@@ -1104,6 +1188,7 @@ static int __device_attach(struct device *dev, bool allow_async)
 	bool async = false;
 
 	device_lock(dev);
+	mutex_lock(&dev->p->driver_lock);
 	if (dev->p->dead) {
 		goto out_unlock;
 	} else if (dev->driver) {
@@ -1111,7 +1196,7 @@ static int __device_attach(struct device *dev, bool allow_async)
 			ret = 1;
 			goto out_unlock;
 		}
-		ret = device_bind_driver(dev);
+		ret = __device_bind_driver(dev);
 		if (ret == 0)
 			ret = 1;
 		else {
@@ -1149,6 +1234,7 @@ static int __device_attach(struct device *dev, bool allow_async)
 			pm_runtime_put(dev->parent);
 	}
 out_unlock:
+	mutex_unlock(&dev->p->driver_lock);
 	device_unlock(dev);
 	if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
 		put_device(dev);
@@ -1285,11 +1371,13 @@ static int __driver_attach(struct device *dev, void *data)
 		 */
 		dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
 		device_lock(dev);
+		mutex_lock(&dev->p->driver_lock);
 		if (!dev->driver && !dev->p->async_driver) {
 			get_device(dev);
 			dev->p->async_driver = drv;
 			async = true;
 		}
+		mutex_unlock(&dev->p->driver_lock);
 		device_unlock(dev);
 		if (async)
 			async_schedule_dev(__driver_attach_async_helper, dev);
@@ -1374,6 +1462,9 @@ void device_release_driver_internal(struct device *dev,
 				    const struct device_driver *drv,
 				    struct device *parent)
 {
+	if (!dev->p)
+		return;
+
 	__device_driver_lock(dev, parent);
 
 	if (!drv || drv == dev->driver)
diff --git a/include/linux/device.h b/include/linux/device.h
index 429e39f647cf..5433b2609ab3 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1314,6 +1314,8 @@ static inline void *dev_get_platdata(const struct device *dev)
  */
 int __must_check device_driver_attach(const struct device_driver *drv,
 				      struct device *dev);
+int __must_check device_driver_attach_unrestricted(
+	const struct device_driver *drv, struct device *dev);
 int __must_check device_bind_driver(struct device *dev);
 void device_release_driver(struct device *dev);
 int  __must_check device_attach(struct device *dev);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 17/33] Input: gameport - use the compatibility driver attachment API
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (15 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 16/33] driver core: Add a compatibility API for manual driver attachment Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 18/33] Input: serio " Leon Romanovsky
                   ` (15 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Manual gameport binding calls the driver's connect callback without the
device lock, allowing it to race with generic sysfs probing. Normal
probing would run CONFIG_DEBUG_TEST_DRIVER_REMOVE under gameport_mutex;
a disconnect callback that unregisters a child port would then deadlock
trying to acquire the same mutex.

Use device_driver_attach_unrestricted() to centralize binding and failure
cleanup without the debug removal cycle. Take the device lock with
device_trylock() to serialize attachment with generic probing. Return
-EBUSY on contention because waiting while holding gameport_mutex could
deadlock with probe cleanup that needs it.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/input/gameport/gameport.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
index 9707b155bc94..88b1a3464bf3 100644
--- a/drivers/input/gameport/gameport.c
+++ b/drivers/input/gameport/gameport.c
@@ -216,24 +216,12 @@ static int gameport_bind_driver(struct gameport *gameport, struct gameport_drive
 {
 	int error;
 
-	gameport->dev.driver = &drv->driver;
-	if (drv->connect(gameport, drv)) {
-		gameport->dev.driver = NULL;
-		return -ENODEV;
-	}
+	if (!device_trylock(&gameport->dev))
+		return -EBUSY;
 
-	error = device_bind_driver(&gameport->dev);
-	if (error) {
-		dev_warn(&gameport->dev,
-			 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
-			gameport->phys, gameport->name,
-			drv->description, error);
-		drv->disconnect(gameport);
-		gameport->dev.driver = NULL;
-		return error;
-	}
-
-	return 0;
+	error = device_driver_attach_unrestricted(&drv->driver, &gameport->dev);
+	device_unlock(&gameport->dev);
+	return error;
 }
 
 static void gameport_find_driver(struct gameport *gameport)

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 18/33] Input: serio - use the compatibility driver attachment API
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (16 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 17/33] Input: gameport - use the compatibility driver attachment API Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 19/33] net: phy: Use " Leon Romanovsky
                   ` (14 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Manual serio binding holds serio_mutex, but generic sysfs probing does
not take that mutex. The two paths can race while updating driver state
or cleaning up a failed probe.

Use device_driver_attach_unrestricted() after the port ID check to
centralize binding and cleanup. Acquire the device lock with
device_trylock() to exclude generic probing, returning -EBUSY instead of
waiting while holding serio_mutex. Map legacy -1 connect failures to
-ENODEV in the probe callback so they are not reported as -EPERM, while
preserving errors returned by the attachment helper.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/input/serio/serio.c | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/drivers/input/serio/serio.c b/drivers/input/serio/serio.c
index 54dd26249b02..f239217d7de4 100644
--- a/drivers/input/serio/serio.c
+++ b/drivers/input/serio/serio.c
@@ -83,26 +83,15 @@ static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
 {
 	int error;
 
-	if (serio_match_port(drv->id_table, serio)) {
+	if (!serio_match_port(drv->id_table, serio))
+		return 0;
 
-		serio->dev.driver = &drv->driver;
-		if (serio_connect_driver(serio, drv)) {
-			serio->dev.driver = NULL;
-			return -ENODEV;
-		}
+	if (!device_trylock(&serio->dev))
+		return -EBUSY;
 
-		error = device_bind_driver(&serio->dev);
-		if (error) {
-			dev_warn(&serio->dev,
-				 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
-				 serio->phys, serio->name,
-				 drv->description, error);
-			serio_disconnect_driver(serio);
-			serio->dev.driver = NULL;
-			return error;
-		}
-	}
-	return 0;
+	error = device_driver_attach_unrestricted(&drv->driver, &serio->dev);
+	device_unlock(&serio->dev);
+	return error;
 }
 
 static void serio_find_driver(struct serio *serio)
@@ -744,8 +733,10 @@ static int serio_driver_probe(struct device *dev)
 {
 	struct serio *serio = to_serio_port(dev);
 	struct serio_driver *drv = to_serio_driver(dev->driver);
+	int error;
 
-	return serio_connect_driver(serio, drv);
+	error = serio_connect_driver(serio, drv);
+	return error == -1 ? -ENODEV : error;
 }
 
 static void serio_driver_remove(struct device *dev)

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 19/33] net: phy: Use the compatibility driver attachment API
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (17 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 18/33] Input: serio " Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 20/33] USB: Drain usbfs when a device is unregistered Leon Romanovsky
                   ` (13 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

The generic PHY fallback probes directly before publishing its binding.
Device trust admission needs to precede that probe without changing the
caller's serialization, so use device_driver_attach_unrestricted()
to serialize the generic attachment.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/net/phy/phy_device.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 5d4508bcfbe9..4f20b451e270 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1857,7 +1857,6 @@ static void phy_detach_internal(struct phy_device *phydev, bool notify_bus)
 		/* The release below lets phy_probe() write this field. */
 		phydev->irq = phydev->mdio.bus->irq[phydev->mdio.addr];
 		device_release_driver(&phydev->mdio.dev);
-		phydev->is_genphy_driven = 0;
 	}
 
 	/* Assert the reset signal */
@@ -1912,6 +1911,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	struct device *d = &phydev->mdio.dev;
 	struct module *ndev_owner = NULL;
 	struct module *drv_owner;
+	struct device_driver *drv;
+	bool bind_genphy;
 	int irq = phydev->irq;
 	int err;
 
@@ -1939,16 +1940,16 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	/* Assume that if there is no driver, that it doesn't
 	 * exist, and we should use the genphy driver.
 	 */
-	if (!d->driver) {
+	drv = d->driver;
+	bind_genphy = !drv;
+	if (bind_genphy) {
 		if (phydev->is_c45)
-			d->driver = &genphy_c45_driver.mdiodrv.driver;
+			drv = &genphy_c45_driver.mdiodrv.driver;
 		else
-			d->driver = &genphy_driver.mdiodrv.driver;
-
-		phydev->is_genphy_driven = 1;
+			drv = &genphy_driver.mdiodrv.driver;
 	}
 
-	drv_owner = d->driver->owner;
+	drv_owner = drv->owner;
 	if (!try_module_get(drv_owner)) {
 		phydev_err(phydev, "failed to get the device driver module\n");
 		err = -EIO;
@@ -1956,11 +1957,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	}
 	phydev->drv_owner = drv_owner;
 
-	if (phydev->is_genphy_driven) {
-		err = d->driver->probe(d);
-		if (err >= 0)
-			err = device_bind_driver(d);
-
+	if (bind_genphy) {
+		err = device_driver_attach_unrestricted(drv, d);
 		if (err)
 			goto error_module_put;
 	}
@@ -2071,8 +2069,6 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	module_put(phydev->drv_owner);
 	phydev->drv_owner = NULL;
 	phydev->irq = irq;
-	phydev->is_genphy_driven = 0;
-	d->driver = NULL;
 error_put_device:
 	put_device(d);
 	if (ndev_owner != bus->owner)
@@ -3813,6 +3809,8 @@ static int phy_probe(struct device *dev)
 	int err = 0;
 
 	phydev->drv = phydrv;
+	phydev->is_genphy_driven = phydrv == &genphy_driver ||
+				 phydrv == &genphy_c45_driver;
 
 	/* Disable the interrupt if the PHY doesn't support it
 	 * but the interrupt is still a valid one
@@ -3949,6 +3947,7 @@ static int phy_probe(struct device *dev)
 out_reset:
 	/* Re-assert the reset signal on error */
 	phy_device_reset(phydev, 1);
+	phydev->is_genphy_driven = 0;
 
 	return err;
 }
@@ -3977,6 +3976,7 @@ static int phy_remove(struct device *dev)
 	phy_device_reset(phydev, 1);
 
 	phydev->drv = NULL;
+	phydev->is_genphy_driven = 0;
 
 	return 0;
 }

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 20/33] USB: Drain usbfs when a device is unregistered
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (18 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 19/33] net: phy: Use " Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 21/33] driver core: Introduce a generic device trust level Leon Romanovsky
                   ` (12 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Only the generic USB device driver tells usbfs that a device is gone.
A device without that driver bound, such as one unbound through sysfs,
is unplugged without any notification. usbdev_open() does not check for
a driver, so a process can open such a device and sleep in poll() on its
idle descriptor. The unplug never wakes it or sends its disconnect
signal.

Drain usbfs from usb_disconnect() before device_del(). The device is
already marked as not attached, so no new user can open it. The later
drain from the generic driver finds nothing left.

Fixes: 782da727b0d59 ("usbcore: make usb_generic a usb_device_driver")
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/usb/core/devio.c | 4 ++--
 drivers/usb/core/hub.c   | 3 +++
 drivers/usb/core/usb.h   | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 1491ba015bcd..a881f06361bf 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -2862,7 +2862,7 @@ const struct file_operations usbdev_file_operations = {
 	.release =	  usbdev_release,
 };
 
-static void usbdev_remove(struct usb_device *udev)
+void usbfs_notify_remove(struct usb_device *udev)
 {
 	struct usb_dev_state *ps;
 
@@ -2889,7 +2889,7 @@ static int usbdev_notify(struct notifier_block *self,
 	case USB_DEVICE_ADD:
 		break;
 	case USB_DEVICE_REMOVE:
-		usbdev_remove(dev);
+		usbfs_notify_remove(dev);
 		break;
 	}
 	return NOTIFY_OK;
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 199d3f535a89..372bd6368cc9 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -2379,6 +2379,9 @@ void usb_disconnect(struct usb_device **pdev)
 	if (udev->usb4_link)
 		device_link_del(udev->usb4_link);
 
+	/* usbfs is otherwise notified only by a bound generic driver */
+	usbfs_notify_remove(udev);
+
 	/* Unregister the device.  The device driver is responsible
 	 * for de-configuring the device and invoking the remove-device
 	 * notifier chain (used by usbfs and possibly others).
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 1b2de58fd6ce..cbee73183747 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -195,6 +195,7 @@ extern const struct file_operations usbdev_file_operations;
 
 extern int usb_devio_init(void);
 extern void usb_devio_cleanup(void);
+void usbfs_notify_remove(struct usb_device *udev);
 
 /*
  * Firmware specific cookie identifying a port's location. '0' == no location

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 21/33] driver core: Introduce a generic device trust level
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (19 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 20/33] USB: Drain usbfs when a device is unregistered Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 22/33] driver core: Expose device trust authorization interfaces Leon Romanovsky
                   ` (11 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Attestation needs to hold devices before driver probing and DMA setup.
Authorization on individual buses does not provide a common policy that
drivers and infrastructure can use.

Introduce a shared admission policy and operating trust level. Keep the
accepted level fixed for the device's lifetime because unbind or failed
setup does not necessarily undo hardware configuration. Retain active trust
through teardown so cleanup uses the same assumptions as setup. Preserve
existing behavior when no trust boot options are supplied.

Reviewed-by: Alexey Kardashevskiy <aik@amd.com>
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/admin-guide/kernel-parameters.txt |  14 ++
 drivers/base/Makefile                           |   2 +-
 drivers/base/base.h                             |  31 ++++
 drivers/base/core.c                             |   5 +-
 drivers/base/dd.c                               |  47 +++++-
 drivers/base/trust.c                            | 187 ++++++++++++++++++++++++
 drivers/usb/core/driver.c                       |   8 +
 drivers/usb/core/message.c                      |   5 +
 include/linux/device.h                          |   8 +
 include/linux/device/bus.h                      |  10 ++
 include/linux/device/trust.h                    |  49 +++++++
 11 files changed, 361 insertions(+), 5 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..919000af842a 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1259,6 +1259,20 @@ Kernel parameters
 	dell_smm_hwmon.fan_max=
 			[HW] Maximum configurable fan speed.
 
+	device_trust	[KNL]
+			Enable device trust with blocked as the implicit default.
+
+	device_trust.default=
+			[KNL] Set initial trust policy for all devices, including
+			hotplugged devices.
+			Format: { bus | blocked | adversarial | unrestricted }
+			No trust options selects legacy unrestricted mode, preserving
+			bus policy and existing authorization. Otherwise blocked is the
+			implicit default. An explicit value wins regardless of option
+			order. bus keeps bus policy; other values replace it.
+			unrestricted disables runtime policy changes.
+			See Documentation/driver-api/driver-model/device-trust.rst.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
diff --git a/drivers/base/Makefile b/drivers/base/Makefile
index 435710f643a5..cb3bd5fa2287 100644
--- a/drivers/base/Makefile
+++ b/drivers/base/Makefile
@@ -3,7 +3,7 @@
 
 obj-y			:= component.o core.o bus.o dd.o syscore.o \
 			   driver.o class.o platform.o \
-			   cpu.o firmware.o init.o map.o devres.o \
+			   cpu.o firmware.o init.o map.o devres.o trust.o \
 			   attribute_container.o transport_class.o \
 			   topology.o container.o property.o cacheinfo.o \
 			   swnode.o faux.o
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 61f49fc142f4..87e098790d09 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -10,6 +10,7 @@
  * shared outside of the drivers/base/ directory.
  *
  */
+#include <linux/device/trust.h>
 #include <linux/notifier.h>
 
 /**
@@ -88,6 +89,27 @@ struct driver_private {
 };
 #define to_driver(obj) container_of(obj, struct driver_private, kobj)
 
+/**
+ * enum device_trust_state - Device trust authorization and commitment
+ * @DEVICE_TRUST_SELECTABLE: No authorization or commitment. When runtime
+ *	policy changes are enabled, any policy can be selected. Selecting
+ *	blocked enters %DEVICE_TRUST_WAITING.
+ * @DEVICE_TRUST_WAITING: Blocked policy without commitment. Runtime
+ *	authorization requires adversarial or unrestricted and enters
+ *	%DEVICE_TRUST_AUTHORIZED; selecting default is rejected.
+ * @DEVICE_TRUST_AUTHORIZED: Adversarial or unrestricted selected from
+ *	%DEVICE_TRUST_WAITING. Policy is fixed, but no operating level has
+ *	been committed.
+ * @DEVICE_TRUST_COMMITTED: Policy and operating level accepted before
+ *	driver setup. Both remain fixed through setup failure and unbind.
+ */
+enum device_trust_state {
+	DEVICE_TRUST_SELECTABLE,
+	DEVICE_TRUST_WAITING,
+	DEVICE_TRUST_AUTHORIZED,
+	DEVICE_TRUST_COMMITTED,
+};
+
 /**
  * struct device_private - structure to hold the private to the driver core
  *			   portions of the device structure.
@@ -106,6 +128,7 @@ struct driver_private {
  * @device: pointer back to the struct device that this structure is
  *	    associated with.
  * @driver_lock: serializes binding and removal, including compatibility attach
+ * @trust_state: policy admission state
  * @dead: This device is currently either in the process of or has been
  *	  removed from the system. Any asynchronous events scheduled for this
  *	  device should exit without taking any action.
@@ -124,6 +147,7 @@ struct device_private {
 	struct device *device;
 	/* Taken after the device mutex, except by compatibility attach. */
 	struct mutex driver_lock;
+	enum device_trust_state trust_state;
 	u8 dead:1;
 };
 #define to_device_private_parent(obj)	\
@@ -169,6 +193,13 @@ void bus_remove_driver(struct device_driver *drv);
 void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
 				    struct device *parent);
 
+int device_private_init(struct device *dev);
+
+/* Unrestricted-only manual attachment uses the caller's subsystem serialization. */
+void device_trust_init_state(struct device *dev);
+int device_trust_prepare_unrestricted(struct device *dev,
+				      const struct device_driver *drv);
+
 void driver_detach(const struct device_driver *drv);
 void driver_deferred_probe_del(struct device *dev);
 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 92d25bf4f863..c0006b3aa38f 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -3613,7 +3613,7 @@ static void device_remove_sys_dev_entry(struct device *dev)
 	}
 }
 
-static int device_private_init(struct device *dev)
+int device_private_init(struct device *dev)
 {
 	dev->p = kzalloc_obj(*dev->p);
 	if (!dev->p)
@@ -3694,6 +3694,9 @@ int device_add(struct device *dev)
 	if (error)
 		goto name_error;
 
+	device_trust_init(dev);
+	device_trust_init_state(dev);
+
 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
 
 	parent = get_device(dev->parent);
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 6ac1486b9fbb..4c4723384dc6 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -541,6 +541,25 @@ static int __device_bind_driver(struct device *dev)
 	return 0;
 }
 
+static int device_bind_driver_locked(struct device *dev)
+{
+	bool admitted = dev->trust_level != DEVICE_TRUST_BLOCKED;
+	int ret;
+
+	if (dev->bus && dev->bus->trust_resolve)
+		return -EOPNOTSUPP;
+
+	ret = device_trust_prepare(dev, dev->driver);
+	if (ret)
+		return ret;
+
+	ret = __device_bind_driver(dev);
+	/* An early claimant remains active until its bus finishes cleanup. */
+	if (ret && !admitted)
+		device_trust_clear(dev);
+	return ret;
+}
+
 /**
  * device_bind_driver - bind a driver to one device.
  * @dev: device.
@@ -561,14 +580,14 @@ int device_bind_driver(struct device *dev)
 	int ret;
 
 	mutex_lock(&dev->p->driver_lock);
-	ret = __device_bind_driver(dev);
+	ret = device_bind_driver_locked(dev);
 	mutex_unlock(&dev->p->driver_lock);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(device_bind_driver);
 
 /**
- * device_driver_attach_unrestricted - attach a driver using subsystem serialization
+ * device_driver_attach_unrestricted - attach under a fixed unrestricted policy
  * @drv: driver to attach
  * @dev: device to attach it to
  *
@@ -583,6 +602,12 @@ EXPORT_SYMBOL_GPL(device_bind_driver);
  * It calls the bus or driver probe callback and publishes the binding without
  * performing the normal driver-core DMA, pinctrl or power-domain setup.
  *
+ * Requires an unrestricted operating level and either no device trust boot options
+ * or device_trust.default=unrestricted. Changes through trust_policy are disabled
+ * in both modes. The caller must exclude bus-specific policy changes.
+ * When driver boot rules exist, an explicit device boot rule must exclude
+ * them for @dev. Buses with a trust resolver must use device_driver_attach().
+ *
  * Return: 0 on success, -EAGAIN if registration or probing must be retried,
  * or another negative error before binding.
  */
@@ -608,6 +633,10 @@ int device_driver_attach_unrestricted(const struct device_driver *drv,
 		goto unlock;
 	}
 
+	ret = device_trust_prepare_unrestricted(dev, drv);
+	if (ret)
+		goto unlock;
+
 	device_set_driver(dev, drv);
 	if (dev->bus->probe)
 		ret = dev->bus->probe(dev);
@@ -632,6 +661,7 @@ int device_driver_attach_unrestricted(const struct device_driver *drv,
 	devres_release_all(dev);
 	device_set_driver(dev, NULL);
 	dev_set_drvdata(dev, NULL);
+	WRITE_ONCE(dev->trust_level, DEVICE_TRUST_BLOCKED);
 	if (ret == -EPROBE_DEFER)
 		ret = -EAGAIN;
 unlock:
@@ -763,6 +793,14 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
 	}
 
 re_probe:
+	ret = device_trust_prepare(dev, drv);
+	if (ret) {
+		device_links_no_driver(dev);
+		/* Report it like a probe error so a scan tries the next driver. */
+		ret = -ret;
+		goto done;
+	}
+
 	device_set_driver(dev, drv);
 
 	/* If using pinctrl, bind pins now before probing */
@@ -829,6 +867,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
 		if (dev->bus && dev->bus->dma_cleanup)
 			dev->bus->dma_cleanup(dev);
 		device_unbind_cleanup(dev);
+		device_trust_clear(dev);
 
 		goto re_probe;
 	}
@@ -855,6 +894,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
 pinctrl_bind_failed:
 	device_links_no_driver(dev);
 	device_unbind_cleanup(dev);
+	device_trust_clear(dev);
 done:
 	return ret;
 }
@@ -1196,7 +1236,7 @@ static int __device_attach(struct device *dev, bool allow_async)
 			ret = 1;
 			goto out_unlock;
 		}
-		ret = __device_bind_driver(dev);
+		ret = device_bind_driver_locked(dev);
 		if (ret == 0)
 			ret = 1;
 		else {
@@ -1448,6 +1488,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)
 			dev->bus->dma_cleanup(dev);
 
 		device_unbind_cleanup(dev);
+		device_trust_clear(dev);
 		device_links_driver_cleanup(dev);
 
 		klist_remove(&dev->p->knode_driver);
diff --git a/drivers/base/trust.c b/drivers/base/trust.c
new file mode 100644
index 000000000000..01e7dfc9c6f3
--- /dev/null
+++ b/drivers/base/trust.c
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <linux/init.h>
+#include <linux/string.h>
+
+#include "base.h"
+
+static enum device_trust_policy device_trust_default_policy __ro_after_init =
+	DEVICE_TRUST_POLICY_UNRESTRICTED;
+static bool device_trust_configured __ro_after_init;
+
+static void __init device_trust_configure(void)
+{
+	if (device_trust_configured)
+		return;
+
+	device_trust_default_policy = DEVICE_TRUST_POLICY_BLOCKED;
+	device_trust_configured = true;
+}
+
+static int __init device_trust_detect_setup(char *str)
+{
+	if (!*str || *str == '=' || *str == '.')
+		device_trust_configure();
+	return !*str;
+}
+__setup("device_trust", device_trust_detect_setup);
+
+void device_trust_init_state(struct device *dev)
+{
+	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
+		dev->p->trust_state = DEVICE_TRUST_COMMITTED;
+	else
+		dev->p->trust_state = dev->trust_policy == DEVICE_TRUST_POLICY_BLOCKED ?
+			DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
+}
+
+static int __device_trust_prepare(struct device *dev,
+				  const struct device_driver *drv, bool unrestricted_only)
+{
+	enum device_trust_policy policy;
+	enum device_trust_level level;
+	int ret;
+
+	policy = dev->trust_policy;
+
+	if (policy == DEVICE_TRUST_POLICY_BLOCKED)
+		return -EPERM;
+	if (dev->bus && dev->bus->trust_resolve) {
+		ret = dev->bus->trust_resolve(dev, drv, policy, &level);
+		if (ret)
+			return ret;
+	} else {
+		level = policy == DEVICE_TRUST_POLICY_ADVERSARIAL ?
+			DEVICE_TRUST_ADVERSARIAL : DEVICE_TRUST_UNRESTRICTED;
+	}
+	if (level == DEVICE_TRUST_BLOCKED)
+		return -EPERM;
+	if (unrestricted_only && level != DEVICE_TRUST_UNRESTRICTED)
+		return -EPERM;
+	if (dev->p->trust_state == DEVICE_TRUST_COMMITTED &&
+	    dev->trust_committed_level != level)
+		return -EINVAL;
+
+	WRITE_ONCE(dev->trust_committed_level, level);
+	dev->p->trust_state = DEVICE_TRUST_COMMITTED;
+	WRITE_ONCE(dev->trust_level, level);
+	return 0;
+}
+
+/**
+ * device_trust_prepare - Admit a driver before operating a device
+ * @dev: initialized device with its bus, name and initial policy set
+ * @drv: driver claiming or probing the device
+ *
+ * The caller must hold the device lock. Buses may admit a claim before
+ * device_add(); registration preserves the resulting policy and commitment.
+ * Clear the active level after the driver has stopped operating the device.
+ *
+ * Return: 0 on admission, or a negative error before driver setup.
+ */
+int device_trust_prepare(struct device *dev, const struct device_driver *drv)
+{
+	int ret;
+
+	device_lock_assert(dev);
+	if (!dev->p) {
+		ret = device_private_init(dev);
+		if (ret)
+			return ret;
+		device_trust_init(dev);
+		device_trust_init_state(dev);
+	}
+	return __device_trust_prepare(dev, drv, false);
+}
+EXPORT_SYMBOL_GPL(device_trust_prepare);
+
+int device_trust_prepare_unrestricted(struct device *dev,
+				      const struct device_driver *drv)
+{
+	if (device_trust_default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+		return -EPERM;
+
+	return __device_trust_prepare(dev, drv, true);
+}
+
+/**
+ * device_trust_clear - End a device's active operating level
+ * @dev: device whose driver setup and cleanup have finished
+ *
+ * The caller must hold the device lock. Lifetime commitment is retained.
+ */
+void device_trust_clear(struct device *dev)
+{
+	device_lock_assert(dev);
+	WRITE_ONCE(dev->trust_level, DEVICE_TRUST_BLOCKED);
+}
+EXPORT_SYMBOL_GPL(device_trust_clear);
+
+/**
+ * device_get_trust_level - Return a device's active trust level
+ * @dev: device to inspect
+ *
+ * The value is stable from the start of DMA configuration through the end of
+ * DMA cleanup. An unbound device has %DEVICE_TRUST_BLOCKED as its active
+ * trust level.
+ *
+ * Return: The active trust level for the current bind transaction.
+ */
+enum device_trust_level device_get_trust_level(const struct device *dev)
+{
+	return READ_ONCE(dev->trust_level);
+}
+EXPORT_SYMBOL_GPL(device_get_trust_level);
+
+/**
+ * device_is_adversarial - Test whether a device is operated as an adversary
+ * @dev: device to inspect
+ *
+ * Return: True when the active trust level is %DEVICE_TRUST_ADVERSARIAL.
+ */
+bool device_is_adversarial(const struct device *dev)
+{
+	return device_get_trust_level(dev) == DEVICE_TRUST_ADVERSARIAL;
+}
+EXPORT_SYMBOL_GPL(device_is_adversarial);
+
+static const char * const device_trust_policy_names[] = {
+	[DEVICE_TRUST_POLICY_BUS] = "bus",
+	[DEVICE_TRUST_POLICY_BLOCKED] = "blocked",
+	[DEVICE_TRUST_POLICY_ADVERSARIAL] = "adversarial",
+	[DEVICE_TRUST_POLICY_UNRESTRICTED] = "unrestricted",
+};
+
+static int __init device_trust_default_setup(char *str)
+{
+	int policy = match_string(device_trust_policy_names,
+				  ARRAY_SIZE(device_trust_policy_names), str);
+
+	device_trust_configure();
+	if (policy < 0) {
+		pr_warn("device_trust: ignoring invalid default policy '%s'\n", str);
+		return 1;
+	}
+
+	device_trust_default_policy = policy;
+	return 1;
+}
+__setup("device_trust.default=", device_trust_default_setup);
+
+/**
+ * device_trust_init - Apply global boot policy before registration
+ * @dev: unregistered device with its initial policy set
+ */
+void device_trust_init(struct device *dev)
+{
+	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
+		return;
+
+	if (device_trust_configured &&
+	    device_trust_default_policy != DEVICE_TRUST_POLICY_BUS)
+		dev->trust_policy = device_trust_default_policy;
+}
+EXPORT_SYMBOL_GPL(device_trust_init);
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 9dd022c1ca15..1d488111a70f 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -582,6 +582,12 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 		iface->claim_owner = driver->driver.owner;
 	}
 
+	retval = device_trust_prepare(dev, &driver->driver);
+	if (retval) {
+		usb_put_claim_owner(iface);
+		goto unlock;
+	}
+
 	dev->driver = &driver->driver;
 	usb_set_intfdata(iface, data);
 	iface->needs_binding = 0;
@@ -619,6 +625,7 @@ int usb_driver_claim_interface(struct usb_driver *driver,
 		if (driver->supports_autosuspend)
 			pm_runtime_disable(dev);
 		pm_runtime_set_suspended(dev);
+		device_trust_clear(dev);
 	}
 
 unlock:
@@ -634,6 +641,7 @@ static void usb_unbind_claim(struct usb_interface *iface)
 	usb_unbind_interface(dev);
 	dev_set_drvdata(dev, NULL);
 	dev->driver = NULL;
+	device_trust_clear(dev);
 	usb_put_claim_owner(iface);
 }
 
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index e7a23e406f8a..ca6976e266f0 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -2237,6 +2237,8 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 		pm_runtime_no_callbacks(&intf->dev);
 		dev_set_name(&intf->dev, "%d-%s:%d.%d", dev->bus->busnum,
 				dev->devpath, configuration, ifnum);
+		/* A sibling may claim the interface before it is registered */
+		device_trust_init(&intf->dev);
 		/*
 		 * Make sure the interface does not outlive its parent (e.g.
 		 * as needed for usb_queue_reset_device() and
@@ -2319,6 +2321,9 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 			get_device(&intf->dev);
 			if (unconfigure)
 				usb_set_configuration(dev, -1);
+			device_lock(&intf->dev);
+			device_trust_clear(&intf->dev);
+			device_unlock(&intf->dev);
 			module_put(claim_owner);
 			put_device(&intf->dev);
 			if (unconfigure) {
diff --git a/include/linux/device.h b/include/linux/device.h
index 5433b2609ab3..702eb66b81bf 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -30,6 +30,7 @@
 #include <linux/device/class.h>
 #include <linux/device/devres.h>
 #include <linux/device/driver.h>
+#include <linux/device/trust.h>
 #include <linux/cleanup.h>
 #include <asm/device.h>
 
@@ -695,6 +696,10 @@ enum struct_device_flags {
  * @removable:  Whether the device can be removed from the system. This
  *              should be set by the subsystem / bus driver that discovered
  *              the device.
+ * @trust_policy: Requested operating policy, resolved before driver probe.
+ * @trust_level: Active trust level through driver setup and cleanup, see
+ *		device_get_trust_level().
+ * @trust_committed_level: Operating level retained across bind transactions.
  * @flags:	DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
  *
  * At the lowest level, every device in a Linux system is represented by an
@@ -798,6 +803,9 @@ struct device {
 	struct device_physical_location *physical_location;
 
 	enum device_removable	removable;
+	enum device_trust_policy trust_policy;
+	enum device_trust_level trust_level;
+	enum device_trust_level trust_committed_level;
 
 	DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
 };
diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h
index 9ef7538a9a7f..ba2b5743a47c 100644
--- a/include/linux/device/bus.h
+++ b/include/linux/device/bus.h
@@ -17,6 +17,7 @@
 #include <linux/kobject.h>
 #include <linux/klist.h>
 #include <linux/pm.h>
+#include <linux/device/trust.h>
 
 struct device_driver;
 struct fwnode_handle;
@@ -59,6 +60,11 @@ struct fwnode_handle;
  * @resume:	Called to bring a device on this bus out of sleep mode.
  * @num_vf:	Called to find out how many virtual functions a device on this
  *		bus supports.
+ * @trust_resolve: Resolve a requested trust policy to an active trust level
+ *		immediately before driver probe. The device lock is held and
+ *		@drv is the candidate driver, which dev->driver does not name
+ *		yet. Return 0 on success or a negative errno. This callback
+ *		must not enable the device.
  * @dma_configure:	Called to setup DMA configuration on a device on
  *			this bus.
  * @dma_cleanup:	Called to cleanup DMA configuration on a device on
@@ -102,6 +108,10 @@ struct bus_type {
 
 	int (*num_vf)(struct device *dev);
 
+	int (*trust_resolve)(struct device *dev,
+			     const struct device_driver *drv,
+			     enum device_trust_policy policy,
+			     enum device_trust_level *level);
 	int (*dma_configure)(struct device *dev);
 	void (*dma_cleanup)(struct device *dev);
 
diff --git a/include/linux/device/trust.h b/include/linux/device/trust.h
new file mode 100644
index 000000000000..f6f8e58a2e2b
--- /dev/null
+++ b/include/linux/device/trust.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _DEVICE_TRUST_H_
+#define _DEVICE_TRUST_H_
+
+#include <linux/types.h>
+
+struct device;
+struct device_driver;
+
+/**
+ * enum device_trust_level - active trust level for a device
+ * @DEVICE_TRUST_BLOCKED: The device cannot be operated by a driver
+ * @DEVICE_TRUST_ADVERSARIAL: Classify the operating device as potentially malicious
+ * @DEVICE_TRUST_UNRESTRICTED: Operate the device with the usual kernel privileges
+ *
+ * Active trust levels are discrete states, not an ordered privilege scale.
+ * Adversarial classification alone adds no protections; these depend on bus,
+ * DMA and driver support.
+ */
+enum device_trust_level {
+	DEVICE_TRUST_BLOCKED,
+	DEVICE_TRUST_ADVERSARIAL,
+	DEVICE_TRUST_UNRESTRICTED,
+};
+
+/**
+ * enum device_trust_policy - requested device trust policy
+ * @DEVICE_TRUST_POLICY_BUS: Let the bus select the active trust level
+ * @DEVICE_TRUST_POLICY_BLOCKED: Request that the device remain blocked
+ * @DEVICE_TRUST_POLICY_ADVERSARIAL: Request adversarial classification
+ * @DEVICE_TRUST_POLICY_UNRESTRICTED: Request normal operation
+ *
+ * A requested policy is resolved to an active trust level before a driver is
+ * probed.
+ */
+enum device_trust_policy {
+	DEVICE_TRUST_POLICY_BUS,
+	DEVICE_TRUST_POLICY_BLOCKED,
+	DEVICE_TRUST_POLICY_ADVERSARIAL,
+	DEVICE_TRUST_POLICY_UNRESTRICTED,
+};
+
+void device_trust_init(struct device *dev);
+int device_trust_prepare(struct device *dev, const struct device_driver *drv);
+void device_trust_clear(struct device *dev);
+enum device_trust_level device_get_trust_level(const struct device *dev);
+bool device_is_adversarial(const struct device *dev);
+
+#endif /* _DEVICE_TRUST_H_ */

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 22/33] driver core: Expose device trust authorization interfaces
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (20 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 21/33] driver core: Introduce a generic device trust level Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 23/33] driver core: Accept device trust policy on the kernel command line Leon Romanovsky
                   ` (10 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

An initrd must be able to authorize a blocked device after attestation and
distinguish that permission from a successful driver binding. Existing bus
authorization interfaces also need a way to grant access without bypassing
the common policy.

Expose policy and active trust through sysfs, with shared authorization
rules for bus callers. Authorizing a blocked device fixes the choice for
its lifetime, so a failed probe cannot reopen the decision. Require boot
opt-in for the new writable interface to preserve legacy attachment
assumptions.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-devices-trust |  32 +++++++
 MAINTAINERS                                   |   1 +
 drivers/base/base.h                           |   2 +
 drivers/base/core.c                           |  15 ++-
 drivers/base/trust.c                          | 129 ++++++++++++++++++++++++++
 include/linux/device.h                        |   5 +
 include/linux/device/trust.h                  |   3 +
 7 files changed, 186 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
new file mode 100644
index 000000000000..54feb26ce135
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-devices-trust
@@ -0,0 +1,32 @@
+What:		/sys/devices/.../trust
+Date:		September 2026
+Contact:	driver-core@lists.linux.dev
+Description:
+		(RO) The active trust level for the device. The values are
+		"blocked", "adversarial", and "unrestricted". Driver core
+		resolves the level before driver setup and retains it through
+		DMA cleanup. It is "blocked" outside a binding transaction.
+
+What:		/sys/devices/.../trust_policy
+Date:		September 2026
+Contact:	driver-core@lists.linux.dev
+Description:
+		(RW) The requested trust policy for the device. The initial
+		value may be selected by the bus before the device is
+		registered; otherwise it is "bus". The policy is resolved
+		to an active trust level before the device is probed.
+
+		Changes through this attribute require an explicit device
+		trust boot option. Any such option selects an implicit
+		blocked default; use device_trust.default=bus to
+		enable them while preserving bus policy. Without one,
+		only writing the current policy succeeds. Existing bus
+		authorization interfaces remain available. Explicit unrestricted
+		policy disables runtime policy changes.
+
+		The values are "bus", "blocked", "adversarial", and
+		"unrestricted". "bus" lets the bus select the operating level.
+		"blocked" prevents driver binding. "adversarial" requests
+		classification as potentially malicious. "unrestricted" requests
+		normal operation. Adversarial protections depend on bus, DMA,
+		and driver support; classification alone adds none.
diff --git a/MAINTAINERS b/MAINTAINERS
index b726344362b4..a145aaaca2ef 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8150,6 +8150,7 @@ L:	driver-core@lists.linux.dev
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
 F:	Documentation/ABI/testing/sysfs-bus-drivers-probe
+F:	Documentation/ABI/testing/sysfs-devices-trust
 F:	Documentation/core-api/kobject.rst
 F:	Documentation/driver-api/driver-model/
 F:	drivers/base/
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 87e098790d09..e51637ed3719 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -195,6 +195,8 @@ void device_release_driver_internal(struct device *dev, const struct device_driv
 
 int device_private_init(struct device *dev);
 
+extern const struct attribute_group device_trust_attr_group;
+
 /* Unrestricted-only manual attachment uses the caller's subsystem serialization. */
 void device_trust_init_state(struct device *dev);
 int device_trust_prepare_unrestricted(struct device *dev,
diff --git a/drivers/base/core.c b/drivers/base/core.c
index c0006b3aa38f..e72424618d62 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2999,10 +2999,14 @@ static int device_add_attrs(struct device *dev)
 	if (error)
 		goto err_remove_type_groups;
 
+	error = device_add_group(dev, &device_trust_attr_group);
+	if (error)
+		goto err_remove_dev_groups;
+
 	if (device_supports_offline(dev) && !dev_offline_disabled(dev)) {
 		error = device_create_file(dev, &dev_attr_online);
 		if (error)
-			goto err_remove_dev_groups;
+			goto err_remove_dev_trust_group;
 	}
 
 	if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
@@ -3032,6 +3036,8 @@ static int device_add_attrs(struct device *dev)
 	device_remove_file(dev, &dev_attr_waiting_for_supplier);
  err_remove_dev_online:
 	device_remove_file(dev, &dev_attr_online);
+ err_remove_dev_trust_group:
+	device_remove_group(dev, &device_trust_attr_group);
  err_remove_dev_groups:
 	device_remove_groups(dev, dev->groups);
  err_remove_type_groups:
@@ -3057,6 +3063,7 @@ static void device_remove_attrs(struct device *dev)
 	device_remove_file(dev, &dev_attr_removable);
 	device_remove_file(dev, &dev_attr_waiting_for_supplier);
 	device_remove_file(dev, &dev_attr_online);
+	device_remove_group(dev, &device_trust_attr_group);
 	device_remove_groups(dev, dev->groups);
 
 	if (type)
@@ -4809,6 +4816,12 @@ static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
 	if (error)
 		return error;
 
+	/* Change the trust attributes of @dev to @kuid/@kgid. */
+	error = sysfs_group_change_owner(kobj, &device_trust_attr_group, kuid,
+					 kgid);
+	if (error)
+		return error;
+
 	if (device_supports_offline(dev) && !dev_offline_disabled(dev)) {
 		/* Change online device attributes of @dev to @kuid/@kgid. */
 		error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
diff --git a/drivers/base/trust.c b/drivers/base/trust.c
index 01e7dfc9c6f3..684a6a7c1d8b 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -5,6 +5,7 @@
 #include <linux/export.h>
 #include <linux/init.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 
 #include "base.h"
 
@@ -155,6 +156,134 @@ static const char * const device_trust_policy_names[] = {
 	[DEVICE_TRUST_POLICY_UNRESTRICTED] = "unrestricted",
 };
 
+static const char * const device_trust_level_names[] = {
+	[DEVICE_TRUST_BLOCKED] = "blocked",
+	[DEVICE_TRUST_ADVERSARIAL] = "adversarial",
+	[DEVICE_TRUST_UNRESTRICTED] = "unrestricted",
+};
+
+static ssize_t trust_show(struct device *dev,
+			  const struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n",
+			  device_trust_level_names[device_get_trust_level(dev)]);
+}
+static DEVICE_ATTR_RO(trust);
+
+static ssize_t trust_policy_show(struct device *dev,
+				 const struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n",
+			  device_trust_policy_names[READ_ONCE(dev->trust_policy)]);
+}
+
+static bool device_trust_policy_frozen(void)
+{
+	return device_trust_configured &&
+		device_trust_default_policy == DEVICE_TRUST_POLICY_UNRESTRICTED;
+}
+
+/**
+ * device_trust_set_policy_locked - Request policy with the device locked
+ * @dev: registered device
+ * @policy: requested policy
+ *
+ * Apply the admission rules while the caller holds the device lock.
+ *
+ * Return: 0 on success, -EBUSY if the policy can no longer change, or
+ * -ENODEV if the device is being removed outside explicit unrestricted mode.
+ */
+int device_trust_set_policy_locked(struct device *dev,
+				   enum device_trust_policy policy)
+{
+	device_lock_assert(dev);
+	if (device_trust_policy_frozen())
+		return policy == dev->trust_policy ? 0 : -EBUSY;
+	if (dev->p->dead)
+		return -ENODEV;
+	if (policy == dev->trust_policy)
+		goto set_policy;
+
+	if (dev->driver)
+		return -EBUSY;
+
+	switch (dev->p->trust_state) {
+	case DEVICE_TRUST_SELECTABLE:
+		if (policy == DEVICE_TRUST_POLICY_BLOCKED)
+			dev->p->trust_state = DEVICE_TRUST_WAITING;
+		break;
+	case DEVICE_TRUST_WAITING:
+		if (policy != DEVICE_TRUST_POLICY_ADVERSARIAL &&
+		    policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+			return -EBUSY;
+		dev->p->trust_state = DEVICE_TRUST_AUTHORIZED;
+		break;
+	default:
+		return -EBUSY;
+	}
+set_policy:
+	WRITE_ONCE(dev->trust_policy, policy);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(device_trust_set_policy_locked);
+
+/**
+ * device_trust_set_policy - Request a trust policy for a device
+ * @dev: registered device
+ * @policy: requested policy
+ *
+ * Apply admission rules, including on legacy boots where trust_policy writes
+ * are unavailable. Buses with their own authorization interfaces may use this
+ * API. Callers of device_driver_attach_unrestricted() must preserve its invariants.
+ *
+ * Takes the device lock except in explicit unrestricted mode, where a same-policy
+ * request is a no-op and does not override a pending driver boot rule.
+ *
+ * Return: 0 on success, -EBUSY if the policy can no longer change, or
+ * -ENODEV if the device is being removed outside explicit unrestricted mode.
+ */
+int device_trust_set_policy(struct device *dev, enum device_trust_policy policy)
+{
+	int ret;
+
+	if (device_trust_policy_frozen())
+		return policy == READ_ONCE(dev->trust_policy) ? 0 : -EBUSY;
+
+	device_lock(dev);
+	ret = device_trust_set_policy_locked(dev, policy);
+	device_unlock(dev);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(device_trust_set_policy);
+
+static ssize_t trust_policy_store(struct device *dev,
+				  const struct device_attribute *attr,
+				  const char *buf, size_t count)
+{
+	int policy, ret;
+
+	policy = sysfs_match_string(device_trust_policy_names, buf);
+	if (policy < 0)
+		return policy;
+
+	if (!device_trust_configured)
+		return policy == READ_ONCE(dev->trust_policy) ? count : -EBUSY;
+
+	ret = device_trust_set_policy(dev, policy);
+
+	return ret ? ret : count;
+}
+static DEVICE_ATTR_RW(trust_policy);
+
+static struct attribute *device_trust_attrs[] = {
+	&dev_attr_trust.attr,
+	&dev_attr_trust_policy.attr,
+	NULL,
+};
+
+const struct attribute_group device_trust_attr_group = {
+	.attrs = device_trust_attrs,
+};
 static int __init device_trust_default_setup(char *str)
 {
 	int policy = match_string(device_trust_policy_names,
diff --git a/include/linux/device.h b/include/linux/device.h
index 702eb66b81bf..2914b7191d34 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -933,6 +933,11 @@ static inline bool device_iommu_mapped(struct device *dev)
 	return (dev->iommu_group != NULL);
 }
 
+static inline bool device_policy_is_blocked(const struct device *dev)
+{
+	return READ_ONCE(dev->trust_policy) == DEVICE_TRUST_POLICY_BLOCKED;
+}
+
 /* Get the wakeup routines, which depend on struct device */
 #include <linux/pm_wakeup.h>
 
diff --git a/include/linux/device/trust.h b/include/linux/device/trust.h
index f6f8e58a2e2b..6ce0fcd00caf 100644
--- a/include/linux/device/trust.h
+++ b/include/linux/device/trust.h
@@ -43,6 +43,9 @@ enum device_trust_policy {
 void device_trust_init(struct device *dev);
 int device_trust_prepare(struct device *dev, const struct device_driver *drv);
 void device_trust_clear(struct device *dev);
+int device_trust_set_policy_locked(struct device *dev,
+				   enum device_trust_policy policy);
+int device_trust_set_policy(struct device *dev, enum device_trust_policy policy);
 enum device_trust_level device_get_trust_level(const struct device *dev);
 bool device_is_adversarial(const struct device *dev);
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 23/33] driver core: Accept device trust policy on the kernel command line
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (21 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 22/33] driver core: Expose device trust authorization interfaces Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 24/33] driver core: Accept device trust policy by driver name Leon Romanovsky
                   ` (9 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Blocking devices until initrd attestation also blocks the hardware needed
to reach the initrd and perform that attestation. A global policy alone
cannot allow those dependencies while keeping other devices blocked.

Allow boot policy exceptions for specific bus devices before probing,
including devices discovered later. Keep unmatched devices blocked unless
the administrator explicitly chooses another default. This makes the
exceptions usable with built-in drivers and avoids depending on userspace
winning a race against early probing.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-devices-trust   |   2 +
 Documentation/admin-guide/kernel-parameters.txt |  14 ++-
 drivers/base/base.h                             |   8 ++
 drivers/base/trust.c                            | 116 +++++++++++++++++++++++-
 include/linux/device/trust.h                    |   1 +
 5 files changed, 134 insertions(+), 7 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
index 54feb26ce135..e8629760a1c8 100644
--- a/Documentation/ABI/testing/sysfs-devices-trust
+++ b/Documentation/ABI/testing/sysfs-devices-trust
@@ -15,6 +15,8 @@ Description:
 		value may be selected by the bus before the device is
 		registered; otherwise it is "bus". The policy is resolved
 		to an active trust level before the device is probed.
+		A matching device_trust= kernel command-line entry overrides
+		the initial requested policy before registration.
 
 		Changes through this attribute require an explicit device
 		trust boot option. Any such option selects an implicit
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 919000af842a..466665fc1262 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1262,9 +1262,19 @@ Kernel parameters
 	device_trust	[KNL]
 			Enable device trust with blocked as the implicit default.
 
+	device_trust=	[KNL]
+			Set initial device trust policy before probing, including
+			for devices added after boot.
+			Format: <bus>:<device>=<policy>[,...]
+			Names match exactly as in /sys/bus/<bus>/devices/<device>.
+			Policies are bus, blocked, adversarial, and
+			unrestricted.
+			Entries override bus policy and device_trust.default.
+			See Documentation/driver-api/driver-model/device-trust.rst.
+
 	device_trust.default=
-			[KNL] Set initial trust policy for all devices, including
-			hotplugged devices.
+			[KNL] Set initial trust policy for devices without a
+			matching device_trust= entry, including hotplugged devices.
 			Format: { bus | blocked | adversarial | unrestricted }
 			No trust options selects legacy unrestricted mode, preserving
 			bus policy and existing authorization. Otherwise blocked is the
diff --git a/drivers/base/base.h b/drivers/base/base.h
index e51637ed3719..761d52f4c9ca 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -197,6 +197,14 @@ int device_private_init(struct device *dev);
 
 extern const struct attribute_group device_trust_attr_group;
 
+#if IS_ENABLED(CONFIG_KUNIT)
+int device_trust_parse_cmdline(const char *options, const struct device *dev,
+			       enum device_trust_policy *policy);
+void device_trust_apply_boot_policy(struct device *dev,
+				    enum device_trust_policy policy,
+				    const char *options);
+#endif
+
 /* Unrestricted-only manual attachment uses the caller's subsystem serialization. */
 void device_trust_init_state(struct device *dev);
 int device_trust_prepare_unrestricted(struct device *dev,
diff --git a/drivers/base/trust.c b/drivers/base/trust.c
index 684a6a7c1d8b..103b8d00eb84 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -1,11 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <linux/ctype.h>
 #include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/export.h>
 #include <linux/init.h>
 #include <linux/string.h>
 #include <linux/sysfs.h>
+#include <kunit/visibility.h>
 
 #include "base.h"
 
@@ -13,6 +15,17 @@ static enum device_trust_policy device_trust_default_policy __ro_after_init =
 	DEVICE_TRUST_POLICY_UNRESTRICTED;
 static bool device_trust_configured __ro_after_init;
 
+/**
+ * device_trust_enabled - Test whether boot options enabled device trust
+ *
+ * Return: True if any device trust option was supplied at boot.
+ */
+bool device_trust_enabled(void)
+{
+	return device_trust_configured;
+}
+EXPORT_SYMBOL_GPL(device_trust_enabled);
+
 static void __init device_trust_configure(void)
 {
 	if (device_trust_configured)
@@ -284,6 +297,88 @@ static struct attribute *device_trust_attrs[] = {
 const struct attribute_group device_trust_attr_group = {
 	.attrs = device_trust_attrs,
 };
+
+static const char *device_trust_cmdline __ro_after_init;
+
+static int device_trust_parse_policy(const char *options, const char *target_bus,
+				     const char *target_name,
+				     enum device_trust_policy *policy)
+{
+	enum device_trust_policy selected = DEVICE_TRUST_POLICY_BUS;
+	const char *entry = options;
+	bool matched = false;
+
+	if (!*entry)
+		return -EINVAL;
+
+	for (;;) {
+		const char *end = entry + strcspn(entry, ",");
+		const char *bus_end, *name, *level, *p;
+		unsigned int i;
+
+		bus_end = memchr(entry, ':', end - entry);
+		if (!bus_end || bus_end == entry ||
+		    memchr(entry, '=', bus_end - entry))
+			return -EINVAL;
+		name = bus_end + 1;
+		level = memchr(name, '=', end - name);
+		if (!level || level == name || level + 1 == end)
+			return -EINVAL;
+
+		for (p = entry; p < end; p++)
+			if (isspace(*p) || iscntrl(*p) || *p == '"')
+				return -EINVAL;
+
+		for (i = 0; i < ARRAY_SIZE(device_trust_policy_names); i++)
+			if (strlen(device_trust_policy_names[i]) == end - level - 1 &&
+			    !strncmp(level + 1, device_trust_policy_names[i],
+				     end - level - 1))
+				break;
+		if (i == ARRAY_SIZE(device_trust_policy_names))
+			return -EINVAL;
+
+		if (target_bus && target_name &&
+		    strlen(target_bus) == bus_end - entry &&
+		    !strncmp(target_bus, entry, bus_end - entry) &&
+		    strlen(target_name) == level - name &&
+		    !strncmp(target_name, name, level - name)) {
+			selected = i;
+			matched = true;
+		}
+
+		if (!*end)
+			break;
+		entry = end + 1;
+	}
+
+	if (matched && policy)
+		*policy = selected;
+	return matched;
+}
+
+VISIBLE_IF_KUNIT int device_trust_parse_cmdline(const char *options,
+						const struct device *dev,
+						enum device_trust_policy *policy)
+{
+	return device_trust_parse_policy(options, dev && dev->bus ? dev->bus->name : NULL,
+					 dev ? dev_name(dev) : NULL, policy);
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_parse_cmdline);
+
+static int __init device_trust_setup(char *str)
+{
+	device_trust_configure();
+	if (device_trust_parse_cmdline(str, NULL, NULL) < 0) {
+		pr_warn("device_trust: ignoring invalid policy list '%s'\n", str);
+		return 1;
+	}
+
+	/* The command-line buffer survives init. */
+	device_trust_cmdline = str;
+	return 1;
+}
+__setup("device_trust=", device_trust_setup);
+
 static int __init device_trust_default_setup(char *str)
 {
 	int policy = match_string(device_trust_policy_names,
@@ -300,17 +395,28 @@ static int __init device_trust_default_setup(char *str)
 }
 __setup("device_trust.default=", device_trust_default_setup);
 
+VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,
+						   enum device_trust_policy policy,
+						   const char *options)
+{
+	if (policy != DEVICE_TRUST_POLICY_BUS)
+		dev->trust_policy = policy;
+	if (options)
+		device_trust_parse_cmdline(options, dev, &dev->trust_policy);
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_boot_policy);
+
 /**
- * device_trust_init - Apply global boot policy before registration
- * @dev: unregistered device with its initial policy set
+ * device_trust_init - Apply boot policy before registration or bus policy use
+ * @dev: unregistered device with its bus, name, and initial policy set
  */
 void device_trust_init(struct device *dev)
 {
 	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
 		return;
 
-	if (device_trust_configured &&
-	    device_trust_default_policy != DEVICE_TRUST_POLICY_BUS)
-		dev->trust_policy = device_trust_default_policy;
+	if (device_trust_configured)
+		device_trust_apply_boot_policy(dev, device_trust_default_policy,
+					       device_trust_cmdline);
 }
 EXPORT_SYMBOL_GPL(device_trust_init);
diff --git a/include/linux/device/trust.h b/include/linux/device/trust.h
index 6ce0fcd00caf..f39523df03ae 100644
--- a/include/linux/device/trust.h
+++ b/include/linux/device/trust.h
@@ -40,6 +40,7 @@ enum device_trust_policy {
 	DEVICE_TRUST_POLICY_UNRESTRICTED,
 };
 
+bool device_trust_enabled(void);
 void device_trust_init(struct device *dev);
 int device_trust_prepare(struct device *dev, const struct device_driver *drv);
 void device_trust_clear(struct device *dev);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 24/33] driver core: Accept device trust policy by driver name
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (22 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 23/33] driver core: Accept device trust policy on the kernel command line Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 25/33] driver core: Expose committed device trust level Leon Romanovsky
                   ` (8 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Device names can depend on enumeration order, making device-specific boot
exceptions difficult to reuse across machines. Userspace is also too late
to establish policy for drivers that probe during boot.

Allow exceptions selected by bus and driver name, using the names published
in sysfs so policy does not depend on module aliases. Preserve more
specific device rules and prior authorization. Keep a matched rule fixed
across retries so changing the candidate driver cannot replace that choice.
When several drivers match a device, reject a later candidate whose own
rule conflicts with that choice rather than operate the device against
the rule.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-devices-trust   |   7 +-
 Documentation/admin-guide/kernel-parameters.txt |  15 ++-
 drivers/base/base.h                             |  14 +++
 drivers/base/trust.c                            | 124 ++++++++++++++++++++++--
 4 files changed, 148 insertions(+), 12 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
index e8629760a1c8..7ce5a2202dd7 100644
--- a/Documentation/ABI/testing/sysfs-devices-trust
+++ b/Documentation/ABI/testing/sysfs-devices-trust
@@ -15,8 +15,11 @@ Description:
 		value may be selected by the bus before the device is
 		registered; otherwise it is "bus". The policy is resolved
 		to an active trust level before the device is probed.
-		A matching device_trust= kernel command-line entry overrides
-		the initial requested policy before registration.
+		A device_trust.default= kernel command-line value other than
+		"bus" replaces the bus policy before registration. A matching
+		device_trust= entry takes precedence over both, regardless
+		of command-line order. Boot policy also applies to new devices
+		discovered after boot.
 
 		Changes through this attribute require an explicit device
 		trust boot option. Any such option selects an implicit
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 466665fc1262..96f1a38b49b9 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1269,12 +1269,15 @@ Kernel parameters
 			Names match exactly as in /sys/bus/<bus>/devices/<device>.
 			Policies are bus, blocked, adversarial, and
 			unrestricted.
-			Entries override bus policy and device_trust.default.
+			Entries override bus policy, device_trust.default, and
+			device_trust.driver.
 			See Documentation/driver-api/driver-model/device-trust.rst.
 
 	device_trust.default=
 			[KNL] Set initial trust policy for devices without a
 			matching device_trust= entry, including hotplugged devices.
+			A matching device_trust.driver= rule can replace this
+			initial policy at the first binding attempt.
 			Format: { bus | blocked | adversarial | unrestricted }
 			No trust options selects legacy unrestricted mode, preserving
 			bus policy and existing authorization. Otherwise blocked is the
@@ -1283,6 +1286,16 @@ Kernel parameters
 			unrestricted disables runtime policy changes.
 			See Documentation/driver-api/driver-model/device-trust.rst.
 
+	device_trust.driver=
+			[KNL] Set initial device trust policy by driver name,
+			including for drivers built into the kernel.
+			Format: <bus>:<driver>=<policy>[,...]
+			Names match exactly as in /sys/bus/<bus>/drivers/<driver>.
+			Quote the option value for driver names containing spaces.
+			Policies are bus, blocked, adversarial, and
+			unrestricted.
+			Rules remain available for hotplug and modules.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 761d52f4c9ca..7e7a805885fa 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -129,6 +129,8 @@ enum device_trust_state {
  *	    associated with.
  * @driver_lock: serializes binding and removal, including compatibility attach
  * @trust_state: policy admission state
+ * @trust_policy_override: explicit policy prevents deferred driver boot policy
+ * @trust_driver_rule: the explicit policy came from a driver boot rule
  * @dead: This device is currently either in the process of or has been
  *	  removed from the system. Any asynchronous events scheduled for this
  *	  device should exit without taking any action.
@@ -148,6 +150,8 @@ struct device_private {
 	/* Taken after the device mutex, except by compatibility attach. */
 	struct mutex driver_lock;
 	enum device_trust_state trust_state;
+	u8 trust_policy_override:1;
+	u8 trust_driver_rule:1;
 	u8 dead:1;
 };
 #define to_device_private_parent(obj)	\
@@ -200,6 +204,16 @@ extern const struct attribute_group device_trust_attr_group;
 #if IS_ENABLED(CONFIG_KUNIT)
 int device_trust_parse_cmdline(const char *options, const struct device *dev,
 			       enum device_trust_policy *policy);
+int device_trust_parse_driver_cmdline(const char *options,
+				      const struct device_driver *drv,
+				      enum device_trust_policy *policy);
+void device_trust_apply_driver_policy(struct device *dev,
+				      const struct device_driver *drv,
+				      const char *options);
+int device_trust_select_driver_policy(struct device *dev,
+				      const struct device_driver *drv,
+				      const char *options,
+				      enum device_trust_policy *policy);
 void device_trust_apply_boot_policy(struct device *dev,
 				    enum device_trust_policy policy,
 				    const char *options);
diff --git a/drivers/base/trust.c b/drivers/base/trust.c
index 103b8d00eb84..8ed414d6eb0e 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -43,6 +43,12 @@ static int __init device_trust_detect_setup(char *str)
 }
 __setup("device_trust", device_trust_detect_setup);
 
+static bool device_trust_unrestricted_allowed(const struct device *dev);
+
+static int device_trust_init_driver_policy(struct device *dev,
+					   const struct device_driver *drv,
+					   enum device_trust_policy *policy);
+
 void device_trust_init_state(struct device *dev)
 {
 	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
@@ -59,8 +65,9 @@ static int __device_trust_prepare(struct device *dev,
 	enum device_trust_level level;
 	int ret;
 
-	policy = dev->trust_policy;
-
+	ret = device_trust_init_driver_policy(dev, drv, &policy);
+	if (ret)
+		return ret;
 	if (policy == DEVICE_TRUST_POLICY_BLOCKED)
 		return -EPERM;
 	if (dev->bus && dev->bus->trust_resolve) {
@@ -115,7 +122,7 @@ EXPORT_SYMBOL_GPL(device_trust_prepare);
 int device_trust_prepare_unrestricted(struct device *dev,
 				      const struct device_driver *drv)
 {
-	if (device_trust_default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+	if (!device_trust_unrestricted_allowed(dev))
 		return -EPERM;
 
 	return __device_trust_prepare(dev, drv, true);
@@ -236,6 +243,8 @@ int device_trust_set_policy_locked(struct device *dev,
 	}
 set_policy:
 	WRITE_ONCE(dev->trust_policy, policy);
+	dev->p->trust_policy_override = true;
+	dev->p->trust_driver_rule = false;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(device_trust_set_policy_locked);
@@ -299,10 +308,11 @@ const struct attribute_group device_trust_attr_group = {
 };
 
 static const char *device_trust_cmdline __ro_after_init;
+static const char *device_trust_driver_cmdline __ro_after_init;
 
 static int device_trust_parse_policy(const char *options, const char *target_bus,
 				     const char *target_name,
-				     enum device_trust_policy *policy)
+				     enum device_trust_policy *policy, bool driver_name)
 {
 	enum device_trust_policy selected = DEVICE_TRUST_POLICY_BUS;
 	const char *entry = options;
@@ -326,7 +336,8 @@ static int device_trust_parse_policy(const char *options, const char *target_bus
 			return -EINVAL;
 
 		for (p = entry; p < end; p++)
-			if (isspace(*p) || iscntrl(*p) || *p == '"')
+			if (iscntrl(*p) || *p == '"' ||
+			    (isspace(*p) && (!driver_name || p < name || p >= level)))
 				return -EINVAL;
 
 		for (i = 0; i < ARRAY_SIZE(device_trust_policy_names); i++)
@@ -361,10 +372,89 @@ VISIBLE_IF_KUNIT int device_trust_parse_cmdline(const char *options,
 						enum device_trust_policy *policy)
 {
 	return device_trust_parse_policy(options, dev && dev->bus ? dev->bus->name : NULL,
-					 dev ? dev_name(dev) : NULL, policy);
+					 dev ? dev_name(dev) : NULL, policy, false);
 }
 EXPORT_SYMBOL_IF_KUNIT(device_trust_parse_cmdline);
 
+VISIBLE_IF_KUNIT int device_trust_parse_driver_cmdline(const char *options,
+						       const struct device_driver *drv,
+						       enum device_trust_policy *policy)
+{
+	return device_trust_parse_policy(options, drv ? drv->bus->name : NULL,
+					 drv ? kobject_name(&drv->p->kobj) : NULL,
+					 policy, true);
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_parse_driver_cmdline);
+
+static bool device_trust_unrestricted_allowed(const struct device *dev)
+{
+	if (device_trust_default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+		return false;
+
+	/* Driver rules can otherwise change policy outside subsystem locks. */
+	return !device_trust_driver_cmdline ||
+		(device_trust_cmdline &&
+		 device_trust_parse_cmdline(device_trust_cmdline, dev, NULL) > 0);
+}
+
+VISIBLE_IF_KUNIT void device_trust_apply_driver_policy(struct device *dev,
+						     const struct device_driver *drv,
+						     const char *options)
+{
+	enum device_trust_policy policy;
+
+	if (!options || dev->p->trust_policy_override ||
+	    (dev->p->trust_state != DEVICE_TRUST_SELECTABLE &&
+	     dev->p->trust_state != DEVICE_TRUST_WAITING))
+		return;
+
+	if (device_trust_parse_driver_cmdline(options, drv, &policy) <= 0)
+		return;
+
+	WRITE_ONCE(dev->trust_policy, policy);
+	dev->p->trust_state = policy == DEVICE_TRUST_POLICY_BLOCKED ?
+		DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
+	dev->p->trust_policy_override = true;
+	dev->p->trust_driver_rule = true;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_driver_policy);
+
+/*
+ * A policy fixed by an earlier candidate driver, through its rule or a
+ * commitment, cannot change. Reject a candidate whose own rule disagrees
+ * instead of operating the device against that rule.
+ */
+VISIBLE_IF_KUNIT int device_trust_select_driver_policy(struct device *dev,
+						       const struct device_driver *drv,
+						       const char *options,
+						       enum device_trust_policy *policy)
+{
+	enum device_trust_policy rule;
+
+	device_trust_apply_driver_policy(dev, drv, options);
+	*policy = dev->trust_policy;
+	if (!options ||
+	    (dev->p->trust_policy_override && !dev->p->trust_driver_rule) ||
+	    device_trust_parse_driver_cmdline(options, drv, &rule) <= 0)
+		return 0;
+
+	if (dev->p->trust_driver_rule)
+		return rule == *policy ? 0 : -EPERM;
+
+	/* Committed without a rule: resolution must match the commitment. */
+	*policy = rule;
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_select_driver_policy);
+
+static int device_trust_init_driver_policy(struct device *dev,
+					   const struct device_driver *drv,
+					   enum device_trust_policy *policy)
+{
+	return device_trust_select_driver_policy(dev, drv, device_trust_driver_cmdline,
+						 policy);
+}
+
 static int __init device_trust_setup(char *str)
 {
 	device_trust_configure();
@@ -379,6 +469,19 @@ static int __init device_trust_setup(char *str)
 }
 __setup("device_trust=", device_trust_setup);
 
+static int __init device_trust_driver_setup(char *str)
+{
+	device_trust_configure();
+	if (device_trust_parse_driver_cmdline(str, NULL, NULL) < 0) {
+		pr_warn("device_trust: ignoring invalid driver policy list '%s'\n", str);
+		return 1;
+	}
+
+	device_trust_driver_cmdline = str;
+	return 1;
+}
+__setup("device_trust.driver=", device_trust_driver_setup);
+
 static int __init device_trust_default_setup(char *str)
 {
 	int policy = match_string(device_trust_policy_names,
@@ -401,8 +504,10 @@ VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,
 {
 	if (policy != DEVICE_TRUST_POLICY_BUS)
 		dev->trust_policy = policy;
-	if (options)
-		device_trust_parse_cmdline(options, dev, &dev->trust_policy);
+	if (options &&
+	    device_trust_parse_cmdline(options, dev, &dev->trust_policy) > 0 &&
+	    dev->p)
+		dev->p->trust_policy_override = true;
 }
 EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_boot_policy);
 
@@ -412,7 +517,8 @@ EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_boot_policy);
  */
 void device_trust_init(struct device *dev)
 {
-	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
+	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED ||
+	    (dev->p && dev->p->trust_policy_override))
 		return;
 
 	if (device_trust_configured)

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 25/33] driver core: Expose committed device trust level
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (23 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 24/33] driver core: Accept device trust policy by driver name Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 26/33] driver core: Cover trust authorization and binding with KUnit Leon Romanovsky
                   ` (7 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

After probe failure or unbind, active trust returns to blocked while the
accepted operating level remains committed. IOMMU state and other
infrastructure can outlive the driver, so active trust alone cannot tell
them which operating level was accepted.

Expose the retained commitment separately. This lets infrastructure honor
the original level after probe failure or unbind without mistaking inactive
driver state for a new authorization opportunity.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/base/trust.c         | 13 +++++++++++++
 include/linux/device/trust.h |  1 +
 2 files changed, 14 insertions(+)

diff --git a/drivers/base/trust.c b/drivers/base/trust.c
index 8ed414d6eb0e..9f7478bd7a6d 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -157,6 +157,19 @@ enum device_trust_level device_get_trust_level(const struct device *dev)
 }
 EXPORT_SYMBOL_GPL(device_get_trust_level);
 
+/**
+ * device_get_committed_trust_level - Return the lifetime trust commitment
+ * @dev: device to inspect
+ *
+ * Return: %DEVICE_TRUST_BLOCKED before admission, otherwise the level fixed
+ * before driver setup. The commitment survives setup failure and unbind.
+ */
+enum device_trust_level device_get_committed_trust_level(const struct device *dev)
+{
+	return READ_ONCE(dev->trust_committed_level);
+}
+EXPORT_SYMBOL_GPL(device_get_committed_trust_level);
+
 /**
  * device_is_adversarial - Test whether a device is operated as an adversary
  * @dev: device to inspect
diff --git a/include/linux/device/trust.h b/include/linux/device/trust.h
index f39523df03ae..250cf4004193 100644
--- a/include/linux/device/trust.h
+++ b/include/linux/device/trust.h
@@ -48,6 +48,7 @@ int device_trust_set_policy_locked(struct device *dev,
 				   enum device_trust_policy policy);
 int device_trust_set_policy(struct device *dev, enum device_trust_policy policy);
 enum device_trust_level device_get_trust_level(const struct device *dev);
+enum device_trust_level device_get_committed_trust_level(const struct device *dev);
 bool device_is_adversarial(const struct device *dev);
 
 #endif /* _DEVICE_TRUST_H_ */

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 26/33] driver core: Cover trust authorization and binding with KUnit
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (24 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 25/33] driver core: Expose committed device trust level Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 27/33] driver core: Document trust admission and initrd authorization Leon Romanovsky
                   ` (6 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Authorization, committed trust and active trust change at different points
in a device's lifetime. Confusing them can reopen authorization after a
failed probe or let cleanup erase another binding's state. Manual
attachment also uses different locks from normal probing.

Add KUnit coverage for these boundaries and for boot policy precedence. Use
controlled overlap to check binding serialization, since sequential
operations cannot reveal interference between attachment and removal.
Isolate policy changes from external sysfs writes so unrelated userspace
activity cannot alter the expected result.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/base/test/Makefile            |    1 +
 drivers/base/test/device-trust-test.c | 2609 +++++++++++++++++++++++++++++++++
 drivers/base/trust.c                  |    2 +
 3 files changed, 2612 insertions(+)

diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile
index 9ced7bbd569f..dfff5e1d83f7 100644
--- a/drivers/base/test/Makefile
+++ b/drivers/base/test/Makefile
@@ -3,6 +3,7 @@ obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE)	+= test_async_driver_probe.o
 
 obj-$(CONFIG_DM_KUNIT_TEST)	+= root-device-test.o
 obj-$(CONFIG_DM_KUNIT_TEST)	+= platform-device-test.o
+obj-$(CONFIG_DM_KUNIT_TEST)	+= device-trust-test.o
 
 obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o
 CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
diff --git a/drivers/base/test/device-trust-test.c b/drivers/base/test/device-trust-test.c
new file mode 100644
index 000000000000..eab29c41ef3b
--- /dev/null
+++ b/drivers/base/test/device-trust-test.c
@@ -0,0 +1,2609 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <kunit/platform_device.h>
+#include <kunit/resource.h>
+#include <kunit/test.h>
+
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/sched/task.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+
+#include "../base.h"
+
+struct device_trust_test_sysfs {
+	struct kobj_type type;
+	struct sysfs_ops ops;
+	const struct kobj_type *original;
+	struct completion *released;
+};
+
+static ssize_t device_trust_test_sysfs_store(struct kobject *kobj,
+					     struct attribute *attr,
+					     const char *buf, size_t count)
+{
+	const struct device_trust_test_sysfs *sysfs =
+		container_of(kobj->ktype, struct device_trust_test_sysfs, type);
+
+	if (!strcmp(attr->name, "trust_policy"))
+		return -EPERM;
+	return sysfs->original->sysfs_ops->store(kobj, attr, buf, count);
+}
+
+static void device_trust_test_sysfs_release(struct kobject *kobj)
+{
+	const struct device_trust_test_sysfs *sysfs =
+		container_of(kobj->ktype, struct device_trust_test_sysfs, type);
+	const struct kobj_type *original = sysfs->original;
+	struct completion *released = sysfs->released;
+
+	kobj->ktype = original;
+	kfree(sysfs);
+	original->release(kobj);
+	complete(released);
+}
+
+static int device_trust_test_isolate_sysfs(struct device *dev,
+					   struct completion *released)
+{
+	struct device_trust_test_sysfs *sysfs;
+
+	sysfs = kzalloc_obj(*sysfs);
+	if (!sysfs)
+		return -ENOMEM;
+	sysfs->original = dev->kobj.ktype;
+	sysfs->released = released;
+	sysfs->type = *sysfs->original;
+	sysfs->ops = *sysfs->original->sysfs_ops;
+	sysfs->ops.store = device_trust_test_sysfs_store;
+	sysfs->type.sysfs_ops = &sysfs->ops;
+	sysfs->type.release = device_trust_test_sysfs_release;
+	reinit_completion(released);
+	dev->kobj.ktype = &sysfs->type;
+	return 0;
+}
+
+/*
+ * A test device is freed when the last reference to it is dropped, which
+ * deferred probing, device links and delayed kobject release may do after the
+ * test. Wait for that so its release callbacks never outlive the test.
+ */
+static void device_trust_test_wait_released(void *data)
+{
+	struct completion *released = data;
+
+	if (!wait_for_completion_timeout(released, 10 * HZ)) {
+		WARN(1, "device-trust test device is still referenced\n");
+		wait_for_completion(released);
+	}
+}
+
+static struct completion *device_trust_test_released_alloc(struct kunit *test)
+{
+	struct completion *released;
+	int ret;
+
+	released = kunit_kzalloc(test, sizeof(*released), GFP_KERNEL);
+	if (!released)
+		return ERR_PTR(-ENOMEM);
+	init_completion(released);
+	/* Nothing is pending until a device is isolated. */
+	complete(released);
+
+	ret = kunit_add_action_or_reset(test, device_trust_test_wait_released,
+					released);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return released;
+}
+
+struct device_trust_test_context {
+	struct device dev;
+	struct device_driver drv;
+
+	enum device_trust_level resolve_level;
+	enum device_trust_policy resolve_policy;
+	enum device_trust_level configure_level;
+	enum device_trust_level probe_level;
+	enum device_trust_level probe_exit_level;
+	enum device_trust_level remove_level;
+	enum device_trust_level cleanup_level;
+	const struct device_driver *resolve_driver;
+	const struct device_driver *reject_driver;
+	struct completion *probe_entered;
+	struct completion *probe_continue;
+
+	int match_ret;
+	int resolve_ret;
+	int configure_ret;
+	int probe_ret;
+	unsigned int resolve_count;
+	unsigned int configure_count;
+	unsigned int probe_count;
+	unsigned int remove_count;
+	unsigned int cleanup_count;
+	unsigned int release_count;
+	bool probe_adversarial;
+	bool device_registered;
+	bool driver_registered;
+};
+
+static struct device_trust_test_context *
+to_device_trust_test_context(struct device *dev)
+{
+	return container_of(dev, struct device_trust_test_context, dev);
+}
+
+static int device_trust_test_match(struct device *dev,
+				   const struct device_driver *drv)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	return READ_ONCE(ctx->match_ret);
+}
+
+static int device_trust_test_resolve(struct device *dev,
+				     const struct device_driver *drv,
+				     enum device_trust_policy policy,
+				     enum device_trust_level *level)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	ctx->resolve_count++;
+	ctx->resolve_policy = policy;
+	ctx->resolve_driver = drv;
+	if (ctx->reject_driver && drv == ctx->reject_driver)
+		return -EPERM;
+	if (ctx->resolve_ret)
+		return ctx->resolve_ret;
+
+	*level = ctx->resolve_level;
+	return 0;
+}
+
+static int device_trust_test_dma_configure(struct device *dev)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	ctx->configure_count++;
+	ctx->configure_level = device_get_trust_level(dev);
+	return ctx->configure_ret;
+}
+
+static void device_trust_test_dma_cleanup(struct device *dev)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	if (dev->parent)
+		device_lock_assert(dev->parent);
+	ctx->cleanup_count++;
+	ctx->cleanup_level = device_get_trust_level(dev);
+}
+
+static int device_trust_test_probe(struct device *dev)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	if (dev->parent)
+		device_lock_assert(dev->parent);
+	ctx->probe_count++;
+	ctx->probe_level = device_get_trust_level(dev);
+	ctx->probe_adversarial = device_is_adversarial(dev);
+	if (ctx->probe_entered) {
+		complete(ctx->probe_entered);
+		wait_for_completion(ctx->probe_continue);
+	}
+	ctx->probe_exit_level = device_get_trust_level(dev);
+	return ctx->probe_ret;
+}
+
+static int device_trust_test_remove(struct device *dev)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+
+	if (dev->parent)
+		device_lock_assert(dev->parent);
+	ctx->remove_count++;
+	ctx->remove_level = device_get_trust_level(dev);
+	return 0;
+}
+
+static const struct bus_type device_trust_test_plain_bus = {
+	.name = "device-trust-test-plain",
+	.match = device_trust_test_match,
+	.dma_configure = device_trust_test_dma_configure,
+	.dma_cleanup = device_trust_test_dma_cleanup,
+};
+
+static const struct bus_type device_trust_test_resolving_bus = {
+	.name = "device-trust-test-resolving",
+	.match = device_trust_test_match,
+	.trust_resolve = device_trust_test_resolve,
+	.dma_configure = device_trust_test_dma_configure,
+	.dma_cleanup = device_trust_test_dma_cleanup,
+};
+
+static const struct device_type device_trust_test_child_type = {
+	.need_parent_lock = true,
+};
+
+static void device_trust_test_release(struct device *dev)
+{
+	kfree(to_device_trust_test_context(dev));
+}
+
+static void device_trust_test_device_unregister(void *data)
+{
+	struct device_trust_test_context *ctx = data;
+
+	if (ctx->device_registered)
+		device_unregister(&ctx->dev);
+	else
+		put_device(&ctx->dev);
+}
+
+static void device_trust_test_driver_unregister(void *data)
+{
+	struct device_trust_test_context *ctx = data;
+
+	if (!ctx->driver_registered)
+		return;
+
+	driver_unregister(&ctx->drv);
+}
+
+static struct device_trust_test_context *
+device_trust_test_device_alloc(struct kunit *test, const struct bus_type *bus,
+			       enum device_trust_policy policy, struct device *parent)
+{
+	struct device_trust_test_context *ctx;
+	struct completion *released;
+	int ret;
+
+	released = device_trust_test_released_alloc(test);
+	if (IS_ERR(released))
+		return ERR_CAST(released);
+
+	ctx = kzalloc_obj(*ctx);
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	ctx->match_ret = 1;
+	ctx->resolve_level = DEVICE_TRUST_UNRESTRICTED;
+	ctx->drv.probe_type = PROBE_FORCE_SYNCHRONOUS;
+	ctx->drv.suppress_bind_attrs = true;
+	device_initialize(&ctx->dev);
+	ctx->dev.bus = bus;
+	ctx->dev.parent = parent;
+	if (parent && bus == &device_trust_test_plain_bus)
+		ctx->dev.type = &device_trust_test_child_type;
+	ctx->dev.release = device_trust_test_release;
+	ctx->dev.trust_policy = policy;
+
+	ret = kunit_add_action_or_reset(test,
+					device_trust_test_device_unregister, ctx);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = device_trust_test_isolate_sysfs(&ctx->dev, released);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = dev_set_name(&ctx->dev, "%s-%s",
+			   bus ? bus->name : "device-trust-test-busless",
+			   test->name);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return ctx;
+}
+
+static struct device_trust_test_context *
+device_trust_test_device_register_parent(struct kunit *test,
+						 const struct bus_type *bus,
+						 enum device_trust_policy policy,
+						 enum device_trust_policy boot_policy,
+						 struct device *parent)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_alloc(test, bus, policy, parent);
+	if (IS_ERR(ctx))
+		return ctx;
+	device_trust_apply_boot_policy(&ctx->dev, boot_policy, NULL);
+	ret = device_add(&ctx->dev);
+	if (ret)
+		return ERR_PTR(ret);
+	ctx->device_registered = true;
+
+	return ctx;
+}
+
+static struct device_trust_test_context *
+device_trust_test_device_register(struct kunit *test,
+				  const struct bus_type *bus,
+				  enum device_trust_policy policy)
+{
+	return device_trust_test_device_register_parent(test, bus, policy,
+						       DEVICE_TRUST_POLICY_BUS, NULL);
+}
+
+static int device_trust_test_driver_register_named(struct kunit *test,
+						   struct device_trust_test_context *ctx,
+						   const char *name)
+{
+	int ret;
+
+	ctx->drv.name = name;
+	ctx->drv.bus = ctx->dev.bus ?: &device_trust_test_plain_bus;
+	ctx->drv.probe = device_trust_test_probe;
+	ctx->drv.remove = device_trust_test_remove;
+
+	ret = kunit_add_action_or_reset(test,
+					device_trust_test_driver_unregister, ctx);
+	if (ret)
+		return ret;
+
+	ret = driver_register(&ctx->drv);
+	if (ret)
+		return ret;
+	ctx->driver_registered = true;
+
+	return 0;
+}
+
+static int device_trust_test_driver_register(struct kunit *test,
+					     struct device_trust_test_context *ctx)
+{
+	return device_trust_test_driver_register_named(test, ctx, test->name);
+}
+
+static void device_trust_expect_policy_locked(struct kunit *test,
+					      struct device *dev)
+{
+	enum device_trust_policy original = dev->trust_policy;
+	enum device_trust_level active = device_get_trust_level(dev);
+	enum device_trust_policy policy;
+
+	for (policy = DEVICE_TRUST_POLICY_BUS;
+	     policy <= DEVICE_TRUST_POLICY_UNRESTRICTED; policy++) {
+		KUNIT_EXPECT_EQ(test, device_trust_set_policy(dev, policy),
+				policy == original ? 0 : -EBUSY);
+		KUNIT_EXPECT_EQ(test, dev->trust_policy, original);
+		KUNIT_EXPECT_EQ(test, device_get_trust_level(dev), active);
+	}
+}
+
+static void device_trust_default_bind_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_FALSE(test, ctx->probe_adversarial);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_bus_adversarial_bind_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus,
+		DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->resolve_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->resolve_policy,
+			DEVICE_TRUST_POLICY_BUS);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->resolve_driver, &ctx->drv);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_TRUE(test, ctx->probe_adversarial);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+}
+
+static void device_trust_test_other_driver_unregister(void *data)
+{
+	driver_unregister(data);
+}
+
+static void device_trust_default_pinned_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	struct device_driver *other;
+	int ret;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_resolving_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	device_release_driver(&ctx->dev);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	other = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, other);
+	other->name = "device-trust-pinned-other";
+	other->bus = ctx->dev.bus;
+	other->probe = device_trust_test_probe;
+	other->remove = device_trust_test_remove;
+	other->probe_type = PROBE_FORCE_SYNCHRONOUS;
+	other->suppress_bind_attrs = true;
+	ctx->resolve_level = DEVICE_TRUST_UNRESTRICTED;
+	KUNIT_ASSERT_EQ(test, driver_register(other), 0);
+	ret = kunit_add_action_or_reset(test,
+					device_trust_test_other_driver_unregister, other);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, device_driver_attach(other, &ctx->dev), -EINVAL);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->resolve_driver, other);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_BUS);
+
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	KUNIT_ASSERT_EQ(test, device_driver_attach(other, &ctx->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, other);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_authorize_test(struct kunit *test,
+					enum device_trust_policy initial_policy,
+					enum device_trust_policy boot_policy,
+					enum device_trust_policy policy,
+					enum device_trust_level level)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register_parent(
+		test, &device_trust_test_plain_bus, initial_policy, boot_policy, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	KUNIT_ASSERT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_BLOCKED);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	ret = device_driver_attach(&ctx->drv, &ctx->dev);
+	KUNIT_EXPECT_EQ(test, ret, -EPERM);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_TRUE(test, dev_can_match(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BLOCKED), 0);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BUS), -EBUSY);
+
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev, policy), 0);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, level);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, level);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, level);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			level);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_blocked_bind_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_BLOCKED,
+				    DEVICE_TRUST_POLICY_BUS, DEVICE_TRUST_POLICY_ADVERSARIAL,
+				    DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_blocked_unrestricted_bind_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_BLOCKED,
+				    DEVICE_TRUST_POLICY_BUS, DEVICE_TRUST_POLICY_UNRESTRICTED,
+				    DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_global_blocked_adversarial_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARIAL,
+				    DEVICE_TRUST_POLICY_BLOCKED, DEVICE_TRUST_POLICY_ADVERSARIAL,
+				    DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_global_blocked_unrestricted_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_UNRESTRICTED,
+				    DEVICE_TRUST_POLICY_BLOCKED, DEVICE_TRUST_POLICY_UNRESTRICTED,
+				    DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_test_link_wait(void *data)
+{
+	device_link_wait_removal();
+}
+
+static void device_trust_supplier_retry_test(struct kunit *test)
+{
+	struct device_trust_test_context *supplier, *consumer;
+	struct device_link *link;
+	int ret;
+
+	supplier = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						     DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, supplier);
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, supplier), 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&supplier->dev));
+	/* Runs after the consumer is gone and before the supplier goes. */
+	ret = kunit_add_action_or_reset(test, device_trust_test_link_wait, NULL);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	consumer = device_trust_test_device_register(test, &device_trust_test_resolving_bus,
+						     DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, consumer);
+	consumer->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	link = device_link_add(&consumer->dev, &supplier->dev, 0);
+	KUNIT_ASSERT_NOT_NULL(test, link);
+
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, consumer), 0);
+	KUNIT_ASSERT_EQ(test, link->status, DL_STATE_AVAILABLE);
+	KUNIT_EXPECT_EQ(test, consumer->dev.links.status, DL_DEV_NO_DRIVER);
+	KUNIT_EXPECT_EQ(test, consumer->configure_count, 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&consumer->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	consumer->resolve_ret = -EAGAIN;
+	KUNIT_EXPECT_EQ(test, device_driver_attach(&consumer->drv, &consumer->dev), -EAGAIN);
+	KUNIT_ASSERT_EQ(test, link->status, DL_STATE_AVAILABLE);
+	KUNIT_EXPECT_EQ(test, consumer->dev.links.status, DL_DEV_NO_DRIVER);
+
+	consumer->resolve_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&consumer->drv, &consumer->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&consumer->dev));
+	KUNIT_EXPECT_EQ(test, link->status, DL_STATE_ACTIVE);
+	device_release_driver(&consumer->dev);
+	KUNIT_ASSERT_EQ(test, link->status, DL_STATE_AVAILABLE);
+	device_release_driver(&supplier->dev);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&supplier->dev));
+}
+
+static void device_trust_match_defer_authorize_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->match_ret = -EPROBE_DEFER;
+
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_EXPECT_TRUE(test, dev_can_match(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	WRITE_ONCE(ctx->match_ret, 1);
+	KUNIT_ASSERT_EQ(test, device_attach(&ctx->dev), 1);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_test_parent_unregister(void *data)
+{
+	root_device_unregister(data);
+}
+
+static void device_trust_async_policy_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->drv.probe_type = PROBE_PREFER_ASYNCHRONOUS;
+
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BLOCKED), 0);
+	ret = device_trust_test_driver_register(test, ctx);
+	wait_for_device_probe();
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_TRUE(test, dev_can_match(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BUS), -EBUSY);
+
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_generic_adversarial_bind_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus,
+		DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_TRUE(test, ctx->probe_adversarial);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+}
+
+static int device_trust_platform_probe(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static void device_trust_no_iommu_bind_test(struct kunit *test)
+{
+	struct completion *released;
+	struct platform_device *pdev;
+	struct platform_driver *drv;
+
+	released = device_trust_test_released_alloc(test);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, released);
+	drv = kunit_kzalloc(test, sizeof(*drv), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, drv);
+	drv->driver.name = test->name;
+	drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
+	drv->driver.suppress_bind_attrs = true;
+	drv->probe = device_trust_platform_probe;
+	pdev = kunit_platform_device_alloc(test, test->name, PLATFORM_DEVID_NONE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+	KUNIT_ASSERT_EQ(test, device_trust_test_isolate_sysfs(&pdev->dev, released), 0);
+	KUNIT_ASSERT_EQ(test, device_set_driver_override(&pdev->dev, "none"), 0);
+	pdev->dev.trust_policy = DEVICE_TRUST_POLICY_BLOCKED;
+	KUNIT_ASSERT_EQ(test, kunit_platform_device_add(test, pdev), 0);
+	KUNIT_ASSERT_PTR_EQ(test, pdev->dev.iommu_group, NULL);
+	KUNIT_ASSERT_EQ(test, kunit_platform_driver_register(test, drv), 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&pdev->dev));
+
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&pdev->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&drv->driver, &pdev->dev), 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&pdev->dev));
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&pdev->dev), DEVICE_TRUST_ADVERSARIAL);
+
+	device_release_driver(&pdev->dev);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&pdev->dev), DEVICE_TRUST_BLOCKED);
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&drv->driver, &pdev->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&pdev->dev));
+}
+
+static void device_trust_resolver_retry_test(struct kunit *test, int error,
+					     enum device_trust_level level)
+{
+	struct device_trust_test_context *ctx;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_resolving_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_ret = error;
+	ctx->resolve_level = level;
+
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_GT(test, ctx->resolve_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_UNRESTRICTED), 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BUS), 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BLOCKED), 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	KUNIT_EXPECT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), error ?: -EPERM);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	ctx->resolve_ret = 0;
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_resolver_failure_test(struct kunit *test)
+{
+	device_trust_resolver_retry_test(test, -EAGAIN, DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_resolver_blocked_test(struct kunit *test)
+{
+	device_trust_resolver_retry_test(test, 0, DEVICE_TRUST_BLOCKED);
+}
+
+static void device_trust_probe_failure_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus,
+		DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	ctx->probe_ret = -EINVAL;
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_probe_defer_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus,
+		DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	ctx->probe_ret = -EPROBE_DEFER;
+	ctx->match_ret = 0;
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), -EAGAIN);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->resolve_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	ctx->probe_ret = 0;
+	ret = device_driver_attach(&ctx->drv, &ctx->dev);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->resolve_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 2);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_ADVERSARIAL);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_dma_failure_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus,
+		DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	ctx->configure_ret = -EIO;
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_ADVERSARIAL);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+
+	ctx->configure_ret = 0;
+	ctx->resolve_level = DEVICE_TRUST_UNRESTRICTED;
+	KUNIT_EXPECT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), -EINVAL);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARIAL;
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_force_bind_guard_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	unsigned int resolve_count;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus,
+		DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	resolve_count = ctx->resolve_count;
+
+	device_lock(&ctx->dev);
+	ctx->dev.driver = &ctx->drv;
+	ret = device_bind_driver(&ctx->dev);
+	ctx->dev.driver = NULL;
+	device_unlock(&ctx->dev);
+
+	KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
+	KUNIT_EXPECT_EQ(test, ctx->resolve_count, resolve_count);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+}
+
+static void device_trust_legacy_force_bind_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus,
+		DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_UNRESTRICTED), 0);
+
+	device_lock(&ctx->dev);
+	ctx->dev.driver = &ctx->drv;
+	ret = device_bind_driver(&ctx->dev);
+	if (ret)
+		ctx->dev.driver = NULL;
+	device_unlock(&ctx->dev);
+
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+}
+
+static void device_trust_busless_force_bind_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, NULL, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	device_lock(&ctx->dev);
+	ctx->dev.driver = &ctx->drv;
+	ret = device_bind_driver(&ctx->dev);
+	if (ret)
+		ctx->dev.driver = NULL;
+	device_unlock(&ctx->dev);
+
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			DEVICE_TRUST_BLOCKED);
+}
+
+static const struct bus_type device_trust_test_pci_bus = { .name = "pci" };
+static const struct bus_type device_trust_test_usb_bus = { .name = "usb" };
+static const struct bus_type device_trust_test_platform_bus = { .name = "platform" };
+
+struct device_trust_test_stub {
+	struct device_driver drv;
+	struct driver_private driver_private;
+	struct device_private private;
+	struct device dev;
+};
+
+/* Stack instances of these objects overflow the frame limit under KASAN. */
+static struct device_trust_test_stub *
+device_trust_test_stub_alloc(struct kunit *test)
+{
+	struct device_trust_test_stub *stub;
+
+	stub = kunit_kzalloc(test, sizeof(*stub), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, stub);
+
+	return stub;
+}
+
+static struct device *
+device_trust_test_stub_reset(struct device_trust_test_stub *stub,
+			     const struct bus_type *bus, const char *name,
+			     enum device_trust_policy policy)
+{
+	memset(stub, 0, sizeof(*stub));
+	stub->drv.bus = bus;
+	stub->drv.name = "test";
+	stub->driver_private.kobj.name = stub->drv.name;
+	stub->drv.p = &stub->driver_private;
+	stub->dev.bus = bus;
+	stub->dev.init_name = name;
+	stub->dev.trust_policy = policy;
+	mutex_init(&stub->dev.mutex);
+
+	return &stub->dev;
+}
+
+static struct device *
+device_trust_test_pci_stub(struct device_trust_test_stub *stub, const char *name,
+			   enum device_trust_policy policy)
+{
+	return device_trust_test_stub_reset(stub, &device_trust_test_pci_bus, name,
+					    policy);
+}
+
+static void device_trust_cmdline_policies_test(struct kunit *test)
+{
+	static const char options[] =
+		"pci:0000:01:00.0=adversarial,usb:1-1=unrestricted,usb:1-2=blocked,platform:i8042=bus";
+	static const struct {
+		const struct bus_type *bus;
+		const char *name;
+		enum device_trust_policy policy;
+	} cases[] = {
+		{ &device_trust_test_pci_bus, "0000:01:00.0", DEVICE_TRUST_POLICY_ADVERSARIAL },
+		{ &device_trust_test_usb_bus, "1-1", DEVICE_TRUST_POLICY_UNRESTRICTED },
+		{ &device_trust_test_usb_bus, "1-2", DEVICE_TRUST_POLICY_BLOCKED },
+		{ &device_trust_test_platform_bus, "i8042", DEVICE_TRUST_POLICY_BUS },
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		enum device_trust_policy policy = DEVICE_TRUST_POLICY_BLOCKED;
+		struct device *dev = device_trust_test_stub_reset(stub, cases[i].bus,
+								  cases[i].name,
+								  DEVICE_TRUST_POLICY_BUS);
+		int ret;
+
+		ret = device_trust_parse_cmdline(options, dev, &policy);
+		KUNIT_EXPECT_EQ_MSG(test, ret, 1, "case %u", i);
+		KUNIT_EXPECT_EQ_MSG(test, policy, cases[i].policy, "case %u", i);
+	}
+}
+
+static void device_trust_cmdline_exact_match_test(struct kunit *test)
+{
+	static const char * const options[] = {
+		"pcie:0000:01:00.0=unrestricted",
+		"pc:0000:01:00.0=unrestricted",
+		"usb:0000:01:00.0=unrestricted",
+		"pci:0000:01:00.01=unrestricted",
+		"pci:0000:01:00=unrestricted",
+		"pci:0000:01:00.1=unrestricted",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, "0000:01:00.0",
+							DEVICE_TRUST_POLICY_BUS);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(options); i++) {
+		enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
+		int ret;
+
+		ret = device_trust_parse_cmdline(options[i], dev, &policy);
+		KUNIT_EXPECT_EQ_MSG(test, ret, 0, "%s", options[i]);
+		KUNIT_EXPECT_EQ_MSG(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL,
+				    "%s", options[i]);
+	}
+}
+
+static void device_trust_cmdline_last_match_test(struct kunit *test)
+{
+	static const char options[] =
+		"pci:0000:01:00.0=blocked,usb:1-1=unrestricted,pci:0000:01:00.0=unrestricted,usb:1-2=blocked";
+	static const char defaults[] =
+		"pci:0000:01:00.0=blocked,pci:0000:01:00.0=bus";
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, "0000:01:00.0",
+							DEVICE_TRUST_POLICY_BUS);
+	enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
+	int ret;
+
+	ret = device_trust_parse_cmdline(options, dev, &policy);
+	KUNIT_ASSERT_EQ(test, ret, 1);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+
+	ret = device_trust_parse_cmdline(defaults, dev, &policy);
+	KUNIT_ASSERT_EQ(test, ret, 1);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_BUS);
+}
+
+static void device_trust_cmdline_nameless_test(struct kunit *test)
+{
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, NULL,
+							DEVICE_TRUST_POLICY_BUS);
+	enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
+	int ret;
+
+	ret = device_trust_parse_cmdline("pci:0000:01:00.0=unrestricted", dev, &policy);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+}
+
+static void device_trust_cmdline_validate_test(struct kunit *test)
+{
+	static const char valid[] = "pci:0000:01:00.0=adversarial,usb:1-2=blocked";
+	static const char invalid[] = "pci:0000:01:00.0=adversarial,usb:1-2=invalid";
+
+	KUNIT_EXPECT_EQ(test, device_trust_parse_cmdline(valid, NULL, NULL), 0);
+	KUNIT_EXPECT_EQ(test, device_trust_parse_cmdline(invalid, NULL, NULL), -EINVAL);
+}
+
+static void device_trust_cmdline_invalid_test(struct kunit *test)
+{
+	static const char * const options[] = {
+		"",
+		",pci:0000:01:00.0=unrestricted",
+		"pci:0000:01:00.0=unrestricted,",
+		"pci:0000:01:00.0=unrestricted,,usb:1-2=blocked",
+		":0000:01:00.0=unrestricted",
+		"pci:=unrestricted",
+		"pci:0000:01:00.0=",
+		"pci=unrestricted",
+		"pci=bad:0000:01:00.0=unrestricted",
+		"pci:0000:01:00.0",
+		"pci:0000:01:00.0=unknown",
+		"pci:0000:01:00.0=unrestricted=blocked",
+		"pc i:0000:01:00.0=unrestricted",
+		"pci:0000:01:00.0 =unrestricted",
+		"pci:0000:01:00.0= unrestricted",
+		"pci:0000:01:00.0=unrestricted\n",
+		"pci:0000:01:00.0\t=unrestricted",
+		"pci:0000:01:00.0\001=unrestricted",
+		"p\"ci:0000:01:00.0=unrestricted",
+		"pci:0000:01:00.0=\"unrestricted\"",
+		"pci:0000:01:00.0=unrestricted,usb:1-2=unknown",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, "0000:01:00.0",
+							DEVICE_TRUST_POLICY_BUS);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(options); i++) {
+		enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
+		int ret;
+
+		ret = device_trust_parse_cmdline(options[i], dev, &policy);
+		KUNIT_EXPECT_EQ_MSG(test, ret, -EINVAL, "case %u", i);
+		KUNIT_EXPECT_EQ_MSG(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL,
+				    "case %u", i);
+	}
+}
+
+static void device_trust_boot_precedence_test(struct kunit *test)
+{
+	static const char * const overrides[] = {
+		[DEVICE_TRUST_POLICY_BUS] = "pci:0000:01:00.0=bus",
+		[DEVICE_TRUST_POLICY_BLOCKED] = "pci:0000:01:00.0=blocked",
+		[DEVICE_TRUST_POLICY_ADVERSARIAL] = "pci:0000:01:00.0=adversarial",
+		[DEVICE_TRUST_POLICY_UNRESTRICTED] = "pci:0000:01:00.0=unrestricted",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	unsigned int initial, global, override;
+
+	for (initial = DEVICE_TRUST_POLICY_BUS;
+	     initial <= DEVICE_TRUST_POLICY_UNRESTRICTED; initial++) {
+		for (global = DEVICE_TRUST_POLICY_BUS;
+		     global <= DEVICE_TRUST_POLICY_UNRESTRICTED; global++) {
+			struct device *dev = device_trust_test_pci_stub(stub, "0000:01:00.0",
+									initial);
+			enum device_trust_policy expected = global ?: initial;
+
+			device_trust_apply_boot_policy(dev, global, NULL);
+			KUNIT_EXPECT_EQ(test, dev->trust_policy, expected);
+			device_trust_apply_boot_policy(dev, global,
+						       "pci:0000:02:00.0=unrestricted");
+			KUNIT_EXPECT_EQ(test, dev->trust_policy, expected);
+			for (override = 0; override < ARRAY_SIZE(overrides); override++) {
+				dev->trust_policy = initial;
+				device_trust_apply_boot_policy(dev, global,
+							       overrides[override]);
+				KUNIT_EXPECT_EQ(test, dev->trust_policy, override);
+				device_trust_apply_boot_policy(dev, global,
+							       overrides[override]);
+				KUNIT_EXPECT_EQ(test, dev->trust_policy, override);
+			}
+		}
+	}
+}
+
+static void device_trust_global_busless_test(struct kunit *test)
+{
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_stub_reset(stub, NULL, NULL,
+							  DEVICE_TRUST_POLICY_UNRESTRICTED);
+
+	device_trust_apply_boot_policy(dev, DEVICE_TRUST_POLICY_BLOCKED,
+				       "pci:0000:01:00.0=unrestricted");
+	KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_BLOCKED);
+}
+
+static void device_trust_driver_precedence_test(struct kunit *test)
+{
+	static const char * const driver_rules[] = {
+		"pci:test=bus", "pci:test=blocked",
+		"pci:test=adversarial", "pci:test=unrestricted",
+	};
+	static const char * const device_rules[] = {
+		NULL, "pci:0000:01:00.0=bus", "pci:0000:01:00.0=blocked",
+		"pci:0000:01:00.0=adversarial", "pci:0000:01:00.0=unrestricted",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	unsigned int initial, global, driver, device;
+	struct device *dev;
+
+	for (initial = DEVICE_TRUST_POLICY_BUS;
+	     initial <= DEVICE_TRUST_POLICY_UNRESTRICTED; initial++) {
+		for (global = DEVICE_TRUST_POLICY_BUS;
+		     global <= DEVICE_TRUST_POLICY_UNRESTRICTED; global++) {
+			for (driver = 0; driver < ARRAY_SIZE(driver_rules); driver++) {
+				for (device = 0; device < ARRAY_SIZE(device_rules); device++) {
+					dev = device_trust_test_pci_stub(stub, "0000:01:00.0",
+									 initial);
+
+					device_trust_apply_boot_policy(dev, global,
+								       device_rules[device]);
+					dev->p = &stub->private;
+					device_trust_apply_boot_policy(dev, global,
+								       device_rules[device]);
+					device_trust_init_state(dev);
+					device_lock(dev);
+					device_trust_apply_driver_policy(dev, &stub->drv,
+									 driver_rules[driver]);
+					device_unlock(dev);
+					KUNIT_EXPECT_EQ(test, dev->trust_policy,
+							device ? device - 1 : driver);
+				}
+			}
+		}
+	}
+}
+
+static void device_trust_driver_matching_test(struct kunit *test)
+{
+	static const char * const unmatched[] = {
+		NULL, "usb:test=blocked", "pc:test=blocked", "pcie:test=blocked",
+		"pci:tes=blocked", "pci:test1=blocked", "pci:te*=blocked",
+		"pci:test=blocked,pci:other=invalid", "pci:test=blocked,",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, NULL,
+							DEVICE_TRUST_POLICY_BUS);
+	unsigned int i;
+
+	dev->p = &stub->private;
+	device_trust_init_state(dev);
+	for (i = 0; i < ARRAY_SIZE(unmatched); i++) {
+		device_lock(dev);
+		device_trust_apply_driver_policy(dev, &stub->drv, unmatched[i]);
+		device_unlock(dev);
+		KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_BUS);
+	}
+	device_lock(dev);
+	device_trust_apply_driver_policy(dev, &stub->drv,
+					 "pci:test=blocked,pci:test=adversarial");
+	device_unlock(dev);
+	KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+}
+
+static void device_trust_driver_quoted_name_test(struct kunit *test)
+{
+	static const char * const args[] = {
+		"device_trust.driver=\"pci:test driver=adversarial\"",
+		"\"device_trust.driver=pci:test driver=adversarial\"",
+		"device_trust.driver=\"pci:other=unrestricted,pci:test driver=adversarial\"",
+	};
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(args); i++) {
+		struct device *dev = device_trust_test_pci_stub(stub, NULL,
+							    DEVICE_TRUST_POLICY_BUS);
+		char *arg, *param, *value;
+
+		stub->drv.name = "test driver";
+		stub->driver_private.kobj.name = stub->drv.name;
+		dev->p = &stub->private;
+		device_trust_init_state(dev);
+		arg = kunit_kstrdup(test, args[i], GFP_KERNEL);
+		KUNIT_ASSERT_NOT_NULL(test, arg);
+		KUNIT_EXPECT_STREQ(test, next_arg(arg, &param, &value), "");
+		KUNIT_ASSERT_STREQ(test, param, "device_trust.driver");
+		KUNIT_ASSERT_NOT_NULL(test, value);
+		KUNIT_EXPECT_EQ(test, device_trust_parse_driver_cmdline(value, NULL, NULL), 0);
+		device_lock(dev);
+		device_trust_apply_driver_policy(dev, &stub->drv, value);
+		device_unlock(dev);
+		KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	}
+}
+
+static void device_trust_driver_invalid_name_test(struct kunit *test)
+{
+	static const char * const options[] = {
+		"pc i:test driver=unrestricted",
+		"pci:test\tdriver=unrestricted",
+		"pci:test\ndriver=unrestricted",
+		"pci:test\"driver=unrestricted",
+		"pci:test driver= unrestricted",
+		"pci:test driver=unrestricted=blocked",
+		"pci:test driver=unrestricted,pci:other=invalid",
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(options); i++)
+		KUNIT_EXPECT_EQ_MSG(test,
+				    device_trust_parse_driver_cmdline(options[i], NULL, NULL),
+				    -EINVAL, "case %u", i);
+}
+
+static void device_trust_driver_sysfs_name_test(struct kunit *test)
+{
+	const char *rule = "device-trust-test-plain:device-trust!slash=adversarial";
+	const char *raw_rule = "device-trust-test-plain:device-trust/slash=unrestricted";
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy policy;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->match_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register_named(test, ctx,
+								      "device-trust/slash"), 0);
+	KUNIT_EXPECT_STREQ(test, kobject_name(&ctx->drv.p->kobj), "device-trust!slash");
+	KUNIT_ASSERT_EQ(test, device_trust_parse_driver_cmdline(rule, &ctx->drv, &policy), 1);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_trust_parse_driver_cmdline(raw_rule, &ctx->drv, &policy), 0);
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &ctx->drv, rule);
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+}
+
+static void device_trust_driver_user_precedence_test(struct kunit *test)
+{
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	enum device_trust_policy policy;
+
+	for (policy = DEVICE_TRUST_POLICY_BUS;
+	     policy <= DEVICE_TRUST_POLICY_UNRESTRICTED; policy++) {
+		struct device *dev = device_trust_test_pci_stub(stub, NULL,
+								policy);
+
+		dev->p = &stub->private;
+		device_trust_init_state(dev);
+		KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, policy), 0);
+		device_lock(dev);
+		device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=blocked");
+		device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=unrestricted");
+		device_unlock(dev);
+		KUNIT_EXPECT_EQ(test, dev->trust_policy, policy);
+	}
+}
+
+static void device_trust_driver_rejected_write_test(struct kunit *test)
+{
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	struct device *dev = device_trust_test_pci_stub(stub, NULL,
+							DEVICE_TRUST_POLICY_BLOCKED);
+
+	dev->p = &stub->private;
+	device_trust_init_state(dev);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_BUS),
+			-EBUSY);
+	device_lock(dev);
+	device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=unrestricted");
+	device_unlock(dev);
+	KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+}
+
+static void device_trust_driver_authorize_test(struct kunit *test)
+{
+	struct driver_private private = { .kobj.name = "test" };
+	struct driver_private other_private = { .kobj.name = "other" };
+	const struct device_driver drv = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "test",
+		.p = &private,
+	};
+	struct device_trust_test_context *ctx;
+	const struct device_driver other = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "other",
+		.p = &other_private,
+	};
+	int ret;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &drv,
+					 "device-trust-test-plain:test=blocked");
+	device_unlock(&ctx->dev);
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &other,
+					 "device-trust-test-plain:other=unrestricted");
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						     DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &drv,
+					 "device-trust-test-plain:test=unrestricted");
+	device_unlock(&ctx->dev);
+	ret = device_attach(&ctx->dev);
+	KUNIT_ASSERT_EQ(test, ret, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+
+	device_release_driver(&ctx->dev);
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &drv,
+					 "device-trust-test-plain:test=unrestricted");
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_driver_resolver_failure_test(struct kunit *test)
+{
+	struct driver_private private = { .kobj.name = "test" };
+	const struct device_driver drv = {
+		.bus = &device_trust_test_resolving_bus,
+		.name = "test",
+		.p = &private,
+	};
+	struct device_trust_test_context *ctx;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_resolving_bus, DEVICE_TRUST_POLICY_BLOCKED);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &drv,
+					 "device-trust-test-resolving:test=unrestricted");
+	device_unlock(&ctx->dev);
+	ctx->resolve_ret = -EIO;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->resolve_policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+
+	device_lock(&ctx->dev);
+	device_trust_apply_driver_policy(&ctx->dev, &drv,
+					 "device-trust-test-resolving:test=blocked");
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						     DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+}
+
+#define DEVICE_TRUST_TEST_RULES \
+	"device-trust-test-plain:test=unrestricted," \
+	"device-trust-test-plain:other=adversarial"
+
+static void device_trust_driver_candidate_test(struct kunit *test)
+{
+	struct driver_private private = { .kobj.name = "test" };
+	struct driver_private other_private = { .kobj.name = "other" };
+	const struct device_driver drv = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "test",
+		.p = &private,
+	};
+	const struct device_driver other = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "other",
+		.p = &other_private,
+	};
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy policy;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	device_lock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &drv,
+					DEVICE_TRUST_TEST_RULES, &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_trust_prepare(&ctx->dev, &drv), 0);
+	device_trust_clear(&ctx->dev);
+
+	/* The first candidate declined; the next one disagrees with its rule. */
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &other,
+					DEVICE_TRUST_TEST_RULES, &policy), -EPERM);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &other,
+					"device-trust-test-plain:other=unrestricted",
+					&policy), 0);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &drv,
+					DEVICE_TRUST_TEST_RULES, &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_driver_committed_default_test(struct kunit *test)
+{
+	struct driver_private private = { .kobj.name = "test" };
+	struct driver_private other_private = { .kobj.name = "other" };
+	const struct device_driver drv = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "test",
+		.p = &private,
+	};
+	const struct device_driver other = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "other",
+		.p = &other_private,
+	};
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy policy;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	device_lock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_prepare(&ctx->dev, &drv), 0);
+	device_trust_clear(&ctx->dev);
+
+	/* A later rule is resolved against the commitment, not dropped. */
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &other,
+					DEVICE_TRUST_TEST_RULES, &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_driver_authorized_candidate_test(struct kunit *test)
+{
+	struct driver_private private = { .kobj.name = "test" };
+	struct driver_private other_private = { .kobj.name = "other" };
+	const struct device_driver drv = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "test",
+		.p = &private,
+	};
+	const struct device_driver other = {
+		.bus = &device_trust_test_plain_bus,
+		.name = "other",
+		.p = &other_private,
+	};
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy policy;
+
+	ctx = device_trust_test_device_register(
+		test, &device_trust_test_plain_bus, DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	device_lock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &drv,
+					"device-trust-test-plain:test=blocked", &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_BLOCKED);
+	device_unlock(&ctx->dev);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						     DEVICE_TRUST_POLICY_ADVERSARIAL), 0);
+
+	/* Authorization takes precedence over every driver rule. */
+	device_lock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &other,
+					DEVICE_TRUST_TEST_RULES, &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_trust_select_driver_policy(&ctx->dev, &drv,
+					DEVICE_TRUST_TEST_RULES, &policy), 0);
+	KUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	device_unlock(&ctx->dev);
+}
+
+static void device_trust_claimed_policy_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+
+	/* A driver that claimed the device before registration skipped admission. */
+	device_lock(&ctx->dev);
+	ctx->dev.driver = &ctx->drv;
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BLOCKED), -EBUSY);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BUS), 0);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_BUS);
+
+	device_lock(&ctx->dev);
+	ctx->dev.driver = NULL;
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_BLOCKED), 0);
+}
+
+static void device_trust_scan_continues_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	struct device_driver *other;
+	int ret;
+
+	ctx = device_trust_test_device_register(test, &device_trust_test_resolving_bus,
+						DEVICE_TRUST_POLICY_BUS);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->match_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+
+	other = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, other);
+	other->name = "device-trust-scan-other";
+	other->bus = ctx->dev.bus;
+	other->probe = device_trust_test_probe;
+	other->remove = device_trust_test_remove;
+	other->probe_type = PROBE_FORCE_SYNCHRONOUS;
+	other->suppress_bind_attrs = true;
+	KUNIT_ASSERT_EQ(test, driver_register(other), 0);
+	ret = kunit_add_action_or_reset(test,
+					device_trust_test_other_driver_unregister, other);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	/* The first candidate is rejected; the scan must reach the second. */
+	ctx->reject_driver = &ctx->drv;
+	WRITE_ONCE(ctx->match_ret, 1);
+	KUNIT_EXPECT_EQ(test, device_attach(&ctx->dev), 1);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, other);
+	KUNIT_EXPECT_EQ(test, ctx->resolve_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+}
+
+static void device_trust_early_claim_test(struct kunit *test,
+					 enum device_trust_policy policy, bool device_override)
+{
+	static const char * const policies[] = {
+		"bus", "blocked", "adversarial", "unrestricted",
+	};
+	struct device_trust_test_context *ctx;
+	enum device_trust_level level;
+	char *driver_rule, *device_rule;
+	size_t driver_len, device_len;
+	bool admitted;
+	int ret, probe_ret = 0;
+
+	ctx = device_trust_test_device_alloc(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_BLOCKED, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->match_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	KUNIT_ASSERT_PTR_EQ(test, ctx->dev.p, NULL);
+	device_lock(&ctx->dev);
+	ret = device_trust_prepare(&ctx->dev, &ctx->drv);
+	device_unlock(&ctx->dev);
+	KUNIT_ASSERT_EQ(test, ret, -EPERM);
+	KUNIT_ASSERT_NOT_NULL(test, ctx->dev.p);
+
+	driver_len = strlen(ctx->drv.bus->name) + strlen(ctx->drv.name) +
+		strlen(policies[policy]) + 3;
+	device_len = strlen(ctx->dev.bus->name) + strlen(dev_name(&ctx->dev)) +
+		sizeof(":=unrestricted");
+	driver_rule = kunit_kmalloc(test, driver_len, GFP_KERNEL);
+	device_rule = kunit_kmalloc(test, device_len, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, driver_rule);
+	KUNIT_ASSERT_NOT_NULL(test, device_rule);
+	snprintf(driver_rule, driver_len, "%s:%s=%s", ctx->drv.bus->name,
+		 ctx->drv.name, policies[policy]);
+	snprintf(device_rule, device_len, "%s:%s=unrestricted", ctx->dev.bus->name,
+		 dev_name(&ctx->dev));
+	device_lock(&ctx->dev);
+	if (device_override) {
+		device_trust_apply_boot_policy(&ctx->dev, DEVICE_TRUST_POLICY_BUS,
+					       device_rule);
+		device_trust_init_state(&ctx->dev);
+	}
+	device_trust_apply_driver_policy(&ctx->dev, &ctx->drv, driver_rule);
+	ret = device_trust_prepare(&ctx->dev, &ctx->drv);
+	if (!ret)
+		probe_ret = ctx->drv.probe(&ctx->dev);
+	device_unlock(&ctx->dev);
+
+	if (device_override)
+		policy = DEVICE_TRUST_POLICY_UNRESTRICTED;
+	admitted = policy != DEVICE_TRUST_POLICY_BLOCKED;
+	level = !admitted ? DEVICE_TRUST_BLOCKED :
+		policy == DEVICE_TRUST_POLICY_ADVERSARIAL ? DEVICE_TRUST_ADVERSARIAL :
+		DEVICE_TRUST_UNRESTRICTED;
+	KUNIT_ASSERT_EQ(test, ret, admitted ? 0 : -EPERM);
+	KUNIT_ASSERT_EQ(test, probe_ret, 0);
+	KUNIT_EXPECT_FALSE(test, device_is_registered(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, admitted ? 1 : 0);
+	if (admitted)
+		KUNIT_EXPECT_EQ(test, ctx->probe_level, level);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev), level);
+
+	ret = device_add(&ctx->dev);
+	if (!ret)
+		ctx->device_registered = true;
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+	KUNIT_EXPECT_EQ(test, ctx->dev.p->trust_state,
+			admitted ? DEVICE_TRUST_COMMITTED : DEVICE_TRUST_WAITING);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev), level);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev), level);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	if (admitted) {
+		device_lock(&ctx->dev);
+		ctx->dev.driver = &ctx->drv;
+		ret = device_bind_driver(&ctx->dev);
+		if (ret)
+			ctx->dev.driver = NULL;
+		device_unlock(&ctx->dev);
+		KUNIT_ASSERT_EQ(test, ret, 0);
+	}
+	KUNIT_EXPECT_EQ(test, device_is_bound(&ctx->dev), admitted);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, admitted ? 1 : 0);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+}
+
+static void device_trust_early_adversarial_test(struct kunit *test)
+{
+	device_trust_early_claim_test(test, DEVICE_TRUST_POLICY_ADVERSARIAL, false);
+}
+
+static void device_trust_early_unrestricted_test(struct kunit *test)
+{
+	device_trust_early_claim_test(test, DEVICE_TRUST_POLICY_UNRESTRICTED, false);
+}
+
+static void device_trust_early_blocked_test(struct kunit *test)
+{
+	device_trust_early_claim_test(test, DEVICE_TRUST_POLICY_BLOCKED, false);
+}
+
+static void device_trust_early_device_override_test(struct kunit *test)
+{
+	device_trust_early_claim_test(test, DEVICE_TRUST_POLICY_ADVERSARIAL, true);
+}
+
+static struct kunit_case device_trust_test_cases[] = {
+	KUNIT_CASE(device_trust_default_bind_test),
+	KUNIT_CASE(device_trust_bus_adversarial_bind_test),
+	KUNIT_CASE(device_trust_default_pinned_test),
+	KUNIT_CASE(device_trust_blocked_bind_test),
+	KUNIT_CASE(device_trust_blocked_unrestricted_bind_test),
+	KUNIT_CASE(device_trust_global_blocked_adversarial_test),
+	KUNIT_CASE(device_trust_global_blocked_unrestricted_test),
+	KUNIT_CASE(device_trust_supplier_retry_test),
+	KUNIT_CASE(device_trust_match_defer_authorize_test),
+	KUNIT_CASE(device_trust_async_policy_test),
+	KUNIT_CASE(device_trust_generic_adversarial_bind_test),
+	KUNIT_CASE(device_trust_no_iommu_bind_test),
+	KUNIT_CASE(device_trust_resolver_failure_test),
+	KUNIT_CASE(device_trust_resolver_blocked_test),
+	KUNIT_CASE(device_trust_probe_failure_test),
+	KUNIT_CASE(device_trust_probe_defer_test),
+	KUNIT_CASE(device_trust_dma_failure_test),
+	KUNIT_CASE(device_trust_force_bind_guard_test),
+	KUNIT_CASE(device_trust_legacy_force_bind_test),
+	KUNIT_CASE(device_trust_busless_force_bind_test),
+	KUNIT_CASE(device_trust_cmdline_policies_test),
+	KUNIT_CASE(device_trust_cmdline_exact_match_test),
+	KUNIT_CASE(device_trust_cmdline_last_match_test),
+	KUNIT_CASE(device_trust_cmdline_nameless_test),
+	KUNIT_CASE(device_trust_cmdline_validate_test),
+	KUNIT_CASE(device_trust_cmdline_invalid_test),
+	KUNIT_CASE(device_trust_boot_precedence_test),
+	KUNIT_CASE(device_trust_global_busless_test),
+	KUNIT_CASE(device_trust_driver_precedence_test),
+	KUNIT_CASE(device_trust_driver_matching_test),
+	KUNIT_CASE(device_trust_driver_quoted_name_test),
+	KUNIT_CASE(device_trust_driver_invalid_name_test),
+	KUNIT_CASE(device_trust_driver_sysfs_name_test),
+	KUNIT_CASE(device_trust_driver_user_precedence_test),
+	KUNIT_CASE(device_trust_driver_rejected_write_test),
+	KUNIT_CASE(device_trust_driver_authorize_test),
+	KUNIT_CASE(device_trust_driver_resolver_failure_test),
+	KUNIT_CASE(device_trust_driver_candidate_test),
+	KUNIT_CASE(device_trust_driver_committed_default_test),
+	KUNIT_CASE(device_trust_driver_authorized_candidate_test),
+	KUNIT_CASE(device_trust_claimed_policy_test),
+	KUNIT_CASE(device_trust_scan_continues_test),
+	KUNIT_CASE(device_trust_early_adversarial_test),
+	KUNIT_CASE(device_trust_early_unrestricted_test),
+	KUNIT_CASE(device_trust_early_blocked_test),
+	KUNIT_CASE(device_trust_early_device_override_test),
+	{}
+};
+
+static struct device_trust_test_context *
+device_trust_unrestricted_test_register(struct kunit *test, const struct bus_type *bus,
+					struct device *parent)
+{
+	struct device_trust_test_context *ctx;
+	int ret;
+
+	ctx = device_trust_test_device_register_parent(test, bus,
+						       DEVICE_TRUST_POLICY_BUS,
+						       DEVICE_TRUST_POLICY_BUS, parent);
+	if (IS_ERR(ctx))
+		return ctx;
+	ctx->match_ret = 0;
+	ret = device_trust_test_driver_register(test, ctx);
+	if (ret)
+		return ERR_PTR(ret);
+	return ctx;
+}
+
+#if IS_BUILTIN(CONFIG_DM_KUNIT_TEST)
+static bool device_trust_test_parse_boot_rules(struct kunit *test,
+					       const char *cmdline, bool *driver_rules,
+					       enum device_trust_policy *default_policy)
+{
+	static const char * const policies[] = {
+		"bus", "blocked", "adversarial", "unrestricted",
+	};
+	enum device_trust_policy policy = DEVICE_TRUST_POLICY_BLOCKED;
+	char *args, *param, *value;
+	int parsed;
+	bool configured = false;
+
+	*driver_rules = false;
+	args = kunit_kstrdup(test, cmdline, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, args);
+	while (*args) {
+		args = next_arg(args, &param, &value);
+		if (!value && !strcmp(param, "--"))
+			break;
+		if (parameq(param, "device_trust") ||
+		    parameqn(param, "device_trust.", strlen("device_trust.")))
+			configured = true;
+		if (!value)
+			continue;
+		if (parameq(param, "device_trust.default")) {
+			parsed = match_string(policies, ARRAY_SIZE(policies), value);
+			if (parsed >= 0)
+				policy = parsed;
+		} else if (parameq(param, "device_trust.driver") &&
+			   device_trust_parse_driver_cmdline(value, NULL, NULL) >= 0) {
+			*driver_rules = true;
+		}
+	}
+	if (default_policy)
+		*default_policy = configured ? policy : DEVICE_TRUST_POLICY_BUS;
+	return configured;
+}
+
+static void device_trust_boot_separator_test(struct kunit *test)
+{
+	static const struct {
+		const char *cmdline;
+		enum device_trust_policy policy;
+		bool configured;
+	} cases[] = {
+		{ "device_trust.default=unrestricted --=ignored device_trust.default=blocked",
+		  DEVICE_TRUST_POLICY_BLOCKED, true },
+		{ "device_trust.default=unrestricted --= device_trust.default=blocked",
+		  DEVICE_TRUST_POLICY_BLOCKED, true },
+		{ "device_trust.default=unrestricted -- device_trust.default=blocked",
+		  DEVICE_TRUST_POLICY_UNRESTRICTED, true },
+		{ "-- device_trust.default=unrestricted", DEVICE_TRUST_POLICY_BUS, false },
+		{ "--=ignored device_trust.default=unrestricted",
+		  DEVICE_TRUST_POLICY_UNRESTRICTED, true },
+	};
+	enum device_trust_policy policy;
+	bool configured, driver_rules;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		configured = device_trust_test_parse_boot_rules(test, cases[i].cmdline,
+							       &driver_rules, &policy);
+		KUNIT_EXPECT_EQ_MSG(test, configured, cases[i].configured, "case %u", i);
+		KUNIT_EXPECT_EQ_MSG(test, policy, cases[i].policy, "case %u", i);
+		KUNIT_EXPECT_FALSE(test, driver_rules);
+	}
+}
+#endif
+
+static bool device_trust_unrestricted_test_boot_rules(struct kunit *test,
+						      bool *driver_rules,
+						      enum device_trust_policy *default_policy)
+{
+#if IS_BUILTIN(CONFIG_DM_KUNIT_TEST)
+	return device_trust_test_parse_boot_rules(test, saved_command_line,
+						  driver_rules, default_policy);
+#else
+	kunit_skip(test, "boot policy coverage requires built-in tests");
+	return false;
+#endif
+}
+
+static bool device_trust_unrestricted_test_allowed(struct kunit *test,
+						   struct device *dev)
+{
+	enum device_trust_policy default_policy;
+	bool driver_rules;
+
+	if (device_trust_unrestricted_test_boot_rules(test, &driver_rules, &default_policy) &&
+	    default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+		return false;
+	if (dev->trust_policy != DEVICE_TRUST_POLICY_BUS &&
+	    dev->trust_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+		return false;
+	return !driver_rules || dev->p->trust_policy_override;
+}
+
+static int device_trust_test_attach_unrestricted(struct device_trust_test_context *ctx)
+{
+	struct device *dev = &ctx->dev;
+	struct device *parent = dev->parent;
+	int ret;
+
+	if (parent)
+		device_lock(parent);
+	device_lock(dev);
+	ret = device_driver_attach_unrestricted(&ctx->drv, dev);
+	device_unlock(dev);
+	if (parent)
+		device_unlock(parent);
+	return ret;
+}
+
+static void device_trust_unrestricted_boot_policy_test(struct kunit *test)
+{
+#if IS_BUILTIN(CONFIG_DM_KUNIT_TEST)
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy policy;
+	bool allowed;
+	int ret;
+
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	policy = ctx->dev.trust_policy;
+	allowed = device_trust_unrestricted_test_allowed(test, &ctx->dev);
+
+	ret = device_trust_test_attach_unrestricted(ctx);
+	KUNIT_ASSERT_EQ(test, ret, allowed ? 0 : -EPERM);
+	KUNIT_EXPECT_EQ(test, device_is_bound(&ctx->dev), allowed);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, allowed ? 1 : 0);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			allowed ? DEVICE_TRUST_UNRESTRICTED : DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			allowed ? DEVICE_TRUST_UNRESTRICTED : DEVICE_TRUST_BLOCKED);
+	if (!allowed)
+		KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, NULL);
+#else
+	kunit_skip(test, "boot policy coverage requires built-in tests");
+#endif
+}
+
+static void device_trust_unrestricted_attach_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	struct device *parent;
+	int ret;
+
+	parent = root_device_register(test->name);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+	ret = kunit_add_action_or_reset(test,
+					device_trust_test_parent_unregister, parent);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, parent);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	if (!device_trust_unrestricted_test_allowed(test, &ctx->dev))
+		kunit_skip(test, "boot policy disallows unrestricted-only attachment");
+
+	ret = device_trust_test_attach_unrestricted(ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, &ctx->drv);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, device_trust_test_attach_unrestricted(ctx), -EBUSY);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+
+	device_lock(parent);
+	device_release_driver(&ctx->dev);
+	device_unlock(parent);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev), DEVICE_TRUST_BLOCKED);
+	KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+			DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_ASSERT_EQ(test, device_trust_test_attach_unrestricted(ctx), 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+}
+
+struct device_trust_registration_test {
+	struct notifier_block notifier;
+	struct device_trust_test_context *ctx;
+	int result;
+	bool notified;
+};
+
+static int device_trust_test_notify_add(struct notifier_block *notifier,
+					unsigned long action, void *data)
+{
+	struct device_trust_registration_test *registration =
+		container_of(notifier, struct device_trust_registration_test, notifier);
+	struct device_trust_test_context *ctx = registration->ctx;
+
+	if (action != BUS_NOTIFY_ADD_DEVICE || data != &ctx->dev)
+		return NOTIFY_DONE;
+	registration->result = device_driver_attach_unrestricted(&ctx->drv, &ctx->dev);
+	registration->notified = true;
+	return NOTIFY_OK;
+}
+
+static void device_trust_unrestricted_registration_test(struct kunit *test)
+{
+	struct device_trust_registration_test registration = {
+		.notifier.notifier_call = device_trust_test_notify_add,
+	};
+	struct device_trust_test_context *ctx;
+	bool allowed;
+	int ret;
+
+	ctx = device_trust_test_device_alloc(test, &device_trust_test_plain_bus,
+					     DEVICE_TRUST_POLICY_BUS, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	KUNIT_ASSERT_PTR_EQ(test, ctx->dev.p, NULL);
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->dev.p, NULL);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, NULL);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 0);
+	ctx->match_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+	registration.ctx = ctx;
+	KUNIT_ASSERT_EQ(test, bus_register_notifier(ctx->dev.bus, &registration.notifier), 0);
+	ret = device_add(&ctx->dev);
+	if (!ret)
+		ctx->device_registered = true;
+	bus_unregister_notifier(ctx->dev.bus, &registration.notifier);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_TRUE(test, registration.notified);
+	KUNIT_EXPECT_EQ(test, registration.result, -EAGAIN);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	allowed = device_trust_unrestricted_test_allowed(test, &ctx->dev);
+	KUNIT_EXPECT_EQ(test, device_trust_test_attach_unrestricted(ctx), allowed ? 0 : -EPERM);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, allowed ? 1 : 0);
+}
+
+static void device_trust_unrestricted_test_release_resource(void *data)
+{
+	struct device_trust_test_context *ctx = data;
+
+	ctx->release_count++;
+}
+
+static int device_trust_unrestricted_test_probe(struct device *dev)
+{
+	struct device_trust_test_context *ctx = to_device_trust_test_context(dev);
+	int ret;
+
+	ret = devm_add_action(dev, device_trust_unrestricted_test_release_resource, ctx);
+	if (ret)
+		return ret;
+	dev_set_drvdata(dev, ctx);
+	return device_trust_test_probe(dev);
+}
+
+enum device_trust_test_operation {
+	DEVICE_TRUST_TEST_UNRESTRICTED_ATTACH,
+	DEVICE_TRUST_TEST_ATTACH,
+	DEVICE_TRUST_TEST_SCAN,
+	DEVICE_TRUST_TEST_UNBIND,
+	DEVICE_TRUST_TEST_REMOVE,
+	DEVICE_TRUST_TEST_LOCK,
+};
+
+struct device_trust_test_worker {
+	struct device_trust_test_context *ctx;
+	struct task_struct *task;
+	struct completion started;
+	struct completion done;
+	struct completion *resume;
+	enum device_trust_test_operation operation;
+	int result;
+};
+
+struct device_trust_test_race {
+	struct device_trust_test_context *ctx;
+	struct completion entered;
+	struct completion resume;
+	struct device_trust_test_worker workers[2];
+};
+
+static int device_trust_test_worker(void *data)
+{
+	struct device_trust_test_worker *worker = data;
+	struct device_trust_test_context *ctx = worker->ctx;
+
+	if (worker->operation == DEVICE_TRUST_TEST_LOCK)
+		device_lock(&ctx->dev);
+	complete(&worker->started);
+	switch (worker->operation) {
+	case DEVICE_TRUST_TEST_UNRESTRICTED_ATTACH:
+		worker->result = device_driver_attach_unrestricted(&ctx->drv, &ctx->dev);
+		break;
+	case DEVICE_TRUST_TEST_ATTACH:
+		worker->result = device_driver_attach(&ctx->drv, &ctx->dev);
+		break;
+	case DEVICE_TRUST_TEST_SCAN:
+		worker->result = device_attach(&ctx->dev);
+		break;
+	case DEVICE_TRUST_TEST_UNBIND:
+		device_release_driver(&ctx->dev);
+		worker->result = 0;
+		break;
+	case DEVICE_TRUST_TEST_REMOVE:
+		device_del(&ctx->dev);
+		ctx->device_registered = false;
+		worker->result = 0;
+		break;
+	case DEVICE_TRUST_TEST_LOCK:
+		wait_for_completion(worker->resume);
+		device_unlock(&ctx->dev);
+		worker->result = 0;
+		break;
+	}
+	complete(&worker->done);
+	return 0;
+}
+
+static void device_trust_test_race_stop(void *data)
+{
+	struct device_trust_test_race *race = data;
+	unsigned int i;
+
+	complete_all(&race->resume);
+	for (i = 0; i < ARRAY_SIZE(race->workers); i++) {
+		if (!race->workers[i].task)
+			continue;
+		kthread_stop(race->workers[i].task);
+		put_task_struct(race->workers[i].task);
+	}
+}
+
+static struct device_trust_test_race *device_trust_test_race_alloc(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	struct device_trust_test_race *race;
+
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	if (!device_trust_unrestricted_test_allowed(test, &ctx->dev))
+		kunit_skip(test, "boot policy disallows unrestricted-only attachment");
+	race = kunit_kzalloc(test, sizeof(*race), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, race);
+	race->ctx = ctx;
+	init_completion(&race->entered);
+	init_completion(&race->resume);
+	ctx->probe_entered = &race->entered;
+	ctx->probe_continue = &race->resume;
+	ctx->drv.probe = device_trust_unrestricted_test_probe;
+	KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test,
+							device_trust_test_race_stop, race), 0);
+	return race;
+}
+
+static int device_trust_test_start_worker(struct device_trust_test_race *race,
+					  unsigned int index,
+					  enum device_trust_test_operation operation)
+{
+	struct device_trust_test_worker *worker = &race->workers[index];
+	struct task_struct *task;
+
+	worker->ctx = race->ctx;
+	worker->operation = operation;
+	worker->resume = &race->resume;
+	init_completion(&worker->started);
+	init_completion(&worker->done);
+	task = kthread_create(device_trust_test_worker, worker, "device-trust-race");
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+	get_task_struct(task);
+	worker->task = task;
+	wake_up_process(task);
+	return 0;
+}
+
+static bool device_trust_test_wait_for_contention(struct device_trust_test_worker *worker)
+{
+	unsigned long deadline = jiffies + 5 * HZ;
+
+	do {
+		if (completion_done(&worker->done))
+			return false;
+		/* These workers only sleep on driver_lock after taking the device lock. */
+		if (READ_ONCE(worker->task->__state) == TASK_UNINTERRUPTIBLE &&
+		    mutex_is_locked(&worker->ctx->dev.mutex))
+			return true;
+		usleep_range(1000, 2000);
+	} while (time_before(jiffies, deadline));
+	return false;
+}
+
+static void device_trust_unrestricted_concurrent_test(struct kunit *test,
+						      enum device_trust_test_operation operation)
+{
+	struct device_trust_test_race *race = device_trust_test_race_alloc(test);
+	struct device_trust_test_context *ctx = race->ctx;
+	bool unbind = operation != DEVICE_TRUST_TEST_SCAN;
+
+	KUNIT_ASSERT_EQ(test,
+		device_trust_test_start_worker(race, 0,
+					       DEVICE_TRUST_TEST_UNRESTRICTED_ATTACH), 0);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->entered, 5 * HZ), 0UL);
+	KUNIT_ASSERT_EQ(test, device_trust_test_start_worker(race, 1, operation), 0);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[1].started, 5 * HZ), 0UL);
+	KUNIT_ASSERT_TRUE(test, device_trust_test_wait_for_contention(&race->workers[1]));
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_TRUE(test, device_is_registered(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev), DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->release_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 0);
+
+	complete(&race->resume);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[0].done, 5 * HZ), 0UL);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[1].done, 5 * HZ), 0UL);
+	KUNIT_EXPECT_EQ(test, race->workers[0].result, 0);
+	KUNIT_EXPECT_EQ(test, race->workers[1].result, unbind ? 0 : 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_exit_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->release_count, unbind ? 1 : 0);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, unbind ? 1 : 0);
+	KUNIT_EXPECT_EQ(test, device_is_bound(&ctx->dev), !unbind);
+	KUNIT_EXPECT_EQ(test, device_is_registered(&ctx->dev),
+			operation != DEVICE_TRUST_TEST_REMOVE);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+			unbind ? DEVICE_TRUST_BLOCKED : DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_unrestricted_scan_race_test(struct kunit *test)
+{
+	device_trust_unrestricted_concurrent_test(test, DEVICE_TRUST_TEST_SCAN);
+}
+
+static void device_trust_unrestricted_unbind_race_test(struct kunit *test)
+{
+	device_trust_unrestricted_concurrent_test(test, DEVICE_TRUST_TEST_UNBIND);
+}
+
+static void device_trust_unrestricted_remove_race_test(struct kunit *test)
+{
+	device_trust_unrestricted_concurrent_test(test, DEVICE_TRUST_TEST_REMOVE);
+}
+
+static void device_trust_unrestricted_device_lock_test(struct kunit *test)
+{
+	struct device_trust_test_race *race = device_trust_test_race_alloc(test);
+	struct device_trust_test_context *ctx = race->ctx;
+
+	ctx->probe_entered = NULL;
+	ctx->probe_continue = NULL;
+	KUNIT_ASSERT_EQ(test, device_trust_test_start_worker(race, 0, DEVICE_TRUST_TEST_LOCK), 0);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[0].started, 5 * HZ), 0UL);
+	KUNIT_ASSERT_EQ(test,
+		device_trust_test_start_worker(race, 1,
+					       DEVICE_TRUST_TEST_UNRESTRICTED_ATTACH), 0);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[1].done, 5 * HZ), 0UL);
+	KUNIT_EXPECT_EQ(test, race->workers[1].result, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+	complete(&race->resume);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[0].done, 5 * HZ), 0UL);
+}
+
+static void device_trust_unrestricted_busy_probe_test(struct kunit *test)
+{
+	struct device_trust_test_race *race = device_trust_test_race_alloc(test);
+	struct device_trust_test_context *ctx = race->ctx;
+
+	KUNIT_ASSERT_EQ(test, device_trust_test_start_worker(race, 0, DEVICE_TRUST_TEST_ATTACH), 0);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->entered, 5 * HZ), 0UL);
+	KUNIT_EXPECT_EQ(test, device_driver_attach_unrestricted(&ctx->drv, &ctx->dev), -EBUSY);
+	KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+	KUNIT_EXPECT_EQ(test, ctx->release_count, 0);
+	KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev), DEVICE_TRUST_UNRESTRICTED);
+	complete(&race->resume);
+	KUNIT_ASSERT_NE(test, wait_for_completion_timeout(&race->workers[0].done, 5 * HZ), 0UL);
+	KUNIT_EXPECT_EQ(test, race->workers[0].result, 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_exit_level, DEVICE_TRUST_UNRESTRICTED);
+	KUNIT_EXPECT_EQ(test, ctx->release_count, 0);
+	KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+}
+
+static void device_trust_unrestricted_probe_failure_test(struct kunit *test)
+{
+	static const int errors[] = { -EIO, -EPROBE_DEFER };
+	static const int expected[] = { -EIO, -EAGAIN };
+	struct device_trust_test_context *ctx;
+	int i;
+
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	if (!device_trust_unrestricted_test_allowed(test, &ctx->dev))
+		kunit_skip(test, "boot policy disallows unrestricted-only attachment");
+	ctx->drv.probe = device_trust_unrestricted_test_probe;
+
+	for (i = 0; i < ARRAY_SIZE(errors); i++) {
+		ctx->probe_ret = errors[i];
+		KUNIT_ASSERT_EQ(test, device_trust_test_attach_unrestricted(ctx),
+				expected[i]);
+		KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+		KUNIT_EXPECT_PTR_EQ(test, ctx->dev.driver, NULL);
+		KUNIT_EXPECT_PTR_EQ(test, dev_get_drvdata(&ctx->dev), NULL);
+		KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_UNRESTRICTED);
+		KUNIT_EXPECT_EQ(test, ctx->probe_count, i + 1);
+		KUNIT_EXPECT_EQ(test, ctx->release_count, i + 1);
+		KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+				DEVICE_TRUST_BLOCKED);
+		KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+				DEVICE_TRUST_UNRESTRICTED);
+	}
+	KUNIT_EXPECT_EQ(test, ctx->configure_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 0);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 0);
+	ctx->probe_ret = 0;
+	KUNIT_ASSERT_EQ(test, device_trust_test_attach_unrestricted(ctx), 0);
+	KUNIT_EXPECT_PTR_EQ(test, dev_get_drvdata(&ctx->dev), ctx);
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->release_count, ARRAY_SIZE(errors) + 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_unrestricted_policy_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	enum device_trust_policy default_policy;
+	enum device_trust_state state;
+	bool driver_rules, override;
+
+	if (!device_trust_unrestricted_test_boot_rules(test, &driver_rules, &default_policy) ||
+	    default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED)
+		kunit_skip(test, "requires device_trust.default=unrestricted");
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	state = ctx->dev.p->trust_state;
+	override = ctx->dev.p->trust_policy_override;
+	device_lock(&ctx->dev);
+	device_trust_expect_policy_locked(test, &ctx->dev);
+	device_unlock(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->dev.p->trust_state, state);
+	KUNIT_EXPECT_EQ(test, (bool)ctx->dev.p->trust_policy_override, override);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+}
+
+static void device_trust_sysfs_boot_policy_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	struct device_attribute *attr = NULL;
+	struct attribute **attrs;
+	enum device_trust_policy policy, target, default_policy;
+	const char *value;
+	bool configured, driver_rules, allowed, override;
+	ssize_t ret;
+
+	configured = device_trust_unrestricted_test_boot_rules(test, &driver_rules,
+							     &default_policy);
+	ctx = device_trust_unrestricted_test_register(test, &device_trust_test_plain_bus, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	policy = ctx->dev.trust_policy;
+	override = ctx->dev.p->trust_policy_override;
+	for (attrs = device_trust_attr_group.attrs; *attrs; attrs++) {
+		if (!strcmp((*attrs)->name, "trust_policy")) {
+			attr = container_of(*attrs, struct device_attribute, attr);
+			break;
+		}
+	}
+	KUNIT_ASSERT_NOT_NULL(test, attr);
+	if (policy == DEVICE_TRUST_POLICY_ADVERSARIAL) {
+		target = DEVICE_TRUST_POLICY_UNRESTRICTED;
+		value = "unrestricted";
+	} else {
+		target = DEVICE_TRUST_POLICY_ADVERSARIAL;
+		value = "adversarial";
+	}
+	allowed = configured && default_policy != DEVICE_TRUST_POLICY_UNRESTRICTED;
+	ret = attr->store_const(&ctx->dev, attr, value, strlen(value));
+	KUNIT_ASSERT_EQ(test, ret, allowed ? (ssize_t)strlen(value) : -EBUSY);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, allowed ? target : policy);
+	KUNIT_EXPECT_EQ(test, (bool)ctx->dev.p->trust_policy_override,
+			allowed ? true : override);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+
+	if (!configured) {
+		KUNIT_ASSERT_EQ(test, policy, DEVICE_TRUST_POLICY_BUS);
+		ret = attr->store_const(&ctx->dev, attr, "bus", 3);
+		KUNIT_EXPECT_EQ(test, ret, 3);
+		KUNIT_EXPECT_EQ(test, (bool)ctx->dev.p->trust_policy_override, override);
+		/* Existing bus authorization remains available on legacy boots. */
+		KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+							      DEVICE_TRUST_POLICY_BLOCKED), 0);
+		KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+							      DEVICE_TRUST_POLICY_UNRESTRICTED), 0);
+		KUNIT_EXPECT_EQ(test, ctx->dev.p->trust_state, DEVICE_TRUST_AUTHORIZED);
+	}
+}
+
+static void device_trust_legacy_bus_policy_test(struct kunit *test)
+{
+	struct device_trust_test_context *ctx;
+	bool driver_rules;
+	int ret;
+
+	if (device_trust_unrestricted_test_boot_rules(test, &driver_rules, NULL))
+		kunit_skip(test, "requires a legacy boot without device trust options");
+	ctx = device_trust_test_device_register(test, &device_trust_test_plain_bus,
+						DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->match_ret = 0;
+	ret = device_trust_test_driver_register(test, ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_ADVERSARIAL);
+	KUNIT_EXPECT_EQ(test, device_trust_test_attach_unrestricted(ctx), -EPERM);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+	KUNIT_ASSERT_EQ(test, device_driver_attach(&ctx->drv, &ctx->dev), 0);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARIAL);
+}
+
+static void device_trust_boot_default_test(struct kunit *test)
+{
+	struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+	enum device_trust_policy expected, initial;
+	struct device *dev;
+	bool configured, driver_rules;
+
+	configured = device_trust_unrestricted_test_boot_rules(test, &driver_rules, &expected);
+	KUNIT_EXPECT_EQ(test, device_trust_enabled(), configured);
+	dev = device_trust_test_stub_reset(stub, NULL, NULL, DEVICE_TRUST_POLICY_BUS);
+	for (initial = DEVICE_TRUST_POLICY_BUS;
+	     initial <= DEVICE_TRUST_POLICY_UNRESTRICTED; initial++) {
+		dev->trust_policy = initial;
+		device_trust_init(dev);
+		KUNIT_EXPECT_EQ(test, dev->trust_policy,
+				expected == DEVICE_TRUST_POLICY_BUS ? initial : expected);
+	}
+}
+
+static struct kunit_case device_trust_unrestricted_test_cases[] = {
+#if IS_BUILTIN(CONFIG_DM_KUNIT_TEST)
+	KUNIT_CASE(device_trust_boot_separator_test),
+#endif
+	KUNIT_CASE(device_trust_boot_default_test),
+	KUNIT_CASE(device_trust_sysfs_boot_policy_test),
+	KUNIT_CASE(device_trust_legacy_bus_policy_test),
+	KUNIT_CASE(device_trust_unrestricted_boot_policy_test),
+	KUNIT_CASE(device_trust_unrestricted_attach_test),
+	KUNIT_CASE(device_trust_unrestricted_registration_test),
+	KUNIT_CASE(device_trust_unrestricted_scan_race_test),
+	KUNIT_CASE(device_trust_unrestricted_unbind_race_test),
+	KUNIT_CASE(device_trust_unrestricted_remove_race_test),
+	KUNIT_CASE(device_trust_unrestricted_device_lock_test),
+	KUNIT_CASE(device_trust_unrestricted_busy_probe_test),
+	KUNIT_CASE(device_trust_unrestricted_probe_failure_test),
+	KUNIT_CASE(device_trust_unrestricted_policy_test),
+	{}
+};
+
+static int device_trust_test_init(struct kunit *test)
+{
+	if (device_trust_enabled())
+		kunit_skip(test, "live boot policy can replace fixture policies");
+
+	return 0;
+}
+
+static int device_trust_test_bus_register(const struct bus_type *bus)
+{
+	static const struct attribute probe = { .name = "drivers_probe" };
+	static const struct attribute autoprobe = { .name = "drivers_autoprobe" };
+	struct kobject *kobj;
+	int ret;
+
+	ret = bus_register(bus);
+	if (ret)
+		return ret;
+
+	/* Exclude userspace changes to probe scheduling before creating fixtures. */
+	kobj = &bus_get_kset(bus)->kobj;
+	sysfs_remove_file(kobj, &probe);
+	sysfs_remove_file(kobj, &autoprobe);
+	to_subsys_private(kobj)->drivers_autoprobe = 1;
+	return 0;
+}
+
+static int device_trust_test_suite_init(struct kunit_suite *suite)
+{
+	int ret;
+
+	ret = device_trust_test_bus_register(&device_trust_test_plain_bus);
+	if (ret)
+		return ret;
+
+	ret = device_trust_test_bus_register(&device_trust_test_resolving_bus);
+	if (ret) {
+		bus_unregister(&device_trust_test_plain_bus);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void device_trust_test_suite_exit(struct kunit_suite *suite)
+{
+	bus_unregister(&device_trust_test_resolving_bus);
+	bus_unregister(&device_trust_test_plain_bus);
+}
+
+static struct kunit_suite device_trust_test_suite = {
+	.name = "device-trust",
+	.init = device_trust_test_init,
+	.suite_init = device_trust_test_suite_init,
+	.suite_exit = device_trust_test_suite_exit,
+	.test_cases = device_trust_test_cases,
+};
+
+static struct kunit_suite device_trust_unrestricted_test_suite = {
+	.name = "device-trust-unrestricted",
+	.suite_init = device_trust_test_suite_init,
+	.suite_exit = device_trust_test_suite_exit,
+	.test_cases = device_trust_unrestricted_test_cases,
+};
+
+kunit_test_suites(&device_trust_test_suite, &device_trust_unrestricted_test_suite);
+
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
+MODULE_DESCRIPTION("KUnit tests for device trust");
+MODULE_LICENSE("GPL");
diff --git a/drivers/base/trust.c b/drivers/base/trust.c
index 9f7478bd7a6d..8f6a060a1d26 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -57,6 +57,7 @@ void device_trust_init_state(struct device *dev)
 		dev->p->trust_state = dev->trust_policy == DEVICE_TRUST_POLICY_BLOCKED ?
 			DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
 }
+EXPORT_SYMBOL_IF_KUNIT(device_trust_init_state);
 
 static int __device_trust_prepare(struct device *dev,
 				  const struct device_driver *drv, bool unrestricted_only)
@@ -319,6 +320,7 @@ static struct attribute *device_trust_attrs[] = {
 const struct attribute_group device_trust_attr_group = {
 	.attrs = device_trust_attrs,
 };
+EXPORT_SYMBOL_IF_KUNIT(device_trust_attr_group);
 
 static const char *device_trust_cmdline __ro_after_init;
 static const char *device_trust_driver_cmdline __ro_after_init;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 27/33] driver core: Document trust admission and initrd authorization
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (25 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 26/33] driver core: Cover trust authorization and binding with KUnit Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 28/33] PCI: Use device trust for platform restrictions Leon Romanovsky
                   ` (5 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

Allowing a driver to bind does not establish device identity or prevent DMA
initiated by the hardware. An initrd that treats those as equivalent could
authorize a device before attestation or containment is ready.

Document the authorization workflow and the division of responsibility
between userspace, driver core and bus infrastructure. Explain which
decisions survive probe failure and unbind, and when queued probes may
start, so policy agents can prepare the device before granting access.
Include the kernel-doc of the device trust API.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 .../driver-api/driver-model/device-trust.rst       | 247 +++++++++++++++++++++
 Documentation/driver-api/driver-model/index.rst    |   1 +
 2 files changed, 248 insertions(+)

diff --git a/Documentation/driver-api/driver-model/device-trust.rst b/Documentation/driver-api/driver-model/device-trust.rst
new file mode 100644
index 000000000000..de26a2cf049e
--- /dev/null
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -0,0 +1,247 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============
+Device trust
+============
+
+Device trust controls how the kernel uses a device. It does not verify
+the device's identity or firmware.
+
+Each device has a requested policy and an active trust level. The bus,
+boot options and userspace set the policy. When a driver binds, driver
+core turns the policy into a level, and bus, DMA and driver code act on
+that level.
+
+Policies and levels
+===================
+
+The ``trust_policy`` sysfs attribute holds the requested policy:
+
+- ``bus``: the bus chooses the level.
+- ``blocked``: no driver may bind.
+- ``adversarial``: treat the device as potentially malicious.
+- ``unrestricted``: use the device normally.
+
+The bus may set the initial policy; otherwise it is ``bus``. Boot
+options can replace it; see `Boot options`_.
+
+The read-only ``trust`` attribute shows the active level:
+
+- ``blocked`` (``DEVICE_TRUST_BLOCKED``): no driver is set up or bound.
+- ``adversarial`` (``DEVICE_TRUST_ADVERSARIAL``): the device may be
+  malicious. This is only a label; protection comes from bus, DMA and
+  driver code that checks it.
+- ``unrestricted`` (``DEVICE_TRUST_UNRESTRICTED``): the device has the
+  normal privileges that the bus and DMA policy allow.
+
+On a bus without a ``trust_resolve`` callback, ``bus`` and
+``unrestricted`` give ``unrestricted``, and ``adversarial`` gives
+``adversarial``.
+
+Boot options
+============
+
+Built-in drivers can bind before userspace starts, so boot options set
+the initial policy. ``<policy>`` is one of the four policy names.
+
+``device_trust``
+  Enable device trust with ``blocked`` as the default.
+
+``device_trust.default=<policy>``
+  Set the policy of all devices. ``bus`` keeps the bus policy; other
+  values replace it, including topology and removability defaults.
+
+``device_trust=<bus>:<device>=<policy>[,...]``
+  Set the policy of single devices, named as in
+  ``/sys/bus/<bus>/devices/<device>``. The first colon ends the bus name;
+  later colons belong to the device name.
+
+``device_trust.driver=<bus>:<driver>=<policy>[,...]``
+  Set the policy of devices that a driver binds, named as in
+  ``/sys/bus/<bus>/drivers/<driver>``. Module names and aliases do not
+  match. Quote the option if a driver name contains spaces.
+
+Any ``device_trust`` option, even an unknown one or one without a value,
+makes ``blocked`` the default. ``device_trust.default=`` replaces that
+default wherever it appears; the last valid value wins.
+
+Names must match exactly, without wildcards. They cannot contain commas,
+equals signs, double quotes or control characters, and device names
+cannot contain whitespace. In a list, the last entry for a name wins. If
+an option is repeated, the last valid list replaces the earlier ones. A
+malformed list is ignored entirely, with a warning. Boot rules also apply
+to devices found after boot, and driver rules to modules.
+
+A device entry takes precedence over driver rules, the global default
+and bus policy, wherever it appears. A device entry of ``bus`` lets
+the bus choose the level even when the global default is ``blocked``.
+
+Boot options do not prove a device's identity.
+
+Driver rules
+------------
+
+A driver rule applies at the first bind attempt by a matching driver,
+before the level is chosen. It replaces bus and global policy, including
+a global ``blocked``. It does not apply if the device has a device entry,
+has been authorized or committed, or has had a successful policy write.
+With ``device_trust.default=unrestricted``, writing the current value
+does not count as such a write. A rule applies only once per device,
+even if binding fails.
+
+Several drivers can match one device. Once a driver rule has set the
+policy, a later driver whose rule names a different policy is rejected,
+even if both policies give the same level. If the device was committed
+without a driver rule, a later driver's rule may name another policy but
+must give the committed level. A driver never binds against its own
+rule. Drivers without a rule use the device's current policy.
+
+After a ``blocked`` driver rule applies, the device waits for
+authorization, even if other drivers try to bind. Userspace can
+authorize it unless ``device_trust.default=unrestricted`` is set.
+
+Changing the policy
+===================
+
+Without device trust boot options, the kernel works as before: bus
+defaults and bus authorization interfaces are unchanged, and
+``trust_policy`` accepts only its current value. Any device trust boot
+option enables policy changes; ``device_trust.default=bus`` does so
+without changing bus defaults.
+
+``device_trust.default=unrestricted`` is the exception. It fixes the
+policy of every device, including devices with boot exceptions. Writing
+a different value returns ``-EBUSY``, and writing the current value
+changes nothing. Boot rules still apply.
+
+When policy changes are enabled, a device is in one of these states:
+
+- Selectable: any policy can be written. Writing ``blocked`` makes the
+  device wait.
+- Waiting: the policy is ``blocked``. Only ``adversarial`` or
+  ``unrestricted`` can be written, which authorizes the device. Writing
+  ``bus`` does not unblock it.
+- Authorized: the policy cannot change.
+- Committed: before driver setup, the kernel fixed the policy and the
+  level. Neither can change.
+
+A device starts as waiting if its initial policy is ``blocked``, and as
+selectable otherwise. A different policy cannot be written while a
+driver is bound. Rejected writes return ``-EBUSY``.
+
+A device keeps its state for the lifetime of the device object,
+including across the switch from initrd to the root filesystem.
+Unbinding and rebinding do not reset it. A removed device that is found
+again is a new object and starts from bus and boot policy.
+
+Policy writes cannot revoke authorization or move a running device
+between ``adversarial`` and ``unrestricted``. Stopping a device needs a
+separate procedure that stops its driver, tears down DMA and contains the
+device on its bus.
+
+Driver binding
+==============
+
+A driver cannot bind a ``blocked`` device. A manual bind returns
+``-EPERM`` before any driver or DMA setup, and automatic matching moves
+on to the next driver. The device keeps waiting for authorization.
+
+The active level stays the same from DMA setup, through probe, operation
+and removal, until ``bus_type::dma_cleanup`` finishes. Then it returns to
+``blocked``.
+
+Initrd attestation
+==================
+
+An initrd can check devices before their drivers use them:
+
+1. Boot with ``device_trust.default=blocked`` to hold all devices, or
+   with ``device_trust.default=bus`` and ``blocked`` device entries
+   to hold some. Add exceptions for the devices and suppliers needed to
+   reach the initrd, attest devices and contain DMA meanwhile. Held
+   devices stay blocked even if their bus would choose another level, and
+   built-in drivers cannot bind them.
+
+2. In the initrd, control probing and attest the device through a
+   transport that works without its driver. Keep it ``blocked`` during
+   verification and preparation, and leave it blocked if verification
+   fails or is not possible. Attest again if a reset or preparation step
+   invalidates the evidence. Removing or replacing the device invalidates
+   the result; a reused bus address does not prove identity.
+
+3. Write ``adversarial`` or ``unrestricted`` to ``trust_policy``. This
+   fixes the policy and allows binding. Queued or concurrent probes may
+   start at once, so the evidence must still be valid at this point.
+
+4. Bind the driver through ``drivers_probe`` or another attach path. The
+   kernel commits the level before setup, and retries keep the same
+   policy and level. Retry on ``-EAGAIN``. A scan may skip rejected
+   drivers without returning an error, and a successful
+   ``drivers_probe`` write does not wait for a probe that is already
+   running.
+
+The ``driver`` link appears before probe completes, so it does not show
+that binding succeeded. Watch for the ``bind`` uevent of the current
+device object, starting before step 3, or use a synchronous per-driver
+bind.
+
+A driver whose probe callback exists only during init cannot retry a
+binding held by ``blocked``. Its devices need a boot exception, or the
+driver needs a probe callback that stays available. Boot exceptions with
+``adversarial`` or ``unrestricted`` let devices operate without fresh
+attestation. The kernel provides neither an attestation transport nor a
+verifier.
+
+DMA
+===
+
+``blocked`` stops driver binding, not DMA that the hardware starts on its
+own. The bus and IOMMU must contain such DMA while the device waits.
+
+``adversarial`` does not require an IOMMU and does not isolate DMA by
+itself. Without an IOMMU group, or with IOMMU support compiled out,
+binding uses the platform's normal DMA setup. Drivers that manage DMA
+themselves must provide their own containment. Drivers that do not use
+DMA need no domain.
+
+Device trust does not change DMA handling, IOMMU domain selection or
+ATS based on the active level; existing bus and platform code still
+decides them. Connecting them is separate work. If setup fails after
+commitment, the device keeps the committed level and cannot fall back to
+``unrestricted``.
+
+Bus and driver interfaces
+=========================
+
+A bus can implement the ``trust_resolve`` callback of struct bus_type to
+choose the level from the device, the candidate driver and the policy.
+For ``bus`` it may use topology or firmware data; any other policy
+must give the level of the same name, or the callback fails. The
+callback must not enable the device or grant DMA access; setup callbacks
+do that after it, before probe.
+
+Global and device boot rules apply when the device is added. A bus that
+needs the policy earlier calls device_trust_init(). Bus code that
+operates a device before device_add() admits it with
+device_trust_prepare() and ends the active level with
+device_trust_clear(). Buses with their own authorization interfaces can
+check device_trust_enabled() and request policies with
+device_trust_set_policy().
+
+Drivers may cache the active level while bound, but must not change the
+policy. Check device_is_adversarial() on paths that bypass the DMA API;
+general DMA containment belongs in bus and IOMMU code. A committed level
+does not allow using the device while it is unbound.
+
+Trust does not describe transport protection, encryption, verified
+identity, memory privacy or DMA addressability. Keep those properties
+separate from the policy and level.
+
+device_driver_attach_unrestricted() keeps devices out of device trust
+authorization and **must not be used in new code**; use
+device_driver_attach() instead.
+
+.. kernel-doc:: include/linux/device/trust.h
+
+.. kernel-doc:: drivers/base/trust.c
+   :export:
diff --git a/Documentation/driver-api/driver-model/index.rst b/Documentation/driver-api/driver-model/index.rst
index abeb4b36636b..af547cd13b2c 100644
--- a/Documentation/driver-api/driver-model/index.rst
+++ b/Documentation/driver-api/driver-model/index.rst
@@ -9,6 +9,7 @@ Driver Model
    bus
    design-patterns
    device
+   device-trust
    devres
    driver
    overview

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 28/33] PCI: Use device trust for platform restrictions
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (26 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 27/33] driver core: Document trust admission and initrd authorization Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 29/33] iommu: Use device trust for DMA restrictions Leon Romanovsky
                   ` (4 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

PCI topology already identifies devices that need restricted operation.
Generic trust policy must inherit that classification so authorization
and PCI restrictions use the same policy.

Resolve discovery-time restrictions from the requested policy, since active
trust is established only before driver setup. Retain the untrusted flag
for IOMMU consumers until their conversion. The initial target is
confidential computing guests without an IOMMU; synchronizing later
authorization with IOMMU domains and ATS remains follow-up work.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 .../driver-api/driver-model/device-trust.rst          |  6 +++---
 drivers/pci/ats.c                                     |  4 ++--
 drivers/pci/pci.c                                     |  5 +++--
 drivers/pci/probe.c                                   | 19 +++++++++----------
 drivers/pci/quirks.c                                  |  7 ++++---
 include/linux/device.h                                |  5 +++++
 include/linux/pci.h                                   | 10 ++++++++++
 7 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/Documentation/driver-api/driver-model/device-trust.rst b/Documentation/driver-api/driver-model/device-trust.rst
index de26a2cf049e..a3dab9534460 100644
--- a/Documentation/driver-api/driver-model/device-trust.rst
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -204,9 +204,9 @@ binding uses the platform's normal DMA setup. Drivers that manage DMA
 themselves must provide their own containment. Drivers that do not use
 DMA need no domain.
 
-Device trust does not change DMA handling, IOMMU domain selection or
-ATS based on the active level; existing bus and platform code still
-decides them. Connecting them is separate work. If setup fails after
+The first target is confidential computing guests without an IOMMU. On
+systems with an IOMMU, policy changes after discovery do not yet update
+domain selection or ATS; that is follow-up work. If setup fails after
 commitment, the device keeps the committed level and cannot fall back to
 ``unrestricted``.
 
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 9cb23780093d..c8c96acc2634 100644
--- a/drivers/pci/ats.c
+++ b/drivers/pci/ats.c
@@ -21,7 +21,7 @@ void pci_ats_init(struct pci_dev *dev)
 {
 	int pos;
 
-	if (pci_ats_disabled())
+	if (pci_ats_disabled() || pci_dev_default_is_restricted(dev))
 		return;
 
 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
@@ -40,7 +40,7 @@ void pci_ats_init(struct pci_dev *dev)
  */
 bool pci_ats_supported(struct pci_dev *dev)
 {
-	if (!dev->ats_cap || dev->untrusted)
+	if (!dev->ats_cap)
 		return false;
 
 	if (dev->is_virtfn)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7c6f2aba8144..a60701bdece4 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -1016,8 +1016,9 @@ static void pci_std_enable_acs(struct pci_dev *dev, struct pci_acs *caps)
 	/* Upstream Forwarding */
 	caps->ctrl |= (dev->acs_capabilities & PCI_ACS_UF);
 
-	/* Enable Translation Blocking for external devices and noats */
-	if (pci_ats_disabled() || dev->external_facing || dev->untrusted)
+	/* Enable Translation Blocking when ATS is unavailable or restricted. */
+	if (pci_ats_disabled() || dev->external_facing ||
+	    pci_dev_default_is_restricted(dev))
 		caps->ctrl |= (dev->acs_capabilities & PCI_ACS_TB);
 }
 
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..606ab8a3aa8e 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1738,24 +1738,21 @@ static void set_pcie_cxl(struct pci_dev *dev)
 
 }
 
-static void set_pcie_untrusted(struct pci_dev *dev)
+static void pci_set_default_trust_policy(struct pci_dev *dev)
 {
 	struct pci_dev *parent = pci_upstream_bridge(dev);
 
 	if (!parent)
 		return;
-	/*
-	 * If the upstream bridge is untrusted we treat this device as
-	 * untrusted as well.
-	 */
-	if (parent->untrusted) {
-		dev->untrusted = true;
+
+	if (device_policy_is_adversarial(&parent->dev)) {
+		dev->dev.trust_policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
 		return;
 	}
 
 	if (arch_pci_dev_is_removable(dev)) {
-		pci_dbg(dev, "marking as untrusted\n");
-		dev->untrusted = true;
+		pci_dbg(dev, "setting adversarial trust policy\n");
+		dev->dev.trust_policy = DEVICE_TRUST_POLICY_ADVERSARIAL;
 	}
 }
 
@@ -2082,7 +2079,9 @@ int pci_setup_device(struct pci_dev *dev)
 
 	set_pcie_cxl(dev);
 
-	set_pcie_untrusted(dev);
+	pci_set_default_trust_policy(dev);
+	dev->untrusted = device_policy_is_adversarial(&dev->dev);
+	device_trust_init(&dev->dev);
 
 	/* "Unknown power state" */
 	dev->current_state = PCI_UNKNOWN;
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 7f1f47ea2ee4..1afad28d1b5e 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -5525,8 +5525,8 @@ static void pci_quirk_enable_intel_rp_mpc_acs(struct pci_dev *dev)
  * Currently this quirk does the equivalent of
  * PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF
  *
- * TODO: This quirk also needs to do equivalent of PCI_ACS_TB,
- * if dev->external_facing || dev->untrusted
+ * TODO: This quirk also needs to do the equivalent of PCI_ACS_TB when the
+ * device is external-facing or ATS is restricted by trust policy.
  */
 static int pci_quirk_enable_intel_pch_acs(struct pci_dev *dev)
 {
@@ -5567,7 +5567,8 @@ static int pci_quirk_enable_intel_spt_pch_acs(struct pci_dev *dev)
 	ctrl |= (cap & PCI_ACS_CR);
 	ctrl |= (cap & PCI_ACS_UF);
 
-	if (pci_ats_disabled() || dev->external_facing || dev->untrusted)
+	if (pci_ats_disabled() || dev->external_facing ||
+	    pci_dev_default_is_restricted(dev))
 		ctrl |= (cap & PCI_ACS_TB);
 
 	pci_write_config_dword(dev, pos + INTEL_SPT_ACS_CTRL, ctrl);
diff --git a/include/linux/device.h b/include/linux/device.h
index 2914b7191d34..5c86e7a19e47 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -933,6 +933,11 @@ static inline bool device_iommu_mapped(struct device *dev)
 	return (dev->iommu_group != NULL);
 }
 
+static inline bool device_policy_is_adversarial(const struct device *dev)
+{
+	return READ_ONCE(dev->trust_policy) == DEVICE_TRUST_POLICY_ADVERSARIAL;
+}
+
 static inline bool device_policy_is_blocked(const struct device *dev)
 {
 	return READ_ONCE(dev->trust_policy) == DEVICE_TRUST_POLICY_BLOCKED;
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96..5da9f7fabf1e 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -616,6 +616,16 @@ static inline struct pci_dev *to_pci_dev(const struct device *dev)
 {
 	return container_of(dev, struct pci_dev, dev);
 }
+
+static inline bool
+pci_dev_default_is_restricted(const struct pci_dev *pdev)
+{
+	enum device_trust_policy policy = READ_ONCE(pdev->dev.trust_policy);
+
+	return policy == DEVICE_TRUST_POLICY_BLOCKED ||
+	       policy == DEVICE_TRUST_POLICY_ADVERSARIAL;
+}
+
 #define for_each_pci_dev(d) while ((d = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, d)) != NULL)
 #define for_each_pci_dev_reverse(d) \
 	while ((d = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID, d)) != NULL)

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 29/33] iommu: Use device trust for DMA restrictions
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (27 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 28/33] PCI: Use device trust for platform restrictions Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 30/33] driver core: Allow driver attachment with the device lock held Leon Romanovsky
                   ` (3 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

PCI-specific untrusted checks exclude devices classified as adversaries
through generic policy. Their DMA protections should depend on the selected
trust policy rather than the source of that classification.

Use policy for early domain selection and active trust for DMA bouncing.
This removes the remaining need for PCI's duplicate untrusted flag.
Changes to IOMMU domains after authorization remain separate work.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 .../driver-api/driver-model/device-trust.rst          | 19 +++++++++++++++++++
 drivers/iommu/amd/iommu.c                             |  3 +--
 drivers/iommu/dma-iommu.c                             | 14 +++++---------
 drivers/iommu/intel/iommu.c                           | 14 +++++---------
 drivers/iommu/iommu.c                                 |  9 +++++----
 drivers/pci/probe.c                                   |  1 -
 include/linux/pci.h                                   |  8 --------
 7 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/Documentation/driver-api/driver-model/device-trust.rst b/Documentation/driver-api/driver-model/device-trust.rst
index a3dab9534460..abe050ca25f1 100644
--- a/Documentation/driver-api/driver-model/device-trust.rst
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -210,6 +210,25 @@ domain selection or ATS; that is follow-up work. If setup fails after
 commitment, the device keeps the committed level and cannot fall back to
 ``unrestricted``.
 
+PCI
+===
+
+PCI starts a device as ``adversarial`` if the platform reports it as
+removable through an external-facing port, or if it sits below a bridge
+whose policy is ``adversarial``. IOMMU domain selection, ATS and ACS
+Translation Blocking follow the policy at discovery; DMA bouncing follows
+the active level. Without device trust boot options this cannot change,
+so PCI devices keep the restrictions they had before.
+
+A global value other than ``bus``, or a device entry, replaces this
+classification at discovery. A global ``blocked`` or ``adversarial``
+therefore restricts every PCI device, and authorization does not revisit
+these discovery-time choices. PCI has no ``trust_resolve`` callback, so
+a device entry of ``bus`` gives ``unrestricted``. A device inherits
+``adversarial`` from its bridge only while the bridge's policy is
+``adversarial``. A driver rule applies when the driver binds and changes
+only DMA bouncing.
+
 Bus and driver interfaces
 =========================
 
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 960e1dc05ec6..e358bbb1f2ba 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -3401,8 +3401,7 @@ static int amd_iommu_def_domain_type(struct device *dev)
 	if (!dev_is_pci(dev))
 		return 0;
 
-	/* Always use DMA domain for untrusted device */
-	if (to_pci_dev(dev)->untrusted)
+	if (pci_dev_default_is_restricted(to_pci_dev(dev)))
 		return IOMMU_DOMAIN_DMA;
 
 	/* Apply device specific quirks */
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 78742aeba932..a48fb511bfe6 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -589,16 +589,11 @@ static int iova_reserve_iommu_regions(struct device *dev,
 	return ret;
 }
 
-static bool dev_is_untrusted(struct device *dev)
-{
-	return dev_is_pci(dev) && to_pci_dev(dev)->untrusted;
-}
-
 static bool dev_use_swiotlb(struct device *dev, size_t size,
 			    enum dma_data_direction dir)
 {
 	return IS_ENABLED(CONFIG_SWIOTLB) &&
-		(dev_is_untrusted(dev) ||
+		(device_is_adversarial(dev) ||
 		 dma_kmalloc_needs_bounce(dev, size, dir));
 }
 
@@ -611,7 +606,7 @@ static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg,
 	if (!IS_ENABLED(CONFIG_SWIOTLB))
 		return false;
 
-	if (dev_is_untrusted(dev))
+	if (device_is_adversarial(dev))
 		return true;
 
 	/*
@@ -1189,7 +1184,8 @@ static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,
 	 * swiotlb_tbl_map_single() has initialized the bounce buffer proper to
 	 * the contents of the original memory buffer.
 	 */
-	if (phys != (phys_addr_t)DMA_MAPPING_ERROR && dev_is_untrusted(dev)) {
+	if (phys != (phys_addr_t)DMA_MAPPING_ERROR &&
+	    device_is_adversarial(dev)) {
 		size_t start, virt = (size_t)phys_to_virt(phys);
 
 		/* Pre-padding */
@@ -1790,7 +1786,7 @@ size_t iommu_dma_max_opt_mapping_size(void)
 
 size_t iommu_dma_max_mapping_size(struct device *dev)
 {
-	if (dev_is_untrusted(dev))
+	if (device_is_adversarial(dev))
 		return swiotlb_max_mapping_size(dev);
 
 	return SIZE_MAX;
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 5a2e705a2f2e..0202b491f222 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2624,8 +2624,8 @@ static void __init platform_optin_force_iommu(void)
 		return;
 
 	/*
-	 * If Intel-IOMMU is disabled by default, we will apply identity
-	 * map for all devices except those marked as being untrusted.
+	 * If Intel-IOMMU is disabled by default, an external device could
+	 * otherwise receive an identity map despite its adversarial policy.
 	 */
 	if (dmar_policy_off()) {
 		pr_info("Intel-IOMMU force enabled due to platform opt in\n");
@@ -3629,16 +3629,12 @@ static bool intel_iommu_is_attach_deferred(struct device *dev)
 	return translation_pre_enabled(info->iommu) && !info->domain;
 }
 
-/*
- * Check that the device does not live on an external facing PCI port that is
- * marked as untrusted. Such devices should not be able to apply quirks and
- * thus not be able to bypass the IOMMU restrictions.
- */
+/* Bypass quirks must not prevent later adversarial operation. */
 static bool risky_device(struct pci_dev *pdev)
 {
-	if (pdev->untrusted) {
+	if (pci_dev_default_is_restricted(pdev)) {
 		pci_info(pdev,
-			 "Skipping IOMMU quirk for dev [%04X:%04X] on untrusted PCI link\n",
+			 "Skipping IOMMU quirk due to trust policy [%04X:%04X]\n",
 			 pdev->vendor, pdev->device);
 		pci_info(pdev, "Please check with your BIOS/Platform vendor about this\n");
 		return true;
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index b486b8bbd1fc..891b0cd2dad0 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1901,10 +1901,11 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
 		driver_type = iommu_get_def_domain_type(group, gdev->dev,
 							driver_type);
 
-		if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
+		if (dev_is_pci(gdev->dev) &&
+		    pci_dev_default_is_restricted(to_pci_dev(gdev->dev))) {
 			/*
-			 * No ARM32 using systems will set untrusted, it cannot
-			 * work.
+			 * No ARM32 using systems select a restricted trust
+			 * policy; it cannot work.
 			 */
 			if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
 				return -1;
@@ -1928,7 +1929,7 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
 		if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
 			dev_err_ratelimited(
 				untrusted,
-				"Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
+				"Device trust policy restricts DMA, but driver is overriding group %u to %s, refusing to probe.\n",
 				group->id, iommu_domain_type_str(driver_type));
 			return -1;
 		}
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 606ab8a3aa8e..d890049823a6 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -2080,7 +2080,6 @@ int pci_setup_device(struct pci_dev *dev)
 	set_pcie_cxl(dev);
 
 	pci_set_default_trust_policy(dev);
-	dev->untrusted = device_policy_is_adversarial(&dev->dev);
 	device_trust_init(&dev->dev);
 
 	/* "Unknown power state" */
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 5da9f7fabf1e..25a54b25775d 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -490,14 +490,6 @@ struct pci_dev {
 	unsigned int	shpc_managed:1;		/* SHPC owned by shpchp */
 	unsigned int	is_thunderbolt:1;	/* Thunderbolt controller */
 	unsigned int	is_cxl:1;               /* Compute Express Link (CXL) */
-	/*
-	 * Devices marked being untrusted are the ones that can potentially
-	 * execute DMA attacks and similar. They are typically connected
-	 * through external ports such as Thunderbolt but not limited to
-	 * that. When an IOMMU is enabled they should be getting full
-	 * mappings to make sure they cannot access arbitrary memory.
-	 */
-	unsigned int	untrusted:1;
 	/*
 	 * Info from the platform, e.g., ACPI or device tree, may mark a
 	 * device as "external-facing".  An external-facing device is

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 30/33] driver core: Allow driver attachment with the device lock held
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (28 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 29/33] iommu: Use device trust for DMA restrictions Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 31/33] USB: Retain the result of configuration requests Leon Romanovsky
                   ` (2 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

USB authorization needs to keep driver attachment and its configuration
result under the same device lock. Dropping the lock to call
device_attach() would let another request change that result before the
authorizing caller can inspect it.

Provide synchronous attachment for callers that already hold the device
lock. Keep asynchronous scheduling outside the lock in the existing path.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/base/dd.c      | 34 ++++++++++++++++++++++++++++++----
 include/linux/device.h |  1 +
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 4c4723384dc6..2f45b53e5ba9 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -1222,12 +1222,11 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 	put_device(dev);
 }
 
-static int __device_attach(struct device *dev, bool allow_async)
+static int __device_attach_locked(struct device *dev, bool allow_async,
+				  bool *async)
 {
 	int ret = 0;
-	bool async = false;
 
-	device_lock(dev);
 	mutex_lock(&dev->p->driver_lock);
 	if (dev->p->dead) {
 		goto out_unlock;
@@ -1265,7 +1264,7 @@ static int __device_attach(struct device *dev, bool allow_async)
 			 */
 			dev_dbg(dev, "scheduling asynchronous probe\n");
 			get_device(dev);
-			async = true;
+			*async = true;
 		} else {
 			pm_request_idle(dev);
 		}
@@ -1275,6 +1274,16 @@ static int __device_attach(struct device *dev, bool allow_async)
 	}
 out_unlock:
 	mutex_unlock(&dev->p->driver_lock);
+	return ret;
+}
+
+static int __device_attach(struct device *dev, bool allow_async)
+{
+	bool async = false;
+	int ret;
+
+	device_lock(dev);
+	ret = __device_attach_locked(dev, allow_async, &async);
 	device_unlock(dev);
 	if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
 		put_device(dev);
@@ -1284,6 +1293,23 @@ static int __device_attach(struct device *dev, bool allow_async)
 	return ret;
 }
 
+/**
+ * device_attach_locked - try to attach a driver with the device locked
+ * @dev: device to attach
+ *
+ * Synchronous device_attach() with the device lock held by the caller.
+ * When called for a USB interface, the parent lock must also be held.
+ * The device lock remains held on return.
+ *
+ * Return: 1 if bound, 0 if no driver bound, or a negative error.
+ */
+int device_attach_locked(struct device *dev)
+{
+	device_lock_assert(dev);
+	return __device_attach_locked(dev, false, NULL);
+}
+EXPORT_SYMBOL_GPL(device_attach_locked);
+
 /**
  * device_attach - try to attach device to a driver.
  * @dev: device.
diff --git a/include/linux/device.h b/include/linux/device.h
index 5c86e7a19e47..3857545f67df 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1337,6 +1337,7 @@ int __must_check device_driver_attach_unrestricted(
 int __must_check device_bind_driver(struct device *dev);
 void device_release_driver(struct device *dev);
 int  __must_check device_attach(struct device *dev);
+int __must_check device_attach_locked(struct device *dev);
 int __must_check driver_attach(const struct device_driver *drv);
 void device_initial_probe(struct device *dev);
 int __must_check device_reprobe(struct device *dev);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 31/33] USB: Retain the result of configuration requests
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (29 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 30/33] driver core: Allow driver attachment with the device lock held Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 32/33] USB: Authorize interfaces through device trust Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 33/33] USB: Authorize devices " Leon Romanovsky
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

The generic USB device driver can bind successfully even when setting its
configuration fails. Authorization that attaches this driver therefore
cannot use the attachment result to report configuration errors or decide
whether setup can be retried.

Retain the last configuration result for callers holding the device lock.
Keep driver binding successful so userspace can recover by selecting
another configuration.

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

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index ca6976e266f0..32014605b6db 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -2042,6 +2042,8 @@ static bool usb_has_claimant(struct usb_device *dev,
 	return false;
 }
 
+static int usb_set_configuration_internal(struct usb_device *dev, int configuration);
+
 /*
  * usb_set_configuration - Makes a particular device setting be current
  * @dev: the device whose configuration is being updated
@@ -2089,6 +2091,15 @@ static bool usb_has_claimant(struct usb_device *dev,
  * drivers currently known to the kernel.
  */
 int usb_set_configuration(struct usb_device *dev, int configuration)
+{
+	int ret = usb_set_configuration_internal(dev, configuration);
+
+	dev->config_error = ret;
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_set_configuration);
+
+static int usb_set_configuration_internal(struct usb_device *dev, int configuration)
 {
 	int i, ret;
 	struct usb_host_config *cp = NULL;
@@ -2347,7 +2358,6 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 	usb_autosuspend_device(dev);
 	return 0;
 }
-EXPORT_SYMBOL_GPL(usb_set_configuration);
 
 static LIST_HEAD(set_config_list);
 static DEFINE_SPINLOCK(set_config_lock);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index a5e706f67f33..d372712eba34 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -599,6 +599,7 @@ struct usb3_lpm_parameters {
  * @descriptor: USB device descriptor
  * @bos: USB device BOS descriptor set
  * @config: all of the device's configs
+ * @config_error: Result of the last configuration request.
  * @actconfig: the active configuration
  * @ep_in: array of IN endpoints
  * @ep_out: array of OUT endpoints
@@ -686,6 +687,7 @@ struct usb_device {
 	struct usb_host_config *config;
 
 	struct usb_host_config *actconfig;
+	int config_error;
 	struct usb_host_endpoint *ep_in[16];
 	struct usb_host_endpoint *ep_out[16];
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 32/33] USB: Authorize interfaces through device trust
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (30 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 31/33] USB: Retain the result of configuration requests Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  2026-12-03 12:03 ` [PATCH v5 33/33] USB: Authorize devices " Leon Romanovsky
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

An interface denied by USB defaults must remain blocked until its trust
policy permits binding. Keeping an independent denial flag would prevent
generic trust authorization from making that interface usable.

Represent the initial denial as blocked policy when trust is enabled, and
let interface authorization request unrestricted operation. Preserve
explicit USB deauthorization and report rejected policy changes to
userspace. Interface policies remain independent of the device policy.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-bus-usb |  5 +++++
 Documentation/usb/authorization.rst     | 11 +++++++++++
 drivers/usb/core/driver.c               |  2 +-
 drivers/usb/core/message.c              | 23 ++++++++++++++++++-----
 drivers/usb/core/sysfs.c                |  7 +++++--
 drivers/usb/core/usb.h                  |  2 +-
 include/linux/usb.h                     |  9 +++++++--
 7 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb
index 5213117fd957..62e326710ef5 100644
--- a/Documentation/ABI/testing/sysfs-bus-usb
+++ b/Documentation/ABI/testing/sysfs-bus-usb
@@ -12,6 +12,11 @@ Description:
 
 		A deauthorized interface cannot be probed or claimed.
 
+		With device trust enabled at boot, an interface that is not
+		authorized by default starts with the "blocked" trust policy
+		(see sysfs-devices-trust). Writing 1 then requests the "unrestricted"
+		policy. Deauthorizing does not change the committed trust level.
+
 What:		/sys/bus/usb/devices/usbX/interface_authorized_default
 Date:		August 2015
 Description:
diff --git a/Documentation/usb/authorization.rst b/Documentation/usb/authorization.rst
index 150a14970e95..cc419a11ffc4 100644
--- a/Documentation/usb/authorization.rst
+++ b/Documentation/usb/authorization.rst
@@ -120,6 +120,13 @@ Deny interfaces per default::
 Per default the interface_authorized_default bit is 1.
 So all interfaces would authorized per default.
 
+With device trust enabled, an interface that is not authorized by default
+starts with the "blocked" trust policy, and writing 1 to its "authorized"
+attribute requests the "unrestricted" policy. Device and interface trust policies
+are independent: selecting "adversarial" for a USB device does not select it
+for its interfaces. Set interface policies separately before their drivers
+bind, using boot rules when necessary.
+
 Note:
   If a deauthorized interface will be authorized so the driver probing must
   be triggered manually by writing INTERFACE to /sys/bus/usb/drivers_probe
@@ -127,3 +134,7 @@ Note:
 For drivers that need multiple interfaces all needed interfaces should be
 authorized first. After that the drivers should be probed.
 This avoids side effects.
+
+Driver boot rules apply when an interface is claimed, including before
+registration. An interface deauthorized through sysfs must still be
+authorized before it can be claimed.
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 1d488111a70f..80f4d2644023 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -338,7 +338,7 @@ static int usb_probe_interface(struct device *dev)
 	if (udev->authorized == 0) {
 		dev_info(&intf->dev, "Device is not authorized for usage\n");
 		return error;
-	} else if (intf->authorized == 0) {
+	} else if (!usb_interface_authorized(intf)) {
 		dev_info(&intf->dev, "Interface %d is not authorized for usage\n",
 				intf->altsetting->desc.bInterfaceNumber);
 		return error;
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 32014605b6db..534f8e645a85 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1881,16 +1881,24 @@ void usb_deauthorize_interface(struct usb_interface *intf)
  * usb_authorize_interface - authorize an USB interface
  *
  * @intf: USB interface structure
+ *
+ * Return: 0 on success, or the error of the trust policy request.
  */
-void usb_authorize_interface(struct usb_interface *intf)
+int usb_authorize_interface(struct usb_interface *intf)
 {
 	struct device *dev = &intf->dev;
+	int ret = 0;
 
-	if (!intf->authorized) {
-		device_lock(dev);
-		intf->authorized = 1; /* authorize interface */
-		device_unlock(dev);
+	device_lock(dev);
+	if (device_policy_is_blocked(dev)) {
+		ret = device_trust_set_policy_locked(dev, DEVICE_TRUST_POLICY_UNRESTRICTED);
+		if (ret)
+			goto out_unlock;
 	}
+	intf->authorized = 1;
+out_unlock:
+	device_unlock(dev);
+	return ret;
 }
 
 static int usb_if_uevent(const struct device *dev, struct kobj_uevent_env *env)
@@ -2213,6 +2221,11 @@ static int usb_set_configuration_internal(struct usb_device *dev, int configurat
 		intf->altsetting = intfc->altsetting;
 		intf->num_altsetting = intfc->num_altsetting;
 		intf->authorized = !!HCD_INTF_AUTHORIZED(hcd);
+		if (device_trust_enabled()) {
+			if (!intf->authorized)
+				intf->dev.trust_policy = DEVICE_TRUST_POLICY_BLOCKED;
+			intf->authorized = 1;
+		}
 		kref_get(&intfc->ref);
 
 		alt = usb_altnum_to_altsetting(intf, 0);
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index d22dc78457d7..b8974e9db3a2 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -1213,7 +1213,7 @@ static ssize_t interface_authorized_show(struct device *dev,
 {
 	struct usb_interface *intf = to_usb_interface(dev);
 
-	return sysfs_emit(buf, "%u\n", intf->authorized);
+	return sysfs_emit(buf, "%u\n", usb_interface_authorized(intf));
 }
 
 /*
@@ -1225,12 +1225,15 @@ static ssize_t interface_authorized_store(struct device *dev,
 	struct usb_interface *intf = to_usb_interface(dev);
 	bool val;
 	struct kernfs_node *kn;
+	int ret;
 
 	if (kstrtobool(buf, &val) != 0)
 		return -EINVAL;
 
 	if (val) {
-		usb_authorize_interface(intf);
+		ret = usb_authorize_interface(intf);
+		if (ret)
+			return ret;
 	} else {
 		/*
 		 * Prevent deadlock if another process is concurrently
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index cbee73183747..6f65d6cd5d11 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -34,7 +34,7 @@ extern void usb_disable_device(struct usb_device *dev, int skip_ep0);
 extern int usb_deauthorize_device(struct usb_device *);
 extern int usb_authorize_device(struct usb_device *);
 extern void usb_deauthorize_interface(struct usb_interface *);
-extern void usb_authorize_interface(struct usb_interface *);
+extern int usb_authorize_interface(struct usb_interface *);
 extern void usb_detect_quirks(struct usb_device *udev);
 extern void usb_detect_interface_quirks(struct usb_device *udev);
 extern void usb_release_quirk_list(void);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index d372712eba34..9399c816b367 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -204,8 +204,8 @@ enum usb_wireless_status {
  *	has been deferred.
  * @needs_binding: flag set when the driver should be re-probed or unbound
  *	following a reset or suspend operation it doesn't support.
- * @authorized: This allows to (de)authorize individual interfaces instead
- *	a whole device in contrast to the device authorization.
+ * @authorized: USB authorization flag; trust policy may also restrict use,
+ *	see usb_interface_authorized().
  * @wireless_status: if the USB device uses a receiver/emitter combo, whether
  *	the emitter is connected.
  * @wireless_status_work: Used for scheduling wireless status changes
@@ -278,6 +278,11 @@ struct usb_interface {
 
 #define to_usb_interface(__dev)	container_of_const(__dev, struct usb_interface, dev)
 
+static inline bool usb_interface_authorized(const struct usb_interface *intf)
+{
+	return intf->authorized && !device_policy_is_blocked(&intf->dev);
+}
+
 static inline void *usb_get_intfdata(struct usb_interface *intf)
 {
 	return dev_get_drvdata(&intf->dev);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v5 33/33] USB: Authorize devices through device trust
  2026-12-03 12:03 [PATCH v5 00/33] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (31 preceding siblings ...)
  2026-12-03 12:03 ` [PATCH v5 32/33] USB: Authorize interfaces through device trust Leon Romanovsky
@ 2026-12-03 12:03 ` Leon Romanovsky
  32 siblings, 0 replies; 34+ messages in thread
From: Leon Romanovsky @ 2026-12-03 12:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Dmitry Torokhov, Andrew Lunn, Heiner Kallweit, Russell King,
	David S. Miller, Jakub Kicinski, Paolo Abeni, Johannes Berg,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Bjorn Helgaas,
	Joerg Roedel (AMD), Suravee Suthikulpanit, Vasant Hegde,
	Will Deacon, Robin Murphy, David Woodhouse, Lu Baolu,
	Johan Hovold, Eric Dumazet, Marcel Holtmann,
	Luiz Augusto von Dentz, Nipun Gupta, Nikhil Agarwal,
	Pieter Jansen van Vuuren, Chris Lu, Mans Rullgard, Alan Stern,
	Jaroslav Kysela, Takashi Iwai, Andrew Morton, Cornelia Huck,
	Andrew Morton, Jeff Garzik, Rafael J. Wysocki, Bart Van Assche,
	Alexander Duyck, Dan Williams, Lukas Wunner
  Cc: linux-kernel, driver-core, linux-input, netdev, linux-wireless,
	linux-usb, linux-bluetooth, linux-acpi, linux-doc, linux-pci,
	iommu, linux-coco, Alexey Kardashevskiy, Luiz Augusto von Dentz,
	Greg Kroah-Hartman, linux-sound, Jason Gunthorpe

From: Leon Romanovsky <leonro@nvidia.com>

USB authorization can configure a device and expose its interfaces. With
generic trust enabled, that must wait until the device driver has been
admitted, otherwise configuration bypasses policy.

Keep authorization, attachment and the configuration result under the
device lock so concurrent requests cannot report each other's failures.
Allow configuration retries after failure while preserving authorization
and any active configuration. Retain the existing flow on legacy boots.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 Documentation/ABI/testing/sysfs-bus-usb | 13 +++++++++++
 Documentation/usb/authorization.rst     | 26 ++++++++++++++++++++++
 drivers/usb/core/driver.c               | 10 +++++----
 drivers/usb/core/generic.c              |  2 +-
 drivers/usb/core/hub.c                  | 39 +++++++++++++++++++++++++++------
 drivers/usb/core/message.c              |  7 ++++--
 drivers/usb/core/sysfs.c                |  2 +-
 drivers/usb/core/trace.h                |  2 +-
 drivers/usb/core/usb.c                  |  6 +++++
 include/linux/usb.h                     | 12 +++++-----
 10 files changed, 98 insertions(+), 21 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb
index 62e326710ef5..dbe70c481e7c 100644
--- a/Documentation/ABI/testing/sysfs-bus-usb
+++ b/Documentation/ABI/testing/sysfs-bus-usb
@@ -33,6 +33,19 @@ Description:
 		drivers, non-authorized one are not.  By default, wired
 		USB devices are authorized.
 
+		With device trust enabled at boot, a device that is not
+		authorized by default starts with the "blocked" trust policy
+		(see sysfs-devices-trust). Writing 1 requests the "unrestricted" policy
+		if the policy is blocked, then binds and configures the device.
+		Configuration errors leave authorization granted. If setup failed
+		and no configuration remains, write 1 again to retry, or select a
+		configuration through bConfigurationValue after driver admission.
+		Writing 1 preserves an existing configuration. Finding no suitable
+		configuration remains successful authorization without setup.
+		Writing 0 deconfigures it without changing its committed trust
+		level. Without trust boot options, the existing authorization
+		flow is unchanged.
+
 What:		/sys/bus/usb/drivers/.../new_id
 Date:		October 2011
 Contact:	linux-usb@vger.kernel.org
diff --git a/Documentation/usb/authorization.rst b/Documentation/usb/authorization.rst
index cc419a11ffc4..062b2c614d0e 100644
--- a/Documentation/usb/authorization.rst
+++ b/Documentation/usb/authorization.rst
@@ -92,6 +92,32 @@ can fake descriptors and device info. Don't trust that. You are
 welcome.
 
 
+Device trust
+------------
+
+Without device trust boot options, USB authorization is unchanged. With
+device trust enabled, authorization uses the policy described in
+Documentation/driver-api/driver-model/device-trust.rst. A device that the
+authorized_default setting does not authorize starts with the "blocked"
+trust policy and reads 0 in "authorized". Boot device rules can replace
+this initial policy.
+
+Writing 1 to "authorized" for such a device requests the "unrestricted" policy and
+binds the device, which configures it. To operate it as an adversary,
+enable trust policy writes at boot, for example with
+``device_trust.default=bus``, and write "adversarial" to its
+"trust_policy" attribute before writing 1. The choice is fixed for the life
+of the device: writing 0 deconfigures it, and a later 1 configures it again
+at the same trust level.
+
+Configuration errors leave authorization granted. If setup failed and no
+configuration remains, write 1 again to retry, or use "bConfigurationValue"
+to select a configuration after the device driver has been admitted.
+Writing 1 preserves an existing configuration. As before, failure to choose
+a suitable configuration is not an authorization error; an explicit
+"bConfigurationValue" selection remains available.
+
+
 Interface authorization
 -----------------------
 
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 80f4d2644023..0eb35e64b770 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -335,7 +335,7 @@ static int usb_probe_interface(struct device *dev)
 	if (usb_device_is_owned(udev))
 		return error;
 
-	if (udev->authorized == 0) {
+	if (!usb_device_authorized(udev)) {
 		dev_info(&intf->dev, "Device is not authorized for usage\n");
 		return error;
 	} else if (!usb_interface_authorized(intf)) {
@@ -1350,8 +1350,7 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
 	if (udev->state == USB_STATE_NOTATTACHED)
 		goto done;
 
-	/* Can't resume it if it doesn't have a driver. */
-	if (udev->dev.driver == NULL) {
+	if (!udev->dev.driver && !device_trust_enabled()) {
 		status = -ENOTCONN;
 		goto done;
 	}
@@ -1366,7 +1365,10 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
 	if (udev->quirks & USB_QUIRK_RESET_RESUME)
 		udev->reset_resume = 1;
 
-	udriver = to_usb_device_driver(udev->dev.driver);
+	if (udev->dev.driver)
+		udriver = to_usb_device_driver(udev->dev.driver);
+	else
+		udriver = &usb_generic_driver;
 	if (udriver->generic_subclass)
 		status = usb_generic_driver_resume(udev, msg);
 	if (status == 0 && udriver->resume)
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index a48994e11ef3..bcfc01eefd09 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -242,7 +242,7 @@ int usb_generic_driver_probe(struct usb_device *udev)
 	/* Choose and set the configuration.  This registers the interfaces
 	 * with the driver core and lets interface drivers bind to them.
 	 */
-	if (udev->authorized == 0)
+	if (!usb_device_authorized(udev))
 		dev_info(&udev->dev, "Device is not authorized for usage\n");
 	else {
 		c = usb_choose_configuration(udev);
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 372bd6368cc9..c94539b0c697 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -2773,7 +2773,8 @@ int usb_new_device(struct usb_device *udev)
 int usb_deauthorize_device(struct usb_device *usb_dev)
 {
 	usb_lock_device(usb_dev);
-	if (usb_dev->authorized == 0)
+	if (!usb_dev->authorized &&
+	    (!device_trust_enabled() || !usb_dev->config_error))
 		goto out_unauthorized;
 
 	usb_dev->authorized = 0;
@@ -2787,17 +2788,41 @@ int usb_deauthorize_device(struct usb_device *usb_dev)
 
 int usb_authorize_device(struct usb_device *usb_dev)
 {
+	bool attach;
 	int result = 0, c;
 
 	usb_lock_device(usb_dev);
-	if (usb_dev->authorized == 1)
-		goto out_authorized;
+	if (device_policy_is_blocked(&usb_dev->dev)) {
+		result = device_trust_set_policy_locked(&usb_dev->dev,
+							DEVICE_TRUST_POLICY_UNRESTRICTED);
+		if (result)
+			goto out_unlock;
+	}
+
+	attach = device_trust_enabled() && !usb_dev->dev.driver;
+	if (!attach && usb_dev->authorized == 1 &&
+	    (!device_trust_enabled() || !usb_dev->config_error ||
+	     usb_dev->actconfig))
+		goto out_unlock;
 
 	result = usb_autoresume_device(usb_dev);
 	if (result < 0) {
 		dev_err(&usb_dev->dev,
 			"can't autoresume for authorization: %d\n", result);
-		goto error_autoresume;
+		goto out_unlock;
+	}
+
+	if (attach) {
+		usb_dev->authorized = 1;
+		usb_dev->config_error = 0;
+		result = device_attach_locked(&usb_dev->dev);
+		if (result <= 0) {
+			if (result >= 0)
+				result = -ENODEV;
+		} else {
+			result = usb_dev->config_error;
+		}
+		goto out_autosuspend;
 	}
 
 	usb_dev->authorized = 1;
@@ -2816,10 +2841,10 @@ int usb_authorize_device(struct usb_device *usb_dev)
 	}
 	dev_info(&usb_dev->dev, "authorized to connect\n");
 
+out_autosuspend:
 	usb_autosuspend_device(usb_dev);
-error_autoresume:
-out_authorized:
-	usb_unlock_device(usb_dev);	/* complements locktree */
+out_unlock:
+	usb_unlock_device(usb_dev);
 	return result;
 }
 
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 534f8e645a85..17ce694bb7da 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -2081,7 +2081,7 @@ static int usb_set_configuration_internal(struct usb_device *dev, int configurat
  * channels are available independently; and choosing between open
  * standard device protocols (like CDC) or proprietary ones.
  *
- * Note that a non-authorized device (dev->authorized == 0) will only
+ * Note that a non-authorized device (see usb_device_authorized()) will only
  * be put in unconfigured mode.
  *
  * Note that USB has an additional level of device configurability,
@@ -2115,7 +2115,7 @@ static int usb_set_configuration_internal(struct usb_device *dev, int configurat
 	struct usb_hcd *hcd = bus_to_hcd(dev->bus);
 	int n, nintf;
 
-	if (dev->authorized == 0 || configuration == -1)
+	if (!usb_device_authorized(dev) || configuration == -1)
 		configuration = 0;
 	else {
 		for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
@@ -2128,6 +2128,9 @@ static int usb_set_configuration_internal(struct usb_device *dev, int configurat
 	}
 	if ((!cp && configuration != 0))
 		return -EINVAL;
+	if (cp && device_trust_enabled() &&
+	    device_get_trust_level(&dev->dev) == DEVICE_TRUST_BLOCKED)
+		return -EPERM;
 
 	/* The USB spec says configuration 0 means unconfigured.
 	 * But if a device includes a configuration numbered 0,
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index b8974e9db3a2..189fb917b81e 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -728,7 +728,7 @@ static ssize_t authorized_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
 	struct usb_device *usb_dev = to_usb_device(dev);
-	return sysfs_emit(buf, "%u\n", usb_dev->authorized);
+	return sysfs_emit(buf, "%u\n", usb_device_authorized(usb_dev));
 }
 
 /*
diff --git a/drivers/usb/core/trace.h b/drivers/usb/core/trace.h
index 903e57dc273a..42f9cfcd8b0f 100644
--- a/drivers/usb/core/trace.h
+++ b/drivers/usb/core/trace.h
@@ -27,7 +27,7 @@ DECLARE_EVENT_CLASS(usb_core_log_usb_device,
 		__entry->speed = udev->speed;
 		__entry->state = udev->state;
 		__entry->bus_mA = udev->bus_mA;
-		__entry->authorized = udev->authorized;
+		__entry->authorized = usb_device_authorized(udev);
 	),
 	TP_printk("usb %s speed %s state %s %dmA [%s]",
 		__get_str(name),
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 2559a180fdee..180f4d797b10 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -748,6 +748,12 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
 #endif
 
 	dev->authorized = usb_dev_authorized(dev, usb_hcd);
+	if (device_trust_enabled()) {
+		if (!dev->authorized)
+			dev->dev.trust_policy = DEVICE_TRUST_POLICY_BLOCKED;
+		dev->authorized = 1;
+	}
+	device_trust_init(&dev->dev);
 	trace_usb_alloc_dev(dev);
 	return dev;
 }
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 9399c816b367..34ee4d6f91c1 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -617,11 +617,8 @@ struct usb3_lpm_parameters {
  * @persist_enabled:  USB_PERSIST enabled for this device
  * @reset_in_progress: the device is being reset
  * @have_langid: whether string_langid is valid
- * @authorized: policy has said we can use it;
- *	(user space) policy determines if we authorize this device to be
- *	used or not. By default, wired USB devices are authorized.
- *	WUSB devices are not, until we authorize them from user space.
- *	FIXME -- complete doc
+ * @authorized: USB authorization flag; trust policy may also restrict use,
+ *	see usb_device_authorized().
  * @authenticated: Crypto authentication passed
  * @tunnel_mode: Connection native or tunneled over USB4
  * @usb4_link: device link to the USB4 host interface
@@ -756,6 +753,11 @@ struct usb_device {
 
 #define to_usb_device(__dev)	container_of_const(__dev, struct usb_device, dev)
 
+static inline bool usb_device_authorized(const struct usb_device *udev)
+{
+	return udev->authorized && !device_policy_is_blocked(&udev->dev);
+}
+
 static inline struct usb_device *__intf_to_usbdev(struct usb_interface *intf)
 {
 	return to_usb_device(intf->dev.parent);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2026-12-03 12:05 UTC | newest]

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox