perf(agc): query guest page access with PROCMAP_QUERY on Linux #157
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/157"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-09-29T19:48:14Z.
What
On Linux
describePages(GuestMemory.cpp) opened and parsed the whole/proc/self/mapsfor every run it reported (the arena/image page spans that answer most queries on Windows are empty there), about 0.5–0.8 ms per lookup.On kernels with the
PROCMAP_QUERYioctl (Linux 6.11+) each run is now one ioctl on an open/proc/self/mapsfd withPROCMAP_QUERY_COVERING_OR_NEXT_VMA: the kernel returns the VMA covering the cursor, or the next one above it, with its read/write flags. No snapshot is kept, so host-only mapping changes (thread stacks, guard pages, driver allocations) are seen immediately. A VMA starting above the cursor is a gap up tovma_start, andENOENTis a gap to the end, matching the gap handling from #442. The fd is opened and probed once; if the probe fails (ENOTTYon older kernels), the line scan stays as the fallback. When<linux/fs.h>predates 6.11 (ubuntu-latesthas 6.8), the v1struct procmap_query, the flags used andPROCMAP_QUERYare defined locally, so the fast path is still compiled and the runtime probe decides.APS5_NO_PROCMAP_QUERYskips the probe, so the line scan can be tested on a kernel that has the ioctl.Tested
Linux 7.2 (CachyOS, GCC): full build,
ctest359/359 with the system headers, with the local definitions (linux/fs.hinclude disabled) and withAPS5_NO_PROCMAP_QUERY=1.Standalone comparison against the line scan over 2003 addresses in a process with ~4000 mixed-protection VMAs: identical readable/writable answers and run ends (the only difference is the end of the gap above
0x7fffffffe000, where the line scan stops at[vsyscall]; both report it inaccessible), 763 µs vs 0.54 µs per lookup.Minecraft (on a local branch with more rendering work), 100 s runs:
[guestmem] verifytotal 6.7 s with this change, against 52.1 s with the previous maps snapshot cache; no verify failures in either run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T00:50:59Z.
A note on the gap case in the Linux
describePages, which this PR rewrites but keeps as it is on main.When the cursor is in a gap between mappings, the Linux path reports the whole rest of the range unmapped and returns. That includes mapped pages after the gap. Main's line scan does this, and here
findRegionreturning null after the reload takes the same exit.A concrete case: map three anonymous RW pages,
munmapthe middle one, then query all three. Results with this PR merged onto current main (plain main gives the same):CommittedRanges(base, 3 pages)[base, +1 page)and[+2, +3 pages)[base, +1 page)onlyCommittedRanges(base + 1 page, 2 pages)[+2, +3 pages)ReadCommitted(base, 3 pages), byte at +2 pages (holds 0x5a)The same skip hits
EqualsCommitted(pages after the gap are never compared),WriteChangedCommitted(writes to them are dropped) and theDescribeCommittedcalls in GuestBufferMemory. On Linux,sceKernelMunmapof part of a mapping really unmaps it, so these gaps do occur inside guest ranges.In this PR's structure the fix is small. When no region contains the cursor, report the gap up to the next region above it and carry on, as the VirtualQuery path does with a free region:
With that change on top of this PR, the three queries return the expected results and
agc_driver_graphics_testspasses on Linux.There's also a cost note, since this is a perf PR. A cursor in a gap takes the reload path, so each such query still reads all of /proc/self/maps. In PPSA21564 on my integration branch, the lighting programs read near-null addresses (0x18, 0x30, 0x58) about 1500 times a second during resource capture. The maps there are about 5700 lines, mostly NVIDIA mappings.
I have this fix against main's loop as about 10 lines plus a test. I can adapt it to your structure and send it as a follow-up once this lands, or it can be folded in here, whichever you prefer.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:39:00Z.
#442 has landed since my earlier comment, so this now conflicts in GuestMemory.cpp. When you rebase, please keep its gap handling (emit the gap up to the next region and continue, as in the snippet above). For the reload cost I mentioned, one option: when the snapshot was just reloaded and is still the current generation, treat "no region contains the cursor" as a real gap instead of reloading again on every query. Happy to re-test on my integration branch once it is rebased.
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-04T17:13:58Z.
Thanks, and sorry for the delay. While rebasing I found that the cache itself no longer holds on current main:
RunLiveStackAccessTests(from #459) mprotects guard pages without a guest generation change, and a cached snapshot still reports them readable ("the released test stack is misreported"). Trusting a just-reloaded snapshot for misses fails the same way in the other direction:agc_driver_graphics_testsmaps host pages after a miss reload and they read as unmapped. So keying the snapshot on the guest allocation generation is only exact for guest-registered ranges, and even there the write-watch protection changes could leave the writable bit stale.Two options I see: (a) use the snapshot only for addresses inside guest-registered allocations and read
/proc/self/mapsfor everything else, or (b) drop the cache and keep only an exact fast path: addresses below/proc/sys/vm/mmap_min_addr(64 KiB by default) can never be mapped, so they are a gap without reading the file. That covers the near-null lighting reads (0x18, 0x30, 0x58) you measured, and #442's gap handling stays as is. Which would you prefer? I'm also fine with closing this in favour of your follow-up.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:41Z.
You're right, and the #459 test is right too: host mappings (thread stacks and their guard pages, driver allocations) change without the guest allocation generation moving, so a snapshot keyed on it can't answer for them, and my suggestion to trust a just-reloaded snapshot on a miss fails the same way, as you found.
Of your two options, (a) still leaves the writable bit stale, as you say, and (b) is exact only while
mmap_min_addrisn't lowered at runtime and the process has no CAP_SYS_RAWIO, so I'd rather not rely on it.One option that is exact without a cache: the
PROCMAP_QUERYioctl on an open/proc/self/mapsfd (Linux 6.11,linux/fs.h). WithPROCMAP_QUERY_COVERING_OR_NEXT_VMAit returns the VMA that contains the address, or the next one above it, with its read and write flags, straight from the kernel. Each run indescribePagesis then one ioctl instead of a full parse, and #442's gap handling maps onto it directly: ifvma_startis above the cursor, the gap runs up tovma_start, andENOENTmeans a gap to the end. On older kernels the ioctl fails withENOTTYand the line scan stays as the fallback.If you'd rather not take that on, closing this is fine with me too.
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-05T20:20:07Z.
Thanks,
PROCMAP_QUERYit is. I rebased onto currentmainand replaced the cache: each run indescribePagesis now one ioctl withPROCMAP_QUERY_COVERING_OR_NEXT_VMA, a VMA above the cursor is a gap up tovma_start,ENOENTis a gap to the end, and the line scan stays as the fallback when the probe on the fd fails (older kernels). No snapshot is kept, so the host-only mappings from the #459 test are always answered exactly.Measurements are in the updated description: same answers as the line scan, ~0.5 µs instead of ~760 µs per lookup, and in a 100 s Minecraft run the
verifytotal went from 52.1 s (old cache) to 6.7 s.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T23:37:39Z.
Thanks, this is what I had in mind. I checked the query path against the line scan:
vma_start, andENOENTbecomes a gap to the end.PROCMAP_QUERY_VMA_READABLE, and writable only together with readable, the same aspermissions[0]/[1].ENOTTY) falls back to the line scan for good.A small program on 7.2 gives the expected answers for each case: an RW page, a hole left by
munmap(the next VMA is returned), aPROT_NONEpage (flags 0), a read-only page,0x18(the first VMA above it) and an address above the last VMA (ENOENT).One thing affects what this PR actually speeds up. The fast path is compiled only when
<linux/fs.h>definesPROCMAP_QUERY, which needs 6.11 or newer UAPI headers. The Linux CI job andrelease.ymlbuild onubuntu-latest(24.04,linux-libc-dev6.8), so there the#if defined(PROCMAP_QUERY)block is compiled out. CI passing tests only the line scan, and the release binaries keep the old cost even on new kernels.Since the runtime probe already handles old kernels, the header check could also fall back to a local definition when the macro is missing:
struct procmap_querywith the v1 fields, fromsizetobuild_id_addr(104 bytes)PROCMAP_QUERYas_IOWR('f', 17, struct procmap_query)The struct is size-versioned, so the kernel accepts that layout on any version that has the ioctl.
Separately, nothing tests the fallback on a machine where the ioctl works. Something like an environment switch that skips the probe would let
RunLiveStackAccessTestsand the gap tests cover both paths locally. That part is optional.Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-06T15:22:57Z.
Thanks for checking it against the line scan. Both points are in now (rebased onto current
main):<linux/fs.h>doesn't definePROCMAP_QUERY, the file definesstruct procmap_querywith the v1 fields (sizetobuild_id_addr,static_asserton 104 bytes), the three flags it uses andPROCMAP_QUERYas_IOWR('f', 17, struct procmap_query), so the fast path is compiled on 6.8 headers too and the runtime probe decides.APS5_NO_PROCMAP_QUERYskips the probe, so the line scan can be tested on a kernel that has the ioctl.Tested on Linux 7.2 (CachyOS, GCC), ctest 359/359 in three setups: system headers, the local definitions (with the
linux/fs.hinclude disabled, as on the 6.8 headers ofubuntu-latest), andAPS5_NO_PROCMAP_QUERY=1. Under strace,agc_driver_graphics_testsmakes 246PROCMAP_QUERYioctls and opens/proc/self/mapstwice with both header setups, and opens it 246 times with no ioctls when the switch is set.Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-06T15:39:45Z.
The Linux job failed on a timeout in
agc_driver_flat_store(the recorded batch never finishes on llvmpipe). The same test timed out the same way on an unrelated branch (run 37406080556,feat/agc-image-tables-global), and it passes 20 times in a row here with and withoutAPS5_NO_PROCMAP_QUERY. I can't re-run jobs, so could you re-run the failed one when you get a chance?Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-06T18:10:55Z.
Tested
a1427b83with LEGO Horizon Adventures (PPSA14632) on Linux: Arch, kernel 7.2.7, GCC 16.2.1, RX 9070 XT (RADV GFX1201),--to-intel. I cherry-picked it onto my test branch (an oldermainplus #861, #425 and a few of my open PRs).Before: the game died about 2 minutes in, inside a deliberate crash by Unreal's GC (
IncrementalDestroyGarbage→AbortInsideMemberFunction). A texture's render-thread release fence had been pending for more than 10 s, because each frame took about 75 s. Almost all of that time went intodescribePagesre-reading/proc/self/maps:The busy queue worker's stack ended in
std::ios_base::~ios_base()underdescribePages←verify←CheckRange←WriteChanged←StorageTexture::writeBackLayers.With this PR: no abort in an 8-minute run. The game presents steadily (about 4 frames per 10 s, still CPU-bound elsewhere) and reaches its boot-flow movie for the first time:
That is about 127× as many verifies in 2% of the time. No verify failures and no new errors in the log.
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-06T21:34:55Z.
Thanks a lot for testing it on LEGO Horizon Adventures, and for the before/after numbers. Seeing the GC abort go away and the game reach its boot-flow movie is a much better proof than my Minecraft runs, and it confirms the gain on RDNA4 as well.
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-07T13:24:15Z.
Measured with a game on Linux, in case it helps the review.
Demon's Souls (PPSA01341 v1.07) on Linux 7.2, RTX 5070 Ti. During its intro movie (Bink 2 decoded in compute), almost all CPU time on cache hits went to dispatch validation, and in validation to
describePagesre-reading/proc/self/maps: about 48,000 opens of the file in 25 s, with 64% of samples insideread()and the rest in stream parsing.mainmain+ this PRGuestMemory::CompareMappedaveragePROCMAP_QUERY, 2 to 4 us)Same build otherwise, same run directory,
APS5_PROFILE_DRAW=1. Nothing broke in about 10 minutes of runs with the PR applied.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T13:55:14Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppL537-551Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T02:55:25Z.
Replying to the invitation when #1114 was closed as a duplicate of this one: #157 does not miss the case I hit. I verified it on both code paths.
The case was the guard-page assertion in
tests/LiveStack.cpp(a thread stack whose two guard pages aremprotected toPROT_NONE), reached throughagc_driver_graphics. On the snapshot-cache version that test failed withthe released test stack is misreported; the maintainer's closing comment quotes that as the stale hit.Linux 7.2.9 (CachyOS, GCC, Release), NVIDIA RTX 3080 Ti:
Kernel here is 7.2.9, so the
PROCMAP_QUERYpath is the live one rather than the fallback — worth stating, since a pass on an older kernel would only have exercised the line scan.Why it does not have the problem the other one had, in the terms the failure showed. The stale hit came from a cache refreshed only on a miss: a region that still covered the queried address was a hit, so nothing was re-read, and a page that had since become
PROT_NONEkept reporting accessible. This keeps no snapshot at all, so a guard page whose protection changed after the fact is re-examined on the next query. TheLiveStackassertion is exactly the shape that "host-only mapping changes are seen immediately" predicts, which is why it is a better test of the claim than a timing number.The forced-fallback run is the part I would keep in the PR: it makes the line scan testable on a kernel that does have the ioctl, and 359/359 with the fallback forced means the two paths agree on the case that mattered rather than only the fast path being exercised.
That is everything I had. Nothing outstanding from my side.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T10:31:44Z.
Tested this on current main: ctest 410/410, and in a title whose mappings grow over the run it removes the slowdown completely (it held 60 fps where main falls to single digits). One thing before merging: in
describePages, when theioctlfails with anything other thanENOENT, it falls through to the/proc/self/mapsline scan. That's a retry after an error; pleasereturn falsethere instead, so it surfaces as "cannot query guest memory". The path chosen once by the probe at startup is fine.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T11:25:37Z.
Pushed the fix Dean asked for, so this doesn't have to wait:
18bc4b6d0: indescribePages, aPROCMAP_QUERYthat fails with anything other thanENOENTnow returnsfalse("cannot query guest memory") instead of falling back to the/proc/self/mapsline scan. The fallback is only used when the startup probe picks it.main(084aeeb67).Linux, GCC 16.2.1, RADV gfx1035: full ctest 442/442. With
APS5_NO_PROCMAP_QUERY=1(line-scan path),agc_driver_graphicsand the guest/shader memory tests pass too. Merging once CI is green.