| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/08 04:35 | flow | patch-triage |
3mArgs:null Results: {
"EnableConfigs": [
"SYSFS",
"PCI",
"USB",
"BT_HCIBTUSB",
"BT_HCIBTUSB_MTK",
"MAC80211_HWSIM"
],
"FocusSymbols": [
"trust_policy_store",
"trust_policy_show",
"trust_show",
"device_trust_set_policy",
"device_trust_prepare",
"device_trust_clear",
"device_trust_may_claim",
"device_driver_try_attach",
"drivers_probe_store",
"usb_driver_release_refused_claim",
"btusb_mtk_claim_iso_intf",
"mac80211_hwsim_new_radio"
],
"KMSANReasoning": "The patch series implements a device trust framework within the Linux driver core, allowing system administrators and subsystems to control driver binding and DMA operation based on requested policies (default, disabled, adversary, full). It introduces sysfs attributes (trust, trust_policy), kernel parameters (device_trust, device_trust.default, device_trust.driver), state machine transitions in the driver core, and integration across various subsystems (PCI, USB, IOMMU, input, networking).\n\nEvaluation against KMSAN applicability criteria:\n1. Kernel structures sent to user space: The patch only introduces text-based sysfs attributes ('trust' and 'trust_policy') formatted with sysfs_emit(). It does not introduce or modify any binary structures, ioctl outputs, netlink attributes, socket options, or BPF buffers copied to userspace that could leak uninitialized padding or fields.\n2. Uninitialized variables in branching: All local variables in the parsing and helper functions (e.g. level, policy, matched, err) are explicitly initialized before use or branch evaluation.\n3. Complex structure allocation and initialization: New fields added to 'struct device' and 'struct device_private' (trust_policy, trust_level, trust_committed_level, trust_state, etc.) reside in structures allocated via kzalloc (e.g., device_private_init uses kzalloc_obj) or initialized during device setup (device_trust_init, device_trust_init_state). Zero represents the default/disabled state across all relevant enums.\n4. Buffer bounds and lengths: The command-line parsing routines safely parse bounded string slices with memchr, strcspn, and strncmp without creating uninitialized buffer regions.\n\nPrimary risks in these changes relate to lock ordering and concurrency (device_driver_attach vs device_driver_try_attach, device_lock vs parent lock), probe error handling, state machine correctness, and driver/device reference counting during bind/unbind. These classes of bugs (deadlocks, NULL pointer dereferences, use-after-free) are effectively covered by standard KASAN, LOCKDEP, and general kernel debuggers.\n\nBecause no uninitialized memory reads or information leak hazards are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "This patch introduces the Device Trust subsystem to the Linux kernel driver core along with new sysfs attributes (/sys/devices/.../trust, /sys/devices/.../trust_policy) and changes to /sys/bus/.../drivers_probe. It modifies the core driver binding and probe flow (really_probe, device_bind_driver, device_driver_attach, and adds device_driver_try_attach) to enforce new invariants, state machine transitions, and device locking assertions. Furthermore, it modifies driver binding and interface claiming logic across USB, mac80211_hwsim, Bluetooth (btusb), network PHY, gameport, and serio subsystems. These changes touch reachable core code and user-accessible interfaces, making them prime candidates for fuzzing to uncover potential locking regressions, state machine violations, and race conditions.",
"WorthFuzzing": true
} |
| 1/1 | 2026/11/08 04:35 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 8c56033573e04f5245d4c1c50d24fa102cb32e43\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Nov 8 04:35:17 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/ABI/testing/sysfs-bus-drivers-probe b/Documentation/ABI/testing/sysfs-bus-drivers-probe\nnew file mode 100644\nindex 00000000000000..851c65b9331ae2\n--- /dev/null\n+++ b/Documentation/ABI/testing/sysfs-bus-drivers-probe\n@@ -0,0 +1,13 @@\n+What:\t\t/sys/bus/.../drivers_probe\n+Date:\t\tFebruary 2007\n+Contact:\tdriver-core@lists.linux.dev\n+Description:\n+\t\t(WO) Writing a device name requests driver attachment on this\n+\t\tbus, even when drivers_autoprobe is disabled. A successful\n+\t\twrite does not guarantee that a driver bound. The device's\n+\t\tdriver link reports the result.\n+\n+\t\tEAGAIN means attachment was deferred before the driver's probe\n+\t\tcallback, for example while a supplier or the device itself\n+\t\tis not ready. ENODEV means the device was not found. Other\n+\t\tattachment errors return EINVAL.\ndiff --git a/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust\nnew file mode 100644\nindex 00000000000000..6f52102a1dfbf9\n--- /dev/null\n+++ b/Documentation/ABI/testing/sysfs-devices-trust\n@@ -0,0 +1,61 @@\n+What:\t\t/sys/devices/.../trust\n+Date:\t\tSeptember 2026\n+Contact:\tdriver-core@lists.linux.dev\n+Description:\n+\t\t(RO) The active trust level for the device. The values are\n+\t\t\"disabled\", \"adversary\", and \"full\". Driver core resolves\n+\t\tthe level before DMA configuration and probe, and retains it\n+\t\tthrough removal and DMA cleanup. Outside a bind transaction\n+\t\tthe level is \"disabled\". This does not imply that policy is\n+\t\twritable or that the device has never operated.\n+\n+What:\t\t/sys/devices/.../trust_policy\n+Date:\t\tSeptember 2026\n+Contact:\tdriver-core@lists.linux.dev\n+Description:\n+\t\t(RW) The requested trust policy for the device. The initial\n+\t\tvalue may be selected by the bus before the device is\n+\t\tregistered; otherwise it is \"default\". The policy is resolved\n+\t\tto an active trust level before the device is probed.\n+\t\tA non-default device_trust.default= kernel command-line value\n+\t\treplaces the bus policy before registration. A matching\n+\t\tdevice_trust= entry takes precedence over both, regardless\n+\t\tof command-line order. Boot policy also applies to new devices\n+\t\tdiscovered after boot.\n+\t\tOtherwise, device_trust.driver= can initialize policy on the\n+\t\tfirst binding attempt with a matching driver rule. A successful\n+\t\tpolicy write, including the existing value, suppresses this\n+\t\tdeferred initialization. Driver rules do not change an existing\n+\t\tauthorization or commitment and apply at most once per device.\n+\n+\t\tThe values are \"default\", \"disabled\", \"adversary\", and\n+\t\t\"full\". \"default\" lets the bus select the active trust level.\n+\t\t\"disabled\" prevents a functional driver from binding.\n+\t\t\"adversary\" requests operation with the restrictions that the\n+\t\tbus and DMA layers can enforce for a potentially malicious\n+\t\tdevice; it does not isolate DMA by itself. \"full\" requests\n+\t\tnormal operation.\n+\n+\t\tBefore an operating level is committed, policy may change\n+\t\tfreely until \"disabled\" is selected. Explicit \"disabled\"\n+\t\tblocks binding while allowing a later write of \"adversary\" or\n+\t\t\"full\". Repeated rejected probes preserve this opportunity.\n+\t\tAuthorization immediately fixes the requested policy.\n+\t\tReturning to \"default\" or \"disabled\", or selecting another\n+\t\tmode, fails.\n+\n+\t\tSuccessful resolution to an allowed level commits policy\n+\t\tand operating level before driver or DMA setup. Both remain\n+\t\tfixed for the device object's lifetime, including through\n+\t\tfailed or deferred probes, unbind, reset, and driver reload.\n+\t\tLater binds must resolve to the same level, including when\n+\t\tthe policy is \"default\". A resolver error or disabled result\n+\t\tbefore commitment leaves the policy's writability unchanged.\n+\n+\t\tFor a live device, writing the existing value succeeds\n+\t\twithout changing state. Other forbidden writes fail with\n+\t\tEBUSY; invalid strings fail with EINVAL. Removed devices\n+\t\treject writes with ENODEV. Writes do not trigger probing or\n+\t\tattestation.\n+\t\tQueued probes may proceed immediately after authorization,\n+\t\tso userspace must complete verification before granting it.\ndiff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt\nindex 0b3c140babd944..458f59a8af854c 100644\n--- a/Documentation/admin-guide/kernel-parameters.txt\n+++ b/Documentation/admin-guide/kernel-parameters.txt\n@@ -1259,6 +1259,70 @@ Kernel parameters\n \tdell_smm_hwmon.fan_max=\n \t\t\t[HW] Maximum configurable fan speed.\n \n+\tdevice_trust=\t[KNL]\n+\t\t\tSet requested device trust policy before driver probing,\n+\t\t\tincluding for drivers built into the kernel.\n+\t\t\tFormat: \u003cbus\u003e:\u003cdevice\u003e=\u003cpolicy\u003e[,...]\n+\t\t\tThe bus and device names must match exactly, as in\n+\t\t\t/sys/bus/\u003cbus\u003e/devices/\u003cdevice\u003e. The first colon separates\n+\t\t\tthe bus name; further colons belong to the device name.\n+\t\t\tPolicies are default, disabled, adversary, and full.\n+\t\t\tEntries override device_trust.default, device_trust.driver,\n+\t\t\tand bus-initialized policy, regardless of option order.\n+\t\t\tThe last entry for a device wins. If specified more than\n+\t\t\tonce, the last valid list replaces all earlier lists.\n+\t\t\tMalformed lists are warned about and ignored in full.\n+\t\t\tNames containing whitespace, control characters, commas,\n+\t\t\tequals signs, or double quotes cannot be specified.\n+\t\t\tRules also apply to devices added after boot.\n+\t\t\tExplicit disabled blocks early binding but permits later\n+\t\t\tauthorization by writing adversary or full to trust_policy.\n+\t\t\tRejected disabled probes leave this opportunity intact.\n+\t\t\tAuthorization fixes the policy before probing; adversary\n+\t\t\tor full at boot permits early probes without attestation.\n+\t\t\tExample:\n+\t\t\tdevice_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled\n+\t\t\tSee Documentation/driver-api/driver-model/device-trust.rst.\n+\n+\tdevice_trust.default=\n+\t\t\t[KNL] Set initial trust policy for devices without a\n+\t\t\tmatching device_trust= entry, including hotplugged devices.\n+\t\t\tA matching device_trust.driver= rule can replace this\n+\t\t\tinitial policy at the first binding attempt.\n+\t\t\tFormat: { default | disabled | adversary | full }\n+\t\t\tdefault preserves bus-initialized policy (the default).\n+\t\t\tOther values override bus-initialized policy before\n+\t\t\tprobing, including for built-in drivers.\n+\t\t\tThe last valid value wins; invalid values are warned\n+\t\t\tabout and ignored.\n+\t\t\tWithout a matching driver rule, disabled prevents binding\n+\t\t\tuntil userspace authorizes each device by writing adversary\n+\t\t\tor full to trust_policy. Boot-critical devices need explicit\n+\t\t\tdevice or driver rules to operate before userspace. DMA\n+\t\t\tcontainment remains a bus and IOMMU responsibility.\n+\t\t\tSee Documentation/driver-api/driver-model/device-trust.rst.\n+\n+\tdevice_trust.driver=\n+\t\t\t[KNL] Set initial device trust policy by driver name,\n+\t\t\tincluding for drivers built into the kernel.\n+\t\t\tFormat: \u003cbus\u003e:\u003cdriver\u003e=\u003cpolicy\u003e[,...]\n+\t\t\tNames match exactly as in /sys/bus/\u003cbus\u003e/drivers/\u003cdriver\u003e.\n+\t\t\tPolicies are default, disabled, adversary, and full.\n+\t\t\tThe name restrictions and repeated-list rules are the same\n+\t\t\tas device_trust=. Module aliases and wildcards are not\n+\t\t\texpanded. Rules remain available for hotplug and modules.\n+\t\t\tThe first binding attempt with a matching rule applies it\n+\t\t\tonce, before trust resolution and driver or DMA setup.\n+\t\t\tA per-device device_trust= entry or any successful write\n+\t\t\tto trust_policy, even of its current value, takes precedence.\n+\t\t\tRules never replace an existing authorization or commitment.\n+\t\t\tA rejected disabled attempt leaves the device disabled until\n+\t\t\tuserspace authorizes it, including across driver changes.\n+\t\t\tDriver rules apply after bus discovery and do not reconfigure\n+\t\t\tIOMMU domains. Adversary operation needs suitable existing\n+\t\t\tDMA configuration; see the device trust documentation.\n+\t\t\tExample: device_trust.driver=pci:e1000=disabled,pci:nvme=full\n+\n \tdfltcc=\t\t[HW,S390]\n \t\t\tFormat: { on | off | def_only | inf_only | always }\n \t\t\ton: s390 zlib hardware support for compression on\ndiff --git a/Documentation/driver-api/driver-model/device-trust.rst b/Documentation/driver-api/driver-model/device-trust.rst\nnew file mode 100644\nindex 00000000000000..1013784daeaca1\n--- /dev/null\n+++ b/Documentation/driver-api/driver-model/device-trust.rst\n@@ -0,0 +1,294 @@\n+.. SPDX-License-Identifier: GPL-2.0\n+\n+============\n+Device trust\n+============\n+\n+Device trust controls how the kernel uses a device. It does not verify the\n+device's identity or firmware. Driver core tracks the requested policy,\n+permission to bind, and active trust level separately.\n+\n+Policy and active trust\n+=======================\n+\n+The writable ``trust_policy`` attribute accepts ``default``, ``disabled``,\n+``adversary``, or ``full``. The bus may set the initial policy; otherwise it\n+starts as ``default``. Boot options can override it for all devices, a\n+named driver, or an individual device. See `Boot policy`_ for precedence.\n+\n+The read-only ``trust`` attribute reports the active trust level:\n+\n+``DEVICE_TRUST_DISABLED`` (``disabled``)\n+ There is no active driver setup or binding. The requested policy decides\n+ whether a later bind is allowed.\n+\n+``DEVICE_TRUST_ADVERSARY`` (``adversary``)\n+ The kernel treats the device as potentially malicious. The bus and DMA\n+ layers must enforce the restrictions needed to operate it.\n+\n+``DEVICE_TRUST_FULL`` (``full``)\n+ The device uses the normal privileges allowed by the bus and DMA policy.\n+\n+Policy resolution\n+=================\n+\n+A bus can implement ``bus_type::trust_resolve`` to choose the active level\n+from the device, candidate driver, and requested policy. For ``default``,\n+it may use topology or firmware information. Other policies must resolve\n+to the matching level or fail.\n+\n+The callback must not enable the device or grant DMA access. Those steps\n+belong in setup callbacks, after resolution and before probe.\n+\n+Without this callback, ``default`` and ``full`` resolve to\n+``DEVICE_TRUST_FULL``, and ``adversary`` to ``DEVICE_TRUST_ADVERSARY``.\n+Explicit ``disabled`` blocks binding. The bus remains responsible for\n+enforcing the chosen level's restrictions.\n+\n+Admission and policy writes\n+===========================\n+\n+Authorization is userspace's choice of adversary or full for a disabled\n+device. Commitment happens when the kernel accepts a policy and operating\n+level, before starting driver setup. Both limit later policy changes.\n+\n+The states below are internal; they are not additional sysfs values.\n+``P`` is the requested policy and ``L`` is the committed operating level.\n+\n+.. list-table:: Admission states\n+ :header-rows: 1\n+ :widths: 25 40 35\n+\n+ * - State\n+ - Meaning\n+ - Policy writes\n+ * - SELECTABLE\n+ - The device has not been authorized or committed.\n+ - Any policy is allowed. Selecting disabled enters WAITING.\n+ * - WAITING\n+ - Policy is disabled - no operating level is committed.\n+ - Choose adversary or full to enter AUTHORIZED(P).\n+ * - AUTHORIZED(P)\n+ - Userspace has chosen adversary or full from WAITING.\n+ - The policy cannot change, even before setup begins.\n+ * - COMMITTED(P, L)\n+ - The kernel has accepted P and L for setup.\n+ - Neither the policy nor the operating level can change.\n+\n+Bus defaults and boot options set the initial state: disabled starts in\n+WAITING; other policies start in SELECTABLE. Driver boot rules apply later,\n+when a matching driver attempts to bind. They may still replace this\n+initial state, including WAITING, unless a per-device boot entry or a\n+successful policy write takes precedence.\n+\n+Userspace cannot move WAITING back to SELECTABLE by writing ``default``.\n+Choosing adversary or full from WAITING locks that policy immediately.\n+\n+After boot initialization, the state transitions are::\n+\n+ SELECTABLE ----select disabled----\u003e WAITING\n+ | |\n+ | authorize\n+ | adversary/full\n+ | |\n+ | v\n+ | AUTHORIZED(P)\n+ | |\n+ +------ permitted resolution -----+\n+ |\n+ v\n+ COMMITTED(P, L)\n+\n+Binding and device lifetime\n+============================\n+\n+A manual bind that reaches WAITING returns ``-EPERM`` before driver or DMA\n+setup. Automatic matching skips the rejected driver and tries the next\n+candidate. Repeated rejected attempts leave the policy writable for later\n+authorization.\n+\n+Active trust stays at L through DMA setup, probe, operation, removal, and\n+``bus_type::dma_cleanup``. It returns to disabled only after cleanup ends.\n+\n+The state lasts for the device object's lifetime, including the switch\n+from initrd to the root filesystem. Unbinding and rebinding a registered\n+device does not reset it. A removed device cannot be registered again; a\n+new object starts with bus and boot policy.\n+\n+A driver can claim a USB interface before it is registered. The claim fails\n+if the interface's own policy is disabled. A driver rule and other claims\n+are admitted when the interface is registered, and a refusal then releases\n+the claim. The policy of a claimed interface cannot be changed, and\n+registration does not admit it while ``drivers_autoprobe`` is disabled.\n+\n+Boot policy\n+===========\n+\n+Built-in drivers can probe before userspace starts. Use boot options to\n+set their initial policy. All three options accept default, disabled,\n+adversary, and full.\n+\n+``device_trust.default=`` sets policy for all devices. If omitted or set\n+to default, it preserves bus policy. Other values replace bus policy,\n+including topology and removability defaults. The last valid value wins;\n+invalid values are reported and ignored.\n+\n+``device_trust=`` sets policy for individual devices::\n+\n+ device_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled\n+\n+Each entry is ``\u003cbus\u003e:\u003cdevice\u003e=\u003cpolicy\u003e``. Names must match\n+``/sys/bus/\u003cbus\u003e/devices/\u003cdevice\u003e`` exactly. The first colon separates the\n+bus name; later colons are part of the device name. Names cannot contain\n+whitespace, control characters, commas, equals signs, or double quotes.\n+Wildcards are not supported.\n+\n+A device entry overrides driver rules, the global setting, and bus policy,\n+regardless of option order. Explicit default asks the bus or generic\n+resolver to choose a level, even if the global setting is disabled. The\n+last entry for a device wins. If the option is repeated, the last valid\n+list replaces all earlier lists. A malformed list is ignored in full,\n+with a warning.\n+\n+``device_trust.driver=`` sets policy by driver name::\n+\n+ device_trust.driver=pci:e1000=disabled,pci:nvme=full\n+\n+Each entry is ``\u003cbus\u003e:\u003cdriver\u003e=\u003cpolicy\u003e``. Use exact names from\n+``/sys/bus/\u003cbus\u003e/drivers/\u003cdriver\u003e``, not module names or aliases. The name\n+restrictions and list handling are the same as for device entries. The\n+two lists are independent. Driver rules cover built-in drivers, modules,\n+and hotplug.\n+\n+The first bind attempt with a matching driver rule applies it before\n+resolution and setup. It overrides bus and global defaults, including\n+global disabled. It cannot override a per-device entry, any successful\n+sysfs policy write, or an existing authorization or commitment. It applies\n+only once per device object, even if resolution or setup fails.\n+\n+After a disabled driver rule applies, rejected probes and driver changes\n+leave the device in WAITING until userspace authorizes it. Before any rule\n+applies, a driver with no matching rule may still bind. Use global or\n+per-device disabled to block every driver.\n+\n+For example, an initrd can hold all devices except one needed at boot::\n+\n+ device_trust.default=disabled device_trust=pci:0000:00:01.0=full\n+\n+Include exceptions for every device and supplier needed to reach the\n+initrd and perform attestation. These exceptions allow operation before\n+fresh initrd attestation. Unlisted devices stay disabled even if their\n+bus would normally choose adversary or full.\n+\n+Global and per-device rules apply before device registration. Buses that\n+need policy earlier call ``device_trust_init()`` after setting the bus,\n+name, and initial policy. PCI does this during discovery, before header\n+fixups, ACS setup, and IOMMU configuration. PCI applies topology and\n+removability defaults only to default policy. A child's explicit boot\n+entry overrides inherited policy. Driver rules wait until a candidate\n+driver is known and cannot change these earlier discovery choices.\n+\n+All boot rules remain available for newly discovered devices. Reapplying\n+initialization to an already registered device does not reset its state.\n+These options are not runtime controls or proof of hardware identity.\n+Disabled blocks early binding and allows later authorization. Adversary\n+and full permit early binding without fresh initrd attestation.\n+\n+DMA containment\n+===============\n+\n+Disabled blocks driver binding, not DMA that hardware starts on its own.\n+The bus and IOMMU must provide containment while a device waits for\n+authorization.\n+\n+For PCI devices whose policy is disabled or adversary at discovery, the\n+IOMMU core selects a strict translated default domain for the whole group.\n+AMD domain selection and Intel bypass checks apply the same restriction.\n+This prepares a disabled device for later adversary authorization without\n+granting permission to bind. Later full authorization may keep these\n+restrictions. PCI does not record the ATS capability of such a device, even\n+after a later full authorization, and sets ACS Translation Blocking for it\n+where the path supports it.\n+\n+Adversary does not require an IOMMU. Without an IOMMU group, or with IOMMU\n+support compiled out, binding uses the platform's normal DMA configuration.\n+Adversary policy alone does not provide DMA isolation.\n+\n+Binding does not check the IOMMU domain. Neither a driver rule nor a sysfs\n+policy write reconfigures it, so a policy selected after discovery may\n+bind with an identity or deferred-invalidation domain. Select the policy\n+with ``device_trust=`` or ``device_trust.default=`` for it to apply at\n+discovery. Outside PCI, discovery does not select a domain. Changing a\n+group's domain type through sysfs is not checked against trust. A setup\n+failure after commitment leaves the chosen mode fixed; it cannot fall back\n+to full.\n+\n+Adversarial IOMMU mappings that are not aligned to IOMMU pages need an\n+active SWIOTLB bounce-buffer pool. Low-memory x86 systems may need\n+``swiotlb=force`` to keep this pool available. Without it, mappings can fail\n+even after the driver binds.\n+\n+Drivers marked ``driver_managed_dma`` bypass the kernel DMA API's default\n+domain handling and must provide their own DMA containment. Drivers that\n+do not use DMA need no domain.\n+\n+Initrd attestation sequence\n+===========================\n+\n+For a device that uses normal driver attachment, such as a PCI function:\n+\n+1. Boot with ``device_trust.default=disabled`` to hold all devices, or\n+ ``device_trust=pci:0000:01:00.0=disabled`` to hold one. Add exceptions\n+ needed to reach the initrd and provide DMA containment while waiting.\n+ ``device_trust.driver=pci:e1000=disabled`` can instead hold a known\n+ driver; global disabled also covers other drivers. Disabled attempts\n+ remain in WAITING, including attempts by built-in drivers.\n+2. In the initrd, control probing and attest the current device through a\n+ transport that works without its functional driver. Keep policy disabled\n+ during verification and preparation. Leave it disabled if verification\n+ fails or is unavailable. Removal or replacement invalidates the result;\n+ a reused bus address is not proof of identity. Repeat attestation if a\n+ reset or preparation step invalidates the evidence.\n+3. Once all checks and preparation are complete, write adversary or full\n+ to ``trust_policy``. This locks the choice and allows binding.\n+4. Request binding through ``drivers_probe`` or another attach path. The\n+ kernel commits the operating level before setup. Retries keep the same\n+ policy and level. An attachment deferred before the driver's probe\n+ callback returns ``-EAGAIN``; retry later. A successful write does not\n+ guarantee that a driver bound, so check the device's ``driver`` link.\n+\n+Queued or concurrent probes may start as soon as step 3 completes, before\n+step 4. Evidence must still be valid when authorizing the device.\n+Boot exceptions that permit adversary or full allow early operation\n+without fresh initrd attestation.\n+\n+The kernel interface provides neither an attestation transport nor a verifier.\n+\n+Driver use and independent properties\n+=====================================\n+\n+Drivers read active trust with ``device_get_trust_level()`` or\n+``device_is_adversarial()``. They may cache it while bound, but must not\n+change requested policy or treat trust as proof of other security\n+properties. Use the adversary check for features such as peer-to-peer DMA,\n+device memory, PASID/SVA, or paths that bypass the DMA API. General DMA\n+containment belongs in bus and IOMMU code.\n+\n+Discovery code uses ``device_policy_is_adversarial()`` to check requested\n+policy before binding or after unbind. This helper does not resolve\n+default policy or report active trust.\n+\n+Code that needs the level fixed for the device's lifetime can use\n+``device_get_committed_trust_level()``. It returns disabled before\n+commitment and the fixed level afterwards, including after failed setup\n+or unbind. It does not permit operation while unbound.\n+\n+Trust does not describe transport protection, TDISP T=1/RUN, link\n+encryption, private MMIO, verified evidence, or access to private versus\n+shared memory. T=1 can use either adversary or full. In particular,\n+``force_dma_unencrypted()`` describes DMA addressability and must not be\n+derived from trust.\n+\n+Policy writes cannot revoke authorization or switch an operating device\n+between adversary and full. Stopping it requires a separate procedure to\n+quiesce the driver, tear down DMA, and contain the device on its bus.\ndiff --git a/Documentation/driver-api/driver-model/index.rst b/Documentation/driver-api/driver-model/index.rst\nindex abeb4b36636b6c..af547cd13b2c41 100644\n--- a/Documentation/driver-api/driver-model/index.rst\n+++ b/Documentation/driver-api/driver-model/index.rst\n@@ -9,6 +9,7 @@ Driver Model\n bus\n design-patterns\n device\n+ device-trust\n devres\n driver\n overview\ndiff --git a/MAINTAINERS b/MAINTAINERS\nindex 93abbb315065de..a145aaaca2ef3f 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -8149,6 +8149,8 @@ M:\tJohan Hovold \u003cjohan@kernel.org\u003e\n L:\tdriver-core@lists.linux.dev\n S:\tSupported\n T:\tgit git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git\n+F:\tDocumentation/ABI/testing/sysfs-bus-drivers-probe\n+F:\tDocumentation/ABI/testing/sysfs-devices-trust\n F:\tDocumentation/core-api/kobject.rst\n F:\tDocumentation/driver-api/driver-model/\n F:\tdrivers/base/\ndiff --git a/drivers/base/Makefile b/drivers/base/Makefile\nindex 435710f643a5c6..cb3bd5fa2287a0 100644\n--- a/drivers/base/Makefile\n+++ b/drivers/base/Makefile\n@@ -3,7 +3,7 @@\n \n obj-y\t\t\t:= component.o core.o bus.o dd.o syscore.o \\\n \t\t\t driver.o class.o platform.o \\\n-\t\t\t cpu.o firmware.o init.o map.o devres.o \\\n+\t\t\t cpu.o firmware.o init.o map.o devres.o trust.o \\\n \t\t\t attribute_container.o transport_class.o \\\n \t\t\t topology.o container.o property.o cacheinfo.o \\\n \t\t\t swnode.o faux.o\ndiff --git a/drivers/base/base.h b/drivers/base/base.h\nindex 866b2beb6baa28..5ff3217eb916ef 100644\n--- a/drivers/base/base.h\n+++ b/drivers/base/base.h\n@@ -10,6 +10,7 @@\n * shared outside of the drivers/base/ directory.\n *\n */\n+#include \u003clinux/device/trust.h\u003e\n #include \u003clinux/notifier.h\u003e\n \n /**\n@@ -88,6 +89,13 @@ struct driver_private {\n };\n #define to_driver(obj) container_of(obj, struct driver_private, kobj)\n \n+enum device_trust_state {\n+\tDEVICE_TRUST_SELECTABLE,\n+\tDEVICE_TRUST_WAITING,\n+\tDEVICE_TRUST_AUTHORIZED,\n+\tDEVICE_TRUST_COMMITTED,\n+};\n+\n /**\n * struct device_private - structure to hold the private to the driver core\n *\t\t\t portions of the device structure.\n@@ -105,6 +113,9 @@ struct driver_private {\n *\t\t\t dev_err_probe() for later retrieval via debugfs\n * @device: pointer back to the struct device that this structure is\n *\t associated with.\n+ * @trust_state: policy admission state\n+ * @trust_initialized: bus and boot policy have initialized admission state\n+ * @trust_policy_override: explicit policy prevents deferred driver boot policy\n * @dead: This device is currently either in the process of or has been\n *\t removed from the system. Any asynchronous events scheduled for this\n *\t device should exit without taking any action.\n@@ -121,6 +132,9 @@ struct device_private {\n \tconst struct device_driver *async_driver;\n \tchar *deferred_probe_reason;\n \tstruct device *device;\n+\tenum device_trust_state trust_state;\n+\tu8 trust_initialized:1;\n+\tu8 trust_policy_override:1;\n \tu8 dead:1;\n };\n #define to_device_private_parent(obj)\t\\\n@@ -166,6 +180,29 @@ void bus_remove_driver(struct device_driver *drv);\n void device_release_driver_internal(struct device *dev, const struct device_driver *drv,\n \t\t\t\t struct device *parent);\n \n+extern const struct attribute_group device_trust_attr_group;\n+\n+#if IS_ENABLED(CONFIG_KUNIT)\n+int device_trust_set_policy(struct device *dev, enum device_trust_policy policy);\n+int device_trust_parse_cmdline(const char *options, const struct device *dev,\n+\t\t\t enum device_trust_policy *policy);\n+void device_trust_apply_driver_policy(struct device *dev,\n+\t\t\t\t const struct device_driver *drv,\n+\t\t\t\t const char *options);\n+bool device_trust_claim_allowed(const struct device *dev,\n+\t\t\t\tconst struct device_driver *drv,\n+\t\t\t\tconst char *device_options,\n+\t\t\t\tconst char *driver_options);\n+void device_trust_apply_boot_policy(struct device *dev,\n+\t\t\t\t enum device_trust_policy policy,\n+\t\t\t\t const char *options);\n+#endif\n+\n+/* All active trust level transitions are serialized by the device lock. */\n+void device_trust_init_state(struct device *dev);\n+int device_trust_prepare(struct device *dev, const struct device_driver *drv);\n+void device_trust_clear(struct device *dev);\n+\n void driver_detach(const struct device_driver *drv);\n void driver_deferred_probe_del(struct device *dev);\n void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);\ndiff --git a/drivers/base/bus.c b/drivers/base/bus.c\nindex 396097fdb31621..ecff32c3704b5f 100644\n--- a/drivers/base/bus.c\n+++ b/drivers/base/bus.c\n@@ -310,13 +310,18 @@ static ssize_t drivers_probe_store(const struct bus_type *bus,\n \t\t\t\t const char *buf, size_t count)\n {\n \tstruct device *dev;\n-\tint err = -EINVAL;\n+\tint err;\n \n \tdev = bus_find_device_by_name(bus, NULL, buf);\n \tif (!dev)\n \t\treturn -ENODEV;\n-\tif (bus_rescan_devices_helper(dev, NULL) == 0)\n+\terr = bus_rescan_devices_helper(dev, NULL);\n+\tif (!err)\n \t\terr = count;\n+\telse if (err == -EPROBE_DEFER)\n+\t\terr = -EAGAIN;\n+\telse\n+\t\terr = -EINVAL;\n \tput_device(dev);\n \treturn err;\n }\ndiff --git a/drivers/base/core.c b/drivers/base/core.c\nindex cbe05197d4a0bc..bd96d84c2ac2c6 100644\n--- a/drivers/base/core.c\n+++ b/drivers/base/core.c\n@@ -2989,10 +2989,14 @@ static int device_add_attrs(struct device *dev)\n \tif (error)\n \t\tgoto err_remove_type_groups;\n \n+\terror = device_add_group(dev, \u0026device_trust_attr_group);\n+\tif (error)\n+\t\tgoto err_remove_dev_groups;\n+\n \tif (device_supports_offline(dev) \u0026\u0026 !dev_offline_disabled(dev)) {\n \t\terror = device_create_file(dev, \u0026dev_attr_online);\n \t\tif (error)\n-\t\t\tgoto err_remove_dev_groups;\n+\t\t\tgoto err_remove_dev_trust_group;\n \t}\n \n \tif (fw_devlink_flags \u0026\u0026 !fw_devlink_is_permissive() \u0026\u0026 dev-\u003efwnode) {\n@@ -3022,6 +3026,8 @@ static int device_add_attrs(struct device *dev)\n \tdevice_remove_file(dev, \u0026dev_attr_waiting_for_supplier);\n err_remove_dev_online:\n \tdevice_remove_file(dev, \u0026dev_attr_online);\n+ err_remove_dev_trust_group:\n+\tdevice_remove_group(dev, \u0026device_trust_attr_group);\n err_remove_dev_groups:\n \tdevice_remove_groups(dev, dev-\u003egroups);\n err_remove_type_groups:\n@@ -3047,6 +3053,7 @@ static void device_remove_attrs(struct device *dev)\n \tdevice_remove_file(dev, \u0026dev_attr_removable);\n \tdevice_remove_file(dev, \u0026dev_attr_waiting_for_supplier);\n \tdevice_remove_file(dev, \u0026dev_attr_online);\n+\tdevice_remove_group(dev, \u0026device_trust_attr_group);\n \tdevice_remove_groups(dev, dev-\u003egroups);\n \n \tif (type)\n@@ -3681,6 +3688,9 @@ int device_add(struct device *dev)\n \tif (error)\n \t\tgoto name_error;\n \n+\tdevice_trust_init(dev);\n+\tdevice_trust_init_state(dev);\n+\n \tpr_debug(\"device: '%s': %s\\n\", dev_name(dev), __func__);\n \n \tparent = get_device(dev-\u003eparent);\n@@ -4788,6 +4798,12 @@ static int device_attrs_change_owner(struct device *dev, kuid_t kuid,\n \tif (error)\n \t\treturn error;\n \n+\t/* Change the trust attributes of @dev to @kuid/@kgid. */\n+\terror = sysfs_group_change_owner(kobj, \u0026device_trust_attr_group, kuid,\n+\t\t\t\t\t kgid);\n+\tif (error)\n+\t\treturn error;\n+\n \tif (device_supports_offline(dev) \u0026\u0026 !dev_offline_disabled(dev)) {\n \t\t/* Change online device attributes of @dev to @kuid/@kgid. */\n \t\terror = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,\ndiff --git a/drivers/base/dd.c b/drivers/base/dd.c\nindex 52123251965de4..0d83f62195f699 100644\n--- a/drivers/base/dd.c\n+++ b/drivers/base/dd.c\n@@ -545,13 +545,21 @@ int device_bind_driver(struct device *dev)\n {\n \tint ret;\n \n+\tif (dev-\u003ebus \u0026\u0026 dev-\u003ebus-\u003etrust_resolve)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tret = device_trust_prepare(dev, dev-\u003edriver);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tret = driver_sysfs_add(dev);\n \tif (!ret) {\n \t\tdevice_links_force_bind(dev);\n \t\tdriver_bound(dev);\n-\t}\n-\telse\n+\t} else {\n \t\tbus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);\n+\t\tdevice_trust_clear(dev);\n+\t}\n \treturn ret;\n }\n EXPORT_SYMBOL_GPL(device_bind_driver);\n@@ -679,6 +687,14 @@ static int really_probe(struct device *dev, const struct device_driver *drv)\n \t}\n \n re_probe:\n+\tret = device_trust_prepare(dev, drv);\n+\tif (ret) {\n+\t\tdevice_links_no_driver(dev);\n+\t\t/* Report it like a probe error so a scan tries the next driver. */\n+\t\tret = -ret;\n+\t\tgoto done;\n+\t}\n+\n \tdevice_set_driver(dev, drv);\n \n \t/* If using pinctrl, bind pins now before probing */\n@@ -745,6 +761,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)\n \t\tif (dev-\u003ebus \u0026\u0026 dev-\u003ebus-\u003edma_cleanup)\n \t\t\tdev-\u003ebus-\u003edma_cleanup(dev);\n \t\tdevice_unbind_cleanup(dev);\n+\t\tdevice_trust_clear(dev);\n \n \t\tgoto re_probe;\n \t}\n@@ -771,6 +788,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)\n pinctrl_bind_failed:\n \tdevice_links_no_driver(dev);\n \tdevice_unbind_cleanup(dev);\n+\tdevice_trust_clear(dev);\n done:\n \treturn ret;\n }\n@@ -1046,6 +1064,25 @@ static void __device_driver_lock(struct device *dev, struct device *parent)\n \tdevice_lock(dev);\n }\n \n+/*\n+ * __device_driver_trylock - __device_driver_lock() without sleeping\n+ *\n+ * Returns true if the locks were taken.\n+ */\n+static bool __device_driver_trylock(struct device *dev, struct device *parent)\n+{\n+\tif (dev-\u003etype \u0026\u0026 dev-\u003etype-\u003eneed_parent_lock \u0026\u0026 parent) {\n+\t\tif (!device_trylock(parent))\n+\t\t\treturn false;\n+\t\tif (!device_trylock(dev)) {\n+\t\t\tdevice_unlock(parent);\n+\t\t\treturn false;\n+\t\t}\n+\t\treturn true;\n+\t}\n+\treturn device_trylock(dev);\n+}\n+\n /*\n * __device_driver_unlock - release locks needed to manipulate dev-\u003edrv\n * @dev: Device we will update driver info for\n@@ -1195,19 +1232,17 @@ void device_initial_probe(struct device *dev)\n \tsubsys_put(sp);\n }\n \n-/**\n- * device_driver_attach - attach a specific driver to a specific device\n- * @drv: Driver to attach\n- * @dev: Device to attach it to\n- *\n- * Manually attach driver to a device. Will acquire both @dev lock and\n- * @dev-\u003eparent lock if needed. Returns 0 on success, -ERR on failure.\n- */\n-int device_driver_attach(const struct device_driver *drv, struct device *dev)\n+static int __device_driver_attach(const struct device_driver *drv,\n+\t\t\t\t struct device *dev, bool trylock)\n {\n \tint ret;\n \n-\t__device_driver_lock(dev, dev-\u003eparent);\n+\tif (trylock) {\n+\t\tif (!__device_driver_trylock(dev, dev-\u003eparent))\n+\t\t\treturn -EBUSY;\n+\t} else {\n+\t\t__device_driver_lock(dev, dev-\u003eparent);\n+\t}\n \tret = __driver_probe_device(drv, dev);\n \t__device_driver_unlock(dev, dev-\u003eparent);\n \n@@ -1218,8 +1253,36 @@ int device_driver_attach(const struct device_driver *drv, struct device *dev)\n \t\treturn -EAGAIN;\n \treturn ret;\n }\n+\n+/**\n+ * device_driver_attach - attach a specific driver to a specific device\n+ * @drv: Driver to attach\n+ * @dev: Device to attach it to\n+ *\n+ * Manually attach driver to a device. Will acquire both @dev lock and\n+ * @dev-\u003eparent lock if needed. Returns 0 on success, -ERR on failure.\n+ */\n+int device_driver_attach(const struct device_driver *drv, struct device *dev)\n+{\n+\treturn __device_driver_attach(drv, dev, false);\n+}\n EXPORT_SYMBOL_GPL(device_driver_attach);\n \n+/**\n+ * device_driver_try_attach - attach a specific driver without waiting for locks\n+ * @drv: Driver to attach\n+ * @dev: Device to attach it to\n+ *\n+ * Like device_driver_attach(), for callers that hold a lock which the holder\n+ * of the @dev or @dev-\u003eparent lock may wait for. Returns -EBUSY, before any\n+ * probing, if one of those locks is contended.\n+ */\n+int device_driver_try_attach(const struct device_driver *drv, struct device *dev)\n+{\n+\treturn __device_driver_attach(drv, dev, true);\n+}\n+EXPORT_SYMBOL_GPL(device_driver_try_attach);\n+\n static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)\n {\n \tstruct device *dev = _dev;\n@@ -1360,6 +1423,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)\n \t\t\tdev-\u003ebus-\u003edma_cleanup(dev);\n \n \t\tdevice_unbind_cleanup(dev);\n+\t\tdevice_trust_clear(dev);\n \t\tdevice_links_driver_cleanup(dev);\n \n \t\tklist_remove(\u0026dev-\u003ep-\u003eknode_driver);\ndiff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile\nindex 9ced7bbd569fc4..dfff5e1d83f7c8 100644\n--- a/drivers/base/test/Makefile\n+++ b/drivers/base/test/Makefile\n@@ -3,6 +3,7 @@ obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE)\t+= test_async_driver_probe.o\n \n obj-$(CONFIG_DM_KUNIT_TEST)\t+= root-device-test.o\n obj-$(CONFIG_DM_KUNIT_TEST)\t+= platform-device-test.o\n+obj-$(CONFIG_DM_KUNIT_TEST)\t+= device-trust-test.o\n \n obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o\n CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN)\ndiff --git a/drivers/base/test/device-trust-test.c b/drivers/base/test/device-trust-test.c\nnew file mode 100644\nindex 00000000000000..590676648e4851\n--- /dev/null\n+++ b/drivers/base/test/device-trust-test.c\n@@ -0,0 +1,1607 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ckunit/platform_device.h\u003e\n+#include \u003ckunit/resource.h\u003e\n+#include \u003ckunit/test.h\u003e\n+\n+#include \u003clinux/completion.h\u003e\n+#include \u003clinux/device.h\u003e\n+#include \u003clinux/err.h\u003e\n+#include \u003clinux/errno.h\u003e\n+#include \u003clinux/module.h\u003e\n+#include \u003clinux/platform_device.h\u003e\n+#include \u003clinux/slab.h\u003e\n+\n+#include \"../base.h\"\n+\n+struct device_trust_test_context {\n+\tstruct device dev;\n+\tstruct device_driver drv;\n+\n+\tenum device_trust_level resolve_level;\n+\tenum device_trust_policy resolve_policy;\n+\tenum device_trust_level configure_level;\n+\tenum device_trust_level probe_level;\n+\tenum device_trust_level remove_level;\n+\tenum device_trust_level cleanup_level;\n+\tconst struct device_driver *resolve_driver;\n+\tconst struct device_driver *reject_driver;\n+\tstruct completion *released;\n+\n+\tint match_ret;\n+\tint resolve_ret;\n+\tint configure_ret;\n+\tint probe_ret;\n+\tunsigned int resolve_count;\n+\tunsigned int configure_count;\n+\tunsigned int probe_count;\n+\tunsigned int remove_count;\n+\tunsigned int cleanup_count;\n+\tbool probe_adversarial;\n+\tbool device_initialized;\n+\tbool device_registered;\n+\tbool driver_registered;\n+};\n+\n+static struct device_trust_test_context *\n+to_device_trust_test_context(struct device *dev)\n+{\n+\treturn container_of(dev, struct device_trust_test_context, dev);\n+}\n+\n+static int device_trust_test_match(struct device *dev,\n+\t\t\t\t const struct device_driver *drv)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\treturn READ_ONCE(ctx-\u003ematch_ret);\n+}\n+\n+static int device_trust_test_resolve(struct device *dev,\n+\t\t\t\t const struct device_driver *drv,\n+\t\t\t\t enum device_trust_policy policy,\n+\t\t\t\t enum device_trust_level *level)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\tctx-\u003eresolve_count++;\n+\tctx-\u003eresolve_policy = policy;\n+\tctx-\u003eresolve_driver = drv;\n+\tif (ctx-\u003ereject_driver \u0026\u0026 drv == ctx-\u003ereject_driver)\n+\t\treturn -EPERM;\n+\tif (ctx-\u003eresolve_ret)\n+\t\treturn ctx-\u003eresolve_ret;\n+\n+\t*level = ctx-\u003eresolve_level;\n+\treturn 0;\n+}\n+\n+static int device_trust_test_dma_configure(struct device *dev)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\tctx-\u003econfigure_count++;\n+\tctx-\u003econfigure_level = device_get_trust_level(dev);\n+\treturn ctx-\u003econfigure_ret;\n+}\n+\n+static void device_trust_test_dma_cleanup(struct device *dev)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\tctx-\u003ecleanup_count++;\n+\tctx-\u003ecleanup_level = device_get_trust_level(dev);\n+}\n+\n+static int device_trust_test_probe(struct device *dev)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\tctx-\u003eprobe_count++;\n+\tctx-\u003eprobe_level = device_get_trust_level(dev);\n+\tctx-\u003eprobe_adversarial = device_is_adversarial(dev);\n+\treturn ctx-\u003eprobe_ret;\n+}\n+\n+static int device_trust_test_remove(struct device *dev)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\n+\tctx-\u003eremove_count++;\n+\tctx-\u003eremove_level = device_get_trust_level(dev);\n+\treturn 0;\n+}\n+\n+static const struct bus_type device_trust_test_plain_bus = {\n+\t.name = \"device-trust-test-plain\",\n+\t.match = device_trust_test_match,\n+\t.dma_configure = device_trust_test_dma_configure,\n+\t.dma_cleanup = device_trust_test_dma_cleanup,\n+};\n+\n+static const struct bus_type device_trust_test_resolving_bus = {\n+\t.name = \"device-trust-test-resolving\",\n+\t.match = device_trust_test_match,\n+\t.trust_resolve = device_trust_test_resolve,\n+\t.dma_configure = device_trust_test_dma_configure,\n+\t.dma_cleanup = device_trust_test_dma_cleanup,\n+};\n+\n+static const struct device_type device_trust_test_child_type = {\n+\t.need_parent_lock = true,\n+};\n+\n+static bool device_trust_test_plain_bus_registered;\n+static bool device_trust_test_resolving_bus_registered;\n+\n+static void device_trust_test_release(struct device *dev)\n+{\n+\tstruct device_trust_test_context *ctx =\n+\t\tto_device_trust_test_context(dev);\n+\tstruct completion *released = ctx-\u003ereleased;\n+\n+\tkfree(ctx);\n+\tcomplete(released);\n+}\n+\n+/*\n+ * The fixture is freed when the last reference to its device is dropped,\n+ * which deferred probing, device links and delayed kobject release may do\n+ * after the test. Wait for that so the release callback never outlives it.\n+ */\n+static void device_trust_test_device_unregister(void *data)\n+{\n+\tstruct device_trust_test_context *ctx = data;\n+\tstruct completion *released = ctx-\u003ereleased;\n+\n+\tif (!ctx-\u003edevice_initialized)\n+\t\treturn;\n+\n+\tctx-\u003edevice_initialized = false;\n+\tif (ctx-\u003edevice_registered) {\n+\t\tctx-\u003edevice_registered = false;\n+\t\tdevice_unregister(\u0026ctx-\u003edev);\n+\t} else {\n+\t\tput_device(\u0026ctx-\u003edev);\n+\t}\n+\tif (!wait_for_completion_timeout(released, 10 * HZ)) {\n+\t\tWARN(1, \"device-trust test fixture is still referenced\\n\");\n+\t\twait_for_completion(released);\n+\t}\n+}\n+\n+static void device_trust_test_driver_unregister(void *data)\n+{\n+\tstruct device_trust_test_context *ctx = data;\n+\n+\tif (!ctx-\u003edriver_registered)\n+\t\treturn;\n+\n+\tctx-\u003edriver_registered = false;\n+\tdriver_unregister(\u0026ctx-\u003edrv);\n+}\n+\n+static struct device_trust_test_context *\n+device_trust_test_device_register_parent(struct kunit *test,\n+\t\t\t\t\t const struct bus_type *bus,\n+\t\t\t\t\t enum device_trust_policy policy,\n+\t\t\t\t\t enum device_trust_policy boot_policy,\n+\t\t\t\t\t struct device *parent)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tstruct completion *released;\n+\tint ret;\n+\n+\treleased = kunit_kzalloc(test, sizeof(*released), GFP_KERNEL);\n+\tif (!released)\n+\t\treturn ERR_PTR(-ENOMEM);\n+\tinit_completion(released);\n+\n+\tctx = kzalloc_obj(*ctx);\n+\tif (!ctx)\n+\t\treturn ERR_PTR(-ENOMEM);\n+\n+\tctx-\u003ereleased = released;\n+\tctx-\u003ematch_ret = 1;\n+\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\n+\tctx-\u003edrv.probe_type = PROBE_FORCE_SYNCHRONOUS;\n+\tctx-\u003edrv.suppress_bind_attrs = true;\n+\tdevice_initialize(\u0026ctx-\u003edev);\n+\tctx-\u003edev.bus = bus;\n+\tctx-\u003edev.parent = parent;\n+\tif (parent \u0026\u0026 bus == \u0026device_trust_test_plain_bus)\n+\t\tctx-\u003edev.type = \u0026device_trust_test_child_type;\n+\tctx-\u003edev.release = device_trust_test_release;\n+\tctx-\u003edev.trust_policy = policy;\n+\tctx-\u003edevice_initialized = true;\n+\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_device_unregister, ctx);\n+\tif (ret)\n+\t\treturn ERR_PTR(ret);\n+\n+\tret = dev_set_name(\u0026ctx-\u003edev, \"%s-%s\",\n+\t\t\t bus ? bus-\u003ename : \"device-trust-test-busless\",\n+\t\t\t test-\u003ename);\n+\tif (ret)\n+\t\treturn ERR_PTR(ret);\n+\n+\tdevice_trust_apply_boot_policy(\u0026ctx-\u003edev, boot_policy, NULL);\n+\tret = device_add(\u0026ctx-\u003edev);\n+\tif (ret)\n+\t\treturn ERR_PTR(ret);\n+\tctx-\u003edevice_registered = true;\n+\n+\treturn ctx;\n+}\n+\n+static struct device_trust_test_context *\n+device_trust_test_device_register(struct kunit *test,\n+\t\t\t\t const struct bus_type *bus,\n+\t\t\t\t enum device_trust_policy policy)\n+{\n+\treturn device_trust_test_device_register_parent(test, bus, policy,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, NULL);\n+}\n+\n+static int device_trust_test_driver_register(struct kunit *test,\n+\t\t\t\t\t struct device_trust_test_context *ctx)\n+{\n+\tint ret;\n+\n+\tctx-\u003edrv.name = test-\u003ename;\n+\tctx-\u003edrv.bus = ctx-\u003edev.bus ?: \u0026device_trust_test_plain_bus;\n+\tctx-\u003edrv.probe = device_trust_test_probe;\n+\tctx-\u003edrv.remove = device_trust_test_remove;\n+\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_driver_unregister, ctx);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tret = driver_register(\u0026ctx-\u003edrv);\n+\tif (ret)\n+\t\treturn ret;\n+\tctx-\u003edriver_registered = true;\n+\n+\treturn 0;\n+}\n+\n+static void device_trust_expect_policy_locked(struct kunit *test,\n+\t\t\t\t\t struct device *dev)\n+{\n+\tenum device_trust_policy original = dev-\u003etrust_policy;\n+\tenum device_trust_level active = device_get_trust_level(dev);\n+\tenum device_trust_policy policy;\n+\n+\tfor (policy = DEVICE_TRUST_POLICY_DEFAULT;\n+\t policy \u003c= DEVICE_TRUST_POLICY_FULL; policy++) {\n+\t\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(dev, policy),\n+\t\t\t\tpolicy == original ? 0 : -EBUSY);\n+\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, original);\n+\t\tKUNIT_EXPECT_EQ(test, device_get_trust_level(dev), active);\n+\t}\n+}\n+\n+static void device_trust_default_bind_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_plain_bus, DEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_FALSE(test, ctx-\u003eprobe_adversarial);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_FULL);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_FULL);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_bus_adversary_bind_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_policy,\n+\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003eresolve_driver, \u0026ctx-\u003edrv);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_TRUE(test, ctx-\u003eprobe_adversarial);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+}\n+\n+static void device_trust_test_other_driver_unregister(void *data)\n+{\n+\tdriver_unregister(data);\n+}\n+\n+static void device_trust_default_pinned_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tstruct device_driver *other;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\n+\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tother = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL);\n+\tKUNIT_ASSERT_NOT_NULL(test, other);\n+\tother-\u003ename = \"device-trust-pinned-other\";\n+\tother-\u003ebus = ctx-\u003edev.bus;\n+\tother-\u003eprobe = device_trust_test_probe;\n+\tother-\u003eremove = device_trust_test_remove;\n+\tother-\u003eprobe_type = PROBE_FORCE_SYNCHRONOUS;\n+\tother-\u003esuppress_bind_attrs = true;\n+\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\n+\tKUNIT_ASSERT_EQ(test, driver_register(other), 0);\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_other_driver_unregister, other);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_EQ(test, device_driver_attach(other, \u0026ctx-\u003edev), -EINVAL);\n+\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003eresolve_driver, other);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DEFAULT);\n+\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(other, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003edev.driver, other);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_authorize_test(struct kunit *test,\n+\t\t\t\t\tenum device_trust_policy initial_policy,\n+\t\t\t\t\tenum device_trust_policy boot_policy,\n+\t\t\t\t\tenum device_trust_policy policy,\n+\t\t\t\t\tenum device_trust_level level)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register_parent(\n+\t\ttest, \u0026device_trust_test_plain_bus, initial_policy, boot_policy, NULL);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DISABLED);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tret = device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ret, -EPERM);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_TRUE(test, dev_can_match(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED), 0);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT), -EBUSY);\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev, policy), 0);\n+\tdevice_trust_apply_boot_policy(\u0026ctx-\u003edev, DEVICE_TRUST_POLICY_DISABLED, NULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, policy);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, level);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, level);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, level);\n+\tdevice_trust_apply_boot_policy(\u0026ctx-\u003edev, DEVICE_TRUST_POLICY_DISABLED, NULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, policy);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tlevel);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_disabled_bind_test(struct kunit *test)\n+{\n+\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,\n+\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_ADVERSARY,\n+\t\t\t\t DEVICE_TRUST_ADVERSARY);\n+}\n+\n+static void device_trust_disabled_full_bind_test(struct kunit *test)\n+{\n+\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,\n+\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_FULL,\n+\t\t\t\t DEVICE_TRUST_FULL);\n+}\n+\n+static void device_trust_global_disabled_adversary_test(struct kunit *test)\n+{\n+\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARY,\n+\t\t\t\t DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_ADVERSARY,\n+\t\t\t\t DEVICE_TRUST_ADVERSARY);\n+}\n+\n+static void device_trust_global_disabled_full_test(struct kunit *test)\n+{\n+\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_FULL,\n+\t\t\t\t DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_FULL,\n+\t\t\t\t DEVICE_TRUST_FULL);\n+}\n+\n+static void device_trust_test_link_wait(void *data)\n+{\n+\tdevice_link_wait_removal();\n+}\n+\n+static void device_trust_supplier_retry_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *supplier, *consumer;\n+\tstruct device_link *link;\n+\tint ret;\n+\n+\tsupplier = device_trust_test_device_register(test, \u0026device_trust_test_plain_bus,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, supplier);\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, supplier), 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026supplier-\u003edev));\n+\t/* Runs after the consumer is gone and before the supplier goes. */\n+\tret = kunit_add_action_or_reset(test, device_trust_test_link_wait, NULL);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tconsumer = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, consumer);\n+\tconsumer-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tlink = device_link_add(\u0026consumer-\u003edev, \u0026supplier-\u003edev, 0);\n+\tKUNIT_ASSERT_NOT_NULL(test, link);\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, consumer), 0);\n+\tKUNIT_ASSERT_EQ(test, link-\u003estatus, DL_STATE_AVAILABLE);\n+\tKUNIT_EXPECT_EQ(test, consumer-\u003edev.links.status, DL_DEV_NO_DRIVER);\n+\tKUNIT_EXPECT_EQ(test, consumer-\u003econfigure_count, 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026consumer-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tconsumer-\u003eresolve_ret = -EAGAIN;\n+\tKUNIT_EXPECT_EQ(test, device_driver_attach(\u0026consumer-\u003edrv, \u0026consumer-\u003edev), -EAGAIN);\n+\tKUNIT_ASSERT_EQ(test, link-\u003estatus, DL_STATE_AVAILABLE);\n+\tKUNIT_EXPECT_EQ(test, consumer-\u003edev.links.status, DL_DEV_NO_DRIVER);\n+\n+\tconsumer-\u003eresolve_ret = 0;\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026consumer-\u003edrv, \u0026consumer-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026consumer-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, link-\u003estatus, DL_STATE_ACTIVE);\n+\tdevice_release_driver(\u0026consumer-\u003edev);\n+\tKUNIT_ASSERT_EQ(test, link-\u003estatus, DL_STATE_AVAILABLE);\n+\tdevice_release_driver(\u0026supplier-\u003edev);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026supplier-\u003edev));\n+}\n+\n+static void device_trust_match_defer_authorize_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\n+\tctx = device_trust_test_device_register(test, \u0026device_trust_test_plain_bus,\n+\t\t\t\t\t\tDEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003ematch_ret = -EPROBE_DEFER;\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\tKUNIT_EXPECT_TRUE(test, dev_can_match(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tWRITE_ONCE(ctx-\u003ematch_ret, 1);\n+\tKUNIT_ASSERT_EQ(test, device_attach(\u0026ctx-\u003edev), 1);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+}\n+\n+static void device_trust_test_parent_unregister(void *data)\n+{\n+\troot_device_unregister(data);\n+}\n+\n+static void device_trust_async_policy_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tstruct device *parent;\n+\tint ret;\n+\n+\tparent = root_device_register(test-\u003ename);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_parent_unregister, parent);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tctx = device_trust_test_device_register_parent(test, \u0026device_trust_test_plain_bus,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, parent);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003edrv.probe_type = PROBE_PREFER_ASYNCHRONOUS;\n+\n+\t/* The queued probe cannot acquire the parent until policy is disabled. */\n+\tdevice_lock(parent);\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tif (!ret)\n+\t\tret = device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED);\n+\tdevice_unlock(parent);\n+\twait_for_device_probe();\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_TRUE(test, dev_can_match(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT), -EBUSY);\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+}\n+\n+static void device_trust_generic_adversary_bind_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_plain_bus,\n+\t\tDEVICE_TRUST_POLICY_ADVERSARY);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_TRUE(test, ctx-\u003eprobe_adversarial);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+}\n+\n+static int device_trust_platform_probe(struct platform_device *pdev)\n+{\n+\treturn 0;\n+}\n+\n+static void device_trust_no_iommu_bind_test(struct kunit *test)\n+{\n+\tstruct platform_device *pdev;\n+\tstruct platform_driver *drv;\n+\n+\tdrv = kunit_kzalloc(test, sizeof(*drv), GFP_KERNEL);\n+\tKUNIT_ASSERT_NOT_NULL(test, drv);\n+\tdrv-\u003edriver.name = test-\u003ename;\n+\tdrv-\u003edriver.probe_type = PROBE_FORCE_SYNCHRONOUS;\n+\tdrv-\u003edriver.suppress_bind_attrs = true;\n+\tdrv-\u003eprobe = device_trust_platform_probe;\n+\tpdev = kunit_platform_device_alloc(test, test-\u003ename, PLATFORM_DEVID_NONE);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);\n+\tpdev-\u003edev.trust_policy = DEVICE_TRUST_POLICY_DISABLED;\n+\tKUNIT_ASSERT_EQ(test, kunit_platform_device_add(test, pdev), 0);\n+\tKUNIT_ASSERT_PTR_EQ(test, pdev-\u003edev.iommu_group, NULL);\n+\tKUNIT_ASSERT_EQ(test, kunit_platform_driver_register(test, drv), 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026pdev-\u003edev));\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026pdev-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026drv-\u003edriver, \u0026pdev-\u003edev), 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026pdev-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026pdev-\u003edev), DEVICE_TRUST_ADVERSARY);\n+\n+\tdevice_release_driver(\u0026pdev-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026pdev-\u003edev), DEVICE_TRUST_DISABLED);\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026drv-\u003edriver, \u0026pdev-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026pdev-\u003edev));\n+}\n+\n+static void device_trust_explicit_policy_mismatch_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_ADVERSARY);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tret = device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ret, -EINVAL);\n+\tKUNIT_EXPECT_GT(test, ctx-\u003eresolve_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_policy,\n+\t\t\tDEVICE_TRUST_POLICY_ADVERSARY);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_FULL), 0);\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_FULL);\n+}\n+\n+static void device_trust_resolver_retry_test(struct kunit *test, int error,\n+\t\t\t\t\t enum device_trust_level level)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\n+\tctx = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\n+\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_ret = error;\n+\tctx-\u003eresolve_level = level;\n+\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_GT(test, ctx-\u003eresolve_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_FULL), 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT), 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED), 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tKUNIT_EXPECT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), error ?: -EINVAL);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tctx-\u003eresolve_ret = 0;\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_resolver_failure_test(struct kunit *test)\n+{\n+\tdevice_trust_resolver_retry_test(test, -EAGAIN, DEVICE_TRUST_FULL);\n+}\n+\n+static void device_trust_resolver_disabled_test(struct kunit *test)\n+{\n+\tdevice_trust_resolver_retry_test(test, 0, DEVICE_TRUST_DISABLED);\n+}\n+\n+static void device_trust_probe_failure_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_ADVERSARY);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tctx-\u003eprobe_ret = -EINVAL;\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_probe_defer_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tctx-\u003eprobe_ret = -EPROBE_DEFER;\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tctx-\u003eprobe_ret = 0;\n+\tret = device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_count, 2);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 2);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 2);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_ADVERSARY);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 2);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_dma_failure_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tctx-\u003econfigure_ret = -EIO;\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_ADVERSARY);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 0);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_ADVERSARY);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+\n+\tctx-\u003econfigure_ret = 0;\n+\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\n+\tKUNIT_EXPECT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), -EINVAL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\n+\tKUNIT_ASSERT_EQ(test, device_driver_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 2);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+}\n+\n+static void device_trust_force_bind_guard_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tunsigned int resolve_count;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus,\n+\t\tDEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tresolve_count = ctx-\u003eresolve_count;\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\n+\tret = device_bind_driver(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = NULL;\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\n+\tKUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_count, resolve_count);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+}\n+\n+static void device_trust_legacy_force_bind_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_plain_bus,\n+\t\tDEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_FULL), 0);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\n+\tret = device_bind_driver(\u0026ctx-\u003edev);\n+\tif (ret)\n+\t\tctx-\u003edev.driver = NULL;\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_FULL);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+}\n+\n+static void device_trust_busless_force_bind_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, NULL, DEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\n+\tret = device_trust_test_driver_register(test, ctx);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\n+\tret = device_bind_driver(\u0026ctx-\u003edev);\n+\tif (ret)\n+\t\tctx-\u003edev.driver = NULL;\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tKUNIT_ASSERT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_FULL);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_FULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 0);\n+\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\n+\t\t\tDEVICE_TRUST_DISABLED);\n+}\n+\n+static const struct bus_type device_trust_test_pci_bus = { .name = \"pci\" };\n+static const struct bus_type device_trust_test_usb_bus = { .name = \"usb\" };\n+static const struct bus_type device_trust_test_platform_bus = { .name = \"platform\" };\n+\n+struct device_trust_test_stub {\n+\tstruct device_driver drv;\n+\tstruct device_private private;\n+\tstruct device dev;\n+};\n+\n+/* Stack instances of these objects overflow the frame limit under KASAN. */\n+static struct device_trust_test_stub *\n+device_trust_test_stub_alloc(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub;\n+\n+\tstub = kunit_kzalloc(test, sizeof(*stub), GFP_KERNEL);\n+\tKUNIT_ASSERT_NOT_NULL(test, stub);\n+\n+\treturn stub;\n+}\n+\n+static struct device *\n+device_trust_test_stub_reset(struct device_trust_test_stub *stub,\n+\t\t\t const struct bus_type *bus, const char *name,\n+\t\t\t enum device_trust_policy policy)\n+{\n+\tmemset(stub, 0, sizeof(*stub));\n+\tstub-\u003edrv.bus = bus;\n+\tstub-\u003edrv.name = \"test\";\n+\tstub-\u003edev.bus = bus;\n+\tstub-\u003edev.init_name = name;\n+\tstub-\u003edev.trust_policy = policy;\n+\tmutex_init(\u0026stub-\u003edev.mutex);\n+\n+\treturn \u0026stub-\u003edev;\n+}\n+\n+static struct device *\n+device_trust_test_pci_stub(struct device_trust_test_stub *stub, const char *name,\n+\t\t\t enum device_trust_policy policy)\n+{\n+\treturn device_trust_test_stub_reset(stub, \u0026device_trust_test_pci_bus, name,\n+\t\t\t\t\t policy);\n+}\n+\n+static void device_trust_cmdline_policies_test(struct kunit *test)\n+{\n+\tstatic const char options[] =\n+\t\t\"pci:0000:01:00.0=adversary,usb:1-1=full,usb:1-2=disabled,platform:i8042=default\";\n+\tstatic const struct {\n+\t\tconst struct bus_type *bus;\n+\t\tconst char *name;\n+\t\tenum device_trust_policy policy;\n+\t} cases[] = {\n+\t\t{ \u0026device_trust_test_pci_bus, \"0000:01:00.0\", DEVICE_TRUST_POLICY_ADVERSARY },\n+\t\t{ \u0026device_trust_test_usb_bus, \"1-1\", DEVICE_TRUST_POLICY_FULL },\n+\t\t{ \u0026device_trust_test_usb_bus, \"1-2\", DEVICE_TRUST_POLICY_DISABLED },\n+\t\t{ \u0026device_trust_test_platform_bus, \"i8042\", DEVICE_TRUST_POLICY_DEFAULT },\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tunsigned int i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(cases); i++) {\n+\t\tenum device_trust_policy policy = DEVICE_TRUST_POLICY_DISABLED;\n+\t\tstruct device *dev = device_trust_test_stub_reset(stub, cases[i].bus,\n+\t\t\t\t\t\t\t\t cases[i].name,\n+\t\t\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT);\n+\t\tint ret;\n+\n+\t\tret = device_trust_parse_cmdline(options, dev, \u0026policy);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, ret, 1, \"case %u\", i);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, policy, cases[i].policy, \"case %u\", i);\n+\t}\n+}\n+\n+static void device_trust_cmdline_exact_match_test(struct kunit *test)\n+{\n+\tstatic const char * const options[] = {\n+\t\t\"pcie:0000:01:00.0=full\",\n+\t\t\"pc:0000:01:00.0=full\",\n+\t\t\"usb:0000:01:00.0=full\",\n+\t\t\"pci:0000:01:00.01=full\",\n+\t\t\"pci:0000:01:00=full\",\n+\t\t\"pci:0000:01:00.1=full\",\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, \"0000:01:00.0\",\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tunsigned int i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(options); i++) {\n+\t\tenum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;\n+\t\tint ret;\n+\n+\t\tret = device_trust_parse_cmdline(options[i], dev, \u0026policy);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, ret, 0, \"%s\", options[i]);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, policy, DEVICE_TRUST_POLICY_ADVERSARY,\n+\t\t\t\t \"%s\", options[i]);\n+\t}\n+}\n+\n+static void device_trust_cmdline_last_match_test(struct kunit *test)\n+{\n+\tstatic const char options[] =\n+\t\t\"pci:0000:01:00.0=disabled,usb:1-1=full,pci:0000:01:00.0=full,usb:1-2=disabled\";\n+\tstatic const char defaults[] =\n+\t\t\"pci:0000:01:00.0=disabled,pci:0000:01:00.0=default\";\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, \"0000:01:00.0\",\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tenum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;\n+\tint ret;\n+\n+\tret = device_trust_parse_cmdline(options, dev, \u0026policy);\n+\tKUNIT_ASSERT_EQ(test, ret, 1);\n+\tKUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_FULL);\n+\n+\tret = device_trust_parse_cmdline(defaults, dev, \u0026policy);\n+\tKUNIT_ASSERT_EQ(test, ret, 1);\n+\tKUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_DEFAULT);\n+}\n+\n+static void device_trust_cmdline_nameless_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, NULL,\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tenum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;\n+\tint ret;\n+\n+\tret = device_trust_parse_cmdline(\"pci:0000:01:00.0=full\", dev, \u0026policy);\n+\tKUNIT_EXPECT_EQ(test, ret, 0);\n+\tKUNIT_EXPECT_EQ(test, policy, DEVICE_TRUST_POLICY_ADVERSARY);\n+}\n+\n+static void device_trust_cmdline_validate_test(struct kunit *test)\n+{\n+\tstatic const char valid[] = \"pci:0000:01:00.0=adversary,usb:1-2=disabled\";\n+\tstatic const char invalid[] = \"pci:0000:01:00.0=adversary,usb:1-2=invalid\";\n+\n+\tKUNIT_EXPECT_EQ(test, device_trust_parse_cmdline(valid, NULL, NULL), 0);\n+\tKUNIT_EXPECT_EQ(test, device_trust_parse_cmdline(invalid, NULL, NULL), -EINVAL);\n+}\n+\n+static void device_trust_cmdline_invalid_test(struct kunit *test)\n+{\n+\tstatic const char * const options[] = {\n+\t\t\"\",\n+\t\t\",pci:0000:01:00.0=full\",\n+\t\t\"pci:0000:01:00.0=full,\",\n+\t\t\"pci:0000:01:00.0=full,,usb:1-2=disabled\",\n+\t\t\":0000:01:00.0=full\",\n+\t\t\"pci:=full\",\n+\t\t\"pci:0000:01:00.0=\",\n+\t\t\"pci=full\",\n+\t\t\"pci=bad:0000:01:00.0=full\",\n+\t\t\"pci:0000:01:00.0\",\n+\t\t\"pci:0000:01:00.0=unknown\",\n+\t\t\"pci:0000:01:00.0=full=disabled\",\n+\t\t\"pc i:0000:01:00.0=full\",\n+\t\t\"pci:0000:01:00.0 =full\",\n+\t\t\"pci:0000:01:00.0= full\",\n+\t\t\"pci:0000:01:00.0=full\\n\",\n+\t\t\"pci:0000:01:00.0\\t=full\",\n+\t\t\"pci:0000:01:00.0\\001=full\",\n+\t\t\"p\\\"ci:0000:01:00.0=full\",\n+\t\t\"pci:0000:01:00.0=\\\"full\\\"\",\n+\t\t\"pci:0000:01:00.0=full,usb:1-2=unknown\",\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, \"0000:01:00.0\",\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tunsigned int i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(options); i++) {\n+\t\tenum device_trust_policy policy = DEVICE_TRUST_POLICY_ADVERSARY;\n+\t\tint ret;\n+\n+\t\tret = device_trust_parse_cmdline(options[i], dev, \u0026policy);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, ret, -EINVAL, \"case %u\", i);\n+\t\tKUNIT_EXPECT_EQ_MSG(test, policy, DEVICE_TRUST_POLICY_ADVERSARY,\n+\t\t\t\t \"case %u\", i);\n+\t}\n+}\n+\n+static void device_trust_boot_precedence_test(struct kunit *test)\n+{\n+\tstatic const char * const overrides[] = {\n+\t\t[DEVICE_TRUST_POLICY_DEFAULT] = \"pci:0000:01:00.0=default\",\n+\t\t[DEVICE_TRUST_POLICY_DISABLED] = \"pci:0000:01:00.0=disabled\",\n+\t\t[DEVICE_TRUST_POLICY_ADVERSARY] = \"pci:0000:01:00.0=adversary\",\n+\t\t[DEVICE_TRUST_POLICY_FULL] = \"pci:0000:01:00.0=full\",\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tunsigned int initial, global, override;\n+\n+\tfor (initial = DEVICE_TRUST_POLICY_DEFAULT;\n+\t initial \u003c= DEVICE_TRUST_POLICY_FULL; initial++) {\n+\t\tfor (global = DEVICE_TRUST_POLICY_DEFAULT;\n+\t\t global \u003c= DEVICE_TRUST_POLICY_FULL; global++) {\n+\t\t\tstruct device *dev = device_trust_test_pci_stub(stub, \"0000:01:00.0\",\n+\t\t\t\t\t\t\t\t\tinitial);\n+\t\t\tenum device_trust_policy expected = global ?: initial;\n+\n+\t\t\tdevice_trust_apply_boot_policy(dev, global, NULL);\n+\t\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, expected);\n+\t\t\tdevice_trust_apply_boot_policy(dev, global,\n+\t\t\t\t\t\t \"pci:0000:02:00.0=full\");\n+\t\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, expected);\n+\t\t\tfor (override = 0; override \u003c ARRAY_SIZE(overrides); override++) {\n+\t\t\t\tdev-\u003etrust_policy = initial;\n+\t\t\t\tdevice_trust_apply_boot_policy(dev, global,\n+\t\t\t\t\t\t\t overrides[override]);\n+\t\t\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, override);\n+\t\t\t\tdevice_trust_apply_boot_policy(dev, global,\n+\t\t\t\t\t\t\t overrides[override]);\n+\t\t\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, override);\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+static void device_trust_global_busless_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_stub_reset(stub, NULL, NULL,\n+\t\t\t\t\t\t\t DEVICE_TRUST_POLICY_FULL);\n+\n+\tdevice_trust_apply_boot_policy(dev, DEVICE_TRUST_POLICY_DISABLED,\n+\t\t\t\t \"pci:0000:01:00.0=full\");\n+\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, DEVICE_TRUST_POLICY_DISABLED);\n+}\n+\n+static void device_trust_driver_precedence_test(struct kunit *test)\n+{\n+\tstatic const char * const driver_rules[] = {\n+\t\t\"pci:test=default\", \"pci:test=disabled\",\n+\t\t\"pci:test=adversary\", \"pci:test=full\",\n+\t};\n+\tstatic const char * const device_rules[] = {\n+\t\tNULL, \"pci:0000:01:00.0=default\", \"pci:0000:01:00.0=disabled\",\n+\t\t\"pci:0000:01:00.0=adversary\", \"pci:0000:01:00.0=full\",\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tunsigned int initial, global, driver, device;\n+\tstruct device *dev;\n+\tbool allowed, ok;\n+\n+\tfor (initial = DEVICE_TRUST_POLICY_DEFAULT;\n+\t initial \u003c= DEVICE_TRUST_POLICY_FULL; initial++) {\n+\t\tfor (global = DEVICE_TRUST_POLICY_DEFAULT;\n+\t\t global \u003c= DEVICE_TRUST_POLICY_FULL; global++) {\n+\t\t\tfor (driver = 0; driver \u003c ARRAY_SIZE(driver_rules); driver++) {\n+\t\t\t\tfor (device = 0; device \u003c ARRAY_SIZE(device_rules); device++) {\n+\t\t\t\t\tdev = device_trust_test_pci_stub(stub, \"0000:01:00.0\",\n+\t\t\t\t\t\t\t\t\t initial);\n+\n+\t\t\t\t\tdevice_trust_apply_boot_policy(dev, global,\n+\t\t\t\t\t\t\t\t device_rules[device]);\n+\t\t\t\t\t/* The verdict for a claim before registration */\n+\t\t\t\t\tallowed = device_trust_claim_allowed(dev, \u0026stub-\u003edrv,\n+\t\t\t\t\t\t\t\t\t device_rules[device],\n+\t\t\t\t\t\t\t\t\t driver_rules[driver]);\n+\t\t\t\t\tdev-\u003ep = \u0026stub-\u003eprivate;\n+\t\t\t\t\tdevice_trust_apply_boot_policy(dev, global,\n+\t\t\t\t\t\t\t\t device_rules[device]);\n+\t\t\t\t\tdevice_trust_init_state(dev);\n+\t\t\t\t\tdevice_lock(dev);\n+\t\t\t\t\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv,\n+\t\t\t\t\t\t\t\t\t driver_rules[driver]);\n+\t\t\t\t\tdevice_unlock(dev);\n+\t\t\t\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy,\n+\t\t\t\t\t\t\tdevice ? device - 1 : driver);\n+\t\t\t\t\t/* Driver rules are left to admission */\n+\t\t\t\t\tok = !device || device - 1 != DEVICE_TRUST_POLICY_DISABLED;\n+\t\t\t\t\tKUNIT_EXPECT_EQ(test, allowed, ok);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+static void device_trust_claim_allowed_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev;\n+\n+\tdev = device_trust_test_pci_stub(stub, \"0000:01:00.0\", DEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv, NULL, NULL));\n+\tKUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv, NULL,\n+\t\t\t\t\t\t\t \"pci:other=full\"));\n+\tKUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv,\n+\t\t\t\t\t\t\t \"pci:0000:01:00.0=full\", NULL));\n+\tKUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv,\n+\t\t\t\t\t\t\t \"pci:0000:01:00.0=disabled\",\n+\t\t\t\t\t\t\t \"pci:test=full\"));\n+\tKUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv, NULL,\n+\t\t\t\t\t\t\t \"pci:test=full\"));\n+\tKUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv, NULL,\n+\t\t\t\t\t\t\t \"pci:test=disabled\"));\n+\n+\tdev = device_trust_test_pci_stub(stub, \"0000:01:00.0\", DEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, \u0026stub-\u003edrv, NULL, NULL));\n+}\n+\n+static void device_trust_driver_matching_test(struct kunit *test)\n+{\n+\tstatic const char * const unmatched[] = {\n+\t\tNULL, \"usb:test=disabled\", \"pc:test=disabled\", \"pcie:test=disabled\",\n+\t\t\"pci:tes=disabled\", \"pci:test1=disabled\", \"pci:te*=disabled\",\n+\t\t\"pci:test=disabled,pci:other=invalid\", \"pci:test=disabled,\",\n+\t};\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, NULL,\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tunsigned int i;\n+\n+\tdev-\u003ep = \u0026stub-\u003eprivate;\n+\tdevice_trust_init_state(dev);\n+\tfor (i = 0; i \u003c ARRAY_SIZE(unmatched); i++) {\n+\t\tdevice_lock(dev);\n+\t\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv, unmatched[i]);\n+\t\tdevice_unlock(dev);\n+\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, DEVICE_TRUST_POLICY_DEFAULT);\n+\t}\n+\tdevice_lock(dev);\n+\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv,\n+\t\t\t\t\t \"pci:test=disabled,pci:test=adversary\");\n+\tdevice_unlock(dev);\n+\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, DEVICE_TRUST_POLICY_ADVERSARY);\n+}\n+\n+static void device_trust_driver_user_precedence_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tenum device_trust_policy policy;\n+\n+\tfor (policy = DEVICE_TRUST_POLICY_DEFAULT;\n+\t policy \u003c= DEVICE_TRUST_POLICY_FULL; policy++) {\n+\t\tstruct device *dev = device_trust_test_pci_stub(stub, NULL,\n+\t\t\t\t\t\t\t\tpolicy);\n+\n+\t\tdev-\u003ep = \u0026stub-\u003eprivate;\n+\t\tdevice_trust_init_state(dev);\n+\t\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, policy), 0);\n+\t\tdevice_lock(dev);\n+\t\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv, \"pci:test=disabled\");\n+\t\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv, \"pci:test=full\");\n+\t\tdevice_unlock(dev);\n+\t\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, policy);\n+\t}\n+}\n+\n+static void device_trust_driver_rejected_write_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, NULL,\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DISABLED);\n+\n+\tdev-\u003ep = \u0026stub-\u003eprivate;\n+\tdevice_trust_init_state(dev);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_DEFAULT),\n+\t\t\t-EBUSY);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_FULL + 1),\n+\t\t\t-EINVAL);\n+\tdevice_lock(dev);\n+\tdevice_trust_apply_driver_policy(dev, \u0026stub-\u003edrv, \"pci:test=full\");\n+\tdevice_unlock(dev);\n+\tKUNIT_EXPECT_EQ(test, dev-\u003etrust_policy, DEVICE_TRUST_POLICY_FULL);\n+}\n+\n+static void device_trust_driver_authorize_test(struct kunit *test)\n+{\n+\tconst struct device_driver drv = {\n+\t\t.bus = \u0026device_trust_test_plain_bus,\n+\t\t.name = \"test\",\n+\t};\n+\tstruct device_trust_test_context *ctx;\n+\tconst struct device_driver other = {\n+\t\t.bus = \u0026device_trust_test_plain_bus,\n+\t\t.name = \"other\",\n+\t};\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_plain_bus, DEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026drv,\n+\t\t\t\t\t \"device-trust-test-plain:test=disabled\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026other,\n+\t\t\t\t\t \"device-trust-test-plain:other=full\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026drv,\n+\t\t\t\t\t \"device-trust-test-plain:test=full\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tret = device_attach(\u0026ctx-\u003edev);\n+\tKUNIT_ASSERT_EQ(test, ret, 1);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\n+\n+\tdevice_release_driver(\u0026ctx-\u003edev);\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026drv,\n+\t\t\t\t\t \"device-trust-test-plain:test=full\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_ADVERSARY);\n+\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n+}\n+\n+static void device_trust_driver_resolver_failure_test(struct kunit *test)\n+{\n+\tconst struct device_driver drv = {\n+\t\t.bus = \u0026device_trust_test_resolving_bus,\n+\t\t.name = \"test\",\n+\t};\n+\tstruct device_trust_test_context *ctx;\n+\n+\tctx = device_trust_test_device_register(\n+\t\ttest, \u0026device_trust_test_resolving_bus, DEVICE_TRUST_POLICY_DISABLED);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026drv,\n+\t\t\t\t\t \"device-trust-test-resolving:test=full\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tctx-\u003eresolve_ret = -EIO;\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_policy, DEVICE_TRUST_POLICY_FULL);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 0);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tdevice_trust_apply_driver_policy(\u0026ctx-\u003edev, \u0026drv,\n+\t\t\t\t\t \"device-trust-test-resolving:test=disabled\");\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_FULL);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\n+}\n+\n+static void device_trust_claimed_policy_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\n+\tctx = device_trust_test_device_register(test, \u0026device_trust_test_plain_bus,\n+\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\n+\t/* A driver that claimed the device before registration skipped admission. */\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED), -EBUSY);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT), 0);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DEFAULT);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tctx-\u003edev.driver = NULL;\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED), 0);\n+}\n+\n+static void device_trust_try_attach_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tstruct device *parent;\n+\tbool parent_free;\n+\tint ret;\n+\n+\tparent = root_device_register(test-\u003ename);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_parent_unregister, parent);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\tctx = device_trust_test_device_register_parent(test, \u0026device_trust_test_plain_bus,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT,\n+\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, parent);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003ematch_ret = 0;\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\n+\tdevice_lock(parent);\n+\tKUNIT_EXPECT_EQ(test, device_driver_try_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), -EBUSY);\n+\tdevice_unlock(parent);\n+\n+\tdevice_lock(\u0026ctx-\u003edev);\n+\tKUNIT_EXPECT_EQ(test, device_driver_try_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), -EBUSY);\n+\tdevice_unlock(\u0026ctx-\u003edev);\n+\n+\tparent_free = device_trylock(parent);\n+\tif (parent_free)\n+\t\tdevice_unlock(parent);\n+\tKUNIT_EXPECT_TRUE(test, parent_free);\n+\tKUNIT_EXPECT_FALSE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 0);\n+\n+\tKUNIT_ASSERT_EQ(test, device_driver_try_attach(\u0026ctx-\u003edrv, \u0026ctx-\u003edev), 0);\n+\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+}\n+\n+static void device_trust_scan_continues_test(struct kunit *test)\n+{\n+\tstruct device_trust_test_context *ctx;\n+\tstruct device_driver *other;\n+\tint ret;\n+\n+\tctx = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\n+\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n+\tctx-\u003ematch_ret = 0;\n+\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n+\n+\tother = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL);\n+\tKUNIT_ASSERT_NOT_NULL(test, other);\n+\tother-\u003ename = \"device-trust-scan-other\";\n+\tother-\u003ebus = ctx-\u003edev.bus;\n+\tother-\u003eprobe = device_trust_test_probe;\n+\tother-\u003eremove = device_trust_test_remove;\n+\tother-\u003eprobe_type = PROBE_FORCE_SYNCHRONOUS;\n+\tother-\u003esuppress_bind_attrs = true;\n+\tKUNIT_ASSERT_EQ(test, driver_register(other), 0);\n+\tret = kunit_add_action_or_reset(test,\n+\t\t\t\t\tdevice_trust_test_other_driver_unregister, other);\n+\tKUNIT_ASSERT_EQ(test, ret, 0);\n+\n+\t/* The first candidate is rejected; the scan must reach the second. */\n+\tctx-\u003ereject_driver = \u0026ctx-\u003edrv;\n+\tWRITE_ONCE(ctx-\u003ematch_ret, 1);\n+\tKUNIT_EXPECT_EQ(test, device_attach(\u0026ctx-\u003edev), 1);\n+\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003edev.driver, other);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_count, 2);\n+\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\n+}\n+\n+static struct kunit_case device_trust_test_cases[] = {\n+\tKUNIT_CASE(device_trust_default_bind_test),\n+\tKUNIT_CASE(device_trust_bus_adversary_bind_test),\n+\tKUNIT_CASE(device_trust_default_pinned_test),\n+\tKUNIT_CASE(device_trust_disabled_bind_test),\n+\tKUNIT_CASE(device_trust_disabled_full_bind_test),\n+\tKUNIT_CASE(device_trust_global_disabled_adversary_test),\n+\tKUNIT_CASE(device_trust_global_disabled_full_test),\n+\tKUNIT_CASE(device_trust_supplier_retry_test),\n+\tKUNIT_CASE(device_trust_match_defer_authorize_test),\n+\tKUNIT_CASE(device_trust_async_policy_test),\n+\tKUNIT_CASE(device_trust_generic_adversary_bind_test),\n+\tKUNIT_CASE(device_trust_no_iommu_bind_test),\n+\tKUNIT_CASE(device_trust_explicit_policy_mismatch_test),\n+\tKUNIT_CASE(device_trust_resolver_failure_test),\n+\tKUNIT_CASE(device_trust_resolver_disabled_test),\n+\tKUNIT_CASE(device_trust_probe_failure_test),\n+\tKUNIT_CASE(device_trust_probe_defer_test),\n+\tKUNIT_CASE(device_trust_dma_failure_test),\n+\tKUNIT_CASE(device_trust_force_bind_guard_test),\n+\tKUNIT_CASE(device_trust_legacy_force_bind_test),\n+\tKUNIT_CASE(device_trust_busless_force_bind_test),\n+\tKUNIT_CASE(device_trust_cmdline_policies_test),\n+\tKUNIT_CASE(device_trust_cmdline_exact_match_test),\n+\tKUNIT_CASE(device_trust_cmdline_last_match_test),\n+\tKUNIT_CASE(device_trust_cmdline_nameless_test),\n+\tKUNIT_CASE(device_trust_cmdline_validate_test),\n+\tKUNIT_CASE(device_trust_cmdline_invalid_test),\n+\tKUNIT_CASE(device_trust_boot_precedence_test),\n+\tKUNIT_CASE(device_trust_global_busless_test),\n+\tKUNIT_CASE(device_trust_driver_precedence_test),\n+\tKUNIT_CASE(device_trust_claim_allowed_test),\n+\tKUNIT_CASE(device_trust_driver_matching_test),\n+\tKUNIT_CASE(device_trust_driver_user_precedence_test),\n+\tKUNIT_CASE(device_trust_driver_rejected_write_test),\n+\tKUNIT_CASE(device_trust_driver_authorize_test),\n+\tKUNIT_CASE(device_trust_driver_resolver_failure_test),\n+\tKUNIT_CASE(device_trust_claimed_policy_test),\n+\tKUNIT_CASE(device_trust_try_attach_test),\n+\tKUNIT_CASE(device_trust_scan_continues_test),\n+\t{}\n+};\n+\n+/* The fixtures set their own policy, which a live boot default would replace. */\n+static int device_trust_test_init(struct kunit *test)\n+{\n+\tstruct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);\n+\tstruct device *dev = device_trust_test_pci_stub(stub, \"device-trust-test\",\n+\t\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\n+\n+\tdevice_trust_init(dev);\n+\tif (dev-\u003etrust_policy != DEVICE_TRUST_POLICY_DEFAULT)\n+\t\tkunit_skip(test, \"device_trust.default= replaces the fixture policy\");\n+\n+\treturn 0;\n+}\n+\n+static int device_trust_test_suite_init(struct kunit_suite *suite)\n+{\n+\tint ret;\n+\n+\tret = bus_register(\u0026device_trust_test_plain_bus);\n+\tif (ret)\n+\t\treturn ret;\n+\tdevice_trust_test_plain_bus_registered = true;\n+\n+\tret = bus_register(\u0026device_trust_test_resolving_bus);\n+\tif (ret) {\n+\t\tdevice_trust_test_plain_bus_registered = false;\n+\t\tbus_unregister(\u0026device_trust_test_plain_bus);\n+\t\treturn ret;\n+\t}\n+\tdevice_trust_test_resolving_bus_registered = true;\n+\n+\treturn 0;\n+}\n+\n+static void device_trust_test_suite_exit(struct kunit_suite *suite)\n+{\n+\tif (device_trust_test_resolving_bus_registered) {\n+\t\tdevice_trust_test_resolving_bus_registered = false;\n+\t\tbus_unregister(\u0026device_trust_test_resolving_bus);\n+\t}\n+\tif (device_trust_test_plain_bus_registered) {\n+\t\tdevice_trust_test_plain_bus_registered = false;\n+\t\tbus_unregister(\u0026device_trust_test_plain_bus);\n+\t}\n+}\n+\n+static struct kunit_suite device_trust_test_suite = {\n+\t.name = \"device-trust\",\n+\t.init = device_trust_test_init,\n+\t.suite_init = device_trust_test_suite_init,\n+\t.suite_exit = device_trust_test_suite_exit,\n+\t.test_cases = device_trust_test_cases,\n+};\n+\n+kunit_test_suite(device_trust_test_suite);\n+\n+MODULE_IMPORT_NS(\"EXPORTED_FOR_KUNIT_TESTING\");\n+MODULE_DESCRIPTION(\"KUnit tests for device trust\");\n+MODULE_LICENSE(\"GPL\");\ndiff --git a/drivers/base/trust.c b/drivers/base/trust.c\nnew file mode 100644\nindex 00000000000000..b68644d197b6fc\n--- /dev/null\n+++ b/drivers/base/trust.c\n@@ -0,0 +1,455 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/ctype.h\u003e\n+#include \u003clinux/device.h\u003e\n+#include \u003clinux/errno.h\u003e\n+#include \u003clinux/export.h\u003e\n+#include \u003clinux/init.h\u003e\n+#include \u003clinux/string.h\u003e\n+#include \u003clinux/sysfs.h\u003e\n+#include \u003ckunit/visibility.h\u003e\n+\n+#include \"base.h\"\n+\n+static void device_trust_init_driver_policy(struct device *dev,\n+\t\t\t\t\t const struct device_driver *drv);\n+\n+void device_trust_init_state(struct device *dev)\n+{\n+\tif (dev-\u003ep-\u003etrust_initialized)\n+\t\treturn;\n+\n+\tdev-\u003ep-\u003etrust_state = dev-\u003etrust_policy == DEVICE_TRUST_POLICY_DISABLED ?\n+\t\tDEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;\n+\tdev-\u003ep-\u003etrust_initialized = true;\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_init_state);\n+\n+static bool device_trust_policy_matches(enum device_trust_policy policy,\n+\t\t\t\t\tenum device_trust_level level)\n+{\n+\tswitch (policy) {\n+\tcase DEVICE_TRUST_POLICY_DEFAULT:\n+\t\treturn true;\n+\tcase DEVICE_TRUST_POLICY_DISABLED:\n+\t\treturn level == DEVICE_TRUST_DISABLED;\n+\tcase DEVICE_TRUST_POLICY_ADVERSARY:\n+\t\treturn level == DEVICE_TRUST_ADVERSARY;\n+\tcase DEVICE_TRUST_POLICY_FULL:\n+\t\treturn level == DEVICE_TRUST_FULL;\n+\tdefault:\n+\t\treturn false;\n+\t}\n+}\n+\n+static int device_trust_resolve_default(enum device_trust_policy policy,\n+\t\t\t\t\tenum device_trust_level *level)\n+{\n+\tswitch (policy) {\n+\tcase DEVICE_TRUST_POLICY_DEFAULT:\n+\tcase DEVICE_TRUST_POLICY_FULL:\n+\t\t*level = DEVICE_TRUST_FULL;\n+\t\treturn 0;\n+\tcase DEVICE_TRUST_POLICY_DISABLED:\n+\t\t*level = DEVICE_TRUST_DISABLED;\n+\t\treturn 0;\n+\tcase DEVICE_TRUST_POLICY_ADVERSARY:\n+\t\t*level = DEVICE_TRUST_ADVERSARY;\n+\t\treturn 0;\n+\tdefault:\n+\t\treturn -EINVAL;\n+\t}\n+}\n+\n+int device_trust_prepare(struct device *dev, const struct device_driver *drv)\n+{\n+\tenum device_trust_policy policy;\n+\tenum device_trust_level level = DEVICE_TRUST_DISABLED;\n+\tint ret;\n+\n+\tdevice_lock_assert(dev);\n+\n+\tif (WARN_ON_ONCE(READ_ONCE(dev-\u003etrust_level) != DEVICE_TRUST_DISABLED))\n+\t\treturn -EBUSY;\n+\tdevice_trust_init_driver_policy(dev, drv);\n+\tpolicy = dev-\u003etrust_policy;\n+\tif (policy == DEVICE_TRUST_POLICY_DISABLED)\n+\t\treturn -EPERM;\n+\tif (dev-\u003ebus \u0026\u0026 dev-\u003ebus-\u003etrust_resolve)\n+\t\tret = dev-\u003ebus-\u003etrust_resolve(dev, drv, policy, \u0026level);\n+\telse\n+\t\tret = device_trust_resolve_default(policy, \u0026level);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (!device_trust_policy_matches(policy, level))\n+\t\treturn -EINVAL;\n+\tif (level == DEVICE_TRUST_DISABLED)\n+\t\treturn -EPERM;\n+\tif (level != DEVICE_TRUST_ADVERSARY \u0026\u0026 level != DEVICE_TRUST_FULL)\n+\t\treturn -EINVAL;\n+\tif (dev-\u003ep-\u003etrust_state == DEVICE_TRUST_COMMITTED \u0026\u0026\n+\t dev-\u003etrust_committed_level != level)\n+\t\treturn -EINVAL;\n+\n+\tWRITE_ONCE(dev-\u003etrust_committed_level, level);\n+\tdev-\u003ep-\u003etrust_state = DEVICE_TRUST_COMMITTED;\n+\tWRITE_ONCE(dev-\u003etrust_level, level);\n+\treturn 0;\n+}\n+\n+void device_trust_clear(struct device *dev)\n+{\n+\tdevice_lock_assert(dev);\n+\tWRITE_ONCE(dev-\u003etrust_level, DEVICE_TRUST_DISABLED);\n+}\n+\n+/**\n+ * device_get_trust_level - Return a device's active trust level\n+ * @dev: device to inspect\n+ *\n+ * The value is stable from the start of DMA configuration through the end of\n+ * DMA cleanup. An unbound device has %DEVICE_TRUST_DISABLED as its active\n+ * trust level.\n+ *\n+ * Return: The active trust level for the current bind transaction.\n+ */\n+enum device_trust_level device_get_trust_level(const struct device *dev)\n+{\n+\treturn READ_ONCE(dev-\u003etrust_level);\n+}\n+EXPORT_SYMBOL_GPL(device_get_trust_level);\n+\n+/**\n+ * device_get_committed_trust_level - Return the lifetime trust commitment\n+ * @dev: device to inspect\n+ *\n+ * Return: %DEVICE_TRUST_DISABLED before admission, otherwise the level fixed\n+ * before driver setup. The commitment survives setup failure and unbind.\n+ */\n+enum device_trust_level device_get_committed_trust_level(const struct device *dev)\n+{\n+\treturn READ_ONCE(dev-\u003etrust_committed_level);\n+}\n+EXPORT_SYMBOL_GPL(device_get_committed_trust_level);\n+\n+/**\n+ * device_is_adversarial - Test whether a device is operated as an adversary\n+ * @dev: device to inspect\n+ *\n+ * Return: True when the active trust level is %DEVICE_TRUST_ADVERSARY.\n+ */\n+bool device_is_adversarial(const struct device *dev)\n+{\n+\treturn device_get_trust_level(dev) == DEVICE_TRUST_ADVERSARY;\n+}\n+EXPORT_SYMBOL_GPL(device_is_adversarial);\n+\n+static const char * const device_trust_policy_names[] = {\n+\t[DEVICE_TRUST_POLICY_DEFAULT] = \"default\",\n+\t[DEVICE_TRUST_POLICY_DISABLED] = \"disabled\",\n+\t[DEVICE_TRUST_POLICY_ADVERSARY] = \"adversary\",\n+\t[DEVICE_TRUST_POLICY_FULL] = \"full\",\n+};\n+\n+static const char * const device_trust_level_names[] = {\n+\t[DEVICE_TRUST_DISABLED] = \"disabled\",\n+\t[DEVICE_TRUST_ADVERSARY] = \"adversary\",\n+\t[DEVICE_TRUST_FULL] = \"full\",\n+};\n+\n+static ssize_t trust_show(struct device *dev,\n+\t\t\t const struct device_attribute *attr, char *buf)\n+{\n+\treturn sysfs_emit(buf, \"%s\\n\",\n+\t\t\t device_trust_level_names[device_get_trust_level(dev)]);\n+}\n+static DEVICE_ATTR_RO(trust);\n+\n+static ssize_t trust_policy_show(struct device *dev,\n+\t\t\t\t const struct device_attribute *attr, char *buf)\n+{\n+\treturn sysfs_emit(buf, \"%s\\n\",\n+\t\t\t device_trust_policy_names[READ_ONCE(dev-\u003etrust_policy)]);\n+}\n+\n+VISIBLE_IF_KUNIT int device_trust_set_policy(struct device *dev,\n+\t\t\t\t\t enum device_trust_policy policy)\n+{\n+\tint ret = 0;\n+\n+\tif (policy \u003c DEVICE_TRUST_POLICY_DEFAULT ||\n+\t policy \u003e DEVICE_TRUST_POLICY_FULL)\n+\t\treturn -EINVAL;\n+\n+\tdevice_lock(dev);\n+\tif (dev-\u003ep-\u003edead) {\n+\t\tret = -ENODEV;\n+\t\tgoto unlock;\n+\t}\n+\tif (policy == dev-\u003etrust_policy)\n+\t\tgoto set_policy;\n+\n+\t/* A driver that claimed the device before registration was not admitted. */\n+\tif (READ_ONCE(dev-\u003edriver)) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\tswitch (dev-\u003ep-\u003etrust_state) {\n+\tcase DEVICE_TRUST_SELECTABLE:\n+\t\tif (policy == DEVICE_TRUST_POLICY_DISABLED)\n+\t\t\tdev-\u003ep-\u003etrust_state = DEVICE_TRUST_WAITING;\n+\t\tbreak;\n+\tcase DEVICE_TRUST_WAITING:\n+\t\tif (policy != DEVICE_TRUST_POLICY_ADVERSARY \u0026\u0026\n+\t\t policy != DEVICE_TRUST_POLICY_FULL) {\n+\t\t\tret = -EBUSY;\n+\t\t\tgoto unlock;\n+\t\t}\n+\t\tdev-\u003ep-\u003etrust_state = DEVICE_TRUST_AUTHORIZED;\n+\t\tbreak;\n+\tdefault:\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+set_policy:\n+\tWRITE_ONCE(dev-\u003etrust_policy, policy);\n+\tdev-\u003ep-\u003etrust_policy_override = true;\n+unlock:\n+\tdevice_unlock(dev);\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_set_policy);\n+\n+static ssize_t trust_policy_store(struct device *dev,\n+\t\t\t\t const struct device_attribute *attr,\n+\t\t\t\t const char *buf, size_t count)\n+{\n+\tint policy, ret;\n+\n+\tpolicy = sysfs_match_string(device_trust_policy_names, buf);\n+\tif (policy \u003c 0)\n+\t\treturn policy;\n+\n+\tret = device_trust_set_policy(dev, policy);\n+\n+\treturn ret ? ret : count;\n+}\n+static DEVICE_ATTR_RW(trust_policy);\n+\n+static struct attribute *device_trust_attrs[] = {\n+\t\u0026dev_attr_trust.attr,\n+\t\u0026dev_attr_trust_policy.attr,\n+\tNULL,\n+};\n+\n+const struct attribute_group device_trust_attr_group = {\n+\t.attrs = device_trust_attrs,\n+};\n+\n+static const char *device_trust_cmdline __ro_after_init;\n+static const char *device_trust_driver_cmdline __ro_after_init;\n+static enum device_trust_policy device_trust_default_policy __ro_after_init;\n+\n+static int device_trust_parse_policy(const char *options, const char *target_bus,\n+\t\t\t\t const char *target_name,\n+\t\t\t\t enum device_trust_policy *policy)\n+{\n+\tenum device_trust_policy selected = DEVICE_TRUST_POLICY_DEFAULT;\n+\tconst char *entry = options;\n+\tbool matched = false;\n+\n+\tif (!entry || !*entry)\n+\t\treturn -EINVAL;\n+\n+\tfor (;;) {\n+\t\tconst char *end = entry + strcspn(entry, \",\");\n+\t\tconst char *bus_end, *name, *level, *p;\n+\t\tunsigned int i;\n+\n+\t\tbus_end = memchr(entry, ':', end - entry);\n+\t\tif (!bus_end || bus_end == entry ||\n+\t\t memchr(entry, '=', bus_end - entry))\n+\t\t\treturn -EINVAL;\n+\t\tname = bus_end + 1;\n+\t\tlevel = memchr(name, '=', end - name);\n+\t\tif (!level || level == name || level + 1 == end)\n+\t\t\treturn -EINVAL;\n+\n+\t\tfor (p = entry; p \u003c end; p++)\n+\t\t\tif (isspace(*p) || iscntrl(*p) || *p == '\"')\n+\t\t\t\treturn -EINVAL;\n+\n+\t\tfor (i = 0; i \u003c ARRAY_SIZE(device_trust_policy_names); i++)\n+\t\t\tif (strlen(device_trust_policy_names[i]) == end - level - 1 \u0026\u0026\n+\t\t\t !strncmp(level + 1, device_trust_policy_names[i],\n+\t\t\t\t end - level - 1))\n+\t\t\t\tbreak;\n+\t\tif (i == ARRAY_SIZE(device_trust_policy_names))\n+\t\t\treturn -EINVAL;\n+\n+\t\tif (target_bus \u0026\u0026 target_name \u0026\u0026\n+\t\t strlen(target_bus) == bus_end - entry \u0026\u0026\n+\t\t !strncmp(target_bus, entry, bus_end - entry) \u0026\u0026\n+\t\t strlen(target_name) == level - name \u0026\u0026\n+\t\t !strncmp(target_name, name, level - name)) {\n+\t\t\tselected = i;\n+\t\t\tmatched = true;\n+\t\t}\n+\n+\t\tif (!*end)\n+\t\t\tbreak;\n+\t\tentry = end + 1;\n+\t}\n+\n+\tif (matched)\n+\t\t*policy = selected;\n+\treturn matched;\n+}\n+\n+VISIBLE_IF_KUNIT int device_trust_parse_cmdline(const char *options,\n+\t\t\t\t\t\tconst struct device *dev,\n+\t\t\t\t\t\tenum device_trust_policy *policy)\n+{\n+\treturn device_trust_parse_policy(options, dev \u0026\u0026 dev-\u003ebus ? dev-\u003ebus-\u003ename : NULL,\n+\t\t\t\t\t dev ? dev_name(dev) : NULL, policy);\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_parse_cmdline);\n+\n+VISIBLE_IF_KUNIT void device_trust_apply_driver_policy(struct device *dev,\n+\t\t\t\t\t\t const struct device_driver *drv,\n+\t\t\t\t\t\t const char *options)\n+{\n+\tenum device_trust_policy policy;\n+\n+\tdevice_lock_assert(dev);\n+\tif (!options || !drv || dev-\u003ep-\u003etrust_policy_override ||\n+\t (dev-\u003ep-\u003etrust_state != DEVICE_TRUST_SELECTABLE \u0026\u0026\n+\t dev-\u003ep-\u003etrust_state != DEVICE_TRUST_WAITING))\n+\t\treturn;\n+\n+\tif (device_trust_parse_policy(options, drv-\u003ebus ? drv-\u003ebus-\u003ename : NULL,\n+\t\t\t\t drv-\u003ename, \u0026policy) \u003c= 0)\n+\t\treturn;\n+\n+\tWRITE_ONCE(dev-\u003etrust_policy, policy);\n+\tdev-\u003ep-\u003etrust_state = policy == DEVICE_TRUST_POLICY_DISABLED ?\n+\t\tDEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;\n+\tdev-\u003ep-\u003etrust_policy_override = true;\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_driver_policy);\n+\n+static void device_trust_init_driver_policy(struct device *dev,\n+\t\t\t\t\t const struct device_driver *drv)\n+{\n+\tdevice_trust_apply_driver_policy(dev, drv, device_trust_driver_cmdline);\n+}\n+\n+/*\n+ * A matching driver rule is applied, together with its admission state, when\n+ * the device is registered, so only the device's own policy rejects a claim.\n+ */\n+VISIBLE_IF_KUNIT bool device_trust_claim_allowed(const struct device *dev,\n+\t\t\t\t\t\t const struct device_driver *drv,\n+\t\t\t\t\t\t const char *device_options,\n+\t\t\t\t\t\t const char *driver_options)\n+{\n+\tenum device_trust_policy policy = READ_ONCE(dev-\u003etrust_policy);\n+\tbool device_rule = device_options \u0026\u0026\n+\t\tdevice_trust_parse_cmdline(device_options, dev, \u0026policy) \u003e 0;\n+\n+\tif (!device_rule \u0026\u0026 driver_options \u0026\u0026 drv \u0026\u0026\n+\t device_trust_parse_policy(driver_options,\n+\t\t\t\t drv-\u003ebus ? drv-\u003ebus-\u003ename : NULL,\n+\t\t\t\t drv-\u003ename, \u0026policy) \u003e 0)\n+\t\treturn true;\n+\n+\treturn policy != DEVICE_TRUST_POLICY_DISABLED;\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_claim_allowed);\n+\n+/**\n+ * device_trust_may_claim - Test whether a driver may claim an unregistered device\n+ * @dev: device with its bus, name, and boot policy set\n+ * @drv: driver that claims the device\n+ *\n+ * A driver can claim a device before it is registered, which skips the bind\n+ * attempt that admits it. Reject a claim that the device's own policy forbids.\n+ *\n+ * Return: False if the policy of @dev rejects the claim.\n+ */\n+bool device_trust_may_claim(const struct device *dev,\n+\t\t\t const struct device_driver *drv)\n+{\n+\treturn device_trust_claim_allowed(dev, drv, device_trust_cmdline,\n+\t\t\t\t\t device_trust_driver_cmdline);\n+}\n+EXPORT_SYMBOL_GPL(device_trust_may_claim);\n+\n+static int __init device_trust_setup(char *str)\n+{\n+\tif (device_trust_parse_cmdline(str, NULL, NULL) \u003c 0) {\n+\t\tpr_warn(\"device_trust: ignoring invalid policy list '%s'\\n\", str);\n+\t\treturn 1;\n+\t}\n+\n+\t/* The command-line buffer survives init. */\n+\tdevice_trust_cmdline = str;\n+\treturn 1;\n+}\n+__setup(\"device_trust=\", device_trust_setup);\n+\n+static int __init device_trust_driver_setup(char *str)\n+{\n+\tif (device_trust_parse_policy(str, NULL, NULL, NULL) \u003c 0) {\n+\t\tpr_warn(\"device_trust: ignoring invalid driver policy list '%s'\\n\", str);\n+\t\treturn 1;\n+\t}\n+\n+\tdevice_trust_driver_cmdline = str;\n+\treturn 1;\n+}\n+__setup(\"device_trust.driver=\", device_trust_driver_setup);\n+\n+static int __init device_trust_default_setup(char *str)\n+{\n+\tint policy = match_string(device_trust_policy_names,\n+\t\t\t\t ARRAY_SIZE(device_trust_policy_names), str);\n+\n+\tif (policy \u003c 0) {\n+\t\tpr_warn(\"device_trust: ignoring invalid default policy '%s'\\n\", str);\n+\t\treturn 1;\n+\t}\n+\n+\tdevice_trust_default_policy = policy;\n+\treturn 1;\n+}\n+__setup(\"device_trust.default=\", device_trust_default_setup);\n+\n+VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,\n+\t\t\t\t\t\t enum device_trust_policy policy,\n+\t\t\t\t\t\t const char *options)\n+{\n+\tif (dev-\u003ep \u0026\u0026 dev-\u003ep-\u003etrust_initialized)\n+\t\treturn;\n+\n+\tif (policy != DEVICE_TRUST_POLICY_DEFAULT)\n+\t\tdev-\u003etrust_policy = policy;\n+\tif (options \u0026\u0026\n+\t device_trust_parse_cmdline(options, dev, \u0026dev-\u003etrust_policy) \u003e 0 \u0026\u0026\n+\t dev-\u003ep)\n+\t\tdev-\u003ep-\u003etrust_policy_override = true;\n+}\n+EXPORT_SYMBOL_IF_KUNIT(device_trust_apply_boot_policy);\n+\n+/**\n+ * device_trust_init - Apply boot policy before registration or bus policy use\n+ * @dev: unregistered device with its bus, name, and default policy set\n+ */\n+void device_trust_init(struct device *dev)\n+{\n+\tdevice_trust_apply_boot_policy(dev, device_trust_default_policy,\n+\t\t\t\t device_trust_cmdline);\n+}\n+EXPORT_SYMBOL_GPL(device_trust_init);\ndiff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c\nindex 92a78a7e603967..8fe6bd1bff9069 100644\n--- a/drivers/bluetooth/btusb_main.c\n+++ b/drivers/bluetooth/btusb_main.c\n@@ -2993,10 +2993,13 @@ static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)\n \treturn hci_recv_frame(hdev, skb);\n }\n \n+/* usb_lock_device_for_reset() gives up after one second of contention */\n+#define BTUSB_MTK_ISO_LOCK_TRIES\t5\n+\n static void btusb_mtk_claim_iso_intf(struct btusb_data *data)\n {\n \tstruct btmtk_data *btmtk_data;\n-\tint err;\n+\tint err, i;\n \n \tif (!data-\u003ehdev)\n \t\treturn;\n@@ -3011,22 +3014,37 @@ static void btusb_mtk_claim_iso_intf(struct btusb_data *data)\n \t}\n \n \t/*\n-\t * The function usb_driver_claim_interface() is documented to need\n-\t * locks held if it's not called from a probe routine. The code here\n-\t * is called from the hci_power_on workqueue, so grab the lock.\n+\t * Disconnect holds the device lock while draining this setup work, so\n+\t * the lock must be taken with disconnect-aware acquisition. Hold a PM\n+\t * reference so the device cannot suspend first. A slow interface\n+\t * registration can keep the lock past the one-second limit of a single\n+\t * attempt, so retry a few times, but not without bound: a teardown\n+\t * that holds the lock may be waiting for this work.\n \t */\n-\tdevice_lock(\u0026btmtk_data-\u003eisopkt_intf-\u003edev);\n-\terr = usb_driver_claim_interface(\u0026btusb_driver,\n-\t\t\t\t\t btmtk_data-\u003eisopkt_intf, data);\n-\tdevice_unlock(\u0026btmtk_data-\u003eisopkt_intf-\u003edev);\n+\terr = usb_autopm_get_interface(data-\u003eintf);\n+\tif (!err) {\n+\t\tfor (i = 0; i \u003c BTUSB_MTK_ISO_LOCK_TRIES; i++) {\n+\t\t\terr = usb_lock_device_for_reset(data-\u003eudev, data-\u003eintf);\n+\t\t\tif (err != -EBUSY)\n+\t\t\t\tbreak;\n+\t\t}\n+\t\tif (!err) {\n+\t\t\terr = usb_driver_claim_interface(\u0026btusb_driver,\n+\t\t\t\t\t\t\t btmtk_data-\u003eisopkt_intf,\n+\t\t\t\t\t\t\t data);\n+\t\t\tif (!err) {\n+\t\t\t\tinit_usb_anchor(\u0026btmtk_data-\u003eisopkt_anchor);\n+\t\t\t\tset_bit(BTMTK_ISOPKT_OVER_INTR,\n+\t\t\t\t\t\u0026btmtk_data-\u003eflags);\n+\t\t\t}\n+\t\t\tusb_unlock_device(data-\u003eudev);\n+\t\t}\n+\t\tusb_autopm_put_interface(data-\u003eintf);\n+\t}\n \tif (err \u003c 0) {\n \t\tbtmtk_data-\u003eisopkt_intf = NULL;\n \t\tbt_dev_err(data-\u003ehdev, \"Failed to claim iso interface: %d\", err);\n-\t\treturn;\n \t}\n-\n-\tset_bit(BTMTK_ISOPKT_OVER_INTR, \u0026btmtk_data-\u003eflags);\n-\tinit_usb_anchor(\u0026btmtk_data-\u003eisopkt_anchor);\n }\n \n static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)\ndiff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c\nindex a118a89a7779d6..0a030bf5226907 100644\n--- a/drivers/cdx/cdx.c\n+++ b/drivers/cdx/cdx.c\n@@ -367,7 +367,7 @@ static int cdx_dma_configure(struct device *dev)\n \t\t\tarch_teardown_dma_ops(dev);\n \t}\n \n-\treturn 0;\n+\treturn ret;\n }\n \n static void cdx_dma_cleanup(struct device *dev)\ndiff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c\nindex 9707b155bc9475..c9efec2dc55da5 100644\n--- a/drivers/input/gameport/gameport.c\n+++ b/drivers/input/gameport/gameport.c\n@@ -214,26 +214,7 @@ static void gameport_run_poll_handler(struct timer_list *t)\n \n static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)\n {\n-\tint error;\n-\n-\tgameport-\u003edev.driver = \u0026drv-\u003edriver;\n-\tif (drv-\u003econnect(gameport, drv)) {\n-\t\tgameport-\u003edev.driver = NULL;\n-\t\treturn -ENODEV;\n-\t}\n-\n-\terror = device_bind_driver(\u0026gameport-\u003edev);\n-\tif (error) {\n-\t\tdev_warn(\u0026gameport-\u003edev,\n-\t\t\t \"device_bind_driver() failed for %s (%s) and %s, error: %d\\n\",\n-\t\t\tgameport-\u003ephys, gameport-\u003ename,\n-\t\t\tdrv-\u003edescription, error);\n-\t\tdrv-\u003edisconnect(gameport);\n-\t\tgameport-\u003edev.driver = NULL;\n-\t\treturn error;\n-\t}\n-\n-\treturn 0;\n+\treturn device_driver_attach(\u0026drv-\u003edriver, \u0026gameport-\u003edev);\n }\n \n static void gameport_find_driver(struct gameport *gameport)\ndiff --git a/drivers/input/serio/serio.c b/drivers/input/serio/serio.c\nindex 54dd26249b027e..604440244c1ca7 100644\n--- a/drivers/input/serio/serio.c\n+++ b/drivers/input/serio/serio.c\n@@ -81,28 +81,10 @@ static int serio_match_port(const struct serio_device_id *ids, struct serio *ser\n \n static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)\n {\n-\tint error;\n-\n-\tif (serio_match_port(drv-\u003eid_table, serio)) {\n-\n-\t\tserio-\u003edev.driver = \u0026drv-\u003edriver;\n-\t\tif (serio_connect_driver(serio, drv)) {\n-\t\t\tserio-\u003edev.driver = NULL;\n-\t\t\treturn -ENODEV;\n-\t\t}\n+\tif (!serio_match_port(drv-\u003eid_table, serio))\n+\t\treturn 0;\n \n-\t\terror = device_bind_driver(\u0026serio-\u003edev);\n-\t\tif (error) {\n-\t\t\tdev_warn(\u0026serio-\u003edev,\n-\t\t\t\t \"device_bind_driver() failed for %s (%s) and %s, error: %d\\n\",\n-\t\t\t\t serio-\u003ephys, serio-\u003ename,\n-\t\t\t\t drv-\u003edescription, error);\n-\t\t\tserio_disconnect_driver(serio);\n-\t\t\tserio-\u003edev.driver = NULL;\n-\t\t\treturn error;\n-\t\t}\n-\t}\n-\treturn 0;\n+\treturn device_driver_attach(\u0026drv-\u003edriver, \u0026serio-\u003edev);\n }\n \n static void serio_find_driver(struct serio *serio)\ndiff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c\nindex 960e1dc05ec69b..e358bbb1f2baa0 100644\n--- a/drivers/iommu/amd/iommu.c\n+++ b/drivers/iommu/amd/iommu.c\n@@ -3401,8 +3401,7 @@ static int amd_iommu_def_domain_type(struct device *dev)\n \tif (!dev_is_pci(dev))\n \t\treturn 0;\n \n-\t/* Always use DMA domain for untrusted device */\n-\tif (to_pci_dev(dev)-\u003euntrusted)\n+\tif (pci_dev_default_is_restricted(to_pci_dev(dev)))\n \t\treturn IOMMU_DOMAIN_DMA;\n \n \t/* Apply device specific quirks */\ndiff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c\nindex 78742aeba932de..a48fb511bfe6fc 100644\n--- a/drivers/iommu/dma-iommu.c\n+++ b/drivers/iommu/dma-iommu.c\n@@ -589,16 +589,11 @@ static int iova_reserve_iommu_regions(struct device *dev,\n \treturn ret;\n }\n \n-static bool dev_is_untrusted(struct device *dev)\n-{\n-\treturn dev_is_pci(dev) \u0026\u0026 to_pci_dev(dev)-\u003euntrusted;\n-}\n-\n static bool dev_use_swiotlb(struct device *dev, size_t size,\n \t\t\t enum dma_data_direction dir)\n {\n \treturn IS_ENABLED(CONFIG_SWIOTLB) \u0026\u0026\n-\t\t(dev_is_untrusted(dev) ||\n+\t\t(device_is_adversarial(dev) ||\n \t\t dma_kmalloc_needs_bounce(dev, size, dir));\n }\n \n@@ -611,7 +606,7 @@ static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg,\n \tif (!IS_ENABLED(CONFIG_SWIOTLB))\n \t\treturn false;\n \n-\tif (dev_is_untrusted(dev))\n+\tif (device_is_adversarial(dev))\n \t\treturn true;\n \n \t/*\n@@ -1189,7 +1184,8 @@ static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,\n \t * swiotlb_tbl_map_single() has initialized the bounce buffer proper to\n \t * the contents of the original memory buffer.\n \t */\n-\tif (phys != (phys_addr_t)DMA_MAPPING_ERROR \u0026\u0026 dev_is_untrusted(dev)) {\n+\tif (phys != (phys_addr_t)DMA_MAPPING_ERROR \u0026\u0026\n+\t device_is_adversarial(dev)) {\n \t\tsize_t start, virt = (size_t)phys_to_virt(phys);\n \n \t\t/* Pre-padding */\n@@ -1790,7 +1786,7 @@ size_t iommu_dma_max_opt_mapping_size(void)\n \n size_t iommu_dma_max_mapping_size(struct device *dev)\n {\n-\tif (dev_is_untrusted(dev))\n+\tif (device_is_adversarial(dev))\n \t\treturn swiotlb_max_mapping_size(dev);\n \n \treturn SIZE_MAX;\ndiff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c\nindex 5a2e705a2f2e02..66942a91b992f0 100644\n--- a/drivers/iommu/intel/iommu.c\n+++ b/drivers/iommu/intel/iommu.c\n@@ -2624,8 +2624,8 @@ static void __init platform_optin_force_iommu(void)\n \t\treturn;\n \n \t/*\n-\t * If Intel-IOMMU is disabled by default, we will apply identity\n-\t * map for all devices except those marked as being untrusted.\n+\t * If Intel-IOMMU is disabled by default, an external device could\n+\t * otherwise receive an identity map despite its adversarial policy.\n \t */\n \tif (dmar_policy_off()) {\n \t\tpr_info(\"Intel-IOMMU force enabled due to platform opt in\\n\");\n@@ -3629,16 +3629,12 @@ static bool intel_iommu_is_attach_deferred(struct device *dev)\n \treturn translation_pre_enabled(info-\u003eiommu) \u0026\u0026 !info-\u003edomain;\n }\n \n-/*\n- * Check that the device does not live on an external facing PCI port that is\n- * marked as untrusted. Such devices should not be able to apply quirks and\n- * thus not be able to bypass the IOMMU restrictions.\n- */\n+/* Bypass quirks must not prevent later adversarial operation. */\n static bool risky_device(struct pci_dev *pdev)\n {\n-\tif (pdev-\u003euntrusted) {\n+\tif (pci_dev_default_is_restricted(pdev)) {\n \t\tpci_info(pdev,\n-\t\t\t \"Skipping IOMMU quirk for dev [%04X:%04X] on untrusted PCI link\\n\",\n+\t\t\t \"Skipping IOMMU quirk due to trust policy [%04X:%04X]\\n\",\n \t\t\t pdev-\u003evendor, pdev-\u003edevice);\n \t\tpci_info(pdev, \"Please check with your BIOS/Platform vendor about this\\n\");\n \t\treturn true;\n@@ -4124,9 +4120,6 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);\n \n static void quirk_iommu_rwbf(struct pci_dev *dev)\n {\n-\tif (risky_device(dev))\n-\t\treturn;\n-\n \t/*\n \t * Mobile 4 Series Chipset neglects to set RWBF capability,\n \t * but needs it. Same seems to hold for the desktop versions.\ndiff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c\nindex b486b8bbd1fcdc..8be2a246d49036 100644\n--- a/drivers/iommu/iommu.c\n+++ b/drivers/iommu/iommu.c\n@@ -1879,7 +1879,7 @@ static int iommu_get_def_domain_type(struct iommu_group *group,\n static int iommu_get_default_domain_type(struct iommu_group *group,\n \t\t\t\t\t int target_type)\n {\n-\tstruct device *untrusted = NULL;\n+\tstruct device *restricted = NULL;\n \tstruct group_device *gdev;\n \tint driver_type = 0;\n \n@@ -1901,14 +1901,15 @@ static int iommu_get_default_domain_type(struct iommu_group *group,\n \t\tdriver_type = iommu_get_def_domain_type(group, gdev-\u003edev,\n \t\t\t\t\t\t\tdriver_type);\n \n-\t\tif (dev_is_pci(gdev-\u003edev) \u0026\u0026 to_pci_dev(gdev-\u003edev)-\u003euntrusted) {\n+\t\tif (dev_is_pci(gdev-\u003edev) \u0026\u0026\n+\t\t pci_dev_default_is_restricted(to_pci_dev(gdev-\u003edev))) {\n \t\t\t/*\n-\t\t\t * No ARM32 using systems will set untrusted, it cannot\n-\t\t\t * work.\n+\t\t\t * No ARM32 using systems select a restricted trust\n+\t\t\t * policy; it cannot work.\n \t\t\t */\n \t\t\tif (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))\n \t\t\t\treturn -1;\n-\t\t\tuntrusted = gdev-\u003edev;\n+\t\t\trestricted = gdev-\u003edev;\n \t\t}\n \t}\n \n@@ -1924,11 +1925,11 @@ static int iommu_get_default_domain_type(struct iommu_group *group,\n \t\t\tdriver_type = IOMMU_DOMAIN_IDENTITY;\n \t}\n \n-\tif (untrusted) {\n+\tif (restricted) {\n \t\tif (driver_type \u0026\u0026 driver_type != IOMMU_DOMAIN_DMA) {\n \t\t\tdev_err_ratelimited(\n-\t\t\t\tuntrusted,\n-\t\t\t\t\"Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\\n\",\n+\t\t\t\trestricted,\n+\t\t\t\t\"Device trust policy restricts DMA, but driver is overriding group %u to %s, refusing to probe.\\n\",\n \t\t\t\tgroup-\u003eid, iommu_domain_type_str(driver_type));\n \t\t\treturn -1;\n \t\t}\ndiff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c\nindex 5d4508bcfbe991..8cc0de755bd082 100644\n--- a/drivers/net/phy/phy_device.c\n+++ b/drivers/net/phy/phy_device.c\n@@ -1912,7 +1912,9 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,\n \tstruct device *d = \u0026phydev-\u003emdio.dev;\n \tstruct module *ndev_owner = NULL;\n \tstruct module *drv_owner;\n+\tstruct device_driver *drv;\n \tint irq = phydev-\u003eirq;\n+\tbool bind_genphy;\n \tint err;\n \n \t/* For Ethernet device drivers that register their own MDIO bus, we\n@@ -1939,16 +1941,16 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,\n \t/* Assume that if there is no driver, that it doesn't\n \t * exist, and we should use the genphy driver.\n \t */\n-\tif (!d-\u003edriver) {\n+\tdrv = d-\u003edriver;\n+\tbind_genphy = !drv;\n+\tif (bind_genphy) {\n \t\tif (phydev-\u003eis_c45)\n-\t\t\td-\u003edriver = \u0026genphy_c45_driver.mdiodrv.driver;\n+\t\t\tdrv = \u0026genphy_c45_driver.mdiodrv.driver;\n \t\telse\n-\t\t\td-\u003edriver = \u0026genphy_driver.mdiodrv.driver;\n-\n-\t\tphydev-\u003eis_genphy_driven = 1;\n+\t\t\tdrv = \u0026genphy_driver.mdiodrv.driver;\n \t}\n \n-\tdrv_owner = d-\u003edriver-\u003eowner;\n+\tdrv_owner = drv-\u003eowner;\n \tif (!try_module_get(drv_owner)) {\n \t\tphydev_err(phydev, \"failed to get the device driver module\\n\");\n \t\terr = -EIO;\n@@ -1956,11 +1958,20 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,\n \t}\n \tphydev-\u003edrv_owner = drv_owner;\n \n-\tif (phydev-\u003eis_genphy_driven) {\n-\t\terr = d-\u003edriver-\u003eprobe(d);\n-\t\tif (err \u003e= 0)\n-\t\t\terr = device_bind_driver(d);\n-\n+\tif (bind_genphy) {\n+\t\t/*\n+\t\t * RTNL is held, and a concurrent bind of a specific driver may\n+\t\t * wait for it with the device lock held. Never wait for that\n+\t\t * lock, and let the caller retry.\n+\t\t */\n+\t\terr = device_driver_try_attach(drv, d);\n+\t\tif (err == -EBUSY) {\n+\t\t\t/* Another bind owns the PHY, so nothing was probed here. */\n+\t\t\tirq = phydev-\u003eirq;\n+\t\t\terr = -EPROBE_DEFER;\n+\t\t} else if (err == -EAGAIN) {\n+\t\t\terr = -EPROBE_DEFER;\n+\t\t}\n \t\tif (err)\n \t\t\tgoto error_module_put;\n \t}\n@@ -2071,8 +2082,6 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,\n \tmodule_put(phydev-\u003edrv_owner);\n \tphydev-\u003edrv_owner = NULL;\n \tphydev-\u003eirq = irq;\n-\tphydev-\u003eis_genphy_driven = 0;\n-\td-\u003edriver = NULL;\n error_put_device:\n \tput_device(d);\n \tif (ndev_owner != bus-\u003eowner)\n@@ -3813,6 +3822,8 @@ static int phy_probe(struct device *dev)\n \tint err = 0;\n \n \tphydev-\u003edrv = phydrv;\n+\tphydev-\u003eis_genphy_driven = phydrv == \u0026genphy_driver ||\n+\t\t\t\t phydrv == \u0026genphy_c45_driver;\n \n \t/* Disable the interrupt if the PHY doesn't support it\n \t * but the interrupt is still a valid one\n@@ -3949,6 +3960,7 @@ static int phy_probe(struct device *dev)\n out_reset:\n \t/* Re-assert the reset signal on error */\n \tphy_device_reset(phydev, 1);\n+\tphydev-\u003eis_genphy_driven = 0;\n \n \treturn err;\n }\n@@ -3977,6 +3989,7 @@ static int phy_remove(struct device *dev)\n \tphy_device_reset(phydev, 1);\n \n \tphydev-\u003edrv = NULL;\n+\tphydev-\u003eis_genphy_driven = 0;\n \n \treturn 0;\n }\ndiff --git a/drivers/net/wireless/virtual/mac80211_hwsim_main.c b/drivers/net/wireless/virtual/mac80211_hwsim_main.c\nindex e53714b81ec2c6..b796da3dd54b36 100644\n--- a/drivers/net/wireless/virtual/mac80211_hwsim_main.c\n+++ b/drivers/net/wireless/virtual/mac80211_hwsim_main.c\n@@ -5769,7 +5769,9 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,\n \t\tgoto failed_drvdata;\n \t}\n \tdata-\u003edev-\u003edriver = \u0026mac80211_hwsim_driver.driver;\n+\tdevice_lock(data-\u003edev);\n \terr = device_bind_driver(data-\u003edev);\n+\tdevice_unlock(data-\u003edev);\n \tif (err != 0) {\n \t\tpr_debug(\"mac80211_hwsim: device_bind_driver failed (%d)\\n\",\n \t\t err);\ndiff --git a/drivers/pci/ats.c b/drivers/pci/ats.c\nindex 9cb23780093dd3..c8c96acc263434 100644\n--- a/drivers/pci/ats.c\n+++ b/drivers/pci/ats.c\n@@ -21,7 +21,7 @@ void pci_ats_init(struct pci_dev *dev)\n {\n \tint pos;\n \n-\tif (pci_ats_disabled())\n+\tif (pci_ats_disabled() || pci_dev_default_is_restricted(dev))\n \t\treturn;\n \n \tpos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);\n@@ -40,7 +40,7 @@ void pci_ats_init(struct pci_dev *dev)\n */\n bool pci_ats_supported(struct pci_dev *dev)\n {\n-\tif (!dev-\u003eats_cap || dev-\u003euntrusted)\n+\tif (!dev-\u003eats_cap)\n \t\treturn false;\n \n \tif (dev-\u003eis_virtfn)\ndiff --git a/drivers/pci/pci.c b/drivers/pci/pci.c\nindex 7c6f2aba81443d..b08adba03f96b1 100644\n--- a/drivers/pci/pci.c\n+++ b/drivers/pci/pci.c\n@@ -1016,8 +1016,9 @@ static void pci_std_enable_acs(struct pci_dev *dev, struct pci_acs *caps)\n \t/* Upstream Forwarding */\n \tcaps-\u003ectrl |= (dev-\u003eacs_capabilities \u0026 PCI_ACS_UF);\n \n-\t/* Enable Translation Blocking for external devices and noats */\n-\tif (pci_ats_disabled() || dev-\u003eexternal_facing || dev-\u003euntrusted)\n+\t/* Enable Translation Blocking when ATS is unavailable or restricted. */\n+\tif (pci_ats_disabled() || dev-\u003eexternal_facing ||\n+\t !pci_dev_ats_permitted(dev))\n \t\tcaps-\u003ectrl |= (dev-\u003eacs_capabilities \u0026 PCI_ACS_TB);\n }\n \ndiff --git a/drivers/pci/probe.c b/drivers/pci/probe.c\nindex 9a178e0e5f7f46..139e6f7b4e0226 100644\n--- a/drivers/pci/probe.c\n+++ b/drivers/pci/probe.c\n@@ -1738,24 +1738,27 @@ static void set_pcie_cxl(struct pci_dev *dev)\n \n }\n \n-static void set_pcie_untrusted(struct pci_dev *dev)\n+static void pci_set_default_trust_policy(struct pci_dev *dev)\n {\n-\tstruct pci_dev *parent = pci_upstream_bridge(dev);\n+\tstruct pci_dev *parent = pci_physfn(dev);\n+\n+\tif (dev-\u003edev.trust_policy != DEVICE_TRUST_POLICY_DEFAULT)\n+\t\treturn;\n+\n+\tif (parent == dev)\n+\t\tparent = pci_upstream_bridge(dev);\n \n \tif (!parent)\n \t\treturn;\n-\t/*\n-\t * If the upstream bridge is untrusted we treat this device as\n-\t * untrusted as well.\n-\t */\n-\tif (parent-\u003euntrusted) {\n-\t\tdev-\u003euntrusted = true;\n+\n+\tif (device_policy_is_adversarial(\u0026parent-\u003edev)) {\n+\t\tdev-\u003edev.trust_policy = DEVICE_TRUST_POLICY_ADVERSARY;\n \t\treturn;\n \t}\n \n \tif (arch_pci_dev_is_removable(dev)) {\n-\t\tpci_dbg(dev, \"marking as untrusted\\n\");\n-\t\tdev-\u003euntrusted = true;\n+\t\tpci_dbg(dev, \"setting adversarial trust policy\\n\");\n+\t\tdev-\u003edev.trust_policy = DEVICE_TRUST_POLICY_ADVERSARY;\n \t}\n }\n \n@@ -2082,7 +2085,8 @@ int pci_setup_device(struct pci_dev *dev)\n \n \tset_pcie_cxl(dev);\n \n-\tset_pcie_untrusted(dev);\n+\tpci_set_default_trust_policy(dev);\n+\tdevice_trust_init(\u0026dev-\u003edev);\n \n \t/* \"Unknown power state\" */\n \tdev-\u003ecurrent_state = PCI_UNKNOWN;\ndiff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c\nindex 7f1f47ea2ee415..9ed3e7660bab25 100644\n--- a/drivers/pci/quirks.c\n+++ b/drivers/pci/quirks.c\n@@ -5525,8 +5525,8 @@ static void pci_quirk_enable_intel_rp_mpc_acs(struct pci_dev *dev)\n * Currently this quirk does the equivalent of\n * PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF\n *\n- * TODO: This quirk also needs to do equivalent of PCI_ACS_TB,\n- * if dev-\u003eexternal_facing || dev-\u003euntrusted\n+ * TODO: This quirk also needs to do the equivalent of PCI_ACS_TB when the\n+ * device is external-facing or ATS is restricted by trust policy.\n */\n static int pci_quirk_enable_intel_pch_acs(struct pci_dev *dev)\n {\n@@ -5567,7 +5567,8 @@ static int pci_quirk_enable_intel_spt_pch_acs(struct pci_dev *dev)\n \tctrl |= (cap \u0026 PCI_ACS_CR);\n \tctrl |= (cap \u0026 PCI_ACS_UF);\n \n-\tif (pci_ats_disabled() || dev-\u003eexternal_facing || dev-\u003euntrusted)\n+\tif (pci_ats_disabled() || dev-\u003eexternal_facing ||\n+\t !pci_dev_ats_permitted(dev))\n \t\tctrl |= (cap \u0026 PCI_ACS_TB);\n \n \tpci_write_config_dword(dev, pos + INTEL_SPT_ACS_CTRL, ctrl);\ndiff --git a/drivers/pnp/card.c b/drivers/pnp/card.c\nindex df5e3d3cbf6c05..9aaaa03d5ffa9b 100644\n--- a/drivers/pnp/card.c\n+++ b/drivers/pnp/card.c\n@@ -52,13 +52,17 @@ static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,\n \n static void card_remove(struct pnp_dev *dev)\n {\n-\tdev-\u003ecard_link = NULL;\n+\tif (device_is_bound(\u0026dev-\u003edev))\n+\t\tdev-\u003ecard_link = NULL;\n }\n \n static void card_remove_first(struct pnp_dev *dev)\n {\n \tstruct pnp_card_driver *drv = to_pnp_card_driver(dev-\u003edriver);\n \n+\t/* The card probe still owns its link until this device is bound. */\n+\tif (!device_is_bound(\u0026dev-\u003edev))\n+\t\treturn;\n \tif (!dev-\u003ecard || !drv)\n \t\treturn;\n \tif (drv-\u003eremove)\n@@ -331,16 +335,12 @@ struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,\n \n found:\n \tdev-\u003ecard_link = clink;\n-\tdev-\u003edev.driver = \u0026drv-\u003elink.driver;\n-\tif (pnp_bus_type.probe(\u0026dev-\u003edev))\n-\t\tgoto err_out;\n-\tif (device_bind_driver(\u0026dev-\u003edev))\n+\tif (device_driver_attach(\u0026drv-\u003elink.driver, \u0026dev-\u003edev))\n \t\tgoto err_out;\n \n \treturn dev;\n \n err_out:\n-\tdev-\u003edev.driver = NULL;\n \tdev-\u003ecard_link = NULL;\n \treturn NULL;\n }\ndiff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c\nindex d3a61ebc6afb00..4620667a11832d 100644\n--- a/drivers/usb/core/driver.c\n+++ b/drivers/usb/core/driver.c\n@@ -563,6 +563,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,\n \tif (!iface-\u003eauthorized)\n \t\treturn -ENODEV;\n \n+\t/* an unregistered interface is admitted later, so refuse what would fail */\n+\tif (!device_is_registered(dev) \u0026\u0026\n+\t !device_trust_may_claim(dev, \u0026driver-\u003edriver))\n+\t\treturn -EPERM;\n+\n \tdev-\u003edriver = \u0026driver-\u003edriver;\n \tusb_set_intfdata(iface, data);\n \tiface-\u003eneeds_binding = 0;\n@@ -584,8 +589,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,\n \t/* if interface was already added, bind now; else let\n \t * the future device_add() bind it, bypassing probe()\n \t */\n-\tif (device_is_registered(dev))\n+\tif (device_is_registered(dev)) {\n+\t\tdevice_lock(dev);\n \t\tretval = device_bind_driver(dev);\n+\t\tdevice_unlock(dev);\n+\t}\n \n \tif (retval) {\n \t\tdev-\u003edriver = NULL;\n@@ -649,6 +657,26 @@ void usb_driver_release_interface(struct usb_driver *driver,\n }\n EXPORT_SYMBOL_GPL(usb_driver_release_interface);\n \n+/*\n+ * The driver core refused to bind an interface that a sibling claimed before\n+ * it was registered, and dropped the claim. Let the claimant release the\n+ * interface instead of operating it with a bound interface but no driver.\n+ */\n+void usb_driver_release_refused_claim(struct usb_interface *iface,\n+\t\t\t\t struct device_driver *claimant)\n+{\n+\tstruct device *dev = \u0026iface-\u003edev;\n+\n+\tdevice_lock(dev);\n+\tif (!dev-\u003edriver \u0026\u0026 iface-\u003econdition == USB_INTERFACE_BOUND) {\n+\t\tdev-\u003edriver = claimant;\n+\t\tusb_unbind_interface(dev);\n+\t\tdev_set_drvdata(dev, NULL);\n+\t\tdev-\u003edriver = NULL;\n+\t}\n+\tdevice_unlock(dev);\n+}\n+\n /* returns 0 if no match, 1 if match */\n int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)\n {\ndiff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c\nindex a738d03c23eacc..2d75b85b2d139b 100644\n--- a/drivers/usb/core/message.c\n+++ b/drivers/usb/core/message.c\n@@ -2209,6 +2209,8 @@ int usb_set_configuration(struct usb_device *dev, int configuration)\n \t\tpm_runtime_no_callbacks(\u0026intf-\u003edev);\n \t\tdev_set_name(\u0026intf-\u003edev, \"%d-%s:%d.%d\", dev-\u003ebus-\u003ebusnum,\n \t\t\t\tdev-\u003edevpath, configuration, ifnum);\n+\t\t/* A sibling may claim the interface before it is registered */\n+\t\tdevice_trust_init(\u0026intf-\u003edev);\n \t\t/*\n \t\t * Make sure the interface does not outlive its parent (e.g.\n \t\t * as needed for usb_queue_reset_device() and\n@@ -2264,6 +2266,7 @@ int usb_set_configuration(struct usb_device *dev, int configuration)\n \t */\n \tfor (i = 0; i \u003c nintf; ++i) {\n \t\tstruct usb_interface *intf = cp-\u003einterface[i];\n+\t\tstruct device_driver *claimant;\n \n \t\tif (intf-\u003edev.of_node \u0026\u0026\n \t\t !of_device_is_available(intf-\u003edev.of_node)) {\n@@ -2277,7 +2280,14 @@ int usb_set_configuration(struct usb_device *dev, int configuration)\n \t\t\tdev_name(\u0026intf-\u003edev), configuration,\n \t\t\tintf-\u003ecur_altsetting-\u003edesc.bInterfaceNumber);\n \t\tdevice_enable_async_suspend(\u0026intf-\u003edev);\n+\t\tclaimant = intf-\u003edev.driver;\n+\t\tif (claimant \u0026\u0026 !try_module_get(claimant-\u003eowner))\n+\t\t\tclaimant = NULL;\n \t\tret = device_add(\u0026intf-\u003edev);\n+\t\tif (!ret \u0026\u0026 claimant \u0026\u0026 !intf-\u003edev.driver)\n+\t\t\tusb_driver_release_refused_claim(intf, claimant);\n+\t\tif (claimant)\n+\t\t\tmodule_put(claimant-\u003eowner);\n \t\tif (ret != 0) {\n \t\t\tdev_err(\u0026dev-\u003edev, \"device_add(%s) --\u003e %d\\n\",\n \t\t\t\tdev_name(\u0026intf-\u003edev), ret);\ndiff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h\nindex a9b37aeb515bef..293340a48b6c57 100644\n--- a/drivers/usb/core/usb.h\n+++ b/drivers/usb/core/usb.h\n@@ -49,6 +49,8 @@ extern int usb_set_isoch_delay(struct usb_device *dev);\n extern int usb_get_bos_descriptor(struct usb_device *dev);\n extern void usb_release_bos_descriptor(struct usb_device *dev);\n extern int usb_set_configuration(struct usb_device *dev, int configuration);\n+void usb_driver_release_refused_claim(struct usb_interface *iface,\n+\t\t\t\t struct device_driver *claimant);\n extern int usb_choose_configuration(struct usb_device *udev);\n extern int usb_generic_driver_probe(struct usb_device *udev);\n extern void usb_generic_driver_disconnect(struct usb_device *udev);\ndiff --git a/include/linux/device.h b/include/linux/device.h\nindex 429e39f647cf90..870ce149249053 100644\n--- a/include/linux/device.h\n+++ b/include/linux/device.h\n@@ -30,6 +30,7 @@\n #include \u003clinux/device/class.h\u003e\n #include \u003clinux/device/devres.h\u003e\n #include \u003clinux/device/driver.h\u003e\n+#include \u003clinux/device/trust.h\u003e\n #include \u003clinux/cleanup.h\u003e\n #include \u003casm/device.h\u003e\n \n@@ -695,6 +696,11 @@ enum struct_device_flags {\n * @removable: Whether the device can be removed from the system. This\n * should be set by the subsystem / bus driver that discovered\n * the device.\n+ * @trust_policy: Requested operating policy, resolved before driver probe.\n+ * @trust_level: Active trust level while a driver is bound, see\n+ *\t\tdevice_get_trust_level().\n+ * @trust_committed_level: Operating level retained across bind transactions,\n+ *\t\tsee device_get_committed_trust_level().\n * @flags:\tDEV_FLAG_XXX flags. Use atomic bitfield operations to modify.\n *\n * At the lowest level, every device in a Linux system is represented by an\n@@ -798,6 +804,9 @@ struct device {\n \tstruct device_physical_location *physical_location;\n \n \tenum device_removable\tremovable;\n+\tenum device_trust_policy trust_policy;\n+\tenum device_trust_level trust_level;\n+\tenum device_trust_level trust_committed_level;\n \n \tDECLARE_BITMAP(flags, DEV_FLAG_COUNT);\n };\n@@ -925,6 +934,11 @@ static inline bool device_iommu_mapped(struct device *dev)\n \treturn (dev-\u003eiommu_group != NULL);\n }\n \n+static inline bool device_policy_is_adversarial(const struct device *dev)\n+{\n+\treturn READ_ONCE(dev-\u003etrust_policy) == DEVICE_TRUST_POLICY_ADVERSARY;\n+}\n+\n /* Get the wakeup routines, which depend on struct device */\n #include \u003clinux/pm_wakeup.h\u003e\n \n@@ -1314,6 +1328,8 @@ static inline void *dev_get_platdata(const struct device *dev)\n */\n int __must_check device_driver_attach(const struct device_driver *drv,\n \t\t\t\t struct device *dev);\n+int __must_check device_driver_try_attach(const struct device_driver *drv,\n+\t\t\t\t\t struct device *dev);\n int __must_check device_bind_driver(struct device *dev);\n void device_release_driver(struct device *dev);\n int __must_check device_attach(struct device *dev);\ndiff --git a/include/linux/device/bus.h b/include/linux/device/bus.h\nindex 9ef7538a9a7f2e..ba2b5743a47cd5 100644\n--- a/include/linux/device/bus.h\n+++ b/include/linux/device/bus.h\n@@ -17,6 +17,7 @@\n #include \u003clinux/kobject.h\u003e\n #include \u003clinux/klist.h\u003e\n #include \u003clinux/pm.h\u003e\n+#include \u003clinux/device/trust.h\u003e\n \n struct device_driver;\n struct fwnode_handle;\n@@ -59,6 +60,11 @@ struct fwnode_handle;\n * @resume:\tCalled to bring a device on this bus out of sleep mode.\n * @num_vf:\tCalled to find out how many virtual functions a device on this\n *\t\tbus supports.\n+ * @trust_resolve: Resolve a requested trust policy to an active trust level\n+ *\t\timmediately before driver probe. The device lock is held and\n+ *\t\t@drv is the candidate driver, which dev-\u003edriver does not name\n+ *\t\tyet. Return 0 on success or a negative errno. This callback\n+ *\t\tmust not enable the device.\n * @dma_configure:\tCalled to setup DMA configuration on a device on\n *\t\t\tthis bus.\n * @dma_cleanup:\tCalled to cleanup DMA configuration on a device on\n@@ -102,6 +108,10 @@ struct bus_type {\n \n \tint (*num_vf)(struct device *dev);\n \n+\tint (*trust_resolve)(struct device *dev,\n+\t\t\t const struct device_driver *drv,\n+\t\t\t enum device_trust_policy policy,\n+\t\t\t enum device_trust_level *level);\n \tint (*dma_configure)(struct device *dev);\n \tvoid (*dma_cleanup)(struct device *dev);\n \ndiff --git a/include/linux/device/trust.h b/include/linux/device/trust.h\nnew file mode 100644\nindex 00000000000000..19052e3301729d\n--- /dev/null\n+++ b/include/linux/device/trust.h\n@@ -0,0 +1,48 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+#ifndef _DEVICE_TRUST_H_\n+#define _DEVICE_TRUST_H_\n+\n+#include \u003clinux/types.h\u003e\n+\n+struct device;\n+struct device_driver;\n+\n+/**\n+ * enum device_trust_level - active trust level for a device\n+ * @DEVICE_TRUST_DISABLED: The device cannot be operated by a driver\n+ * @DEVICE_TRUST_ADVERSARY: Operate the device with adversarial restrictions\n+ * @DEVICE_TRUST_FULL: Operate the device with the usual kernel privileges\n+ *\n+ * Active trust levels are discrete states, not an ordered privilege scale.\n+ */\n+enum device_trust_level {\n+\tDEVICE_TRUST_DISABLED,\n+\tDEVICE_TRUST_ADVERSARY,\n+\tDEVICE_TRUST_FULL,\n+};\n+\n+/**\n+ * enum device_trust_policy - requested device trust policy\n+ * @DEVICE_TRUST_POLICY_DEFAULT: Let the bus select the active trust level\n+ * @DEVICE_TRUST_POLICY_DISABLED: Request that the device remain disabled\n+ * @DEVICE_TRUST_POLICY_ADVERSARY: Request adversarial operation\n+ * @DEVICE_TRUST_POLICY_FULL: Request normal operation\n+ *\n+ * A requested policy is resolved to an active trust level before a driver is\n+ * probed.\n+ */\n+enum device_trust_policy {\n+\tDEVICE_TRUST_POLICY_DEFAULT,\n+\tDEVICE_TRUST_POLICY_DISABLED,\n+\tDEVICE_TRUST_POLICY_ADVERSARY,\n+\tDEVICE_TRUST_POLICY_FULL,\n+};\n+\n+void device_trust_init(struct device *dev);\n+bool device_trust_may_claim(const struct device *dev,\n+\t\t\t const struct device_driver *drv);\n+enum device_trust_level device_get_trust_level(const struct device *dev);\n+enum device_trust_level device_get_committed_trust_level(const struct device *dev);\n+bool device_is_adversarial(const struct device *dev);\n+\n+#endif /* _DEVICE_TRUST_H_ */\ndiff --git a/include/linux/pci.h b/include/linux/pci.h\nindex aa3765a20a96c1..087c63b177c854 100644\n--- a/include/linux/pci.h\n+++ b/include/linux/pci.h\n@@ -490,14 +490,6 @@ struct pci_dev {\n \tunsigned int\tshpc_managed:1;\t\t/* SHPC owned by shpchp */\n \tunsigned int\tis_thunderbolt:1;\t/* Thunderbolt controller */\n \tunsigned int\tis_cxl:1; /* Compute Express Link (CXL) */\n-\t/*\n-\t * Devices marked being untrusted are the ones that can potentially\n-\t * execute DMA attacks and similar. They are typically connected\n-\t * through external ports such as Thunderbolt but not limited to\n-\t * that. When an IOMMU is enabled they should be getting full\n-\t * mappings to make sure they cannot access arbitrary memory.\n-\t */\n-\tunsigned int\tuntrusted:1;\n \t/*\n \t * Info from the platform, e.g., ACPI or device tree, may mark a\n \t * device as \"external-facing\". An external-facing device is\n@@ -616,6 +608,28 @@ static inline struct pci_dev *to_pci_dev(const struct device *dev)\n {\n \treturn container_of(dev, struct pci_dev, dev);\n }\n+\n+static inline bool\n+pci_dev_default_is_restricted(const struct pci_dev *pdev)\n+{\n+\tenum device_trust_policy policy = READ_ONCE(pdev-\u003edev.trust_policy);\n+\n+\treturn policy == DEVICE_TRUST_POLICY_DISABLED ||\n+\t policy == DEVICE_TRUST_POLICY_ADVERSARY;\n+}\n+\n+static inline bool pci_dev_ats_permitted(const struct pci_dev *pdev)\n+{\n+\tenum device_trust_level level = device_get_trust_level(\u0026pdev-\u003edev);\n+\tenum device_trust_policy policy = READ_ONCE(pdev-\u003edev.trust_policy);\n+\n+\tif (level == DEVICE_TRUST_DISABLED)\n+\t\treturn policy != DEVICE_TRUST_POLICY_ADVERSARY \u0026\u0026\n+\t\t policy != DEVICE_TRUST_POLICY_DISABLED;\n+\n+\treturn level == DEVICE_TRUST_FULL;\n+}\n+\n #define for_each_pci_dev(d) while ((d = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, d)) != NULL)\n #define for_each_pci_dev_reverse(d) \\\n \twhile ((d = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID, d)) != NULL)\n"
} |
| 2/1 | 2026/11/08 04:35 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"SYSFS",
"PCI",
"USB",
"BT_HCIBTUSB",
"BT_HCIBTUSB_MTK",
"MAC80211_HWSIM"
],
"FocusSymbols": [
"trust_policy_store",
"trust_policy_show",
"trust_show",
"device_trust_set_policy",
"device_trust_prepare",
"device_trust_clear",
"device_trust_may_claim",
"device_driver_try_attach",
"drivers_probe_store",
"usb_driver_release_refused_claim",
"btusb_mtk_claim_iso_intf",
"mac80211_hwsim_new_radio"
],
"Reasoning": "This patch introduces the Device Trust subsystem to the Linux kernel driver core along with new sysfs attributes (/sys/devices/.../trust, /sys/devices/.../trust_policy) and changes to /sys/bus/.../drivers_probe. It modifies the core driver binding and probe flow (really_probe, device_bind_driver, device_driver_attach, and adds device_driver_try_attach) to enforce new invariants, state machine transitions, and device locking assertions. Furthermore, it modifies driver binding and interface claiming logic across USB, mac80211_hwsim, Bluetooth (btusb), network PHY, gameport, and serio subsystems. These changes touch reachable core code and user-accessible interfaces, making them prime candidates for fuzzing to uncover potential locking regressions, state machine violations, and race conditions.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 8c56033573e04f5245d4c1c50d24fa102cb32e43
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 04:35:17 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/ABI/testing/sysfs-bus-drivers-probe b/Documentation/ABI/testing/sysfs-bus-drivers-probe
new file mode 100644
index 00000000000000..851c65b9331ae2
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-drivers-probe
@@ -0,0 +1,13 @@
+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 device's
+ driver link reports the result.
+
+ 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/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
new file mode 100644
index 00000000000000..6f52102a1dfbf9
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-devices-trust
@@ -0,0 +1,61 @@
+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
+ "disabled", "adversary", and "full". Driver core resolves
+ the level before DMA configuration and probe, and retains it
+ through removal and DMA cleanup. Outside a bind transaction
+ the level is "disabled". This does not imply that policy is
+ writable or that the device has never operated.
+
+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.
+ 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.
+ Otherwise, device_trust.driver= can initialize policy on the
+ first binding attempt with a matching driver rule. A successful
+ policy write, including the existing value, suppresses this
+ deferred initialization. Driver rules do not change an existing
+ authorization or commitment and apply at most once per device.
+
+ The values are "default", "disabled", "adversary", and
+ "full". "default" lets the bus select the active trust level.
+ "disabled" prevents a functional driver from binding.
+ "adversary" requests operation with the restrictions that the
+ bus and DMA layers can enforce for a potentially malicious
+ device; it does not isolate DMA by itself. "full" requests
+ normal operation.
+
+ Before an operating level is committed, policy may change
+ freely until "disabled" is selected. Explicit "disabled"
+ blocks binding while allowing a later write of "adversary" or
+ "full". Repeated rejected probes preserve this opportunity.
+ Authorization immediately fixes the requested policy.
+ Returning to "default" or "disabled", or selecting another
+ mode, fails.
+
+ Successful resolution to an allowed level commits policy
+ and operating level before driver or DMA setup. Both remain
+ fixed for the device object's lifetime, including through
+ failed or deferred probes, unbind, reset, and driver reload.
+ Later binds must resolve to the same level, including when
+ the policy is "default". A resolver error or disabled result
+ before commitment leaves the policy's writability unchanged.
+
+ For a live device, writing the existing value succeeds
+ without changing state. Other forbidden writes fail with
+ EBUSY; invalid strings fail with EINVAL. Removed devices
+ reject writes with ENODEV. Writes do not trigger probing or
+ attestation.
+ Queued probes may proceed immediately after authorization,
+ so userspace must complete verification before granting it.
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd944..458f59a8af854c 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1259,6 +1259,70 @@ Kernel parameters
dell_smm_hwmon.fan_max=
[HW] Maximum configurable fan speed.
+ device_trust= [KNL]
+ Set requested device trust policy before driver probing,
+ including for drivers built into the kernel.
+ Format: <bus>:<device>=<policy>[,...]
+ The bus and device names must match exactly, as in
+ /sys/bus/<bus>/devices/<device>. The first colon separates
+ the bus name; further colons belong to the device name.
+ Policies are default, disabled, adversary, and full.
+ Entries override device_trust.default, device_trust.driver,
+ and bus-initialized policy, regardless of option order.
+ The last entry for a device wins. If specified more than
+ once, the last valid list replaces all earlier lists.
+ Malformed lists are warned about and ignored in full.
+ Names containing whitespace, control characters, commas,
+ equals signs, or double quotes cannot be specified.
+ Rules also apply to devices added after boot.
+ Explicit disabled blocks early binding but permits later
+ authorization by writing adversary or full to trust_policy.
+ Rejected disabled probes leave this opportunity intact.
+ Authorization fixes the policy before probing; adversary
+ or full at boot permits early probes without attestation.
+ Example:
+ device_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled
+ 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 | disabled | adversary | full }
+ default preserves bus-initialized policy (the default).
+ Other values override bus-initialized policy before
+ probing, including for built-in drivers.
+ The last valid value wins; invalid values are warned
+ about and ignored.
+ Without a matching driver rule, disabled prevents binding
+ until userspace authorizes each device by writing adversary
+ or full to trust_policy. Boot-critical devices need explicit
+ device or driver rules to operate before userspace. DMA
+ containment remains a bus and IOMMU responsibility.
+ 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>.
+ Policies are default, disabled, adversary, and full.
+ The name restrictions and repeated-list rules are the same
+ as device_trust=. Module aliases and wildcards are not
+ expanded. Rules remain available for hotplug and modules.
+ The first binding attempt with a matching rule applies it
+ once, before trust resolution and driver or DMA setup.
+ A per-device device_trust= entry or any successful write
+ to trust_policy, even of its current value, takes precedence.
+ Rules never replace an existing authorization or commitment.
+ A rejected disabled attempt leaves the device disabled until
+ userspace authorizes it, including across driver changes.
+ Driver rules apply after bus discovery and do not reconfigure
+ IOMMU domains. Adversary operation needs suitable existing
+ DMA configuration; see the device trust documentation.
+ Example: device_trust.driver=pci:e1000=disabled,pci:nvme=full
+
dfltcc= [HW,S390]
Format: { on | off | def_only | inf_only | always }
on: s390 zlib hardware support for compression on
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 00000000000000..1013784daeaca1
--- /dev/null
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -0,0 +1,294 @@
+.. 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``, ``disabled``,
+``adversary``, or ``full``. 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_DISABLED`` (``disabled``)
+ There is no active driver setup or binding. The requested policy decides
+ whether a later bind is allowed.
+
+``DEVICE_TRUST_ADVERSARY`` (``adversary``)
+ The kernel treats the device as potentially malicious. The bus and DMA
+ layers must enforce the restrictions needed to operate it.
+
+``DEVICE_TRUST_FULL`` (``full``)
+ 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 ``full`` resolve to
+``DEVICE_TRUST_FULL``, and ``adversary`` to ``DEVICE_TRUST_ADVERSARY``.
+Explicit ``disabled`` blocks binding. The bus remains responsible for
+enforcing the chosen level's restrictions.
+
+Admission and policy writes
+===========================
+
+Authorization is userspace's choice of adversary or full for a disabled
+device. Commitment happens when the kernel accepts a policy and operating
+level, before starting driver setup. Both limit later policy changes.
+
+The states below are internal; they 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 disabled enters WAITING.
+ * - WAITING
+ - Policy is disabled - no operating level is committed.
+ - Choose adversary or full to enter AUTHORIZED(P).
+ * - AUTHORIZED(P)
+ - Userspace has chosen adversary or full 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: disabled 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 full from WAITING locks that policy immediately.
+
+After boot initialization, the state transitions are::
+
+ SELECTABLE ----select disabled----> WAITING
+ | |
+ | authorize
+ | adversary/full
+ | |
+ | 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. Repeated rejected attempts leave the policy writable for later
+authorization.
+
+Active trust stays at L through DMA setup, probe, operation, removal, and
+``bus_type::dma_cleanup``. It returns to disabled 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.
+
+A driver can claim a USB interface before it is registered. The claim fails
+if the interface's own policy is disabled. A driver rule and other claims
+are admitted when the interface is registered, and a refusal then releases
+the claim. The policy of a claimed interface cannot be changed, and
+registration does not admit it while ``drivers_autoprobe`` is disabled.
+
+Boot policy
+===========
+
+Built-in drivers can probe before userspace starts. Use boot options to
+set their initial policy. All three options accept default, disabled,
+adversary, and full.
+
+``device_trust.default=`` sets policy for all devices. If omitted or set
+to default, it preserves bus policy. Other values replace bus policy,
+including topology and removability defaults. The last valid value wins;
+invalid values are reported and ignored.
+
+``device_trust=`` sets policy for individual devices::
+
+ device_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled
+
+Each entry is ``<bus>:<device>=<policy>``. 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 disabled. 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::
+
+ device_trust.driver=pci:e1000=disabled,pci:nvme=full
+
+Each entry is ``<bus>:<driver>=<policy>``. Use exact names from
+``/sys/bus/<bus>/drivers/<driver>``, not module names or aliases. The 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 disabled. It cannot override a per-device entry, any successful
+sysfs policy write, or an existing authorization or commitment. It applies
+only once per device object, even if resolution or setup fails.
+
+After a disabled driver rule applies, rejected probes and driver changes
+leave the device in WAITING until userspace authorizes it. Before any rule
+applies, a driver with no matching rule may still bind. Use global or
+per-device disabled to block every driver.
+
+For example, an initrd can hold all devices except one needed at boot::
+
+ device_trust.default=disabled device_trust=pci:0000:00:01.0=full
+
+Include exceptions for every device and supplier needed to reach the
+initrd and perform attestation. These exceptions allow operation before
+fresh initrd attestation. Unlisted devices stay disabled even if their
+bus would normally choose adversary or full.
+
+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. PCI does this during discovery, before header
+fixups, ACS setup, and IOMMU configuration. PCI applies topology and
+removability defaults only to default policy. A child's explicit boot
+entry overrides inherited policy. Driver rules wait until a candidate
+driver is known and cannot change these earlier discovery choices.
+
+All boot rules remain available for newly discovered devices. Reapplying
+initialization to an already registered device does not reset its state.
+These options are not runtime controls or proof of hardware identity.
+Disabled blocks early binding and allows later authorization. Adversary
+and full permit early binding without fresh initrd attestation.
+
+DMA containment
+===============
+
+Disabled blocks driver binding, not DMA that hardware starts on its own.
+The bus and IOMMU must provide containment while a device waits for
+authorization.
+
+For PCI devices whose policy is disabled or adversary at discovery, the
+IOMMU core selects a strict translated default domain for the whole group.
+AMD domain selection and Intel bypass checks apply the same restriction.
+This prepares a disabled device for later adversary authorization without
+granting permission to bind. Later full authorization may keep these
+restrictions. PCI does not record the ATS capability of such a device, even
+after a later full authorization, and sets ACS Translation Blocking for it
+where the path supports it.
+
+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.
+
+Binding does not check the IOMMU domain. Neither a driver rule nor a sysfs
+policy write reconfigures it, so a policy selected after discovery may
+bind with an identity or deferred-invalidation domain. Select the policy
+with ``device_trust=`` or ``device_trust.default=`` for it to apply at
+discovery. Outside PCI, discovery does not select a domain. Changing a
+group's domain type through sysfs is not checked against trust. A setup
+failure after commitment leaves the chosen mode fixed; it cannot fall back
+to full.
+
+Adversarial IOMMU mappings that are not aligned to IOMMU pages need an
+active SWIOTLB bounce-buffer pool. Low-memory x86 systems may need
+``swiotlb=force`` to keep this pool available. Without it, mappings can fail
+even after the driver binds.
+
+Drivers marked ``driver_managed_dma`` bypass the kernel DMA API's default
+domain handling and must provide their own DMA containment. Drivers that
+do not use DMA need no domain.
+
+Initrd attestation sequence
+===========================
+
+For a device that uses normal driver attachment, such as a PCI function:
+
+1. Boot with ``device_trust.default=disabled`` to hold all devices, or
+ ``device_trust=pci:0000:01:00.0=disabled`` to hold one. Add exceptions
+ needed to reach the initrd and provide DMA containment while waiting.
+ ``device_trust.driver=pci:e1000=disabled`` can instead hold a known
+ driver; global disabled also covers other drivers. Disabled 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 disabled
+ during verification and preparation. Leave it disabled 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 full
+ 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. An attachment deferred before the driver's probe
+ callback returns ``-EAGAIN``; retry later. A successful write does not
+ guarantee that a driver bound, so check the device's ``driver`` link.
+
+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 full 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 features such as peer-to-peer DMA,
+device memory, PASID/SVA, or paths that bypass the DMA API. General DMA
+containment belongs in bus and IOMMU code.
+
+Discovery code uses ``device_policy_is_adversarial()`` to check requested
+policy before binding or after unbind. This helper does not resolve
+default policy or report active trust.
+
+Code that needs the level fixed for the device's lifetime can use
+``device_get_committed_trust_level()``. It returns disabled 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, TDISP T=1/RUN, link
+encryption, private MMIO, verified evidence, or access to private versus
+shared memory. T=1 can use either adversary or full. In particular,
+``force_dma_unencrypted()`` describes DMA addressability and must not be
+derived from trust.
+
+Policy writes cannot revoke authorization or switch an operating device
+between adversary and full. 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 abeb4b36636b6c..af547cd13b2c41 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
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065de..a145aaaca2ef3f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8149,6 +8149,8 @@ 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/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/Makefile b/drivers/base/Makefile
index 435710f643a5c6..cb3bd5fa2287a0 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 866b2beb6baa28..5ff3217eb916ef 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,13 @@ struct driver_private {
};
#define to_driver(obj) container_of(obj, struct driver_private, kobj)
+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.
@@ -105,6 +113,9 @@ struct driver_private {
* dev_err_probe() for later retrieval via debugfs
* @device: pointer back to the struct device that this structure is
* associated with.
+ * @trust_state: policy admission state
+ * @trust_initialized: bus and boot policy have initialized 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.
@@ -121,6 +132,9 @@ struct device_private {
const struct device_driver *async_driver;
char *deferred_probe_reason;
struct device *device;
+ enum device_trust_state trust_state;
+ u8 trust_initialized:1;
+ u8 trust_policy_override:1;
u8 dead:1;
};
#define to_device_private_parent(obj) \
@@ -166,6 +180,29 @@ void bus_remove_driver(struct device_driver *drv);
void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
struct device *parent);
+extern const struct attribute_group device_trust_attr_group;
+
+#if IS_ENABLED(CONFIG_KUNIT)
+int device_trust_set_policy(struct device *dev, enum device_trust_policy policy);
+int device_trust_parse_cmdline(const char *options, const struct device *dev,
+ enum device_trust_policy *policy);
+void device_trust_apply_driver_policy(struct device *dev,
+ const struct device_driver *drv,
+ const char *options);
+bool device_trust_claim_allowed(const struct device *dev,
+ const struct device_driver *drv,
+ const char *device_options,
+ const char *driver_options);
+void device_trust_apply_boot_policy(struct device *dev,
+ enum device_trust_policy policy,
+ const char *options);
+#endif
+
+/* All active trust level transitions are serialized by the device lock. */
+void device_trust_init_state(struct device *dev);
+int device_trust_prepare(struct device *dev, const struct device_driver *drv);
+void device_trust_clear(struct device *dev);
+
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/bus.c b/drivers/base/bus.c
index 396097fdb31621..ecff32c3704b5f 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;
}
diff --git a/drivers/base/core.c b/drivers/base/core.c
index cbe05197d4a0bc..bd96d84c2ac2c6 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2989,10 +2989,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) {
@@ -3022,6 +3026,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:
@@ -3047,6 +3053,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)
@@ -3681,6 +3688,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);
@@ -4788,6 +4798,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/dd.c b/drivers/base/dd.c
index 52123251965de4..0d83f62195f699 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -545,13 +545,21 @@ int device_bind_driver(struct device *dev)
{
int ret;
+ if (dev->bus && dev->bus->trust_resolve)
+ return -EOPNOTSUPP;
+
+ ret = device_trust_prepare(dev, dev->driver);
+ if (ret)
+ return ret;
+
ret = driver_sysfs_add(dev);
if (!ret) {
device_links_force_bind(dev);
driver_bound(dev);
- }
- else
+ } else {
bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
+ device_trust_clear(dev);
+ }
return ret;
}
EXPORT_SYMBOL_GPL(device_bind_driver);
@@ -679,6 +687,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 */
@@ -745,6 +761,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;
}
@@ -771,6 +788,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;
}
@@ -1046,6 +1064,25 @@ static void __device_driver_lock(struct device *dev, struct device *parent)
device_lock(dev);
}
+/*
+ * __device_driver_trylock - __device_driver_lock() without sleeping
+ *
+ * Returns true if the locks were taken.
+ */
+static bool __device_driver_trylock(struct device *dev, struct device *parent)
+{
+ if (dev->type && dev->type->need_parent_lock && parent) {
+ if (!device_trylock(parent))
+ return false;
+ if (!device_trylock(dev)) {
+ device_unlock(parent);
+ return false;
+ }
+ return true;
+ }
+ return device_trylock(dev);
+}
+
/*
* __device_driver_unlock - release locks needed to manipulate dev->drv
* @dev: Device we will update driver info for
@@ -1195,19 +1232,17 @@ void device_initial_probe(struct device *dev)
subsys_put(sp);
}
-/**
- * device_driver_attach - attach a specific driver to a specific device
- * @drv: Driver to attach
- * @dev: Device to attach it to
- *
- * Manually attach driver to a device. Will acquire both @dev lock and
- * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
- */
-int device_driver_attach(const struct device_driver *drv, struct device *dev)
+static int __device_driver_attach(const struct device_driver *drv,
+ struct device *dev, bool trylock)
{
int ret;
- __device_driver_lock(dev, dev->parent);
+ if (trylock) {
+ if (!__device_driver_trylock(dev, dev->parent))
+ return -EBUSY;
+ } else {
+ __device_driver_lock(dev, dev->parent);
+ }
ret = __driver_probe_device(drv, dev);
__device_driver_unlock(dev, dev->parent);
@@ -1218,8 +1253,36 @@ int device_driver_attach(const struct device_driver *drv, struct device *dev)
return -EAGAIN;
return ret;
}
+
+/**
+ * device_driver_attach - attach a specific driver to a specific device
+ * @drv: Driver to attach
+ * @dev: Device to attach it to
+ *
+ * Manually attach driver to a device. Will acquire both @dev lock and
+ * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
+ */
+int device_driver_attach(const struct device_driver *drv, struct device *dev)
+{
+ return __device_driver_attach(drv, dev, false);
+}
EXPORT_SYMBOL_GPL(device_driver_attach);
+/**
+ * device_driver_try_attach - attach a specific driver without waiting for locks
+ * @drv: Driver to attach
+ * @dev: Device to attach it to
+ *
+ * Like device_driver_attach(), for callers that hold a lock which the holder
+ * of the @dev or @dev->parent lock may wait for. Returns -EBUSY, before any
+ * probing, if one of those locks is contended.
+ */
+int device_driver_try_attach(const struct device_driver *drv, struct device *dev)
+{
+ return __device_driver_attach(drv, dev, true);
+}
+EXPORT_SYMBOL_GPL(device_driver_try_attach);
+
static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
{
struct device *dev = _dev;
@@ -1360,6 +1423,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/test/Makefile b/drivers/base/test/Makefile
index 9ced7bbd569fc4..dfff5e1d83f7c8 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 00000000000000..590676648e4851
--- /dev/null
+++ b/drivers/base/test/device-trust-test.c
@@ -0,0 +1,1607 @@
+// 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/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#include "../base.h"
+
+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 remove_level;
+ enum device_trust_level cleanup_level;
+ const struct device_driver *resolve_driver;
+ const struct device_driver *reject_driver;
+ struct completion *released;
+
+ 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;
+ bool probe_adversarial;
+ bool device_initialized;
+ 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);
+
+ 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);
+
+ ctx->probe_count++;
+ ctx->probe_level = device_get_trust_level(dev);
+ ctx->probe_adversarial = device_is_adversarial(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);
+
+ 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 bool device_trust_test_plain_bus_registered;
+static bool device_trust_test_resolving_bus_registered;
+
+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_initialized)
+ return;
+
+ ctx->device_initialized = false;
+ if (ctx->device_registered) {
+ ctx->device_registered = false;
+ 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;
+
+ ctx->driver_registered = false;
+ driver_unregister(&ctx->drv);
+}
+
+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;
+ 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_FULL;
+ 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;
+ ctx->device_initialized = true;
+
+ ret = kunit_add_action_or_reset(test,
+ device_trust_test_device_unregister, ctx);
+ 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);
+
+ 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(struct kunit *test,
+ struct device_trust_test_context *ctx)
+{
+ int ret;
+
+ ctx->drv.name = test->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 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_FULL; 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+
+ 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_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_FALSE(test, ctx->probe_adversarial);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_FULL);
+ 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_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_FULL);
+ 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_DISABLED);
+}
+
+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_FULL;
+ 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_DISABLED);
+ 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_DISABLED);
+
+ 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_TRUE(test, dev_can_match(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_DISABLED), 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);
+ device_trust_apply_boot_policy(&ctx->dev, DEVICE_TRUST_POLICY_DISABLED, NULL);
+ 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+
+ 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);
+ device_trust_apply_boot_policy(&ctx->dev, DEVICE_TRUST_POLICY_DISABLED, NULL);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ level);
+ device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_disabled_bind_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,
+ DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_ADVERSARY);
+}
+
+static void device_trust_disabled_full_bind_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,
+ DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_FULL);
+}
+
+static void device_trust_global_disabled_adversary_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_ADVERSARY);
+}
+
+static void device_trust_global_disabled_full_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_FULL);
+}
+
+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_DISABLED);
+ 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_DISABLED);
+ 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;
+ 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_test_device_register_parent(test, &device_trust_test_plain_bus,
+ DEVICE_TRUST_POLICY_DEFAULT,
+ DEVICE_TRUST_POLICY_DEFAULT, parent);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+ ctx->drv.probe_type = PROBE_PREFER_ASYNCHRONOUS;
+
+ /* The queued probe cannot acquire the parent until policy is disabled. */
+ device_lock(parent);
+ ret = device_trust_test_driver_register(test, ctx);
+ if (!ret)
+ ret = device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_DISABLED);
+ device_unlock(parent);
+ 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_DISABLED);
+ 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_DISABLED);
+}
+
+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);
+ pdev->dev.trust_policy = DEVICE_TRUST_POLICY_DISABLED;
+ 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_DISABLED);
+ 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_explicit_policy_mismatch_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_FULL;
+
+ 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, -EINVAL);
+ KUNIT_EXPECT_GT(test, ctx->resolve_count, 0);
+ KUNIT_EXPECT_EQ(test, ctx->resolve_policy,
+ DEVICE_TRUST_POLICY_ADVERSARY);
+ 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_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_FULL), 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_FULL);
+}
+
+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_FULL), 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_DISABLED), 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 ?: -EINVAL);
+ 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_FULL);
+}
+
+static void device_trust_resolver_disabled_test(struct kunit *test)
+{
+ device_trust_resolver_retry_test(test, 0, DEVICE_TRUST_DISABLED);
+}
+
+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_DISABLED);
+ 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;
+
+ 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->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_DISABLED);
+ 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_DISABLED);
+ 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_DISABLED);
+ 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_FULL;
+ 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_DISABLED);
+ 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_DISABLED);
+}
+
+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_DISABLED);
+ 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_FULL), 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_FULL);
+
+ device_release_driver(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+}
+
+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_FULL);
+
+ device_release_driver(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 0);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+}
+
+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 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->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=full,usb:1-2=disabled,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_FULL },
+ { &device_trust_test_usb_bus, "1-2", DEVICE_TRUST_POLICY_DISABLED },
+ { &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_DISABLED;
+ 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=full",
+ "pc:0000:01:00.0=full",
+ "usb:0000:01:00.0=full",
+ "pci:0000:01:00.01=full",
+ "pci:0000:01:00=full",
+ "pci:0000:01:00.1=full",
+ };
+ 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=disabled,usb:1-1=full,pci:0000:01:00.0=full,usb:1-2=disabled";
+ static const char defaults[] =
+ "pci:0000:01:00.0=disabled,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_FULL);
+
+ 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=full", 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=disabled";
+ 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=full",
+ "pci:0000:01:00.0=full,",
+ "pci:0000:01:00.0=full,,usb:1-2=disabled",
+ ":0000:01:00.0=full",
+ "pci:=full",
+ "pci:0000:01:00.0=",
+ "pci=full",
+ "pci=bad:0000:01:00.0=full",
+ "pci:0000:01:00.0",
+ "pci:0000:01:00.0=unknown",
+ "pci:0000:01:00.0=full=disabled",
+ "pc i:0000:01:00.0=full",
+ "pci:0000:01:00.0 =full",
+ "pci:0000:01:00.0= full",
+ "pci:0000:01:00.0=full\n",
+ "pci:0000:01:00.0\t=full",
+ "pci:0000:01:00.0\001=full",
+ "p\"ci:0000:01:00.0=full",
+ "pci:0000:01:00.0=\"full\"",
+ "pci:0000:01:00.0=full,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_DISABLED] = "pci:0000:01:00.0=disabled",
+ [DEVICE_TRUST_POLICY_ADVERSARY] = "pci:0000:01:00.0=adversary",
+ [DEVICE_TRUST_POLICY_FULL] = "pci:0000:01:00.0=full",
+ };
+ 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_FULL; initial++) {
+ for (global = DEVICE_TRUST_POLICY_DEFAULT;
+ global <= DEVICE_TRUST_POLICY_FULL; 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=full");
+ 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_FULL);
+
+ device_trust_apply_boot_policy(dev, DEVICE_TRUST_POLICY_DISABLED,
+ "pci:0000:01:00.0=full");
+ KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_DISABLED);
+}
+
+static void device_trust_driver_precedence_test(struct kunit *test)
+{
+ static const char * const driver_rules[] = {
+ "pci:test=default", "pci:test=disabled",
+ "pci:test=adversary", "pci:test=full",
+ };
+ static const char * const device_rules[] = {
+ NULL, "pci:0000:01:00.0=default", "pci:0000:01:00.0=disabled",
+ "pci:0000:01:00.0=adversary", "pci:0000:01:00.0=full",
+ };
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ unsigned int initial, global, driver, device;
+ struct device *dev;
+ bool allowed, ok;
+
+ for (initial = DEVICE_TRUST_POLICY_DEFAULT;
+ initial <= DEVICE_TRUST_POLICY_FULL; initial++) {
+ for (global = DEVICE_TRUST_POLICY_DEFAULT;
+ global <= DEVICE_TRUST_POLICY_FULL; 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]);
+ /* The verdict for a claim before registration */
+ allowed = device_trust_claim_allowed(dev, &stub->drv,
+ device_rules[device],
+ driver_rules[driver]);
+ 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);
+ /* Driver rules are left to admission */
+ ok = !device || device - 1 != DEVICE_TRUST_POLICY_DISABLED;
+ KUNIT_EXPECT_EQ(test, allowed, ok);
+ }
+ }
+ }
+ }
+}
+
+static void device_trust_claim_allowed_test(struct kunit *test)
+{
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ struct device *dev;
+
+ dev = device_trust_test_pci_stub(stub, "0000:01:00.0", DEVICE_TRUST_POLICY_DISABLED);
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv, NULL, NULL));
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:other=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv,
+ "pci:0000:01:00.0=full", NULL));
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv,
+ "pci:0000:01:00.0=disabled",
+ "pci:test=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:test=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:test=disabled"));
+
+ dev = device_trust_test_pci_stub(stub, "0000:01:00.0", DEVICE_TRUST_POLICY_DEFAULT);
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL, NULL));
+}
+
+static void device_trust_driver_matching_test(struct kunit *test)
+{
+ static const char * const unmatched[] = {
+ NULL, "usb:test=disabled", "pc:test=disabled", "pcie:test=disabled",
+ "pci:tes=disabled", "pci:test1=disabled", "pci:te*=disabled",
+ "pci:test=disabled,pci:other=invalid", "pci:test=disabled,",
+ };
+ 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=disabled,pci:test=adversary");
+ device_unlock(dev);
+ KUNIT_EXPECT_EQ(test, 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_FULL; 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=disabled");
+ device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=full");
+ 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_DISABLED);
+
+ dev->p = &stub->private;
+ device_trust_init_state(dev);
+ KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_DEFAULT),
+ -EBUSY);
+ KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_FULL + 1),
+ -EINVAL);
+ device_lock(dev);
+ device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=full");
+ device_unlock(dev);
+ KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_FULL);
+}
+
+static void device_trust_driver_authorize_test(struct kunit *test)
+{
+ const struct device_driver drv = {
+ .bus = &device_trust_test_plain_bus,
+ .name = "test",
+ };
+ struct device_trust_test_context *ctx;
+ const struct device_driver other = {
+ .bus = &device_trust_test_plain_bus,
+ .name = "other",
+ };
+ 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=disabled");
+ 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=full");
+ device_unlock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_DISABLED);
+ 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=full");
+ 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=full");
+ 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)
+{
+ const struct device_driver drv = {
+ .bus = &device_trust_test_resolving_bus,
+ .name = "test",
+ };
+ struct device_trust_test_context *ctx;
+
+ ctx = device_trust_test_device_register(
+ test, &device_trust_test_resolving_bus, DEVICE_TRUST_POLICY_DISABLED);
+ 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=full");
+ 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_FULL);
+ 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=disabled");
+ device_unlock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_FULL);
+ 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_DISABLED), -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_DISABLED), 0);
+}
+
+static void device_trust_try_attach_test(struct kunit *test)
+{
+ struct device_trust_test_context *ctx;
+ struct device *parent;
+ bool parent_free;
+ 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_test_device_register_parent(test, &device_trust_test_plain_bus,
+ DEVICE_TRUST_POLICY_DEFAULT,
+ DEVICE_TRUST_POLICY_DEFAULT, parent);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+ ctx->match_ret = 0;
+ KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+
+ device_lock(parent);
+ KUNIT_EXPECT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), -EBUSY);
+ device_unlock(parent);
+
+ device_lock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), -EBUSY);
+ device_unlock(&ctx->dev);
+
+ parent_free = device_trylock(parent);
+ if (parent_free)
+ device_unlock(parent);
+ KUNIT_EXPECT_TRUE(test, parent_free);
+ KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+
+ KUNIT_ASSERT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), 0);
+ KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+}
+
+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 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_disabled_bind_test),
+ KUNIT_CASE(device_trust_disabled_full_bind_test),
+ KUNIT_CASE(device_trust_global_disabled_adversary_test),
+ KUNIT_CASE(device_trust_global_disabled_full_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_explicit_policy_mismatch_test),
+ KUNIT_CASE(device_trust_resolver_failure_test),
+ KUNIT_CASE(device_trust_resolver_disabled_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_claim_allowed_test),
+ KUNIT_CASE(device_trust_driver_matching_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_try_attach_test),
+ KUNIT_CASE(device_trust_scan_continues_test),
+ {}
+};
+
+/* The fixtures set their own policy, which a live boot default would replace. */
+static int device_trust_test_init(struct kunit *test)
+{
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ struct device *dev = device_trust_test_pci_stub(stub, "device-trust-test",
+ DEVICE_TRUST_POLICY_DEFAULT);
+
+ device_trust_init(dev);
+ if (dev->trust_policy != DEVICE_TRUST_POLICY_DEFAULT)
+ kunit_skip(test, "device_trust.default= replaces the fixture policy");
+
+ return 0;
+}
+
+static int device_trust_test_suite_init(struct kunit_suite *suite)
+{
+ int ret;
+
+ ret = bus_register(&device_trust_test_plain_bus);
+ if (ret)
+ return ret;
+ device_trust_test_plain_bus_registered = true;
+
+ ret = bus_register(&device_trust_test_resolving_bus);
+ if (ret) {
+ device_trust_test_plain_bus_registered = false;
+ bus_unregister(&device_trust_test_plain_bus);
+ return ret;
+ }
+ device_trust_test_resolving_bus_registered = true;
+
+ return 0;
+}
+
+static void device_trust_test_suite_exit(struct kunit_suite *suite)
+{
+ if (device_trust_test_resolving_bus_registered) {
+ device_trust_test_resolving_bus_registered = false;
+ bus_unregister(&device_trust_test_resolving_bus);
+ }
+ if (device_trust_test_plain_bus_registered) {
+ device_trust_test_plain_bus_registered = false;
+ 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,
+};
+
+kunit_test_suite(device_trust_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
new file mode 100644
index 00000000000000..b68644d197b6fc
--- /dev/null
+++ b/drivers/base/trust.c
@@ -0,0 +1,455 @@
+// 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"
+
+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->p->trust_initialized)
+ return;
+
+ dev->p->trust_state = dev->trust_policy == DEVICE_TRUST_POLICY_DISABLED ?
+ DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
+ dev->p->trust_initialized = true;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_init_state);
+
+static bool device_trust_policy_matches(enum device_trust_policy policy,
+ enum device_trust_level level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ return true;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ return level == DEVICE_TRUST_DISABLED;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ return level == DEVICE_TRUST_ADVERSARY;
+ case DEVICE_TRUST_POLICY_FULL:
+ return level == DEVICE_TRUST_FULL;
+ default:
+ return false;
+ }
+}
+
+static int device_trust_resolve_default(enum device_trust_policy policy,
+ enum device_trust_level *level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ case DEVICE_TRUST_POLICY_FULL:
+ *level = DEVICE_TRUST_FULL;
+ return 0;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ *level = DEVICE_TRUST_DISABLED;
+ return 0;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ *level = DEVICE_TRUST_ADVERSARY;
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+int device_trust_prepare(struct device *dev, const struct device_driver *drv)
+{
+ enum device_trust_policy policy;
+ enum device_trust_level level = DEVICE_TRUST_DISABLED;
+ int ret;
+
+ device_lock_assert(dev);
+
+ if (WARN_ON_ONCE(READ_ONCE(dev->trust_level) != DEVICE_TRUST_DISABLED))
+ return -EBUSY;
+ device_trust_init_driver_policy(dev, drv);
+ policy = dev->trust_policy;
+ if (policy == DEVICE_TRUST_POLICY_DISABLED)
+ return -EPERM;
+ if (dev->bus && dev->bus->trust_resolve)
+ ret = dev->bus->trust_resolve(dev, drv, policy, &level);
+ else
+ ret = device_trust_resolve_default(policy, &level);
+ if (ret)
+ return ret;
+
+ if (!device_trust_policy_matches(policy, level))
+ return -EINVAL;
+ if (level == DEVICE_TRUST_DISABLED)
+ return -EPERM;
+ if (level != DEVICE_TRUST_ADVERSARY && level != DEVICE_TRUST_FULL)
+ return -EINVAL;
+ 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;
+}
+
+void device_trust_clear(struct device *dev)
+{
+ device_lock_assert(dev);
+ WRITE_ONCE(dev->trust_level, DEVICE_TRUST_DISABLED);
+}
+
+/**
+ * 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_DISABLED 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_get_committed_trust_level - Return the lifetime trust commitment
+ * @dev: device to inspect
+ *
+ * Return: %DEVICE_TRUST_DISABLED 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
+ *
+ * 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_DISABLED] = "disabled",
+ [DEVICE_TRUST_POLICY_ADVERSARY] = "adversary",
+ [DEVICE_TRUST_POLICY_FULL] = "full",
+};
+
+static const char * const device_trust_level_names[] = {
+ [DEVICE_TRUST_DISABLED] = "disabled",
+ [DEVICE_TRUST_ADVERSARY] = "adversary",
+ [DEVICE_TRUST_FULL] = "full",
+};
+
+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)]);
+}
+
+VISIBLE_IF_KUNIT int device_trust_set_policy(struct device *dev,
+ enum device_trust_policy policy)
+{
+ int ret = 0;
+
+ if (policy < DEVICE_TRUST_POLICY_DEFAULT ||
+ policy > DEVICE_TRUST_POLICY_FULL)
+ return -EINVAL;
+
+ device_lock(dev);
+ if (dev->p->dead) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+ if (policy == dev->trust_policy)
+ goto set_policy;
+
+ /* A driver that claimed the device before registration was not admitted. */
+ if (READ_ONCE(dev->driver)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ switch (dev->p->trust_state) {
+ case DEVICE_TRUST_SELECTABLE:
+ if (policy == DEVICE_TRUST_POLICY_DISABLED)
+ dev->p->trust_state = DEVICE_TRUST_WAITING;
+ break;
+ case DEVICE_TRUST_WAITING:
+ if (policy != DEVICE_TRUST_POLICY_ADVERSARY &&
+ policy != DEVICE_TRUST_POLICY_FULL) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+ dev->p->trust_state = DEVICE_TRUST_AUTHORIZED;
+ break;
+ default:
+ ret = -EBUSY;
+ goto unlock;
+ }
+set_policy:
+ WRITE_ONCE(dev->trust_policy, policy);
+ dev->p->trust_policy_override = true;
+unlock:
+ device_unlock(dev);
+ return ret;
+}
+EXPORT_SYMBOL_IF_KUNIT(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;
+
+ 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 const char *device_trust_cmdline __ro_after_init;
+static const char *device_trust_driver_cmdline __ro_after_init;
+static enum device_trust_policy device_trust_default_policy __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 || !*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 = 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);
+
+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;
+
+ device_lock_assert(dev);
+ if (!options || !drv || dev->p->trust_policy_override ||
+ (dev->p->trust_state != DEVICE_TRUST_SELECTABLE &&
+ dev->p->trust_state != DEVICE_TRUST_WAITING))
+ return;
+
+ if (device_trust_parse_policy(options, drv->bus ? drv->bus->name : NULL,
+ drv->name, &policy) <= 0)
+ return;
+
+ WRITE_ONCE(dev->trust_policy, policy);
+ dev->p->trust_state = policy == DEVICE_TRUST_POLICY_DISABLED ?
+ 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);
+}
+
+/*
+ * A matching driver rule is applied, together with its admission state, when
+ * the device is registered, so only the device's own policy rejects a claim.
+ */
+VISIBLE_IF_KUNIT bool device_trust_claim_allowed(const struct device *dev,
+ const struct device_driver *drv,
+ const char *device_options,
+ const char *driver_options)
+{
+ enum device_trust_policy policy = READ_ONCE(dev->trust_policy);
+ bool device_rule = device_options &&
+ device_trust_parse_cmdline(device_options, dev, &policy) > 0;
+
+ if (!device_rule && driver_options && drv &&
+ device_trust_parse_policy(driver_options,
+ drv->bus ? drv->bus->name : NULL,
+ drv->name, &policy) > 0)
+ return true;
+
+ return policy != DEVICE_TRUST_POLICY_DISABLED;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_claim_allowed);
+
+/**
+ * device_trust_may_claim - Test whether a driver may claim an unregistered device
+ * @dev: device with its bus, name, and boot policy set
+ * @drv: driver that claims the device
+ *
+ * A driver can claim a device before it is registered, which skips the bind
+ * attempt that admits it. Reject a claim that the device's own policy forbids.
+ *
+ * Return: False if the policy of @dev rejects the claim.
+ */
+bool device_trust_may_claim(const struct device *dev,
+ const struct device_driver *drv)
+{
+ return device_trust_claim_allowed(dev, drv, device_trust_cmdline,
+ device_trust_driver_cmdline);
+}
+EXPORT_SYMBOL_GPL(device_trust_may_claim);
+
+static int __init device_trust_setup(char *str)
+{
+ 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_driver_setup(char *str)
+{
+ if (device_trust_parse_policy(str, NULL, 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,
+ ARRAY_SIZE(device_trust_policy_names), str);
+
+ 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);
+
+VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,
+ enum device_trust_policy policy,
+ const char *options)
+{
+ if (dev->p && dev->p->trust_initialized)
+ return;
+
+ if (policy != DEVICE_TRUST_POLICY_DEFAULT)
+ dev->trust_policy = 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);
+
+/**
+ * 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)
+{
+ device_trust_apply_boot_policy(dev, device_trust_default_policy,
+ device_trust_cmdline);
+}
+EXPORT_SYMBOL_GPL(device_trust_init);
diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 92a78a7e603967..8fe6bd1bff9069 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2993,10 +2993,13 @@ static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
return hci_recv_frame(hdev, skb);
}
+/* usb_lock_device_for_reset() gives up after one second of contention */
+#define BTUSB_MTK_ISO_LOCK_TRIES 5
+
static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
{
struct btmtk_data *btmtk_data;
- int err;
+ int err, i;
if (!data->hdev)
return;
@@ -3011,22 +3014,37 @@ static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
}
/*
- * 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.
+ * Disconnect holds the device lock while draining this setup work, so
+ * the lock must be taken with disconnect-aware acquisition. Hold a PM
+ * reference so the device cannot suspend first. A slow interface
+ * registration can keep the lock past the one-second limit of a single
+ * attempt, so retry a few times, but not without bound: a teardown
+ * that holds the lock may be waiting for this work.
*/
- 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);
+ err = usb_autopm_get_interface(data->intf);
+ if (!err) {
+ 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) {
+ err = usb_driver_claim_interface(&btusb_driver,
+ btmtk_data->isopkt_intf,
+ data);
+ if (!err) {
+ 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);
+ }
if (err < 0) {
btmtk_data->isopkt_intf = NULL;
bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
- return;
}
-
- set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
- init_usb_anchor(&btmtk_data->isopkt_anchor);
}
static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
diff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c
index a118a89a7779d6..0a030bf5226907 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)
diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
index 9707b155bc9475..c9efec2dc55da5 100644
--- a/drivers/input/gameport/gameport.c
+++ b/drivers/input/gameport/gameport.c
@@ -214,26 +214,7 @@ static void gameport_run_poll_handler(struct timer_list *t)
static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
{
- int error;
-
- gameport->dev.driver = &drv->driver;
- if (drv->connect(gameport, drv)) {
- gameport->dev.driver = NULL;
- return -ENODEV;
- }
-
- 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;
+ return device_driver_attach(&drv->driver, &gameport->dev);
}
static void gameport_find_driver(struct gameport *gameport)
diff --git a/drivers/input/serio/serio.c b/drivers/input/serio/serio.c
index 54dd26249b027e..604440244c1ca7 100644
--- a/drivers/input/serio/serio.c
+++ b/drivers/input/serio/serio.c
@@ -81,28 +81,10 @@ static int serio_match_port(const struct serio_device_id *ids, struct serio *ser
static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
{
- int error;
-
- if (serio_match_port(drv->id_table, serio)) {
-
- serio->dev.driver = &drv->driver;
- if (serio_connect_driver(serio, drv)) {
- serio->dev.driver = NULL;
- return -ENODEV;
- }
+ if (!serio_match_port(drv->id_table, serio))
+ return 0;
- 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;
+ return device_driver_attach(&drv->driver, &serio->dev);
}
static void serio_find_driver(struct serio *serio)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 960e1dc05ec69b..e358bbb1f2baa0 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 78742aeba932de..a48fb511bfe6fc 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 5a2e705a2f2e02..66942a91b992f0 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;
@@ -4124,9 +4120,6 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
static void quirk_iommu_rwbf(struct pci_dev *dev)
{
- if (risky_device(dev))
- return;
-
/*
* Mobile 4 Series Chipset neglects to set RWBF capability,
* but needs it. Same seems to hold for the desktop versions.
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index b486b8bbd1fcdc..8be2a246d49036 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1879,7 +1879,7 @@ static int iommu_get_def_domain_type(struct iommu_group *group,
static int iommu_get_default_domain_type(struct iommu_group *group,
int target_type)
{
- struct device *untrusted = NULL;
+ struct device *restricted = NULL;
struct group_device *gdev;
int driver_type = 0;
@@ -1901,14 +1901,15 @@ 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;
- untrusted = gdev->dev;
+ restricted = gdev->dev;
}
}
@@ -1924,11 +1925,11 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
driver_type = IOMMU_DOMAIN_IDENTITY;
}
- if (untrusted) {
+ if (restricted) {
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",
+ restricted,
+ "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/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 5d4508bcfbe991..8cc0de755bd082 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1912,7 +1912,9 @@ 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;
int irq = phydev->irq;
+ bool bind_genphy;
int err;
/* For Ethernet device drivers that register their own MDIO bus, we
@@ -1939,16 +1941,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 +1958,20 @@ 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) {
+ /*
+ * RTNL is held, and a concurrent bind of a specific driver may
+ * wait for it with the device lock held. Never wait for that
+ * lock, and let the caller retry.
+ */
+ err = device_driver_try_attach(drv, d);
+ if (err == -EBUSY) {
+ /* Another bind owns the PHY, so nothing was probed here. */
+ irq = phydev->irq;
+ err = -EPROBE_DEFER;
+ } else if (err == -EAGAIN) {
+ err = -EPROBE_DEFER;
+ }
if (err)
goto error_module_put;
}
@@ -2071,8 +2082,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 +3822,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 +3960,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 +3989,7 @@ static int phy_remove(struct device *dev)
phy_device_reset(phydev, 1);
phydev->drv = NULL;
+ phydev->is_genphy_driven = 0;
return 0;
}
diff --git a/drivers/net/wireless/virtual/mac80211_hwsim_main.c b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
index e53714b81ec2c6..b796da3dd54b36 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);
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 9cb23780093dd3..c8c96acc263434 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 7c6f2aba81443d..b08adba03f96b1 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_ats_permitted(dev))
caps->ctrl |= (dev->acs_capabilities & PCI_ACS_TB);
}
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f46..139e6f7b4e0226 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1738,24 +1738,27 @@ 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);
+ struct pci_dev *parent = pci_physfn(dev);
+
+ if (dev->dev.trust_policy != DEVICE_TRUST_POLICY_DEFAULT)
+ return;
+
+ if (parent == 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 +2085,8 @@ int pci_setup_device(struct pci_dev *dev)
set_pcie_cxl(dev);
- set_pcie_untrusted(dev);
+ pci_set_default_trust_policy(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 7f1f47ea2ee415..9ed3e7660bab25 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_ats_permitted(dev))
ctrl |= (cap & PCI_ACS_TB);
pci_write_config_dword(dev, pos + INTEL_SPT_ACS_CTRL, ctrl);
diff --git a/drivers/pnp/card.c b/drivers/pnp/card.c
index df5e3d3cbf6c05..9aaaa03d5ffa9b 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;
}
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d3a61ebc6afb00..4620667a11832d 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -563,6 +563,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,
if (!iface->authorized)
return -ENODEV;
+ /* an unregistered interface is admitted later, so refuse what would fail */
+ if (!device_is_registered(dev) &&
+ !device_trust_may_claim(dev, &driver->driver))
+ return -EPERM;
+
dev->driver = &driver->driver;
usb_set_intfdata(iface, data);
iface->needs_binding = 0;
@@ -584,8 +589,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,
/* if interface was already added, bind now; else let
* the future device_add() bind it, bypassing probe()
*/
- if (device_is_registered(dev))
+ if (device_is_registered(dev)) {
+ device_lock(dev);
retval = device_bind_driver(dev);
+ device_unlock(dev);
+ }
if (retval) {
dev->driver = NULL;
@@ -649,6 +657,26 @@ void usb_driver_release_interface(struct usb_driver *driver,
}
EXPORT_SYMBOL_GPL(usb_driver_release_interface);
+/*
+ * The driver core refused to bind an interface that a sibling claimed before
+ * it was registered, and dropped the claim. Let the claimant release the
+ * interface instead of operating it with a bound interface but no driver.
+ */
+void usb_driver_release_refused_claim(struct usb_interface *iface,
+ struct device_driver *claimant)
+{
+ struct device *dev = &iface->dev;
+
+ device_lock(dev);
+ if (!dev->driver && iface->condition == USB_INTERFACE_BOUND) {
+ dev->driver = claimant;
+ usb_unbind_interface(dev);
+ dev_set_drvdata(dev, NULL);
+ dev->driver = NULL;
+ }
+ device_unlock(dev);
+}
+
/* 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/message.c b/drivers/usb/core/message.c
index a738d03c23eacc..2d75b85b2d139b 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -2209,6 +2209,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
@@ -2264,6 +2266,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;
if (intf->dev.of_node &&
!of_device_is_available(intf->dev.of_node)) {
@@ -2277,7 +2280,14 @@ 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);
+ claimant = intf->dev.driver;
+ if (claimant && !try_module_get(claimant->owner))
+ claimant = NULL;
ret = device_add(&intf->dev);
+ if (!ret && claimant && !intf->dev.driver)
+ usb_driver_release_refused_claim(intf, claimant);
+ if (claimant)
+ module_put(claimant->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 a9b37aeb515bef..293340a48b6c57 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -49,6 +49,8 @@ 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_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);
diff --git a/include/linux/device.h b/include/linux/device.h
index 429e39f647cf90..870ce149249053 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,11 @@ 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 while a driver is bound, see
+ * device_get_trust_level().
+ * @trust_committed_level: Operating level retained across bind transactions,
+ * see device_get_committed_trust_level().
* @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 +804,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);
};
@@ -925,6 +934,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;
+}
+
/* Get the wakeup routines, which depend on struct device */
#include <linux/pm_wakeup.h>
@@ -1314,6 +1328,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_try_attach(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);
diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h
index 9ef7538a9a7f2e..ba2b5743a47cd5 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 00000000000000..19052e3301729d
--- /dev/null
+++ b/include/linux/device/trust.h
@@ -0,0 +1,48 @@
+/* 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_DISABLED: The device cannot be operated by a driver
+ * @DEVICE_TRUST_ADVERSARY: Operate the device with adversarial restrictions
+ * @DEVICE_TRUST_FULL: Operate the device with the usual kernel privileges
+ *
+ * Active trust levels are discrete states, not an ordered privilege scale.
+ */
+enum device_trust_level {
+ DEVICE_TRUST_DISABLED,
+ DEVICE_TRUST_ADVERSARY,
+ DEVICE_TRUST_FULL,
+};
+
+/**
+ * enum device_trust_policy - requested device trust policy
+ * @DEVICE_TRUST_POLICY_DEFAULT: Let the bus select the active trust level
+ * @DEVICE_TRUST_POLICY_DISABLED: Request that the device remain disabled
+ * @DEVICE_TRUST_POLICY_ADVERSARY: Request adversarial operation
+ * @DEVICE_TRUST_POLICY_FULL: 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_DISABLED,
+ DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_POLICY_FULL,
+};
+
+void device_trust_init(struct device *dev);
+bool device_trust_may_claim(const struct device *dev,
+ const struct device_driver *drv);
+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_ */
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96c1..087c63b177c854 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
@@ -616,6 +608,28 @@ 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_DISABLED ||
+ policy == DEVICE_TRUST_POLICY_ADVERSARY;
+}
+
+static inline bool pci_dev_ats_permitted(const struct pci_dev *pdev)
+{
+ enum device_trust_level level = device_get_trust_level(&pdev->dev);
+ enum device_trust_policy policy = READ_ONCE(pdev->dev.trust_policy);
+
+ if (level == DEVICE_TRUST_DISABLED)
+ return policy != DEVICE_TRUST_POLICY_ADVERSARY &&
+ policy != DEVICE_TRUST_POLICY_DISABLED;
+
+ return level == DEVICE_TRUST_FULL;
+}
+
#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)
|
| 3/2 | 2026/11/08 04:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52712 output: 22 thoughts: 2719 Thoughts: **Analysis of the "Device Trust" Patch Series for Syzkaller Fuzzing** Okay, let's break this down. This patch series... it's substantial. It introduces a whole new "Device Trust" mechanism across the driver core and several subsystems. My initial assessment is that it *absolutely* warrants fuzzing with syzkaller. First, I see the scope: it's not just doc updates; this is a deep dive into the driver model. There are significant changes to core data structures (`struct device_private`, `enum device_trust_state`) and helper functions. We have a *new* file, `drivers/base/trust.c`, which is the heart of this feature. It defines trust levels, policy management, and interaction with sysfs. The core driver model changes are particularly interesting, especially the modifications to `drivers_probe_store`, and the introduction of new attributes (`trust`, `trust_policy`) in sysfs, which are user-accessible. The way `device_bind_driver` is updated with `trust_resolve` checks and `device_trust_prepare` is critical, and the addition of `device_trust_clear` upon error is worth noting. The new KUnit tests are good, but fuzzing will hit much broader scenarios. Then there are changes in other drivers. Bluetooth, CDX, gameport, serio, IOMMU, network PHY, and even the wireless mac80211_hwsim subsystem are all touched. The fact that the locking around `device_bind_driver` is changed in `mac80211_hwsim_main.c` is significant. The USB core changes are also critical: with new hooks like `device_trust_may_claim`, and the introduction of `usb_driver_release_refused_claim`, it's an expanded attack surface for fuzzing. Also the `usb_set_configuration` calls `device_trust_init(&intf->dev)` - very promising for testing. The potential for reaching this code via a standard QEMU/syzkaller environment is high. Driver core is easily testable, and USB and network devices are commonly emulated, which syzkaller heavily exercises! The *critical* thing is the introduction of this Device Trust mechanism. I see sysfs interfaces (`trust`, `trust_policy`, `drivers_probe`), refactored driver binding paths, and locking changes. This introduces an entirely new attack surface. In particular the `device_lock_assert(dev)` within `device_trust_prepare` is particularly important: This change can lead to new lock-related bugs! The `trust_policy` sysfs file that's writeable by userspace is a huge entry point. `/sys/bus/.../drivers_probe` too. Now, as an expert, I know the criteria for syzkaller: we've got to focus the fuzzing effort. "WorthFuzzing" is *definitely* true. We need to focus on specific feature logic. The symbols from the `trust.c`, `dd.c`, `bus.c`, USB, and networking code are key. Let's see... I've got to carefully select these functions for FocusSymbols, and avoid the ubiquitous lifecyle hot-paths. Here's the refined list: * `device_trust_init_state` * `device_trust_prepare` * `device_trust_clear` * `device_get_trust_level` * `device_get_committed_trust_level` * `device_is_adversarial` * `device_trust_set_policy` * `drivers_probe_store` * `device_bind_driver` * `device_driver_attach` * `device_driver_try_attach` * `really_probe` * `usb_driver_claim_interface` * `usb_driver_release_refused_claim` * `btusb_mtk_claim_iso_intf` * `mac80211_hwsim_new_radio` * `phy_attach_direct` * `device_trust_parse_cmdline` * `device_trust_apply_driver_policy` * `device_trust_claim_allowed` * `device_trust_may_claim` * `device_trust_apply_boot_policy` * `device_trust_init` These are feature-specific functions and entrypoints. We're specifically targeting the new "Device Trust" logic, refactored driver binding, new sysfs entrypoints, and the locking changes |
| 4/2 | 2026/11/08 04:35 | tool | grepper |
0mArgs:{
"Expression": "define VISIBLE_IF_KUNIT"
}Results: {
"Output": "include/kunit/visibility.h-18- */\ninclude/kunit/visibility.h:19: #define VISIBLE_IF_KUNIT\ninclude/kunit/visibility.h-20- /**\n--\ninclude/kunit/visibility.h-28-#else\ninclude/kunit/visibility.h:29: #define VISIBLE_IF_KUNIT static\ninclude/kunit/visibility.h-30- #define EXPORT_SYMBOL_IF_KUNIT(symbol)\n"
} |
| 5/2 | 2026/11/08 04:35 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56611 output: 20 thoughts: 1062 Thoughts: **Focusing on Trust and Device Binding** Okay, so I'm looking at how this `VISIBLE_IF_KUNIT` macro affects the code, and it seems like it's making functions `static` unless `CONFIG_KUNIT` is enabled. My immediate thought is, "What functions in `drivers/base/trust.c` are exported and not static?" I need to see what's visible outside that file. Alright, I've checked `drivers/base/trust.c`. I see `device_trust_init_state` (EXPORT_SYMBOL_IF_KUNIT), `device_trust_prepare` (extern in `base.h`, non-static), `device_trust_clear` (extern in `base.h`, non-static), `device_get_trust_level` (EXPORT_SYMBOL_GPL), `device_get_committed_trust_level` (EXPORT_SYMBOL_GPL), `device_is_adversarial` (EXPORT_SYMBOL_GPL), `device_trust_may_claim` (EXPORT_SYMBOL_GPL), and `device_trust_init` (EXPORT_SYMBOL_GPL). That's a good start. Next, I need to consider sysfs callbacks. Let's see... `trust_show`, `trust_policy_show`, and `trust_policy_store`. Okay, I know that static functions, unless inlined, will be symbols in `vmlinux`. And ah, I see! `device_trust_set_policy` is called by `trust_policy_store`. `trust_policy_store` is the sysfs write handler for `/sys/devices/.../trust_policy`. `trust_show` is sysfs read handler for `/sys/devices/.../trust`. `trust_policy_show` is the sysfs read handler for `/sys/devices/.../trust_policy`. I've also spotted `drivers_probe_store`, which is the sysfs write handler for `/sys/bus/.../drivers_probe`. Now, digging into `drivers/base/bus.c`, I see `drivers_probe_store`: static `ssize_t drivers_probe_store(...)`. Oh, wait, let's look at `drivers/base/dd.c`. I see `device_driver_attach`, `device_driver_try_attach`, `really_probe`, and `device_bind_driver`. Hmmm, I should think very carefully about *what* to focus on. I need to make sure I am not including "ubiquitous" functions. The instructions are clear: avoid those functions that are part of every single code path. This also means generic memory allocators, page fault handlers, etc. are not helpful. Okay, so I need to choose my "FocusSymbols" carefully. They need to be specific to the *feature* I'm looking at. Let's see: * `trust_policy_store` (sysfs entrypoint for setting trust policy) * `trust_show` (sysfs entrypoint for reading trust level) * `trust_policy_show` (sysfs entrypoint for reading trust policy) * `device_trust_set_policy` (core policy transition logic) * `device_trust_prepare` (trust preparation before probing/binding) * `device_trust_clear` (clears trust on unbind/teardown) * `device_trust_may_claim` (checks if driver may claim unregistered dev) * `device_driver_try_attach` (new attach function introduced by patch) * `drivers_probe_store` (sysfs drivers_probe write handler modified in bus.c) * `usb_driver_release_refused_claim` (new function handling refused claims) Perfect, those all look like they directly relate to the trust feature and the changes. Wait, I think I need to check something else: `mac80211_hwsim_new_radio`. Let's see how `mac80211_hwsim` was changed. Looks like `data->dev->driver = &mac80211_hwsim_driver.driver;` and then `device_lock(data->dev);` followed by `err = device_bind_driver(data->dev);` and `device_unlock(data->dev);`. Ah! Before, they called `device_bind_driver` *without* a lock in `mac80211_hwsim_new_radio`. But `device_bind_driver` calls `device_trust_prepare`, which has `device_lock_assert(dev)`! So |
| 6/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "device_bind_driver\\("
}Results: {
"Output": "drivers/base/core.c=1326=static const DEVICE_ATTR_RO(waiting_for_supplier);\n--\ndrivers/base/core.c-1331- *\ndrivers/base/core.c:1332: * device_bind_driver() force binds a device to a driver without calling any\ndrivers/base/core.c-1333- * driver probe functions. So the consumer really isn't going to wait for any\n--\ndrivers/base/core.c-1336- *\ndrivers/base/core.c:1337: * In preparation for device_bind_driver(), this function goes through each\ndrivers/base/core.c-1338- * supplier device links and checks if the supplier is bound. If it is, then\n--\ndrivers/base/dd.c=517=static void driver_sysfs_remove(struct device *dev)\n--\ndrivers/base/dd.c-543- */\ndrivers/base/dd.c:544:int device_bind_driver(struct device *dev)\ndrivers/base/dd.c-545-{\n--\ndrivers/base/dd.c=1138=static int __device_attach(struct device *dev, bool allow_async)\n--\ndrivers/base/dd.c-1150-\t\t}\ndrivers/base/dd.c:1151:\t\tret = device_bind_driver(dev);\ndrivers/base/dd.c-1152-\t\tif (ret == 0)\n--\ndrivers/base/test/device-trust-test.c=859=static void device_trust_force_bind_guard_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-875-\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\ndrivers/base/test/device-trust-test.c:876:\tret = device_bind_driver(\u0026ctx-\u003edev);\ndrivers/base/test/device-trust-test.c-877-\tctx-\u003edev.driver = NULL;\n--\ndrivers/base/test/device-trust-test.c=887=static void device_trust_legacy_force_bind_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-903-\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\ndrivers/base/test/device-trust-test.c:904:\tret = device_bind_driver(\u0026ctx-\u003edev);\ndrivers/base/test/device-trust-test.c-905-\tif (ret)\n--\ndrivers/base/test/device-trust-test.c=925=static void device_trust_busless_force_bind_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-938-\tctx-\u003edev.driver = \u0026ctx-\u003edrv;\ndrivers/base/test/device-trust-test.c:939:\tret = device_bind_driver(\u0026ctx-\u003edev);\ndrivers/base/test/device-trust-test.c-940-\tif (ret)\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=5722=static int mac80211_hwsim_new_radio(struct genl_info *info,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-5772-\tdevice_lock(data-\u003edev);\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:5773:\terr = device_bind_driver(data-\u003edev);\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-5774-\tdevice_unlock(data-\u003edev);\n--\ndrivers/usb/core/driver.c=549=int usb_driver_claim_interface(struct usb_driver *driver,\n--\ndrivers/usb/core/driver.c-593-\t\tdevice_lock(dev);\ndrivers/usb/core/driver.c:594:\t\tretval = device_bind_driver(dev);\ndrivers/usb/core/driver.c-595-\t\tdevice_unlock(dev);\n--\ninclude/linux/device.h=1331=int __must_check device_driver_try_attach(const struct device_driver *drv,\ninclude/linux/device.h-1332-\t\t\t\t\t struct device *dev);\ninclude/linux/device.h:1333:int __must_check device_bind_driver(struct device *dev);\ninclude/linux/device.h-1334-void device_release_driver(struct device *dev);\n"
} |
| 7/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 59880 output: 26 thoughts: 557 Thoughts: **Analyzing Device Driver Binding in the Linux Kernel** Okay, so I'm diving into this code and noticing something interesting. It looks like `device_bind_driver()` is being phased out in a lot of places. I've seen it replaced with `device_driver_attach()` or `device_driver_try_attach()` across several driver types: `gameport`, `serio`, `pnp`, and `phy_device`. And in the `mac80211_hwsim` and `usb_driver_claim_interface` modules, they're wrapping the call with `device_lock(dev)` and `device_unlock(dev)`. That's not the usual approach. Now, I need to understand the conditions under which this code is even compiled. I'll check the relevant Kconfig options. Let's start with `drivers/base/Makefile`. Aha! I see `obj-y := ... trust.o ...`. `trust.o` is always built in! It's unconditionally included as part of the driver core! Well that is unusual. Wait a second, let me confirm. Yup, the full line reads: `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 trust.o \ attribute_container.o transport_class.o \ topology.o container.o property.o cacheinfo.o \ swnode.o faux.o` The `trust.o` file is unconditionally compiled as part of the driver core! That seems odd. Okay, let's look at the other modified files to see if there are any conditions there. - `drivers/bluetooth/btusb_main.c` is compiled when `CONFIG_BT_HCIBTUSB` is set. - `drivers/net/phy/phy_device.c` is compiled when `CONFIG_PHYLIB` is set. - `drivers/net/wireless/virtual/mac80211_hwsim_main.c` is compiled when `CONFIG_MAC80211_HWSIM` is set. - `drivers/iommu/dma-iommu.c` is compiled when `CONFIG_IOMMU_DMA` is set. - `drivers/usb/core/driver.c` is compiled when `CONFIG_USB` is set. - The `drivers/pci/` directory requires `CONFIG_PCI`. But what about the core device trust logic itself? Is that dependent on any specific configs? The core logic resides in `drivers/base/trust.c`, `drivers/base/dd.c`, `drivers/base/bus.c`, and `drivers/base/core.c`. These are all also `obj-y` in `drivers/base/Makefile`. The important parts are the driver core. So, what specific config is required to compile those files? I will check `drivers/Makefile`. |
| 8/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "base/",
"PathPrefixes": [
"drivers/Makefile"
]
}Results: {
"Output": "drivers/Makefile=75=obj-$(CONFIG_PARPORT)\t\t+= parport/\ndrivers/Makefile:76:obj-y\t\t\t\t+= base/ block/ misc/ mfd/ nfc/\ndrivers/Makefile-77-obj-$(CONFIG_LIBNVDIMM)\t\t+= nvdimm/\n"
} |
| 9/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61227 output: 37 thoughts: 625 Thoughts: **Analyzing Dependencies for Device Trust** Okay, so I'm diving into this `drivers/base` directory, and it looks like `trust.o` is always built (`obj-y`). That's the baseline. But wait, what about the sysfs attributes? I need to figure out if specific configs are required for their functionality. Thinking through this... I immediately remember, `CONFIG_SYSFS` is fundamental for those sysfs files like `/sys/devices/.../trust` and such. While `CONFIG_SYSFS` is pretty much standard in modern kernels (especially amd64), I should probably explicitly document that `SYSFS` is needed. Alright, moving on, I need to consider all the places `device_trust` is used beyond just the sysfs interface. PCI devices are a good starting point because they use functions like `device_trust_init` and `pci_set_default_trust_policy`. The USB core is another, with `device_trust_init`, `device_trust_may_claim`, and `usb_driver_release_refused_claim`. And I can't forget `CONFIG_MAC80211_HWSIM`, it tests device lock changes. Finally, `CONFIG_BT_HCIBTUSB` pops up, which shows usage in btusb MTK claim change. Okay, let's step back and follow a structured approach. I need to make sure I don't miss anything. Following the EnableConfigs instructions, I need to really *identify* the specific `CONFIG_` options needed. No assumptions. Time to go through the Kconfig files and look at the `#ifdef` guards. Checking the "depends on" lines is important too. And remember, strip that "CONFIG_" prefix when I report the findings. Let's see, has this patch actually *introduced* any new Kconfig options or conditionals? I'll do a quick git diff to check. Looking... Nope, no Kconfig files have been touched. That's interesting. Okay, let's confirm the unconditional nature of `device_trust` then. I'll look again at the `drivers/base/Makefile`. Yep, `obj-y := ... trust.o ...` confirms it's unconditionally compiled. Now, circling back to `CONFIG_SYSFS`. Is it truly *needed* even if the core functionality is always built? Let's check `drivers/base/core.c` around `device_add_attrs` and `device_trust_attr_group`. Aha, there it is: `error = device_add_group(dev, &device_trust_attr_group);`. If `CONFIG_SYSFS` isn't enabled, then the sysfs attributes simply wouldn't be accessible. Bingo! Alright, finally, I should focus on the specific config options that enable the drivers and subsystems where behavior has been changed. Looking at the `btusb_main.c` file shows that `CONFIG_BT_HCIBTUSB` is a critical config option to include. I should consider `CONFIG_BT_HCIBTUSB_MTK` as well. |
| 10/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "btusb_mtk_claim_iso_intf",
"PathPrefixes": [
"drivers/bluetooth/"
]
}Results: {
"Output": "drivers/bluetooth/btusb_main.c=2975=static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)\n--\ndrivers/bluetooth/btusb_main.c-2998-\ndrivers/bluetooth/btusb_main.c:2999:static void btusb_mtk_claim_iso_intf(struct btusb_data *data)\ndrivers/bluetooth/btusb_main.c-3000-{\n--\ndrivers/bluetooth/btusb_main.c=3172=static int btusb_mtk_setup(struct hci_dev *hdev)\n--\ndrivers/bluetooth/btusb_main.c-3189-\t\tbtmtk_data-\u003eisopkt_intf = usb_ifnum_to_if(data-\u003eudev, MTK_ISO_IFNUM);\ndrivers/bluetooth/btusb_main.c:3190:\t\tbtusb_mtk_claim_iso_intf(data);\ndrivers/bluetooth/btusb_main.c-3191-\t}\n"
} |
| 11/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62834 output: 39 thoughts: 24 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating Bluetooth USB Driver Configuration** Right, let's get down to brass tacks. I need to understand the configuration options for the MediaTek Bluetooth USB driver. Specifically, I should start by diving into the `drivers/bluetooth/Kconfig` file. That's the definitive source for determining how this driver is controlled and what dependencies it might have. Understanding `BT_HCIBTUSB_MTK` is crucial here. I need to see exactly what that config symbol enables, if it requires other settings, and what kind of build options it presents. Time to get familiar with the specifics of the MediaTek implementation. |
| 12/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "BTUSB.*MTK|BT_HCIBTUSB",
"PathPrefixes": [
"drivers/bluetooth/Kconfig"
]
}Results: {
"Output": "drivers/bluetooth/Kconfig=22=config BT_MTK\n--\ndrivers/bluetooth/Kconfig-25-\ndrivers/bluetooth/Kconfig:26:config BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-27-\ttristate \"HCI USB driver\"\n--\ndrivers/bluetooth/Kconfig-37-\ndrivers/bluetooth/Kconfig:38:config BT_HCIBTUSB_AUTOSUSPEND\ndrivers/bluetooth/Kconfig-39-\tbool \"Enable USB autosuspend for Bluetooth USB devices by default\"\ndrivers/bluetooth/Kconfig:40:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-41-\thelp\n--\ndrivers/bluetooth/Kconfig-47-\ndrivers/bluetooth/Kconfig:48:config BT_HCIBTUSB_POLL_SYNC\ndrivers/bluetooth/Kconfig-49-\tbool \"Enable USB poll_sync for Bluetooth USB devices by default\"\ndrivers/bluetooth/Kconfig:50:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-51-\tdefault y\n--\ndrivers/bluetooth/Kconfig-58-\ndrivers/bluetooth/Kconfig:59:config BT_HCIBTUSB_BCM\ndrivers/bluetooth/Kconfig-60-\tbool \"Broadcom protocol support\"\ndrivers/bluetooth/Kconfig:61:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-62-\tselect BT_BCM\n--\ndrivers/bluetooth/Kconfig-69-\ndrivers/bluetooth/Kconfig:70:config BT_HCIBTUSB_MTK\ndrivers/bluetooth/Kconfig-71-\tbool \"MediaTek protocol support\"\ndrivers/bluetooth/Kconfig:72:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-73-\tselect BT_MTK\n--\ndrivers/bluetooth/Kconfig-81-\ndrivers/bluetooth/Kconfig:82:config BT_HCIBTUSB_RTL\ndrivers/bluetooth/Kconfig-83-\tbool \"Realtek protocol support\"\ndrivers/bluetooth/Kconfig:84:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-85-\tselect BT_RTL\n--\ndrivers/bluetooth/Kconfig-92-\ndrivers/bluetooth/Kconfig:93:config BT_HCIBTUSB_QCOM\ndrivers/bluetooth/Kconfig-94-\tbool \"Qualcomm protocol support\"\ndrivers/bluetooth/Kconfig:95:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-96-\tselect FW_LOADER\n--\ndrivers/bluetooth/Kconfig=390=config BT_ATH3K\ndrivers/bluetooth/Kconfig-391-\ttristate \"Atheros firmware download driver\"\ndrivers/bluetooth/Kconfig:392:\tdepends on BT_HCIBTUSB\ndrivers/bluetooth/Kconfig-393-\tselect FW_LOADER\n--\ndrivers/bluetooth/Kconfig=402=config BT_MTKSDIO\n--\ndrivers/bluetooth/Kconfig-404-\tdepends on MMC\ndrivers/bluetooth/Kconfig:405:\tdepends on USB || !BT_HCIBTUSB_MTK\ndrivers/bluetooth/Kconfig-406-\tselect BT_MTK\n--\ndrivers/bluetooth/Kconfig=415=config BT_MTKUART\n--\ndrivers/bluetooth/Kconfig-418-\tdepends on SERIAL_DEV_BUS\ndrivers/bluetooth/Kconfig:419:\tdepends on USB || !BT_HCIBTUSB_MTK\ndrivers/bluetooth/Kconfig-420-\tselect BT_HCIUART_H4\n"
} |
| 13/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 63935 output: 38 thoughts: 43 Thoughts: Okay, let's see. My first thought is: **Investigating the `mac80211_hwsim` Configuration**. Right, I need to figure out the specific configuration this module employs. My instinct is to check the build system to see how it's defined. Let's start by looking at `drivers/net/wireless/virtual/Makefile` or perhaps `Kconfig`. That's where the relevant configuration options should be located, and where I can likely identify the exact dependencies and settings being used for `mac80211_hwsim`. This should give me the information I need. |
| 14/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "mac80211_hwsim",
"PathPrefixes": [
"drivers/net/wireless/virtual/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 1620 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (7 files in total):\ndrivers/net/wireless/virtual/Kconfig:2\ndrivers/net/wireless/virtual/Makefile:3\ndrivers/net/wireless/virtual/mac80211_hwsim.h:4\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:12\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:350\ndrivers/net/wireless/virtual/mac80211_hwsim_nan.c:75\ndrivers/net/wireless/virtual/mac80211_hwsim_nan.h:18\n\ndrivers/net/wireless/virtual/Kconfig=1=config MAC80211_HWSIM\n--\ndrivers/net/wireless/virtual/Kconfig-7-\t needed for normal wireless LAN usage and is only for testing. See\ndrivers/net/wireless/virtual/Kconfig:8:\t Documentation/networking/mac80211_hwsim for more information on how\ndrivers/net/wireless/virtual/Kconfig-9-\t to use this tool.\n--\ndrivers/net/wireless/virtual/Kconfig-11-\t To compile this driver as a module, choose M here: the module will be\ndrivers/net/wireless/virtual/Kconfig:12:\t called mac80211_hwsim. If unsure, say N.\ndrivers/net/wireless/virtual/Kconfig-13-\n--\ndrivers/net/wireless/virtual/Makefile:1:obj-$(CONFIG_MAC80211_HWSIM)\t+= mac80211_hwsim.o\ndrivers/net/wireless/virtual/Makefile:2:mac80211_hwsim-objs\t\t+= mac80211_hwsim_main.o\ndrivers/net/wireless/virtual/Makefile:3:mac80211_hwsim-objs\t\t+= mac80211_hwsim_nan.o\ndrivers/net/wireless/virtual/Makefile-4-\n--\ndrivers/net/wireless/virtual/mac80211_hwsim.h-2-/*\ndrivers/net/wireless/virtual/mac80211_hwsim.h:3: * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211\ndrivers/net/wireless/virtual/mac80211_hwsim.h-4- * Copyright (c) 2008, Jouni Malinen \u003cj@w1.fi\u003e\n--\ndrivers/net/wireless/virtual/mac80211_hwsim.h=23=enum hwsim_tx_control_flags {\n--\ndrivers/net/wireless/virtual/mac80211_hwsim.h-33- * entities such as wmediumd to receive and process all broadcasted\ndrivers/net/wireless/virtual/mac80211_hwsim.h:34: * frames from a mac80211_hwsim radio device.\ndrivers/net/wireless/virtual/mac80211_hwsim.h-35- *\n--\ndrivers/net/wireless/virtual/mac80211_hwsim.h-42- * Once registered the user application has to take responsibility of\ndrivers/net/wireless/virtual/mac80211_hwsim.h:43: * broadcasting the frames to all listening mac80211_hwsim radio\ndrivers/net/wireless/virtual/mac80211_hwsim.h-44- * interfaces.\n--\ndrivers/net/wireless/virtual/mac80211_hwsim.h-55- * @HWSIM_CMD_REGISTER: request to register and received all broadcasted\ndrivers/net/wireless/virtual/mac80211_hwsim.h:56: *\tframes by any mac80211_hwsim radio device.\ndrivers/net/wireless/virtual/mac80211_hwsim.h-57- * @HWSIM_CMD_FRAME: send/receive a broadcasted frame from/to kernel/user\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-2-/*\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:3: * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-4- * Copyright (c) 2008, Jouni Malinen \u003cj@w1.fi\u003e\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-13-#include \u003cnet/mac80211.h\u003e\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:14:#include \"mac80211_hwsim.h\"\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:15:#include \"mac80211_hwsim_nan.h\"\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-16-\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h=27=struct hwsim_sta_priv {\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-37-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:38:struct mac80211_hwsim_link_data {\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-39-\tu32 link_id;\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-50-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:51:struct mac80211_hwsim_data {\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-52-\tstruct list_head list;\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-65-\t/* Storage space for channels, etc. */\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:66:\tstruct mac80211_hwsim_phy_data *phy_data;\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-67-\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-144-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:145:\tstruct mac80211_hwsim_link_data link_data[IEEE80211_MLD_MAX_NUM_LINKS];\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-146-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:147:\tstruct mac80211_hwsim_nan_data nan;\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-148-};\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h=151=extern struct list_head hwsim_radios;\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-152-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:153:ktime_t mac80211_hwsim_tsf_to_boottime(struct mac80211_hwsim_data *data,\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-154-\t\t\t\t u64 tsf);\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:155:u64 mac80211_hwsim_boottime_to_tsf(struct mac80211_hwsim_data *data,\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-156-\t\t\t\t ktime_t ts);\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-157-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:158:u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-159-\t\t\t struct ieee80211_vif *vif);\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-160-\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h:161:void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,\ndrivers/net/wireless/virtual/mac80211_hwsim_i.h-162-\t\t\t struct sk_buff *skb,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-2-/*\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:3: * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-4- * Copyright (c) 2008, Jouni Malinen \u003cj@w1.fi\u003e\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-40-#include \u003clinux/string.h\u003e\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:41:#include \"mac80211_hwsim.h\"\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:42:#include \"mac80211_hwsim_i.h\"\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-43-\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=335=static const struct class hwsim_class = {\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:336:\t.name\t= \"mac80211_hwsim\"\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-337-};\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=604=hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-607-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:608:static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-609-\t\t\t\t\t struct wireless_dev *wdev,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-630-\t *\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:631:\t * event_idx = 0 (index in mac80211_hwsim_vendor_commands)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-632-\t */\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-658-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:659:static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-660-\t{\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-663-\t\t.flags = WIPHY_VENDOR_CMD_NEED_NETDEV,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:664:\t\t.doit = mac80211_hwsim_vendor_cmd_test,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-665-\t\t.policy = hwsim_vendor_test_policy,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-670-/* Advertise support vendor specific events */\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:671:static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-672-\t{ .vendor_id = OUI_QCA, .subcmd = 1 },\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=679=static int hwsim_radios_generation = 1;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-680-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:681:static struct platform_driver mac80211_hwsim_driver = {\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-682-\t.driver = {\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:683:\t\t.name = \"mac80211_hwsim\",\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-684-\t},\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=687=static const struct rhashtable_params hwsim_rht_params = {\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-690-\t.key_len = ETH_ALEN,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:691:\t.key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:692:\t.head_offset = offsetof(struct mac80211_hwsim_data, rht),\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-693-};\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=704=struct hwsim_radiotap_ack_hdr {\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-711-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:712:static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-713-{\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=909=static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-910-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:911:static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-912-\t\t\t struct sk_buff *skb)\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-939-/* cause a linker error if this ends up being needed */\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:940:extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-941-\t\t\t struct sk_buff *skb);\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=980=static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-981-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:982:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-983-\tstruct hwsim_vif_priv *vp = (void *)vif-\u003edrv_priv;\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1005-\trcu_read_lock();\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1006:\tmac80211_hwsim_tx_frame(data-\u003ehw, skb,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1007-\t\t\t\trcu_dereference(vif-\u003ebss_conf.chanctx_conf)-\u003edef.chan);\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1010-\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1011:static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1012-\t\t\t\tstruct ieee80211_vif *vif, int ps)\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1043-\trcu_read_lock();\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1044:\tmac80211_hwsim_tx_frame(data-\u003ehw, skb,\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1045-\t\t\t\trcu_dereference(vif-\u003ebss_conf.chanctx_conf)-\u003edef.chan);\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1050=static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1052-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1053:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1054-\thwsim_send_nullfunc(data, mac, vif, 1);\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1057=static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1059-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1060:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1061-\thwsim_send_nullfunc(data, mac, vif, 0);\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1064=static int hwsim_fops_ps_read(void *dat, u64 *val)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1065-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1066:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1067-\t*val = data-\u003eps;\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1071=static int hwsim_fops_ps_write(void *dat, u64 val)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1072-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1073:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1074-\tenum ps_mode old_ps;\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1111=static int hwsim_write_simulate_radar(void *dat, u64 val)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1112-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1113:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1114-\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1123=static ssize_t hwsim_background_cac_write(struct file *file,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1126-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1127:\tstruct mac80211_hwsim_data *data = file-\u003eprivate_data;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1128-\tchar buf[8] = {};\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1178=static ssize_t hwsim_simulate_incumbent_signal_write(struct file *file,\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1181-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1182:\tstruct mac80211_hwsim_data *data = file-\u003eprivate_data;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1183-\tstruct hwsim_chanctx_iter_arg arg = {};\n--\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c=1221=static int hwsim_fops_group_read(void *dat, u64 *val)\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1222-{\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c:1223:\tstruct mac80211_hwsim_data *data = dat;\ndrivers/net/wireless/virtual/mac80211_hwsim_main.c-1224-\t*val = data-\u003egroup;\n--\n\n"
} |
| 15/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 70396 output: 31 thoughts: 1956 Thoughts: **Focusing on Device Trust for Syzkaller Coverage** Okay, so I need to figure out the best approach for fuzzing this device trust patch with syzkaller. Let's start with the basics. We already know that syzkaller config uses `SYSFS`, `PCI`, `USB`, `MAC80211_HWSIM`, and `BT_HCIBTUSB` (or similar) by default, and that's a good starting point. KUnit is for unit tests, but we're interested in the real kernel fuzzing with syzkaller. My strategy is to identify specific symbols introduced or heavily modified by this patch, and then ensure the right kernel configuration options are enabled to increase the likelihood of hitting those symbols. The obvious first symbols are the ones directly related to the new device trust functionality: 1. `trust_policy_store` 2. `trust_policy_show` 3. `trust_show` 4. `device_trust_set_policy` 5. `device_trust_prepare` 6. `device_trust_clear` 7. `device_trust_may_claim` 8. `device_driver_try_attach` 9. `drivers_probe_store` 10. `usb_driver_release_refused_claim` 11. `btusb_mtk_claim_iso_intf` 12. `mac80211_hwsim_new_radio` I need to make sure these symbols are good targets, and not too ubiquitous. Let's review: * The `trust_policy` sysfs handlers are good. They are directly tied to the new sysfs interface, and are clearly specific to this patch. * `device_trust_set_policy` is a strong candidate, as it is only called by the sysfs store handler and also by kunit, but we're not running kunit. It is static, so it *should* appear in the kernel symbol table, since KUNIT is disabled. * `device_trust_prepare` is called during probe/bind, which makes it less specific. But it is still the core logic. * `device_trust_clear` is called during unbind, specific. * `device_trust_may_claim` is linked to USB, so is also specific. * `device_driver_try_attach` is a good one, it has many callers. * `drivers_probe_store` is also a sysfs-related symbol, and thus good. * `usb_driver_release_refused_claim` is good; it deals with USB claims. * `btusb_mtk_claim_iso_intf` is specific to MediaTek Bluetooth. Now, let's look at a few more symbols: `phy_attach_direct`, `pnp_request_card_device`, `gameport_bind_driver`, `serio_bind_driver`. These are modified to call `device_driver_try_attach` or other related functions. This adds to the number of callers, helping. And what about `really_probe`? It's called when a driver probes. But it's very ubiquitous, and device trust specific symbols are much better choices. `device_bind_driver` is called less often, which makes it good to use. `mac80211_hwsim_new_radio` is an excellent choice, because syzkaller commonly uses hwsim through netlink commands. Finally, let's map these to kernel configuration options. The obvious ones are: * `SYSFS` (for `trust`, `trust_policy`, `drivers_probe`) * `USB` (for USB core claims) * `BT_HCIBTUSB` (for MediaTek btusb changes) * `BT_HCIBTUSB_MTK` (for MediaTek protocol support for btusb) * `MAC80211_HWSIM` (for `mac80211_hwsim_new_radio`) * `PHYLIB` (for `phy_attach_direct`) I'll also search the Kconfig files for any `CONFIG_DEVICE_TRUST` options, just in case there are other relevant settings. This set of symbols and configs should give syzkaller good coverage of the device trust changes. |
| 16/2 | 2026/11/08 04:36 | tool | grepper |
0mArgs:{
"Expression": "DEVICE_TRUST",
"PathPrefixes": [
"drivers/",
"init/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 822 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (4 files in total):\ndrivers/base/base.h:4\ndrivers/base/test/device-trust-test.c:189\ndrivers/base/trust.c:51\ndrivers/pci/probe.c:3\n\ndrivers/base/base.h=92=enum device_trust_state {\ndrivers/base/base.h:93:\tDEVICE_TRUST_SELECTABLE,\ndrivers/base/base.h:94:\tDEVICE_TRUST_WAITING,\ndrivers/base/base.h:95:\tDEVICE_TRUST_AUTHORIZED,\ndrivers/base/base.h:96:\tDEVICE_TRUST_COMMITTED,\ndrivers/base/base.h-97-};\n--\ndrivers/base/test/device-trust-test.c=191=device_trust_test_device_register_parent(struct kunit *test,\n--\ndrivers/base/test/device-trust-test.c-211-\tctx-\u003ematch_ret = 1;\ndrivers/base/test/device-trust-test.c:212:\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\ndrivers/base/test/device-trust-test.c-213-\tctx-\u003edrv.probe_type = PROBE_FORCE_SYNCHRONOUS;\n--\ndrivers/base/test/device-trust-test.c=245=device_trust_test_device_register(struct kunit *test,\n--\ndrivers/base/test/device-trust-test.c-249-\treturn device_trust_test_device_register_parent(test, bus, policy,\ndrivers/base/test/device-trust-test.c:250:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, NULL);\ndrivers/base/test/device-trust-test.c-251-}\n--\ndrivers/base/test/device-trust-test.c=276=static void device_trust_expect_policy_locked(struct kunit *test,\n--\ndrivers/base/test/device-trust-test.c-282-\ndrivers/base/test/device-trust-test.c:283:\tfor (policy = DEVICE_TRUST_POLICY_DEFAULT;\ndrivers/base/test/device-trust-test.c:284:\t policy \u003c= DEVICE_TRUST_POLICY_FULL; policy++) {\ndrivers/base/test/device-trust-test.c-285-\t\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(dev, policy),\n--\ndrivers/base/test/device-trust-test.c=292=static void device_trust_default_bind_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-297-\tctx = device_trust_test_device_register(\ndrivers/base/test/device-trust-test.c:298:\t\ttest, \u0026device_trust_test_plain_bus, DEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-299-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\ndrivers/base/test/device-trust-test.c-300-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:301:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-302-\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:303:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-304-\n--\ndrivers/base/test/device-trust-test.c-308-\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_count, 1);\ndrivers/base/test/device-trust-test.c:309:\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-310-\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_count, 1);\ndrivers/base/test/device-trust-test.c:311:\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-312-\tKUNIT_EXPECT_FALSE(test, ctx-\u003eprobe_adversarial);\ndrivers/base/test/device-trust-test.c-313-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:314:\t\t\tDEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-315-\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n--\ndrivers/base/test/device-trust-test.c-318-\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_count, 1);\ndrivers/base/test/device-trust-test.c:319:\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-320-\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_count, 1);\ndrivers/base/test/device-trust-test.c:321:\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-322-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:323:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-324-\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:325:\t\t\tDEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-326-\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n--\ndrivers/base/test/device-trust-test.c=329=static void device_trust_bus_adversary_bind_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-335-\t\ttest, \u0026device_trust_test_resolving_bus,\ndrivers/base/test/device-trust-test.c:336:\t\tDEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-337-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\ndrivers/base/test/device-trust-test.c:338:\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\ndrivers/base/test/device-trust-test.c-339-\n--\ndrivers/base/test/device-trust-test.c-344-\tKUNIT_EXPECT_EQ(test, ctx-\u003eresolve_policy,\ndrivers/base/test/device-trust-test.c:345:\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-346-\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003eresolve_driver, \u0026ctx-\u003edrv);\ndrivers/base/test/device-trust-test.c:347:\tKUNIT_EXPECT_EQ(test, ctx-\u003econfigure_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c:348:\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-349-\tKUNIT_EXPECT_TRUE(test, ctx-\u003eprobe_adversarial);\n--\ndrivers/base/test/device-trust-test.c-351-\tdevice_release_driver(\u0026ctx-\u003edev);\ndrivers/base/test/device-trust-test.c:352:\tKUNIT_EXPECT_EQ(test, ctx-\u003eremove_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c:353:\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-354-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:355:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-356-}\n--\ndrivers/base/test/device-trust-test.c=363=static void device_trust_default_pinned_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-369-\tctx = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\ndrivers/base/test/device-trust-test.c:370:\t\t\t\t\t\tDEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-371-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\ndrivers/base/test/device-trust-test.c:372:\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\ndrivers/base/test/device-trust-test.c-373-\tKUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);\n--\ndrivers/base/test/device-trust-test.c-385-\tother-\u003esuppress_bind_attrs = true;\ndrivers/base/test/device-trust-test.c:386:\tctx-\u003eresolve_level = DEVICE_TRUST_FULL;\ndrivers/base/test/device-trust-test.c-387-\tKUNIT_ASSERT_EQ(test, driver_register(other), 0);\n--\ndrivers/base/test/device-trust-test.c-396-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:397:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c:398:\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-399-\ndrivers/base/test/device-trust-test.c:400:\tctx-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\ndrivers/base/test/device-trust-test.c-401-\tKUNIT_ASSERT_EQ(test, device_driver_attach(other, \u0026ctx-\u003edev), 0);\n--\ndrivers/base/test/device-trust-test.c-403-\tKUNIT_EXPECT_PTR_EQ(test, ctx-\u003edev.driver, other);\ndrivers/base/test/device-trust-test.c:404:\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-405-\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n--\ndrivers/base/test/device-trust-test.c=408=static void device_trust_authorize_test(struct kunit *test,\n--\ndrivers/base/test/device-trust-test.c-419-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\ndrivers/base/test/device-trust-test.c:420:\tKUNIT_ASSERT_EQ(test, ctx-\u003edev.trust_policy, DEVICE_TRUST_POLICY_DISABLED);\ndrivers/base/test/device-trust-test.c-421-\n--\ndrivers/base/test/device-trust-test.c-429-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:430:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-431-\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:432:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-433-\tKUNIT_EXPECT_TRUE(test, dev_can_match(\u0026ctx-\u003edev));\ndrivers/base/test/device-trust-test.c-434-\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\ndrivers/base/test/device-trust-test.c:435:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED), 0);\ndrivers/base/test/device-trust-test.c-436-\tKUNIT_EXPECT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\ndrivers/base/test/device-trust-test.c:437:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT), -EBUSY);\ndrivers/base/test/device-trust-test.c-438-\ndrivers/base/test/device-trust-test.c-439-\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev, policy), 0);\ndrivers/base/test/device-trust-test.c:440:\tdevice_trust_apply_boot_policy(\u0026ctx-\u003edev, DEVICE_TRUST_POLICY_DISABLED, NULL);\ndrivers/base/test/device-trust-test.c-441-\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, policy);\n--\ndrivers/base/test/device-trust-test.c-443-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:444:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-445-\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:446:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-447-\n--\ndrivers/base/test/device-trust-test.c-457-\tKUNIT_EXPECT_EQ(test, ctx-\u003ecleanup_level, level);\ndrivers/base/test/device-trust-test.c:458:\tdevice_trust_apply_boot_policy(\u0026ctx-\u003edev, DEVICE_TRUST_POLICY_DISABLED, NULL);\ndrivers/base/test/device-trust-test.c-459-\tKUNIT_EXPECT_EQ(test, ctx-\u003edev.trust_policy, policy);\ndrivers/base/test/device-trust-test.c-460-\tKUNIT_EXPECT_EQ(test, device_get_trust_level(\u0026ctx-\u003edev),\ndrivers/base/test/device-trust-test.c:461:\t\t\tDEVICE_TRUST_DISABLED);\ndrivers/base/test/device-trust-test.c-462-\tKUNIT_EXPECT_EQ(test, device_get_committed_trust_level(\u0026ctx-\u003edev),\n--\ndrivers/base/test/device-trust-test.c=467=static void device_trust_disabled_bind_test(struct kunit *test)\ndrivers/base/test/device-trust-test.c-468-{\ndrivers/base/test/device-trust-test.c:469:\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,\ndrivers/base/test/device-trust-test.c:470:\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_ADVERSARY,\ndrivers/base/test/device-trust-test.c:471:\t\t\t\t DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-472-}\n--\ndrivers/base/test/device-trust-test.c=474=static void device_trust_disabled_full_bind_test(struct kunit *test)\ndrivers/base/test/device-trust-test.c-475-{\ndrivers/base/test/device-trust-test.c:476:\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,\ndrivers/base/test/device-trust-test.c:477:\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_FULL,\ndrivers/base/test/device-trust-test.c:478:\t\t\t\t DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-479-}\n--\ndrivers/base/test/device-trust-test.c=481=static void device_trust_global_disabled_adversary_test(struct kunit *test)\ndrivers/base/test/device-trust-test.c-482-{\ndrivers/base/test/device-trust-test.c:483:\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARY,\ndrivers/base/test/device-trust-test.c:484:\t\t\t\t DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_ADVERSARY,\ndrivers/base/test/device-trust-test.c:485:\t\t\t\t DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-486-}\n--\ndrivers/base/test/device-trust-test.c=488=static void device_trust_global_disabled_full_test(struct kunit *test)\ndrivers/base/test/device-trust-test.c-489-{\ndrivers/base/test/device-trust-test.c:490:\tdevice_trust_authorize_test(test, DEVICE_TRUST_POLICY_FULL,\ndrivers/base/test/device-trust-test.c:491:\t\t\t\t DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_FULL,\ndrivers/base/test/device-trust-test.c:492:\t\t\t\t DEVICE_TRUST_FULL);\ndrivers/base/test/device-trust-test.c-493-}\n--\ndrivers/base/test/device-trust-test.c=500=static void device_trust_supplier_retry_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-506-\tsupplier = device_trust_test_device_register(test, \u0026device_trust_test_plain_bus,\ndrivers/base/test/device-trust-test.c:507:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT);\ndrivers/base/test/device-trust-test.c-508-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, supplier);\n--\ndrivers/base/test/device-trust-test.c-514-\tconsumer = device_trust_test_device_register(test, \u0026device_trust_test_resolving_bus,\ndrivers/base/test/device-trust-test.c:515:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DISABLED);\ndrivers/base/test/device-trust-test.c-516-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, consumer);\ndrivers/base/test/device-trust-test.c:517:\tconsumer-\u003eresolve_level = DEVICE_TRUST_ADVERSARY;\ndrivers/base/test/device-trust-test.c-518-\tlink = device_link_add(\u0026consumer-\u003edev, \u0026supplier-\u003edev, 0);\n--\ndrivers/base/test/device-trust-test.c-525-\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026consumer-\u003edev,\ndrivers/base/test/device-trust-test.c:526:\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\ndrivers/base/test/device-trust-test.c-527-\tconsumer-\u003eresolve_ret = -EAGAIN;\n--\ndrivers/base/test/device-trust-test.c=542=static void device_trust_match_defer_authorize_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-546-\tctx = device_trust_test_device_register(test, \u0026device_trust_test_plain_bus,\ndrivers/base/test/device-trust-test.c:547:\t\t\t\t\t\tDEVICE_TRUST_POLICY_DISABLED);\ndrivers/base/test/device-trust-test.c-548-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n--\ndrivers/base/test/device-trust-test.c-555-\tKUNIT_ASSERT_EQ(test, device_trust_set_policy(\u0026ctx-\u003edev,\ndrivers/base/test/device-trust-test.c:556:\t\t\t\t\t\t DEVICE_TRUST_POLICY_ADVERSARY), 0);\ndrivers/base/test/device-trust-test.c-557-\tdevice_trust_expect_policy_locked(test, \u0026ctx-\u003edev);\n--\ndrivers/base/test/device-trust-test.c-561-\tKUNIT_EXPECT_TRUE(test, device_is_bound(\u0026ctx-\u003edev));\ndrivers/base/test/device-trust-test.c:562:\tKUNIT_EXPECT_EQ(test, ctx-\u003eprobe_level, DEVICE_TRUST_ADVERSARY);\ndrivers/base/test/device-trust-test.c-563-}\n--\ndrivers/base/test/device-trust-test.c=570=static void device_trust_async_policy_test(struct kunit *test)\n--\ndrivers/base/test/device-trust-test.c-581-\tctx = device_trust_test_device_register_parent(test, \u0026device_trust_test_plain_bus,\ndrivers/base/test/device-trust-test.c:582:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT,\ndrivers/base/test/device-trust-test.c:583:\t\t\t\t\t\t DEVICE_TRUST_POLICY_DEFAULT, parent);\ndrivers/base/test/device-trust-test.c-584-\tKUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);\n--\ndrivers/base/test/device-trust-test.c-591-\t\tret = device_trust_set_policy(\u0026ctx-\u003edev,\n\n"
} |
| 17/2 | 2026/11/08 04:36 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 79027 output: 367 thoughts: 705 Thoughts: **Device Trust Subsystem - Fuzzing Rationale** Okay, here's what's running through my head about this Device Trust subsystem. First off, `drivers/base/trust.c` is always compiled in, which is good. That's the foundation. For userspace interaction, we'll need `SYSFS` enabled. If we are dealing with USB devices, then we will also need `USB`. Bluetooth claims `BT_HCIBTUSB` and `BT_HCIBTUSB_MTK`, which is sensible. If we are testing with mac80211 hwsim, well then we need `MAC80211_HWSIM`. The core of this is the introduction of this Device Trust subsystem into the Linux kernel's driver core. It's adding sysfs attributes: `/sys/devices/.../trust`, `/sys/devices/.../trust_policy`, and it's also changing how `/sys/bus/.../drivers_probe` works. Basically, this lets userspace poke and prod at the trust states and trigger probing. The critical thing here is how it's modifying the driver binding and probing flow – `device_bind_driver`, `really_probe`, `device_driver_attach`, and `device_driver_try_attach`. We're talking about strict locking invariants enforced by `device_lock_assert` within `device_trust_prepare`, with trust resolution happening before any probing or cleanup. That's where it can get tricky! Beyond the core logic, we're seeing adjustments to how drivers claim and attach across USB, mac80211_hwsim, serio, gameport, pnp, and net phy subsystems. That's a wide reach! Now, the important part: why fuzzing? Because this is changing driver binding, locking, state transitions, and adding *new* userspace-accessible sysfs endpoints. That's a recipe for concurrency issues, invariant violations, and regressions, the exact things that fuzzing is designed to catch! We've got our key symbols: `trust_policy_store`, `trust_policy_show`, `trust_show`, `device_trust_set_policy`, `device_trust_prepare`, `device_trust_clear`, `device_trust_may_claim`, `device_driver_try_attach`, `drivers_probe_store`, `usb_driver_release_refused_claim`, `btusb_mtk_claim_iso_intf`, and `mac80211_hwsim_new_radio`. These are the spots where the fuzzing campaign needs to focus. We already know that we need `SYSFS`, `USB`, `BT_HCIBTUSB`, `BT_HCIBTUSB_MTK`, and `MAC80211_HWSIM`. I did a quick check, and `PCI` is also needed for the overall system. Specifically, `pci_dev_default_is_restricted` and `pci_setup_device` in `drivers/pci/` confirms that we need it. So our final list of configurations is `SYSFS`, `PCI`, `USB`, `BT_HCIBTUSB`, `BT_HCIBTUSB_MTK`, and `MAC80211_HWSIM`. Everything's looking good, comprehensive, and covers all the bases. Time to call it in. |
| 18/2 | 2026/11/08 04:37 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"SYSFS",
"PCI",
"USB",
"BT_HCIBTUSB",
"BT_HCIBTUSB_MTK",
"MAC80211_HWSIM"
],
"FocusSymbols": [
"trust_policy_store",
"trust_policy_show",
"trust_show",
"device_trust_set_policy",
"device_trust_prepare",
"device_trust_clear",
"device_trust_may_claim",
"device_driver_try_attach",
"drivers_probe_store",
"usb_driver_release_refused_claim",
"btusb_mtk_claim_iso_intf",
"mac80211_hwsim_new_radio"
],
"Reasoning": "This patch introduces the Device Trust subsystem to the Linux kernel driver core along with new sysfs attributes (/sys/devices/.../trust, /sys/devices/.../trust_policy) and changes to /sys/bus/.../drivers_probe. It modifies the core driver binding and probe flow (really_probe, device_bind_driver, device_driver_attach, and adds device_driver_try_attach) to enforce new invariants, state machine transitions, and device locking assertions. Furthermore, it modifies driver binding and interface claiming logic across USB, mac80211_hwsim, Bluetooth (btusb), network PHY, gameport, and serio subsystems. These changes touch reachable core code and user-accessible interfaces, making them prime candidates for fuzzing to uncover potential locking regressions, state machine violations, and race conditions.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"SYSFS",
"PCI",
"USB",
"BT_HCIBTUSB",
"BT_HCIBTUSB_MTK",
"MAC80211_HWSIM"
],
"FocusSymbols": [
"trust_policy_store",
"trust_policy_show",
"trust_show",
"device_trust_set_policy",
"device_trust_prepare",
"device_trust_clear",
"device_trust_may_claim",
"device_driver_try_attach",
"drivers_probe_store",
"usb_driver_release_refused_claim",
"btusb_mtk_claim_iso_intf",
"mac80211_hwsim_new_radio"
],
"Reasoning": "This patch introduces the Device Trust subsystem to the Linux kernel driver core along with new sysfs attributes (/sys/devices/.../trust, /sys/devices/.../trust_policy) and changes to /sys/bus/.../drivers_probe. It modifies the core driver binding and probe flow (really_probe, device_bind_driver, device_driver_attach, and adds device_driver_try_attach) to enforce new invariants, state machine transitions, and device locking assertions. Furthermore, it modifies driver binding and interface claiming logic across USB, mac80211_hwsim, Bluetooth (btusb), network PHY, gameport, and serio subsystems. These changes touch reachable core code and user-accessible interfaces, making them prime candidates for fuzzing to uncover potential locking regressions, state machine violations, and race conditions.",
"WorthFuzzing": true
} |
| 19/1 | 2026/11/08 04:37 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 20/2 | 2026/11/08 04:37 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch series implements a device trust framework within the Linux driver core, allowing system administrators and subsystems to control driver binding and DMA operation based on requested policies (default, disabled, adversary, full). It introduces sysfs attributes (trust, trust_policy), kernel parameters (device_trust, device_trust.default, device_trust.driver), state machine transitions in the driver core, and integration across various subsystems (PCI, USB, IOMMU, input, networking).\n\nEvaluation against KMSAN applicability criteria:\n1. Kernel structures sent to user space: The patch only introduces text-based sysfs attributes ('trust' and 'trust_policy') formatted with sysfs_emit(). It does not introduce or modify any binary structures, ioctl outputs, netlink attributes, socket options, or BPF buffers copied to userspace that could leak uninitialized padding or fields.\n2. Uninitialized variables in branching: All local variables in the parsing and helper functions (e.g. level, policy, matched, err) are explicitly initialized before use or branch evaluation.\n3. Complex structure allocation and initialization: New fields added to 'struct device' and 'struct device_private' (trust_policy, trust_level, trust_committed_level, trust_state, etc.) reside in structures allocated via kzalloc (e.g., device_private_init uses kzalloc_obj) or initialized during device setup (device_trust_init, device_trust_init_state). Zero represents the default/disabled state across all relevant enums.\n4. Buffer bounds and lengths: The command-line parsing routines safely parse bounded string slices with memchr, strcspn, and strncmp without creating uninitialized buffer regions.\n\nPrimary risks in these changes relate to lock ordering and concurrency (device_driver_attach vs device_driver_try_attach, device_lock vs parent lock), probe error handling, state machine correctness, and driver/device reference counting during bind/unbind. These classes of bugs (deadlocks, NULL pointer dereferences, use-after-free) are effectively covered by standard KASAN, LOCKDEP, and general kernel debuggers.\n\nBecause no uninitialized memory reads or information leak hazards are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 8c56033573e04f5245d4c1c50d24fa102cb32e43
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 04:35:17 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/ABI/testing/sysfs-bus-drivers-probe b/Documentation/ABI/testing/sysfs-bus-drivers-probe
new file mode 100644
index 00000000000000..851c65b9331ae2
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-drivers-probe
@@ -0,0 +1,13 @@
+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 device's
+ driver link reports the result.
+
+ 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/Documentation/ABI/testing/sysfs-devices-trust b/Documentation/ABI/testing/sysfs-devices-trust
new file mode 100644
index 00000000000000..6f52102a1dfbf9
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-devices-trust
@@ -0,0 +1,61 @@
+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
+ "disabled", "adversary", and "full". Driver core resolves
+ the level before DMA configuration and probe, and retains it
+ through removal and DMA cleanup. Outside a bind transaction
+ the level is "disabled". This does not imply that policy is
+ writable or that the device has never operated.
+
+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.
+ 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.
+ Otherwise, device_trust.driver= can initialize policy on the
+ first binding attempt with a matching driver rule. A successful
+ policy write, including the existing value, suppresses this
+ deferred initialization. Driver rules do not change an existing
+ authorization or commitment and apply at most once per device.
+
+ The values are "default", "disabled", "adversary", and
+ "full". "default" lets the bus select the active trust level.
+ "disabled" prevents a functional driver from binding.
+ "adversary" requests operation with the restrictions that the
+ bus and DMA layers can enforce for a potentially malicious
+ device; it does not isolate DMA by itself. "full" requests
+ normal operation.
+
+ Before an operating level is committed, policy may change
+ freely until "disabled" is selected. Explicit "disabled"
+ blocks binding while allowing a later write of "adversary" or
+ "full". Repeated rejected probes preserve this opportunity.
+ Authorization immediately fixes the requested policy.
+ Returning to "default" or "disabled", or selecting another
+ mode, fails.
+
+ Successful resolution to an allowed level commits policy
+ and operating level before driver or DMA setup. Both remain
+ fixed for the device object's lifetime, including through
+ failed or deferred probes, unbind, reset, and driver reload.
+ Later binds must resolve to the same level, including when
+ the policy is "default". A resolver error or disabled result
+ before commitment leaves the policy's writability unchanged.
+
+ For a live device, writing the existing value succeeds
+ without changing state. Other forbidden writes fail with
+ EBUSY; invalid strings fail with EINVAL. Removed devices
+ reject writes with ENODEV. Writes do not trigger probing or
+ attestation.
+ Queued probes may proceed immediately after authorization,
+ so userspace must complete verification before granting it.
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd944..458f59a8af854c 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1259,6 +1259,70 @@ Kernel parameters
dell_smm_hwmon.fan_max=
[HW] Maximum configurable fan speed.
+ device_trust= [KNL]
+ Set requested device trust policy before driver probing,
+ including for drivers built into the kernel.
+ Format: <bus>:<device>=<policy>[,...]
+ The bus and device names must match exactly, as in
+ /sys/bus/<bus>/devices/<device>. The first colon separates
+ the bus name; further colons belong to the device name.
+ Policies are default, disabled, adversary, and full.
+ Entries override device_trust.default, device_trust.driver,
+ and bus-initialized policy, regardless of option order.
+ The last entry for a device wins. If specified more than
+ once, the last valid list replaces all earlier lists.
+ Malformed lists are warned about and ignored in full.
+ Names containing whitespace, control characters, commas,
+ equals signs, or double quotes cannot be specified.
+ Rules also apply to devices added after boot.
+ Explicit disabled blocks early binding but permits later
+ authorization by writing adversary or full to trust_policy.
+ Rejected disabled probes leave this opportunity intact.
+ Authorization fixes the policy before probing; adversary
+ or full at boot permits early probes without attestation.
+ Example:
+ device_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled
+ 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 | disabled | adversary | full }
+ default preserves bus-initialized policy (the default).
+ Other values override bus-initialized policy before
+ probing, including for built-in drivers.
+ The last valid value wins; invalid values are warned
+ about and ignored.
+ Without a matching driver rule, disabled prevents binding
+ until userspace authorizes each device by writing adversary
+ or full to trust_policy. Boot-critical devices need explicit
+ device or driver rules to operate before userspace. DMA
+ containment remains a bus and IOMMU responsibility.
+ 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>.
+ Policies are default, disabled, adversary, and full.
+ The name restrictions and repeated-list rules are the same
+ as device_trust=. Module aliases and wildcards are not
+ expanded. Rules remain available for hotplug and modules.
+ The first binding attempt with a matching rule applies it
+ once, before trust resolution and driver or DMA setup.
+ A per-device device_trust= entry or any successful write
+ to trust_policy, even of its current value, takes precedence.
+ Rules never replace an existing authorization or commitment.
+ A rejected disabled attempt leaves the device disabled until
+ userspace authorizes it, including across driver changes.
+ Driver rules apply after bus discovery and do not reconfigure
+ IOMMU domains. Adversary operation needs suitable existing
+ DMA configuration; see the device trust documentation.
+ Example: device_trust.driver=pci:e1000=disabled,pci:nvme=full
+
dfltcc= [HW,S390]
Format: { on | off | def_only | inf_only | always }
on: s390 zlib hardware support for compression on
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 00000000000000..1013784daeaca1
--- /dev/null
+++ b/Documentation/driver-api/driver-model/device-trust.rst
@@ -0,0 +1,294 @@
+.. 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``, ``disabled``,
+``adversary``, or ``full``. 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_DISABLED`` (``disabled``)
+ There is no active driver setup or binding. The requested policy decides
+ whether a later bind is allowed.
+
+``DEVICE_TRUST_ADVERSARY`` (``adversary``)
+ The kernel treats the device as potentially malicious. The bus and DMA
+ layers must enforce the restrictions needed to operate it.
+
+``DEVICE_TRUST_FULL`` (``full``)
+ 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 ``full`` resolve to
+``DEVICE_TRUST_FULL``, and ``adversary`` to ``DEVICE_TRUST_ADVERSARY``.
+Explicit ``disabled`` blocks binding. The bus remains responsible for
+enforcing the chosen level's restrictions.
+
+Admission and policy writes
+===========================
+
+Authorization is userspace's choice of adversary or full for a disabled
+device. Commitment happens when the kernel accepts a policy and operating
+level, before starting driver setup. Both limit later policy changes.
+
+The states below are internal; they 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 disabled enters WAITING.
+ * - WAITING
+ - Policy is disabled - no operating level is committed.
+ - Choose adversary or full to enter AUTHORIZED(P).
+ * - AUTHORIZED(P)
+ - Userspace has chosen adversary or full 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: disabled 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 full from WAITING locks that policy immediately.
+
+After boot initialization, the state transitions are::
+
+ SELECTABLE ----select disabled----> WAITING
+ | |
+ | authorize
+ | adversary/full
+ | |
+ | 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. Repeated rejected attempts leave the policy writable for later
+authorization.
+
+Active trust stays at L through DMA setup, probe, operation, removal, and
+``bus_type::dma_cleanup``. It returns to disabled 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.
+
+A driver can claim a USB interface before it is registered. The claim fails
+if the interface's own policy is disabled. A driver rule and other claims
+are admitted when the interface is registered, and a refusal then releases
+the claim. The policy of a claimed interface cannot be changed, and
+registration does not admit it while ``drivers_autoprobe`` is disabled.
+
+Boot policy
+===========
+
+Built-in drivers can probe before userspace starts. Use boot options to
+set their initial policy. All three options accept default, disabled,
+adversary, and full.
+
+``device_trust.default=`` sets policy for all devices. If omitted or set
+to default, it preserves bus policy. Other values replace bus policy,
+including topology and removability defaults. The last valid value wins;
+invalid values are reported and ignored.
+
+``device_trust=`` sets policy for individual devices::
+
+ device_trust=pci:0000:01:00.0=adversary,pci:0000:02:00.0=disabled
+
+Each entry is ``<bus>:<device>=<policy>``. 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 disabled. 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::
+
+ device_trust.driver=pci:e1000=disabled,pci:nvme=full
+
+Each entry is ``<bus>:<driver>=<policy>``. Use exact names from
+``/sys/bus/<bus>/drivers/<driver>``, not module names or aliases. The 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 disabled. It cannot override a per-device entry, any successful
+sysfs policy write, or an existing authorization or commitment. It applies
+only once per device object, even if resolution or setup fails.
+
+After a disabled driver rule applies, rejected probes and driver changes
+leave the device in WAITING until userspace authorizes it. Before any rule
+applies, a driver with no matching rule may still bind. Use global or
+per-device disabled to block every driver.
+
+For example, an initrd can hold all devices except one needed at boot::
+
+ device_trust.default=disabled device_trust=pci:0000:00:01.0=full
+
+Include exceptions for every device and supplier needed to reach the
+initrd and perform attestation. These exceptions allow operation before
+fresh initrd attestation. Unlisted devices stay disabled even if their
+bus would normally choose adversary or full.
+
+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. PCI does this during discovery, before header
+fixups, ACS setup, and IOMMU configuration. PCI applies topology and
+removability defaults only to default policy. A child's explicit boot
+entry overrides inherited policy. Driver rules wait until a candidate
+driver is known and cannot change these earlier discovery choices.
+
+All boot rules remain available for newly discovered devices. Reapplying
+initialization to an already registered device does not reset its state.
+These options are not runtime controls or proof of hardware identity.
+Disabled blocks early binding and allows later authorization. Adversary
+and full permit early binding without fresh initrd attestation.
+
+DMA containment
+===============
+
+Disabled blocks driver binding, not DMA that hardware starts on its own.
+The bus and IOMMU must provide containment while a device waits for
+authorization.
+
+For PCI devices whose policy is disabled or adversary at discovery, the
+IOMMU core selects a strict translated default domain for the whole group.
+AMD domain selection and Intel bypass checks apply the same restriction.
+This prepares a disabled device for later adversary authorization without
+granting permission to bind. Later full authorization may keep these
+restrictions. PCI does not record the ATS capability of such a device, even
+after a later full authorization, and sets ACS Translation Blocking for it
+where the path supports it.
+
+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.
+
+Binding does not check the IOMMU domain. Neither a driver rule nor a sysfs
+policy write reconfigures it, so a policy selected after discovery may
+bind with an identity or deferred-invalidation domain. Select the policy
+with ``device_trust=`` or ``device_trust.default=`` for it to apply at
+discovery. Outside PCI, discovery does not select a domain. Changing a
+group's domain type through sysfs is not checked against trust. A setup
+failure after commitment leaves the chosen mode fixed; it cannot fall back
+to full.
+
+Adversarial IOMMU mappings that are not aligned to IOMMU pages need an
+active SWIOTLB bounce-buffer pool. Low-memory x86 systems may need
+``swiotlb=force`` to keep this pool available. Without it, mappings can fail
+even after the driver binds.
+
+Drivers marked ``driver_managed_dma`` bypass the kernel DMA API's default
+domain handling and must provide their own DMA containment. Drivers that
+do not use DMA need no domain.
+
+Initrd attestation sequence
+===========================
+
+For a device that uses normal driver attachment, such as a PCI function:
+
+1. Boot with ``device_trust.default=disabled`` to hold all devices, or
+ ``device_trust=pci:0000:01:00.0=disabled`` to hold one. Add exceptions
+ needed to reach the initrd and provide DMA containment while waiting.
+ ``device_trust.driver=pci:e1000=disabled`` can instead hold a known
+ driver; global disabled also covers other drivers. Disabled 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 disabled
+ during verification and preparation. Leave it disabled 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 full
+ 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. An attachment deferred before the driver's probe
+ callback returns ``-EAGAIN``; retry later. A successful write does not
+ guarantee that a driver bound, so check the device's ``driver`` link.
+
+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 full 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 features such as peer-to-peer DMA,
+device memory, PASID/SVA, or paths that bypass the DMA API. General DMA
+containment belongs in bus and IOMMU code.
+
+Discovery code uses ``device_policy_is_adversarial()`` to check requested
+policy before binding or after unbind. This helper does not resolve
+default policy or report active trust.
+
+Code that needs the level fixed for the device's lifetime can use
+``device_get_committed_trust_level()``. It returns disabled 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, TDISP T=1/RUN, link
+encryption, private MMIO, verified evidence, or access to private versus
+shared memory. T=1 can use either adversary or full. In particular,
+``force_dma_unencrypted()`` describes DMA addressability and must not be
+derived from trust.
+
+Policy writes cannot revoke authorization or switch an operating device
+between adversary and full. 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 abeb4b36636b6c..af547cd13b2c41 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
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065de..a145aaaca2ef3f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8149,6 +8149,8 @@ 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/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/Makefile b/drivers/base/Makefile
index 435710f643a5c6..cb3bd5fa2287a0 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 866b2beb6baa28..5ff3217eb916ef 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,13 @@ struct driver_private {
};
#define to_driver(obj) container_of(obj, struct driver_private, kobj)
+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.
@@ -105,6 +113,9 @@ struct driver_private {
* dev_err_probe() for later retrieval via debugfs
* @device: pointer back to the struct device that this structure is
* associated with.
+ * @trust_state: policy admission state
+ * @trust_initialized: bus and boot policy have initialized 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.
@@ -121,6 +132,9 @@ struct device_private {
const struct device_driver *async_driver;
char *deferred_probe_reason;
struct device *device;
+ enum device_trust_state trust_state;
+ u8 trust_initialized:1;
+ u8 trust_policy_override:1;
u8 dead:1;
};
#define to_device_private_parent(obj) \
@@ -166,6 +180,29 @@ void bus_remove_driver(struct device_driver *drv);
void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
struct device *parent);
+extern const struct attribute_group device_trust_attr_group;
+
+#if IS_ENABLED(CONFIG_KUNIT)
+int device_trust_set_policy(struct device *dev, enum device_trust_policy policy);
+int device_trust_parse_cmdline(const char *options, const struct device *dev,
+ enum device_trust_policy *policy);
+void device_trust_apply_driver_policy(struct device *dev,
+ const struct device_driver *drv,
+ const char *options);
+bool device_trust_claim_allowed(const struct device *dev,
+ const struct device_driver *drv,
+ const char *device_options,
+ const char *driver_options);
+void device_trust_apply_boot_policy(struct device *dev,
+ enum device_trust_policy policy,
+ const char *options);
+#endif
+
+/* All active trust level transitions are serialized by the device lock. */
+void device_trust_init_state(struct device *dev);
+int device_trust_prepare(struct device *dev, const struct device_driver *drv);
+void device_trust_clear(struct device *dev);
+
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/bus.c b/drivers/base/bus.c
index 396097fdb31621..ecff32c3704b5f 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;
}
diff --git a/drivers/base/core.c b/drivers/base/core.c
index cbe05197d4a0bc..bd96d84c2ac2c6 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2989,10 +2989,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) {
@@ -3022,6 +3026,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:
@@ -3047,6 +3053,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)
@@ -3681,6 +3688,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);
@@ -4788,6 +4798,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/dd.c b/drivers/base/dd.c
index 52123251965de4..0d83f62195f699 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -545,13 +545,21 @@ int device_bind_driver(struct device *dev)
{
int ret;
+ if (dev->bus && dev->bus->trust_resolve)
+ return -EOPNOTSUPP;
+
+ ret = device_trust_prepare(dev, dev->driver);
+ if (ret)
+ return ret;
+
ret = driver_sysfs_add(dev);
if (!ret) {
device_links_force_bind(dev);
driver_bound(dev);
- }
- else
+ } else {
bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
+ device_trust_clear(dev);
+ }
return ret;
}
EXPORT_SYMBOL_GPL(device_bind_driver);
@@ -679,6 +687,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 */
@@ -745,6 +761,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;
}
@@ -771,6 +788,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;
}
@@ -1046,6 +1064,25 @@ static void __device_driver_lock(struct device *dev, struct device *parent)
device_lock(dev);
}
+/*
+ * __device_driver_trylock - __device_driver_lock() without sleeping
+ *
+ * Returns true if the locks were taken.
+ */
+static bool __device_driver_trylock(struct device *dev, struct device *parent)
+{
+ if (dev->type && dev->type->need_parent_lock && parent) {
+ if (!device_trylock(parent))
+ return false;
+ if (!device_trylock(dev)) {
+ device_unlock(parent);
+ return false;
+ }
+ return true;
+ }
+ return device_trylock(dev);
+}
+
/*
* __device_driver_unlock - release locks needed to manipulate dev->drv
* @dev: Device we will update driver info for
@@ -1195,19 +1232,17 @@ void device_initial_probe(struct device *dev)
subsys_put(sp);
}
-/**
- * device_driver_attach - attach a specific driver to a specific device
- * @drv: Driver to attach
- * @dev: Device to attach it to
- *
- * Manually attach driver to a device. Will acquire both @dev lock and
- * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
- */
-int device_driver_attach(const struct device_driver *drv, struct device *dev)
+static int __device_driver_attach(const struct device_driver *drv,
+ struct device *dev, bool trylock)
{
int ret;
- __device_driver_lock(dev, dev->parent);
+ if (trylock) {
+ if (!__device_driver_trylock(dev, dev->parent))
+ return -EBUSY;
+ } else {
+ __device_driver_lock(dev, dev->parent);
+ }
ret = __driver_probe_device(drv, dev);
__device_driver_unlock(dev, dev->parent);
@@ -1218,8 +1253,36 @@ int device_driver_attach(const struct device_driver *drv, struct device *dev)
return -EAGAIN;
return ret;
}
+
+/**
+ * device_driver_attach - attach a specific driver to a specific device
+ * @drv: Driver to attach
+ * @dev: Device to attach it to
+ *
+ * Manually attach driver to a device. Will acquire both @dev lock and
+ * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
+ */
+int device_driver_attach(const struct device_driver *drv, struct device *dev)
+{
+ return __device_driver_attach(drv, dev, false);
+}
EXPORT_SYMBOL_GPL(device_driver_attach);
+/**
+ * device_driver_try_attach - attach a specific driver without waiting for locks
+ * @drv: Driver to attach
+ * @dev: Device to attach it to
+ *
+ * Like device_driver_attach(), for callers that hold a lock which the holder
+ * of the @dev or @dev->parent lock may wait for. Returns -EBUSY, before any
+ * probing, if one of those locks is contended.
+ */
+int device_driver_try_attach(const struct device_driver *drv, struct device *dev)
+{
+ return __device_driver_attach(drv, dev, true);
+}
+EXPORT_SYMBOL_GPL(device_driver_try_attach);
+
static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
{
struct device *dev = _dev;
@@ -1360,6 +1423,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/test/Makefile b/drivers/base/test/Makefile
index 9ced7bbd569fc4..dfff5e1d83f7c8 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 00000000000000..590676648e4851
--- /dev/null
+++ b/drivers/base/test/device-trust-test.c
@@ -0,0 +1,1607 @@
+// 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/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#include "../base.h"
+
+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 remove_level;
+ enum device_trust_level cleanup_level;
+ const struct device_driver *resolve_driver;
+ const struct device_driver *reject_driver;
+ struct completion *released;
+
+ 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;
+ bool probe_adversarial;
+ bool device_initialized;
+ 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);
+
+ 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);
+
+ ctx->probe_count++;
+ ctx->probe_level = device_get_trust_level(dev);
+ ctx->probe_adversarial = device_is_adversarial(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);
+
+ 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 bool device_trust_test_plain_bus_registered;
+static bool device_trust_test_resolving_bus_registered;
+
+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_initialized)
+ return;
+
+ ctx->device_initialized = false;
+ if (ctx->device_registered) {
+ ctx->device_registered = false;
+ 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;
+
+ ctx->driver_registered = false;
+ driver_unregister(&ctx->drv);
+}
+
+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;
+ 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_FULL;
+ 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;
+ ctx->device_initialized = true;
+
+ ret = kunit_add_action_or_reset(test,
+ device_trust_test_device_unregister, ctx);
+ 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);
+
+ 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(struct kunit *test,
+ struct device_trust_test_context *ctx)
+{
+ int ret;
+
+ ctx->drv.name = test->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 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_FULL; 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+
+ 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_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->probe_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_FALSE(test, ctx->probe_adversarial);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_FULL);
+ 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_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_FULL);
+ 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_DISABLED);
+}
+
+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_FULL;
+ 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_DISABLED);
+ 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_DISABLED);
+
+ 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_TRUE(test, dev_can_match(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_DISABLED), 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);
+ device_trust_apply_boot_policy(&ctx->dev, DEVICE_TRUST_POLICY_DISABLED, NULL);
+ 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_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+
+ 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);
+ device_trust_apply_boot_policy(&ctx->dev, DEVICE_TRUST_POLICY_DISABLED, NULL);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, policy);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+ KUNIT_EXPECT_EQ(test, device_get_committed_trust_level(&ctx->dev),
+ level);
+ device_trust_expect_policy_locked(test, &ctx->dev);
+}
+
+static void device_trust_disabled_bind_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,
+ DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_ADVERSARY);
+}
+
+static void device_trust_disabled_full_bind_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_DISABLED,
+ DEVICE_TRUST_POLICY_DEFAULT, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_FULL);
+}
+
+static void device_trust_global_disabled_adversary_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_ADVERSARY);
+}
+
+static void device_trust_global_disabled_full_test(struct kunit *test)
+{
+ device_trust_authorize_test(test, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_POLICY_DISABLED, DEVICE_TRUST_POLICY_FULL,
+ DEVICE_TRUST_FULL);
+}
+
+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_DISABLED);
+ 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_DISABLED);
+ 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;
+ 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_test_device_register_parent(test, &device_trust_test_plain_bus,
+ DEVICE_TRUST_POLICY_DEFAULT,
+ DEVICE_TRUST_POLICY_DEFAULT, parent);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+ ctx->drv.probe_type = PROBE_PREFER_ASYNCHRONOUS;
+
+ /* The queued probe cannot acquire the parent until policy is disabled. */
+ device_lock(parent);
+ ret = device_trust_test_driver_register(test, ctx);
+ if (!ret)
+ ret = device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_DISABLED);
+ device_unlock(parent);
+ 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_DISABLED);
+ 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_DISABLED);
+}
+
+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);
+ pdev->dev.trust_policy = DEVICE_TRUST_POLICY_DISABLED;
+ 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_DISABLED);
+ 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_explicit_policy_mismatch_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_FULL;
+
+ 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, -EINVAL);
+ KUNIT_EXPECT_GT(test, ctx->resolve_count, 0);
+ KUNIT_EXPECT_EQ(test, ctx->resolve_policy,
+ DEVICE_TRUST_POLICY_ADVERSARY);
+ 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_ASSERT_EQ(test, device_trust_set_policy(&ctx->dev,
+ DEVICE_TRUST_POLICY_FULL), 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_FULL);
+}
+
+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_FULL), 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_DISABLED), 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 ?: -EINVAL);
+ 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_FULL);
+}
+
+static void device_trust_resolver_disabled_test(struct kunit *test)
+{
+ device_trust_resolver_retry_test(test, 0, DEVICE_TRUST_DISABLED);
+}
+
+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_DISABLED);
+ 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;
+
+ 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->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_DISABLED);
+ 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_DISABLED);
+ 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_DISABLED);
+ 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_FULL;
+ 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_DISABLED);
+ 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_DISABLED);
+}
+
+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_DISABLED);
+ 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_FULL), 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_FULL);
+
+ device_release_driver(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+}
+
+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_FULL);
+
+ device_release_driver(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->remove_count, 1);
+ KUNIT_EXPECT_EQ(test, ctx->remove_level, DEVICE_TRUST_FULL);
+ KUNIT_EXPECT_EQ(test, ctx->cleanup_count, 0);
+ KUNIT_EXPECT_EQ(test, device_get_trust_level(&ctx->dev),
+ DEVICE_TRUST_DISABLED);
+}
+
+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 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->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=full,usb:1-2=disabled,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_FULL },
+ { &device_trust_test_usb_bus, "1-2", DEVICE_TRUST_POLICY_DISABLED },
+ { &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_DISABLED;
+ 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=full",
+ "pc:0000:01:00.0=full",
+ "usb:0000:01:00.0=full",
+ "pci:0000:01:00.01=full",
+ "pci:0000:01:00=full",
+ "pci:0000:01:00.1=full",
+ };
+ 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=disabled,usb:1-1=full,pci:0000:01:00.0=full,usb:1-2=disabled";
+ static const char defaults[] =
+ "pci:0000:01:00.0=disabled,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_FULL);
+
+ 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=full", 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=disabled";
+ 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=full",
+ "pci:0000:01:00.0=full,",
+ "pci:0000:01:00.0=full,,usb:1-2=disabled",
+ ":0000:01:00.0=full",
+ "pci:=full",
+ "pci:0000:01:00.0=",
+ "pci=full",
+ "pci=bad:0000:01:00.0=full",
+ "pci:0000:01:00.0",
+ "pci:0000:01:00.0=unknown",
+ "pci:0000:01:00.0=full=disabled",
+ "pc i:0000:01:00.0=full",
+ "pci:0000:01:00.0 =full",
+ "pci:0000:01:00.0= full",
+ "pci:0000:01:00.0=full\n",
+ "pci:0000:01:00.0\t=full",
+ "pci:0000:01:00.0\001=full",
+ "p\"ci:0000:01:00.0=full",
+ "pci:0000:01:00.0=\"full\"",
+ "pci:0000:01:00.0=full,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_DISABLED] = "pci:0000:01:00.0=disabled",
+ [DEVICE_TRUST_POLICY_ADVERSARY] = "pci:0000:01:00.0=adversary",
+ [DEVICE_TRUST_POLICY_FULL] = "pci:0000:01:00.0=full",
+ };
+ 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_FULL; initial++) {
+ for (global = DEVICE_TRUST_POLICY_DEFAULT;
+ global <= DEVICE_TRUST_POLICY_FULL; 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=full");
+ 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_FULL);
+
+ device_trust_apply_boot_policy(dev, DEVICE_TRUST_POLICY_DISABLED,
+ "pci:0000:01:00.0=full");
+ KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_DISABLED);
+}
+
+static void device_trust_driver_precedence_test(struct kunit *test)
+{
+ static const char * const driver_rules[] = {
+ "pci:test=default", "pci:test=disabled",
+ "pci:test=adversary", "pci:test=full",
+ };
+ static const char * const device_rules[] = {
+ NULL, "pci:0000:01:00.0=default", "pci:0000:01:00.0=disabled",
+ "pci:0000:01:00.0=adversary", "pci:0000:01:00.0=full",
+ };
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ unsigned int initial, global, driver, device;
+ struct device *dev;
+ bool allowed, ok;
+
+ for (initial = DEVICE_TRUST_POLICY_DEFAULT;
+ initial <= DEVICE_TRUST_POLICY_FULL; initial++) {
+ for (global = DEVICE_TRUST_POLICY_DEFAULT;
+ global <= DEVICE_TRUST_POLICY_FULL; 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]);
+ /* The verdict for a claim before registration */
+ allowed = device_trust_claim_allowed(dev, &stub->drv,
+ device_rules[device],
+ driver_rules[driver]);
+ 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);
+ /* Driver rules are left to admission */
+ ok = !device || device - 1 != DEVICE_TRUST_POLICY_DISABLED;
+ KUNIT_EXPECT_EQ(test, allowed, ok);
+ }
+ }
+ }
+ }
+}
+
+static void device_trust_claim_allowed_test(struct kunit *test)
+{
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ struct device *dev;
+
+ dev = device_trust_test_pci_stub(stub, "0000:01:00.0", DEVICE_TRUST_POLICY_DISABLED);
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv, NULL, NULL));
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:other=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv,
+ "pci:0000:01:00.0=full", NULL));
+ KUNIT_EXPECT_FALSE(test, device_trust_claim_allowed(dev, &stub->drv,
+ "pci:0000:01:00.0=disabled",
+ "pci:test=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:test=full"));
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL,
+ "pci:test=disabled"));
+
+ dev = device_trust_test_pci_stub(stub, "0000:01:00.0", DEVICE_TRUST_POLICY_DEFAULT);
+ KUNIT_EXPECT_TRUE(test, device_trust_claim_allowed(dev, &stub->drv, NULL, NULL));
+}
+
+static void device_trust_driver_matching_test(struct kunit *test)
+{
+ static const char * const unmatched[] = {
+ NULL, "usb:test=disabled", "pc:test=disabled", "pcie:test=disabled",
+ "pci:tes=disabled", "pci:test1=disabled", "pci:te*=disabled",
+ "pci:test=disabled,pci:other=invalid", "pci:test=disabled,",
+ };
+ 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=disabled,pci:test=adversary");
+ device_unlock(dev);
+ KUNIT_EXPECT_EQ(test, 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_FULL; 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=disabled");
+ device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=full");
+ 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_DISABLED);
+
+ dev->p = &stub->private;
+ device_trust_init_state(dev);
+ KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_DEFAULT),
+ -EBUSY);
+ KUNIT_ASSERT_EQ(test, device_trust_set_policy(dev, DEVICE_TRUST_POLICY_FULL + 1),
+ -EINVAL);
+ device_lock(dev);
+ device_trust_apply_driver_policy(dev, &stub->drv, "pci:test=full");
+ device_unlock(dev);
+ KUNIT_EXPECT_EQ(test, dev->trust_policy, DEVICE_TRUST_POLICY_FULL);
+}
+
+static void device_trust_driver_authorize_test(struct kunit *test)
+{
+ const struct device_driver drv = {
+ .bus = &device_trust_test_plain_bus,
+ .name = "test",
+ };
+ struct device_trust_test_context *ctx;
+ const struct device_driver other = {
+ .bus = &device_trust_test_plain_bus,
+ .name = "other",
+ };
+ 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=disabled");
+ 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=full");
+ device_unlock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_DISABLED);
+ 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=full");
+ 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=full");
+ 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)
+{
+ const struct device_driver drv = {
+ .bus = &device_trust_test_resolving_bus,
+ .name = "test",
+ };
+ struct device_trust_test_context *ctx;
+
+ ctx = device_trust_test_device_register(
+ test, &device_trust_test_resolving_bus, DEVICE_TRUST_POLICY_DISABLED);
+ 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=full");
+ 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_FULL);
+ 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=disabled");
+ device_unlock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, ctx->dev.trust_policy, DEVICE_TRUST_POLICY_FULL);
+ 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_DISABLED), -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_DISABLED), 0);
+}
+
+static void device_trust_try_attach_test(struct kunit *test)
+{
+ struct device_trust_test_context *ctx;
+ struct device *parent;
+ bool parent_free;
+ 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_test_device_register_parent(test, &device_trust_test_plain_bus,
+ DEVICE_TRUST_POLICY_DEFAULT,
+ DEVICE_TRUST_POLICY_DEFAULT, parent);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+ ctx->match_ret = 0;
+ KUNIT_ASSERT_EQ(test, device_trust_test_driver_register(test, ctx), 0);
+
+ device_lock(parent);
+ KUNIT_EXPECT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), -EBUSY);
+ device_unlock(parent);
+
+ device_lock(&ctx->dev);
+ KUNIT_EXPECT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), -EBUSY);
+ device_unlock(&ctx->dev);
+
+ parent_free = device_trylock(parent);
+ if (parent_free)
+ device_unlock(parent);
+ KUNIT_EXPECT_TRUE(test, parent_free);
+ KUNIT_EXPECT_FALSE(test, device_is_bound(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 0);
+
+ KUNIT_ASSERT_EQ(test, device_driver_try_attach(&ctx->drv, &ctx->dev), 0);
+ KUNIT_EXPECT_TRUE(test, device_is_bound(&ctx->dev));
+ KUNIT_EXPECT_EQ(test, ctx->probe_count, 1);
+}
+
+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 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_disabled_bind_test),
+ KUNIT_CASE(device_trust_disabled_full_bind_test),
+ KUNIT_CASE(device_trust_global_disabled_adversary_test),
+ KUNIT_CASE(device_trust_global_disabled_full_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_explicit_policy_mismatch_test),
+ KUNIT_CASE(device_trust_resolver_failure_test),
+ KUNIT_CASE(device_trust_resolver_disabled_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_claim_allowed_test),
+ KUNIT_CASE(device_trust_driver_matching_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_try_attach_test),
+ KUNIT_CASE(device_trust_scan_continues_test),
+ {}
+};
+
+/* The fixtures set their own policy, which a live boot default would replace. */
+static int device_trust_test_init(struct kunit *test)
+{
+ struct device_trust_test_stub *stub = device_trust_test_stub_alloc(test);
+ struct device *dev = device_trust_test_pci_stub(stub, "device-trust-test",
+ DEVICE_TRUST_POLICY_DEFAULT);
+
+ device_trust_init(dev);
+ if (dev->trust_policy != DEVICE_TRUST_POLICY_DEFAULT)
+ kunit_skip(test, "device_trust.default= replaces the fixture policy");
+
+ return 0;
+}
+
+static int device_trust_test_suite_init(struct kunit_suite *suite)
+{
+ int ret;
+
+ ret = bus_register(&device_trust_test_plain_bus);
+ if (ret)
+ return ret;
+ device_trust_test_plain_bus_registered = true;
+
+ ret = bus_register(&device_trust_test_resolving_bus);
+ if (ret) {
+ device_trust_test_plain_bus_registered = false;
+ bus_unregister(&device_trust_test_plain_bus);
+ return ret;
+ }
+ device_trust_test_resolving_bus_registered = true;
+
+ return 0;
+}
+
+static void device_trust_test_suite_exit(struct kunit_suite *suite)
+{
+ if (device_trust_test_resolving_bus_registered) {
+ device_trust_test_resolving_bus_registered = false;
+ bus_unregister(&device_trust_test_resolving_bus);
+ }
+ if (device_trust_test_plain_bus_registered) {
+ device_trust_test_plain_bus_registered = false;
+ 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,
+};
+
+kunit_test_suite(device_trust_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
new file mode 100644
index 00000000000000..b68644d197b6fc
--- /dev/null
+++ b/drivers/base/trust.c
@@ -0,0 +1,455 @@
+// 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"
+
+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->p->trust_initialized)
+ return;
+
+ dev->p->trust_state = dev->trust_policy == DEVICE_TRUST_POLICY_DISABLED ?
+ DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
+ dev->p->trust_initialized = true;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_init_state);
+
+static bool device_trust_policy_matches(enum device_trust_policy policy,
+ enum device_trust_level level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ return true;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ return level == DEVICE_TRUST_DISABLED;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ return level == DEVICE_TRUST_ADVERSARY;
+ case DEVICE_TRUST_POLICY_FULL:
+ return level == DEVICE_TRUST_FULL;
+ default:
+ return false;
+ }
+}
+
+static int device_trust_resolve_default(enum device_trust_policy policy,
+ enum device_trust_level *level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ case DEVICE_TRUST_POLICY_FULL:
+ *level = DEVICE_TRUST_FULL;
+ return 0;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ *level = DEVICE_TRUST_DISABLED;
+ return 0;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ *level = DEVICE_TRUST_ADVERSARY;
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+int device_trust_prepare(struct device *dev, const struct device_driver *drv)
+{
+ enum device_trust_policy policy;
+ enum device_trust_level level = DEVICE_TRUST_DISABLED;
+ int ret;
+
+ device_lock_assert(dev);
+
+ if (WARN_ON_ONCE(READ_ONCE(dev->trust_level) != DEVICE_TRUST_DISABLED))
+ return -EBUSY;
+ device_trust_init_driver_policy(dev, drv);
+ policy = dev->trust_policy;
+ if (policy == DEVICE_TRUST_POLICY_DISABLED)
+ return -EPERM;
+ if (dev->bus && dev->bus->trust_resolve)
+ ret = dev->bus->trust_resolve(dev, drv, policy, &level);
+ else
+ ret = device_trust_resolve_default(policy, &level);
+ if (ret)
+ return ret;
+
+ if (!device_trust_policy_matches(policy, level))
+ return -EINVAL;
+ if (level == DEVICE_TRUST_DISABLED)
+ return -EPERM;
+ if (level != DEVICE_TRUST_ADVERSARY && level != DEVICE_TRUST_FULL)
+ return -EINVAL;
+ 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;
+}
+
+void device_trust_clear(struct device *dev)
+{
+ device_lock_assert(dev);
+ WRITE_ONCE(dev->trust_level, DEVICE_TRUST_DISABLED);
+}
+
+/**
+ * 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_DISABLED 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_get_committed_trust_level - Return the lifetime trust commitment
+ * @dev: device to inspect
+ *
+ * Return: %DEVICE_TRUST_DISABLED 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
+ *
+ * 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_DISABLED] = "disabled",
+ [DEVICE_TRUST_POLICY_ADVERSARY] = "adversary",
+ [DEVICE_TRUST_POLICY_FULL] = "full",
+};
+
+static const char * const device_trust_level_names[] = {
+ [DEVICE_TRUST_DISABLED] = "disabled",
+ [DEVICE_TRUST_ADVERSARY] = "adversary",
+ [DEVICE_TRUST_FULL] = "full",
+};
+
+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)]);
+}
+
+VISIBLE_IF_KUNIT int device_trust_set_policy(struct device *dev,
+ enum device_trust_policy policy)
+{
+ int ret = 0;
+
+ if (policy < DEVICE_TRUST_POLICY_DEFAULT ||
+ policy > DEVICE_TRUST_POLICY_FULL)
+ return -EINVAL;
+
+ device_lock(dev);
+ if (dev->p->dead) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+ if (policy == dev->trust_policy)
+ goto set_policy;
+
+ /* A driver that claimed the device before registration was not admitted. */
+ if (READ_ONCE(dev->driver)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ switch (dev->p->trust_state) {
+ case DEVICE_TRUST_SELECTABLE:
+ if (policy == DEVICE_TRUST_POLICY_DISABLED)
+ dev->p->trust_state = DEVICE_TRUST_WAITING;
+ break;
+ case DEVICE_TRUST_WAITING:
+ if (policy != DEVICE_TRUST_POLICY_ADVERSARY &&
+ policy != DEVICE_TRUST_POLICY_FULL) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+ dev->p->trust_state = DEVICE_TRUST_AUTHORIZED;
+ break;
+ default:
+ ret = -EBUSY;
+ goto unlock;
+ }
+set_policy:
+ WRITE_ONCE(dev->trust_policy, policy);
+ dev->p->trust_policy_override = true;
+unlock:
+ device_unlock(dev);
+ return ret;
+}
+EXPORT_SYMBOL_IF_KUNIT(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;
+
+ 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 const char *device_trust_cmdline __ro_after_init;
+static const char *device_trust_driver_cmdline __ro_after_init;
+static enum device_trust_policy device_trust_default_policy __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 || !*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 = 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);
+
+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;
+
+ device_lock_assert(dev);
+ if (!options || !drv || dev->p->trust_policy_override ||
+ (dev->p->trust_state != DEVICE_TRUST_SELECTABLE &&
+ dev->p->trust_state != DEVICE_TRUST_WAITING))
+ return;
+
+ if (device_trust_parse_policy(options, drv->bus ? drv->bus->name : NULL,
+ drv->name, &policy) <= 0)
+ return;
+
+ WRITE_ONCE(dev->trust_policy, policy);
+ dev->p->trust_state = policy == DEVICE_TRUST_POLICY_DISABLED ?
+ 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);
+}
+
+/*
+ * A matching driver rule is applied, together with its admission state, when
+ * the device is registered, so only the device's own policy rejects a claim.
+ */
+VISIBLE_IF_KUNIT bool device_trust_claim_allowed(const struct device *dev,
+ const struct device_driver *drv,
+ const char *device_options,
+ const char *driver_options)
+{
+ enum device_trust_policy policy = READ_ONCE(dev->trust_policy);
+ bool device_rule = device_options &&
+ device_trust_parse_cmdline(device_options, dev, &policy) > 0;
+
+ if (!device_rule && driver_options && drv &&
+ device_trust_parse_policy(driver_options,
+ drv->bus ? drv->bus->name : NULL,
+ drv->name, &policy) > 0)
+ return true;
+
+ return policy != DEVICE_TRUST_POLICY_DISABLED;
+}
+EXPORT_SYMBOL_IF_KUNIT(device_trust_claim_allowed);
+
+/**
+ * device_trust_may_claim - Test whether a driver may claim an unregistered device
+ * @dev: device with its bus, name, and boot policy set
+ * @drv: driver that claims the device
+ *
+ * A driver can claim a device before it is registered, which skips the bind
+ * attempt that admits it. Reject a claim that the device's own policy forbids.
+ *
+ * Return: False if the policy of @dev rejects the claim.
+ */
+bool device_trust_may_claim(const struct device *dev,
+ const struct device_driver *drv)
+{
+ return device_trust_claim_allowed(dev, drv, device_trust_cmdline,
+ device_trust_driver_cmdline);
+}
+EXPORT_SYMBOL_GPL(device_trust_may_claim);
+
+static int __init device_trust_setup(char *str)
+{
+ 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_driver_setup(char *str)
+{
+ if (device_trust_parse_policy(str, NULL, 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,
+ ARRAY_SIZE(device_trust_policy_names), str);
+
+ 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);
+
+VISIBLE_IF_KUNIT void device_trust_apply_boot_policy(struct device *dev,
+ enum device_trust_policy policy,
+ const char *options)
+{
+ if (dev->p && dev->p->trust_initialized)
+ return;
+
+ if (policy != DEVICE_TRUST_POLICY_DEFAULT)
+ dev->trust_policy = 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);
+
+/**
+ * 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)
+{
+ device_trust_apply_boot_policy(dev, device_trust_default_policy,
+ device_trust_cmdline);
+}
+EXPORT_SYMBOL_GPL(device_trust_init);
diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 92a78a7e603967..8fe6bd1bff9069 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2993,10 +2993,13 @@ static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
return hci_recv_frame(hdev, skb);
}
+/* usb_lock_device_for_reset() gives up after one second of contention */
+#define BTUSB_MTK_ISO_LOCK_TRIES 5
+
static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
{
struct btmtk_data *btmtk_data;
- int err;
+ int err, i;
if (!data->hdev)
return;
@@ -3011,22 +3014,37 @@ static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
}
/*
- * 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.
+ * Disconnect holds the device lock while draining this setup work, so
+ * the lock must be taken with disconnect-aware acquisition. Hold a PM
+ * reference so the device cannot suspend first. A slow interface
+ * registration can keep the lock past the one-second limit of a single
+ * attempt, so retry a few times, but not without bound: a teardown
+ * that holds the lock may be waiting for this work.
*/
- 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);
+ err = usb_autopm_get_interface(data->intf);
+ if (!err) {
+ 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) {
+ err = usb_driver_claim_interface(&btusb_driver,
+ btmtk_data->isopkt_intf,
+ data);
+ if (!err) {
+ 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);
+ }
if (err < 0) {
btmtk_data->isopkt_intf = NULL;
bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
- return;
}
-
- set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
- init_usb_anchor(&btmtk_data->isopkt_anchor);
}
static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
diff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c
index a118a89a7779d6..0a030bf5226907 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)
diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
index 9707b155bc9475..c9efec2dc55da5 100644
--- a/drivers/input/gameport/gameport.c
+++ b/drivers/input/gameport/gameport.c
@@ -214,26 +214,7 @@ static void gameport_run_poll_handler(struct timer_list *t)
static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
{
- int error;
-
- gameport->dev.driver = &drv->driver;
- if (drv->connect(gameport, drv)) {
- gameport->dev.driver = NULL;
- return -ENODEV;
- }
-
- 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;
+ return device_driver_attach(&drv->driver, &gameport->dev);
}
static void gameport_find_driver(struct gameport *gameport)
diff --git a/drivers/input/serio/serio.c b/drivers/input/serio/serio.c
index 54dd26249b027e..604440244c1ca7 100644
--- a/drivers/input/serio/serio.c
+++ b/drivers/input/serio/serio.c
@@ -81,28 +81,10 @@ static int serio_match_port(const struct serio_device_id *ids, struct serio *ser
static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
{
- int error;
-
- if (serio_match_port(drv->id_table, serio)) {
-
- serio->dev.driver = &drv->driver;
- if (serio_connect_driver(serio, drv)) {
- serio->dev.driver = NULL;
- return -ENODEV;
- }
+ if (!serio_match_port(drv->id_table, serio))
+ return 0;
- 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;
+ return device_driver_attach(&drv->driver, &serio->dev);
}
static void serio_find_driver(struct serio *serio)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 960e1dc05ec69b..e358bbb1f2baa0 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 78742aeba932de..a48fb511bfe6fc 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 5a2e705a2f2e02..66942a91b992f0 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;
@@ -4124,9 +4120,6 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
static void quirk_iommu_rwbf(struct pci_dev *dev)
{
- if (risky_device(dev))
- return;
-
/*
* Mobile 4 Series Chipset neglects to set RWBF capability,
* but needs it. Same seems to hold for the desktop versions.
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index b486b8bbd1fcdc..8be2a246d49036 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1879,7 +1879,7 @@ static int iommu_get_def_domain_type(struct iommu_group *group,
static int iommu_get_default_domain_type(struct iommu_group *group,
int target_type)
{
- struct device *untrusted = NULL;
+ struct device *restricted = NULL;
struct group_device *gdev;
int driver_type = 0;
@@ -1901,14 +1901,15 @@ 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;
- untrusted = gdev->dev;
+ restricted = gdev->dev;
}
}
@@ -1924,11 +1925,11 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
driver_type = IOMMU_DOMAIN_IDENTITY;
}
- if (untrusted) {
+ if (restricted) {
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",
+ restricted,
+ "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/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 5d4508bcfbe991..8cc0de755bd082 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1912,7 +1912,9 @@ 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;
int irq = phydev->irq;
+ bool bind_genphy;
int err;
/* For Ethernet device drivers that register their own MDIO bus, we
@@ -1939,16 +1941,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 +1958,20 @@ 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) {
+ /*
+ * RTNL is held, and a concurrent bind of a specific driver may
+ * wait for it with the device lock held. Never wait for that
+ * lock, and let the caller retry.
+ */
+ err = device_driver_try_attach(drv, d);
+ if (err == -EBUSY) {
+ /* Another bind owns the PHY, so nothing was probed here. */
+ irq = phydev->irq;
+ err = -EPROBE_DEFER;
+ } else if (err == -EAGAIN) {
+ err = -EPROBE_DEFER;
+ }
if (err)
goto error_module_put;
}
@@ -2071,8 +2082,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 +3822,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 +3960,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 +3989,7 @@ static int phy_remove(struct device *dev)
phy_device_reset(phydev, 1);
phydev->drv = NULL;
+ phydev->is_genphy_driven = 0;
return 0;
}
diff --git a/drivers/net/wireless/virtual/mac80211_hwsim_main.c b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
index e53714b81ec2c6..b796da3dd54b36 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);
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 9cb23780093dd3..c8c96acc263434 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 7c6f2aba81443d..b08adba03f96b1 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_ats_permitted(dev))
caps->ctrl |= (dev->acs_capabilities & PCI_ACS_TB);
}
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f46..139e6f7b4e0226 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -1738,24 +1738,27 @@ 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);
+ struct pci_dev *parent = pci_physfn(dev);
+
+ if (dev->dev.trust_policy != DEVICE_TRUST_POLICY_DEFAULT)
+ return;
+
+ if (parent == 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 +2085,8 @@ int pci_setup_device(struct pci_dev *dev)
set_pcie_cxl(dev);
- set_pcie_untrusted(dev);
+ pci_set_default_trust_policy(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 7f1f47ea2ee415..9ed3e7660bab25 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_ats_permitted(dev))
ctrl |= (cap & PCI_ACS_TB);
pci_write_config_dword(dev, pos + INTEL_SPT_ACS_CTRL, ctrl);
diff --git a/drivers/pnp/card.c b/drivers/pnp/card.c
index df5e3d3cbf6c05..9aaaa03d5ffa9b 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;
}
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d3a61ebc6afb00..4620667a11832d 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -563,6 +563,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,
if (!iface->authorized)
return -ENODEV;
+ /* an unregistered interface is admitted later, so refuse what would fail */
+ if (!device_is_registered(dev) &&
+ !device_trust_may_claim(dev, &driver->driver))
+ return -EPERM;
+
dev->driver = &driver->driver;
usb_set_intfdata(iface, data);
iface->needs_binding = 0;
@@ -584,8 +589,11 @@ int usb_driver_claim_interface(struct usb_driver *driver,
/* if interface was already added, bind now; else let
* the future device_add() bind it, bypassing probe()
*/
- if (device_is_registered(dev))
+ if (device_is_registered(dev)) {
+ device_lock(dev);
retval = device_bind_driver(dev);
+ device_unlock(dev);
+ }
if (retval) {
dev->driver = NULL;
@@ -649,6 +657,26 @@ void usb_driver_release_interface(struct usb_driver *driver,
}
EXPORT_SYMBOL_GPL(usb_driver_release_interface);
+/*
+ * The driver core refused to bind an interface that a sibling claimed before
+ * it was registered, and dropped the claim. Let the claimant release the
+ * interface instead of operating it with a bound interface but no driver.
+ */
+void usb_driver_release_refused_claim(struct usb_interface *iface,
+ struct device_driver *claimant)
+{
+ struct device *dev = &iface->dev;
+
+ device_lock(dev);
+ if (!dev->driver && iface->condition == USB_INTERFACE_BOUND) {
+ dev->driver = claimant;
+ usb_unbind_interface(dev);
+ dev_set_drvdata(dev, NULL);
+ dev->driver = NULL;
+ }
+ device_unlock(dev);
+}
+
/* 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/message.c b/drivers/usb/core/message.c
index a738d03c23eacc..2d75b85b2d139b 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -2209,6 +2209,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
@@ -2264,6 +2266,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;
if (intf->dev.of_node &&
!of_device_is_available(intf->dev.of_node)) {
@@ -2277,7 +2280,14 @@ 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);
+ claimant = intf->dev.driver;
+ if (claimant && !try_module_get(claimant->owner))
+ claimant = NULL;
ret = device_add(&intf->dev);
+ if (!ret && claimant && !intf->dev.driver)
+ usb_driver_release_refused_claim(intf, claimant);
+ if (claimant)
+ module_put(claimant->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 a9b37aeb515bef..293340a48b6c57 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -49,6 +49,8 @@ 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_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);
diff --git a/include/linux/device.h b/include/linux/device.h
index 429e39f647cf90..870ce149249053 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,11 @@ 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 while a driver is bound, see
+ * device_get_trust_level().
+ * @trust_committed_level: Operating level retained across bind transactions,
+ * see device_get_committed_trust_level().
* @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 +804,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);
};
@@ -925,6 +934,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;
+}
+
/* Get the wakeup routines, which depend on struct device */
#include <linux/pm_wakeup.h>
@@ -1314,6 +1328,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_try_attach(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);
diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h
index 9ef7538a9a7f2e..ba2b5743a47cd5 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 00000000000000..19052e3301729d
--- /dev/null
+++ b/include/linux/device/trust.h
@@ -0,0 +1,48 @@
+/* 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_DISABLED: The device cannot be operated by a driver
+ * @DEVICE_TRUST_ADVERSARY: Operate the device with adversarial restrictions
+ * @DEVICE_TRUST_FULL: Operate the device with the usual kernel privileges
+ *
+ * Active trust levels are discrete states, not an ordered privilege scale.
+ */
+enum device_trust_level {
+ DEVICE_TRUST_DISABLED,
+ DEVICE_TRUST_ADVERSARY,
+ DEVICE_TRUST_FULL,
+};
+
+/**
+ * enum device_trust_policy - requested device trust policy
+ * @DEVICE_TRUST_POLICY_DEFAULT: Let the bus select the active trust level
+ * @DEVICE_TRUST_POLICY_DISABLED: Request that the device remain disabled
+ * @DEVICE_TRUST_POLICY_ADVERSARY: Request adversarial operation
+ * @DEVICE_TRUST_POLICY_FULL: 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_DISABLED,
+ DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_POLICY_FULL,
+};
+
+void device_trust_init(struct device *dev);
+bool device_trust_may_claim(const struct device *dev,
+ const struct device_driver *drv);
+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_ */
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96c1..087c63b177c854 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
@@ -616,6 +608,28 @@ 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_DISABLED ||
+ policy == DEVICE_TRUST_POLICY_ADVERSARY;
+}
+
+static inline bool pci_dev_ats_permitted(const struct pci_dev *pdev)
+{
+ enum device_trust_level level = device_get_trust_level(&pdev->dev);
+ enum device_trust_policy policy = READ_ONCE(pdev->dev.trust_policy);
+
+ if (level == DEVICE_TRUST_DISABLED)
+ return policy != DEVICE_TRUST_POLICY_ADVERSARY &&
+ policy != DEVICE_TRUST_POLICY_DISABLED;
+
+ return level == DEVICE_TRUST_FULL;
+}
+
#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)
|
| 21/3 | 2026/11/08 04:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51924 output: 29 thoughts: 1978 Thoughts: **KMSAN Evaluation for Device Trust Patch Series** Okay, let's break this down. The task is to determine if this "device trust" patch series warrants a dedicated KMSAN fuzzing session. The key is distinguishing between KASAN and KMSAN. KASAN is already enabled in the standard builds, covering most memory safety issues: out-of-bounds, use-after-free, double-free, invalid frees, locking bugs, and memory corruption. KMSAN, however, targets a specific niche: reads of *uninitialized* memory. This includes uninitialized stack, heap, or page allocations, and kernel-to-user memory leaks. My rule is simple: if KASAN, LOCKDEP, or standard detectors can catch a bug, we *don't* need KMSAN. KMSAN is expensive, and it should only be used if we're dealing with potential uninitialized memory risks *that KMSAN specifically detects*. Looking at the criteria for setting `NeedsKMSAN=true`, I need to scrutinize if the patch: 1. Introduces kernel structures sent to userspace, where uninitialized fields could leak. 2. Contains conditional logic or branching based on uninitialized variables. 3. Allocates and initializes complex structures in a way that allows later reads of uninitialized fields. 4. Modifies bounds checks in a way that allows surrounding code to access uninitialized bytes. Conversely, I set `NeedsKMSAN=false` if: * The code primarily addresses out-of-bounds, overflows, NULL dereferences, or use-after-free issues (KASAN handles those). * All stack/heap structures are zeroed/initialized before being read/copied. * The patch *doesn't* introduce any uninitialized memory risks. Now, let's examine the patch series. It introduces a "device trust" framework in the driver core. From the brief review I've done, I see significant changes to `struct device` and associated functions. My initial concern is how `struct device` is allocated and initialized. Specifically, the new fields: `trust_policy`, `trust_level`, and `trust_committed_level`. Are they initialized? The text indicates that devices are primarily allocated using `kzalloc()` or are embedded in `kzalloc()`-ed structures. Looking at the `include/linux/device.h`, those fields are defined as an enum, and since device struct are zero-initialized by `kzalloc()`, it means the trust fields will start at zero, or `DEVICE_TRUST_POLICY_DEFAULT` and `DEVICE_TRUST_DISABLED` respectively. This is a crucial point. Now I need to do a code walk. Okay, I need to verify how `dev->p` is allocated, so I'll check `drivers/base/core.c` for calls to `kzalloc` or similar. I need to make sure the allocations for the `struct device_private` are handled and that there are no gaps here. The presence of functions like `device_trust_init()` and `device_trust_init_state()` is something I need to evaluate. I need to trace where the trust fields are populated and how they're used. I need to examine the new functions to see where uninitialized values can be read. Since there are several new functions related to this framework, such as `device_trust_policy_matches()`, `device_trust_resolve_default()`, `device_trust_prepare()`, etc., I need to trace those functions, see if there are any uninitialized reads, and if they will leak any data to the user. Therefore, because of the introduction of these new `struct device` fields, and the number of new functions that deal with them, I need to do a thorough code review. While the zero-initialization of the `struct device` itself is reassuring, I'm not convinced we are in the clear. I need to check all of the places where these trust fields are touched and how they are handled, particularly in the context of kernel-to-user data transfers or in conditional logic to ensure no info leaks or uninitialized memory reads occur. Therefore, I'm going to set `NeedsKMSAN=false` *for now*, pending a thorough, more detailed code review. However, this patch series *does* warrant careful scrutiny from the KMSAN perspective. A more in-depth review will determine if a dedicated KMSAN session is necessary. |
| 22/3 | 2026/11/08 04:37 | tool | grepper |
0mArgs:{
"Expression": "device_private_init",
"PathPrefixes": [
"drivers/base"
]
}Results: {
"Output": "drivers/base/core.c=3602=static void device_remove_sys_dev_entry(struct device *dev)\n--\ndrivers/base/core.c-3612-\ndrivers/base/core.c:3613:static int device_private_init(struct device *dev)\ndrivers/base/core.c-3614-{\n--\ndrivers/base/core.c=3652=int device_add(struct device *dev)\n--\ndrivers/base/core.c-3665-\tif (!dev-\u003ep) {\ndrivers/base/core.c:3666:\t\terror = device_private_init(dev);\ndrivers/base/core.c-3667-\t\tif (error)\n"
} |
| 23/3 | 2026/11/08 04:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55077 output: 36 thoughts: 21 Thoughts: Here's a summary of my thought process: **Diving into the Device Initialization** Okay, let's get down to brass tacks. I need to understand what's going on with device initialization, specifically the private initialization. My gut tells me that `drivers/base/core.c` is the right place to start looking. I'm going to jump into `device_private_init()` and see what's happening under the hood. Time to trace the execution and see how the private data for a device is handled during its initialization sequence. This is a critical area, so I'll be keeping a keen eye out for potential race conditions or resource conflicts. Understanding this thoroughly will give me a solid foundation for troubleshooting or extending device driver functionality. Let's see what secrets this function holds. |
| 24/3 | 2026/11/08 04:37 | tool | read-file |
0mArgs:{
"File": "drivers/base/core.c",
"FirstLine": 3612,
"LineCount": 20
}Results: {
"Contents": "3612:\t\n3613:\tstatic int device_private_init(struct device *dev)\n3614:\t{\n3615:\t\tdev-\u003ep = kzalloc_obj(*dev-\u003ep);\n3616:\t\tif (!dev-\u003ep)\n3617:\t\t\treturn -ENOMEM;\n3618:\t\tdev-\u003ep-\u003edevice = dev;\n3619:\t\tklist_init(\u0026dev-\u003ep-\u003eklist_children, klist_children_get,\n3620:\t\t\t klist_children_put);\n3621:\t\tINIT_LIST_HEAD(\u0026dev-\u003ep-\u003edeferred_probe);\n3622:\t\treturn 0;\n3623:\t}\n3624:\t\n3625:\t/**\n3626:\t * device_add - add device to device hierarchy.\n3627:\t * @dev: device.\n3628:\t *\n3629:\t * This is part 2 of device_register(), though may be called\n3630:\t * separately _iff_ device_initialize() has been called separately.\n3631:\t *\n"
} |
| 25/3 | 2026/11/08 04:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55600 output: 43 thoughts: 2341 Thoughts: **Security Analysis of Device Trust Level and Policy** Okay, so I'm diving into this device trust code, and the first thing I notice is how `dev->p` is allocated and then the flow in `trust_show` and `trust_policy_show`. They use `sysfs_emit()`, which formats into a sysfs-provided buffer. This makes me immediately think: is there a potential out-of-bounds access if the index for `device_get_trust_level(dev)` or `dev->trust_policy` goes haywire? I quickly check the bounds: `device_trust_level_names` is explicitly defined, and `dev->trust_level` is set using `WRITE_ONCE` with a `level` variable that's carefully checked to be either `DEVICE_TRUST_ADVERSARY` or `DEVICE_TRUST_FULL`, or it gets set to `DEVICE_TRUST_DISABLED` during initialization. `dev->trust_policy` has similar checks in `device_trust_set_policy`, `trust_policy_store`, and `device_trust_parse_policy` making sure it stays within a valid range. So, that's pretty robust. Then I get to `device_trust_prepare`. Here's where things get interesting. I see `dev->trust_level` is initialized to `DEVICE_TRUST_DISABLED`, which should prevent uninitialized read issues. And, even if `dev->bus->trust_resolve` doesn't write to the `level` variable in this function, that variable has a defined value. The code validates `level` before moving on. That's good. Next, I'm thinking about the command line parsing, `device_trust_parse_cmdline` and `device_trust_parse_policy`. I look specifically at `device_trust_parse_policy` and see that it initializes `selected` to `DEVICE_TRUST_POLICY_DEFAULT` before any parsing. If it finds a match, `*policy` gets assigned to the matched index (`i`). The key is that if it returns after a successful match, it writes to `policy` meaning that there isn't an uninitialized read. The same approach applies to `device_trust_apply_driver_policy`. If the parse is successful (return > 0), `policy` gets properly assigned. So, no uninitialized reads there either. I also check `device_trust_claim_allowed`, which uses `READ_ONCE(dev->trust_policy)` to initialize `policy`. So, it's either initialized to its previous value or set to a new one if `device_trust_parse_cmdline` returns > 0. So no uninitialized reads there either. Finally, I'm looking at `iommu_dma_map_swiotlb` and its bounce-buffer logic. It checks if the device is adversarial. So this is all pretty well-defined. |
| 26/3 | 2026/11/08 04:37 | tool | read-file |
0mArgs:{
"File": "drivers/iommu/dma-iommu.c",
"FirstLine": 1175,
"LineCount": 45
}Results: {
"Contents": "1175:\t\n1176:\t\ttrace_swiotlb_bounced(dev, phys, size);\n1177:\t\n1178:\t\tphys = swiotlb_tbl_map_single(dev, phys, size, iova_mask(iovad), dir,\n1179:\t\t\t\t\t \u0026attrs);\n1180:\t\n1181:\t\t/*\n1182:\t\t * Untrusted devices should not see padding areas with random leftover\n1183:\t\t * kernel data, so zero the pre- and post-padding.\n1184:\t\t * swiotlb_tbl_map_single() has initialized the bounce buffer proper to\n1185:\t\t * the contents of the original memory buffer.\n1186:\t\t */\n1187:\t\tif (phys != (phys_addr_t)DMA_MAPPING_ERROR \u0026\u0026\n1188:\t\t device_is_adversarial(dev)) {\n1189:\t\t\tsize_t start, virt = (size_t)phys_to_virt(phys);\n1190:\t\n1191:\t\t\t/* Pre-padding */\n1192:\t\t\tstart = iova_align_down(iovad, virt);\n1193:\t\t\tmemset((void *)start, 0, virt - start);\n1194:\t\n1195:\t\t\t/* Post-padding */\n1196:\t\t\tstart = virt + size;\n1197:\t\t\tmemset((void *)start, 0, iova_align(iovad, start) - start);\n1198:\t\t}\n1199:\t\n1200:\t\treturn phys;\n1201:\t}\n1202:\t\n1203:\t/*\n1204:\t * Checks if a physical buffer has unaligned boundaries with respect to\n1205:\t * the IOMMU granule. Returns non-zero if either the start or end\n1206:\t * address is not aligned to the granule boundary.\n1207:\t */\n1208:\tstatic inline size_t iova_unaligned(struct iova_domain *iovad, phys_addr_t phys,\n1209:\t\t\t\t\t size_t size)\n1210:\t{\n1211:\t\treturn iova_offset(iovad, phys | size);\n1212:\t}\n1213:\t\n1214:\tdma_addr_t iommu_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,\n1215:\t\t\tenum dma_data_direction dir, unsigned long attrs)\n1216:\t{\n1217:\t\tbool coherent = dev_is_dma_coherent(dev);\n1218:\t\tint prot = dma_info_to_prot(dir, coherent, attrs);\n1219:\t\tstruct iommu_domain *domain = iommu_get_dma_domain(dev);\n"
} |
| 27/3 | 2026/11/08 04:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 59361 output: 32 thoughts: 1536 Thoughts: **Code Review: Trust Level Changes** Okay, so the initial change in `iommu_dma_map_swiotlb` was straightforward: swapping out `dev_is_untrusted(dev)` with `device_is_adversarial(dev)`. This seems to be the core of the security enhancement. I need to make sure this doesn't introduce vulnerabilities. First, I need to remind myself that DMA bounce buffers are hardware-facing and not directly copied to userspace via `copy_to_user`. KMSAN's behavior with DMA memory is also relevant; it generally ignores it. The next critical step is to methodically check for areas where data *is* being copied to userspace, or where kernel data could be leaked or modified. I'm focusing on potential attack vectors through `copy_to_user`, `put_user`, netlink SKB attributes, ioctl output, socket options, and BPF buffers. Are any new avenues opened? I've carefully examined the patch diff. I've ruled out the `Documentation`, `MAINTAINERS`, and `Makefile` changes as irrelevant to data flow. I also know that `drivers/base/bus.c`'s `drivers_probe_store` doesn't leak to userspace. * `drivers/base/core.c` changes are about adding trust attributes, which seems okay. They initialize using `device_trust_init`, `device_trust_init_state`, and add `device_trust_attr_group` to the device. * `drivers/base/dd.c` appears safe because all changes are to locking and attachment/detachment. * `drivers/base/test/...` is a KUnit test. * In `drivers/base/trust.c`, `trust_show` uses `sysfs_emit`, which looks safe. It prints `device_get_trust_level(dev)`, which calls `READ_ONCE(dev->trust_level)`. This is where I have to make sure `dev->trust_level` is properly initialized. * `drivers/bluetooth/btusb_main.c`, `drivers/cdx/cdx.c`, `drivers/input/gameport/gameport.c`, `drivers/input/serio/serio.c`, and the various `drivers/iommu/...` changes are similarly safe. * `drivers/net/phy/phy_device.c` uses `device_driver_try_attach`. I'll revisit this more closely. * `drivers/net/wireless/virtual/mac80211_hwsim_main.c` is just about device locking and unlocking. * `drivers/pci/...` is safe because all calls are related to trust policy, ATS and PCI quirks. * `drivers/pnp/card.c` also looks safe. * `drivers/usb/core/driver.c` has `usb_driver_claim_interface`, which calls `device_trust_may_claim`. * `drivers/usb/core/message.c` has claims handling. * `include/...` is just about struct device, bus_type, and PCI helper additions. Now, let's revisit `phy_device.c` more carefully: I've confirmed that all variables are initialized before use, and there are no potential information leaks from uninitialized reads. Also, `btusb_main.c` appears safe. `err` is properly initialized before any conditional usage. Finally, in `drivers/base/trust.c`, I need to confirm that `dev->trust_level` is always initialized before being accessed. I remember that `device_initialize` is the key function to check. I need to make sure that struct device is always initialized. |
| 28/3 | 2026/11/08 04:38 | tool | grepper |
0mArgs:{
"Expression": "void device_initialize",
"PathPrefixes": [
"drivers/base/core.c"
]
}Results: {
"Output": "drivers/base/core.c=3212=static void klist_children_put(struct klist_node *n)\n--\ndrivers/base/core.c-3239- */\ndrivers/base/core.c:3240:void device_initialize(struct device *dev)\ndrivers/base/core.c-3241-{\n"
} |
| 29/3 | 2026/11/08 04:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61856 output: 36 thoughts: 19 Thoughts: Okay, let's dive in. **Initial Thoughts on `device_initialize()`** Right, so I'm going to take a look at `device_initialize()` – specifically, I'm heading into `drivers/base/core.c`. This function is fundamental, right? It's the lynchpin for getting devices up and running within the kernel's device model. I need to refresh my understanding of how this particular function orchestrates the initialization process. I suspect it's responsible for a lot more than just the basics; it's likely where the groundwork is laid for things like probing, resource allocation, and device registration within the broader system. I'm keen to see how the code handles different device types and their specific initialization sequences. I'm looking for the essential points: what data structures it works with, what critical calls it makes to other functions, and what potential failure points it gracefully handles. Understanding this function thoroughly is crucial to grasping the overall device lifecycle within the Linux kernel. Let's see what gems are contained inside... |
| 30/3 | 2026/11/08 04:38 | tool | read-file |
0mArgs:{
"File": "drivers/base/core.c",
"FirstLine": 3240,
"LineCount": 35
}Results: {
"Contents": "3240:\tvoid device_initialize(struct device *dev)\n3241:\t{\n3242:\t\tdev-\u003ekobj.kset = devices_kset;\n3243:\t\tkobject_init(\u0026dev-\u003ekobj, \u0026device_ktype);\n3244:\t\tINIT_LIST_HEAD(\u0026dev-\u003edma_pools);\n3245:\t\tmutex_init(\u0026dev-\u003emutex);\n3246:\t\tspin_lock_init(\u0026dev-\u003edriver_override.lock);\n3247:\t\tlockdep_set_novalidate_class(\u0026dev-\u003emutex);\n3248:\t\tspin_lock_init(\u0026dev-\u003edevres_lock);\n3249:\t\tINIT_LIST_HEAD(\u0026dev-\u003edevres_head);\n3250:\t\tdevice_pm_init(dev);\n3251:\t\tset_dev_node(dev, NUMA_NO_NODE);\n3252:\t\tINIT_LIST_HEAD(\u0026dev-\u003elinks.consumers);\n3253:\t\tINIT_LIST_HEAD(\u0026dev-\u003elinks.suppliers);\n3254:\t\tINIT_LIST_HEAD(\u0026dev-\u003elinks.defer_sync);\n3255:\t\tdev-\u003elinks.status = DL_DEV_NO_DRIVER;\n3256:\t\tdev_assign_dma_coherent(dev, dma_default_coherent);\n3257:\t\tswiotlb_dev_init(dev);\n3258:\t}\n3259:\tEXPORT_SYMBOL_GPL(device_initialize);\n3260:\t\n3261:\tstruct kobject *virtual_device_parent(void)\n3262:\t{\n3263:\t\tstatic struct kobject *virtual_dir = NULL;\n3264:\t\n3265:\t\tif (!virtual_dir)\n3266:\t\t\tvirtual_dir = kobject_create_and_add(\"virtual\",\n3267:\t\t\t\t\t\t\t \u0026devices_kset-\u003ekobj);\n3268:\t\n3269:\t\treturn virtual_dir;\n3270:\t}\n3271:\t\n3272:\tstruct class_dir {\n3273:\t\tstruct kobject kobj;\n3274:\t\tconst struct class *class;\n"
} |
| 31/3 | 2026/11/08 04:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62698 output: 500 thoughts: 1087 Thoughts: **Analysis of Device Trust Initialization** Okay, let's break this down. My initial assessment is focused on understanding the initialization of this device trust mechanism within the kernel and ensuring it's safe. Specifically, I'm checking for common security vulnerabilities related to uninitialized memory. First, I see the `device_add` function calls `device_trust_init` and `device_trust_init_state`. This is where the magic happens. `device_trust_init` applies the boot policy, potentially overriding the default based on command-line options. `device_trust_init_state` then sets the trust state. Crucially, the code checks `dev->p->trust_initialized` which provides a guardrail to stop any double-initialization. The structure fields are definitely properly initialized. Now, I need to methodically address the prompt's four key areas of concern: 1. **User-Space Exposure:** I confirm that this patch *doesn't* introduce any uninitialized kernel structures sent to user space. It only adds sysfs files that emit strings, so the data is appropriately prepared and formatted before being exposed. No raw, potentially uninitialized memory or structure padding is being leaked. 2. **Conditional Logic:** There's no branching or logic that depends on uninitialized variables. All variables are initialized upon declaration, or they are set in the initialization functions. The new struct fields are zeroed out during device setup. 3. **Complex Data Structures:** All the relevant structures (`struct device`, `struct device_private`) are zero-allocated using `kzalloc`. Any potential uninitialized fields are effectively set to zero, so they are not an issue. 4. **Buffer Handling:** The string parsing functions, like `device_trust_parse_policy`, appear to be carefully written, using functions like `memchr`, `strcspn`, `strlen`, `strncmp`. This means there's no risk of reading past buffer boundaries or accessing uninitialized memory during string manipulation. DMA swiotlb padding clearing is retained. Now, let's consider the KMSAN applicability. This patch series focuses on core driver infrastructure - policy management, sysfs, and command-line parameters. The risks here center around concurrency, locking, reference counting, device lifecycle, probe deferral, and memory management (use-after-free, NULL dereference). These are areas well-covered by KASAN, LOCKDEP, and standard kernel debugging facilities. No new uninitialized memory reads are being introduced. The critical distinction is this: KMSAN is designed to catch uninitialized reads, which KASAN, LOCKDEP, and other bug detectors can catch. There's no point in running KMSAN if a bug can be caught by standard detectors. There are no uninitialized memory hazards here. Therefore, **NeedsKMSAN = false.** The reasoning is the detailed comparison of KASAN/KMSAN above. I am ready to set the results. |
| 32/3 | 2026/11/08 04:38 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series implements a device trust framework within the Linux driver core, allowing system administrators and subsystems to control driver binding and DMA operation based on requested policies (default, disabled, adversary, full). It introduces sysfs attributes (trust, trust_policy), kernel parameters (device_trust, device_trust.default, device_trust.driver), state machine transitions in the driver core, and integration across various subsystems (PCI, USB, IOMMU, input, networking).\n\nEvaluation against KMSAN applicability criteria:\n1. Kernel structures sent to user space: The patch only introduces text-based sysfs attributes ('trust' and 'trust_policy') formatted with sysfs_emit(). It does not introduce or modify any binary structures, ioctl outputs, netlink attributes, socket options, or BPF buffers copied to userspace that could leak uninitialized padding or fields.\n2. Uninitialized variables in branching: All local variables in the parsing and helper functions (e.g. level, policy, matched, err) are explicitly initialized before use or branch evaluation.\n3. Complex structure allocation and initialization: New fields added to 'struct device' and 'struct device_private' (trust_policy, trust_level, trust_committed_level, trust_state, etc.) reside in structures allocated via kzalloc (e.g., device_private_init uses kzalloc_obj) or initialized during device setup (device_trust_init, device_trust_init_state). Zero represents the default/disabled state across all relevant enums.\n4. Buffer bounds and lengths: The command-line parsing routines safely parse bounded string slices with memchr, strcspn, and strncmp without creating uninitialized buffer regions.\n\nPrimary risks in these changes relate to lock ordering and concurrency (device_driver_attach vs device_driver_try_attach, device_lock vs parent lock), probe error handling, state machine correctness, and driver/device reference counting during bind/unbind. These classes of bugs (deadlocks, NULL pointer dereferences, use-after-free) are effectively covered by standard KASAN, LOCKDEP, and general kernel debuggers.\n\nBecause no uninitialized memory reads or information leak hazards are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series implements a device trust framework within the Linux driver core, allowing system administrators and subsystems to control driver binding and DMA operation based on requested policies (default, disabled, adversary, full). It introduces sysfs attributes (trust, trust_policy), kernel parameters (device_trust, device_trust.default, device_trust.driver), state machine transitions in the driver core, and integration across various subsystems (PCI, USB, IOMMU, input, networking).\n\nEvaluation against KMSAN applicability criteria:\n1. Kernel structures sent to user space: The patch only introduces text-based sysfs attributes ('trust' and 'trust_policy') formatted with sysfs_emit(). It does not introduce or modify any binary structures, ioctl outputs, netlink attributes, socket options, or BPF buffers copied to userspace that could leak uninitialized padding or fields.\n2. Uninitialized variables in branching: All local variables in the parsing and helper functions (e.g. level, policy, matched, err) are explicitly initialized before use or branch evaluation.\n3. Complex structure allocation and initialization: New fields added to 'struct device' and 'struct device_private' (trust_policy, trust_level, trust_committed_level, trust_state, etc.) reside in structures allocated via kzalloc (e.g., device_private_init uses kzalloc_obj) or initialized during device setup (device_trust_init, device_trust_init_state). Zero represents the default/disabled state across all relevant enums.\n4. Buffer bounds and lengths: The command-line parsing routines safely parse bounded string slices with memchr, strcspn, and strncmp without creating uninitialized buffer regions.\n\nPrimary risks in these changes relate to lock ordering and concurrency (device_driver_attach vs device_driver_try_attach, device_lock vs parent lock), probe error handling, state machine correctness, and driver/device reference counting during bind/unbind. These classes of bugs (deadlocks, NULL pointer dereferences, use-after-free) are effectively covered by standard KASAN, LOCKDEP, and general kernel debuggers.\n\nBecause no uninitialized memory reads or information leak hazards are introduced or exposed, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|