memory: retry guest accesses while a mapping is briefly replaced (GTA V HDD Streamer crash) #2395
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2395
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2395"
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?
@oguzhanozfe on GitHub · 2026-11-18 22:35 UTC · original
oguzhanozfe:fix/gta5-hdd-streamer-map-fixed-retry→main· +305 −0 · 5 files · 4 commits · openSummary
Fixes an intermittent crash in Grand Theft Auto V (PPSA04263): roughly once every 100 sessions, the
[RAGE] HDD Streamerthread dies with an unhandled access violation inside zlibinflate_fast/inflate. The fault is on a 64 KiB direct-memory page that is mapped again a moment later.This is a port of TheCruZ's closed #1229 onto current
main.Problem
Two game threads
MAP_FIXEDthe same physical page at the same address. On the console that is an atomic no-op, and the streamer is already decompressing into the page. The emulator replaces a mapping by unmapping it and mapping it again (ReplaceFixedRangeWithReserved), so for that moment the page is gone and the streamer faults.Windows has the same gap when a partial unmap splits a file view.
UnmapOneunmaps the whole view, then maps the survivors again.Change
TransientUnmapWindows). A window is open while aMAP_FIXEDreplacement (flexible and direct maps) is in progress, and while a view split's survivors are remapped. The unmapped part of a split is never published.Differences from #1229:
mainalready restores the survivors' protections inUnmapOne.mainalready keeps Linux direct-over-direct replacements atomic.So only the retry windows are taken over, wrapped around
main's current code. #1229's protection snapshot code and its duplicate Windows permission test are dropped;PartialUnmapPreservesHostPermissionsalready covers that.Testing
Windows 11, Ryzen 9 5950X, RTX 3090, clang-cl (LLVM 23), Release.
virtual_memory_allocation_tests→ newAccessDuringMappingReplacement. A worker thread keeps reading and writing a flexible mapping and a direct mapping, while the main thread:MAP_FIXED256 times0xC0000005(access violation)ok (4577303 accesses, 305 retried)page_manager_testsandmemory_tracker_testspass.AI usage
AI (Claude Opus 5.5) was used to:
mainand resolve its conflictsThe analysis and the original fix are TheCruZ's (#1229), credited with
Co-authored-by.🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-11-18 22:44 UTC · original · edited 2026-11-18 23:31 UTC
No actionable comments were generated in the recent review.
Walkthrough
The change tracks guest-memory ranges during fixed mapping replacements and Windows view splits. The exception handler waits for matching transient-unmap windows and retries eligible access violations, with a per-page and per-window retry limit. A Windows-only test exercises concurrent access during flexible and direct mapping replacement and checks for mismatches.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · up to
165cfThis change retries guest memory accesses that land in a brief mapping gap. A rare interleaving of slot reuse may still end in a fatal fault report, and the regression test does not guarantee that it exercises the retry path. Resolve or explicitly accept these before merging.
Pre-merge checks:
4
#1229, is closed and marked as historical context only. Its acceptance criteria do not apply to this pull request. No active directly linked target remains, so no linked-issue c…Full details: Title check
Explanation
The title names the main change: guest memory accesses are retried while a mapping is briefly replaced. It also names the GTA V HDD Streamer crash it fixes. The changeset adds transient-unmap windows, a runtime linker fault retry path, and a test for mapping replacement, which matches the title.
Full details: Description check
Explanation
The description states the crash being fixed, the cause (a transient unmap during MAP_FIXED replacement and Windows view splits), the change itself (TransientUnmapWindows, runtime linker retry, 16-retry limit), the differences from
#1229, and the test results. It matches the changeset closely.Full details: Linked Issues check
Explanation
The only linked issue,
#1229, is closed and marked as historical context only. Its acceptance criteria do not apply to this pull request. No active directly linked target remains, so no linked-issue coding requirements apply. The current PR description states that this pull request ports#1229onto main. That port is not a coding requirement for this assessment. The changes since the previous review (commitsbbd7a79f,5013e95b, and165cf609) concern only the window-generation and publication handling in the transient-unmap registry. They do not change the requirement status.Full details: Out of Scope Changes check
Explanation
All changed files support the transient-unmap objective in the current PR description. src/kernel/memory.cpp and src/kernel/memory.h add WaitForTransientUnmap and RetryTransientUnmapFault, which implement the wait and the bounded retry limit with bounded logging. src/kernel/memoryAddressSpace.inc adds the TransientUnmapWindow and TransientUnmapWindows registry. It opens windows for fixed flexible and direct replacements and for non-empty survivor ranges in the Windows UnmapOne path. src/loader/runtimeLinker.cpp retries eligible faults after GPU fault handling. tests/VirtualMemoryAllocationTests.cpp adds AccessDuringMappingReplacement, which covers this behavior. The PR description states that
#1229's protection-snapshot code and duplicate permission test were dropped. The whole-PR summary shows no such code. The later window-reuse and seqlock-style publication changes apply to the same registry, so they remain within scope. This result relies on the whole-PR summary and the commit message in the PR objectives. I did not read the changed code paths directly, so no unrelated change is confirmed absent from the diff itself.Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
6abe2486-eb91-4d62-aceb-cf5955c5787dReviewing files that changed from the base of the PR and between
5013e95b45and165cf6095e.src/kernel/memoryAddressSpace.incIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-18 22:44 UTC · original · edited 2026-11-18 23:27 UTC
On
src/kernel/memoryAddressSpace.incline 89 (outdated):Keep an open replacement window from being overwritten on Windows.
On Windows, one
MAP_FIXEDreplacement of a fragmented flexible mapping can open 16 or more windows.MapFlexibleMemoryopens the outerreplacingwindow first. Each fragment removal inUnmapOnethat leaves a right survivor opens another window. The sixteenth survivor wraps to the outer window's slot, becauseOpenselectss_windows[... % 16]and does not check whether the entry is open. The outer entry's range is then lost while its scope is still open.Page 0 is unmapped until the new mapping is published. No remaining entry covers it, so
Waitreturns 0 and a guest access to that page is reported as a fault. The outerScopedestructor also writesclosed_nsthrough its stored pointer, which closes the reused entry.Openmust not overwrite an entry that an openScopestill owns. Reuse only entries whoseclosed_nsis nonzero, and claim each entry atomically. Define the required behavior when every entry is open; the current code does not define it. Add a regression test that replaces a fragmented mapping with more than 16 survivor windows while a guest access is in progress.✅ Addressed in commit
165cf60@coderabbitai[bot] on GitHub · 2026-11-18 22:44 UTC · original · edited 2026-11-18 23:01 UTC
On
src/loader/runtimeLinker.cppline 638 (outdated):Run GPU fault tracking before the transient-unmap wait.
The wait at line 638 blocks until the mapping replacement closes, up to ten seconds. It runs before
HandleGpuFault. A fault that GPU tracking can resolve therefore waits for the replacement, even though tracking could handle it at once. Move the wait to the point where the retry decision needs its result.Suggested fix
✅ Addressed in commit
bbd7a79@ -2477,0 +2522,4 @@std::atomic<bool> started {false};std::atomic<uint64_t> mismatches {0};std::atomic<uint64_t> accesses {0};std::thread worker([&] {@coderabbitai[bot] on GitHub · 2026-11-18 22:44 UTC · original · edited 2026-11-18 23:00 UTC
Synchronize the worker before starting replacement operations.
If the worker first runs after
stop = true, it performs no accesses.mismatchesremains zero, so this test reports success without exercising concurrent access.Add a worker-start handshake before the replacement loops. Use a controlled mapping gap to ensure at least one access reaches the retry path. Assert that the controlled recovery occurred; an unsynchronized retry-count assertion alone would make the test flaky.
@coderabbitai[bot] on GitHub · 2026-11-18 22:44 UTC · original · reviewed
Actionable comments posted: 3
da1eec47-ea44-4110-b9c2-fb164110ea63Reviewing files that changed from the base of the PR and between
a90be8dd73and7450c00a5b.src/kernel/memory.cppsrc/kernel/memory.hsrc/kernel/memoryAddressSpace.incsrc/loader/runtimeLinker.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@oguzhanozfe on GitHub · 2026-11-18 22:50 UTC · original
On
src/kernel/memoryAddressSpace.incline 89 (outdated):Fixed in
bbd7a79f. Entries are now claimed atomically (owned) and released only by theirScope, so an open range is never overwritten. The table grew to 64 entries; when all are open,Openyields until one frees, since a scope spans a single remap.One correction: a single replacement opens at most two survivor windows, the left part of its first view and the right part of its last one. Views in between are removed whole. So the wrap needed many concurrent replacements, not one fragmented mapping. It is fixed either way.
TransientUnmapWindowsis internal tomemory.cpp, so there is no direct >16-window test.@oguzhanozfe on GitHub · 2026-11-18 22:50 UTC · original
On
src/loader/runtimeLinker.cppline 638 (outdated):Fixed in
bbd7a79f. GPU tracking and the SRT-walker skip now run first, and the transient wait happens only right before the retry decision. A retried access that then hits GPU write/read tracking faults again and is resolved byHandleGpuFault.@ -2477,0 +2522,4 @@std::atomic<bool> started {false};std::atomic<uint64_t> mismatches {0};std::atomic<uint64_t> accesses {0};std::thread worker([&] {@oguzhanozfe on GitHub · 2026-11-18 22:50 UTC · original
Fixed in
bbd7a79f. The replacement loops start only after the worker has accessed both mappings, and the test checksaccesses > 0. Three consecutive runs retried 316, 317 and 313 faults, so the retry path is exercised every time.@coderabbitai[bot] on GitHub · 2026-11-18 22:58 UTC · original · reviewed
fe5ed817-fc52-46ec-a473-0280e7fff743Reviewing files that changed from the base of the PR and between
7450c00a5bandbbd7a79f16.src/kernel/memoryAddressSpace.incsrc/loader/runtimeLinker.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
@ -2477,0 +2522,4 @@std::atomic<bool> started {false};std::atomic<uint64_t> mismatches {0};std::atomic<uint64_t> accesses {0};std::thread worker([&] {@coderabbitai[bot] on GitHub · 2026-11-18 23:00 UTC · original
@oguzhanozfe Thanks. The handshake ensures the worker accesses both mappings before the replacement loops start.
The worker can still pause immediately after setting
startedand resume after both loops finish. The test can then pass with zero retries. Your three runs exercised the retry path, but the test does not guarantee that future runs will do so.Please use a controlled mapping gap and assert that recovery occurs. I am not marking the finding as addressed yet.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-11-18 23:03 UTC · original
On
src/kernel/memoryAddressSpace.incline 89 (outdated):Analysis chain
Script executed:
Length of output: 98865
Script executed:
Length of output: 60466
Script executed:
Length of output: 24691
@oguzhanozfeThanks. I verified thatbbd7a79fclaims entries atomically and releases them when their scopes close.One correction: the two-survivor bound applies to one
GuestBackingStore::Unmapcall, not necessarily to a whole replacement. Name or protection edits can split the guest ranges without splitting the Windows view.ReplaceFixedRangeWithReservedthen unmaps those guest fragments separately. Their sequential survivor windows can wrap the old 16-entry table while the outer window remains open.The existing replacement test does not cover that case. Please add a deterministic Windows regression test that fragments one view into at least 17 guest ranges and accesses the first range after 16 survivor-window openings, before the replacement is mapped. I have not run the test, so I am not marking the finding as addressed yet.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@oguzhanozfe on GitHub · 2026-11-18 23:05 UTC · original
@coderabbitai Re the outside-diff comment Preserve the matched window generation across entry reuse (
memoryAddressSpace.inc): fixed in5013e95b.Waitsnapshots the entry'sidtogether with its range and re-checks theid, so a torn read against a reopening entry is skipped.closed_nsvalue it observed instead of loading it again.idchanges while it waits, it treats the matched window as just closed. Reuse requires the originalScopeto release the entry, and aScopecloses before it releases.TransientUnmapWindowsis internal tomemory.cpp, so there is no direct test that pauses a reader.AccessDuringMappingReplacementstill passes; three runs retried 316–317 faults each.@coderabbitai[bot] on GitHub · 2026-11-18 23:07 UTC · original
Analysis chain
Script executed:
Length of output: 24692
Script executed:
Length of output: 24701
Script executed:
Length of output: 11523
@oguzhanozfeThanks for the update.Waitnow retains the observedclosed_ns, so it no longer performs the second load that prompted the original comment.However, the generation and range snapshot can still be inconsistent. In
src/kernel/memoryAddressSpace.incat Line 125,Openstores the newidbefore the new range. A reader can therefore read window B’sidtwice while still reading window A’s range.Waitcan then wait for an unrelated window and return B’s ID, which controls the retry budget.Please use a publication protocol that lets readers detect an update in progress, and add a controlled interleaving test for entry reuse. The reported stress runs do not force this interleaving; I did not run them independently. I’m not marking the finding as addressed yet.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-11-18 23:11 UTC · original · edited 2026-11-18 23:27 UTC
On
src/kernel/memoryAddressSpace.incline 71 (outdated):Restart the snapshot when the entry generation changes.
If slot 0 is reused between Line 67 and Line 71, this condition skips the replacement window even when it still covers
vaddr.For example,
Wait(x)reads window A's ID and pauses. A closes, 63 unrelated windows open and close, and window B reuses slot 0 for the same address. If B unmaps the address beforeWaitresumes, the changed ID causescontinue. No other entry coversx, soWaitreturns 0 and the exception handler reports a guest fault. The previous implementation would wait for B to close.Restart this entry's snapshot when the ID changes. Keep the captured-generation handling for reuse after a snapshot has passed validation. This preserves the unchanged-generation path and prevents the replacement window from being discarded.
✅ Confirmed as addressed by @oguzhanozfe
@coderabbitai[bot] on GitHub · 2026-11-18 23:11 UTC · original · reviewed
Actionable comments posted: 1
aefb894d-e720-4fa3-b20a-b821fdb0cdddReviewing files that changed from the base of the PR and between
bbd7a79f16and5013e95b45.src/kernel/memoryAddressSpace.incIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.
@oguzhanozfe on GitHub · 2026-11-18 23:26 UTC · original
On
src/kernel/memoryAddressSpace.incline 71 (outdated):Fixed in
165cf609.Opennow publishes an entry as a seqlock. It writesid = 0, then a release fence, then the range, then the newidwithseq_cst.Waitre-reads the entry until theidit read before the range matches theidafter an acquire fence. While an owned entry showsid0, it spins briefly becauseOpenis still writing it.idis never paired with another opening's range. The previous order,idbeforestart/end, allowed that pairing.AccessDuringMappingReplacementpasses (three runs retried 303–318 faults each), and so dopage_manager_testsandmemory_tracker_tests.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.