fix(libc): resolve only the read traps that cover the faulting page #18848

Merged
krishalivaibhav merged 1 commit from upstream-pr/18848 into main 2026-11-14 04:50:05 +00:00
krishalivaibhav commented 2026-11-12 12:20:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-11-12T12:20:28Z.


What

On macOS, a fault on a read-trapped page ran the resolver for every trap linked to it through shared pages, not only the traps covering the faulting page (takeTraps grew transitively). Images whose boundaries fall mid-page form such chains. In PPSA02070 seven linear targets are packed back to back with mid-page boundaries, and a 720-byte CPU write into a buffer on the last target's tail page would store the whole chain (~140 MB) every frame. Details in #18785.

  • takeTraps takes only the traps that overlap the requested pages.
  • When the resolver thread, or a thread resolving its own traps, stores into a page that another live trap still covers, the page is opened as before, but that trap moves to the same pass. resolve runs it after its own range, so its pages are never given back unresolved. This closes the gap the libc debt entry listed ("a resolver that touches the pages of a trap it was not called for gets them without that trap ever being resolved"). The handler never runs the resolver nested: the watch's signals are not SA_NODEFER, and the resolver must not run inside the handler.
  • The deferred list and _resolving are reserved up front (4096) so the handler does not allocate. Past that, the old gap remains; the debt entry says so.
  • CheckReadTrap covers two traps that share a page: with a resolver that stores its whole range, a fault on the first runs both in order on the resolver thread and gives the shared page back; with a resolver that stores only part of its range, it runs only the first, the shared page stays trapped, and a later read runs the second exactly once.

Also on GuestWriteWatch.cpp:843: #15729 changes the same condition for another reason (a GpuMutex holder bypass); whichever lands second needs a trivial rebase.

Tested

macOS 27.3 (Apple M2, x86-64 under Rosetta 2), Release.

  • All 25 write_watch_* ctest entries pass, including the new cases. Every write_watch_tests mode also passes run directly, and write_watch_backing_tests shared passes 5 out of 5.
  • PPSA02070 (Don't Starve Together) with a local driver prototype that traps adjacent targets as a group (not part of this PR): stores went from about 140 MB per frame to the one 128 KiB target the title writes next to. Without that prototype the driver refuses to trap page-sharing images, so this change alone does not change the title's speed.

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: PPSA02070 #18785
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5277277406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18848). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-11-12T12:20:28Z. --- ### What On macOS, a fault on a read-trapped page ran the resolver for every trap linked to it through shared pages, not only the traps covering the faulting page (`takeTraps` grew transitively). Images whose boundaries fall mid-page form such chains. In PPSA02070 seven linear targets are packed back to back with mid-page boundaries, and a 720-byte CPU write into a buffer on the last target's tail page would store the whole chain (~140 MB) every frame. Details in #18785. - `takeTraps` takes only the traps that overlap the requested pages. - When the resolver thread, or a thread resolving its own traps, stores into a page that another live trap still covers, the page is opened as before, but that trap moves to the same pass. `resolve` runs it after its own range, so its pages are never given back unresolved. This closes the gap the libc debt entry listed ("a resolver that touches the pages of a trap it was not called for gets them without that trap ever being resolved"). The handler never runs the resolver nested: the watch's signals are not `SA_NODEFER`, and the resolver must not run inside the handler. - The deferred list and `_resolving` are reserved up front (4096) so the handler does not allocate. Past that, the old gap remains; the debt entry says so. - `CheckReadTrap` covers two traps that share a page: with a resolver that stores its whole range, a fault on the first runs both in order on the resolver thread and gives the shared page back; with a resolver that stores only part of its range, it runs only the first, the shared page stays trapped, and a later read runs the second exactly once. Also on `GuestWriteWatch.cpp:843`: #15729 changes the same condition for another reason (a `GpuMutex` holder bypass); whichever lands second needs a trivial rebase. ### Tested macOS 27.3 (Apple M2, x86-64 under Rosetta 2), Release. - All 25 `write_watch_*` ctest entries pass, including the new cases. Every `write_watch_tests` mode also passes run directly, and `write_watch_backing_tests shared` passes 5 out of 5. - PPSA02070 (Don't Starve Together) with a local driver prototype that traps adjacent targets as a group (not part of this PR): stores went from about 140 MB per frame to the one 128 KiB target the title writes next to. Without that prototype the driver refuses to trap page-sharing images, so this change alone does not change the title's speed. ### 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: PPSA02070 #18785 - [x] GPUs tested: n/a - [x] 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!18848
No description provided.