fix(agc): drop depth and stencil plane results over memory unmapped since they were drawn #15466
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15466"
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 JJenkx on 2026-11-03T13:18:27Z.
What
A depth or stencil plane keeps the results of its draws on the GPU until something reads its guest memory.
FlushDepthPlanesalready drops those results when the plane's memory is unmapped at store time ("depth plane … was unmapped with GPU results pending; they are dropped"). But a title that unmaps the memory and maps new memory at the same addresses before the store gets the old results written into its new data.Contra: Operation Galuga (PPSA15368) does this at the transition from the title screen to the intro. A pool that held its depth and stencil planes is unmapped, and new memory, which the game uses as a heap, is mapped at the same addresses about a second later. The next read stores the old planes over the heap. The game's allocator then follows a zeroed free-list link and crashes, or hangs walking the list. On
main4f41f4c375this crash happened in 2 of 6 runs here (Contra row in Tested). In a local diagnostic build on the samemain, every run logged 11 to 17 such plane stores over memory removed since the plane's last draw, and 4 of 6 crashed or hung.This change:
retireUnmapped, whichRetireUnmappedDepth_nid_no_patchis part of) now records each removed range that overlaps a stale depth or stencil plane, with a serial. The observer fires only for removals (Remove,Unmap), not for protection changes, so a reprotected plane keeps its results.FlushDepthPlanesandretireStoringStaledrop only the 64 KiB units that overlap a removed range, and the other units are stored as before. A stencil plane is stored whole, so its stores (FlushDepthPlanesandstoreStaleStencil) drop the whole plane. That loses the results of stencil units outside the removed range, a limit the debt entry now states. A plane that is entirely uncommitted is still dropped whole, as before. A draw after the remap marks the plane again, and its results are stored.DepthPlanesStale()joins the condition inUpdateResidentImageUnmapObserver_nid_no_patch).Lock order: the observer runs under the registry mutation. It takes
planes.mutex(throughoverlapsStalePlane) and the new list's mutex, each released before the other is taken. Neither is held while taking any other lock. The store paths read the list under the surfaces mutex, andFlushDepthPlanesprunes it underplanes.mutex. No path holds the list's mutex while takingplanes.mutex.#8932 (closed) drops pending storage-image results at an unmap through the same observer. This change covers depth and stencil planes, which that PR does not touch.
Tested
AMD Radeon RX 6650 XT, RADV Mesa 26.2.6, Linux 7.2.11, Release.
agc_driver_depth_plane_unmapped_plane(DepthPlanePartialStore --unmapped-plane): a depth draw over a registered plane, then one mutation that removes and adds the allocation again, then a CPU fill of the plane. A whole-planeFlushDepthPlanesmust store nothing and leave the CPU's bytes. A second draw after the remap must then be stored in full.agc_driver_stencil_plane_memorygains the same case for a stencil plane: stencil 0x33 drawn, the allocation removed and added again, the CPU writes 0x44, and a flush must store nothing; stencil 0x18 drawn after the remap must reach guest memory. Onmainplus only the tests, both fail: "a read of a plane unmapped and mapped again stored the results drawn before the unmap" and "a read of a stencil plane unmapped and mapped again stored the stencil drawn before the unmap". With this change both pass.agc_driver_depth_plane_unmapped_plane_fillandagc_driver_depth_plane_partially_unmapped_plane): a depth fill recorded before an unmap and remap must not be stored by a later flush with no further draw. When only the first unit is unmapped and mapped again, a flush must store exactly the other five units and leave the first unit's new bytes. Both fail at the previous heada82312517band pass atbe898698d1.be898698d1, every builtagc_driver_(depth|stencil|native_multisample|multisample|idle_store|graphics)test passes: 59 of 59 withVK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, with noVUID-in any log. The Contra runs below are at the earlier heada82312517b.main4f41f4c375plus only this PR's tests, so its libraries aremain's: 2 of 6 crashed (SIGSEGV at 125 s and 101 s, in the allocator after the transition).0x241dd0000. Nomainrun does.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)Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T13:36:51Z.
Review: APPROVED
Reviewed — approved
The change stops depth and stencil plane results drawn before an unmap from being stored over memory mapped again at the same addresses. Both new test cases fail on
mainand pass with the change. I found one non-blocking gap: a depth fast clear still pending at the time of the remap.What I ran
Head
a82312517b2691efc9c98db52404cc9c56d51f35,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged with--no-ffin a clean worktree. Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19), Release.Results
git diff --checkcheck_conventions.pylibs(script)main(agc_driver_division, the denormal-mode tests,vector_constant_factor_tininess, an unwrap test); none are related to this changemain)VUID-linesdepth_plane|stencil_plane(13 tests, includingagc_driver_depth_plane_unmapped_planeandagc_driver_stencil_plane_memory)git status --porcelainis emptyNegative control
I restored
DepthSurface.cppandMultisampleTarget.cppfrommain, kept the PR's tests, and rebuilt both test executables. Both new cases failed with the messages the description quotes:output on main + tests
After I restored the merged files and rebuilt, all 13 depth and stencil plane tests passed again.
Findings
Blocking
None.
Non-blocking
A depth fast clear or plane fill pending since before the unmap is still stored over the remapped memory.
DepthSurface.cpp:2802-2804.FlushDepthPlanescallssurface->MarkAllStale(planeBytes)for a surface whosependingClearhas the depth bit. Only after that does it testunmappedSince(plane, plane + planeBytes, surface->depthMark).MarkAllStalenow setsdepthMark = nextPlaneSerial()(DepthSurface.cpp:1107-1111), so the mark is always newer than any recorded removal and the check never drops anything on this path.pendingClearwithout marking the plane. The callers areDepthSurface.cpp:3034andDepthSurface.cpp:3324.unmappedSincecheck ahead of theMarkAllStalecall at line 2802. Alternatively, take the depth mark when the clear is recorded (inNoteHtileClear/NotePlaneFillfor the depth aspect) and keep it inMarkAllStaleon this path. A variant of--unmapped-planewith a depth HTILE fill between the draw and the remap would cover it.The list of unmapped ranges is pruned only in
FlushDepthPlanes.storeStaleStencilandretireStoringStaleread the list but never prune it. The list stays bounded, becausenoteUnmappedPlanerecords only ranges that overlap a stale plane and the next flush that reaches its main loop prunes them. If no stale plane remains,FlushDepthPlanesreturns early and old entries stay in memory until a later plane triggers a flush. This needs no change; I note it only for future readers.I checked the lock order the description gives, and it holds:
pruneUnmappedPlanesruns withplanes.mutexheld (DepthSurface.cpp:2826-2830), so the order isplanes.mutex→ list mutex.noteUnmappedPlanereleasesplanes.mutex(inoverlapsStalePlane) before it takes the list mutex. No path takes the two in the opposite order.GuestAllocationsSetUnmapObserver_nid_postfixis a plain atomic store, so callingUpdateResidentImageUnmapObserver_nid_no_patchfromnoteStalePlaneunder the surfaces mutex cannot deadlock with the observer.Not verified
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T14:40:45Z.
Review: APPROVED
Re-checked — approved
Commits
317296719d,ce8aaf32dbandbe898698d1take the depth mark when a draw, clear or fill is recorded instead of inMarkAllStale, drop only the depth units a removal covers, add two tests and update the debt entry. This resolves the pending-fill finding from my previous review.What changed since my last review
DepthSurface.cpp:MarkAllStaleno longer advancesdepthMark. A newNoteDepthWork()does that, called from three places: a depth-writing or clearing bind,NoteHtileClearwith the depth aspect, andNotePlaneFillfor depth.takePlanetakes the owner's mark along with the owner's pending clear.FlushDepthPlanesandretireStoringStaleuse the newunmappedUnitsto drop only the stale units that overlap a removal newer than the mark. A fully uncommitted plane is still dropped whole.--partially-unmapped-planeand--unmapped-plane-fill, each registered as its own CTest case withTIMEOUT 30.What I ran
Head
be898698d1a6d310f010efd72c95ddb5c32920b5,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged with--no-ff(4d3ee2824ce1abd4dfc9c4eba41a451e0c6aa9a3) in a clean worktree. Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19), Release.git diff --checkcheck_conventions.pylibs(script)mainand are unrelatedmain)VUID-linesdepth_plane|stencil_plane(15 tests)git status --porcelainis empty; 15/15 pass againNegative control output
--partially-unmapped-planepasses onmain, as expected:mainstores every unit. The test guards against the whole-plane drop that the previous head introduced, and it fails there. So my run confirms the author's negative control againsta82312517b.Previous findings
MarkAllStaleno longer touches the mark (DepthSurface.cpp:1114-1118).DepthSurface.cpp:739-755).MarkAllStaleexpansion inFlushDepthPlanesno longer hides the removal (DepthSurface.cpp:2825-2835).agc_driver_depth_plane_unmapped_plane_fillcovers exactly this sequence. It fails ata82312517band passes at this head.FlushDepthPlanes: No longer applies. This was an observation for future readers, not a requested change. The list is still bounded, becausenoteUnmappedPlanerecords only ranges that overlap a stale plane.oneandonlydean's partial-unmap point is also addressed for depth:
unmappedUnits(DepthSurface.cpp:2363-2375) marks only the units that a removal newer than the mark overlaps.FlushDepthPlanesclears only those units and stores the rest.retireStoringStaledoes the same (DepthSurface.cpp:2474-2480).count, andcountisstaleUnits.size().retireStoringStaleindexesremoved[unit]only up to that same size.New findings
Blocking
None.
Non-blocking
None.
Not verified
a82312517b, not at this one. I have no GPU here, and the rules forbid launching titles. The author's 59/59 run atbe898698d1is accepted as author-reported.SyncFromMemoryskips that while a depth clear is pending. A later draw that does not cover those units could then store old texels over the new memory. I did not check whether mapping fresh memory counts as a write in the tracker, and I did not write a test for this case.