Skip to content

PCI: mark devices below a device tree external-facing port untrusted - #3213

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/pci-of-external-facing-untrusted
Nov 27, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/pci-of-external-facing-untrusted

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: On device tree systems, PCIe devices tunneled behind a USB4/Thunderbolt root port marked external-facing are now marked untrusted and removable again, as the binding requires ("all bridges and endpoints downstream of this port are external to the machine"). Since 3b96b89 only an x86+ACPI hook did this, so on Apple Silicon tunneled devices were treated as built in. iommu-dma mapped their sub-16K buffers in place instead of bouncing them, the device could reach the rest of each 16K IOMMU page, and ATS stayed allowed.
Tested: builds at 027c947 (W=1, arm64); the KUnit thunderbolt suite with this branch merged with the other two USB4 branches: 78/78. Hardware rows still owed (below).
Risk: two-way door · every Mac with USB4/Thunderbolt PCIe tunnels (all apcie-c / USB4 PCIe ports in the Apple DTs); generic PCI code for any DT external-facing port
Not yet: hardware rows at this head on J714s (M5 Pro, USB4 dock) and J773g (M4 mini): dock attach and detach, displays, dock storage or Ethernet I/O with throughput against base, one deep sleep with the dock attached, 0 IOMMU/DART faults.

Details
  • pci_dev_below_of_external_port() is true when the upstream bridge has external_facing set and its DT node has the external-facing property. It checks the property because external_facing can also come from ACPI. The result feeds set_pcie_untrusted() and pci_set_removable(). Descendants inherit the flags from their parent, as before.
  • The x86 rule for a built-in discrete controller below an ACPI ExternalFacingPort stays in arch_pci_dev_is_removable() and is unchanged. The DT binding leaves no room for such a controller.
  • Effects on tunneled devices:
    • iommu-dma bounces unaligned mappings through SWIOTLB, which arm64 always sets up.
    • The maximum mapping size becomes SWIOTLB's (256 KiB), so a dock NVMe shows a smaller max_hw_sectors_kb.
    • The group's default domain is strict DMA, which the PCIe-C DART already chose for tunneled streams.
    • ATS is disabled.
    • removable reads removable in sysfs.
  • What the rows check: removable on the tunneled devices (fixed/unknown on internal ones), dock storage and Ethernet throughput against base, no "swiotlb buffer is full" or "bounce buffers are inactive" messages, 0 DART faults.
Evidence

Head 027c9476464dc3f1a70b91501e94925df427d3a8, base main 40e5e29cc915.

The binding text, from dt-schema pci-pci-bridge.yaml: "When present, the port is externally facing. All bridges and endpoints downstream of this port are external to the machine."

Every external-facing node in arch/arm64/boot/dts/apple/ is the root port of a USB4/Thunderbolt PCIe tunnel (apciec*_port, usb4_*_pcie_port). No other arm64 DT in the tree uses the property.

Row checklist, per Mac, against base:

  • cat /sys/bus/pci/devices/*/removable: tunneled devices read removable, internal ones don't
  • dock NVMe queue/max_hw_sectors_kb (expected 256) and read throughput; dock Ethernet throughput
  • dmesg: no "swiotlb buffer is full", "bounce buffers are inactive" or "Device is not trusted"; 0 DART faults
  • displays over the dock, attach/detach, one deep sleep with the dock attached

The device tree binding defines "external-facing" on a port as: every
bridge and endpoint downstream of it is external to the machine.
pci_set_bus_of_node() still sets external_facing on such a port, but
since commit 3b96b89 ("PCI: Detect and trust built-in Thunderbolt
chips") only arch_pci_dev_is_removable() marks the devices below it
untrusted or removable, and that is implemented for x86 with ACPI only.

On device tree systems, devices tunneled through a USB4 or Thunderbolt
port are therefore treated as built in: iommu-dma maps their buffers
in place instead of bouncing the unaligned ones, so with 16K IOMMU
pages a device can reach the rest of each page around its buffers, and
ATS stays allowed.

Mark the devices directly below a port that the device tree marks
external-facing untrusted and removable again, as before that commit.
The ACPI exception for a built-in discrete controller below an
ExternalFacingPort does not apply: the binding leaves no room for one.

Fixes: 3b96b89 ("PCI: Detect and trust built-in Thunderbolt chips")
Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 08:17
@github-actions github-actions Bot added blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 027c9476: not ready yet

Check Detail
CI at head ok 2 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at 027c9476
Conflicts ok none
PR text ok Risk: two-way · every Mac with USB4/Thunderbolt PCIe tunnels (all apcie-c / USB4 PCIe ports in the Apple DTs); generic PCI code for any DT external-facing port

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 027c9476464d: ready (code). The cost is real, though, and the J714s and J773g rows need to measure it before merge (below).

Blockers: none

Checked

  • pci_dev_below_of_external_port() (drivers/pci/probe.c:1745) looks only at the direct upstream bridge. Deeper devices still get the flags from parent->untrusted and dev_is_removable(&parent->dev), as before. external_facing is set on the bridge in pci_set_bus_of_node() when its secondary bus is created, so the flag is there before any device below it is set up, hotplugged ones included. A NULL of_node reads as false, and the ACPI case is unchanged.
  • This restores what the binding says, and what the generic code did before 3b96b89. Tunneled endpoints become untrusted, so iommu-dma bounces their unaligned mappings and zeroes the padding instead of mapping the whole 16K granule in place.
  • No runtime switches, no constants, and no SoC or board gates.
  • Scope note, not a blocker: besides the Apple USB4/PCIe-C root ports, arch/mips/boot/dts/loongson/loongson64-2k1000.dtsi (223, 242, …) also marks its PCIe root ports external-facing, so devices behind them become untrusted and removable too. That matches the binding and the pre-3b96b895127b behaviour, but the commit message should mention it for upstream.

Runtime impact on docks (for the rows)

  • On these Macs, SWIOTLB is only the arm64 kmalloc-bounce pool: RAM/1024, capped at 64 MiB. The M2 Pro host here logs "SWIOTLB bounce buffer size adjusted to 15MB", which rounds up to 16 MB. With a 16K IOMMU granule, almost every network buffer is unaligned, so after this change nearly all dock Ethernet TX (skb heads and frags) goes through that pool, with a CPU copy each time. Large TSO bursts can fill 16 MB, and when that happens the mapping fails ("swiotlb buffer is full") and the packets are dropped.
  • Untrusted devices are capped at 256 KiB per mapping (max_hw_sectors_kb 256 on a dock NVMe).
  • So besides the checklist in the PR, the rows should run multi-stream iperf3 TX and RX to the dock Ethernet for at least 60 s, together with a sequential read from dock storage. Record throughput against base and /sys/kernel/debug/swiotlb/io_tlb_used_hiwater, on the Mac with the least RAM that has a dock. A throughput drop is the expected price of this change. Mapping failures or "swiotlb buffer is full" would be a blocker.
Evidence
  • make W=1 drivers/pci/probe.o (asahi profile, LLVM=1, arm64) at 027c9476464d: builds, no warnings. Config has IOMMU_DMA=y, SWIOTLB=y, DMA_BOUNCE_UNALIGNED_KMALLOC=y.
  • checkpatch.pl --strict: clean.
  • git grep external-facing -- arch/ at this head finds the Apple apciec*/USB4 PCIe root ports (t8103, t8112, t600x, t602x, t6030, t6040, t6050, t8132, t8142, t8152) and the Loongson 2K1000 root ports.
  • Host boot log: software IO TLB: SWIOTLB bounce buffer size adjusted to 15MB, roundup to 16MB.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (M4 Mac mini) at 027c9476464d: PASS (no dock attached). The internal PCIe devices aren't marked untrusted: Wi-Fi on PCIe runs with no swiotlb bounce lines, 0 Device is not trusted lines, and the same throughput as main.

