fix(libc): wait only for imports over the freed span #12157
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12157"
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 ulissesemuman on 2026-10-27T23:57:01Z.
GuestAllocationsRequireUnpinneddecides whether a guestfree()may mutate anallocation by testing
range.use_count() != 1on the registeredRangethat coversthe address. The AGC driver imports memory in windows of at most 64 MiB out of arenas
that are far larger (the block that fails here is 16 KiB inside a 1.4 GiB arena), so a
host import of any other part of the arena keeps every
shared_ptrin thatRangealive and the wait covers bytes the mutation never touches. The waiter can only retire
imports that overlap the freed span, so the count never reaches one: a deterministic
livelock that ends in
and
abort().This adds a span criterion next to the reference count:
GuestAllocationsSetSpanQuery, a callback a driver registers with thepin waiter.
freed, comparing the physical span actually pinned (
memoryBase/pinnedBytes) andtaking the imports mutex without blocking (a busy mutex answers "covered").
exactly those bytes.
use_countstays the conservative criterion for lease holders,and any lease snapshot taken during the scan forces the old behaviour, so the window
where the answer could go stale is the one the registry lock already closes.
The intent is registry-side span granularity: leases keyed by
(address, bytes)instead of by
Rangeobject, so the query needs no driver callback at all. This hookis the shape that query needs, so moving the criterion into the registry does not have
to change the callers added here.
Overlaps #6565, which is the other half of this wait and not the same cause. That PR
stops a new lease or import of the extent a mutation is already waiting on from
re-pinning it, with a
retiringspan list in the registry; here the holders are fivelive 64 MiB imports of other offsets of the same ~1.4 GiB arena, none of which
overlaps the freed 16 KiB, and the snapshot map of the wait was empty, so the
re-lease race is not what aborts this title. Both edits touch
GuestAllocationsRequireUnpinned,GuestAllocationsAcquireandGuestAllocationsAcquireAll, so whichever lands first needs a small rebase. I wouldrather the
retiringlist grow into the span criterion and drop the driver hookentirely; the measurement above is in that PR's thread.
#6321 is the only other open PR that edits this wait's neighbourhood: it adds
ranges->reserve(registry().ranges.size())as the first line of the body ofGuestAllocationsAcquire, which is the line this change replaces with the snapshotcache. It changes the scan cost of
RequireAvailableandOverlaps, not the pincriterion, so it is a one-line rebase in either order.
Draft because of the base, not because of the mechanism. Everything here is measured
on
aec3b3880;mainis now6e9b9134f6(1660 commits, Apple/Mach-O support addedin
GuestAllocations.cppandGuestAllocationsAcquireMappedinserted next to thefunction this change rewrites), and
git merge-treereports one conflicted file here:the
#ifndef _WIN32line that became#if !defined(_WIN32) && !defined(__APPLE__),directly above the
newSnapshot()insertion. The criterion on6e9b9134f6is stillrange.use_count() != 1over the coveringRange(GuestAllocations.cpp:445), sothe failure and the fix are both unchanged; the rebase and a re-run of the A/B on the
new
mainare the next push to this branch.Tested
Windows 10.0.26300, AMD Ryzen 7 5700X, NVIDIA GeForce RTX 5060 Ti with driver 616.92,
ASTRO BOT PPSA21567 01.018.000.
The 12 focused AGC host-import and memory-lifetime test binaries,
origin/main(
aec3b3880) and this branch, same machine and GPU, withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:identical verdicts on both sides (10 pass,
agc_host_import_sparse_testsskips with77,
agc_driver_recorder_testsfails the same way onmainitself with(9) a scan under a pending write was keptatRecorder.cpp:1680), the same set ofvalidation messages (
VUID-VkDeviceCreateInfo-pNext-pNext, 13 occurrences per side,already present on
main), noterminate calledin either log, and the validationlayer loaded in every process.
Astro Bot, same scene, same pad script, same relinked executable, only the
libc.prx/libSceAgcDriver.prxswapped, withAPS5_TRACE_FRAMES=0:15andAPS5_TRACE_SUMMARY_EVERY=5:main(aec3b3880):this change:
The first 15 presents differ only by boot noise (
mainspent 546 ms and this change619 ms in present 0, which compiles the shaders of the first frame). Per-frame cost is
unchanged; what changes is that the scene after them exists:
mainstops presentingafter its 15th frame, waits 60 s on the lease of a 16 KiB block and aborts at +75.1 s
(reproduced at 72.6 s, 73.1 s, 74.6 s and 75.1 s; no
APS5_TRACE_SECONDSwindow of5 s, 10 s, 30 s or 90 s ever closes), so there is no later-scene capture to compare
against. With this change the same run reaches 213 s and is killed by the harness
timeout, 2296 presents in 15 s intervals, p50 78-81 ms (~12 fps) and GPU busy 51-57 ms
per present from the 108 s mark on, with no
still pinnedmessage:Traces:
aps5-trace-*.json.gzandaps5-summary.jsonlformain, for this changewith the 15-frame window, and for this change over the full 240 s run.
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 MarcZX10 on 2026-11-20T09:51:41Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the code inspected supports the span-based pin check, with test execution and the unprovided excluded-file list not verified.
What I ran
Reviewed head
ef18375f02dff942260509d60238a32bce7795c7using the prepared diff and pinned repository reader (repo_read.py). No commands or tests were run.Results
files.jsonwas unavailableFindings
Blocking
None.
Non-blocking
None.
Not verified
files.jsonwas not available through the reader, so excluded files could not be identified. The three changed files listed inmanifest.jsonwere read:core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp,core/libs/prx/libc/include/GuestAllocations.hpp, andcore/libs/prx/libc/src/GuestAllocations.cpp.To get this to a good state
Nothing is required. Optional: run the focused AGC host-import and memory-lifetime tests on the current head.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T15:44:05Z.
Review: COMMENTED
newSnapshot()incrementsliveSnapshotsbefore allocatingLease::Ranges. If that allocation throws, the deleter is never installed. The counter then stays elevated and disables the span-clear path for later frees. Please make the counter registration exception-safe. The branch also conflicts with current main in the guest allocation and buffer import paths.AI-assisted: yes.
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.