perf(agc): trap CPU reads of label readbacks on Linux instead of storing them #19054
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19054"
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 pawcijo on 2026-11-12T17:52:27Z.
What
Ports the label read traps (
2cf5b41ec8) from macOS to Linux. Before a label, a pending linear or small storage image that shares no page with another pending or storing image gets a read trap instead of being stored; the first CPU access stores it.GuestWriteWatch.cpp: a LinuxReadTrapsbeside theuserfaultfdwrite watch. Trapped pages arePROT_NONE; a SIGSEGV/SIGBUS handler (SA_SIGINFO | SA_RESTART, chained to the previous one; libkernel's fault delivery also callsGuestWriteWatchHandleFault) hands the page to a resolver thread. Host writes andGuestWriteWatchProtectresolve overlapping traps first./proc/self/mapsat the first trap in a registered range, then followsGuestWriteWatchProtect. Registering memory costs no/procread.DirectMemory.cpp(sceKernelMprotect, stack protection) andGuestArena.cppcallGuestWriteWatchProtecton Linux too, as on macOS, so a guestmprotectcannot drop a trap.Texture.cpp: the label trap paths are enabled on Linux. On Linux only an image whose store goes over a host import is trapped; one without is stored at the label as before, because its CPU store saw the trapped pages as unmapped and wrote nothing, so a read got stale bytes.e4f8bedaf1, after the review below): the check that an image shares no page with another pending or storing image is one sorted sweep per label instead of a scan of all pending images per image, on macOS too, and the LinuxTrapReadsreturns as soon as it finds the trap already held. Onbig_hand_pirate_islanda reviewer measured this head at about 252 ms frame p50 against 245 ms onmain(310 ms before it), see the comments.6e0650ef8c):GuestWriteWatchReadTrapGenerationreturns a counter the Linux read traps bump when a trap is dropped or taken by the resolver; an image keeps the generation its trap was confirmed at, so a label callsTrapReadsfor it again only after the counter moved. While the pending and flushing lists are unchanged and every trapped image passed its checks at the last label, the label skips trapped images outright. The range list is kept on the pending set and rebuilt only when that set changed. On macOS the generation differs on every call, so the label asks as before. The effect on frame time is not measured yet.59cea6f125): an import on Linux pinned pages a label read trap had madePROT_NONE; NVIDIA reports the failed pin asVK_ERROR_OUT_OF_DEVICE_MEMORY, and three in a row lower the import budget for the rest of the run (Cyberpunk 2077 intro at about 6 instead of 51 presents per 10 s, see the comments).ImportWatchednow opens the same host write over the import as on macOS, so the traps on its range are resolved first and none is set while the driver pins the pages.05dc8b4a69): on RADV a submission fails withVK_ERROR_DEVICE_LOSTwhile a host pointer import has aPROT_NONEpage (see the comments). A label read trap is now set only where every import over the image's pages is a dma-buf, or where the driver is NVIDIA's proprietary one (ReadTrapsOverHostPointerImportsUsable), which pins the pages at import and has dma-buf imports off; that is observed in runs, not documented. Other drivers store those images at the label as onmain.6efccc8efe, reworked in815baabafb): with the traps on, PPSA03974 stopped presenting about 20 s into its intro (see the comments). The draw pipeline's committer runs a queued item without the GPU mutex while the item keeps its read ranges; when it touched a trapped page it waited for the resolver, andResolveLabelTrapwaited forDrawPipeline::HeldReadsover that range, which only the blocked committer could clear. The Linux read traps now record which thread waits in each fault slot (GuestWriteWatchAwaitsReadTrap), andResolveLabelTrapstill stores first but stops waiting on held reads when the committer is the thread blocked on a trap (LabelTrapResolveWaits). A page no queued item holds is stored before the committer reads it; one a queued item holds comes back unstored, as the label store leaves it onmain. The bypass stays GPU mutex holders only. Whether this was the PPSA03974 hang is not yet shown by a run.APS5_NO_LABEL_READ_TRAP=1keeps the store at the label. Gaps are in the fragmentdebt/changes/perf-agc-linux-label-read-traps.md(fordebt/libc.mdanddebt/libSceAgcDriver.md).Tests:
linux_read_trap_*(new, no GPU, skips without theuserfaultfdwatch). The read trap part ofagc_small_tiled_shadow_labelnow runs on Linux; its case of a small image inside a large tiled one first passes one label, because since44df0c5fbca label stores an image on its first write and that store took the small image with it, which contradicted the case on every platform. Not run on macOS. On lavapipe the whole test skips (host imports unavailable), so CI covers the Linux traps only throughlinux_read_trap_*.agc_driver_label_trap_committer_resolve(new, Linux, no GPU) traps two pages and queues an item that holds reads over one of them and reads both: the held page must read back unstored, the other stored, and it fails when the resolver waits on held reads regardless of the blocked committer.agc_read_trap_import_submit(new, Linux, GPU; adapted from a probe posted in the comments) makes one page of an importPROT_NONEas a label read trap does and requires a fill of an unrelated buffer and of another page of the import to succeed; it runs over a host pointer import only whereReadTrapsOverHostPointerImportsUsablekeeps traps on them and over a udmabuf dma-buf import where the device can import one, and skips otherwise.agc_read_traps_over_host_pointer_imports_driver(new) checks the driver table, and a new BdaResources case checks that an import is trappable over a host pointer import only where the driver allows it, not past its import and not once it is gone; its dma-buf side runs only where dma-buf imports are on, so not on NVIDIA.agc_write_tracking_host_pointer_import_over_read_traps(new, Linux) checks that an import over a trapped range runs with no trap on its pages, that no trap is set during it, that the kernel can read the pages and that the trapped bytes were stored first; it fails without the import fix.agc_driver_label_ordered_flushswitches the read traps off for itself: a trapped image is not stored at the label, so it never takes the ordered flush that test counts.Related open PRs that change the macOS read traps this mirrors: #15729 (conflicts on the
debt/libSceAgcDriver.mdentry), #18848, #16884. Whichever lands later should carry its change to the other platform.Tested
Linux 7.0.0-43-generic, NVIDIA GeForce RTX 3070 Ti, driver 595.99.02, Intel Core i5-14600K.
Head
815baabafb(mergedmaine825fa1492; committer and label trap fix):ctest -R 'agc_driver_label_trap_committer_resolve|agc_read_trap_import_submit|agc_read_traps_over_host_pointer_imports_driver|agc_host_import_sparse|bda|label|storage|shadow|write_back|writeback|read_trap|write_tracking|linear_target|draw_pipeline|queued|mprotect|kernel_stack_protect|protection_report|write_watch' -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 215/215 passed.Insert instance layer "VK_LAYER_KHRONOS_validation"is in the log, nowas not found but was requested, 0VUID-. Not rerun onmainat this head. Not run on PPSA03974, PPSA21564 or AMD here.python3 tools/check_conventions.py --base origin/mainandpython3 tools/debt_fold.py --check: passed.main'smacos-drawn-linear-label-trap.mdfragment changed the same "Label read traps" entry, so its sentence is folded into this branch's fragment and that file is removed.Head
794cd8fc8d(addsagc_read_trap_import_submit):ctest -R agc_read_trap_import_submit -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: passed. The host pointer case ran and passed; the dma-buf case did not run, since this driver'svkAllocateMemoryrefuses the udmabuf import (VK_ERROR_OUT_OF_DEVICE_MEMORY). The layer is inserted, 0VUID-. Not run on AMD.python3 tools/check_conventions.py --base origin/main: passed.Head
a3703fcf50(mergedmain0766863ef6; debt entries moved to a fragment, dma-buf case added toagc_host_import_sparse):ctest -R 'agc_read_traps_over_host_pointer_imports_driver|agc_host_import_sparse|bda|label|storage|shadow|write_back|writeback|read_trap|write_tracking' -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 146/146 passed.Insert instance layer "VK_LAYER_KHRONOS_validation"is in the log, nowas not found but was requested, 0VUID-.agc_graphics_profile_draw(no Vulkan instance) passes. Not rerun onmainat this head. Not run on AMD or on Cyberpunk 2077 here.python3 tools/check_conventions.py --base origin/mainandpython3 tools/debt_fold.py --check: passed.Head
05dc8b4a69(host pointer imports by driver):ctest -R 'agc_read_traps_over_host_pointer_imports_driver|bda|label|storage|shadow|write_back|writeback|read_trap|write_tracking' -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 136/136 passed.Insert instance layer "VK_LAYER_KHRONOS_validation"is in the log, nowas not found but was requested, 0VUID-.agc_graphics_profile_draw(no Vulkan instance) passes. Not rerun onmainat this head. Not run on AMD or on Cyberpunk 2077 here.python3 tools/check_conventions.py --base origin/main: passed.Head
59cea6f125(import fix):ctest -R 'label|storage|shadow|write_back|writeback|read_trap|write_tracking' -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 125/125 passed (5 skipped on this device), among them the newagc_write_tracking_host_pointer_import_over_read_traps.Insert instance layer "VK_LAYER_KHRONOS_validation"is in the log, nowas not found but was requested, 0VUID-. Not rerun onmainat this head. Not run on Cyberpunk 2077 here.python3 tools/check_conventions.py --base origin/main: passed.Head
6e0650ef8c(mergedmain5e58891ed2):ctest -R 'label|storage|shadow|write_back|writeback|read_trap|write_tracking' -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(layer 1.4.341),VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1: 124/124 passed, among them all threeagc_driver_label_ordered_flush*,agc_small_tiled_shadow_labeland the tenlinux_read_trap_*(new:linux_read_trap_generation).Insert instance layer "VK_LAYER_KHRONOS_validation"is in the log, nowas not found but was requested, 0VUID-. This set was not rerun onmainat this head, so there is no side-by-side log; with 0VUID-on the change it adds none.linux_read_trap_threads: 20/20 runs;agc_small_tiled_shadow_label: 5/5 runs.python3 tools/check_conventions.py --base origin/main: passed.e4f8bedaf1(big_hand_pirate_islandat 3840x2160 locks up this 8 GB machine); the reviewer's numbers fore4f8bedaf1are in the comments. The numbers below are from055ee969b4. Not run on macOS.Earlier head
055ee969b4, basef92cd1c990:ctest -R linux_read_trap: 7/7 passed;linux_read_trap_threads30/30 runs.agc_driver_recorder_tests) onmainand this PR withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(layer 1.4.341),VK_LOADER_DEBUG=layer,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1,ctest -V: both 34/34 passed (3 skipped on this device),Insert instance layer "VK_LAYER_KHRONOS_validation"in all 34, nowas not found but was requested, 0VUID-in both logs.python3 tools/check_conventions.py --base origin/main: passed.The speed figures below are from
055ee969b4againstf92cd1c990and predate both the review that found the slowdown onbig_hand_pirate_islandand its fix. PPSA21564 01.007.000, title screen (title_controller_ship) with no input, warm shader and pipeline caches,APS5_TRACE_SECONDS=60:20, runs alternated main / PR / main / PR, each ended bytimeout 100(exit 124). The title ignoresAPS5_OUTPUT_RESOLUTIONand renders 3840x2160.f92cd1c990055ee969b4f92cd1c990055ee969b4[storage]label write-backs per 10 s: about 3,940 (16.5 GB) on main, 25 to 35 (14 to 20 MB) with this PR. In all four captures the graphics queue worker's event buffer filled after about 13 s (trace_dropped,totals_full: false), so the summary totals are complete but the traces lack that thread's last 6 s.Traces: attached below.
aps5-trace-f92cd1c990a5-260164.json.gz
aps5-trace-055ee969b4dc-261372.json.gz
aps5-trace-f92cd1c990a5-262210.json.gz
aps5-trace-055ee969b4dc-263079.json.gz
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 MarcZX10 on 2026-11-18T15:36:22Z.
Review: COMMENTED
Reviewed — comments
This review identified material uncertainty in the Linux signal-handler and mapping-protection lifecycle, and several changed files were not fully read.
What I ran
No tests or builds were run. I reviewed the prepared diff and used the pinned repository reader for targeted source ranges.
Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
The complete changed-file text was not read for
VulkanDevice.cpp,GuestBufferMemory.cpp,Texture.cpp, andRecorder.cpp; their relevant hunks were inspected indiff.patch. ForGuestWriteWatch.cpp, the added trap implementation and exported entry points were inspected in the diff, along with targeted head ranges for the surrounding watch and API paths. The full 580-line change was not read at the pinned head. Most other changed files were inspected through the prepared diff, not by reading every changed line from their head versions. No build, test, signal-chain integration, hardware, or console behavior was verified. The description's reported results are author supplied and were not run here.To get this to a good state
Nothing is required. Optional: run the focused Linux read trap and AGC import tests on the current head, including with Vulkan validation enabled.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T05:39:33Z.
Review: COMMENTED
Reviewed — no blocking findings
The prepared diff and targeted source review at
6efccc8efe835e8d27e74aba613538bbbdac09c7found no verified blocking defect; I did not run checks, and several changed files remain unread.What I ran
6efccc8efe835e8d27e74aba613538bbbdac09c7.Results
Findings
Blocking
None verified in the code inspected.
Non-blocking
None.
Not verified
The full text of
VulkanDevice.cpp,GuestBufferMemory.cpp,Texture.cpp, andRecorder.cppwas not read. The relevant hunks inGuestBufferMemory.cppwere inspected via the pinned reader, but the rest of that changed file was not. Other changed files were not read in full, including the added tests and the largerGuestWriteWatch.cppaddition beyond the portions noted above. No build, tests, or runtime checks were performed. The author reports focused test runs and convention checks in the PR description; those were not rerun here.Files read:
description.md,commits.md,manifest.json,diff.patch,README.md, and portions ofGuestWriteWatch.cppandGuestBufferMemory.cpp. Also inspected targeted diff hunks forDrawPipeline.cpp,GuestMemory.cpp,Texture.cpp,VulkanDevice.cpp, andBdaResources.cpp. Files excluded from direct reading:VulkanDevice.cpp,Texture.cpp,Recorder.cpp, and the deleted debt fragment; full texts of other included changed files were not all reviewed.To get this to a good state
Nothing is required. The author-reported tests and convention checks remain unverified in this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T09:47:41Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected changes show no verified blocking defect, but this review is incomplete because several changed files were excluded or not read and no checks were run.
What I ran
Reviewed head
d4ce495f8e8c8f60883828214fd1acde321f7b1dusing the prepared review bundle and the pinned repository reader. No test, build, or convention command was run.Results
Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Not verified
The complete review is not verified:
VulkanDevice.cpp,GuestBufferMemory.cpp,Texture.cpp, andRecorder.cppwere excluded from the review folder. I inspected selected ranges ofGuestWriteWatch.cpp,GuestBufferMemory.cpp,Texture.cpp, andDrawPipeline.cpp; I did not read every changed file marked included in the manifest. Tests, build, convention checks, macOS behavior, AMD behavior, and performance at this head were not run or confirmed. The author-reported test results in the description were not independently run.To get this to a good state
Nothing is required to address a verified defect. To support a formal decision, complete inspection of all changed files and run the relevant checks on this head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:20:21Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change based on the inspected code; the Linux read-trap implementation and its label and import integration are not fully verified because several large changed files were excluded from the prepared review bundle.
What I ran
No commands or tests were run. I inspected the prepared diff and targeted source ranges at head
815baabafbc8a501f741e03bc4b10a30ddc0b113.Results
Findings
Blocking
None in the code inspected.
Non-blocking
None.
Not verified
I read the description, commits, manifest, prepared diff, and included changed files:
DrawPipeline.hpp,ReadTrapsOverHostPointerImports.hpp,DrawPipeline.cpp,GuestMemory.cpp,Context.hpp,GuestBufferMemory.hpp,Texture.hpp,BdaResources.cpp,HostImportSparseDmaBuf.cpp,ReadTrapImportSubmit.cpp,ReadTrapsOverHostPointerImports.cpp,WriteTracking.cpp, the three changed CMake test files,LabelOrderedFlush.cpp,LabelTrapCommitterResolve.cpp,GuestWriteWatch.hpp,GuestArena.cpp,DirectMemory.cpp,LinuxReadTraps.cpp, andperf-agc-linux-label-read-traps.md.VulkanDevice.cpp,GuestBufferMemory.cpp,Texture.cpp, andRecorder.cppwere excluded from the prepared full-text bundle; I read only targeted ranges of the first three and one line ofRecorder.cpp. The deleted debt fragment was not read. I did not run tests, convention checks, or verify the reported device and game behavior.To get this to a good state
Nothing is required. Optional: review the remaining changed ranges in the excluded files and run the focused checks on this head before relying on the reported results.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T10:05:52Z.
Review: COMMENTED
Reviewed — comments
The review inspected the prepared diff at
c53e84a189ed6c06c1d84ffbd1d1f1854f4cd502; a concrete trap lifecycle defect needs correction, and no checks were run independently.What I ran
Read the prepared review bundle and targeted source context using
repo_read.py; no tests or build commands were run.Results
Findings
Blocking
core/libs/prx/libc/src/GuestWriteWatch.cpp:1506erases overlapping active traps inRegistered()without callingrestore(). If a watched range is registered again while a read trap overlaps it, the trap record is removed but its pages remainPROT_NONE; later faults can no longer find a trap to resolve, so access can hang or fail. Restore the removed traps before discarding them, while preserving protection tracking for the newly registered range.Non-blocking
None.
Not verified
Read changed files listed as included in
manifest.jsonand inspected targeted head source around the excluded large files. Full text excluded by the prepared bundle:Execution/src/VulkanDevice.cpp,Graphics/src/GuestBufferMemory.cpp,Graphics/src/Texture.cpp, andtests/Recorder.cpp; those were read only at relevant hunks throughrepo_read.py. The deleted debt fragment has no new text. No runtime checks, build, or independent test run was performed; author-reported tests were not independently verified.To get this to a good state
Restore active overlapping traps during registration and add a regression test that re-registers a range overlapping a trapped page, then verifies that access resolves or the page is restored.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T01:39:50Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this review; the points below are optional.
What I ran
Reviewed head
46f01f71b72b9a8774c0c826de5eff6644bdeab7against the prepared diff. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
Not verified
The complete text of these changed files was not read:
core/libs/prx/libSceAgcDriver/tests/Recorder.cppandcore/libs/prx/libc/src/GuestWriteWatch.cpp; targeted ranges ofGuestWriteWatch.cppwere read.core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp,core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp, andcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppwere read through the pinned-head reader at relevant ranges. No tests were run; macOS and AMD behavior, PPSA03974 behavior, and the final performance effect of the label memo were not verified. The deleted debt fragment has no head text to read.To get this to a good state
Nothing is required. Optional: add coverage for NVIDIA driver versions and AMD before broadening the host pointer import allowlist.