fix(libkernel): recheck a remap target's resident run after waiting for aliases #24034
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24034"
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-20T01:34:24Z.
What
On Windows a fixed remap of direct memory (
sceKernelMapDirectMemorywithMAP_FIXEDover a live mapping,RemapFixedIntoRegisteredinDirectMemory.cpp) waited for the GPU leases on its target's resident run, then for the leases on every alias of the new physical range. Waiting releases the registry lock while the driver's pin waiter finishes the leasing work, and driver workers lease again during that window, so the target was never rechecked.GuestAllocationsProtectonly rechecks the remapped range, not the rest of the resident run, which the mapping code frees and replaces as a whole (WindowsMappings::demote). A lease on a neighbouring registry range of the run (onesceKernelMprotecthad split off) went unnoticed, and the remap replaced the run under it.The same shape was in every mapping call that waits for pins on more than one span: it waited for one span at a time, each with its own loop, and kept decisions made before the wait (placement, conflicts, the mprotect span list, whether a fixed map covers registered memory) although the layout could change while the lock was released. A worker could also lease a span that had already been waited for, and the call went round again until the deadline.
One pin wait per mapping call
GuestAllocationsnow has one primitive,Mutation::WaitedForPins(spans, deadline):Acquire/AcquireAllleases skip them), waits one round with the lock released, unmarks them and returns true.Every caller builds its plan inside
do { ... } while (mutation.WaitedForPins(spans, deadline))under one deadline per guest call (APS5_PIN_WAIT_MS). It changes memory only after a scan of the whole current plan was clean, and the lock is held from that scan until the change, so the inner checks inUnmap/Protectfind nothing and do not wait. A timeout throws before anything changes, so the guest keeps its old mapping.Callers:
PlaceMap(fixed and hinted maps:DoMapDirect,MapAnonymous, the file-backed maps), which rechecks hint placement, the no-overwrite conflict and the exclusive check each round;DoMprotect;DoMunmap;PhysicalViews: every view, one wait, then unmap).RemapFixedIntoRegisteredandUnmapRegisteredno longer wait on their own.ReplaceFixedOverlapis nowClearFixedTargetandReplacesFixedOverlapis nowOverwritesRegistered. The per-span loops,RequireUnpinnedByandFirstPhysicalVieware gone. The single-spanRequireUnpinnedis the same primitive with one span and does not allocate. On Linux and macOS the resident-run and alias spans are empty, so only registered ranges being replaced can wait there.GuestAllocationsEndmerges a mutation's recorded spans once and gives a range a new backing object only while nothing else holds it. A range leased again during a later wait keeps its object, so later pin checks still see the lease. Its span is still reported as changed.Notes:
AcquireRegistered/AcquireMappedstill ignore the retiring marks, as on main, so leases taken through them can still cost extra rounds. The deadline bounds them.Tests in
core/libs/tests/GuestMemory.cpp, each red on the code before its fix:CheckFixedRemapKeepsTheTargetRunOutOfLeasesDuringTheAliasWait: (Windows only) during the alias wait, the pin waiter tries to lease a neighbour of the target's resident run thatsceKernelMprotectsplit off. The run is marked retiring, so no lease is taken and the remap finishes after one round with nothing leased.CheckFixedMapWhoseAliasWaitFailsKeepsTheOverlapCheckNoOverwriteConflictRefusesWithoutWaitingForAliasesCheckMunmapFinishesWhileWorkersLeaseAcrossTheRunEdgeCheckFixedReservationFinishesWhileWorkersLeaseAcrossTheRunEdgeCheckMprotectFinishesWhileWorkersLeaseEverySpanCheckMprotectRebuildsItsSpansAfterTheWaitCheckReleaseFinishesWhileWorkersLeaseEveryViewCheckFixedSystemFlexibleMapOverAnAllocationShrunkDuringTheWaitCheckDirectMapHintLeavesAMappingMadeDuringTheAliasWaitCheckDirectMapStopsWaitingForAnAliasUnmappedDuringTheWaitCheckNoOverwriteReservationRefusesAMappingMadeDuringTheLeaseWaitCheckMprotectWhosePinWaitFailsChangesNoSpanandCheckFailedFixedRemapKeepsTheLeasedNeighbourVisibleguard the timeout paths. The mprotect, release, reservation and system-flexible tests run on every platform.Not observed: what a GPU does with the replaced pages. The tests show the calls completing with leases held; no title or GPU run reproduces it.
Tested
173fbce19b:guest_memoryand 46 related memory, heap, import and write-tracking tests pass (2 skipped, as on main). Each new test was run against the code before its fix and failed.173fbce19bagainst main1f27e2abfa5, Linux gate (lavapipe, validation layer): 7 of 7 on both sides, no VUIDs: https://github.com/chitters2004/AnyPS5/actions/runs/46367580254Checklist
main(1f27e2abfa); no other open PR implements the same functions (#23613, #23770, #20349, #17936, #15040, #7149 touchDirectMemory.cppbut not the pin waits)NotImplemented_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 PrivacyOnion on 2026-11-20T02:00:59Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
9b8ad3d7bc:tools/check_conventions.pypassed.guest_memory_testsbuilt cleanly.guest_memory,guest_mmap_fixed_anonymous,guest_mmap_executable):core/libs/prx/libc/include/GuestAllocations.hpp,core/libs/prx/libc/src/GuestAllocations.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T10:30:30Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
9b8ad3d7bc5578a3785f94cfc7c6b96fde7000c5using the prepared diff and pinned repository reader. No tests or build commands were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Build/test results in the description, GPU/title behavior, and any files excluded from the review bundle were not independently verified. Read:
core/libs/prx/libc/include/GuestAllocations.hpp,core/libs/prx/libc/src/GuestAllocations.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp, andcore/libs/tests/GuestMemory.cpp. The manifest lists all four changed files as included; no excluded changed files are listed.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T23:27:00Z.
Review: COMMENTED
Architecture findings (2)
Advisory architecture findings (2)
38658574-df8b-4311-b596-5fc09828a1d8Reviewing files that changed from the base of the PR and between
1f27e2abfaanda6c5affa7b.core/libs/prx/libc/include/GuestAllocations.hppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.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-29T23:29:55Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the implementation and regression test support the stated fix, though Windows runtime behavior was not independently verified.
What I ran
Reviewed head
a6c5affa7bea558fe4938f0c08e0021c3986e66dusingrepo_read.pyto inspect the prepared diff and targeted source ranges. I ran no build or tests. Read all four changed files listed inmanifest.json; none of the changed files were excluded.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The author reports a passing Windows
guest_memoryrun and a passing Linux gate, but I did not run either. The Windows CI failure and negative-control results are author-reported. No title or GPU run was reported.files.jsonwas not read; the manifest lists all four changed files as included, and each was read.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T02:50:29Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this review; the optional points below are for follow-up.
What I ran
Reviewed head
c9dfc321400d85a5ad46315f3420a65208706e04from the prepared diff and targeted source ranges usingrepo_read.py. No build or tests were run here; author-reported test results are not independent runs.Results
Findings
Blocking
None identified in the code read.
Non-blocking
DoReserveVirtualnow callsRequireResidentUnpinnedwith a deadline, but then calls the legacymutation.RequireUnpinnedoverload, which creates a fresh deadline. If a pin moves between those waits, this operation can exceed one overallAPS5_PIN_WAIT_MSlimit. Consider passing one shared deadline through both calls, asRequireMapUnpinneddoes. Changed lines:core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1920.Not verified
GuestAllocations.hpp,GuestAllocations.cpp,DirectMemory.cpp, andGuestMemory.cpp. No changed files were excluded.To get this to a good state
Nothing is required. Optional: carry one deadline through both pin checks in
DoReserveVirtual.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T05:05:26Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
b6176fd9a7bd94ad81f7194dd7b83e3d944ab3e2; no build or tests were run. I readreview/description.md,review/commits.md,review/manifest.json,review/diff.patch, and the full included changed filescore/libs/prx/libc/include/GuestAllocations.hpp,core/libs/prx/libc/src/GuestAllocations.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp, andcore/libs/tests/GuestMemory.cpp(targeted ranges for the latter three).review/files.jsonwas not available through the reader; the manifest lists four changed files, all included.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not run the Windows regression tests or independently verify the author-reported Linux and Windows results. I did not inspect excluded files, if any, because
review/files.jsonwas unavailable; the manifest shows every changed file included. Physical-console, title, and GPU behavior remains unverified, as noted in the PR description.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T05:13:52Z.
Review: COMMENTED
Reviewed — no blocking findings
The change addresses the described pin-wait race, but I could not run the tests in this review interface.
What I ran
Reviewed head
ce069dae091681eb44e6407aa5c47091d1a8ec76. No commands or tests were run. I read the prepared diff and the relevant source and test ranges throughrepo_read.py.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not execute the Windows regression tests or Linux suite. The description reports local Windows results and fork CI results; I did not independently verify them. No files were excluded from review:
GuestAllocations.hpp,GuestAllocations.cpp,DirectMemory.cpp, andGuestMemory.cppwere read against the four files listed as reviewed.To get this to a good state
Nothing is required. Optional: run the new Windows regression cases on the current head when a suitable runner is available.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T05:15:00Z.
Review: COMMENTED
Architecture findings (1)
Advisory architecture findings (1)
c71fce97-5460-40c4-bc91-6a7530c052d5Reviewing files that changed from the base of the PR and between
b6176fd9a7andce069dae09.core/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T06:50:59Z.
Review: COMMENTED
Actionable comments posted: 4
309fcf90-6c30-4d39-adcb-b27adb945da8Reviewing files that changed from the base of the PR and between
ce069dae09and173fbce19b.core/libs/prx/libc/include/GuestAllocations.hppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.hppcore/libs/tests/GuestMemory.cppdocs/dev/ARCHITECTURE.mdIncluded 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-30T06:54:36Z.
Review: COMMENTED
Reviewed — comments
The change addresses the described pin-wait race, but I could not run the tests or complete a full-file review of the changed implementation.
What I ran
Reviewed head
173fbce19b4bea87c42b27dc1c67e44e4ae66c22using the prepared diff and the pinned repository reader. No test or build commands were run.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
I read the full
GuestAllocations.hppandDirectMemory.hpp, selected ranges fromGuestAllocations.cpp,DirectMemory.cpp, andARCHITECTURE.md, and reviewed the relevant added test hunks indiff.patch. I did not read all ofGuestAllocations.cpp,DirectMemory.cpp, orGuestMemory.cpp; the test additions were not inspected as a full file. The prepared bundle lists six changed files, and all six were represented in the diff;files.jsonwas not available through the reader. The author-reported Windows and Linux test results were not independently verified. GPU, title, and homebrew behavior was not tested.To get this to a good state
Nothing is required. Optional: run the Windows regression tests on this head and confirm the reported results.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-01T02:33:57Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; no follow-up is required.
What I ran
Reviewed head
26d35af106daa0ea92c88f4a32b19e8792a14efbusing the prepared diff and head-pinnedrepo_read.pycontext. Environment: Linux review container; no compiler or build tool was invoked.The diff and relevant source ranges were inspected; tests and builds were not run.
Results
Negative control
Not run; no tests were executed in this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
The reviewed implementation gathers and rechecks mutation spans around pin waits (GuestAllocations.cpp, DirectMemory.cpp). The diff adds regression coverage for wait and replan behavior (GuestMemory.cpp).
Not verified
No tests, builds, GPU or physical-console behavior were run. The PR’s reported local and CI test results were not independently checked. All six files listed under
reviewedinfiles.jsonwere reviewed through the prepared diff and head text; no files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T20:26:31Z.
Review: COMMENTED
The Linux
guest_memoryCTest passed with this head applied to6c6b29320f. The exact head also trial-merges onto700fae9438. The Linux run covers the fixed reservation recheck. The new cases involving resident-run demotion and waiting on direct memory aliases are Windows-specific. Could the test report list which of those cases ran in the native Windows build?Source review at
94a19c1ff8. Native Windows execution remains unverified here.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T23:50:01Z.
Review: APPROVED
Reviewed — looks good
The pinned change coordinates pin waits across current mapping plans and adds regression cases for wait races and timeout preservation.
What I ran
Reviewed head
a7c9b18ea0378f2803e15776b0e60407b87656aaon the Linux review host. I read the prepared description, commits, manifest, diff, all added lines, and relevant source context usingrepo_read.py; no build or tests were run.Results
Negative control
Not run; no test executable was run during this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
GuestAllocations.cpppin wait and backing renewalDirectMemory.cppmapping plans and callersGuestMemory.cppadded regression casesNot verified
No tests were run. Windows and Linux checks described by the author, and GPU, title, and console behavior, were not independently verified. All six changed files listed in
manifest.jsonwere reviewed; none were excluded.To get this to a good state
No changes are 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.