How it was run: two guarded one-shots on the default cmdline, the same day on the same Mac. Both returned to the stock kernel afterwards, and the kits were removed from the Mac.

No USB4/Thunderbolt device is attached to this Mac, so this is a smoke check: no new kernel lines, PCIe and Wi-Fi unaffected, and one s2idle cycle survived. It doesn't exercise a dock.

control (main) candidate
boot 0 kernel bugs, 0 WARNING: lines, 0 failed units same
thunderbolt devices none none
path goes out of non-existent adapter 0 0
DROM, Device is not trusted, xdomain released for the sleep 0 / 0 / 0 0 / 0 / 0
swiotlb buffer is full, bounce buffers are inactive, KASAN, Oops 0 / 0 / 0 / 0 0 / 0 / 0 / 0
pcie-apple / PCIe-C error lines 0 0
Wi-Fi (BCM4388 on PCIe, bidirectional receive DMA) 4 × 200 MB each way, 0 rx errors same, 0 rx errors
RTC s2idle (12 s) success +1, same boot, Wi-Fi reconnected, 0 failed units same

The only extra err-level kernel lines on the candidate are the scaler module A/B's two job timeouts (#3217). They aren't TB/PCI lines.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row at 027c9476464d: PASS for what this Mac can exercise.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict at 027c9476: approve (code). Merge after the hardware rows the body lists.

Blockers: none in the code.

Checked

  • Devices below a bridge that is external_facing and whose DT node has external-facing are marked untrusted and removable. That matches the binding: nothing built in sits below such a port.
  • The ACPI path is unchanged, and the change is keyed on the DT property, with no chip gates.

Note: this turns on strict IOMMU and bounce buffering for USB4 tunnel devices. The rows should cover a USB4 dock's PCIe devices (Ethernet throughput, NVMe enclosure) working, plus one built-in PCIe device that must stay trusted (the Wi-Fi), on one Mac per USB4 generation.

@RyanTheTide
RyanTheTide merged commit c9f9324 into main Nov 27, 2026
3 checks passed
@RyanTheTide

Copy link
Copy Markdown
Contributor

This merged through auto-merge on my code approval, which I had written as "merge after the hardware rows the body lists". I've reverted it in #3351 so no fleet cut picks up an untested change to how USB4-tunnelled devices are trusted. Please reland it as is, with the dock row (tunnelled Ethernet and NVMe working, throughput noted, the built-in Wi-Fi still trusted) on one Mac per USB4 generation. The code review stands. Sorry for the churn.

RyanTheTide added a commit to term-guy/linux that referenced this pull request Nov 27, 2026
…trusted"

This reverts merge c9f9324 (aurora-silicon#3213). It merged on an approval before
the hardware rows its description lists as owed. It marks every device
behind a USB4/Thunderbolt PCIe tunnel untrusted and removable on every
Apple Silicon Mac, which turns on strict IOMMU handling and bounce
buffering for them, and that has not run on a Mac yet. Reland it once a
USB4 dock's PCIe devices (Ethernet, NVMe) and the built-in Wi-Fi have
been checked on one Mac per USB4 generation.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide deleted the fix/pci-of-external-facing-untrusted branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants