Linux Documentation
 help / color / mirror / Atom feed
* [PATCH v3 00/31] driver core: Authorize device trust before driver binding
@ 2026-11-18 19:26 Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 01/31] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
                   ` (30 more replies)
  0 siblings, 31 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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

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=default        General: use each bus's initial policy.
                                    PCI: use platform trust policy; keep
                                      existing domain and ATS decisions.
                                    USB: devices and interfaces denied by
                                      USB defaults wait for authorization
                                      before binding.

device_trust.default=blocked        General: block driver binding until
                                      authorization.
                                    PCI: keep existing domain and ATS
                                      decisions while binding is blocked.
                                    USB: leave devices and interfaces
                                      unbound until authorized.

device_trust.default=adversary      General: allow binding as an adversary.
                                    PCI: keep existing platform-based DMA
                                      handling, domain and ATS decisions.
                                    USB: allow binding as an adversary,
                                      overriding USB authorization
                                      defaults.

device_trust.default=unrestricted   General: allow unrestricted binding;
                                      freeze trust policy changes.
                                    PCI: unrestricted active trust; keep
                                      existing domain and ATS decisions.
                                    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 select admission policy;
                                      existing domain and ATS decisions
                                      remain.
                                    USB: matching rules select device or
                                      interface policy.

Signed-off-by: Leon Romanovsky <leonro@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 (31):
      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: 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
      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    |   35 +
 .../driver-api/driver-model/device-trust.rst       |  323 +++
 Documentation/driver-api/driver-model/index.rst    |    1 +
 Documentation/usb/authorization.rst                |   37 +
 MAINTAINERS                                        |    2 +
 drivers/base/Makefile                              |    2 +-
 drivers/base/base.h                                |   52 +
 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              | 2461 ++++++++++++++++++++
 drivers/base/trust.c                               |  508 ++++
 drivers/bluetooth/btusb_main.c                     |   57 +-
 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/driver.c                          |  121 +-
 drivers/usb/core/generic.c                         |    2 +-
 drivers/usb/core/hub.c                             |   39 +-
 drivers/usb/core/message.c                         |  106 +-
 drivers/usb/core/sysfs.c                           |    9 +-
 drivers/usb/core/trace.h                           |    2 +-
 drivers/usb/core/usb.c                             |    5 +
 drivers/usb/core/usb.h                             |    6 +-
 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 +
 47 files changed, 4190 insertions(+), 213 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260915-device-trust-core-12934e618489

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


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

* [PATCH v3 01/31] wifi: mac80211_hwsim: lock device for manual driver binding
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 02/31] Bluetooth: btusb: Fix MediaTek ISO claim locking Leon Romanovsky
                   ` (29 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 e53714b81ec2c..b796da3dd54b3 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] 32+ messages in thread

* [PATCH v3 02/31] Bluetooth: btusb: Fix MediaTek ISO claim locking
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 01/31] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 03/31] USB: Lock interfaces during manual driver binding Leon Romanovsky
                   ` (28 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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. This change
propagates claim errors to setup together with -EHOSTUNREACH which means
that the parent is suspended, since subsequent controller setup requires
the parent device.

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

diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 92a78a7e60396..8307fd819cc2e 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2993,40 +2993,57 @@ 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)
+		goto err_clear_iso_intf;
+
+	/* 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 err_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;
-		bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
-		return;
-	}
+	if (err)
+		goto err_unlock;
 
-	set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
 	init_usb_anchor(&btmtk_data->isopkt_anchor);
+	set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
+	usb_unlock_device(data->udev);
+	usb_autopm_put_interface(data->intf);
+	return 0;
+
+err_unlock:
+	usb_unlock_device(data->udev);
+err_put_interface:
+	usb_autopm_put_interface(data->intf);
+err_clear_iso_intf:
+	bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
+	return err;
 }
 
 static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
@@ -3169,7 +3186,11 @@ 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 (err) {
+			btmtk_data->isopkt_intf = NULL;
+			return err;
+		}
 	}
 
 	err = btmtk_usb_setup(hdev);

-- 
2.56.1


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

* [PATCH v3 03/31] USB: Lock interfaces during manual driver binding
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 01/31] wifi: mac80211_hwsim: lock device for manual " Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 02/31] Bluetooth: btusb: Fix MediaTek ISO claim locking Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 04/31] USB: serial: Register USB drivers with their owning module Leon Romanovsky
                   ` (27 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 8307fd819cc2e..f7f312af35c81 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 err_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)
 		goto err_unlock;
 
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d3a61ebc6afb0..e1de348cc0cac 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] 32+ messages in thread

* [PATCH v3 04/31] USB: serial: Register USB drivers with their owning module
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (2 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 03/31] USB: Lock interfaces during manual driver binding Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 05/31] USB: Reject claims of DT-disabled interfaces Leon Romanovsky
                   ` (26 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 4a19621f08ba2..ccbd0875339a3 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -1513,6 +1513,7 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
  * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
  * @owner: owning module
  * @name: name of the usb_driver for this set of @serial_drivers
+ * @mod_name: name of the module containing the serial drivers
  * @id_table: list of all devices this @serial_drivers set binds to
  *
  * Registers all the drivers in the @serial_drivers array, and dynamically
@@ -1520,6 +1521,7 @@ static void usb_serial_deregister(struct usb_serial_driver *device)
  */
 int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
 				  struct module *owner, const char *name,
+				  const char *mod_name,
 				  const struct usb_device_id *id_table)
 {
 	int rc;
@@ -1559,7 +1561,7 @@ int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers
 		}
 	}
 
-	rc = usb_register(udriver);
+	rc = usb_register_driver(udriver, owner, mod_name);
 	if (rc)
 		goto err_free_driver;
 
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 5866f19d3c7b8..fcfd0e26e0ffb 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -321,9 +321,11 @@ struct usb_serial_driver {
 	container_of(d, struct usb_serial_driver, driver)
 
 #define usb_serial_register_drivers(serial_drivers, name, id_table) \
-	__usb_serial_register_drivers(serial_drivers, THIS_MODULE, name, id_table)
+	__usb_serial_register_drivers(serial_drivers, THIS_MODULE, name, \
+				      KBUILD_MODNAME, id_table)
 int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
 				  struct module *owner, const char *name,
+				  const char *mod_name,
 				  const struct usb_device_id *id_table);
 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[]);
 void usb_serial_port_softint(struct usb_serial_port *port);

-- 
2.56.1


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

* [PATCH v3 05/31] USB: Reject claims of DT-disabled interfaces
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (3 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 04/31] USB: serial: Register USB drivers with their owning module Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 06/31] USB: Release interface claims according to binding state Leon Romanovsky
                   ` (25 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 e1de348cc0cac..735ae3310b45d 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] 32+ messages in thread

* [PATCH v3 06/31] USB: Release interface claims according to binding state
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (4 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 05/31] USB: Reject claims of DT-disabled interfaces Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 07/31] USB: Reject claims after interface registration failure Leon Romanovsky
                   ` (24 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 735ae3310b45d..926f63cd2c834 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 != &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] 32+ messages in thread

* [PATCH v3 07/31] USB: Reject claims after interface registration failure
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (5 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 06/31] USB: Release interface claims according to binding state Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 08/31] ALSA: usb-audio: Release interface claims when probe fails Leon Romanovsky
                   ` (23 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 926f63cd2c834..0a6fdf2e88f4e 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 a738d03c23eac..e56079398d26e 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;
@@ -2278,6 +2283,11 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 			intf->cur_altsetting->desc.bInterfaceNumber);
 		device_enable_async_suspend(&intf->dev);
 		ret = device_add(&intf->dev);
+		if (ret) {
+			device_lock(&intf->dev);
+			intf->unregistering = 1;
+			device_unlock(&intf->dev);
+		}
 		if (ret != 0) {
 			dev_err(&dev->dev, "device_add(%s) --> %d\n",
 				dev_name(&intf->dev), ret);

-- 
2.56.1


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

* [PATCH v3 08/31] ALSA: usb-audio: Release interface claims when probe fails
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (6 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 07/31] USB: Reject claims after interface registration failure Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 09/31] USB: Pin modules for unregistered interface claims Leon Romanovsky
                   ` (22 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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>

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")
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 59979b793fe91..56ebdd61f72e7 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] 32+ messages in thread

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

-- 
2.56.1


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

* [PATCH v3 10/31] USB: Add helpers to release unpublished interface claims
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (8 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 09/31] USB: Pin modules for unregistered interface claims Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 11/31] USB: Unwind interface claims after registration or binding failure Leon Romanovsky
                   ` (20 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 b7bd9226d2627..3beed56fabf3d 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_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 dfa48f43046d4..1b2de58fd6cef 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] 32+ messages in thread

* [PATCH v3 11/31] USB: Unwind interface claims after registration or binding failure
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (9 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 10/31] USB: Add helpers to release unpublished " Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 12/31] PNP: Attach card devices through the driver core Leon Romanovsky
                   ` (19 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 and binding failure can
also clear dev->driver without disconnecting the claimant.

Release the failed claim, remove the configuration if sibling claims
remain, and disconnect unpublished claims before freeing interfaces or
dropping the module reference.

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/driver.c  |  3 ++-
 drivers/usb/core/message.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 3beed56fabf3d..e2a120c64f6e1 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -488,7 +488,8 @@ static int usb_unbind_interface(struct device *dev)
 		/* Already in altsetting 0 so skip Set-Interface.
 		 * Just re-enable it without affecting the endpoint toggles.
 		 */
-		usb_enable_interface(udev, intf, false);
+		if (!intf->unregistering)
+			usb_enable_interface(udev, intf, false);
 	} else if (!error && !intf->dev.power.is_prepared) {
 		r = usb_set_interface(udev, intf->altsetting[0].
 				desc.bInterfaceNumber, 0);
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 306978caaf1ef..b2e5ed35a4a59 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,6 +2308,7 @@ 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);
@@ -2293,7 +2317,22 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
 			intf->unregistering = 1;
 			device_unlock(&intf->dev);
 		}
-		module_put(claim_owner);
+		if (claimant && (ret || !intf->dev.driver) &&
+		    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) {
 			dev_err(&dev->dev, "device_add(%s) --> %d\n",
 				dev_name(&intf->dev), ret);

-- 
2.56.1


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

* [PATCH v3 12/31] PNP: Attach card devices through the driver core
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (10 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 11/31] USB: Unwind interface claims after registration or binding failure Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 13/31] cdx: Propagate DMA configuration errors Leon Romanovsky
                   ` (18 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 df5e3d3cbf6c0..9aaaa03d5ffa9 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] 32+ messages in thread

* [PATCH v3 13/31] cdx: Propagate DMA configuration errors
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (11 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 12/31] PNP: Attach card devices through the driver core Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 14/31] driver core: Report deferred probes through sysfs drivers_probe Leon Romanovsky
                   ` (17 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 a118a89a7779d..0a030bf522690 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] 32+ messages in thread

* [PATCH v3 14/31] driver core: Report deferred probes through sysfs drivers_probe
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (12 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 13/31] cdx: Propagate DMA configuration errors Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 15/31] driver core: Add a compatibility API for manual driver attachment Leon Romanovsky
                   ` (16 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 0000000000000..ed8828e55d6bf
--- /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 93abbb315065d..b726344362b41 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 396097fdb3162..ecff32c3704b5 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] 32+ messages in thread

* [PATCH v3 15/31] driver core: Add a compatibility API for manual driver attachment
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (13 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 14/31] driver core: Report deferred probes through sysfs drivers_probe Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 16/31] Input: gameport - use the compatibility driver attachment API Leon Romanovsky
                   ` (15 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 866b2beb6baa2..61f49fc142f45 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 cbe05197d4a0b..92d25bf4f8632 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 52123251965de..6ac1486b9fbb5 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 429e39f647cf9..5433b2609ab30 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] 32+ messages in thread

* [PATCH v3 16/31] Input: gameport - use the compatibility driver attachment API
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (14 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 15/31] driver core: Add a compatibility API for manual driver attachment Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 17/31] Input: serio " Leon Romanovsky
                   ` (14 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 9707b155bc947..88b1a3464bf3e 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] 32+ messages in thread

* [PATCH v3 17/31] Input: serio - use the compatibility driver attachment API
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (15 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 16/31] Input: gameport - use the compatibility driver attachment API Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 18/31] net: phy: Use " Leon Romanovsky
                   ` (13 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 54dd26249b027..f239217d7de45 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] 32+ messages in thread

* [PATCH v3 18/31] net: phy: Use the compatibility driver attachment API
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (16 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 17/31] Input: serio " Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 19/31] driver core: Introduce a generic device trust level Leon Romanovsky
                   ` (12 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 5d4508bcfbe99..4f20b451e270d 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] 32+ messages in thread

* [PATCH v3 19/31] driver core: Introduce a generic device trust level
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (17 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 18/31] net: phy: Use " Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 20/31] driver core: Expose device trust authorization interfaces Leon Romanovsky
                   ` (11 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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.

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 0b3c140babd94..81f98655f5297 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: { default | blocked | adversary | 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. default preserves 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 435710f643a5c..cb3bd5fa2287a 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 61f49fc142f45..56f1a9c73964b 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 adversary or unrestricted and enters
+ *	%DEVICE_TRUST_AUTHORIZED; selecting default is rejected.
+ * @DEVICE_TRUST_AUTHORIZED: Adversary 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 92d25bf4f8632..c0006b3aa38f7 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 6ac1486b9fbb5..4c4723384dc66 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 0000000000000..35893786a163d
--- /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_ADVERSARY ?
+			DEVICE_TRUST_ADVERSARY : 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_ADVERSARY.
+ */
+bool device_is_adversarial(const struct device *dev)
+{
+	return device_get_trust_level(dev) == DEVICE_TRUST_ADVERSARY;
+}
+EXPORT_SYMBOL_GPL(device_is_adversarial);
+
+static const char * const device_trust_policy_names[] = {
+	[DEVICE_TRUST_POLICY_DEFAULT] = "default",
+	[DEVICE_TRUST_POLICY_BLOCKED] = "blocked",
+	[DEVICE_TRUST_POLICY_ADVERSARY] = "adversary",
+	[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_DEFAULT)
+		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 e2a120c64f6e1..b10b80b5fd6d4 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -583,6 +583,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;
@@ -620,6 +626,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:
@@ -635,6 +642,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 b2e5ed35a4a59..b8e2d864faa46 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
@@ -2324,6 +2326,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 5433b2609ab30..702eb66b81bf3 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 9ef7538a9a7f2..ba2b5743a47cd 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 0000000000000..32071f12ed257
--- /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_ADVERSARY: 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.
+ * Adversary classification alone adds no protections; these depend on bus,
+ * DMA and driver support.
+ */
+enum device_trust_level {
+	DEVICE_TRUST_BLOCKED,
+	DEVICE_TRUST_ADVERSARY,
+	DEVICE_TRUST_UNRESTRICTED,
+};
+
+/**
+ * enum device_trust_policy - requested device trust policy
+ * @DEVICE_TRUST_POLICY_DEFAULT: Let the bus select the active trust level
+ * @DEVICE_TRUST_POLICY_BLOCKED: Request that the device remain blocked
+ * @DEVICE_TRUST_POLICY_ADVERSARY: Request adversary 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_DEFAULT,
+	DEVICE_TRUST_POLICY_BLOCKED,
+	DEVICE_TRUST_POLICY_ADVERSARY,
+	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] 32+ messages in thread

* [PATCH v3 20/31] driver core: Expose device trust authorization interfaces
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (18 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 19/31] driver core: Introduce a generic device trust level Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 21/31] driver core: Accept device trust policy on the kernel command line Leon Romanovsky
                   ` (10 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 0000000000000..154c5577c65e0
--- /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", "adversary", 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 "default". 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=default 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 "default", "blocked", "adversary", and
+		"unrestricted". "default" lets the bus select the operating level.
+		"blocked" prevents driver binding. "adversary" requests
+		classification as potentially malicious. "unrestricted" requests
+		normal operation. Adversary protections depend on bus, DMA,
+		and driver support; classification alone adds none.
diff --git a/MAINTAINERS b/MAINTAINERS
index b726344362b41..a145aaaca2ef3 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 56f1a9c73964b..1e7ee9856fe6f 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 c0006b3aa38f7..e72424618d622 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 35893786a163d..215ea6151ecfc 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_ADVERSARY] = "adversary",
+	[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_ADVERSARY &&
+		    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 702eb66b81bf3..2914b7191d342 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 32071f12ed257..b940dddf10e46 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] 32+ messages in thread

* [PATCH v3 21/31] driver core: Accept device trust policy on the kernel command line
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (19 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 20/31] driver core: Expose device trust authorization interfaces Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 22/31] driver core: Accept device trust policy by driver name Leon Romanovsky
                   ` (9 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 |  13 ++-
 drivers/base/base.h                             |   8 ++
 drivers/base/trust.c                            | 116 +++++++++++++++++++++++-
 include/linux/device/trust.h                    |   1 +
 5 files changed, 133 insertions(+), 7 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
index 154c5577c65e0..130e5b0d712c2 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 "default". 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 81f98655f5297..e77ce08ac8f79 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1262,9 +1262,18 @@ 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 default, blocked, adversary, 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: { default | blocked | adversary | 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 1e7ee9856fe6f..e8c24d02a5390 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 215ea6151ecfc..8211a869fc82e 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_DEFAULT;
+	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_DEFAULT)
+		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 default 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_DEFAULT)
-		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 b940dddf10e46..0258e07f938d1 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] 32+ messages in thread

* [PATCH v3 22/31] driver core: Accept device trust policy by driver name
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (20 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 21/31] driver core: Accept device trust policy on the kernel command line Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 23/31] driver core: Expose committed device trust level Leon Romanovsky
                   ` (8 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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.

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

diff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
index 130e5b0d712c2..504fe77be4ac1 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 "default". 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 non-default device_trust.default= kernel command-line value
+		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 e77ce08ac8f79..18708190e2a9b 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1268,12 +1268,15 @@ Kernel parameters
 			Format: <bus>:<device>=<policy>[,...]
 			Names match exactly as in /sys/bus/<bus>/devices/<device>.
 			Policies are default, blocked, adversary, 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: { default | blocked | adversary | unrestricted }
 			No trust options selects legacy unrestricted mode, preserving
 			bus policy and existing authorization. Otherwise blocked is the
@@ -1282,6 +1285,15 @@ 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 default, blocked, adversary, 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 e8c24d02a5390..b7768f5679c3b 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -129,6 +129,7 @@ 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
  * @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 +149,7 @@ 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 dead:1;
 };
 #define to_device_private_parent(obj)	\
@@ -200,6 +202,12 @@ 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);
 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 8211a869fc82e..03a56f85f0f33 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -43,6 +43,11 @@ 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 void device_trust_init_driver_policy(struct device *dev,
+					   const struct device_driver *drv);
+
 void device_trust_init_state(struct device *dev)
 {
 	if (dev->trust_committed_level != DEVICE_TRUST_BLOCKED)
@@ -59,8 +64,8 @@ static int __device_trust_prepare(struct device *dev,
 	enum device_trust_level level;
 	int ret;
 
+	device_trust_init_driver_policy(dev, drv);
 	policy = dev->trust_policy;
-
 	if (policy == DEVICE_TRUST_POLICY_BLOCKED)
 		return -EPERM;
 	if (dev->bus && dev->bus->trust_resolve) {
@@ -115,7 +120,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 +241,7 @@ int device_trust_set_policy_locked(struct device *dev,
 	}
 set_policy:
 	WRITE_ONCE(dev->trust_policy, policy);
+	dev->p->trust_policy_override = true;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(device_trust_set_policy_locked);
@@ -299,10 +305,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_DEFAULT;
 	const char *entry = options;
@@ -326,7 +333,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 +369,58 @@ 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;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_driver_policy);
+
+static void device_trust_init_driver_policy(struct device *dev,
+					   const struct device_driver *drv)
+{
+	device_trust_apply_driver_policy(dev, drv, device_trust_driver_cmdline);
+}
+
 static int __init device_trust_setup(char *str)
 {
 	device_trust_configure();
@@ -379,6 +435,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 +470,10 @@ VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,
 {
 	if (policy != DEVICE_TRUST_POLICY_DEFAULT)
 		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 +483,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] 32+ messages in thread

* [PATCH v3 23/31] driver core: Expose committed device trust level
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (21 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 22/31] driver core: Accept device trust policy by driver name Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 24/31] driver core: Cover trust authorization and binding with KUnit Leon Romanovsky
                   ` (7 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 03a56f85f0f33..bf4f2c8b31d09 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -155,6 +155,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 0258e07f938d1..22d3a4dc768e8 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] 32+ messages in thread

* [PATCH v3 24/31] driver core: Cover trust authorization and binding with KUnit
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (22 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 23/31] driver core: Expose committed device trust level Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 25/31] driver core: Document trust admission and initrd authorization Leon Romanovsky
                   ` (6 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 | 2461 +++++++++++++++++++++++++++++++++
 drivers/base/trust.c                  |    1 +
 3 files changed, 2463 insertions(+)

diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile
index 9ced7bbd569fc..dfff5e1d83f7c 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 0000000000000..c78eec9424f4f
--- /dev/null
+++ b/drivers/base/test/device-trust-test.c
@@ -0,0 +1,2461 @@
+// 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;
+};
+
+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;
+
+	kobj->ktype = original;
+	kfree(sysfs);
+	original->release(kobj);
+}
+
+static int device_trust_test_isolate_sysfs(struct device *dev)
+{
+	struct device_trust_test_sysfs *sysfs;
+
+	sysfs = kzalloc_obj(*sysfs);
+	if (!sysfs)
+		return -ENOMEM;
+	sysfs->original = dev->kobj.ktype;
+	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;
+	dev->kobj.ktype = &sysfs->type;
+	return 0;
+}
+
+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 *released;
+	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)
+{
+	struct device_trust_test_context *ctx =
+		to_device_trust_test_context(dev);
+	struct completion *released = ctx->released;
+
+	kfree(ctx);
+	complete(released);
+}
+
+/*
+ * The fixture is freed when the last reference to its device is dropped,
+ * which deferred probing, device links and delayed kobject release may do
+ * after the test. Wait for that so the release callback never outlives it.
+ */
+static void device_trust_test_device_unregister(void *data)
+{
+	struct device_trust_test_context *ctx = data;
+	struct completion *released = ctx->released;
+
+	if (ctx->device_registered)
+		device_unregister(&ctx->dev);
+	else
+		put_device(&ctx->dev);
+	if (!wait_for_completion_timeout(released, 10 * HZ)) {
+		WARN(1, "device-trust test fixture is still referenced\n");
+		wait_for_completion(released);
+	}
+}
+
+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 = kunit_kzalloc(test, sizeof(*released), GFP_KERNEL);
+	if (!released)
+		return ERR_PTR(-ENOMEM);
+	init_completion(released);
+
+	ctx = kzalloc_obj(*ctx);
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	ctx->released = released;
+	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);
+	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_DEFAULT, 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_DEFAULT;
+	     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_DEFAULT);
+	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_adversary_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_DEFAULT);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+
+	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_DEFAULT);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->resolve_driver, &ctx->drv);
+	KUNIT_EXPECT_EQ(test, ctx->configure_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_TRUE(test, ctx->probe_adversarial);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARY);
+	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_DEFAULT);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+	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_DEFAULT);
+
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+	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_ADVERSARY);
+	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_DEFAULT), -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_DEFAULT, DEVICE_TRUST_POLICY_ADVERSARY,
+				    DEVICE_TRUST_ADVERSARY);
+}
+
+static void device_trust_blocked_unrestricted_bind_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_BLOCKED,
+				    DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_UNRESTRICTED,
+				    DEVICE_TRUST_UNRESTRICTED);
+}
+
+static void device_trust_global_blocked_adversary_test(struct kunit *test)
+{
+	device_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARY,
+				    DEVICE_TRUST_POLICY_BLOCKED, DEVICE_TRUST_POLICY_ADVERSARY,
+				    DEVICE_TRUST_ADVERSARY);
+}
+
+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_DEFAULT);
+	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_ADVERSARY;
+	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_ADVERSARY), 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_ADVERSARY), 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_ADVERSARY);
+}
+
+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_DEFAULT);
+	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_DEFAULT), -EBUSY);
+
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_ADVERSARY), 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_ADVERSARY);
+}
+
+static void device_trust_generic_adversary_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_ADVERSARY);
+	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_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_TRUE(test, ctx->probe_adversarial);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARY);
+	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 platform_device *pdev;
+	struct platform_driver *drv;
+
+	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), 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_ADVERSARY), 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_ADVERSARY);
+
+	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_DEFAULT);
+	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_ADVERSARY), 0);
+	KUNIT_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+						      DEVICE_TRUST_POLICY_DEFAULT), 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_ADVERSARY), 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_ADVERSARY;
+	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_ADVERSARY);
+	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_ADVERSARY);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+	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_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_ADVERSARY);
+	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_ADVERSARY);
+	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_DEFAULT);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+	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_ADVERSARY);
+	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_ADVERSARY);
+
+	device_release_driver(&ctx->dev);
+	KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+	KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_ADVERSARY);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 2);
+	KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_ADVERSARY);
+	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_DEFAULT);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->resolve_level = DEVICE_TRUST_ADVERSARY;
+	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_ADVERSARY);
+	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_ADVERSARY);
+	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_ADVERSARY;
+	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_ADVERSARY);
+}
+
+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_DEFAULT);
+	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=adversary,usb:1-1=unrestricted,usb:1-2=blocked,platform:i8042=default";
+	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_ADVERSARY },
+		{ &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_DEFAULT },
+	};
+	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_DEFAULT);
+		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_DEFAULT);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(options); i++) {
+		enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;
+		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_ADVERSARY,
+				    "%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=default";
+	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_DEFAULT);
+	enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;
+	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_DEFAULT);
+}
+
+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_DEFAULT);
+	enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;
+	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_ADVERSARY);
+}
+
+static void device_trust_cmdline_validate_test(struct kunit *test)
+{
+	static const char valid[] = "pci:0000:01:00.0=adversary,usb:1-2=blocked";
+	static const char invalid[] = "pci:0000:01:00.0=adversary,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_DEFAULT);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(options); i++) {
+		enum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;
+		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_ADVERSARY,
+				    "case %u", i);
+	}
+}
+
+static void device_trust_boot_precedence_test(struct kunit *test)
+{
+	static const char * const overrides[] = {
+		[DEVICE_TRUST_POLICY_DEFAULT] = "pci:0000:01:00.0=default",
+		[DEVICE_TRUST_POLICY_BLOCKED] = "pci:0000:01:00.0=blocked",
+		[DEVICE_TRUST_POLICY_ADVERSARY] = "pci:0000:01:00.0=adversary",
+		[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_DEFAULT;
+	     initial <= DEVICE_TRUST_POLICY_UNRESTRICTED; initial++) {
+		for (global = DEVICE_TRUST_POLICY_DEFAULT;
+		     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=default", "pci:test=blocked",
+		"pci:test=adversary", "pci:test=unrestricted",
+	};
+	static const char * const device_rules[] = {
+		NULL, "pci:0000:01:00.0=default", "pci:0000:01:00.0=blocked",
+		"pci:0000:01:00.0=adversary", "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_DEFAULT;
+	     initial <= DEVICE_TRUST_POLICY_UNRESTRICTED; initial++) {
+		for (global = DEVICE_TRUST_POLICY_DEFAULT;
+		     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_DEFAULT);
+	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_DEFAULT);
+	}
+	device_lock(dev);
+	device_trust_apply_driver_policy(dev, &stub->drv,
+					 "pci:test=blocked,pci:test=adversary");
+	device_unlock(dev);
+	KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_ADVERSARY);
+}
+
+static void device_trust_driver_quoted_name_test(struct kunit *test)
+{
+	static const char * const args[] = {
+		"device_trust.driver=\"pci:test driver=adversary\"",
+		"\"device_trust.driver=pci:test driver=adversary\"",
+		"device_trust.driver=\"pci:other=unrestricted,pci:test driver=adversary\"",
+	};
+	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_DEFAULT);
+		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_ADVERSARY);
+	}
+}
+
+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=adversary";
+	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_DEFAULT);
+	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_ADVERSARY);
+	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_ADVERSARY);
+}
+
+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_DEFAULT;
+	     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_DEFAULT),
+			-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_DEFAULT);
+	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_ADVERSARY), 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_ADVERSARY);
+
+	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_ADVERSARY);
+	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_ADVERSARY), 0);
+}
+
+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_DEFAULT);
+	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_DEFAULT), 0);
+	KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_DEFAULT);
+
+	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_DEFAULT);
+	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[] = {
+		"default", "blocked", "adversary", "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_DEFAULT,
+					       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_ADVERSARY ? DEVICE_TRUST_ADVERSARY :
+		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_adversary_test(struct kunit *test)
+{
+	device_trust_early_claim_test(test, DEVICE_TRUST_POLICY_ADVERSARY, 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_ADVERSARY, true);
+}
+
+static struct kunit_case device_trust_test_cases[] = {
+	KUNIT_CASE(device_trust_default_bind_test),
+	KUNIT_CASE(device_trust_bus_adversary_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_adversary_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_adversary_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_claimed_policy_test),
+	KUNIT_CASE(device_trust_scan_continues_test),
+	KUNIT_CASE(device_trust_early_adversary_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_DEFAULT,
+						       DEVICE_TRUST_POLICY_DEFAULT, 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[] = {
+		"default", "blocked", "adversary", "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_DEFAULT;
+	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_DEFAULT, 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_DEFAULT &&
+	    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_DEFAULT, 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_ADVERSARY) {
+		target = DEVICE_TRUST_POLICY_UNRESTRICTED;
+		value = "unrestricted";
+	} else {
+		target = DEVICE_TRUST_POLICY_ADVERSARY;
+		value = "adversary";
+	}
+	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_DEFAULT);
+		ret = attr->store_const(&ctx->dev, attr, "default", 7);
+		KUNIT_EXPECT_EQ(test, ret, 7);
+		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_ADVERSARY);
+	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_ADVERSARY);
+	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_ADVERSARY);
+}
+
+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_DEFAULT);
+	for (initial = DEVICE_TRUST_POLICY_DEFAULT;
+	     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_DEFAULT ? 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 bf4f2c8b31d09..aa2a6a6283c60 100644
--- a/drivers/base/trust.c
+++ b/drivers/base/trust.c
@@ -56,6 +56,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)

