Skip to content

thunderbolt: apple: avoid lock inversion during controller teardown - #51

Closed
DjDeveloperr wants to merge 1 commit into
fix/j414s-dual-dock-dpfrom
fix/apple-usb4-teardown-deadlock
Closed

DjDeveloperr wants to merge 1 commit into
fix/j414s-dual-dock-dpfrom
fix/apple-usb4-teardown-deadlock

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Summary

Fix a lock-order inversion between Apple USB4 controller shutdown and PCIe
tunnel hot-unplug handling. This is a single-commit follow-up to #46, targeting
fix/j414s-dual-dock-dp; retarget after that stack lands. It changes only
drivers/thunderbolt/apple.c.

Failure mechanism

Two paths take the same locks in opposite order:

controller shutdown: pcie_tunnel_lock -> NHI removal -> tb->lock
PCIe hot-unplug:     tb->lock -> pci_tunnel_deactivate -> pcie_tunnel_lock

If controller shutdown races a PCIe hot-unplug worker, both can wait forever.
The relevant call/lock ordering was checked in the source and in the binary
matching the loaded J414s driver.

The user reports hangs followed by resets when unplugging or powering off a
Winstars TB5 dock operating at TB4 speed, with a USB4 NVMe enclosure connected
to another controller. This defect is a plausible cause, not a confirmed
crash-time diagnosis
: the recent journals end without a panic stack and
persistent crash storage is empty. The SSD's causal role is unknown.

Fix

  • Gate PCIe activation while shutdown is in progress, serialized with the
    pre-/post-activation callbacks.
  • Drain activation work without holding pcie_tunnel_lock.
  • Release that lock before removing the NHI and draining the domain workqueue.
  • Keep PCIe quiesce/depopulation serialized; reopen activation on the next
    controller start.

The gate prevents a late callback from queueing activation after shutdown
has drained the worker. No link-rate cap, bandwidth policy change, delay,
or reduction in normal transfer concurrency is introduced.

Validation

  • Built natively on J414s/M2 Pro with Clang 22.1.8 against the running
    7.1.12-m2fix48 configuration and verified live symbol providers.
  • Rebuilding the unmodified driver produced .text byte-identical to the
    installed driver, establishing a matching executable baseline.
  • Patched module builds with matching vermagic; all 93 imports were checked
    against live kernel exports. No compiler diagnostics for the patched module.
  • Source-order regression checks reject the old lock-across-removal sequence
    and pass for the new unlocked removal, activation gate, and restart gate.
    These are source-level checks, not hardware/concurrency tests.
  • checkpatch.pl --no-tree --no-signoff passes with zero warnings/errors.
  • Staged in a separate tbstop1 initramfs for the next boot, preserving the
    original Image, initramfs, on-disk driver, and stage2. No runtime driver
    reload, hotplug test, or reboot has been performed for this patch.

Hardware validation remains pending. In particular, verify dock removal
with the independent SSD active, dock reattachment, display/USB recovery,
and absence of hangs. This PR does not include the separate DPRX callback
reference leak, display bandwidth-allocation experiments, SEP, or AVD changes.

apple_cio_stop() holds pcie_tunnel_lock while destroying the NHI, whose
domain removal acquires tb->lock and drains hotplug work. A concurrent
unplug worker can hold tb->lock and wait for pcie_tunnel_lock through
apple_nhi_pci_tunnel_deactivate(), creating an ABBA deadlock.

Gate PCIe activation under pcie_tunnel_lock, drain pending activation work
without that lock, and drop the lock before removing the NHI. Keep PCIe
quiesce and final depopulation serialized. Reset the gate before bringing
up the next NHI so a later cable attachment can activate normally.

The gate also prevents a post-activation callback from requeueing the
worker after shutdown has drained it. Normal tunnel bandwidth and workqueue
concurrency are unchanged.
RyanTheTide added a commit that referenced this pull request Oct 8, 2026
…adlock

thunderbolt: apple: avoid lock inversion during controller teardown

Original contributor commits are preserved. Conflict resolutions retain the audited shared driver APIs and their integrated review fixes.

Contributor credit; Signed-off-by outstanding:
- 8153565: Dj <[email protected]>
- 3075f55: DjDeveloperr <[email protected]>
- a9aa605: DjDeveloperr <[email protected]>
- 0cd936d: DjDeveloperr <[email protected]>
- aff9d4e: DjDeveloperr <[email protected]>
- a044eab: DjDeveloperr <[email protected]>
- c5653cb: DjDeveloperr <[email protected]>
- e9e52e6: Oliver Lukschander <[email protected]>
- 0c16b38: Oliver Lukschander <[email protected]>
- e83f46d: Oliver Lukschander <[email protected]>
- 2b5254a: Oliver Lukschander <[email protected]>
- cc895f2: Oliver Lukschander <[email protected]>
- f8cec39: Oliver Lukschander <[email protected]>
- 81eee7b: Oliver Lukschander <[email protected]>
- 45c34d5: Oliver Lukschander <[email protected]>
- 6f0468f: Oliver Lukschander <[email protected]>
Ryan integrates these contributions under DCO (b); no author signature is forged.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@DjDeveloperr
DjDeveloperr deleted the fix/apple-usb4-teardown-deadlock branch October 12, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant