Repository navigation
fix(agc): keep a rewatched import's memory compared once it is shared - #9159
adriadam10 merged 13 commits into
Conversation
On Windows a fault that AnyPS5 resumes in guest code can overwrite the guest's red zone, [rsp-0x80, rsp-0x10) (boykopovar#361). WindowsMappings arms every alias of a shared page read-only when a clearing collect settles it, so each guest store into that page faults and is resumed. CompareWhenShared marks the pages of a range, and disarms any alias of them that is already armed. Once a marked page is shared, collectable() and the fresh pre-check refuse a clearing collect over it before anything is armed, so its aliases are never armed and the caller compares the range instead. While the page is still a resident run it is collected through GetWriteWatch as before, which takes no fault. windows_write_watch gains CheckComparedWhenShared: a marked resident range stays collected, then a second mapping makes it shared, and from then on its collects fail, neither mapping is armed and stores into it take no fault, while the unmarked half of the same mapping is still collected.
Since boykopovar#6678 a Windows host import under 1 MiB returns to the write watch when its deferred settle completes (rewatchSettledImports). Grand Theft Auto V (PPSA04264) imports buffers that are resident direct memory at that point and later become shared 16 KiB views, when a second mapping of the same direct memory demotes them. From then on the write watch arms them, every guest store into them faults and is resumed, and the resumed faults damage the red zone the game's zlib inflate_fast reloads its pointers from, so the open world crashes in every run (boykopovar#9136). Before boykopovar#6678 such a range stayed out of the write watch for good and was compared. Rewatch now marks each range it returns to the watch with GuestArenaCompareWhenShared first. While the memory stays resident it is write-watched, as boykopovar#6678 intends. Once it is shared its collects fail and it is compared again, as before boykopovar#6678, instead of being armed. agc_rewatched_direct_shared maps direct memory resident, takes a block out of the watch and rewatches it as a settle does, then maps the same memory a second time. The rewatched block must collect while resident, then collect as 0 through either mapping, stay unarmed and show the store through both, while a block that never left the watch is still collected.
|
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.
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.
|
…g case WindowsMappings keys its physical pages by the section handle's value. A handle value that an earlier case closed without ForgetSection can come back from CreateFileMappingW with that case's stored pages, and then the first mapping is shared from the start instead of resident, so CheckComparedWhenShared would fail its residency check. It passes today only because the case before it forgets its section. The case now forgets its own handle value before it maps.
Since boykopovar#8172, a host import that stays in the write watch keeps each of its watched runs in the watch instead of taking the whole import out when part of it was unwatched. Grand Theft Auto V (PPSA04264) imports resident direct memory that way, and later maps the same memory at a second address, which turns it into shared views. From then on the write watch arms them, and the game's own stores fault and are resumed, the red zone hazard of boykopovar#361 (boykopovar#9136, eboot 0x162b9e4 and 0x28f413a). ImportWatched now marks the resident pages of each run it keeps in the watch with GuestArenaCompareResidentWhenShared. They stay write-watched while they are private, as boykopovar#8172 needs for resident DCC targets. Once they are shared, their collects fail and they are compared, as before boykopovar#8172, instead of being armed. Memory that is already shared when it is imported is not marked and stays tracked as before. agc_rewatched_direct_shared gains the imported case: resident direct memory imported with the watch kept, then mapped a second time, must collect as 0 through either mapping, stay writable and show a store through both, while unimported shared direct memory is still collected. windows_write_watch gains 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.
MarcZX10
left a comment
There was a problem hiding this comment.
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 current main is 94fbb8cea80af91db62ab6ee7e4da23341675c40; 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.
|
Thanks, @MarcZX10. 99aeb0a merges current |
|
@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. |
|
@EagleBearerYS thanks, I reran the unchanged external fixture on Same workstation: Windows 11 build 26300, Ryzen 9 3900X, RTX 3080 Ti, NVIDIA 610.88, WinLibs GCC 15.2.0, Release. No game was launched. Four orderings, three repetitions per tree (36 fixture executions):
The two affected orderings cache the texture while resident, share its memory, then write from the CPU with no lookup between sharing and the write. One includes the fixture's explicit refusal-check collect; the other omits it. On the old combined branch, the first lookup still keeps the old texture, performs no byte comparison and the GPU reads The fixture source and shader are unchanged from the original report. I rebuilt it against the corrected head's own libraries. The baseline and old-branch binaries were rerun, with their runtime library hashes checked against their original build manifests. The corrected PR head was tested directly, without adding #9181; the old control is the original branch with both PRs. This is a comparison of those pinned trees, not isolation of individual commits. Your new All fixture runs and the new regression test used This closes the sampled-texture snapshot gap I reported, within this synthetic scope. I have not retested the host-import lifetime suite in this run, or tested real host-import storage views, buffer/draw/dispatch caches, other shared-memory orderings, other platforms or GPUs, any game, or performance. The fixture still calls AI-assisted: yes. |
|
Thanks, @embersas, for rerunning the unchanged fixture on the corrected head, with the old branch alongside as the control. The two orderings that showed the stale first lookup now pass in all three repetitions, and so does |
Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.
|
Re-checked df89dd1: it only merges current |
… run On Windows a cut inside a resident run turns the pages that are kept into shared views, which boykopovar#9159 keeps compared instead of armed. The cut lifetime test required the write watch to cover the larger side after such a cut. It now accepts a range that is not watched when every page of it is reported as compared, and still requires the GPU to read the guest's bytes. Fixes boykopovar#10746. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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):main47a266c, and the merge before #6678mainf6ce8e3MEM_MAPPEDmain7280a17 withAPS5_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 faultsImportWatchedkeeps 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:
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.Rewatchmarks each range withGuestArenaCompareWhenSharedbefore it returns the range to the watch.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.ImportWatchedmarks the resident pages of each run it keeps in the watch, withGuestArenaCompareResidentWhenShared, a resident-only variant of the same mark.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.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.WindowsMappingsflags it unreported.mainreports the pages at the first collect after the sharing.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.agc_driver_compared_shared_snapshottakes embersas's ordering through the productionCachedSampledTexture.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:
MEM_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. fix(agc): put a retired import's unchanged ranges back in the write watch #8926 depends on this PR.WindowsMappings::Map's shared path, where refactor(libc): map a run of shared guest pages as one view on Windows #9505 restructures the loop that turns a page's resident aliases shared. The resolution keeps refactor(libc): map a run of shared guest pages as one view on Windows #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
main663de1a + Dripl1x's d6e270e (fix(libc): stop write-protecting resident runs on Windows #9181) + 81aec58, 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.main7e2ba73 + fix(voice): accept valid worker priorities and affinities in sceVoiceSetThreadsParams #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.main3bbd232 + fix(libc): stop write-protecting resident runs on Windows #9181 + this PR up to 81aec58 + fix(voice): accept valid worker priorities and affinities in sceVoiceSetThreadsParams #9730 + fix(agc): avoid CPU waits for GPU-ordered labels #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.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.main0a0ed72 alone and together, plustest/9159-9181-together(d7cec80):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.agc_driver_partially_resident_snapshotand_pending_stores.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1and the Khronos validation layer; no game was run. Four orderings, three repetitions per tree:mainef0d8ae: 12 of 12 pass. Their earlier branch of this PR with fix(libc): stop write-protecting resident runs on Windows #9181 (d7cec80): 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.TextureCompareread, 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.maindab0113 (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.main1e20650 (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.windows_write_watchcase uses the call the commit adds, so it doesn't build with 1ad5f29's code. The next commit's red covers the behaviour. At 37a5d94, 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.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 at 1712336: 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.agc_import_alias_sharedfails with "memory an import's alias demoted was armed by the import";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, 47c8df4 and 1ad5f29, 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;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;windows_write_watchstops earlier under Wine 9.0 on both trees, so the GitHub run is the check for the rest.maindf2569d (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".agc_rewatched_direct_sharedfails, with "imported direct memory was collected once shared".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.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 fix(agc): put a retired import's unchanged ranges back in the write watch #8926's retired-import case. The same libkernel timeout appeared there once.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.main27bcd6a with fix(agc): put a retired import's unchanged ranges back in the write watch #8926 and this PR's three code commits (all but the test-only ead292e), 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.mainagainst cb9de80, 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
maindab0113, merged in df89dd1; 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 fix(libc): stop write-protecting resident runs on Windows #9181 is the resident side)main; the compared-shared snapshot test on this PC's AMD Radeon integrated GPU (31.0.21923.1000), Windows 11