perf(agc): answer verifications of the caller's own live stack without a page query #459

Merged
oneandonlydean merged 1 commit from upstream-pr/459 into main 2026-10-03 22:34:25 +00:00
oneandonlydean commented 2026-10-03 13:52:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-03T13:52:09Z.


What

Driver::Submit checks the title's submission packet with GuestMemory::CheckRange, and titles pass that packet on their own thread stack. Host thread stacks are neither registry mappings nor write-watched, so no cached page table covers them, and on Linux every such check scans /proc/self/maps line by line. In PPSA21564 that was about 1.5 ms per submission on the render thread, 66% of that thread's samples in perf at the title screen.

A range between the calling thread's current frame and the top of its stack holds the frames of every caller, so it is mapped readable and writable for as long as the call lasts. verify() now accepts such a range without a page query:

  • the thread's stack bounds come from pthread_getattr_np (Linux) or GetCurrentThreadStackLimits (Windows), once per thread;
  • the current frame is the address of a local in the check;
  • a thread running on another stack (a fiber's, a signal stack) has its frame outside the thread's bounds and still gets the page query, as does any range that starts below the frame or runs past the stack's top.

GetCurrentThreadStackLimits needs _WIN32_WINNT 0x0602 on MinGW, as libkernel already sets. Several targets compile GuestMemory.cpp, so the definition is a property of that source file.

#157 caches the parsed maps for these queries; if it lands, this saves less, and I will measure it again then. #157 already conflicts with main in GuestMemory.cpp.

Tested

  • Linux (CachyOS, x86-64): full build with BUILD_TESTING=ON and ctest pass (145/145).
  • New RunLiveStackAccessTests in agc_driver_graphics_tests (tests/LiveStack.cpp): a packet on the test's own stack is accessible and passes CheckRange; a thread on a stack of its own with an inaccessible page above and one at the bottom sees its stack top accessible, a range past the top and the page above it rejected (Accessible and CheckRange), and the inaccessible page below its frames rejected. After the thread ends, that memory is reported as the page query says. The checks bound the new path: dropping the past-the-top bound fails with "a range past the stack's top counts as accessible", and starting the accepted range at the stack's bottom instead of the frame fails with "an inaccessible page of the stack below the live frames counts as accessible". Without the change the test passes too, since the answers are the same; only the cost differs.
  • Cost of one 64-byte CheckRange on the stack, in the test process (a short maps file): 12.3 us before, 0.01 us after. The title's maps file has thousands of lines (mostly driver mappings), hence the 1.5 ms above.
  • PPSA21564: on my integration branch since 2026-09-30 through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS).
  • My #442 also edits GuestMemory.cpp, BdaTests.hpp and tests/Graphics.cpp; the two merge cleanly in either order.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4727049961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/459). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-03T13:52:09Z. --- ### What `Driver::Submit` checks the title's submission packet with `GuestMemory::CheckRange`, and titles pass that packet on their own thread stack. Host thread stacks are neither registry mappings nor write-watched, so no cached page table covers them, and on Linux every such check scans `/proc/self/maps` line by line. In PPSA21564 that was about 1.5 ms per submission on the render thread, 66% of that thread's samples in `perf` at the title screen. A range between the calling thread's current frame and the top of its stack holds the frames of every caller, so it is mapped readable and writable for as long as the call lasts. `verify()` now accepts such a range without a page query: - the thread's stack bounds come from `pthread_getattr_np` (Linux) or `GetCurrentThreadStackLimits` (Windows), once per thread; - the current frame is the address of a local in the check; - a thread running on another stack (a fiber's, a signal stack) has its frame outside the thread's bounds and still gets the page query, as does any range that starts below the frame or runs past the stack's top. `GetCurrentThreadStackLimits` needs `_WIN32_WINNT` 0x0602 on MinGW, as libkernel already sets. Several targets compile `GuestMemory.cpp`, so the definition is a property of that source file. #157 caches the parsed maps for these queries; if it lands, this saves less, and I will measure it again then. #157 already conflicts with `main` in `GuestMemory.cpp`. ### Tested - Linux (CachyOS, x86-64): full build with `BUILD_TESTING=ON` and `ctest` pass (145/145). - New `RunLiveStackAccessTests` in `agc_driver_graphics_tests` (`tests/LiveStack.cpp`): a packet on the test's own stack is accessible and passes `CheckRange`; a thread on a stack of its own with an inaccessible page above and one at the bottom sees its stack top accessible, a range past the top and the page above it rejected (`Accessible` and `CheckRange`), and the inaccessible page below its frames rejected. After the thread ends, that memory is reported as the page query says. The checks bound the new path: dropping the past-the-top bound fails with "a range past the stack's top counts as accessible", and starting the accepted range at the stack's bottom instead of the frame fails with "an inaccessible page of the stack below the live frames counts as accessible". Without the change the test passes too, since the answers are the same; only the cost differs. - Cost of one 64-byte `CheckRange` on the stack, in the test process (a short maps file): 12.3 us before, 0.01 us after. The title's maps file has thousands of lines (mostly driver mappings), hence the 1.5 ms above. - PPSA21564: on my integration branch since 2026-09-30 through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS). - My #442 also edits `GuestMemory.cpp`, `BdaTests.hpp` and `tests/Graphics.cpp`; the two merge cleanly in either order. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] 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!459
No description provided.