fix(agc): validate host imports against their recorded backing #16751
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16751"
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 chitters2004 on 2026-11-07T15:13:27Z.
What
A host import was valid only while the import policy would still choose the same run for it.
refreshImportschecked each import withsameRange, which compared it with the runimportRunBoundspicks now, under the current budget and remembered refusals and failures. Any change to the guest allocation registry that rechecked an import could retire it although every range it was made from was still registered, for example when a neighbouring range made a larger run possible or a remembered refusal expired.importRunAtalso retired the imports a larger run overlapped before it tried to create the larger one, so a refused or failed replacement left those ranges with no import and their uses fell back to copies.The first commit makes an import's validity depend only on its recorded backing:
sameRangekeeps an import while every registered range it was made from is still registered with the same owner.The second commit keeps growth owed until it is made:
retireFreesAtOnce, shared by retirement, admission and the evicting refusal). The[gpu] host import budgetline and the performance overlay report pinned bytes.Remaining gaps, in the
Host import refreshentry ofdocs/dev/debt/libSceAgcDriver.md:VK_EXT_memory_budgetreport is seen only when the budget is next computed.Overlap, checked on 2026-11-07 against
main46552cb461withtools/pr_overlap.pyand by hand: three open pull requests change lines ofGuestBufferMemory.cppthat this branch changes, and none implements the same thing. #2434 adds an early failure inimportRunAt, which has to stay before the retirement loop that now runs after the replacement import is created; #4448 changes the same check inrefreshImportsand addsRetireIdleImports, which retires throughretireImportand so gets the pinned accounting unchanged; #15919 adds another reason to rebuild a cached address space inAcquireRegisteredbeside the growth rebuild, and the two compose. #15674, #16474, #6548, #10443, #15302 and #16286 change other functions of the same file (#16286 only reads imports throughExistingHostImportFor). The other 70 the tool reported conflict withmain; I compared those with a commit since 2026-10-24 by function. Rechecked against4977f64052: the tool confirms the same three (#2434, #4448, #15919) and could not compare 81 that conflict withmain.Tested
Head
11a59dedf4onmain6b733c4ef2.Rebased onto
main6b733c4ef2.0254c4186fconflicted with #15919 (patch the cached address space after an import change) inGuestBufferMemory.cppand.hpp. The resolution keeps both counters (patchedEpochandrebuiltGrowth) in the stats struct, the stats return and the profile line. It keepsmain's order in the rebuild decision: when the imports epoch changed it triespatchedSpacefirst, and the growth check comes next, so a patched space still rebuilds when growth is owed. #16130 (keep joined imports when a window block is picked) merged cleanly; it changes only howimportRunBoundschooses a block. The debt entry was shortened to stay under 1000 characters.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationand the same focused AGC set. Each side was built by the fork's Windows CI job and run here, with the toolchain and commit checked.6b733c4ef2: 233 tests, 100 % passed.11a59dedf4: 234 tests, 100 % passed, includingagc_host_import_validity.VUID-line in its log or its replay subprocess logs, neither log containswas not found but was requested by env var VK_INSTANCE_LAYERS, and there are no new failures.agc_driver_graphics(which runs the patched-space and growth cases),agc_host_import_validityandagc_host_import_retire*: Linux and Windows 4 of 4 passed.VUID-lines; the conventions check passed.Review fix
efaedd5df4(rebased as11a59dedf4; unrelated imports retire before a replacement is created). The newagc_host_import_validitycase fails on0254c4186f's order with "a join that evicts an unrelated import was created while that import was still pinned" and passes with the fix. Local RTX 3080 gate (driver 621.14), same environment and focused AGC set as below: 233 tests, 100 % passed, noVUID-line, no new failures againstmain4977f64052. GitHub runners, Mesa lavapipe with validation, rebased onto4977f64052(run 42395591789): Linux and Windows pass, noVUID-lines.Refreshed onto
main4977f64052. Head0254c4186f;git range-diffagainstf62ea7bd08shows the same patches (2 commits,=). Local RTX 3080 gate on both, same environment as below (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation), built one after the other in one build tree, with the focused AGC set of all five pull requests of this series (233 tests on this head, 232 onmain): both 100 % passed, noVUID-line in either log or in replay subprocess logs, and neither log containswas not found but was requested by env var VK_INSTANCE_LAYERS. Fork CI on the rebased head against4977f64052: run 42374063297: Linux 37 of 38 passed, 1 skipped; Windows 38 of 44 passed, 5 skipped, andagc_driver_recorderfailed, as it does onmain. NoVUID-line on either side, conventions check passed. The full runs below were on the previous base46552cb461.Vulkan validation. RTX 3080,
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest --test-dir build -R 'agc|write_tracking|guest|indirect_draw|draw_finisher|depth_bias|draw_pipeline' -Vonf62ea7bd08(3200 tests) and onmain46552cb461(3199 tests). Every test failing in either full run was then rerun alone on both builds, one build at a time with nothing else on the GPU, usingctest -Rwith the union of the 16 names and the same environment: on both, 15 of 16 passed and onlyagc_driver_direct_write_completionfailed, which fails onmaintoo (#15547); the other failures of the full runs were timeouts and0xc0000409exits under load from concurrent gates and did not reproduce alone. Both full logs hold the same 18VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605lines and no otherVUID-; neither containswas not found but was requested by env var VK_INSTANCE_LAYERS. The change adds no validation error.Fork CI (GitHub runners, Mesa lavapipe with the Khronos validation layer, head against
main): run 42329076516, rebased there onto46552cb461. Linux: 28 of 31 passed, 3 skipped; Windows: 30 of 48 passed, 17 skipped, andagc_driver_recorderfailed, as it does onmainon the same runner. NoVUID-line on either side, conventions check passed. The skips are lavapipe's: no other queue family to settle imports on, no deferred settle of small imports, no/dev/udmabuf, and write watching not kept for native clear imports; the RTX 3080 gate above runs them.Tests.
agc_host_import_validity(new): imports stay while their ranges are unchanged and retire when their backing changes; a range registered next to an imported block does not retire it; a block whose larger replacement failed is kept, stays in the address space and is not counted against the budget; a request for a larger run does not replace an import a build still holds; a run kept from being imported whole by a held build, the budget or a registry change is queued and imported whole by the first build that allows it, also when only the budget rose; a partial promotion stays queued until one import covers the run; the queue holds one entry per live run over the budget; cached builds with a run queued for growth do not query the budget; a join that evicts an unrelated import retires it before the join is created, so the imported bytes stay within the budget plus the imports the join replaces (a mock cap on total imported bytes refuses the join on the old order).agc_host_import_retire_tests(extended): replaced imports a recorder holds are counted as retired and pinned until it releases them; the live and retired bytes match the memory the driver holds; a grown run that fits only when replaced and retired imports are not counted is refused; a run refused for pinned bytes is promoted by the next build after the recorder releases them, with no registry or budget change.BdaResources.cpp,HostImportProofs.cppandRecorder.cppnow expect an unchanged import to be kept and replaced by exactly one larger import when the larger run can be made, and kept with nothing made when it cannot.Red proof, from the branch history:
agc_host_import_validityfailed 3 of its 5 checks on themainof 2026-11-04, before the first commit.agc_host_import_validityred.agc_host_import_retire_testsfailed, among them replaced imports held by the recorder not counted, counts not matching the memory the driver holds, a grown run imported although it fits only without the retired imports, and imports pinning more than the budget. Mutants that drop the count on retirement, drop the subtraction on release, admit without the retired bytes, skip the wake-up after a release, or free at once while an idle recorder's hold keeps the import each turn it red (13, 17, 8, 2 and 14 failing checks).GTA V, under host import pressure. GTA V (PPSA04263, content 01.011.001), standing scene, 3840x2160, with
APS5_HOST_IMPORT_MIB=4096so that the budget refuses imports (at the default budget neither side refused anything in this scene, with about 8.1 GiB imported). Counts over 75 s after the player stands still, one boot per side,APS5_PROFILE_DRAW=1(counts only, never timing), with a local counter patch applied identically to both sides and not part of this PR (diag-imp.patch).mainwas7383be1a19, the change0159dee3a1, this branch before its rebase onto46552cb461(git range-diffshows the same patches).mainmainRefusals per draw are 29 % lower, copies and staging per frame are within 1.2 % of
main(twomainboots at the default budget differ by 1.4 to 2.6 % on these counters), the fallback work that refusals cause is lower, and the retired-and-pinned bytes drain. 1,482 refusals in the window come from the access requester on this change and none onmain; their cause was not traced. One boot per side, and the two boots drew a different number of draws per present (about 3,150 and 2,960), so the sizes are uncertain; the direction is the same for every fallback counter.This is a fix, not a speed claim. Frame time in two interleaved pairs of the standing scene at the default budget (warm caches, same settings as above but without the counter patch or
APS5_PROFILE_DRAW) shows no change beyond noise:7383be1a19c70159dee3a18e7383be1a19c70159dee3a18eChange minus main, mean ms/frame: pair 1 -0.77, pair 2 +2.58, mean +0.90 (+0.7 %); p50 -0.5 and +2.4; p99 -20.6 and +0.9.
Twelve
maincaptures of this scene give a standard deviation of 1.85 ms (1.35 %) per frame, and the pair deltas have opposite signs. These pairs ran on this branch before its rebase (0159dee3a1), on the host before Resizable BAR was turned on and Windows moved from build 26220 to 26320 (driver 621.14 on both). Summary lines, digests and the deviations from the Performance report form are attached to the pr-imp-traces release.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 gabichuuuuu on 2026-11-07T16:06:16Z.
Review: COMMENTED
One ordering change that lets the pinned total go past the budget while a replacement is created.
On
main(importRunAt), the imports a run replaces are retired, andretireLeastRecentfrees down to the budget, beforeImportConstruction. At0254c4186fboth steps come after the new import is created:GuestMemory::ImportWatchedcallscreateImportatGuestBufferMemory.cpp:2459, butretireImportfor the replaced range runs at 2538–2543 andretireLeastRecentat 2544. Admission at 2362–2366 counts thereplacedandevictablebytes as already free when no recorder is active (immediate). So whilecreateImportruns, every byte it counted is still pinned.Example, budget B = 256 MiB, no recorder:
live = 192, and192 + 128 > 256. Soevictable = 192and the request is admitted.createImportallocates 128 MiB with 256 MiB still pinned, which makes 384 MiB, or B + 128. Onmain, A and then C were retired first, so the peak stayed at B.This contradicts the description's "The budget bounds every pinned import". If the driver refuses the host-pointer allocation over
heapBudget − granted/8, the run falls to copies wheremainimported it, andallocationFailedswitches the budget source. That last part is inferred, not run.I see why the order changed:
retireLeastRecentdoesn't skip[base, base + bytes), so moving it back as is could evict the very imports being replaced. Retiring the unrelated least-recent imports before the construction would keep the bound, for example with a variant that skips entries overlapping the run, likeevictableBytesalready does. Otherwise the debt entry could state the transient overshoot. A test withmock.importLimitequal to the budget, a replacement that evicts an LRU import, and no recorder would show which one happens.Read only; not built or run here.
AI-assisted: yes.