-- 
2.56.1


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

* [PATCH v3 25/31] driver core: Document trust admission and initrd authorization
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (23 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 24/31] driver core: Cover trust authorization and binding with KUnit Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 26/31] PCI: Use device trust for platform restrictions Leon Romanovsky
                   ` (5 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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.

Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 .../driver-api/driver-model/device-trust.rst       | 323 +++++++++++++++++++++
 Documentation/driver-api/driver-model/index.rst    |   1 +
 2 files changed, 324 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 0000000000000..5c06f3a3340c2
--- /dev/null
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -0,0 +1,323 @@
+.. 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. Driver core tracks the requested policy,
+permission to bind, and active trust level separately.
+
+Policy and active trust
+=======================
+
+The writable ``trust_policy`` attribute accepts ``default``, ``blocked``,
+``adversary``, or ``unrestricted``. The bus may set the initial policy;
+otherwise it starts as ``default``. Boot options can override it for all
+devices, a named driver, or an individual device. See `Boot policy`_ for
+precedence.
+
+The read-only ``trust`` attribute reports the active trust level:
+
+``DEVICE_TRUST_BLOCKED`` (``blocked``)
+  There is no active driver setup or binding. The requested policy decides
+  whether a later bind is allowed.
+
+``DEVICE_TRUST_ADVERSARY`` (``adversary``)
+  The device is classified as potentially malicious. Protections depend
+  on bus, DMA, and driver support; classification alone adds none.
+
+``DEVICE_TRUST_UNRESTRICTED`` (``unrestricted``)
+  The device uses the normal privileges allowed by the bus and DMA policy.
+
+Policy resolution
+=================
+
+A bus can implement ``bus_type::trust_resolve`` to choose the active level
+from the device, candidate driver, and requested policy. For ``default``,
+it may use topology or firmware information. Other policies must resolve
+to the matching level or fail.
+
+The callback must not enable the device or grant DMA access. Those steps
+belong in setup callbacks, after resolution and before probe.
+
+Without this callback, ``default`` and ``unrestricted`` resolve to
+``DEVICE_TRUST_UNRESTRICTED``, and ``adversary`` to
+``DEVICE_TRUST_ADVERSARY``. Explicit ``blocked`` blocks binding.
+Enforcement is provided separately by bus, DMA, and driver code.
+
+Admission and policy writes
+===========================
+
+Authorization is userspace's choice of adversary or unrestricted for a blocked
+device. Commitment happens when the kernel accepts a policy and operating
+level, before starting driver setup. Both limit later policy changes.
+
+Without device trust boot options, legacy unrestricted mode preserves bus
+defaults and existing bus authorization. Buses can use
+``device_trust_enabled()`` to distinguish this mode from boot opt-in.
+Changes through ``trust_policy`` are disabled. An explicit
+``device_trust.default=default`` enables them while preserving bus
+defaults; device and driver boot rules also opt in.
+
+With ``device_trust.default=unrestricted``, runtime policy changes are
+unavailable for every device, including devices with a boot policy
+exception. Writing a different policy returns ``-EBUSY``. Writing the
+current policy succeeds without changing state or suppressing a pending
+driver boot rule. Boot device and driver rules retain their precedence.
+
+When enabled, runtime policy writes follow the internal states
+below; these are not additional sysfs values.
+``P`` is the requested policy and ``L`` is the committed operating level.
+
+.. list-table:: Admission states
+   :header-rows: 1
+   :widths: 25 40 35
+
+   * - State
+     - Meaning
+     - Policy writes
+   * - SELECTABLE
+     - The device has not been authorized or committed.
+     - Any policy is allowed. Selecting blocked enters WAITING.
+   * - WAITING
+     - Policy is blocked - no operating level is committed.
+     - Choose adversary or unrestricted to enter AUTHORIZED(P).
+   * - AUTHORIZED(P)
+     - Userspace has chosen adversary or unrestricted from WAITING.
+     - The policy cannot change, even before setup begins.
+   * - COMMITTED(P, L)
+     - The kernel has accepted P and L for setup.
+     - Neither the policy nor the operating level can change.
+
+Bus defaults and boot options set the initial state: blocked starts in
+WAITING; other policies start in SELECTABLE. Driver boot rules apply later,
+when a matching driver attempts to bind. They may still replace this
+initial state, including WAITING, unless a per-device boot entry or a
+successful policy write takes precedence.
+
+Userspace cannot move WAITING back to SELECTABLE by writing ``default``.
+Choosing adversary or unrestricted from WAITING locks that policy immediately.
+
+After boot initialization, the state transitions are::
+
+  SELECTABLE ----select blocked----> WAITING
+      |                                 |
+      |                             authorize
+      |                       adversary/unrestricted
+      |                                 |
+      |                                 v
+      |                            AUTHORIZED(P)
+      |                                 |
+      +------ permitted resolution -----+
+                       |
+                       v
+                 COMMITTED(P, L)
+
+Binding and device lifetime
+============================
+
+A manual bind that reaches WAITING returns ``-EPERM`` before driver or DMA
+setup. Automatic matching skips the rejected driver and tries the next
+candidate. Rejected attempts leave the device waiting for authorization
+through an enabled policy interface.
+
+Active trust stays at L through DMA setup, probe, operation, removal, and
+``bus_type::dma_cleanup``. It returns to blocked only after cleanup ends.
+
+The state lasts for the device object's lifetime, including the switch
+from initrd to the root filesystem. Unbinding and rebinding a registered
+device does not reset it. A removed device cannot be registered again; a
+new object starts with bus and boot policy.
+
+Bus code that operates a device before ``device_add()`` must call
+``device_trust_prepare(dev, drv)`` with the device lock held before driver
+setup. Registration preserves the admitted policy and commitment. After
+setup fails or the claimant stops using the device, call
+``device_trust_clear()`` under the same lock to clear active trust while
+retaining the commitment.
+
+Compatibility attachment
+========================
+
+This compatibility API **must not be used in any new code**. Its
+unrestricted-only restriction prevents devices from using the device trust
+authorization infrastructure. New code must use ``device_driver_attach()``.
+
+``device_driver_attach_unrestricted()`` supports manual attachment with the
+caller's existing subsystem serialization. It requires a policy that
+resolves to unrestricted and either no device trust boot options or an
+explicit ``device_trust.default=unrestricted``. Other configurations return
+``-EPERM`` before calling the driver. The legacy case preserves bus
+defaults. Buses implementing ``trust_resolve`` must use
+``device_driver_attach()``.
+
+Per-device boot exceptions still apply: blocked and adversary reject
+attachment. If a ``device_trust.driver=`` list is configured, the device
+must also have an explicit ``device_trust=`` entry; otherwise this API
+returns ``-EPERM``. That entry prevents a concurrent driver rule from
+changing the policy.
+
+The caller provides the subsystem's existing manual-binding serialization
+and excludes bus-specific policy changes. Binding and removal are
+serialized separately; an overlapping transaction returns ``-EBUSY``.
+Holding the device lock alone does not prevent attachment. The API takes
+neither the device nor parent lock, commits unrestricted before the probe
+callback, and publishes the binding after probe succeeds. It does not
+perform normal driver-core DMA, pinctrl, or power-domain setup. A failed
+probe or binding clears active trust but retains the unrestricted
+commitment.
+
+Boot policy
+===========
+
+Built-in drivers can probe before userspace starts. Use boot options to
+set their initial policy. All three options accept default, blocked,
+adversary, and unrestricted.
+
+Any option in the ``device_trust`` namespace selects blocked as the
+implicit default, including missing values and unrecognized options.
+The bare ``device_trust`` option enables this default without device entries.
+``device_trust.default=`` overrides this default regardless of option
+order; the last valid value wins. Explicit default preserves bus policy.
+Other values replace bus policy, including topology and removability
+defaults.
+
+``device_trust=`` sets policy for individual devices using a comma-separated
+list of ``<bus>:<device>=<policy>`` entries. Names must match
+``/sys/bus/<bus>/devices/<device>`` exactly. The first colon separates the
+bus name; later colons are part of the device name. Names cannot contain
+whitespace, control characters, commas, equals signs, or double quotes.
+Wildcards are not supported.
+
+A device entry overrides driver rules, the global setting, and bus policy,
+regardless of option order. Explicit default asks the bus or generic
+resolver to choose a level, even if the global setting is blocked. The
+last entry for a device wins. If the option is repeated, the last valid
+list replaces all earlier lists. A malformed list is ignored in full,
+with a warning.
+
+``device_trust.driver=`` sets policy by driver name using a comma-separated
+list of ``<bus>:<driver>=<policy>`` entries. Use exact names from
+``/sys/bus/<bus>/drivers/<driver>``, not module names or aliases. Driver
+names may contain spaces; quote the option when they do. Other name
+restrictions and list handling are the same as for device entries. The two
+lists are independent. Driver rules cover built-in drivers, modules, and
+hotplug.
+
+The first bind attempt with a matching driver rule applies it before
+resolution and setup. It overrides bus and global defaults, including
+global blocked. It cannot override a per-device entry or an existing
+authorization or commitment. A successful sysfs policy write also takes
+precedence, except for a no-op write with
+``device_trust.default=unrestricted``. The rule applies only once per
+device object, even if resolution or setup fails.
+
+After a blocked driver rule applies, rejected probes and driver changes
+leave the device in WAITING. Userspace can authorize it unless the global
+mode is unrestricted. A driver without a matching rule follows the device's
+current policy.
+
+An initrd can hold devices with ``device_trust.default=blocked`` and use
+boot exceptions for devices and suppliers needed to reach the initrd and
+perform attestation. These exceptions allow operation before fresh initrd
+attestation. Devices without an exception stay blocked even if their bus
+would normally choose adversary or unrestricted.
+
+Global and per-device rules apply before device registration. Buses that
+need policy earlier call ``device_trust_init()`` after setting the bus,
+name, and initial policy. Driver rules apply later, when a candidate driver
+is known.
+
+All boot rules remain available for newly discovered devices. Initialize
+boot policy before device registration. These options are not runtime
+controls or proof of hardware identity. Blocked blocks early binding and,
+outside global unrestricted mode, allows later authorization. Adversary and
+unrestricted permit early binding without fresh initrd attestation.
+
+DMA containment
+===============
+
+Blocked blocks driver binding, not DMA that hardware starts on its own.
+The bus and IOMMU must provide containment while a device waits for
+authorization.
+
+Adversary does not require an IOMMU. Without an IOMMU group, or with IOMMU
+support compiled out, binding uses the platform's normal DMA configuration.
+Adversary policy alone does not provide DMA isolation.
+
+Device trust admission does not change DMA handling, IOMMU domain selection,
+or ATS permission according to active trust. Those remain governed by existing
+bus and platform mechanisms. Integrating them with admission is separate
+work. A setup failure after commitment leaves the chosen mode fixed; it
+cannot fall back to unrestricted.
+
+Drivers that manage DMA themselves must provide their own containment.
+Drivers that do not use DMA need no domain.
+
+Initrd attestation sequence
+===========================
+
+Drivers with init-only probe callbacks cannot retry a binding held by
+blocked policy. They need a boot exception or a persistent probe callback.
+
+For a device that uses normal driver attachment:
+
+1. Boot with ``device_trust.default=blocked`` to hold all devices. To
+   hold selected devices, use ``device_trust.default=default`` with
+   per-device blocked rules. Add exceptions needed to reach the initrd
+   and provide DMA containment while waiting. Blocked attempts remain
+   in WAITING, including attempts by built-in drivers.
+
+2. In the initrd, control probing and attest the current device through a
+   transport that works without its functional driver. Keep policy blocked
+   during verification and preparation. Leave it blocked if verification
+   fails or is unavailable. Removal or replacement invalidates the result;
+   a reused bus address is not proof of identity. Repeat attestation if a
+   reset or preparation step invalidates the evidence.
+
+3. Once all checks and preparation are complete, write adversary or
+   unrestricted to ``trust_policy``. This locks the choice and allows
+   binding.
+
+4. Request binding through ``drivers_probe`` or another attach path. The
+   kernel commits the operating level before setup. Retries keep the same
+   policy and level. Retry if the interface reports ``-EAGAIN``. A scan
+   can skip rejected candidates without returning an error, and a
+   successful ``drivers_probe`` write need not wait for a concurrent probe.
+
+The ``driver`` link appears before probe completes. To observe successful
+binding, start monitoring ``bind`` uevents before authorization and
+correlate them with the current device lifetime, or use a successful
+synchronous per-driver bind operation. Link visibility alone is not a
+completion signal.
+
+Queued or concurrent probes may start as soon as step 3 completes, before
+step 4. Evidence must still be valid when authorizing the device.
+Boot exceptions that permit adversary or unrestricted allow early operation
+without fresh initrd attestation.
+
+The kernel interface provides neither an attestation transport nor a verifier.
+
+Driver use and independent properties
+=====================================
+
+Drivers read active trust with ``device_get_trust_level()`` or
+``device_is_adversarial()``. They may cache it while bound, but must not
+change requested policy or treat trust as proof of other security
+properties. Use the adversary check for paths that bypass the DMA API.
+General DMA containment belongs in bus and IOMMU code.
+
+Code that needs the level fixed for the device's lifetime can use
+``device_get_committed_trust_level()``. It returns blocked before
+commitment and the fixed level afterwards, including after failed setup
+or unbind. It does not permit operation while unbound.
+
+Trust does not describe transport protection, encryption, verified
+identity, memory privacy, or DMA addressability. Keep these properties
+independent of the requested policy and active trust level.
+
+Policy writes cannot revoke authorization or switch an operating device
+between adversary and unrestricted. Stopping it requires a separate
+procedure to quiesce the driver, tear down DMA, and contain the device on
+its bus.
diff --git a/Documentation/driver-api/driver-model/index.rst b/Documentation/driver-api/driver-model/index.rst
index abeb4b36636b6..af547cd13b2c4 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] 32+ messages in thread

* [PATCH v3 26/31] PCI: Use device trust for platform restrictions
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (24 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 25/31] driver core: Document trust admission and initrd authorization Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 27/31] iommu: Use device trust for DMA restrictions Leon Romanovsky
                   ` (4 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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          | 10 +++++-----
 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, 38 insertions(+), 22 deletions(-)

diff --git a/Documentation/driver-api/driver-model/device-trust.rst b/Documentation/driver-api/driver-model/device-trust.rst
index 5c06f3a3340c2..1a1847e943675 100644
--- a/Documentation/driver-api/driver-model/device-trust.rst
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -246,11 +246,11 @@ Adversary does not require an IOMMU. Without an IOMMU group, or with IOMMU
 support compiled out, binding uses the platform's normal DMA configuration.
 Adversary policy alone does not provide DMA isolation.
 
-Device trust admission does not change DMA handling, IOMMU domain selection,
-or ATS permission according to active trust. Those remain governed by existing
-bus and platform mechanisms. Integrating them with admission is separate
-work. A setup failure after commitment leaves the chosen mode fixed; it
-cannot fall back to unrestricted.
+The initial target is confidential computing guests without an IOMMU.
+On systems with an IOMMU, policy changes after discovery are not yet
+synchronized with domain selection or ATS configuration. That integration
+is follow-up work. A setup failure after commitment leaves the chosen mode
+fixed; it cannot fall back to unrestricted.
 
 Drivers that manage DMA themselves must provide their own containment.
 Drivers that do not use DMA need no domain.
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 9cb23780093dd..c8c96acc26343 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 7c6f2aba81443..a60701bdece4c 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 9a178e0e5f7f4..9aa69d19ceab4 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_ADVERSARY;
 		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_ADVERSARY;
 	}
 }
 
@@ -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 7f1f47ea2ee41..1afad28d1b5e0 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 2914b7191d342..3db6657dffe41 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_ADVERSARY;
+}
+
 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 aa3765a20a96c..d8edf8403a5ad 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_ADVERSARY;
+}
+
 #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] 32+ messages in thread

* [PATCH v3 27/31] iommu: Use device trust for DMA restrictions
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (25 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 26/31] PCI: Use device trust for platform restrictions Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 28/31] driver core: Allow driver attachment with the device lock held Leon Romanovsky
                   ` (3 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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>
---
 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 --------
 6 files changed, 16 insertions(+), 33 deletions(-)

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 960e1dc05ec69..e358bbb1f2baa 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 78742aeba932d..a48fb511bfe6f 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 5a2e705a2f2e0..0202b491f222e 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 b486b8bbd1fcd..891b0cd2dad02 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 9aa69d19ceab4..40b449b8e736c 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 d8edf8403a5ad..17612cb18385e 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] 32+ messages in thread

* [PATCH v3 28/31] driver core: Allow driver attachment with the device lock held
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (26 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 27/31] iommu: Use device trust for DMA restrictions Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:26 ` [PATCH v3 29/31] USB: Retain the result of configuration requests Leon Romanovsky
                   ` (2 subsequent siblings)
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 4c4723384dc66..2f45b53e5ba91 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 3db6657dffe41..05b8f4af36873 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] 32+ messages in thread

* [PATCH v3 29/31] USB: Retain the result of configuration requests
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (27 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 28/31] driver core: Allow driver attachment with the device lock held Leon Romanovsky
@ 2026-11-18 19:26 ` Leon Romanovsky
  2026-11-18 19:27 ` [PATCH v3 30/31] USB: Authorize interfaces through device trust Leon Romanovsky
  2026-11-18 19:27 ` [PATCH v3 31/31] USB: Authorize devices " Leon Romanovsky
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:26 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
  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 b8e2d864faa46..0a022ce8739c2 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;
@@ -2349,7 +2360,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 a5e706f67f33a..d372712eba34c 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] 32+ messages in thread

* [PATCH v3 30/31] USB: Authorize interfaces through device trust
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (28 preceding siblings ...)
  2026-11-18 19:26 ` [PATCH v3 29/31] USB: Retain the result of configuration requests Leon Romanovsky
@ 2026-11-18 19:27 ` Leon Romanovsky
  2026-11-18 19:27 ` [PATCH v3 31/31] USB: Authorize devices " Leon Romanovsky
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:27 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
  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 5213117fd957e..62e326710ef5f 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 150a14970e951..31ff34df29717 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 "adversary" 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 b10b80b5fd6d4..893bcdb430878 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 0a022ce8739c2..d388d7371a247 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 d22dc78457d79..b8974e9db3a22 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 1b2de58fd6cef..233dacbc446b4 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 d372712eba34c..9399c816b367d 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] 32+ messages in thread

* [PATCH v3 31/31] USB: Authorize devices through device trust
  2026-11-18 19:26 [PATCH v3 00/31] driver core: Authorize device trust before driver binding Leon Romanovsky
                   ` (29 preceding siblings ...)
  2026-11-18 19:27 ` [PATCH v3 30/31] USB: Authorize interfaces through device trust Leon Romanovsky
@ 2026-11-18 19:27 ` Leon Romanovsky
  30 siblings, 0 replies; 32+ messages in thread
From: Leon Romanovsky @ 2026-11-18 19:27 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
  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                  |  5 +++++
 include/linux/usb.h                     | 12 +++++-----
 10 files changed, 97 insertions(+), 21 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb
index 62e326710ef5f..dbe70c481e7c8 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 31ff34df29717..b886b2da85605 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=default``, and write "adversary" 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 893bcdb430878..1bd2b3e120708 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)) {
@@ -1351,8 +1351,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;
 	}
@@ -1367,7 +1366,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 a48994e11ef3d..bcfc01eefd09c 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 199d3f535a891..62eedae9d0077 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -2770,7 +2770,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;
@@ -2784,17 +2785,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;
@@ -2813,10 +2838,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 d388d7371a247..b201356309e17 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 b8974e9db3a22..189fb917b81ee 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 903e57dc273af..42f9cfcd8b0f2 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 2559a180fdeec..ed71f1155e2ed 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -748,6 +748,11 @@ 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;
+	}
 	trace_usb_alloc_dev(dev);
 	return dev;
 }
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 9399c816b367d..34ee4d6f91c19 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] 32+ messages in thread

end of thread, other threads:[~2026-11-18 19:29 UTC | newest]

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

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