graphics: take the GPU's pages for the CPU in one critical section #1412
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!1412
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1412"
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?
@Franseven on GitHub · 2026-10-11 08:52 UTC · original
Franseven:pr/dirty-ownership→main· +32 −2 · 3 files · 1 commits · openGame
Marvel's Wolverine PPSA03671.
Problem
BufferCache::ReadMemoryreleases the GPU's ownership of a page and claims it for the CPU as twoseparate calls:
Each call takes the region lock on its own. In between them, a
SynchronizeBufferon anotherthread — an upload from the renderer thread — can mark the same page GPU-dirty, and
RegionManagerthen ends the process on its own invariant:
regionManager.h:106. That is anEXIT, not an assert: a page that a command still in flight namesends the session, from a fault path the guest cannot avoid — it is the guest's own read that
triggered the fault.
The same window is reachable without a download. When
DownloadBufferMemoryreturns false thewrite branch still runs, and it marks the page CPU-dirty on top of the GPU's untouched claim; the
release never happened at all. That is the path the crash was first reproduced on.
Change
One call that does both halves under one lock:
and the caller uses it whenever the guest is about to write:
Three notes:
Iteratewalks tracker regions page bypage, so a claim narrower than a page is not expressible; this is strictly more than the two calls
did, and costs only redundant uploads.
enabledirection asserts.ChangeStatechecks the other source's bits only whensetting them, so clearing an already-clean range is not an error; the release-then-claim order
above is what keeps the CPU claim from tripping its own invariant.
MarkRegionAsCpuModifiedhad exactly one caller — thisone — and it now goes through the take. The only other
UnmarkRegionAsGpuModifiedcaller is thebuffer-cache retire path, which releases the pages as the buffer is deleted and makes no CPU
claim.
Testing
Built with clang-cl 23.1.3 (Release) on Windows; the emulator links and
memory_tracker_testsandpage_manager_testspass.The same change has been running in a downstream tree, on a much larger
ReadMemoryStepthatcarries the same two-call sequence: there Wolverine ran 3933 frames past the point where it
previously ended around 2000, with
EXIT("CPU dirty state conflicts with GPU dirty state")in thelog. That tree sits on a different base, so what this PR adds is the change against this tree's own
ReadMemory, which is where the pair is simplest to see.BufferCache::ReadMemory released the GPU's ownership of a page and claimed it for the CPU as two calls, each taking the region lock on its own. Between them a SynchronizeBuffer on another thread can mark the same page GPU-dirty, and RegionManager then ends the process on its own invariant: EXIT("CPU dirty state conflicts with GPU dirty state\n"); // regionManager.h:106 That is an EXIT, not an assert, and it runs on a fault path the guest cannot avoid: it is the guest's own read that faulted. The same window is reachable without a download -- when DownloadBufferMemory returns false the write branch still marks the page CPU-dirty over the GPU's untouched claim. Add MemoryTracker::TakeGpuPagesForCpu, which clears the GPU bits and sets the CPU bits for the range under one lock, and use it wherever the guest is about to write. The claim covers whole pages, which is strictly more than the pair of calls did and costs only redundant uploads; clearing an already-clean range does not assert, so a write to a page the GPU never owned behaves as before.@coderabbitai[bot] on GitHub · 2026-10-11 08:55 UTC · original
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
0aaea92c-572f-4643-91aa-bc34fc7b32dd📥 Commits
Reviewing files that changed from the base of the PR and between
6f0cddd440and1a65d2638a.📒 Files selected for processing (3)
src/graphics/host_gpu/memoryTracker.cppsrc/graphics/host_gpu/memoryTracker.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
📝 Walkthrough
Walkthrough
MemoryTracker adds
TakeGpuPagesForCputo clear GPU-modified state and mark the requested bytes CPU-modified under region locks. BufferCache uses this operation for writes inReadMemory, including when no download is needed. For reads, it unmarks GPU-modified pages after a successful download.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
1a65dNo concrete merge-blocking issue is established. The change is mergeable after normal checks.
Pre-merge checks |
4 |
1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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
Comment
@coderabbitai helpto get the list of available commands.@theantipopau on GitHub · 2026-10-12 11:11 UTC · original
Read the diff against main. The fix is sound and the claim matches the code:
ChangeState<Cpu, true>hard-EXITs when any page in the range is still GPU-dirty, so the two-stepUnmarkRegionAsGpuModified→MarkRegionAsCpuModifiedinReadMemory's write path did have a race window against a concurrentMarkRegionAsGpuModified(SynchronizeBuffer/upload): release, then re-mark, then the CPU claim walks in between and kills the process.TakeGpuPagesForCpudoes both halves under the samemanager->lock, which removes the window deterministically.UnmarkRegionAsGpuModified(no CPU claim happens there, so nothing to make atomic), and skipping it on writes is right sinceTakeGpuPagesForCpureleases the same range under its own lock — double release would just be a redundant second lock acquisition.MarkRegionAsCpuModifiedalso iterated whole pages (GetPageRange), so there's no semantic widening beyond redundant uploads.One note: the changelog-worthy behavior change is that a guest write now releases GPU dirty state even when
DownloadBufferMemoryhad nothing to download — previously the page stayed GPU-dirty under the new CPU-dirty mark, which was exactly the inconsistent state that could never be reached before EXIT'ing. So this is strictly better on that path.Holding off on a formal approve only because CI on this branch is sitting in
action_required(maintainer-approval-gated workflow runs) rather than verified green — no objection to whoever can see the result approving it.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.