fix(agc): read a sparse buffer piece the guest decommitted as zeros #27451
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27451"
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 DerKarussellbremser on 2026-11-26T12:38:02Z.
What
A buffer range the guest commits on demand is copied as a sparse region.
DescribeCommittednames its committed pieces when the range is added (AddReadable,AddWritable), andGuestBufferMemory::copyRegionreads each of those pieces withGuestMemory::Readat upload and fills the gaps with zeros. When the guest decommits one of the pieces between the two,Readthrowsguest memory is not readable at … (range …)and the dispatch fails.Assassin's Creed Black Flag Resynced (PPSA28183, 01.006.038) does that in its first 3D scene. A compute shader has a 128 MiB heap bound as a writable buffer, and the title changes which 8 MiB chunks of that heap are committed several times a second. In the failure I captured, the region was made with 24 MiB committed at its start and copied 83 ms later, when the last of the three chunks was gone. All five runs with #27437 ended there, between the first present of the scene and about 100 s into it.
copyRegionnow reads the pieces withGuestMemory::ReadCommitted, the read for heaps the guest commits on demand: a page that is no longer committed reads as zeros, which is what the region's gaps already upload as. It takes the same read guard and GPU write flush asRead, so a piece that is still committed is read as before. A copy enters the upload cache only when its piece was readable andGuestMemory::ForgetSerialdid not move while it was read (second and third commit, after the reviews), so zeros read from a piece that was gone cannot be served to a later upload of the range. A range that was wholly committed when it was added has the same window: it is read whole at upload, and that read threw when the guest had decommitted part of it since. It now falls back toReadCommittedwhen the whole read fails, and that copy is not cached (fourth commit; the title reached this branch in a later run, see Tested).Write-back is unchanged: it stores only bytes the GPU changed, and a GPU change over a piece that is no longer writable still throws (
the GPU changed guest memory … that is not writable).Tested
Windows 11 Home 10.0.26200, i7-10750H, 16 GB, NVIDIA GeForce RTX 2060 6 GB, driver 610.91, WinLibs GCC 15.2.0 r7 UCRT, Release, on
main97319cf0b9.RunSparseBufferHoleTestshas a fourth shape,decommitted: the last 64 KiB unit of the heap is committed when the range is added and decommitted beforeUpload. It checks that the upload holds zeros there and the first unit's bytes, that write-back still stores a change in the first unit of a writable range, and that it does not commit the unit again. A fifth shape,hollowed, commits the whole heap, adds the range and then decommits its middle unit; it checks zeros there, the other two units' bytes, and write-back to both of them. Both shapes run readable and writable, inagc_sparse_buffer_holeson the mock device and inagc_driver_sparse_buffer_holeson the GPU. Outside Windows the shapes take the page away withmprotect(PROT_NONE), which is how this test models uncommitted pages there; they cover a stable hole, not the window insideReadCommittednamed under Not covered.GuestBufferMemory.cppas onmain,readable decommittedandwritable decommittedfail withAGC driver: guest memory is not readable at 0x… (range 0x…+0x10000), the title's message. With the first three commits and without the fourth,readable hollowedandwritable hollowedfail with the same message.ctest -R "agc.*sparse|host_import|heap_mirror|bda_|upload_cache|^agc_driver_graphics$": 57 tests, 53 pass and 4 skip on Windows (agc_driver_host_import_dma_buf_watch,agc_driver_sparse_dma_buf_import,agc_host_import_sparse,agc_host_import_retire_snapshot).agc_driver_upload_cache_unreadable_piece(fifth commit, Windows only: it changes page protection through the guest arena) covers the cache guards on write-watched arena memory. It adds a range, makes one 64 KiB unit unreadable before the upload, checks zeros there, restores the unit without writing it, and uploads the range again: the second upload must hold the unit's bytes. One case reads the range whole, one as a sparse region. Red without the guards: withuploadGeneration = 0removed from the whole-read fallback the first case fails, and with the sparse guard line removed the second fails, both withthe zeros read while a unit of … range was unreadable were served to a later upload. It does not time a mapping change inside the read itself; the unit is unreadable before the read starts.50c07b6794, review): withAPS5_COMPARE_SPARSE_UPLOADS=1an unwatched sparse copy is kept in the upload cache and refreshed at the next upload byrefreshSparseUpload, whose untracked path reads each span withGuestMemory::Readand so never reached the committed-page read.copyRegionnow abandons a refresh that throws and rebuilds the copy, and a rebuild that meets a hole drops the sparse spans together with the generation.RunSparseBufferHoleTestswarms the upload of thedecommittedshape before the unit goes and poisons that copy; the newagc_sparse_buffer_holes_compared_uploadsruns the shapes with the switch set.readable decommitted: AGC driver: guest memory is not readable at 0x… (range 0x…+0x10000). With it, the 63 tests of thectestexpression above pass onf73d791b3d(59 pass, the same 4 skip); the branch was then rebased ontoa4ebd9084cwithout conflict and not rebuilt there.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branch fix/agc-sparse-region-decommitted-piece: the 53 pull requests it could compare do not overlap. 30 more conflict withmainand could not be compared (#4448, #5372, #6186, #6197, #6412, #6481, #7642, #9289, #9508, #9852, #10193, #12157, #12944, #13148, #13863, #13920, #13928, #13929, #15296, #15297, #15570, #15621, #16831, #18173, #20315, #20580, #22190, #22550, #22647, #25610). I searched their diffs (for #7642 and #9508, whose diffs GitHub does not serve, the fetched heads against their merge bases) for the read of a backed piece incopyRegion,ReadCommitted(first,RunSparseBufferHoleTests, itssparse buffer holesmessages and adecommittedshape, and none changes them.compute shader 0x…: AGC driver: guest memory is not readable at … (range …), after 1 present, a few seconds, about 15 s, about 30 s and about 100 s of the scene.6ac1b81541cherry-picked with #27437 and #27371 ontomain11952ab7d1, a clean build): one run, 34 min from the first present, without this abort. It passes the scene and the sequence after it, reaches the title's map loading screen and its online service dialog, and ends there with a different message,AGC graphics: a color target's sample count differs from the rasterizer's, which this change does not concern.fcc294a958, testa235ef3cae): the two fallbacks ofcopyRegioncaught everyruntime_errorof the read, the flush hook's included. They now rethrow unless a piece of the range is inaccessible.agc_driver_upload_cache_unreadable_piecegains a written range whose flush hook throws once and requires that error to reach the caller. Red without the fix:a flush failure over a readable written range did not reach the caller. With the validation layer loaded ata235ef3cae: this test,agc_driver_graphicsand theagc_driver_bda*tests, 15 of 15, noVUID-line. A flush failure while a piece is also unreadable still takes the fallback, whose own read flushes again.ReadCommittedcopies the committed ranges it has just queried, so a decommit between its query and its copy still faults; that is how every caller ofReadCommittedbehaves there and this change does not address it. AMD and Intel GPUs.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
https://claude.ai/code/session_011TXsSW8zeAgKgHLCFsYwM3
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T12:46:25Z.
Review: COMMENTED
Actionable comments posted: 1
6f1024fd-76e6-470d-8c6e-66d8146ee4bbReviewing files that changed from the base of the PR and between
35d3474ef0andadb7f50ee9.core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T12:58:20Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: clarify and cover the possibility that a piece becomes accessible again between the accessibility check and the committed read, leaving a zero upload cacheable.
What I ran
Reviewed head
bf67b15377dae57ff28d3e11711cff1348d4cdf7. No commands or tests were run; repository context was read withrepo_read.py.Results
Findings
Blocking
None verified.
Non-blocking
GuestBufferMemory.cpp:7543checksAccessibleand then callsReadCommitted. If a page is inaccessible at the check but becomes committed beforeReadCommittedqueries it, the latter can copy its contents whileuploadGenerationremains zero. This only avoids caching that upload, so it is conservative; the reverse race can cache zeros for a page committed immediately afterReadCommittedqueried it. Consider using one page-state snapshot for the read and cacheability decision, or add a focused cache test for commit/decommit transitions around upload.Not verified
Read the changed
GuestBufferMemory.cppfunction and relevantGuestMemorydeclarations and implementation, plus the changedBdaResources.cpptest function.files.jsonandreview/files/were unavailable through the reader; no additional changed files were listed inmanifest.json. Tests and platform behavior were not independently verified. Author-reported results are indescription.md.To get this to a good state
Nothing is required. Optional: cover the upload cache behavior across decommit and recommit transitions.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T13:02:39Z.
Review: COMMENTED
a6a1d7f1-9b89-4aa8-93c0-bb51cee558beReviewing files that changed from the base of the PR and between
adb7f50ee9andbf67b15377.core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T13:26:48Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
f186c7e11361244dc5db21b72db9af107765ef2cagainst basemain. I read the prepared description, commits, manifest, diff, all changed lines, and the relevantGuestMemoryfunctions throughrepo_read.py. No tests or builds were run.Results
Findings
Blocking
None.
Non-blocking
BdaResources.cpp:4585-4587: The decommit test usesVirtualFree(..., MEM_DECOMMIT)on Windows but only changes protection toPROT_NONEon other platforms. The description notes that Linux and macOS still have a race inReadCommitted; consider limiting this case to Windows or documenting the platform-specific test expectation so a passing non-Windows test does not imply equivalent decommit behavior.Not verified
Tests, builds, runtime behavior, and the author-reported Black Flag runs were not independently verified. The full
GuestBufferMemory.cppandBdaResources.cppfiles were not read; only the changed lines and relevant surrounding ranges were inspected. Files reviewed:core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp,core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp. Files excluded by the review-folder size rule:core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp.To get this to a good state
Nothing is required. The two non-blocking points above are optional.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T14:32:00Z.
Review: COMMENTED
Reviewed — comments
The review covers head
6ac1b815414fd4b9217f0eb25c8da38ad5dc5157; the changed implementation and added tests were inspected, and no checks were run in this review.What I ran
Read the prepared description, commit list, diff, manifest, and the changed-line ranges through the pinned reader. Read the changed
copyRegionimplementation and relevantGuestMemoryhelpers at the pinned head. No tests or build commands were run.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:7551.Not verified
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppwas read through the pinned reader because it was excluded from the review folder as too large.core/libs/prx/libSceAgcDriver/tests/BdaResources.cppwas read at all changed lines. No other changed files were listed in the manifest.To get this to a good state
Nothing is required. Consider adding direct coverage for the sparse upload cacheability guard described above.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T16:13:54Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
f0182fa9f516f6a44eb96e0fe62e0bb8970f94b8. No commands or tests were run in this review. Readreview/description.md,review/commits.md,review/manifest.json,review/diff.patch,GuestBufferMemory.cpp,BdaResources.cpp, andUploadCacheUnreadablePiece.cpp; the fullGuestBufferMemory.cppwas excluded from the review folder but the relevant head ranges were read throughrepo_read.py.Results
Findings
Blocking
None.
Non-blocking
UploadCacheUnreadablePiece.cppis not listed in the graphics test executable sources or any matching test registration found in the test CMake files reviewed. The author reports running this test, but its integration into the repository test build could not be confirmed. Consider adding its target and registration so future CI runs exercise the cache guard.Not verified
Tests, builds, and the reported Windows game runs were not run or independently verified. Linux and macOS behavior, AMD and Intel GPUs, and the race where mappings change during
ReadCommittedwere not verified.files.jsonwas not read; the manifest listsGuestBufferMemory.cppas excluded from the review folder, and the changed code plus relevant callers and test-build context were read throughrepo_read.py.To get this to a good state
Nothing is required. Optionally, wire the new upload-cache test into the test build and CI registration.
Imported from boykopovar/AnyPS5. Originally posted by kteal on 2026-11-28T00:27:06Z.
Review: COMMENTED
I found a warm-cache case when testing this change together with the sparse upload cache on main. With
APS5_COMPARE_SPARSE_UPLOADS=1, I warmed an entirely unwatched sparse upload, released the caller's reference, then made one previously readable unit inaccessible afterAddReadableand before the nextUpload. On both Intel Iris Xe and an RTX 4070 Laptop GPU, the second upload threwguest memory is not readable. The cold upload and the same case with caching disabled both produced the expected zeros.The exclusively owned snapshot takes the untracked path in
refreshSparseUpload. That path reads the old backing directly, so execution never reaches the newReadCommittedfallback incopyRegion.One possible fix is to abandon the cached refresh if a backed span is no longer fully readable, then let the committed-page copy rebuild it. If that copy encounters a hole, clear both the generation and the sparse spans used to publish the snapshot. I tested that approach locally on both GPUs. Readback returned zeros for the inaccessible unit, preserved its neighbors, and returned the original nonzero data after access was restored. Warming the unwatched snapshot before the existing decommit test would cover this interaction.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T12:18:50Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the reviewed code supports merging.
What I ran
Reviewed head
50c07b679415615a8b3b81174fb341ae07834a99usingrepo_read.pyto inspect the prepared diff and selected source ranges. No tests or build commands were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests were run. The full repository file list was not available as a separate
files.json; the review bundle’s file listing and manifest identify the four changed files. Read all four:GuestBufferMemory.cpp,BdaResources.cpp,agc_driver_graphics_tests.cmake, andUploadCacheUnreadablePiece.cpp. No changed files were excluded from review. Linux/macOS and AMD/Intel GPU behavior remain unverified here; the description notes a race window inReadCommittedon Linux and macOS.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T21:18:15Z.
Review: COMMENTED
The compact-buffer fallback catches
std::runtime_erroraround all ofGuestMemory::Read, including the GPU flush hook. A transient synchronization failure can therefore cause a second flush and disappear if the second call succeeds. Could the fallback apply specifically to an unavailable piece, with flush failures propagated? The sparse copy already has a committed-range read that preserves holes without using this exception path.Source review at
50c07b6794. The head trial-merges onto192c4e45a0. Paired Linux checks are queued.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T02:16:07Z.
Review: APPROVED
Reviewed — looks good
The changed upload paths handle decommitted pieces and avoid caching zero-filled reads; I found no blocking defect.
What I ran
Reviewed head
a235ef3cae8d449ca8cb71c304752bc1fcc1db10using the pinnedrepo_read.pyreader and prepared diff. No automated checks were run.Results
Negative control
Not run; no tests were run in this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
No runtime tests were run. Linux and macOS behavior, other GPU vendors, and physical-console behavior were not verified. The description reports Windows test runs; I did not rerun them. Read the changed source hunk and relevant context in
GuestBufferMemory.cpp, the changedRunSparseBufferHoleTestsfunction inBdaResources.cpp, the changed test registration inagc_driver_graphics_tests.cmake, and all ofUploadCacheUnreadablePiece.cpp.files.jsonlists no excluded files.To get this to a good state
Nothing is required.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.