Skip to content

iommu/apple-dart: undo a paging attach that fails part way - #3227

Open
DjDeveloperr wants to merge 5 commits into
mainfrom
fix/apple-dart-undo-failed-attach
Open

DjDeveloperr wants to merge 5 commits into
mainfrom
fix/apple-dart-undo-failed-attach

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: When a paging attach failed after programming some of a device's DART stream maps (for example on a TLB invalidate that times out after the new tables are installed), those streams kept translating through the new domain's tables. The IOMMU core may free that domain as soon as the attach returns. Now every stream map the attach reached goes back to the old domain, or is blocked when there is no old domain to go back to. The device's streams also leave the failed domain on every DART.

Tested: The new host test tools/asahi/apple-dart-attach-undo-tests.py passes all 14 cases at 6d99e00e825d; main fails 11 of them, and the runner records that as a kill. All host tests that read apple-dart.c pass (18 DART runs plus the MTP and crashlog tests). apple-dart.o builds with W=1 on 16K pages. J773g (M4 mini) row PASS at 6d99e00e825d: the same 16 IOMMU groups and device-to-group map as base, USB deauthorize/authorize ×3 with reads, Wi-Fi, s2idle, 0 DART faults, and the same error-level lines as base (#3227 (comment)).

Risk: two-way · every Mac, since the DART driver is shared; only the failure path of a paging attach changes.

Not yet: The second-generation row on J700 (Neo). The failure path itself is covered by the host test only.

Details
  • apple_dart_attach_dev_paging_unguarded() records how many stream maps it reached, the failing one included, because apple_dart_setup_translation() installs the tables before the invalidate that can fail.
  • apple_dart_attach_paging_undo() handles each reached stream map:
    • identity old domain: bypass, as apple_dart_attach_dev_identity sets it (a retained-MTP stream, which identity leaves alone, is blocked instead);
    • paging old domain of this driver (finalized, with tables, not DCP): its tables go back through apple_dart_setup_translation();
    • anything else (no old domain on a first attach, the blocked domain, a DCP domain), or when putting the old tables back fails: blocked, as apple_dart_attach_dev_blocked does;
    • managed DART: quarantined, the same as when its own stream fails;
    • locked DART: left alone. Its firmware root stays in the domain, and apple_dart_domain_free() unmaps and invalidates it before it frees the tables.
  • apple_dart_attach_paging_drop_streams() takes the device's streams out of the failed domain's stream maps on every DART, so the domain's invalidations and its free stop counting them. The exception is a locked stream the attach reached. Before, only a managed DART's streams were removed. Another device's streams in the domain stay.
  • The early returns (busy MTP, a domain that fails to finalize, a DCP domain) don't touch any stream, so they are unchanged.
  • The same undo was first proposed in iommu/apple-dart, PCI: apple: undo a failed attach, keep RID-to-SID slots, drop TEST labels #2991, together with other PCIe and Thunderbolt changes; this PR carries only the undo, plus a test.
Evidence
  • python3 tools/asahi/apple-dart-attach-undo-tests.py: 14 paging attach undo cases PASS.
  • python3 tools/asahi/apple-dart-attach-undo-tests.py --ref 255761f1d55f: fails, among others, "first attach, only stream fails: stream map 0 is on the failed domain, expected blocked" and "from a paging domain: stream map 0 is on the failed domain, expected the old domain". It passes only the managed-failure, not-finalized and success cases.
  • tools/asahi/run-host-tests.py --only dart: 18/18; --only j613-mtp-s2idle-tests and --only rtkit-crashlog-map-tests: pass.
  • ./linux-build/build kernel … -- -j4 W=1 drivers/iommu/apple-dart.o with CONFIG_ARM64_16K_PAGES=y, CONFIG_APPLE_DART=y: no warnings.
  • scripts/checkpatch.pl --strict: 0 errors; one warning about MAINTAINERS for the new test file, which other tools/asahi tests share.
  • Base: main 255761f1d55f.

apple_dart_attach_dev_paging_unguarded() programs a device's stream maps
one at a time. When one fails, for example on a TLB invalidate that times
out after the new tables are installed, that stream map and the ones
before it kept translating through the new domain's tables. Only a
managed DART's streams were taken back out of the domain. The IOMMU core
keeps a device whose attach failed on its old domain, and may free the
new domain and its tables as soon as the attach returns.

On failure, put each stream map the attach reached back on the old
domain: bypass for the identity domain, the old tables for a paging
domain of this driver. Block it when there is no old domain (a first
attach), for the blocked or a DCP domain, and when putting the old domain
back fails. Quarantine a managed DART. Take the device's streams out of
the failed domain on every DART, except a locked stream the attach
reached: apple_dart_domain_free() still has to unmap its firmware root
before it frees the tables.

tools/asahi/apple-dart-attach-undo-tests.py runs the production attach
against a modelled DART, fails it at each stream map from every kind of
old domain, and checks where each stream ends up; the previous code
fails 11 of its 14 cases. The same undo was first proposed in #2991,
together with other changes.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 10:01
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 6d99e00e825d: ready (code). The J773g and J700 rows listed under "Not yet" are still owed, and CI was still running when I checked.

Blockers: none

Checked

  • No stream is left pointing at freed tables. reached (drivers/iommu/apple-dart.c:3932) is set before each stream map is touched, so it includes the one whose apple_dart_setup_translation() installed the new TTBRs and then failed its TLB invalidate. In apple_dart_attach_paging_undo() (3839), every reached stream ends one of these ways:
    • in bypass (identity old, bypass-capable, not retained-MTP);
    • back on old's own tables, which old still owns because the core keeps the device there (apple_dart_setup_translation() rewrites the TTBRs and invalidates);
    • blocked: TCR disabled, for no old, the blocked domain, a DCP domain, a old that isn't finalized, or when putting old back fails;
    • quarantined, for a managed DART.
      Stale TTBRs under bypass or disable aren't walked. Any later translation goes through apple_dart_setup_translation(), which writes the new TTBRs before it enables translation and invalidates.
      A locked DART's reached stream stays in the domain on purpose. apple_dart_setup_translation_locked() already unmaps its own TTBRs when it fails, and apple_dart_domain_free() (4241) unmaps the roots of any locked stream left in the domain before freeing the tables. A domain with a managed stream map is never freed (4264).
  • The quarantine can't wedge a healthy DART. A managed DART is quarantined only if the attach reached its stream map. Before this change that happened only when its own setup failed. Now it can also happen when a later stream map of the same device fails. But the only managed DART in the tree is dart_apcie0 (T8132, apple,managed-sids = <1 2>), and the PCI functions behind it have that single DART, so no other DART's failure can reach it. If a device ever pairs a managed DART with another one, that's the case to look at again.
  • Lock order on the undo path. The undo runs where the forward path does: inside the sleep-writer gate and the v27 cfg section, under the core's group mutex, and with managed_lock already dropped before goto err. It takes managed_lock alone, for the quarantine, or dart->lock through the invalidate in apple_dart_setup_translation(). That's never one inside the other in a new order. The forward path's managed_lock → invalidate nesting isn't repeated, because managed DARTs are only quarantined, never retranslated.
  • apple_dart_attach_paging_drop_streams() (3882) removes only this device's SID bits from the failed domain on every DART, except a locked one it reached. The early returns (busy MTP, a domain that won't finalize, DCP) touch no stream and are unchanged.
  • No runtime switches, no new constants, and no SoC or board gates. The description credits iommu/apple-dart, PCI: apple: undo a failed attach, keep RID-to-SID slots, drop TEST labels #2991's approach without taking its commits.
