Skip to content

fix(agc): keep registered-range queries from pinning guest ranges - #1973

Open
Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/28166
Open

Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/28166

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#28166, originally opened by meh7an.

Source commit: 67c8e0fba8c96eb58235a5cd3106d3bc3bc0e6f7. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

RegisteredReadableCovers and RegisteredReadableEnd ask whether readable registered memory covers a span. Storage-image stores, sampled lookups, DMA copy destinations and the texture segment walk call them per dispatch or per lookup. Both took GuestAllocationsAcquireRegistered, a lease that holds a shared_ptr to every readable registered range. While a mutation waits in GuestAllocationsRequireUnpinned for a range's use count to drop to 1, that lease is not cached, so every query builds a new one and the range the mutation waits on stays pinned while any query is in flight. The pin waiter then syncs the recorder round after round (leased allocation ... still pinned after 8 recorder syncs). When no mutation waits, the query fills the cached readable lease instead, which keeps a reference to every range until the next mutation.

libc gets GuestAllocationsRegisteredReadableEnd(address, bytes, pieces). Under the registry lock it walks the registered ranges that cover the span and returns the end of the last one, or 0 when a byte is not covered; with pieces it copies the covering ranges out by value. It takes no reference to any range. Registered ranges do not overlap, so only the range starting at or before an address can hold it, as lookupLocked already assumes. Like GuestAllocationsAcquireRegistered, it still lists a range a mutation waits on, which 71427a8 requires (the host import lifetime test queries the registry from inside vkFreeMemory). The two queries now call it. GuestAllocationsAcquireRegistered stays for the unit shadow retire loops and the address-based builds, which hold their lease.

The header comment that described the query as one registry lease is removed, and a debt fragment updates the libc entry "Leases while a mutation waits for an unpinned range" to name the new function.

Related open PRs: boykopovar#12157 removes a different source of these pins (imports of other parts of an arena), and boykopovar#26802 and boykopovar#6481 add RegisteredReadableRunEnd and RegisteredReadableOverlaps next to these two queries, both on a lease; either could move to this walk once both land.

Tested

  • Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 2080 Ti with driver 621.40, Vulkan SDK 1.4.363.1.
  • Two new test cases. In registeredReadableCoverTests (agc_graphics_profile_draw), a query must leave no reference to the range it answered for; main's queries fail it (a registered query kept a reference to the range it answered for: the cached readable lease holds it). The new entry agc_registered_readable_retiring (--registered-readable-retiring) removes a pinned range on one thread. Its pin waiter, which the removal calls while the range is retiring, checks that the range has left new GuestAllocationsAcquire leases but is still covered for both queries, then drops the pin. A walk that skips retiring ranges fails it (an allocation a removal waits on must stay covered for registered queries). It runs in its own process, so the driver's pin waiter of the shared suite is not replaced, and nothing in it depends on timing; it passed 100 of 100 repeated runs.
  • The ctest entries that exercise these queries and the pin wait (agc_graphics_profile_draw, agc_registered_readable_retiring on this change, agc_host_import_validity, the six agc_driver_guest_memory_lifetime* entries, the host import lifetime, other family, mutation race, cut lifetime, unmap, retirement and second mapping entries, the four unit shadow entries, guest_memory, guest_sync_on_address and guest_allocations_changes) ran with ctest --test-dir build -R <those names> -V on main fa6e12b and on this change, both with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer (validation layer 1.4.363.1). 22 of 22 pass on main and 23 of 23 on this change. In both logs the loader inserts the layer in the same 17 tests, the ones that create a Vulkan instance (the other five are the mock-Vulkan graphics suite and three libc and libkernel tests), neither log has was not found but was requested by env var VK_INSTANCE_LAYERS, and neither has a VUID- line.
  • The cached-lease mutant ran on main 6100e07, before main moved to fa6e12b (none of the five commits between them touches these files); the skip-retiring mutant ran against agc_registered_readable_retiring on fa6e12b.
  • PPSA38500 on a bring-up tree (main 1632904 plus 14 open PRs and local changes): 8 still pinned after 8 recorder syncs lines within 170 s without this change, none in 244 s with it. These are counts of that log line from one run each, not trace summaries.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA38500 Grand Theft Auto VI (PPSA38500) boykopovar/AnyPS5#20383
  • GPUs tested: NVIDIA GeForce RTX 2080 Ti, driver 621.40, Windows 11 Pro 10.0.26300
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved readable-memory range checks, including while an allocation is being released, so reported coverage reflects the registered ranges.
  • Tests
    • Added coverage for readable-range queries during allocation retirement and for releasing pinned allocations.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Registered-readable coverage

Layer / File(s) Summary
Add the libc coverage query
core/libs/prx/libc/include/GuestAllocations.hpp, core/libs/prx/libc/src/GuestAllocations.cpp
The new API returns the end of contiguous host-readable registered coverage, or zero when the request is invalid or coverage is absent.
Integrate the query and test retiring allocations
core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp, core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp, core/libs/prx/libSceAgcDriver/tests/Graphics.cpp, core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake, docs/dev/debt/changes/fix-agc-registered-queries-unpinned.md
AGC registered-readable checks use the libc query. Tests check coverage while an allocation retires and after its pin is released. The graphics test runner and change note are updated.

Suggested reviewers: chitters2004, jjenkx, oneandonlydean

Change: Bug fix

Merge Risk: Minimal · up to 67c8e

This change stops frequent registered-range queries from pinning guest ranges. No merge-blocking risk was identified in the supplied evidence.

Security architecture risk: Minimal · up to 67c8e

AGC can check whether guest memory is still registered without keeping that memory pinned. The check still recognizes readable ranges while release is pending, but it does not grant permission to use the memory or replace the leases that protect in-flight GPU work.

Security review details

Security Blast Radius

  • observed — The inspected production path takes guest-derived addresses and sizes through existing AGC helpers to the in-process libc registry. For example, shader-buffer sizing uses the returned bound to limit or reject a descriptor. The new helper reads registry metadata, not the memory at the supplied address.

Trust Boundaries and Controls

  • observed — A retiring range can still be reported as covered while it remains registered and host-readable. That answer is distinct from admission to new ordinary leases, which exclude retiring ranges. Existing pins still govern when the mutation may proceed.

Resilience and Maintainability Implications

  • observed — The waiter still releases the registry mutex for recorder progress, rescans after reacquiring it, and clears the retirement marker on normal return or exception unwinding. The added test checks that an ordinary lease excludes the marked range, that the coverage query still sees it, and that coverage disappears after removal.

Rollout and rollback

  • inferred — No operator migration is indicated by the inspected change. libc still controls when a mapping can be removed or reprotected; reverting AGC and libc together restores the old query path. Reverting libc alone would remove the helper that head AGC calls.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Cppcheck (2.22.0)

Cppcheck (2.22.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

core/libs/prx/libSceAgcDriver/tests/Graphics.cpp

Cppcheck interrupted; analysis of this file is incomplete

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 6
  • Waived: $1.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e41176fc-2ee5-406a-a8a9-6cc8f16257a0
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 67c8e0f.

Files selected for processing: 7

  • core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp
  • core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
  • core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
  • core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake
  • core/libs/prx/libc/include/GuestAllocations.hpp
  • core/libs/prx/libc/src/GuestAllocations.cpp
  • docs/dev/debt/changes/fix-agc-registered-queries-unpinned.md
Files with no reviewable changes: 1

  • core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp

Limit details: You’ve used all 10 included reviews currently available.

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants