perf(agc): cache the main image's page states on Linux #7788

Merged
ANONYMOUSZED-beep merged 2 commits from upstream-pr/7788 into main 2026-10-20 02:07:27 +00:00
ANONYMOUSZED-beep commented 2026-10-19 07:35:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ANONYMOUSZED-beep on 2026-10-19T07:35:51Z.


What

On Linux, GuestMemory's page table covered only the guest arena. Every check of an address in the main executable (its code, read-only data and the writable data that backWritableImageAnonymously backs with anonymous pages) went to the host: one PROCMAP_QUERY ioctl on 6.11+ kernels, and a parse of the whole of /proc/self/maps on older ones (Ubuntu 24.04's 6.8, or APS5_NO_PROCMAP_QUERY=1). This is the part left open at the end of #6183 ("PageStates::initialize only sets up the image span on Windows, so on Linux every lookup there goes to the host"), measured there at about 140 MB/s of /proc/self/maps reads by one thread in Guardians of the Galaxy after #6184.

PageStates::initialize now sets up the image span on Linux too, from the executable's PT_LOAD segments as dl_iterate_phdr reports them (the first entry, the main program), page-aligned from the lowest segment start to the highest segment end. APS5_NO_IMAGE_PAGE_CACHE=1 leaves it uncached on both systems, as before on Windows.

Invalidation is the same as on Windows: guest mprotect of the main image goes through GuestAllocations::Mutation (DoMprotect registers the main image on Linux as well and protects through Mutation::Protect), whose recorded change reaches the invalidator, which forgets the pages in both spans. backWritableImageAnonymously and the loader's RELRO protection run before main, before the table exists. The Linux write watch uses userfaultfd write protection, which does not change the VMA's permissions, so it does not make cached states stale.

RunUnmappedGapTests (agc_driver_graphics) gains a case for a page-aligned page in the test executable's .bss: after it is checked writable, it is made read-only behind the registry and must still read as writable from the table; after GuestAllocationsInvalidate it must read as read-only, and after it is restored and invalidated, as writable again.

Tested

Linux (Amazon Linux 2023, kernel 6.18, 2-core VM), GCC 14, Release:

  • agc_driver_graphics and agc_driver_graphics_maps_fallback (APS5_NO_PROCMAP_QUERY=1) pass, and the 35 ctest entries that run agc_driver_graphics_tests, agc_driver_tests and agc_shader_memory_tests all pass.
  • Negative check: with APS5_NO_IMAGE_PAGE_CACHE=1 (the behaviour of main), the new case fails with "a checked main image page was queried again instead of read from the page table".
  • A temporary loop (not in this PR) of 2,000 GuestMemory::Accessible(page, 4096, true) calls on the test's image page, in a process with about 50 lines in /proc/self/maps:
PROCMAP_QUERY APS5_NO_PROCMAP_QUERY=1
uncached (APS5_NO_IMAGE_PAGE_CACHE=1, as main) 0.51 ms 30.1 ms
this PR 0.08 ms 0.07 ms

