Probabilistic boot hang: UnmapMemory waits for a full GPU drain while holding the memory-operation mutex (regression from #135) #149

Closed
github-bot wants to merge 1 commit from gh-pr/149/head into gh-pr/149/base
Owner

@⁠Stepz97 on GitHub · 2026-08-01 15:40 UTC · original
Stepz97:fix-unmap-gpu-drain-deadlock → main · +36 −0 · 2 files · 1 commits · closed without merging

Symptom

Roughly one in three boots (measured on a commercial title on macOS, but the mechanism is platform-independent) freezes at a wandering point: the process stays alive at low CPU, the log stops advancing, no crash. Sampling the live process shows a three-way stall:

  1. A guest thread inside a memory syscall (holding the global memory-operation mutex) reached GpuResourceManager::UnmapMemory, which takes Gpu::SubmissionLock (PauseSubmissions: grabs the submission mutex) and then waits for a full GPU drain (WaitLocked).
  2. The GPU thread retries a blocked submission every 100 ms: its PM4 stream sits in WaitRegMem64 on a label that a guest thread is supposed to write.
  3. The label writers never run: one was caught blocked in GpuState::Submit on the submission mutex held by the pauser, the rest block on the memory-operation mutex held by thread 1.

Since the memory rework (#135), MapGpuRange/UnmapGpuRange run inside the memory paths that hold the global mutex, so any unmap pays the pause + full-drain price there — and GPU progress can depend on guest threads that need that same mutex. The specific caller caught in the sample was AllocateGuestRuntimeMemory consuming a reserved span: a range that had never been GPU-mapped at all, paying a full GPU drain as a defensive no-op.

Fix

If the range has no intersection with the GPU-mapped set (m_mapped_ranges), take the same no-pause local path the pre-renderer branch (m_gpu == nullptr) already uses. RangeSet grows an Intersects query — intersection, not containment, so partially mapped ranges still go through the full drain. The short path also verifies the "never mapped => nothing cached" invariant against both caches (BufferCache::HasPageOverlap, TextureCache::QueryRegion().image_pages) and fails loud on a violation instead of silently re-creating the cross-thread wait.

This narrows the window rather than restructuring the locking: unmaps of ranges that really are GPU-mapped still drain as before. A follow-up could revisit whether the full drain under the memory mutex is avoidable in general.

Validation

  • 13 consecutive boots of the affected title with no stall (previous reproduction rate ~1 in 3), the defensive check never firing.
  • Full test suite unchanged; Mandelbrot homebrew still byte-identical to its reference.
  • Applies and builds cleanly on a65d17a. Independent of the relocation-livelock fix submitted separately; boot-level validation on macOS ran with both applied (the livelock otherwise blocks boot there).

Disclosure (README § AI Use): this fix was developed with AI assistance. I reviewed the analysis and the code, ran the validation on my machine, and take responsibility for this submission.

**@⁠Stepz97** on GitHub · 2026-08-01 15:40 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/149) `Stepz97:fix-unmap-gpu-drain-deadlock` → `main` · +36 −0 · 2 files · 1 commits · closed without merging ## Symptom Roughly one in three boots (measured on a commercial title on macOS, but the mechanism is platform-independent) freezes at a wandering point: the process stays alive at low CPU, the log stops advancing, no crash. Sampling the live process shows a three-way stall: 1. A guest thread inside a memory syscall (holding the global memory-operation mutex) reached `GpuResourceManager::UnmapMemory`, which takes `Gpu::SubmissionLock` (`PauseSubmissions`: grabs the submission mutex) and then waits for a **full GPU drain** (`WaitLocked`). 2. The GPU thread retries a blocked submission every 100 ms: its PM4 stream sits in `WaitRegMem64` on a label that a guest thread is supposed to write. 3. The label writers never run: one was caught blocked in `GpuState::Submit` on the submission mutex held by the pauser, the rest block on the memory-operation mutex held by thread 1. Since the memory rework (#135), `MapGpuRange`/`UnmapGpuRange` run inside the memory paths that hold the global mutex, so any unmap pays the pause + full-drain price there — and GPU progress can depend on guest threads that need that same mutex. The specific caller caught in the sample was `AllocateGuestRuntimeMemory` consuming a reserved span: a range that had never been GPU-mapped at all, paying a full GPU drain as a defensive no-op. ## Fix If the range has no intersection with the GPU-mapped set (`m_mapped_ranges`), take the same no-pause local path the pre-renderer branch (`m_gpu == nullptr`) already uses. `RangeSet` grows an `Intersects` query — intersection, not containment, so partially mapped ranges still go through the full drain. The short path also verifies the "never mapped => nothing cached" invariant against both caches (`BufferCache::HasPageOverlap`, `TextureCache::QueryRegion().image_pages`) and fails loud on a violation instead of silently re-creating the cross-thread wait. This narrows the window rather than restructuring the locking: unmaps of ranges that really are GPU-mapped still drain as before. A follow-up could revisit whether the full drain under the memory mutex is avoidable in general. ## Validation - 13 consecutive boots of the affected title with no stall (previous reproduction rate ~1 in 3), the defensive check never firing. - Full test suite unchanged; Mandelbrot homebrew still byte-identical to its reference. - Applies and builds cleanly on `a65d17a`. Independent of the relocation-livelock fix submitted separately; boot-level validation on macOS ran with both applied (the livelock otherwise blocks boot there). --- *Disclosure (README § AI Use): this fix was developed with AI assistance. I reviewed the analysis and the code, ran the validation on my machine, and take responsibility for this submission.*
The guest memory rework calls UnmapGpuRange from memory paths that run
under the global memory-operation mutex, and GpuResourceManager::UnmapMemory
unconditionally paused submissions and waited for a full GPU drain. That
wait can never finish when the GPU is itself waiting on a WaitRegMem label
whose guest writer is blocked on the same memory mutex (or on the
submission mutex the pause holds) - a probabilistic three-way stall that
froze roughly one in three boots at wandering points.

Ranges that never went through MapMemory (e.g. reserved spans consumed
during allocation, the caller caught in the stalled sample) have nothing
cached the GPU could reference, so take the same no-pause path the
pre-renderer branch already uses. RangeSet grows an Intersects query -
intersection, not containment, so partially mapped ranges still drain -
and the short path verifies the "never mapped => nothing cached" invariant
against both caches and fails loud on a violation instead of silently
re-creating the cross-thread wait.

Validated with 13 consecutive Astro Bot boots without a stall (previous
reproduction rate ~1 in 3), the defensive check never firing, and the
full test gate green.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:12:26 +00:00
github-bot closed this pull request 2026-11-26 09:12:27 +00:00
Author
Owner

@⁠Nmzik on GitHub · 2026-08-02 11:34 UTC · original

@⁠Stepz97 This should be fixed by github.com/KytyPS5/KytyPS5@fc8d2a3b83. Please retest.

**@⁠Nmzik** on GitHub · 2026-08-02 11:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/149#issuecomment-5157559574) @⁠Stepz97 This should be fixed by https://github.com/KytyPS5/KytyPS5/commit/fc8d2a3b834d53566522503169fc096ec233ca59. Please retest.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.