fix(libc): keep write tracking when a run of shared guest pages is split #11552

Merged
thuanquang merged 1 commit from upstream-pr/11552 into main 2026-11-09 21:35:58 +00:00
thuanquang commented 2026-10-26 18:35:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-26T18:35:36Z.


What

Since #9505, a run of shared guest pages is one host view. When a guest munmap or MAP_FIXED remap covers part of a run, breakView takes these steps:

  1. It unmaps the whole view.
  2. It maps both surviving halves PAGE_EXECUTE_READWRITE.
  3. Only then does it restore each part's recorded protection.

Between steps 2 and 3, a store from another guest thread to an armed (write-tracked, read-only) page of the surviving part does not fault. So HandleWrite never runs. The page then returns to read-only with armed, seen and its clean range unchanged. Collect never reports the store, and the GPU keeps a stale copy.

breakView now copies the armed 4 KiB subpages of the run before the unmap. After the restore, it compares each one with its copy and invalidates the subpages whose bytes changed. Single-threaded results are the same as on main, because a subpage is reported only when its bytes changed during the window.

This is point 2 of the post-merge review of #9505, which its author asked us to fix (comment).

Cheaper detection, tried and ruled out (Windows 11 26200, small probe programs):

  • Map the halves read-only and raise them in the restore loop. VirtualProtect cannot raise a view mapped PAGE_EXECUTE_READ or PAGE_READONLY to read-write (error 87).
  • Map with PAGE_GUARD. A guard fires once per page. After any thread's first access, a store from a second thread goes through, so it isn't exact.
  • Working-set tracking. Windows' working-set change log and QueryWorkingSetEx let you ask which pages of the new view were touched during the window. Neither records stores made in kernel mode (for example ReadFile into the page). After a working-set trim, QueryWorkingSetEx reports no page as touched.

Cost. The copy and compare cost scales with the run's armed bytes. Splitting a run with no armed page costs the same as on main. A split of a fully armed run is slower: each value is the median of 7 splits of one run, measured with this header and with main's on the same machine:

run main this change
16 MiB, no page armed 1.90 / 1.94 ms 1.98 / 2.71 ms
16 MiB, every page armed 1.90 / 1.95 ms 35.88 / 36.14 ms
256 MiB, no page armed 37.66 / 39.14 ms 40.58 / 39.42 ms
256 MiB, every page armed 43.72 / 43.30 ms 638.03 / 619.30 ms

Two alternating rounds are shown. A persistent copy buffer roughly halves the armed cost (17 ms and 335 ms), but it keeps up to a run's size of memory, so this change allocates per split instead. The remaining cost is mostly the first touch of each page of the freshly mapped view.

Tested

Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release. Head 18095f023b on main f95acfc948.

  • New check in windows_write_watch. It maps a 64-page run as one view, arms every other page from 12 to 50, and splits the run at page 10. Meanwhile a writer thread polls VirtualQuery on page 50 and stores a new byte only while it sees PAGE_EXECUTE_READWRITE, which happens only inside the window. Collect(Armed) must then report exactly that subpage when its byte changed, and nothing when it didn't. The check stops after 16 stores in the window or 2000 rounds, and it fails if no store landed in the window at all, so a run where the race never happened cannot pass.
    • With main's WindowsMappings.hpp: fails in 5 of 5 runs with a store that raced the split of its run was not reported, within the first round of each run.
    • With this change: passes in 10 of 10 runs.
    • An earlier version that reported every armed subpage after a split fails the same check with a split of a run reported a page nobody stored to.
  • ^(windows_write_watch|windows_mappings_release.*|guest_memory|windows_shared_map_cost)$: 7 of 7 pass, in 3 of 3 rounds.

Not tested: Linux and macOS (WindowsMappings is Windows-only). No title is known to split armed runs. Persona 5 Royal's reachable scenes made one flexible map and no unmap in a 330 s memory trace.

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: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5022725985 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11552). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-26T18:35:36Z. --- ### What Since #9505, a run of shared guest pages is one host view. When a guest `munmap` or `MAP_FIXED` remap covers part of a run, `breakView` takes these steps: 1. It unmaps the whole view. 2. It maps both surviving halves `PAGE_EXECUTE_READWRITE`. 3. Only then does it restore each part's recorded protection. Between steps 2 and 3, a store from another guest thread to an armed (write-tracked, read-only) page of the surviving part does not fault. So `HandleWrite` never runs. The page then returns to read-only with `armed`, `seen` and its clean range unchanged. `Collect` never reports the store, and the GPU keeps a stale copy. `breakView` now copies the armed 4 KiB subpages of the run before the unmap. After the restore, it compares each one with its copy and invalidates the subpages whose bytes changed. Single-threaded results are the same as on `main`, because a subpage is reported only when its bytes changed during the window. This is point 2 of the post-merge review of #9505, which its author asked us to fix ([comment](https://github.com/boykopovar/AnyPS5/pull/9505#issuecomment-6408543887)). **Cheaper detection, tried and ruled out (Windows 11 26200, small probe programs):** - **Map the halves read-only and raise them in the restore loop.** `VirtualProtect` cannot raise a view mapped `PAGE_EXECUTE_READ` or `PAGE_READONLY` to read-write (error 87). - **Map with `PAGE_GUARD`.** A guard fires once per page. After any thread's first access, a store from a second thread goes through, so it isn't exact. - **Working-set tracking.** Windows' working-set change log and `QueryWorkingSetEx` let you ask which pages of the new view were touched during the window. Neither records stores made in kernel mode (for example `ReadFile` into the page). After a working-set trim, `QueryWorkingSetEx` reports no page as touched. **Cost.** The copy and compare cost scales with the run's armed bytes. Splitting a run with no armed page costs the same as on `main`. A split of a fully armed run is slower: each value is the median of 7 splits of one run, measured with this header and with `main`'s on the same machine: | run | `main` | this change | |---|---|---| | 16 MiB, no page armed | 1.90 / 1.94 ms | 1.98 / 2.71 ms | | 16 MiB, every page armed | 1.90 / 1.95 ms | 35.88 / 36.14 ms | | 256 MiB, no page armed | 37.66 / 39.14 ms | 40.58 / 39.42 ms | | 256 MiB, every page armed | 43.72 / 43.30 ms | 638.03 / 619.30 ms | Two alternating rounds are shown. A persistent copy buffer roughly halves the armed cost (17 ms and 335 ms), but it keeps up to a run's size of memory, so this change allocates per split instead. The remaining cost is mostly the first touch of each page of the freshly mapped view. ### Tested Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release. Head `18095f023b` on `main` `f95acfc948`. - **New check in `windows_write_watch`.** It maps a 64-page run as one view, arms every other page from 12 to 50, and splits the run at page 10. Meanwhile a writer thread polls `VirtualQuery` on page 50 and stores a new byte only while it sees `PAGE_EXECUTE_READWRITE`, which happens only inside the window. `Collect(Armed)` must then report exactly that subpage when its byte changed, and nothing when it didn't. The check stops after 16 stores in the window or 2000 rounds, and it fails if no store landed in the window at all, so a run where the race never happened cannot pass. - With `main`'s `WindowsMappings.hpp`: fails in 5 of 5 runs with `a store that raced the split of its run was not reported`, within the first round of each run. - With this change: passes in 10 of 10 runs. - An earlier version that reported every armed subpage after a split fails the same check with `a split of a run reported a page nobody stored to`. - `^(windows_write_watch|windows_mappings_release.*|guest_memory|windows_shared_map_cost)$`: 7 of 7 pass, in 3 of 3 rounds. Not tested: Linux and macOS (`WindowsMappings` is Windows-only). No title is known to split armed runs. Persona 5 Royal's reachable scenes made one flexible map and no unmap in a 330 s memory trace. ### 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: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-26 18:45:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T18:45:53Z.

Review: CHANGES_REQUESTED


The new race check can pass without exercising the race. The loop stops after Rounds even when raced remains zero, and every iteration then accepts an empty Collection::Armed result because stored is false. That makes scheduler timing capable of turning this regression test into a false green (and the current hosted Windows product job is skipped). Please assert after the loop that at least one store was observed during the writable window, or otherwise synchronize the writer so the test proves that the window was hit before it reports success. The reported 8/8 runs are useful, but they do not make the zero-race path fail.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5657283729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11552#pullrequestreview-5657283729). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T18:45:53Z. **Review:** CHANGES_REQUESTED --- The new race check can pass without exercising the race. The loop stops after `Rounds` even when `raced` remains zero, and every iteration then accepts an empty `Collection::Armed` result because `stored` is false. That makes scheduler timing capable of turning this regression test into a false green (and the current hosted Windows product job is skipped). Please assert after the loop that at least one store was observed during the writable window, or otherwise synchronize the writer so the test proves that the window was hit before it reports success. The reported 8/8 runs are useful, but they do not make the zero-race path fail.
MarcZX10 commented 2026-10-26 18:51:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T18:51:24Z.

Review: APPROVED


The new Require(raced > 0, ...) closes the false-green path I flagged: raced increments only after the writer observes PAGE_EXECUTE_READWRITE and performs its store, and the check now fails if no such store occurs in the 2,000-round limit. The author reports 10/10 runs on Windows with the regression, 5/5 failures on the old implementation, and 7 focused write-watch/mapping tests passing in 3 rounds. I did not run Windows locally. Current conventions, eligibility, and labels pass; the applicable product/relinker checks are still running or skipped by path policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5657357494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11552#pullrequestreview-5657357494). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T18:51:24Z. **Review:** APPROVED --- The new `Require(raced > 0, ...)` closes the false-green path I flagged: `raced` increments only after the writer observes `PAGE_EXECUTE_READWRITE` and performs its store, and the check now fails if no such store occurs in the 2,000-round limit. The author reports 10/10 runs on Windows with the regression, 5/5 failures on the old implementation, and 7 focused write-watch/mapping tests passing in 3 rounds. I did not run Windows locally. Current conventions, eligibility, and labels pass; the applicable product/relinker checks are still running or skipped by path policy.
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!11552
No description provided.