fix(libc): stop macOS write-watch fault-ahead at the end of a driver's own store #20126

Merged
XIIYAZ merged 1 commit from upstream-pr/20126 into main 2026-12-04 02:02:17 +00:00
XIIYAZ commented 2026-11-14 17:08:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T17:08:06Z.


What

On macOS, agc_driver_native_depth_publication stops in the case samples=2 write=None unmap=Middle:

AGC graphics: guest writes raced native multisample depth publication

There is no guest write in that case. The macOS write watch tracks writes by write-protecting pages and handling the fault. Its fault-ahead (_faultAhead, default 32) works like this: when a write faults on a page and the two pages before it were just opened for writing, the handler opens the following pages of the same 64 KiB block in one mprotect and marks them Written before anything writes them.

The publication commits one plane in parts, because the hole splits it. WriteChangedIfHeld writes each part through storeOwn, which walks the write watch over the part's own pages before and after the store. I traced the runs those walks stamp, with addresses from one run:

WriteChangedIfHeld [10a450000..+3fff)        part A, before the hole
 storeOwn span [10a450000..10a454000) pre-walk
 run [10a450000..10a454000) kind=Driver       part A's own pages, post-walk
WriteChangedIfHeld [10a454010..+3ff0)        part B, after the hole
 storeOwn span [10a454000..10a458000) pre-walk
 run [10a454000..10a458000) kind=Cpu          pages part A's store opened ahead

Part A's sequential store faulted through its pages, and fault-ahead opened 10a454000..10a458000 too. Those pages are outside part A's span, so its post-walk did not take them. Part B's pre-walk then collected them as a CPU store. cpuUnchanged failed for the whole part, lost was the part's size, and publication refused. With APS5_WRITE_WATCH_FAULT_AHEAD=1 the whole --publication run passes (76 cases). Linux and Windows do not track writes through faults, so they never see these pages.

A driver store should not open pages beyond the range it stores. storeOwn now registers its page span with the write watch for the duration of the store (GuestWriteWatchBeginOwnStore / GuestWriteWatchEndOwnStore, through a scoped OwnStoreFaults):

  • The watch keeps these spans in _ownStores under its existing mutex, like _hostWrites.
  • The fault handler clamps fault-ahead so it does not pass the end of a span containing the faulting page.
  • Pages fault-ahead still opens inside the span are taken by storeOwn's own post-walk, as before.
  • Guest stores and stores outside a span keep the full fault-ahead.
  • The handler only reads the vector while holding FaultLock, so nothing allocates in the signal handler.
  • Off macOS both functions do nothing.

Tested

Setup:

  • Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0
  • full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under Rosetta
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
  • on main with the open macOS build fixes applied

Results:

  • agc_driver_native_depth_publication fails before this change and passes after it, with fault-ahead at its default.
  • The 27 write_watch_* and guest_arena_host* tests pass.
  • ctest -R 'native_depth|native_multisample|write_watch|write_tracking|guest_arena' (68 tests): all pass except agc_write_tracking, which fails the same way before and after. That one is the import window's host write marking every page of its range, not fault-ahead.
  • I ran ctest -R '^(agc|shader|job)' (1578 tests, -j 1) and compared it with the same run before this change: agc_driver_native_depth_publication is fixed and nothing new fails (14 failures before, 13 after).

Linux and Windows are unchanged.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5314011646 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20126). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T17:08:06Z. --- ### What On macOS, `agc_driver_native_depth_publication` stops in the case `samples=2 write=None unmap=Middle`: ``` AGC graphics: guest writes raced native multisample depth publication ``` There is no guest write in that case. The macOS write watch tracks writes by write-protecting pages and handling the fault. Its fault-ahead (`_faultAhead`, default 32) works like this: when a write faults on a page and the two pages before it were just opened for writing, the handler opens the following pages of the same 64 KiB block in one `mprotect` and marks them `Written` before anything writes them. The publication commits one plane in parts, because the hole splits it. `WriteChangedIfHeld` writes each part through `storeOwn`, which walks the write watch over the part's own pages before and after the store. I traced the runs those walks stamp, with addresses from one run: ``` WriteChangedIfHeld [10a450000..+3fff) part A, before the hole storeOwn span [10a450000..10a454000) pre-walk run [10a450000..10a454000) kind=Driver part A's own pages, post-walk WriteChangedIfHeld [10a454010..+3ff0) part B, after the hole storeOwn span [10a454000..10a458000) pre-walk run [10a454000..10a458000) kind=Cpu pages part A's store opened ahead ``` Part A's sequential store faulted through its pages, and fault-ahead opened `10a454000..10a458000` too. Those pages are outside part A's span, so its post-walk did not take them. Part B's pre-walk then collected them as a CPU store. `cpuUnchanged` failed for the whole part, `lost` was the part's size, and publication refused. With `APS5_WRITE_WATCH_FAULT_AHEAD=1` the whole `--publication` run passes (76 cases). Linux and Windows do not track writes through faults, so they never see these pages. A driver store should not open pages beyond the range it stores. `storeOwn` now registers its page span with the write watch for the duration of the store (`GuestWriteWatchBeginOwnStore` / `GuestWriteWatchEndOwnStore`, through a scoped `OwnStoreFaults`): - The watch keeps these spans in `_ownStores` under its existing mutex, like `_hostWrites`. - The fault handler clamps fault-ahead so it does not pass the end of a span containing the faulting page. - Pages fault-ahead still opens inside the span are taken by `storeOwn`'s own post-walk, as before. - Guest stores and stores outside a span keep the full fault-ahead. - The handler only reads the vector while holding `FaultLock`, so nothing allocates in the signal handler. - Off macOS both functions do nothing. ### Tested Setup: - Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0 - full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`; tests run under Rosetta - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` - on `main` with the open macOS build fixes applied Results: - `agc_driver_native_depth_publication` fails before this change and passes after it, with fault-ahead at its default. - The 27 `write_watch_*` and `guest_arena_host*` tests pass. - `ctest -R 'native_depth|native_multisample|write_watch|write_tracking|guest_arena'` (68 tests): all pass except `agc_write_tracking`, which fails the same way before and after. That one is the import window's host write marking every page of its range, not fault-ahead. - I ran `ctest -R '^(agc|shader|job)'` (1578 tests, `-j 1`) and compared it with the same run before this change: `agc_driver_native_depth_publication` is fixed and nothing new fails (14 failures before, 13 after). Linux and Windows are unchanged. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
kurexw commented 2026-11-16 20:51:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-16T20:51:57Z.

