fix(agc): cache Linux /proc/self/maps regions for describePages #1114

Closed
prompterror wants to merge 1 commit from upstream-pr/1114 into main
prompterror commented 2026-10-07 12:57:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by prompterror on 2026-10-07T12:57:22Z.


What

On Linux the AGC driver's guest memory is ordinary host memory (malloc/mmap), so a page's protection comes from /proc/self/maps rather than a Windows-style reserved arena. describePages re-read and re-parsed that file for every range the driver verified — no caching, unlike the Windows path (VirtualQuery arena table).

Measured in Dreaming Sarah (2D title, otherwise reaches 60 FPS):

  • ~41,000 read()/s + ~1,600 openat+close/s on one hot thread re-reading all of /proc/self/maps
  • one CPU thread pegged ~100% of a core (≈ half in sys), GPU at ~9%, < 7 FPS
  • 10-second verify window took ~7 s of that

Fix

Adds LinuxPageCache:

  • std::map of parsed regions keyed by begin address, guarded by a shared_mutex
  • O(log n) lookups; a missed query refreshes the whole cache once, so changes made outside the guest-allocation registry are seen on the next query
  • the registry invalidator (ForgetPages) is registered unconditionally on Linux and drops only the affected ranges (splitting/re-inserting partial regions), matching the existing arena/image escape hatches

Result: verify window ~7 s → ~0.1 s, title back to 59.96 FPS on the same profile run. Windows path untouched (#ifdef _WIN32/#else split).

Testing

  • GuestMemory.cpp compiles clean (-std=c++20, -Wall set from CI's flags) against current main
  • Validated end-to-end on a Linux (Ubuntu, kernel 7.0) host with the Dreaming Sarah title
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4772964877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1114). Originally posted by [prompterror](https://github.com/prompterror) on 2026-10-07T12:57:22Z. --- ## What On Linux the AGC driver's guest memory is ordinary host memory (`malloc`/`mmap`), so a page's protection comes from `/proc/self/maps` rather than a Windows-style reserved arena. `describePages` re-read and re-parsed that file for **every range the driver verified** — no caching, unlike the Windows path (`VirtualQuery` arena table). Measured in **Dreaming Sarah** (2D title, otherwise reaches 60 FPS): - ~41,000 `read()`/s + ~1,600 `openat`+`close`/s on one hot thread re-reading all of `/proc/self/maps` - one CPU thread pegged ~100% of a core (≈ half in sys), GPU at ~9%, **< 7 FPS** - 10-second `verify` window took ~7 s of that ## Fix Adds `LinuxPageCache`: - `std::map` of parsed regions keyed by begin address, guarded by a `shared_mutex` - O(log n) lookups; a missed query refreshes the whole cache **once**, so changes made outside the guest-allocation registry are seen on the next query - the registry invalidator (`ForgetPages`) is registered unconditionally on Linux and drops only the affected ranges (splitting/re-inserting partial regions), matching the existing arena/image escape hatches Result: `verify` window ~7 s → **~0.1 s**, title back to **59.96 FPS** on the same profile run. Windows path untouched (`#ifdef _WIN32`/`#else` split). ## Testing - `GuestMemory.cpp` compiles clean (`-std=c++20`, `-Wall` set from CI's flags) against current `main` - Validated end-to-end on a Linux (Ubuntu, kernel 7.0) host with the Dreaming Sarah title
Dextroid17 commented 2026-10-08 02:05:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T02:05:02Z.

Review: COMMENTED


This breaks an existing test on Linux — the platform the change is for — so it is blocking in my reading. I bisected it to GuestMemory.cpp rather than guessing.

Failing test: agc_driver_graphics, 1 of 395 on this branch:

70 - agc_driver_graphics (Failed)
AGC graphics: the released test stack is misreported

Bisection. Same tree, only GuestMemory.cpp swapped to the commit's parent:

pr-1114 (your GuestMemory.cpp):   FAILS   0% tests passed, 1 tests failed out of 1
pr-1114^ (parent GuestMemory.cpp): PASSES  100% tests passed out of 1

So it is this file, and it reproduces on every run — not a flake.

The assertion is tests/LiveStack.cpp:69, after a stack is built and one thread has run on it:

Require(GuestMemory::Accessible(block + page, 64, true)
        && !GuestMemory::Accessible(block, 16)
        && !GuestMemory::Accessible(block + stackBytes, 16),
        "the released test stack is misreported");

The block is mmaped read-write and then both guard pages are mprotected to PROT_NONE (lines 45–47). The middle must report accessible and the two guards must report inaccessible. One of those two negatives now reports positive through the new cache.

Why I think the cache is the cause. LinuxPageCache::find returns a region that covers the address, and the refresh happens only on a miss:

"a lookup that misses any cached region refreshes the whole cache once"

A stale entry that still covers the queried address is a hit, so nothing is re-read — and that is the one case where the answer is wrong. This test is close to a worst case for it: the block is freshly mmaped and can reuse an address the cache still describes from an earlier, differently-protected mapping. A negative answer (guard page not accessible) is exactly what a stale positive turns into a wrong one. I did not instrument which of the three conditions fails, so treat the mechanism as the explanation consistent with the evidence rather than as measured; the bisection and the reproduction are measured.

Direction, if it helps. Refreshing on miss cannot see a change at an address the cache believes it covers, so the hit path has to be able to revalidate — which is awkward here because this test (and anything else using raw mmap/mprotect) never calls ForgetPages, so the invalidator cannot be relied on. Two shapes that would both be sound: revalidate a hit against /proc/self/maps (the cache then caches the parse, not the answer), or keep the answer cache but give it a bounded lifetime/generation so a stale region cannot be trusted indefinitely. Making the test call ForgetPages would hide the problem rather than fix it, since the failure case is precisely a mapping the registry never saw.

Environment, so you can reproduce exactly: Linux (CachyOS), GCC, Release, NVIDIA RTX 3080 Ti. I ran the suite with APS5_WRITE_WATCH_IMPORTS=watch because this branch predates the dma-buf gating in #1329; without it the NVIDIA probe failures mask other results. The other 394 tests pass, so the rest of the change is clean here.

Happy to bisect further or instrument which condition fails if that is useful — say the word.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450559970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1114#pullrequestreview-5450559970). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T02:05:02Z. **Review:** COMMENTED --- This breaks an existing test on Linux — the platform the change is for — so it is blocking in my reading. I bisected it to `GuestMemory.cpp` rather than guessing. **Failing test:** `agc_driver_graphics`, `1 of 395` on this branch: ``` 70 - agc_driver_graphics (Failed) AGC graphics: the released test stack is misreported ``` **Bisection.** Same tree, only `GuestMemory.cpp` swapped to the commit's parent: ``` pr-1114 (your GuestMemory.cpp): FAILS 0% tests passed, 1 tests failed out of 1 pr-1114^ (parent GuestMemory.cpp): PASSES 100% tests passed out of 1 ``` So it is this file, and it reproduces on every run — not a flake. **The assertion** is `tests/LiveStack.cpp:69`, after a stack is built and one thread has run on it: ```cpp Require(GuestMemory::Accessible(block + page, 64, true) && !GuestMemory::Accessible(block, 16) && !GuestMemory::Accessible(block + stackBytes, 16), "the released test stack is misreported"); ``` The block is `mmap`ed read-write and then both guard pages are `mprotect`ed to `PROT_NONE` (lines 45–47). The middle must report accessible and the two guards must report *inaccessible*. One of those two negatives now reports positive through the new cache. **Why I think the cache is the cause.** `LinuxPageCache::find` returns a region that *covers* the address, and the refresh happens only on a miss: > *"a lookup that misses any cached region refreshes the whole cache once"* A stale entry that still covers the queried address is a **hit**, so nothing is re-read — and that is the one case where the answer is wrong. This test is close to a worst case for it: the block is freshly `mmap`ed and can reuse an address the cache still describes from an earlier, differently-protected mapping. A negative answer (guard page not accessible) is exactly what a stale positive turns into a wrong one. I did not instrument which of the three conditions fails, so treat the mechanism as the explanation consistent with the evidence rather than as measured; the bisection and the reproduction are measured. **Direction, if it helps.** Refreshing on miss cannot see a *change* at an address the cache believes it covers, so the hit path has to be able to revalidate — which is awkward here because this test (and anything else using raw `mmap`/`mprotect`) never calls `ForgetPages`, so the invalidator cannot be relied on. Two shapes that would both be sound: revalidate a hit against `/proc/self/maps` (the cache then caches the *parse*, not the answer), or keep the answer cache but give it a bounded lifetime/generation so a stale region cannot be trusted indefinitely. Making the test call `ForgetPages` would hide the problem rather than fix it, since the failure case is precisely a mapping the registry never saw. **Environment**, so you can reproduce exactly: Linux (CachyOS), GCC, Release, NVIDIA RTX 3080 Ti. I ran the suite with `APS5_WRITE_WATCH_IMPORTS=watch` because this branch predates the dma-buf gating in #1329; without it the NVIDIA probe failures mask other results. The other 394 tests pass, so the rest of the change is clean here. Happy to bisect further or instrument which condition fails if that is useful — say the word. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
oneandonlydean commented 2026-10-08 02:44:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T02:44:09Z.


Closing as a duplicate of #157, which removes the same /proc/self/maps re-read with PROCMAP_QUERY and keeps no cache that can go stale; the stale hit is also what fails agc_driver_graphics in the review above. If #157 misses a case you hit, please comment on #157 with what it misses.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051101444 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1114#issuecomment-6051101444). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T02:44:09Z. --- Closing as a duplicate of #157, which removes the same `/proc/self/maps` re-read with `PROCMAP_QUERY` and keeps no cache that can go stale; the stale hit is also what fails `agc_driver_graphics` in the review above. If #157 misses a case you hit, please comment on #157 with what it misses.

Pull request closed

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!1114
No description provided.