feat(libc): track guest memory with windows write-watch tokens #13784

Merged
Celegans12 merged 1 commit from upstream-pr/13784 into main 2026-11-04 08:32:06 +00:00
Celegans12 commented 2026-10-31 06:10:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-31T06:10:13Z.


What

libc has no host-side token for checking whether cached guest bytes changed. Add GuestArenaArmOwned_nid_no_patch and GuestArenaOwnedUnchanged_nid_no_patch over whole 64 KiB blocks, using Windows GetWriteWatch.

Arming preserves consumed dirty pages for collection and eviction. Mapping changes, pins, host access, protection notifications and clearing collections invalidate tokens under the mapping mutex. Guest page protection stays unchanged. Linux and other unsupported platforms return false; private memory requires flag 1. Pending and newly reported pages are merged using a sorted lookup before collection returns them. The token and pending maps are capped independently at 65,536 blocks. Token pressure invalidates lowest-address proofs; pending pressure refuses new blocks before resetting their writes. Existing pending blocks can still be armed. The byte-only contract and unmeasured title impact are recorded in libc debt.

This rebase incorporates merged #13220's standalone write-watch registration fix, which addresses the inherited configure failure in the old macOS jobs. Token behavior is unchanged. Driver consumers, GPU stamps and epochs remain separate; no title performance claim. #11552 and #6926 supply different APIs. Adjacent libc debt entries from #12184/#12849 must be retained together. The current overlap scan left 20 shared-file comparisons unresolved. #10025 adds zero-page mapping and must preserve token invalidation; #5021 changes test registration. The other unresolved changes concern independent platform, registration or debt work.

Tested

Based on main 66e1f7f993. Windows 11, MinGW-w64 GCC 15.2.0, Release, -DBUILD_TESTING=ON: eight mapping tests passed three times, including guest_owned_memory_limits. Its eight-entry fixture checks pending saturation, refusal before reset, reuse after collection and token eviction; removing either bound made it fail. A synthetic 512-block workload reproduced unbounded growth on the previous head. A separate 8,192-page collection improved from 6.1–6.4 ms to 0.35–0.37 ms before the rebase.

A current-main merge with the dependent draw and storage changes passed 10 selected Windows tests and seven Linux tests three times each. Linux does not exercise Windows write-watch. Earlier Ubuntu 24.04/GCC 13.3 host tests passed three times; unsupported platforms refuse ownership. Not tested: macOS, titles, console behavior or title performance. These synthetic results do not establish frame impact.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5095110296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13784). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-31T06:10:13Z. --- ### What libc has no host-side token for checking whether cached guest bytes changed. Add `GuestArenaArmOwned_nid_no_patch` and `GuestArenaOwnedUnchanged_nid_no_patch` over whole 64 KiB blocks, using Windows [GetWriteWatch](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-getwritewatch). Arming preserves consumed dirty pages for collection and eviction. Mapping changes, pins, host access, protection notifications and clearing collections invalidate tokens under the mapping mutex. Guest page protection stays unchanged. Linux and other unsupported platforms return false; private memory requires flag 1. Pending and newly reported pages are merged using a sorted lookup before collection returns them. The token and pending maps are capped independently at 65,536 blocks. Token pressure invalidates lowest-address proofs; pending pressure refuses new blocks before resetting their writes. Existing pending blocks can still be armed. The byte-only contract and unmeasured title impact are recorded in libc debt. This rebase incorporates merged #13220's standalone write-watch registration fix, which addresses the inherited configure failure in the old macOS jobs. Token behavior is unchanged. Driver consumers, GPU stamps and epochs remain separate; no title performance claim. #11552 and #6926 supply different APIs. Adjacent libc debt entries from #12184/#12849 must be retained together. The current overlap scan left 20 shared-file comparisons unresolved. #10025 adds zero-page mapping and must preserve token invalidation; #5021 changes test registration. The other unresolved changes concern independent platform, registration or debt work. ### Tested Based on `main` 66e1f7f993. Windows 11, MinGW-w64 GCC 15.2.0, Release, `-DBUILD_TESTING=ON`: eight mapping tests passed three times, including `guest_owned_memory_limits`. Its eight-entry fixture checks pending saturation, refusal before reset, reuse after collection and token eviction; removing either bound made it fail. A synthetic 512-block workload reproduced unbounded growth on the previous head. A separate 8,192-page collection improved from 6.1–6.4 ms to 0.35–0.37 ms before the rebase. A current-main merge with the dependent draw and storage changes passed 10 selected Windows tests and seven Linux tests three times each. Linux does not exercise Windows write-watch. Earlier Ubuntu 24.04/GCC 13.3 host tests passed three times; unsupported platforms refuse ownership. Not tested: macOS, titles, console behavior or title performance. These synthetic results do not establish frame impact. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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
CnRJay commented 2026-11-01 19:47:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:47:48Z.

Review: COMMENTED


Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

Three things before this can be judged.

  1. No title ID and no consumer. "Unblocks: none", "no performance claim", and the only caller is the stacked #13793 (and later #13809). A new libc export pair with no title reaching it on main is the export-only shape the maintainers close. If the point is to make draw-input copies skippable, the evidence has to be on the consumer PR (trace_digest of main vs change, same scene), and this one should say which title/scene makes GuestArenaArmOwned hot.

  2. Linux gets nothing. "Linux native tokens are unsupported", so on Linux both functions return false and the consumer always falls back to the copy. Everything the PR adds is a Windows-only code path (GetWriteWatch), plus the same two exports being callable on every platform. Please state that in the PR title/What so nobody reads it as a general facility.

  3. Cost and growth in WindowsMappings.hpp:

  • ArmOwned and OwnedUnchanged take the mapping mutex, the same lock the map/unmap/protect paths use, and call GetWriteWatch (a syscall) once per 64 KiB block per call. For a draw that touches many blocks this is a syscall per block per draw on a lock shared with the allocator. Is there a number for how many blocks and calls per frame in a real scene?
  • collectOwnedWrites runs std::find(pages, pages + *count, pointer) for every pending bit, so merging pending pages into the result is O(pending x reported) inside the collection path that previously was a single GetWriteWatch. With a large dirty set that is quadratic work under the mapping mutex.
  • ownedWords / ownedPending only shrink on disown/unmap/pin. A title that arms many distinct blocks and never unmaps them keeps one map node per 64 KiB block forever. What bounds it?
  • nextOwned++ hands out tokens from a 64-bit counter; fine, but ArmOwned fails once count > UINT64_MAX - nextOwned + 1, and that failure is silent to the caller (just false).

Please add a counter or a trace phase for arm/check/refuse so the reuse rate can be read in a trace_digest, otherwise nobody can tell whether the feature fires.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729916907 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13784#pullrequestreview-5729916907). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:47:48Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows `Unblocks: none`; a title is only required when the PR fixes a title. The technical points below are unaffected. Three things before this can be judged. 1. No title ID and no consumer. "Unblocks: none", "no performance claim", and the only caller is the stacked #13793 (and later #13809). A new libc export pair with no title reaching it on main is the export-only shape the maintainers close. If the point is to make draw-input copies skippable, the evidence has to be on the consumer PR (trace_digest of main vs change, same scene), and this one should say which title/scene makes `GuestArenaArmOwned` hot. 2. Linux gets nothing. "Linux native tokens are unsupported", so on Linux both functions return false and the consumer always falls back to the copy. Everything the PR adds is a Windows-only code path (`GetWriteWatch`), plus the same two exports being callable on every platform. Please state that in the PR title/What so nobody reads it as a general facility. 3. Cost and growth in `WindowsMappings.hpp`: - `ArmOwned` and `OwnedUnchanged` take the mapping `mutex`, the same lock the map/unmap/protect paths use, and call `GetWriteWatch` (a syscall) once per 64 KiB block per call. For a draw that touches many blocks this is a syscall per block per draw on a lock shared with the allocator. Is there a number for how many blocks and calls per frame in a real scene? - `collectOwnedWrites` runs `std::find(pages, pages + *count, pointer)` for every pending bit, so merging pending pages into the result is O(pending x reported) inside the collection path that previously was a single `GetWriteWatch`. With a large dirty set that is quadratic work under the mapping mutex. - `ownedWords` / `ownedPending` only shrink on `disown`/unmap/pin. A title that arms many distinct blocks and never unmaps them keeps one map node per 64 KiB block forever. What bounds it? - `nextOwned++` hands out tokens from a 64-bit counter; fine, but `ArmOwned` fails once `count > UINT64_MAX - nextOwned + 1`, and that failure is silent to the caller (just `false`). Please add a counter or a trace phase for arm/check/refuse so the reuse rate can be read in a trace_digest, otherwise nobody can tell whether the feature fires.
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!13784
No description provided.