perf(libc, libkernel): keep aliased direct memory on the Linux write watch and collect stores through every alias #12452
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12452"
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 Tehshortbus on 2026-10-28T14:11:34Z.
What
On Linux, when a title maps one direct memory block at a second guest address,
WatchMappinginlibkernel/DirectMemory/DirectMemory.cppunregisters both views from the write watch. From then on the driver's tracker refuses every collect over them (GuestWriteWatchCoversis false). Textures over that memory fall back to whole-surface comparisons, and the resource caches over it to full revalidation walks.GTA V maps direct memory this way. In the first two Story cutscenes it compares 8,000–22,000 textures per 10 s (3–9.5 GiB), and the committer pays for it per draw.
The two views share physical pages, but the userfaultfd write-protect bit and
PAGEMAP_SCANare per virtual page: a store through one view marks only that view's page written. So both views can stay registered, as long as a collect over one also scans the other and reports its stores at both addresses. This PR does that:GuestWriteWatchAlias_nid_postfix(first, second, bytes)(libc): new export. It records a link in each direction between two page-aligned, watched ranges of the same size, and returns false (changing nothing) when the watch is unavailable or either range is not fully watched. It returns false on macOS and Windows.Collect,CollectFreshandCollectArmed(GuestWriteWatch.cpp): over a range with links, each also walks the linked ranges. Every written page found through any view is reported at its own address and at the address of each view linked to it.WatchMappinglinks every pair, so one level of indirection covers all of them.RegisterandUnregistercut the links of the range they change: the linked parts outside it are kept, on both sides.WatchMapping(libkernel): links an overlapping pair withGuestWriteWatchAlias_nid_postfix, and unregisters both views as before only when that returns false.WindowCopy::Holds(libSceAgcDriver/Execution/src/GuestMemory.cpp): returns false for a page outside the window. A collect over an import window can now report a store at a linked page outside it. Without the check,stampWindowRunsread pasthashesand GTA V crashed (SIGSEGV) in the first cutscene.docs/dev/debt/libSceAgcDriver.md): Linux now records a store through one alias at every alias, instead of recording none.Tested
CachyOS Linux (kernel 7.3.0-4-cachyos), GCC 16.2.1, Release, Ryzen 7 5800X, NVIDIA GeForce RTX 4070 SUPER (615.78.08).
Tests (head
4621b11d00,mainba5b5330b3merged intoa160f789ec;range-diffofa160f789ecagainst the measured headd10b767163shows both commits unchanged, and the merge preview ontomainis clean):guest_memory:CheckDirectMemoryWriteWatchSeesWritesThroughAliasesmaps one physical page at three addresses: the upper page of a two-page mapping, and two one-page mappings. It checks that:CollectFresh,CollectArmedandCollecteach report a store through one view at every view;CheckDirectMemoryWriteWatchDropsAliasOnRemaplinks two views of one page, then maps other physical memory over the second withsceKernelMapDirectMemory(fixed). It checks that the second view stays watched, that a collect over the first no longer reports the second's pages, and that a store through either view is reported only at that view.CheckDirectMemoryWriteWatchandCheckDirectMemoryWriteWatchFollowsRemovedMappingsnow expect aliased views to stay covered.agc_write_tracking:CheckImportWindowOverAlias(Linux) links two views of one memfd block and imports the upper view with a window. A store through the window is reported at both views, including the lower one, outside the window.4621b11d00):main'sGuestWriteWatch.cppandDirectMemory.cpp,guest_memoryfails on the first coverage check of an aliased view (core/libs/tests/GuestMemory.cpp:3036).Register,guest_memoryfails inCheckDirectMemoryWriteWatchDropsAliasOnRemap: the first view still reports the remapped view's pages.WindowCopy::Holds,agc_write_trackingcrashes (SIGSEGV) inCheckImportWindowOverAlias.Alias, the alias walks of all three collects, the link cut on bothRegisterandUnregister, and theWindowCopy::Holdsbound.guest_*tests pass (ctest -R '^guest_').guest_freebsd_clocksaborts, the same way on plainmain(#12736, fixed by the open #12660 and #12747); this PR does not touch clocks.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, with noVUID-line or missing-layer message. They includeagc_write_watch_coverage,agc_import_watch,agc_shared_write,agc_write_tracking,agc_driver_alias_copy_labeland the heap mirror unwatch tests.python3 tools/check_conventions.py --base origin/mainandgit diff --checkpass.GTA V (PPSA04264 01.011.001, 3840x2160, warm shader cache):
main: it stops while the save loads (#11425), which #11580 fixes. Both arms carry #11580, and Depends on lists it, so this merges after #11580. The code shares no files with #11580.d10b767163onmain2960e9a6e7. The current head is the same two commits on1db077e876, withmainba5b5330b3merged in. Of the merges since2960e9a6e7, only #11681 (a new direct-memory allocation test incore/libs/tests/GuestMemory.cpp), #11684 and #9595 (debt entries, and the unclamped-blend path, which GTA V never uses) #9431 (PS4-only fill-kernel matching inBufferFill.cpp) and #12706 (a different debt entry) touch this PR's files or the driver's memory code. None changes the write watch, direct memory orWindowCopy, so the measured path is unchanged.WriteChangedinExecution/src/GuestMemory.cpp; this PR changes one line ofWindowCopy::Holdsin the same file, about 1,900 lines away.main2960e9a6e7+ #11580 (b8f4e6343a).d10b767163(98efd2e7b2).APS5_PROFILE_DRAW=1 APS5_PIPELINE_STATS=1 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_SECONDS=100:60in every run.Cutscene 1, per 10 s:
[guestmem] compare)collectfast-failsCutscene 2, per 10 s:
collectfast-failsgta-alias10-*.zip) in this comment, with the summary lines, the trace, the[guestmem]/[rescache]/[draw]lines and a README with the build, switches, cutscene marks and where the trace window falls. The traces are recompressed with xz to fit GitHub's attachment limit;xz -dgives the.jsonthattools/trace_digest.pyreads.Summary lines and trace digests (the
APS5_TRACE_SECONDS=100:60window of each trace):Base, round 1 (
b8f4e6343a:main2960e9a6e7+ #11580):tools/trace_digest.pyof the base, round 1seconds 100:60 from present 2761: 829 presents, frame p50 71.4 / p90 77.0 / p99 114.1 / max 203.2 ms, GPU busy 36.9 ms per present
NVIDIA GeForce RTX 4070 SUPER, NVIDIA 615.78.08, Linux 7.3.0-4-cachyos, 3840x2160, commit b8f4e6343a96, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 52.8, batch 36.9, Graphics.Draw 28.6, Driver.Packet.draw 26.0, Driver.Draw 25.5, Driver.Packet.dispatch 23.8, Graphics.Draw.draw_and_resource_release 23.6, Recorder.Sync 15.4, Driver.Draw.shader_compile_and_link 15.4, VideoOut.Flip 14.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
PR, run 3 (
98efd2e7b2: the same + this PR atd10b767163):tools/trace_digest.pyof PR, run 3seconds 100:60 from present 2706: 832 presents, frame p50 70.6 / p90 77.4 / p99 114.1 / max 197.2 ms, GPU busy 36.3 ms per present
NVIDIA GeForce RTX 4070 SUPER, NVIDIA 615.78.08, Linux 7.3.0-4-cachyos, 3840x2160, commit 98efd2e7b261, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 52.1, batch 36.3, Graphics.Draw 28.0, Driver.Packet.draw 25.8, Driver.Draw 25.3, Driver.Packet.dispatch 23.5, Graphics.Draw.draw_and_resource_release 22.9, Driver.Draw.shader_compile_and_link 15.2, Recorder.Sync 14.8, VideoOut.Flip 13.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Overlap:
tools/pr_overlap.pylists five conflicts, all indocs/dev/debt/libSceAgcDriver.md:DirectMemory.cpporWindowCopy.WatchMapping, the write-watch exports,WindowCopyand the write-watch tests:SharedBackingandCreateDirectMemoryBackinginDirectMemory.cpp, the driver's import probes and a backing test. None of those is touched here. Its call in themainofcore/libs/tests/GuestMemory.cppis 3 lines from ours, so whichever merges later may need a trivial rebase.#if defined(__linux__)aroundWatchMappingto macOS. That is compatible: on macOSGuestWriteWatchAlias_nid_postfixreturns false, so aliased views are unregistered there as now.CheckImportWindowinWriteTracking.cpp.CheckImportWindowOverAliasis placed afterCheckImportWindowAcrossThreads, away from that hunk.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:23:29Z.
Review: CHANGES_REQUESTED
The new GTA V A/B archives are useful: I downloaded all four, verified their README and summary metadata, and ran
tools/trace_digest.pyon the attachedaps5-summary.jsonlfiles. They provide same-machine/same-scene data formain+ #11580 (48919964c63c) versus the earlier PR build (690ca444159f). However, the PR is currently atb6535bf6cb6231cd39fd336234b15fbe85652692, and the report explicitly says the change capture predates the rebase onto currentmain; I cannot confirm that those numbers represent the current head. Also, the repository requires aperfPR description to contain two valid summary lines with different commits, and the current body has neither line; GitHub still showsperf numbers missing. Please update the description with paired current-main/current-headaps5-summary.jsonllines, attach their traces, and include thetools/trace_digest.pyoutput. The performance claim remains unverified for the current head until then. The overlap scan reports only the independent debt-file hunk in #10760; the hosted build and platform checks are still running. I am requesting changes for the missing current-head performance report, not alleging a code defect.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T14:25:52Z.
Review: DISMISSED
The change reads correctly: links are kept in both directions, cut on
Register/Unregister, and the three collects report every page at each linked view. The newCheckDirectMemoryWriteWatchSeesWritesThroughAliasespins the reported pages for two and three views, and the updated expectations in the existing tests fail onmain. What is left:690ca44415, base48919964c6), and the head is nowb6535bf6cb. @MarcZX10 asked for the pair on the current head and that is still open. Please rerun the base/PR pair onb6535bf6cb(or on the head after any further push), update the twoaps5-summary.jsonllines and thetrace_digest.pyoutput in the description, and attach the traces. Thelabelscheck is still pending on that.WindowCopy::Holdsbounds check (core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:930) has no test: only GTA V reached it. A test where a collect over an import window reports a store at a linked page outside the window would show the crash it prevents.Registerover an already linked range (GuestWriteWatch.cpp:64) is not reached by any test either; remapping a view over an aliased one and checking that no store is reported at the old partner would cover it.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:34:15Z.
Review: COMMENTED
Follow-up on the new head
dc64a22147: the addedCheckDirectMemoryWriteWatchDropsAliasOnRemapandCheckImportWindowOverAliastests now exercise the two previously uncovered paths (cutting a link onRegisterand rejecting a linked page outside an import window). I confirmed those cases are present in the exact-head diff, andpython3 tools/check_conventions.py --base upstream/mainplusgit diff --check upstream/main...HEADpass locally. I have not run the C++ tests on this host. The remaining blocker from my earlier review is still theperfreport: the attached A/B captures use the pre-rebase build690ca44415, not this head, and GitHub still reportsperf numbers missing. Please attach a same-machine/title/scenemainvs current-head pair, the two summary lines, traces, andtools/trace_digest.pyoutput; the performance claim cannot yet be checked for this commit.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T14:36:32Z.
Review: DISMISSED
Both test gaps are covered:
CheckDirectMemoryWriteWatchDropsAliasOnRemappins that neither view reports the other's stores after a remap to other physical pages, andCheckImportWindowOverAliaspins the window collect reporting the linked page below the window. Each fails without the corresponding code (the link cut onRegister, theHoldsbound).One point is left, the one @MarcZX10 still has open: the
perfreport. The GTA V pair and bothaps5-summary.jsonllines are from690ca44415on base48919964c6; the head is nowdc64a22147. The patch under test did not change in the rebase (range-diffshows=), so a pair on the current head is enough, no new analysis: rerun base/PR on the current head, update the two summary lines and thetrace_digest.pyoutput in the description, and attach the traces. Once that is in, this is ready to merge.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T21:48:49Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The new head adds the two regression cases requested in the earlier review: remapping one alias now checks that tracking is detached correctly, and the import-window case checks that a linked store outside the window is bounded. Those close the previously identified test gaps. The remaining blocker is still the performance comparison baseline.
What I ran
99fdd715b0857c44abf120d4c1974c21d142f84aagainst currentmain319621ba2e578bd053b0baba2877fe70520e0737.dc64a2214708ca7bf3e69d0ff18aae4ebb3397a0; the new commit changes only the test file.git diff --check refs/review/base...refs/review/pr-12452.main.Results
git diff --checkmaind46006acea9dd9f061eedae09c99d781c4309703mainFindings
Blocking
The performance data remains a comparison of current PR head
99fdd715b0againstmain446bd8776cplus #11580. #11580 is still open, so that baseline is not currentmain319621ba2e. The paired traces may measure the optimization accurately on top of #11580, but they do not meet the repository’s required main-versus-change comparison. Please recapture on current main or obtain maintainer agreement to an explicitly declared stacked baseline.Non-blocking
The new tests address the two test-coverage gaps from the previous review. The remaining open limitation in the report is the baseline, not those test cases.
Sources
Not verified
I did not rerun the C++ build, CTest, Vulkan validation, GTA V captures, or trace digests. Hosted Linux/product checks were skipped.
To get this to a good state
Provide a matched same-machine/title/scene capture pair using current
mainand this head, with both summary lines, traces, and digest output; alternatively, document maintainer-approved use of the stacked baseline and declare the dependency.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T23:40:42Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The added alias and import-window regression cases address the earlier test gaps, but the performance report still does not provide a complete, accepted comparison against the current base.
What I ran
60df38935c6c26fbb05d47e38d8b9a791df05c61; the head's merge base with the fetched currentmain3a2b91981651569163768854a1fcdc7193b6667bisc40a9fbd1c373134505c12ce71013d903430e128.git merge-tree --write-tree upstream/main HEAD; the current-main merge preview is clean (beb1054126a9341f130e7868627e1c3a177dc9c7).python3 tools/check_conventions.py --base upstream/mainandgit diff --check upstream/main...HEAD; both passed.main446bd8776c+ #11580. The comment adds a table formain319621ba2e+ #11580, but it does not include the summary lines, trace files ortrace_digest.pyoutput for that pair. I did not rerun the C++ tests or captures locally.Results
beb1054126a9341f130e7868627e1c3a177dc9c7Findings
Blocking
main446bd8776c+ #11580 baseline. The newer comment reportsmain319621ba2e+ #11580 results, but omits the exactaps5-summary.jsonllines, attached traces andtools/trace_digest.pyoutput for those runs. I also do not see maintainer agreement in the thread to use a stacked baseline; #11580 remains open.33acc03447034b61424cc1c36143cc40ea02bad3), which changescore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp; this PR also changes that file. The merge preview is clean, but the posted tests and captures do not verify the integrated tree or the current baseline.Sources
GuestMemory.cppchangeGuestMemory.cppchangeNot verified
I did not independently run the C++ tests, the GPU captures, or trace digests. The posted exact summary lines and trace attachments remain the earlier pair; the current
main+ #11580 table is author-reported.To get this to a good state
Rebase onto current
mainand rerun the focused alias/write-watch tests with #8930 present. Then provide the full same-machine/title/scene report for the current main and change (both summary lines, attached traces and digest output). If retainingmain+ #11580 as the baseline because plain main cannot reach the scene, record maintainer agreement to that stacked baseline and provide those same artifacts for the exact runs.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:39:28Z.
Review: CHANGES_REQUESTED
Re-reviewed — changes requested
The 2026-10-29 06:11 update now supplies paired summary data and trace attachments for its reported
main+ #11580 comparison. That closes the earlier artifact omission, but the comparison and test report still use an older code baseline than currentmain.What I ran
d10b767163fc1ea4c83e4cebb6d8175c25fcd373against currentmain2960e9a6e7e1fc99c4ed180c71495b5eb83464c7.git merge-tree --write-tree refs/remotes/upstream/main refs/review/upstream-12452; it returned clean treec0626db37359d66afacb135852016190dbe81dc3.git diff --check refs/remotes/upstream/main...refs/review/upstream-12452; exit 0, no output.c40a9fbd1c373134505c12ce71013d903430e128with currentmain. Currentmainincludes #8930 (33acc03447034b61424cc1c36143cc40ea02bad3), which changes the sameGuestMemory.cpppath changed by this PR.Results
c40a9fbd1c+ #11580 and the then-tested PR headGuestMemory.cppchange from #8930 is absent from the reported baseline60df38935chead; current head isd10b767163fc, and hosted Linux product tests are skippedNegative control
The PR's earlier reported controls distinguish alias tracking from the prior implementation and cover the remap and import-window cases. I did not rerun them on the current head or with #8930 integrated.
Findings
Blocking
main. The updated report usesc40a9fbd1c+ #11580. Currentmainis2960e9a6e7e1fc99c4ed180c71495b5eb83464c7and includes #8930's 44-line change tocore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp, which is also changed by this PR. Please rerun the focused tests and the required matched performance comparison on currentmain+ #11580 and this PR on that same baseline. If #11580 is retained as the baseline, record maintainer agreement and the dependency/order in the checklist.d10b767163fcadds the remap and import-window cases after the measured/tested60df38935chead. Please report those focused tests on the current integrated head.Sources
GuestMemory.cppNot verified
I did not independently run the test suite, produce captures, inspect the attached trace contents, or execute
tools/trace_digest.py. The reported artifacts and prior controls are author-reported; the merge preview, path comparison and diff check are local.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T11:07:57Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
Your update resolves the prior stale-baseline request against
aaccd0ba39: it rebases to1db077e876, reports all 18 focused tests and the negative controls on the new head, and explains why the captures still cover an unchanged production path. Currentmainis nowba5b5330b3. Please merge that current head and rerun the 18 focused write-watch/import/alias tests and negative controls. The current-main changes in the PR's files are limited to its debt entry, so the existing performance captures can remain if the updated diff confirms the measured production path is unchanged.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T13:27:27Z.
Review: APPROVED
Re-checked — new head, looks good
The requested current-main refresh and exact-head focused evidence are present, the measured production paths are unchanged since the capture baseline, and the required hosted checks pass.
What I ran
4621b11d0034ac851ca5691829050dc621504c8fagainst currentmain7aabd03d0a32bb928311ef1323b95dffb4618f7e; the merge preview is clean.1db077e876to currentmain. The only overlapping path is the debt document; the measured write-watch, direct-memory, and import-window implementation paths are unchanged.Results
main7e84b4ba1296e790168b317d2b7c652988478574docs/dev/debt/libSceAgcDriver.md; measured production paths are unchangedguest_*tests on exact headguest_freebsd_clockshas the same failure on mainUnedited command output
Negative control
GuestWriteWatch.cppandDirectMemory.cppRegisterWindowCopy::HoldsboundFindings
Blocking
None found in the reviewed exact head.
Non-blocking
The hosted Linux product job is skipped, and I did not independently reproduce the author's Linux/GPU test and capture runs.
Sources
Not verified
I did not run a local C++ build, CTest, Vulkan validation, GTA V capture, trace digest, or console test. The focused Linux results and GPU captures are author-reported.