AGC: on Linux, describePages never stores PROCMAP_QUERY results in the page table, so every uncached guest-range check is an ioctl #1791

Closed
opened 2026-10-08 19:16:24 +00:00 by fisherjoey · 3 comments
fisherjoey commented 2026-10-08 19:16:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T19:16:24Z.


Summary

describePages (Execution/src/GuestMemory.cpp:536 on main 6b56175a) looks pages up in the arena/image page table first. On Windows, a VirtualQuery miss stores the readable pages it found (:578-584), under the generation check, and the registry's invalidator (ForgetPages) clears them on unmap and protect. The Linux branch (:589-611) issues PROCMAP_QUERY and stores nothing, so the table never fills and every check that reaches it is a syscall.

On main today the Linux arena span is empty (GuestArenaRange returns size 0 without #861), so the table is not used on Linux at all. With #861 the span covers the PS5 window, and every check in it that misses the table becomes an ioctl.

Evidence

Demon's Souls (PPSA01341 v1.07), tutorial level, queue 0, bpftrace on the syscall tracepoints over 20 s: 2.1M PROCMAP_QUERY calls, 1.67 s. Syscalls were 27.7% of queue 0's on-CPU samples, most of them these queries. Measured on our local build (an older main plus #157, #861 and our own patches). The query path there is the same code as main's.

#1330 adds a per-thread cache of up to 64 permission runs inside registered allocations. On a later build I ran it in place of the change below, and it still left 495k-502k queries per 10 s (0.8 s per 10 s). This title verifies more distinct ranges per frame than 64 runs hold.

Fix

After a successful PROCMAP_QUERY, store the readable pages the request covers in the arena/image table, the same way the Windows branch does: same values, same generation check, and the same ForgetPages invalidation. It stores only the pages of the request, not the whole VMA (an arena VMA can span GiBs).

Measured on the same build, 20 s windows:

before after
PROCMAP_QUERY on queue 0 2.1M calls, 1.67 s 2.7k calls, 4.7 ms

Queue 0 then got through ~75k draw packets per 10 s instead of ~53k (that run also enlarged the staging pool). On a later build, single runs in the level: this change 2.20 fps, #1330 instead of it 1.80, both 2.00 (9.9k queries per 10 s).

The stale-hit question raised on #1114 applies here as it does to the Windows table: a page the store covers that changes protection outside the registry stays cached until the next invalidation. The table only covers the guest arena, and guest mappings there change through the registry. If that's not enough, the store could also skip host regions inside the window.

The fix is ready on a branch: https://github.com/boykopovar/AnyPS5/compare/main...fisherjoey:AnyPS5:perf/linux-page-cache

It depends on #861: on main alone the arena span is empty and the change stores nothing.

The branch adds a Linux case to RunUnmappedGapTests. It maps two arena pages, registers them, protects one read-only through the registry and checks that the cached access follows, then unmaps both and checks again. It is skipped while the arena is unavailable.

Tests: the 195 agc_driver tests pass on the branch (rebased on 6b56175a). With #861 merged locally, agc_driver_graphics passes, and it fails on the new check if ForgetPages stops clearing the table.

Found on Linux 7.2 (Nobara 44), GCC 16, RTX 5070 Ti (595.91.07), with Demon's Souls PPSA01341 v1.07.

I'll open the PR if the approach looks right.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5767555819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1791). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T19:16:24Z. --- ### Summary `describePages` (`Execution/src/GuestMemory.cpp:536` on `main` 6b56175a) looks pages up in the arena/image page table first. On Windows, a `VirtualQuery` miss stores the readable pages it found (:578-584), under the generation check, and the registry's invalidator (`ForgetPages`) clears them on unmap and protect. The Linux branch (:589-611) issues `PROCMAP_QUERY` and stores nothing, so the table never fills and every check that reaches it is a syscall. On `main` today the Linux arena span is empty (`GuestArenaRange` returns size 0 without #861), so the table is not used on Linux at all. With #861 the span covers the PS5 window, and every check in it that misses the table becomes an ioctl. ### Evidence Demon's Souls (PPSA01341 v1.07), tutorial level, queue 0, bpftrace on the syscall tracepoints over 20 s: 2.1M `PROCMAP_QUERY` calls, 1.67 s. Syscalls were 27.7% of queue 0's on-CPU samples, most of them these queries. Measured on our local build (an older `main` plus #157, #861 and our own patches). The query path there is the same code as `main`'s. #1330 adds a per-thread cache of up to 64 permission runs inside registered allocations. On a later build I ran it in place of the change below, and it still left 495k-502k queries per 10 s (0.8 s per 10 s). This title verifies more distinct ranges per frame than 64 runs hold. ### Fix After a successful `PROCMAP_QUERY`, store the readable pages the request covers in the arena/image table, the same way the Windows branch does: same values, same generation check, and the same `ForgetPages` invalidation. It stores only the pages of the request, not the whole VMA (an arena VMA can span GiBs). Measured on the same build, 20 s windows: | | before | after | |---|---|---| | `PROCMAP_QUERY` on queue 0 | 2.1M calls, 1.67 s | 2.7k calls, 4.7 ms | Queue 0 then got through ~75k draw packets per 10 s instead of ~53k (that run also enlarged the staging pool). On a later build, single runs in the level: this change 2.20 fps, #1330 instead of it 1.80, both 2.00 (9.9k queries per 10 s). The stale-hit question raised on #1114 applies here as it does to the Windows table: a page the store covers that changes protection outside the registry stays cached until the next invalidation. The table only covers the guest arena, and guest mappings there change through the registry. If that's not enough, the store could also skip host regions inside the window. The fix is ready on a branch: https://github.com/boykopovar/AnyPS5/compare/main...fisherjoey:AnyPS5:perf/linux-page-cache It depends on #861: on `main` alone the arena span is empty and the change stores nothing. The branch adds a Linux case to `RunUnmappedGapTests`. It maps two arena pages, registers them, protects one read-only through the registry and checks that the cached access follows, then unmaps both and checks again. It is skipped while the arena is unavailable. Tests: the 195 `agc_driver` tests pass on the branch (rebased on 6b56175a). With #861 merged locally, `agc_driver_graphics` passes, and it fails on the new check if `ForgetPages` stops clearing the table. Found on Linux 7.2 (Nobara 44), GCC 16, RTX 5070 Ti (595.91.07), with Demon's Souls PPSA01341 v1.07. I'll open the PR if the approach looks right. AI-assisted: yes.
jeffrey12cali commented 2026-10-09 14:46:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-09T14:46:46Z.


Second title, same diagnosis, and a frame-rate number for it.

A retail title (PPSA28997) on Linux, RTX 4070 Laptop, Hyprland/XWayland. perf record on the hot threads
during gameplay put 50.8 % of all samples inside libstdc++ std::istream, called from describePages
through verify / DescribeCommittedUncached / CompareMapped. Counting the queries directly:
1,172,045 map queries and 64,207,427 parsed lines in one run, with the page table never populated. The
game's own module accounted for 3.0 %.

Populating the table moved the CPU-bound frame rate from 7.40 to 60.32 FPS (56.5 median over three runs),
the query count to zero, and 42,772 verifications to 0.1 s total. No fatal lines over a 13 minute
soak.

Two details from this diagnosis that may matter to #1810:

  • The store-back has nowhere to write until a span exists. On Linux PageStates::initialize gets
    base = 0 size = 0 from GuestArenaRange, because the Arena constructor in libc/src/GuestArena.cpp is
    entirely inside #ifdef _WIN32, so PageSpan::allocate() fails on size == 0. Your issue already says
    this depends on #861; on this tree, which predates it, we had to fall back to the window libkernel really
    does reserve (0x200000000, size 0x400000000) to get any effect at all.
  • Enabling the span has a side benefit: initialize only registers ForgetPages when a span allocated, so
    before this there was no invalidation installed on Linux at all. After it, the counters report
    forgets 715 (2377 MiB) during a run.

Written with AI assistance; the numbers were measured on the hardware listed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083252667 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1791#issuecomment-6083252667). Originally posted by [jeffrey12cali](https://github.com/jeffrey12cali) on 2026-10-09T14:46:46Z. --- Second title, same diagnosis, and a frame-rate number for it. A retail title (`PPSA28997`) on Linux, RTX 4070 Laptop, Hyprland/XWayland. `perf record` on the hot threads during gameplay put **50.8 % of all samples inside `libstdc++` `std::istream`**, called from `describePages` through `verify` / `DescribeCommittedUncached` / `CompareMapped`. Counting the queries directly: **1,172,045 map queries and 64,207,427 parsed lines in one run**, with the page table never populated. The game's own module accounted for 3.0 %. Populating the table moved the CPU-bound frame rate from **7.40 to 60.32 FPS** (56.5 median over three runs), the query count to **zero**, and 42,772 verifications to **0.1 s** total. No fatal lines over a 13 minute soak. Two details from this diagnosis that may matter to #1810: - The store-back has nowhere to write until a span exists. On Linux `PageStates::initialize` gets `base = 0 size = 0` from `GuestArenaRange`, because the `Arena` constructor in `libc/src/GuestArena.cpp` is entirely inside `#ifdef _WIN32`, so `PageSpan::allocate()` fails on `size == 0`. Your issue already says this depends on #861; on this tree, which predates it, we had to fall back to the window libkernel really does reserve (`0x200000000`, size `0x400000000`) to get any effect at all. - Enabling the span has a side benefit: `initialize` only registers `ForgetPages` when a span allocated, so before this there was **no invalidation installed on Linux at all**. After it, the counters report `forgets 715 (2377 MiB)` during a run. Written with AI assistance; the numbers were measured on the hardware listed.
jeffrey12cali commented 2026-10-10 11:01:08 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-10T11:01:08Z.


Corroborated on a second title, with one addition: the store-back alone does not remove the decay

Our fork is pinned to c8830699 (2026-10-04), which predates #157, so we implemented both halves
independently: the PROCMAP_QUERY switch and the store-back into the arena/image table. The title is a 2D
IL2CPP title, PPSA28997, on an RTX 4070 Laptop with driver 615.71.09.

A cheap diagnostic for this cause. Sample the process mapping count and the frame counter together. If the
product stays constant, the cost is a per-mapping scan and this is the issue:

mappings FPS FPS x mappings
2,628 44.7 115,600
5,566 18.3 101,900
9,238 9.5 87,800
19,800 5.7 112,900

The constant is about 1.1e5 here, which is roughly 9.5 microseconds per mapping per frame. A perf call graph
placed 43.4 % of samples in libstdc++ stream extraction under describePages.

The addition, and the reason I would not close this issue with the store-back alone. On our build the
store-back fixed the boot-time cost but not the decay:

  • Store-back only: the query counter read 0 calls, 42,772 verifies completed in 0.1 s, and the title went from
    7.40 FPS to 60.32 FPS at the main menu. Over the following minutes the frame rate still fell from 44.7 FPS
    to 9.5 FPS as mappings grew.
  • Store-back plus PROCMAP_QUERY: no decay. Two 15-minute runs held a mean of 57 to 59 FPS, and four runs
    totalling 50 minutes of scripted gameplay produced no monotonic decline.

The reason is that the table only answers probes inside the arena and image spans. Our counters recorded
26,398 probes at addresses outside those spans, plus every probe of an unmapped hole. Each of those still
reaches the query path, so with a text parse each one cost a full scan, and the cost still grew with the
mapping count. Storing the request's pages fixes the mapped-and-in-span case, which is the majority by count
but not the one that paces the frame.

So the two changes are complementary rather than alternative: #157 makes the residual probes cheap, and the
store-back removes the probes that dominate by count. If #1791 is intended to be sufficient on its own, it is
worth deciding explicitly whether out-of-span and unmapped probes should also be cached, or left to the ioctl.

One semantic note on the gap case, relevant to #442. An earlier revision of our change treated an
unmapped address as a hole to walk past, continuing into the next mapping rather than ending the walk with the
remainder reported unreadable. That is a guest-visible change, and it coincided with the only permanent stall
we saw: the frame counter stopped at frame 8953 during a boss fight, all 93 threads were parked on a futex,
and the last rendered frame was a correct gameplay image. Restoring the end-the-walk behaviour removed it, in
four subsequent runs without a recurrence. Single instance, so it is a lead rather than a proven cause, but I
would not want a caching change to reintroduce it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096806761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1791#issuecomment-6096806761). Originally posted by [jeffrey12cali](https://github.com/jeffrey12cali) on 2026-10-10T11:01:08Z. --- ### Corroborated on a second title, with one addition: the store-back alone does not remove the decay Our fork is pinned to `c8830699` (2026-10-04), which predates #157, so we implemented both halves independently: the `PROCMAP_QUERY` switch and the store-back into the arena/image table. The title is a 2D IL2CPP title, `PPSA28997`, on an RTX 4070 Laptop with driver 615.71.09. **A cheap diagnostic for this cause.** Sample the process mapping count and the frame counter together. If the product stays constant, the cost is a per-mapping scan and this is the issue: | mappings | FPS | FPS x mappings | | --- | --- | --- | | 2,628 | 44.7 | 115,600 | | 5,566 | 18.3 | 101,900 | | 9,238 | 9.5 | 87,800 | | 19,800 | 5.7 | 112,900 | The constant is about 1.1e5 here, which is roughly 9.5 microseconds per mapping per frame. A `perf` call graph placed 43.4 % of samples in `libstdc++` stream extraction under `describePages`. **The addition, and the reason I would not close this issue with the store-back alone.** On our build the store-back fixed the boot-time cost but not the decay: - Store-back only: the query counter read 0 calls, 42,772 verifies completed in 0.1 s, and the title went from 7.40 FPS to 60.32 FPS at the main menu. Over the following minutes the frame rate still fell from 44.7 FPS to 9.5 FPS as mappings grew. - Store-back plus `PROCMAP_QUERY`: no decay. Two 15-minute runs held a mean of 57 to 59 FPS, and four runs totalling 50 minutes of scripted gameplay produced no monotonic decline. The reason is that the table only answers probes inside the arena and image spans. Our counters recorded 26,398 probes at addresses outside those spans, plus every probe of an unmapped hole. Each of those still reaches the query path, so with a text parse each one cost a full scan, and the cost still grew with the mapping count. Storing the request's pages fixes the mapped-and-in-span case, which is the majority by count but not the one that paces the frame. So the two changes are complementary rather than alternative: #157 makes the residual probes cheap, and the store-back removes the probes that dominate by count. If #1791 is intended to be sufficient on its own, it is worth deciding explicitly whether out-of-span and unmapped probes should also be cached, or left to the ioctl. **One semantic note on the gap case, relevant to #442.** An earlier revision of our change treated an unmapped address as a hole to walk past, continuing into the next mapping rather than ending the walk with the remainder reported unreadable. That is a guest-visible change, and it coincided with the only permanent stall we saw: the frame counter stopped at frame 8953 during a boss fight, all 93 threads were parked on a futex, and the last rendered frame was a correct gameplay image. Restoring the end-the-walk behaviour removed it, in four subsequent runs without a recurrence. Single instance, so it is a lead rather than a proven cause, but I would not want a caching change to reintroduce it.
fisherjoey commented 2026-10-10 11:42:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-10T11:42:15Z.


@jeffrey12cali thanks, the mappings × FPS product is a neat check. On current main both halves are covered by separate changes: #157 (merged 2026-10-08) switched Linux describePages to PROCMAP_QUERY through a cached fd, so the out-of-span and unmapped probes go to the ioctl instead of a text parse. #1810 (open, with #861, now merged) adds the store-back for in-span pages. So the case you saw decay on, store-back without PROCMAP_QUERY, shouldn't occur on main with #1810. Whether out-of-span and unmapped probes should also be cached is a question for #1810. Your note on ending the walk at an unmapped address is worth keeping there too.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097128844 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1791#issuecomment-6097128844). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-10T11:42:15Z. --- @jeffrey12cali thanks, the mappings × FPS product is a neat check. On current `main` both halves are covered by separate changes: #157 (merged 2026-10-08) switched Linux `describePages` to `PROCMAP_QUERY` through a cached fd, so the out-of-span and unmapped probes go to the ioctl instead of a text parse. #1810 (open, with #861, now merged) adds the store-back for in-span pages. So the case you saw decay on, store-back without `PROCMAP_QUERY`, shouldn't occur on `main` with #1810. Whether out-of-span and unmapped probes should also be cached is a question for #1810. Your note on ending the walk at an unmapped address is worth keeping there too. AI-assisted: yes
Sign in to join this conversation.
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#1791
No description provided.