Evidence
  • python3 tools/asahi/apple-dart-attach-undo-tests.py at 6d99e00e825d: 14 paging attach undo cases PASS. With --ref 255761f1d55f (main), it fails, for example "managed DART reached, second stream fails: the managed DART was not quarantined" and "stream map 1 is on the failed domain, expected blocked".
  • tools/asahi/run-host-tests.py --only dart: 18/18 passed.
  • make W=1 drivers/iommu/apple-dart.o (asahi profile, 16K pages, LLVM=1, arm64): no warnings.
  • checkpatch.pl --strict: 0 errors. One warning: MAINTAINERS doesn't list the new test file, which other tools/asahi tests share.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (M4 Mac mini) at 6d99e00e825d: PASS, no regression in ordinary attaches. The failure path itself isn't reached on this Mac; the host test covers it.

How it was run: two guarded one-shots on the same Mac and boot chain, 7 minutes apart.

base (no #3227) candidate (#3227)
ls /sys/kernel/iommu_groups | wc -l 16 16
Adding to iommu group lines 16 16
device → group map sha eaa14853… identical
apple-dart probe failures 0 0
USB: deauthorize/authorize ×3, then a 64 MB O_DIRECT read each time 3/3 re-enumerated, ~5.2 MB/s, 0 error lines same
Wi-Fi: 200 MB, then 50 MB after sleep, each way 0 rx errors 0 rx errors
GPU: api.smoke 8/8 + api.external.memory.dma_buf (44 pass / 6 not supported) ok, 0 faults ok, 0 faults
RTC s2idle success +1, same boot success +1, same boot
DART faults / WARNING: lines 0 / 0 0 / 0
error-level kernel lines 21 the same 21

The USB device is a SanDisk Cruzer Blade, the only non-hub USB device on this Mac. Plug-out/plug-in is a software re-enumeration (authorized 0 → 1), not a physical replug.

Both boots returned to the stock kernel, and the kits were removed from the Mac.

@DjDeveloperr

DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

J700 (MacBook Neo, second generation for the shared DART code) at 6d99e00e825dbfc2e338b682ec22be1df70c626e: PASS, now including USB. There were two one-shot boots, each checked against the stock 7.3.0-41 boot in the same session:

  • a combined candidate with this head merged in (main 255761f plus this PR and other non-overlapping PRs);

  • this head on its own (main 255761f plus this PR), for the USB checks.

  • (a) The same IOMMU groups and devices: "Adding to iommu group" appears 29 times on stock and 29 on both candidates, and /sys/kernel/iommu_groups has 28 groups on all three. The group-to-device map is identical before and after the USB cycles and after a sleep/wake.

  • (b) USB deauthorize/authorize through sysfs: each cycle runs on the internal hub 1-1 and both root hubs (usb1, usb2). Wi-Fi is on PCIe, so it wasn't touched.

    • Cycles: 3 cycles on this head, 1 more after a sleep/wake, and 3 on stock for comparison.
    • Every cycle on every device:
      • Deauthorizing reads back authorized=0, with 0 interfaces bound and the children gone.
      • Authorizing reads back authorized=1, with the same interfaces bound and the same children as before.
      • The only lsusb -t change is the hub's device number after it re-enumerates, the same as on stock.
    • Faults: 0 DART fault or error lines on any cycle, on both kernels. The only error-level kernel lines during the cycles are the SEP sensor's late-attach retries, which happen at every boot on stock too.
  • (c) Traffic, GPU work and sleep:

    • Wi-Fi: 64 MiB pulled over ssh, 20.0 MiB/s on the combined candidate vs 19.5 MiB/s on stock, and 17.0 MiB/s on this head vs 8.9 MiB/s on stock (this link is noisy).
    • About 15 minutes of headless GPU and media work ran clean on the combined candidate: Vulkan sync CTS, the killed-jobs stress, AVD decode, AVE encode and the scaler.
    • One test sleep per candidate (rtcwake -m mem, 30 s) resumed on the same boot.
  • (d) 0 DART faults and 0 DART error lines on both candidates, before and after the sleep. No new kernel errors compared with stock, apart from the media rows' expected refusals on the combined candidate.

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (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 797ec253: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, KUnit ane-boot, KUnit ane-pm, KUnit dcp-fabric, KUnit dcp-topology
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 797ec253, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · every Mac, since the DART driver is shared; only the failure path of a paging attach changes.

Areas: area:gpu, area:platform

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

@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: FINISH, then merge at 6d99e00e

Blockers:

  1. host-tests CI fails apple/bcm4377-radio-sids (tools/asahi/apple-bcm4377-radio-sids.py): apple_dart_attach_dev_paging_unguarded() now calls apple_dart_attach_paging_undo() and apple_dart_attach_paging_drop_streams(), which that host test does not extract/stub, causing -Werror=implicit-function-declaration. Update tools/asahi/apple-bcm4377-radio-sids.py to include/stub the new helpers and verify host-tests passes cleanly.
  2. Rerun the 18 cancelled CI checks once fixed.

Checked:

  • drivers/iommu/apple-dart.c: paging attach rollback logic (reached tracking before touching each stream map, restoring previous domain or blocking on failure, dropping failed domain stream membership) is sound.
  • Verified exact-head (6d99e00e825d) hardware rows on J773g (M4 mini) and J700 (Neo, including USB).
Evidence
  • host-tests CI log (run 45797470485): FAIL apple/bcm4377-radio-sids: error: implicit declaration of function 'apple_dart_attach_paging_undo' and 'apple_dart_attach_paging_drop_streams'.

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 29, 2026
apple_dart_attach_dev_paging_unguarded() now calls
apple_dart_attach_paging_undo() and apple_dart_attach_paging_drop_streams(),
which the harness did not extract, so host-tests failed with an implicit
function declaration. Add both to the definition list, with the
iommu_domain ops/type and apple_dart_domain finalized stub fields the undo
helper reads.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Pushed d29e548: host-tests failed because bcm4377-radio-sids.py extracts apple_dart_attach_dev_paging_unguarded() without the two new helpers. The definition list now also extracts apple_dart_attach_paging_undo() and apple_dart_attach_paging_drop_streams(), and the harness stubs gain the iommu_domain ops/type and apple_dart_domain finalized fields the undo helper reads.
Test-only change, apple-dart.c is untouched, so the J773g/J700 rows still apply.
Locally: apple/bcm4377-radio-sids passes, and run-host-tests.py --only dart passes 18/18.
Still needed: CI on the new head and App re-review. Thanks @DjDeveloperr.

@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: FINISH, then merge at d29e5482.

Blockers:

  1. Merge conflict with main (mergeable: CONFLICTING, mergeStateStatus: DIRTY): Because the branch conflicts with main, GitHub cannot create the merge ref and none of the pull_request CI workflows (host-tests, apple-build, KUnit) have been triggered at d29e5482. Merge main into the branch (normal merge commit, no force push) so it is MERGEABLE and CI runs green.

Checked: Full 4-file diff (drivers/iommu/apple-dart.c, tools/asahi/apple-dart-attach-undo-tests.py, tools/asahi/run-host-tests.py, tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py), delta commit d29e548257, Signed-off-by lines on both commits, and live mergeability/CI status.

Evidence
  • Prior bcm4377-radio-sids blocker resolved (d29e548257):
    • tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py now extracts static void apple_dart_attach_paging_undo( and static void apple_dart_attach_paging_drop_streams(, and adds const void *ops; int type; to struct iommu_domain and bool finalized; to struct apple_dart_domain (matching the fields read by apple_dart_attach_paging_undo()).
    • drivers/iommu/apple-dart.c is untouched relative to 6d99e00e (which already carries J773g and J700 PASS hardware rows); local apple/bcm4377-radio-sids and 18/18 DART host tests are reported passing.
  • Live GitHub state at d29e5482: mergeable: CONFLICTING, mergeStateStatus: DIRTY, only readiness / readiness (QUEUED) present — no host-tests or build workflows triggered until main is merged in.

Resolve apple-dart.c: keep the attach undo helpers next to main's
apple_dart_cfg_pm_get()/put(), and on a failed attach run the undo and
drop the streams (which covers main's managed-DART stream removal) before
apple_dart_cfg_pm_put(). The radio-sids harness extracts all four helpers;
apple-dart-attach-undo-tests.py extracts the PM helpers too, as optional
for older revisions.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Pushed 99b2c50, a forward merge of main (1957bb7) into this branch. No force push.
apple-dart.c conflicted with main's new apple_dart_cfg_pm_get()/put(). I kept both helper sets. A failed attach now runs the undo and drops the streams, which covers main's managed-DART-only stream removal, and then calls apple_dart_cfg_pm_put(). bcm4377-radio-sids.py extracts all four helpers, and apple-dart-attach-undo-tests.py extracts the PM helpers too (optional, so --ref on older revisions still works).
Locally: run-host-tests.py --only dart passes 24/24, bcm4377-radio-sids passes, and drivers/iommu/apple-dart.o builds clean (arm64 LLVM W=1, defconfig+asahi.config, 16K, J713_DCP=y).
Still needed: CI on the new head and App re-review. The attach error path changed shape in the merge, so a short J700 or J773g attach smoke at 99b2c50 would be good.

@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: HOLD — forward merge and attach-undo/PM integration clean, awaiting queued CI and exact-head attach smoke row at 99b2c50c0c27

Blockers:

  1. 19 CI checks at 99b2c50c0c27 are still queued and must finish green.
  2. Exact-head attach smoke row on J700 or J773g at 99b2c50c0c27 remains open as noted on the PR (since the error path merged apple_dart_cfg_pm_put() alongside apple_dart_attach_paging_undo() / drop_streams()).

Checked:

  • Forward merge 99b2c50c0c27 of main (1957bb76) across all 4 files (MERGEABLE on main):
    • drivers/iommu/apple-dart.c: apple_dart_attach_dev_paging_unguarded() tracks reached = i + 1 per stream map, runs apple_dart_attach_paging_undo(domain, cfg, reached, old) and (if streams_added) apple_dart_attach_paging_drop_streams(dart_domain, cfg, reached) before apple_dart_cfg_pm_put(cfg) on error; managed DARTs are quarantined and dropped while reached locked non-managed streams remain tracked so apple_dart_domain_free() unmaps them before freeing tables.
    • Host test harnesses (apple-bcm4377-radio-sids.py and apple-dart-attach-undo-tests.py) updated to extract both the undo helpers and apple_dart_cfg_pm_get()/put(), passing 24/24 --only dart host tests and clean W=1 arm64 apple-dart.o build.
Evidence
  • Verified apple_dart_attach_paging_drop_streams() handles managed DART stream removal (superseding main's managed-only apple_dart_mod_streams(..., false) on error) while preserving locked non-managed stream tracking for safe teardown.
  • No code blockers found at 99b2c50c0c27.

@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 29, 2026
@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed merge-conflict Conflicts with the base branch (bot) labels Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

99b2c50 J700 (MacBook Neo, T8140) PASS: combined guarded one-shot 7.3.0-aurora-peer1+ on main 498d53fd (clean merge of 99b2c50c0c27, default cmdline, saved Stage 2 unchanged; host test 14/14 PASS); GPU (api.smoke 8/8, synchronization.smoke 12/12), Wi-Fi, and Bluetooth all up cleanly, 0 apple-dart faults, s2idle 1/1 (rtc0), Enforcing, 0 AVC, 0 failed units; restored saved 7.3.0-aurora-edge.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

I merged current main into this branch (797ec253, normal push, no rebase, your commits unchanged). The last CI failures came from main itself on 11-29: the apple-scaler-recovery and g17-compute-owed-stamp fixtures, plus the apple-dcp harness break fixed in #3672. Re-running a job would reuse the old merge commit, so a fresh merge was the only way to clear them. Both merges were conflict-free. Host tests pass on the merged tree. The PR's own code is unchanged from 99b2c50c, so the existing hardware rows still cover it. Next: CI at this head and the review app's verdict.

@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 30, 2026

@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: HOLD at 797ec253.

Checked: Compared the PR patch before and after the forward merge. The file set and added and removed lines match the reviewed revision. Keep hosted checks and attach smoke on J700 or J773g for the paging rollback integration.

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Dec 1, 2026

@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 797ec253b1e9

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: Compared the PR patch before and after the forward merge. The file set and added and removed lines match the reviewed revision. Keep hosted checks and attach smoke on J700 or J773g for the paging rollback integration.

Follow-ups (not blocking): hosted checks; J700 or J773g attach smoke

This branch has not been deployed

No deployments
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) area:platform Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants