Skip to content

thunderbolt: apple: release a sleeping XDomain peer at the adapter that holds it - #3211

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/tb-cio-sleep-xdomain-holder
Nov 28, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/tb-cio-sleep-xdomain-holder

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: Before a native CIO deep sleep, a host-to-host (XDomain) peer was found by the route of the lane adapter being walked, but only that adapter's pointer was cleared. When a different adapter held the peer (the J713 case the code describes), that adapter kept a pointer to the peer after it was unregistered and freed. The release now finds the adapter that holds the peer, clears it before the last reference goes, and unconfigures that adapter.
Tested: host model tools/asahi/tb-cio-sleep-tests.py at 15b9039: 42 cases pass, all 29 negative controls caught (2 new). Hardware rows still owed (below).
Risk: two-way door · Macs that use the native CIO sleep: M4 (T8132: J713, J773g) and M5 Pro (T6050: J714s)
Not yet: hardware rows at this head on J714s (dock attached: attach and detach, displays, dock I/O, one deep sleep) and J773g (the same, plus a deep sleep with a host-to-host peer if one is cabled).

Details
  • tb_cio_xdomain_port() walks the router tree the same way switch_find_xdomain() does for a route lookup, through the adapters' xdomain pointers, and returns the adapter whose peer has the route. So the peers that get released are exactly the ones the old lookup found.
  • The peer is released as the hotplug unplug path does it: take a reference, mark it unplugged, tb_xdomain_remove(), clear the holder's pointer, tear down its paths, drop the reference, then unconfigure the holder. It is unregistered outside the lock as before.
  • When the walked adapter isn't the holder, a debug line names the holder.
  • Host model: new J713 case where the first adapter walked holds no peer but its route finds one on another adapter. Two new negative controls, clearing or unconfiguring the walked adapter instead of the holder, both fail it.
Evidence

Head 15b903917a94f24177940d37acad8fd03d658a7f, base main 40e5e29cc915.

$ python3 tools/asahi/tb-cio-sleep-tests.py
PASS 42 native tunnel replay cases
PASS 29 negative controls

New negative controls: clear-iterated-port (clear the walked adapter, as before this change) and unconfigure-iterated-port. Both fail the new J713 case, the first because the holder keeps its pointer and the peer is released a second time.

KUnit thunderbolt suite (arm64 QEMU, KASAN) with this branch merged with the other two USB4 branches, W=1: 78/78, no warnings in the changed files.

…s it

Before a native CIO sleep, tb_cio_release_xdomains() looks a peer up by
the downstream route of each lane adapter, because on the J713 the
adapter iterated did not hold the peer while the lookup found it. It
then cleared only the iterated adapter's pointer. In that case the
adapter that does hold the peer keeps its pointer, which holds no
reference, after the peer is unregistered outside the lock and freed.
The next lookup, rescan or sleep walks through it.

Find the adapter that holds the peer the same way the route lookup
finds the peer, and release the peer there: clear that adapter's
pointer before the reference taken for the release is dropped, and
unconfigure that adapter.

The host model gains the J713 case, a peer found by the route of an
adapter that does not hold it, with two negative controls: clearing
or unconfiguring the iterated adapter instead of the holder.

Fixes: a05c35c ("thunderbolt: Apple native CIO deep sleep with a host-to-host peer or no PCIe tunnel (M4 MacBook Air) (#2692)")
Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 08:12
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 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 15b90391: not ready yet

Check Detail
CI at head running running: KUnit clpc, KUnit trusted-keys
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 15b90391
Conflicts ok none
PR text ok Risk: two-way · Macs that use the native CIO sleep: M4 (T8132: J713, J773g) and M5 Pro (T6050: J714s)

Areas: area:gpu

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 15b903917a94: ready (code); the J714s and J773g rows listed under "Not yet" are still owed before merge

Blockers: none

Checked

  • tb_cio_xdomain_port() (drivers/thunderbolt/tb-cio-sleep.c:33) walks the tree the same way switch_find_xdomain() does for a route lookup. It follows the xdomain pointers, recurses only through tb_port_has_remote() (which skips the upstream adapter and the second lane of a dual link), and starts from tb->root_switch as tb_xdomain_find_by_route() did. So it finds the same peers, and it returns the adapter that holds each one.
  • The release at 77-91 follows the hotplug unplug order on that adapter: get, mark unplugged, tb_xdomain_remove(), clear the holder's pointer, tear down the paths, put, unconfigure. The adapter that the route lookup reached through now loses its pointer before the peer is unregistered outside the lock and freed, so a later lookup, rescan or sleep can't walk into a freed peer. Once a peer is released, its holder has no pointer, so a second adapter with the same route finds nothing and the peer isn't released twice.
  • No runtime switches, no new constants, and no SoC or board gates. The J713 mentions are in comments only.
Evidence
  • make W=1 drivers/thunderbolt/tb.o (asahi profile, LLVM=1, arm64; tb-cio-sleep.c is included by tb.c) at 15b903917a94: builds, no warnings.
  • python3 tools/asahi/tb-cio-sleep-tests.py: PASS 42 native tunnel replay cases, PASS 29 negative controls, with the new clear-iterated-port and unconfigure-iterated-port controls included.
  • checkpatch.pl --strict: 0 errors and 0 warnings. The 3 checks are blank-line style in the host model, which matches the file's existing compact style.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (M4 Mac mini) at 15b903917a94: PASS as no-regression evidence (no USB4 peer attached, so no XDomain to release). s2idle with this PR resumes the same 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 15b903917a94: no regression. The XDomain path isn't exercised on this Mac.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at 15b90391 (once cancelled CI checks are rerun green)

Blockers: none (code, host model tests, and two-generation no-regression rows are ready; rerun the 16 cancelled CI checks before merge).

Checked:

  • drivers/thunderbolt/tb-cio-sleep.c:13-71: verified tb_cio_xdomain_port() recursively locates the actual holder adapter holding the peer matching route, takes a reference via tb_xdomain_get(holder->xdomain), removes it, clears holder->xdomain = NULL before tb_xdomain_put(xd), and calls tb_port_unconfigure_xdomain(holder) rather than the iterated alias port.
  • tools/asahi/tb-cio-sleep-tests.{c,py}: verified alias-holder test case and mutations (clear-iterated-port, unconfigure-iterated-port, keep-xdomain-port) cover the exact J713 failure mode (42/42 cases, 29/29 negative controls).
  • Verified exact-head (15b903917a94) no-regression rows on J773g (M4 mini, s2idle PASS) and J714s (M5 Pro, deep sleep PASS).
Evidence
  • Diff reviewed at 15b90391 (all 3 files, 168 lines).
  • CI at review time: 3 passed, 0 failed, 16 cancelled.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train status at 15b90391 (App approval noted; rows: 16 cancelled; J773g + J714s exact-head no-regression PASS). @DjDeveloperr code-owner ping (6-point rule, point 6): merge earliest 17:15 AEDT once full CI is green (I re-ran cancelled jobs) and the second-family row point holds. Revert: git revert -m 1 <merge>.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at 15b90391. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J773g+J714s. (5) There's no blocking review. (6) Dj was pinged here 529 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit be010f4 into main Nov 28, 2026
31 of 100 checks passed
@RyanTheTide
RyanTheTide deleted the fix/tb-cio-sleep-xdomain-holder branch November 28, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 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