fix(agc): keep a rewatched import's memory compared once it is shared #9159
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9159"
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 EagleBearerYS on 2026-10-21T14:02:16Z.
What
On Windows, a fault that AnyPS5 resumes in guest code can overwrite the guest's red zone,
[rsp-0x80, rsp-0x10)(#361). Since #6678 this happens again in Grand Theft Auto V (PPSA04264)'s open world, in every run. @tibzejoker's report in #9136:At eboot
0x28aaa50the game's zlibinflate_fastreloads two pointers from its red zone, then reads through a garbage one. Their bisect counts the faults AnyPS5 resumes over 60 s of Story (Windows 11 26200, RTX 4090 with NVIDIA 616.92, i9-13900K):main47a266cf9, and the merge before #6678755fecb33mainf6ce8e34aMEM_MAPPEDmain7280a17ecwithAPS5_NO_DEFERRED_SETTLES=1MEM_MAPPED, 44MEM_PRIVATE), 0 in zlibWhy #6678 brings the faults back:
rewatchSettledImportsreturns its range to the write watch throughGuestMemory::Rewatch.GetWriteWatchtracks without faults.WindowsMappings::demote). From then on every clearing collect over the range makes all its aliases read-only, so each guest store faults and is resumed.A second source of the same faults, from @tibzejoker's next bisect (#9136 comment). These are
MEM_MAPPEDfaults over 60 s of Story withAPS5_NO_DEFERRED_SETTLES=1, so #6678's path is out:MEM_MAPPEDresumed faultse9cc4a8ef, the merge before #8172d9df34d08, #8172's merge28f126650, #6598's merge28f126650with #8172 revertedImportWatchedkeeps each watched run of such an import in the watch, instead of taking the whole import out.0x162b9e4among their stores).The change, in seven commits, plus two test-only commits and a debt entry:
98adb5f205):WindowsMappings::CompareWhenSharedmarks the pages of a range, and makes writable again any alias of them that is already armed.collectable()and the fresh-collect pre-check refuse a clearing collect over it before anything is made read-only. Its aliases are never armed, and the caller gets no write record, so it compares the range.GetWriteWatchas before.d98ee15162):Rewatchmarks each range withGuestArenaCompareWhenSharedbefore it returns the range to the watch.ead292e2e5):CheckComparedWhenSharedforgets its section's handle value before it maps.WindowsMappingskeys its physical pages by that value, so a value that an earlier case closed withoutForgetSectioncould come back with that case's stored pages. Test-only.81aec58343):ImportWatchedmarks the resident pages of each run it keeps in the watch, withGuestArenaCompareResidentWhenShared, a resident-only variant of the same mark.47c8df4950): the mark survives a full unmap and remap.WindowsMappingsholds weakly. Once every view of that memory was unmapped, the nextMapmade a fresh, unmarked page, and the next clearing collect armed its views again.CompareWhenSharednow also marks the physical page, and a shared page made for it starts marked.ForgetSectionstill drops the mark, so a reused section handle starts unmarked.1ad5f29e20): the resident runs an import's read-write alias turns into shared views are marked too.MapAliasturns every resident run in the range into shared views beforeImportWatchedruns. The import's resident-only mark skipped those views, so the import's walk and every later clearing collect armed them.MapAliasnow records the views it turns shared, and the resident-only mark includes them. A second guest mapping of the page clears the record, so memory the guest itself shares keeps today's tracking.37a5d9495d): a compared shared page is reported once.WindowsMappingsflags it unreported.mainreports the pages at the first collect after the sharing.1712336477): compared shared memory is never read as unchanged since before a refused collect.UnchangedSince,UnchangedSinceAllandUnchangedCommittedSinceanswer false against an older generation there, so a cache compares bytes instead of trusting stamps that never came.WrittenSince,ChangedBlocks,StoredOverandNewestStampstay stamp-only, so storage textures and depth surfaces don't reload or drop at every epoch. Stamping the whole range at every refused collect would make depth surfaces reload from guest memory at every bind, dropping depth the GPU drew but hasn't stored.std::map::lower_boundperUnchangedSince*call (the map is empty on Linux), and on Windows twoWindowsMappingslistings per refused walk. Analytical, not timed.388f5f13e6):agc_driver_compared_shared_snapshottakes embersas's ordering through the productionCachedSampledTexture.cb9de8049c): a debt entry for depth surfaces over compared shared memory (below).Scope:
WindowsMappings,Rewatch's Windows branch and the new mark inImportWatchedare Windows-only.mainthrows it at the sharing. Before the eighth commit, this PR trusted the missing stamps there.Overlap:
d6e270ea9f, first posted in #9136) stops arming resident runs, which is #7473's part of the faults (theMEM_PRIVATEones). This change covers memory that turns shared. The two touch different code and merge cleanly. @tibzejoker's run with both is in Tested.Rewatch, so this change covers it too. #8926 depends on this PR.1ad5f29e20inWindowsMappings::Map's shared path, where #9505 restructures the loop that turns a page's resident aliases shared. The resolution keeps #9505's structure and clearsaliasDemotedafter eachdemotein both of its loops. They already conflicted inWindowsWriteWatch.cpp'smain()list, where both sides stay.git merge-treeof the head against every open PR touching the changed files: no conflicts. Of the PRs that already conflict withmain, 21 touch these files, and none touches the changed functions.Tested
main663de1ab2+ Dripl1x'sd6e270ea9(#9181) +81aec58343, 150 s of Story with deferred settles on.MEM_PRIVATE, 0 faults in zlib,0x162b9e4gone, no crash.MEM_MAPPEDfaults remain, at eboot0x28f413a(a store in a loop, not a red-zone leaf) and in a system DLL's host code. Both write 16 KiB views that were shared when written. As they read it, that is direct memory that was never imported and is mapped twice, which this change leaves armed.sceVoiceSetThreadsParamsfix to reach gameplay.main7e2ba73e4+ #9730: the[RAGE] HDD Streamercrash, the game'sint 0x41assert afterinflatereturnedZ_DATA_ERROR, in 4 of 4 runs, 236-253 s after launch. One earlier manual run of that tree survived 900 s.d6e270ea9) + this PR (99aeb0ac5): 0 of 3 runs, up to about 12 min of gameplay.main3bbd2321d+ #9181 + this PR up to81aec5834+ #9730 + #9445, Windows 11, RTX 4090,APS5_MIMALLOC=1, 7-minute open-world runs with every resumed write fault logged.inflate_fast, one per 4 KiB subpage of a fresh 16 KiBMEM_MAPPEDview. One of those runs crashed, on the thread of its only zlib fault. The other 4 runs had none.47c8df4950nor1ad5f29e20.1ad5f29e20closes one path that arms such views, and this PR doesn't claim to fix these faults.breakRun, where unmapping part of a resident run turns the whole run shared, and the demotion of a whole resident run when a second mapping shares one of its pages. A diagnostic that names the path behind each armed view would tell them apart in their runs.1ad5f29e20and #9181'sd6e270ea9f, onmain0a0ed72167alone and together, plustest/9159-9181-together(d7cec80e8c):guest_memory,windows_write_watch,agc_write_tracking,agc_write_tracking_metadata_commit,agc_resident_direct_remap,agc_rewatched_direct_shared,agc_import_alias_shared,agc_host_import_retireand more.[rsp-0x80, rsp), one 32-byte store per case, any fault resumed byGuestArenaHandleWrite; 90 executions. With this PR, the four shared cases no longer fault and keep all 16 qwords: rewatched, then a second mapping; a watched import, then a second mapping; rewatched, every view unmapped, mapped twice; an import alias over resident memory. Onmain, each faults and loses 13.Rewatchmark, theImportWatchedmark, the physical-page mark,demote(..., true)) makes its test fail and the store fault again. With only the refusal checks reverted, the alias store didn't fault in that sequence, butagc_import_alias_sharedfailed on the refusal.breakRunand the whole-run demotion paths, the GPU tests, and any game.windows_write_watch:CheckComparedWhenShared: a marked resident range stays collected. A second mapping then makes it shared. From then on its collects fail, neither mapping is read-only, and stores into it raise no fault (the test's vectored handler counts them). The unmarked half of the same mapping is still collected.CheckResidentOnlyComparedWhenShared: a resident-only mark leaves a shared view armed and collected, and still keeps a resident page compared once a second mapping makes it shared.CheckComparedWhenSharedRemapped: a marked resident page is unmapped, then mapped into two views. A clearing collect through either view refuses, neither is made read-only, and a store raises no fault. AfterForgetSectionthe same mapping is collected and armed again.agc_import_alias_shared(WIN32, a process mode ofagc_write_tracking_tests, next toagc_rewatched_direct_shared): it imports direct memory whose range needs a read-write alias, the way an import maps one, and requires that the pages the alias turned shared are compared and not armed. Memory the guest itself mapped twice before the alias is still armed, as before.agc_rewatched_direct_shared(WIN32, its own process mode ofagc_write_tracking_tests, in thePATHlist), with two cases:agc_driver_compared_shared_snapshot, this PC, Windows 11). The test runs on its first Vulkan device, which on this PC is the AMD Radeon integrated GPU (driver 31.0.21923.1000), not the RTX 3070. It checks the CPU-side cache logic; the GPU only reads the texels back.1ad5f29e20plus the test, 3 of 3: "the first lookup after a CPU store into shared compared memory returned the snapshot cached before the sharing".388f5f13e6, 3 of 3, withagc_driver_partially_resident_snapshotand_pending_stores.c442afa5d1) plus the test passes 3 of 3, so #9181 isn't affected.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1and the Khronos validation layer; no game was run. Four orderings, three repetitions per tree:mainef0d8ae3b5: 12 of 12 pass. Their earlier branch of this PR with #9181 (d7cec80e8c): the two orderings that write from the CPU after the sharing, with no lookup in between, still return the stale first lookup, 3 of 3 each; the other two pass.cb9de8049c, without #9181: 12 of 12 pass. In the two affected orderings the first lookup makes oneTextureCompareread, replaces the texture, and the GPU reads the new texel (0xa0001234, against0x10001234before); the other texels match, and the store raises no write fault.agc_driver_compared_shared_snapshotpasses 3 of 3 on this head on the RTX 3080 Ti, none skipped.VUID-, validation or synchronization message.Unwatch/Rewatchdirectly and reads through a computetexelFetch. It doesn't cover host-import settling, draws, other caches or orderings, other GPUs, or any game.df89dd1041, merged withmaindab0113e06(run). The merge's one conflict wasdocs/dev/debt/libSceAgcDriver.md, wheremainadded its "Unmapped depth planes" entry at the same place as this PR's "Depth surfaces over compared shared memory" entry; both are kept. The PR's own added and removed lines are unchanged.agc_driver_compared_shared_snapshotskips, as the runner has no Vulkan device;agc_write_tracking,agc_write_watch_coverage,guest_memory,guest_shm_openandguest_jit_shared_memorypass ×10: 5 run, 5 pass, 0 skip, 0 fail;APS5_SKIP_TIMING_TESTS=1.719a048a1f, merged withmain1e20650e84(run). The merge's one conflict was the list of cases inWindowsWriteWatch.cpp'smain(), wheremainadded three private-commit cases next to this PR's four; all seven run. The PR's own added and removed lines are unchanged.windows_write_watch,agc_rewatched_direct_shared,agc_import_alias_shared,agc_compared_shared_generations,agc_resident_direct_remap,agc_write_tracking,agc_write_tracking_metadata_commit,agc_write_watch_coverage,guest_memory,guest_shm_openandguest_jit_shared_memorypass ×10: 12 run, 11 pass, 1 skip, 0 fail.agc_driver_compared_shared_snapshotskips, as the runner has no Vulkan device;agc_write_tracking,agc_write_watch_coverage,guest_memory,guest_shm_openandguest_jit_shared_memorypass ×10: 5 run, 5 pass, 0 skip, 0 fail;APS5_SKIP_TIMING_TESTS=1.37a5d9495d(run): its only red is a build failure: its newwindows_write_watchcase uses the call the commit adds, so it doesn't build with 1ad5f29e20's code. The next commit's red covers the behaviour. At37a5d9495d, on Windows,windows_write_watch,agc_rewatched_direct_shared,agc_import_alias_shared,agc_resident_direct_remap,agc_write_trackingandagc_write_tracking_metadata_commitpass 10 runs in a row, 6 of 6, none skipped; on Linux,agc_write_trackingpasses 10 runs in a row. The libc suite passes on both and the libkernel suite on Linux; on Windows,guest_raise_in_system_calltimed out once at the end of the libkernel suite, the runner timeout above.1712336477(run): red with 37a5d9495d's code, 3 of 3 on Windows:agc_compared_shared_generationsfails with "write tracking test failed: a store into rewatched direct memory once shared reads as unchanged since a generation before the sharing", and the other 6 pass. Green at1712336477: on Windows those 7 pass 10 runs in a row, none skipped; on Linux,agc_write_trackingpasses 10 runs in a row. The libc and libkernel suites pass on both.388f5f13e6(run): on Windows the same 7 pass 10 runs in a row, none skipped.1ad5f29e20(run):47c8df4950), 3/3:agc_import_alias_sharedfails with "memory an import's alias demoted was armed by the import";1ad5f29e20: on Windows,agc_import_alias_shared,windows_write_watch,agc_rewatched_direct_shared,agc_resident_direct_remap,agc_write_trackingandagc_write_tracking_metadata_commitpass ×10; on Linux,agc_write_trackingpasses ×10;guest_raise_in_system_calltimed out at 30 s on both arms,47c8df4950and1ad5f29e20, so 206 of 207 pass; this commit changes nothing in libkernel. The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners;47c8df4950and green 10/10 at1ad5f29e20, with two control cases, and a mutant without the line that clears the record fails.47c8df4950(run):99aeb0ac56), 3/3: "a page to be compared when shared was collected after every view was unmapped and it was mapped again";47c8df4950: on Windows,windows_write_watch,guest_shm_open,guest_jit_shared_memoryandguest_memorypass ×10, and the libc (142) and libkernel (207) suites pass; on Linux, the last three pass ×10, and the libc (115) and libkernel (197) suites pass;APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners;99aeb0ac56and green 10/10 at47c8df4950. The fullwindows_write_watchstops earlier under Wine 9.0 on both trees, so the GitHub run is the check for the rest.99aeb0ac56, merged withmaindf2569d2c5(run), with the PR's own diff unchanged by the merge:windows_write_watch,agc_rewatched_direct_shared,agc_write_tracking,agc_write_tracking_metadata_commit,agc_resident_direct_remap,agc_write_watch_coverageandguest_memorypass ×10, and the libc (142) and libkernel (207) suites pass;agc_write_tracking,agc_write_watch_coverageandguest_memorypass ×10, and the libkernel suite passes (197). libc's only failure isprecise_wait, a timing test that fails there withmain's code too.collectable()and the fresh pre-check reverted (run): "a page to be compared when shared was collected once shared" and "rewatched direct memory was collected once shared".81aec58343(run): onlyagc_rewatched_direct_sharedfails, with "imported direct memory was collected once shared".81aec58343(run):windows_write_watch,agc_rewatched_direct_shared,agc_write_tracking,agc_write_tracking_metadata_commitandagc_resident_direct_remappass ×10, and the libc suite passes;guest_raise_in_system_callhit its 30 s timeout once; this change doesn't touch libkernel;agc_write_trackingand the suites pass.ead292e2e5on its own (run): green on both runners.main's code, the new cases don't build, because they use the new calls.test/9136-guard-8926-v3, run): 33 Windows tests pass, includingagc_driver_graphicswith #8926's retired-import case. The same libkernel timeout appeared there once.81aec58343:agc_driver_msaa_dcc_clear_4andagc_driver_msaa_dcc_statepass 10/10;agc_driver_msaa_dcc_clear_2and_clear_8skip, because lavapipe has only 1 and 4 color samples;mainskips them the same way;^agcpasses 862/862 on the head and onmain.main27bcd6a29ewith #8926 and this PR's three code commits (all but the test-onlyead292e2e5), from before the merge withmain.ctest -R '^agc_driver_msaa_dcc_(clear_[248]|state)$' --repeat until-fail:10passes all four 10 of 10,_clear_2and_clear_8included.mainagainstcb9de8049c, on GitHub's Linux runner (llvmpipe, LLVM 20.1.2) (run):agc_write_tracking,agc_driver_partially_resident_snapshotand the threeagc_driver_host_import_*tests, 5 of 5 passing on both trees. The layer loaded 4 times on each, and neither log has aVUID-message, so the change adds none. The new GPU test,agc_driver_compared_shared_snapshot, runs only on Windows, so this comparison doesn't cover it. On the Windows runner, without a Vulkan device, 7 of the 11 tests pass and the 4 device tests skip.check_conventions.pypasses, with no comment lines added.Checklist
maindab0113e06, merged indf89dd1041; 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)MEM_MAPPEDside; Dripl1x's #9181 is the resident side)main; the compared-shared snapshot test on this PC's AMD Radeon integrated GPU (31.0.21923.1000), Windows 11Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:42:54Z.
Review: DISMISSED
I reviewed the Windows shared-page comparison changes, the resident-only call path and the added write-watch regressions against the issue evidence. The latest PR body reports zero zlib/resident faults for the combined fix; hosted conventions, relinker, register-context and write-watch checks pass. Platform Windows/Linux product jobs were skipped, and I did not run a local Windows test. I found no code-level blocker in the submitted changes.
Approval is blocked on the repository freshness rule: this head is based on
27bcd6a29e2fd69aba0626610b2350eb8f2847f6, while currentmainis94fbb8cea80af91db62ab6ee7e4da23341675c40; current main is not an ancestor of the PR head. Please rebase/update to current main and rerun the relevant focused Windows/write-watch validation on the updated branch. #9181 is an independent, complementary half of #9136; the two PR heads merge cleanly together, so I found no required dependency ordering between them.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T02:09:18Z.
Thanks, @MarcZX10.
99aeb0ac56merges currentmain(df2569d2c5) into this branch with no conflicts, and the PR's own diff is unchanged by it. On GitHub's runners (run), the write-watch and resident tests pass 10 runs in a row on Windows, with the libc and libkernel suites, and the focused tests pass on Linux too. Tested has the details. Thanks for checking #9181 as well.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T10:25:38Z.
@dantdmdarealguy thanks for the alternating runs of Grand Theft Auto V on the RTX 5080 (#9136 comment): the HDD Streamer crash in 4 of 4 runs without #9181 and this PR, and in none of 3 with them. It's in Tested as a second machine, with your note that the runs didn't count resumed faults.