Review: COMMENTED


Tested on an Apple GPU: Apple M4, macOS 27.2, MoltenVK 1.4.2 (Vulkan 1.4), with the Vulkan loader and validation layers from LunarG SDK 1.4.363.1. Full build, x86-64 under Rosetta 2. This PR was merged alone onto main 3b17977ba6 with git merge --no-ff.

All 30 ctest entries that run agc_driver_native_multisample_depth_tests (one per argument mode, from no arguments to --driver-queued-fill) were run once on main and once with this PR, and --publication three times on each side. Every run used ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer.

  • --publication (agc_driver_native_depth_publication) fails in all 3 runs on main with "AGC graphics: guest writes raced native multisample depth publication", right after the samples=2 write=3 cases. It passes in all 3 runs with this PR.
  • The other 29 modes pass on both sides.
  • Every log contains Insert instance layer "VK_LAYER_KHRONOS_validation", none contains was not found but was requested by env var VK_INSTANCE_LAYERS, and neither side logs any VUID- error.

Not run here: the write-watch suite (CI covers it on macOS) and an Intel Mac.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5927426198 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20126#pullrequestreview-5927426198). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-16T20:51:57Z. **Review:** COMMENTED --- Tested on an Apple GPU: Apple M4, macOS 27.2, MoltenVK 1.4.2 (Vulkan 1.4), with the Vulkan loader and validation layers from LunarG SDK 1.4.363.1. Full build, x86-64 under Rosetta 2. This PR was merged alone onto `main` 3b17977ba6 with `git merge --no-ff`. All 30 ctest entries that run `agc_driver_native_multisample_depth_tests` (one per argument mode, from no arguments to `--driver-queued-fill`) were run once on `main` and once with this PR, and `--publication` three times on each side. Every run used `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer`. - `--publication` (`agc_driver_native_depth_publication`) fails in all 3 runs on `main` with "AGC graphics: guest writes raced native multisample depth publication", right after the `samples=2 write=3` cases. It passes in all 3 runs with this PR. - The other 29 modes pass on both sides. - Every log contains `Insert instance layer "VK_LAYER_KHRONOS_validation"`, none contains `was not found but was requested by env var VK_INSTANCE_LAYERS`, and neither side logs any `VUID-` error. Not run here: the write-watch suite (CI covers it on macOS) and an Intel Mac.
MarcZX10 commented 2026-11-30 10:31:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T10:31:20Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected implementation bounds write-watch fault-ahead to the active own-store span on macOS.

What I ran

Reviewed head 422efbbb6aa06ac05ba0fba525a50c7356ca3837. Read the prepared description, commit list, manifest, diff, and all changed lines in the three changed files. Used repo_read.py to inspect the nearby fault-lock, write-watch range, and store call paths. No build or tests were run in this review.

Results

Check Result Note
Changed-line review passed All changed lines read
Targeted call-path review passed Locking and store scope inspected
Build and tests not run Author reports tests in the description

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build, test suite, or physical macOS/Linux/Windows runtime was run. The author-reported test results were not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6110526169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20126#pullrequestreview-6110526169). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-30T10:31:20Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected implementation bounds write-watch fault-ahead to the active own-store span on macOS. ## What I ran Reviewed head `422efbbb6aa06ac05ba0fba525a50c7356ca3837`. Read the prepared description, commit list, manifest, diff, and all changed lines in the three changed files. Used `repo_read.py` to inspect the nearby fault-lock, write-watch range, and store call paths. No build or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Changed-line review | passed | All changed lines read | | Targeted call-path review | passed | Locking and store scope inspected | | Build and tests | not run | Author reports tests in the description | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No build, test suite, or physical macOS/Linux/Windows runtime was run. The author-reported test results were not independently verified. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 22:24:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:24:06Z.

Review: COMMENTED


The trial merge onto a1b75b0947 was clean. OwnStoreFaults registers a page-aligned scope around the actual write and removes it on exit, and the fault handler clips its look-ahead at the registered end. Please retain the split-depth-publication case with default fault-ahead enabled. Disabling fault-ahead avoids the failing path altogether. The platform-specific behavior needs the paired macOS logs. My review is source-based.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6179664440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20126#pullrequestreview-6179664440). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:24:06Z. **Review:** COMMENTED --- The trial merge onto `a1b75b0947` was clean. `OwnStoreFaults` registers a page-aligned scope around the actual write and removes it on exit, and the fault handler clips its look-ahead at the registered end. Please retain the split-depth-publication case with default fault-ahead enabled. Disabling fault-ahead avoids the failing path altogether. The platform-specific behavior needs the paired macOS logs. My review is source-based. AI-assisted: yes.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20126
No description provided.