The parse cost grows with the length of /proc/self/maps (about 7,500 lines in the #6183 title).

Not tested: no title was run, so there are no performance-report summary lines or traces; Windows is unchanged (the new code is in the non-Windows branch); macOS.

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 (follow-up to the "Still open" part of #6183, after #6184)
  • Unblocks: none
  • GPUs tested: none; the tests above use the mock Vulkan device and run on the CPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4917516206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7788). Originally posted by [ANONYMOUSZED-beep](https://github.com/ANONYMOUSZED-beep) on 2026-10-19T07:35:51Z. --- ### What On Linux, `GuestMemory`'s page table covered only the guest arena. Every check of an address in the main executable (its code, read-only data and the writable data that `backWritableImageAnonymously` backs with anonymous pages) went to the host: one `PROCMAP_QUERY` ioctl on 6.11+ kernels, and a parse of the whole of `/proc/self/maps` on older ones (Ubuntu 24.04's 6.8, or `APS5_NO_PROCMAP_QUERY=1`). This is the part left open at the end of #6183 ("`PageStates::initialize` only sets up the image span on Windows, so on Linux every lookup there goes to the host"), measured there at about 140 MB/s of `/proc/self/maps` reads by one thread in Guardians of the Galaxy after #6184. `PageStates::initialize` now sets up the image span on Linux too, from the executable's `PT_LOAD` segments as `dl_iterate_phdr` reports them (the first entry, the main program), page-aligned from the lowest segment start to the highest segment end. `APS5_NO_IMAGE_PAGE_CACHE=1` leaves it uncached on both systems, as before on Windows. Invalidation is the same as on Windows: guest `mprotect` of the main image goes through `GuestAllocations::Mutation` (`DoMprotect` registers the main image on Linux as well and protects through `Mutation::Protect`), whose recorded change reaches the invalidator, which forgets the pages in both spans. `backWritableImageAnonymously` and the loader's RELRO protection run before `main`, before the table exists. The Linux write watch uses userfaultfd write protection, which does not change the VMA's permissions, so it does not make cached states stale. `RunUnmappedGapTests` (`agc_driver_graphics`) gains a case for a page-aligned page in the test executable's `.bss`: after it is checked writable, it is made read-only behind the registry and must still read as writable from the table; after `GuestAllocationsInvalidate` it must read as read-only, and after it is restored and invalidated, as writable again. ### Tested Linux (Amazon Linux 2023, kernel 6.18, 2-core VM), GCC 14, Release: - `agc_driver_graphics` and `agc_driver_graphics_maps_fallback` (`APS5_NO_PROCMAP_QUERY=1`) pass, and the 35 ctest entries that run `agc_driver_graphics_tests`, `agc_driver_tests` and `agc_shader_memory_tests` all pass. - Negative check: with `APS5_NO_IMAGE_PAGE_CACHE=1` (the behaviour of `main`), the new case fails with "a checked main image page was queried again instead of read from the page table". - A temporary loop (not in this PR) of 2,000 `GuestMemory::Accessible(page, 4096, true)` calls on the test's image page, in a process with about 50 lines in `/proc/self/maps`: | | `PROCMAP_QUERY` | `APS5_NO_PROCMAP_QUERY=1` | |---|---|---| | uncached (`APS5_NO_IMAGE_PAGE_CACHE=1`, as `main`) | 0.51 ms | 30.1 ms | | this PR | 0.08 ms | 0.07 ms | The parse cost grows with the length of `/proc/self/maps` (about 7,500 lines in the #6183 title). Not tested: no title was run, so there are no performance-report summary lines or traces; Windows is unchanged (the new code is in the non-Windows branch); macOS. ### 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 (follow-up to the "Still open" part of #6183, after #6184) - [x] Unblocks: none - [x] GPUs tested: none; the tests above use the mock Vulkan device and run on the CPU - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-19 07:36:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T07:36:04Z.


This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6262140036 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7788#issuecomment-6262140036). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T07:36:04Z. --- <!-- pr-labels-perf --> This pull request is labeled `perf`, so it needs the summary line of a capture on `main` and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both `aps5-summary.jsonl` lines, attach the two traces and add the output of `tools/trace_digest.py`.
github-actions[bot] commented 2026-10-19 07:44:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T07:44:16Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1324 core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 5021 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6177 core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp
PR 6185 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6186 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6187 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6192 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6209 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6273 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6292 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6615 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6718 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6759 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6820 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6868 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6926 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6946 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6947 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6960 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 7201 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 7332 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 7505 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 7604 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6262266134 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7788#issuecomment-6262266134). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T07:44:16Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 1324](https://www.github.com/boykopovar/AnyPS5/pull/1324) | `core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp` | | [PR 4448](https://www.github.com/boykopovar/AnyPS5/pull/4448) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6177](https://www.github.com/boykopovar/AnyPS5/pull/6177) | `core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp` | | [PR 6185](https://www.github.com/boykopovar/AnyPS5/pull/6185) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6186](https://www.github.com/boykopovar/AnyPS5/pull/6186) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6187](https://www.github.com/boykopovar/AnyPS5/pull/6187) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6188](https://www.github.com/boykopovar/AnyPS5/pull/6188) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6192](https://www.github.com/boykopovar/AnyPS5/pull/6192) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6209](https://www.github.com/boykopovar/AnyPS5/pull/6209) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6273](https://www.github.com/boykopovar/AnyPS5/pull/6273) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6292](https://www.github.com/boykopovar/AnyPS5/pull/6292) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6615](https://www.github.com/boykopovar/AnyPS5/pull/6615) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6718](https://www.github.com/boykopovar/AnyPS5/pull/6718) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6759](https://www.github.com/boykopovar/AnyPS5/pull/6759) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6820](https://www.github.com/boykopovar/AnyPS5/pull/6820) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6868](https://www.github.com/boykopovar/AnyPS5/pull/6868) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6926](https://www.github.com/boykopovar/AnyPS5/pull/6926) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 6960](https://www.github.com/boykopovar/AnyPS5/pull/6960) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 7505](https://www.github.com/boykopovar/AnyPS5/pull/7505) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 7604](https://www.github.com/boykopovar/AnyPS5/pull/7604) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` | | [PR 7642](https://www.github.com/boykopovar/AnyPS5/pull/7642) | `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` |
jotitadev commented 2026-10-19 10:55:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T10:55:13Z.

Review: COMMENTED


The new image-span code is in the non-Windows branch (#ifndef _WIN32 around mainImageExtent, and the #else of PageStates::initialize), so macOS compiles it too, and macOS has no <link.h>, dl_phdr_info, dl_iterate_phdr or PT_LOAD. On main this is hidden because libSceAgcDriver does not build on macOS yet, but it breaks the macOS series from #1, which puts an #elif defined(__APPLE__) include block in front of the one that gets <link.h>.

Checked on macOS 27.2, Apple M2 Max, Apple clang 21.0.0, -arch x86_64 -std=c++20 -fsyntax-only on GuestMemory.cpp, on top of mugurc's ci/macos-series-2 (0d43cff93):

errors
series alone 0
series + this PR (e78dd4cbf) 2: unknown type name 'dl_phdr_info' (line 503), use of undeclared identifier 'PT_LOAD' (line 510)
series + this PR, with #ifdef __linux__ on mainImageExtent and #elif defined(__linux__) in initialize 0

Guarding both with __linux__ (the title already says "on Linux") keeps macOS on the uncached path; a macOS image span would need dyld (_dyld_get_image_header(0) and its segments) rather than dl_iterate_phdr. Only a syntax check, I did not build or run the tests.

Written with AI assistance (Claude Code). I ran the checks above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5570508560 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7788#pullrequestreview-5570508560). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T10:55:13Z. **Review:** COMMENTED --- The new image-span code is in the non-Windows branch (`#ifndef _WIN32` around `mainImageExtent`, and the `#else` of `PageStates::initialize`), so macOS compiles it too, and macOS has no `<link.h>`, `dl_phdr_info`, `dl_iterate_phdr` or `PT_LOAD`. On `main` this is hidden because `libSceAgcDriver` does not build on macOS yet, but it breaks the macOS series from #1, which puts an `#elif defined(__APPLE__)` include block in front of the one that gets `<link.h>`. Checked on macOS 27.2, Apple M2 Max, Apple clang 21.0.0, `-arch x86_64 -std=c++20 -fsyntax-only` on `GuestMemory.cpp`, on top of mugurc's `ci/macos-series-2` (0d43cff93): | | errors | |---|---| | series alone | 0 | | series + this PR (e78dd4cbf) | 2: `unknown type name 'dl_phdr_info'` (line 503), `use of undeclared identifier 'PT_LOAD'` (line 510) | | series + this PR, with `#ifdef __linux__` on `mainImageExtent` and `#elif defined(__linux__)` in `initialize` | 0 | Guarding both with `__linux__` (the title already says "on Linux") keeps macOS on the uncached path; a macOS image span would need dyld (`_dyld_get_image_header(0)` and its segments) rather than `dl_iterate_phdr`. Only a syntax check, I did not build or run the tests. Written with AI assistance (Claude Code). I ran the checks above on my machine.
adriadam10 commented 2026-10-20 02:13:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T02:13:21Z.


Checked the new image span against dl_iterate_phdr's main-program entry and the invalidation path (DoMprotect registers the main image on Linux and goes through Mutation::Protect); the new test pins a value only the page table gives.

Added one commit: mainImageExtent and the Linux branch of PageStates::initialize are now guarded with __linux__ (reported by @jotitadev: dl_phdr_info/PT_LOAD don't exist on macOS), so other platforms stay on the uncached path. Full ctest passes after the rebase; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6281734237 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7788#issuecomment-6281734237). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T02:13:21Z. --- Checked the new image span against `dl_iterate_phdr`'s main-program entry and the invalidation path (`DoMprotect` registers the main image on Linux and goes through `Mutation::Protect`); the new test pins a value only the page table gives. Added one commit: `mainImageExtent` and the Linux branch of `PageStates::initialize` are now guarded with `__linux__` (reported by @jotitadev: `dl_phdr_info`/`PT_LOAD` don't exist on macOS), so other platforms stay on the uncached path. Full ctest passes after the rebase; merged.
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!7788
No description provided.