graphics: every guest unmap drains the whole GPU queue, and 100% of the drains are triggered by a condition unrelated to the unmapped range #1178

Open
opened 2026-11-26 09:46:42 +00:00 by github-bot · 0 comments
Owner

@⁠everyoneexe on GitHub · 2026-10-08 01:58 UTC · original

Summary

RenderContext::UnmapMemory drains the entire GPU queue when a guest unmaps memory. I instrumented the trigger on Ready or Not (PPSA18605) and 100% of the drains came from the one condition that has nothing to do with the range being unmapped — HasPendingPriorityOperations(). The two conditions that actually test whether the GPU owns that memory never fired once.

The drain runs while g_memory_operation_mutex is held, so it stalls every guest thread that touches memory, not just rendering.

Code

src/graphics/host_gpu/renderer/renderContext.cpp:101-116 @ 5a880808:

const auto unmap = [this, vaddr, size] {
    // Check cache ownership on the GPU thread. Guest-memory callbacks can still
    // access a range with no cached data, so they must finish before it is unmapped.
    if (m_command_scheduler.Active() &&
        (m_buffer_cache.IsRegionRegistered(vaddr, size) ||
         m_texture_cache.IsRegionRegistered(vaddr, size) ||
         m_command_scheduler.HasPendingPriorityOperations())) {   // <-- not range-specific
        const auto tick = m_command_scheduler.CurrentTick();
        m_command_scheduler.Finish();
        m_command_scheduler.WaitPriorityOperations(tick);
    }

CommandScheduler::Finish() (commandScheduler.cpp:184-191) is submit + m_master.Wait(CurrentTick() - 1), i.e. a full queue drain with an unbounded host wait.

Lock context: KernelMunmap (kernel/memory.cpp:2621-2625) takes g_memory_operation_mutex and then reaches this through UnmapMemory -> UnmapGpuRange (:92-97) -> SendCommandSync. That mutex is static std::recursive_mutex (memory.cpp:828) and is taken by every memory ABI entry point (memory.cpp:2259: "The ABI entry points and batch operations hold g_memory_operation_mutex").

Measurements

Instrumented build (committed, so the pipeline cache stays enabled), counters reported every 5 s. Linux, RTX 4070 Laptop, i7-13620H, 170 s sessions.

Which condition triggered each drain — second session, totals over the whole run:

TOTAL drains = 38
  buffer cache owns the range : 0   (0%)
  texture cache owns the range: 0   (0%)
  pending priority operation  : 38  (100%)
  >>> priority-op ONLY        : 38  (100%)

Per 5 s window, every drain is PRIO_ONLY:

[UNMAP] calls=11900 drains=4  total=  4.5 ms max= 4.0 ms | buf=0 tex=0 prio=4  PRIO_ONLY=4
[UNMAP] calls=2922  drains=2  total=  3.0 ms max= 1.6 ms | buf=0 tex=0 prio=2  PRIO_ONLY=2
[UNMAP] calls=910   drains=16 total= 98.4 ms max=12.9 ms | buf=0 tex=0 prio=16 PRIO_ONLY=16
[UNMAP] calls=166   drains=13 total= 89.1 ms max=14.0 ms | buf=0 tex=0 prio=13 PRIO_ONLY=13
[UNMAP] calls=9     drains=3  total= 13.2 ms max=12.8 ms | buf=0 tex=0 prio=3  PRIO_ONLY=3

Cost — first session, same build, different phase:

TOTAL: unmaps=16054  drains=126  drain time=3.62 s  longest single drain=998 ms

with these windows:

[UNMAP] calls=467  drains=4  drain_total=1037.2 ms max=997.0 ms   <- 21% of a 5 s window
[UNMAP] calls=41   drains=15 drain_total=1256.1 ms max=998.3 ms   <- 25%
[UNMAP] calls=34   drains=15 drain_total=1065.6 ms max=952.9 ms   <- 21%
[UNMAP] calls=6983 drains=1  drain_total=   1.1 ms                <- harmless

So the drain is rare (126 of 16054 unmaps, 0.8%) but individual drains reach ~1 s, and they land in bursts. Cost varies a lot by phase — 3.62 s in one session, 0.21 s in another — but the trigger split was 100% PRIO_ONLY in both.

Why this matters

A ~1 s drain under g_memory_operation_mutex blocks every guest thread that calls any memory syscall for that whole second. In this title that includes the audio feeder, so it is audible as a dropout, not just a frame-time spike.

It also makes the frame counter freeze: Presenter::Impl::Present needs renderer.GetMutex(), and the CP thread is parked inside the drain.

Suggested direction

The comment explains the intent:

Guest-memory callbacks can still access a range with no cached data, so they must finish before it is unmapped.

That is a real hazard, but HasPendingPriorityOperations() answers "is anything queued?" rather than "could anything queued touch this range?". Given the measurement, every drain in these sessions was for a range the GPU demonstrably did not own.

Two options, in increasing effort:

  1. Record the guest range (or a coarse bucket) alongside each deferred priority operation and only drain when a pending operation overlaps the range being unmapped.
  2. If per-operation ranges are impractical, at least order the test so the cheap range checks short-circuit, and treat HasPendingPriorityOperations() as a drain reason only for operations enqueued without a known range.

I have not attempted a fix — the correct scoping of a priority operation's range is maintainer territory, and I did not want to guess at the invariant the comment is protecting.

Reproduction

  1. Boot any title that unmaps frequently during load (Ready or Not PPSA18605 here; 16k unmaps in ~65 s of sampling).
  2. Add counters around the branch at renderContext.cpp:104, splitting IsRegionRegistered (buffer), IsRegionRegistered (texture) and HasPendingPriorityOperations, plus a timer around Finish() + WaitPriorityOperations.
  3. Report every 5 s from RunGarbageCollector.

Happy to share the instrumentation diff, the raw logs, or to test a patch on Linux/NVIDIA.

Environment

  • KytyPS5 5a880808, built from source, Release, clang 21 + lld, Linux (CachyOS, kernel 7.2.2)
  • RTX 4070 Laptop (proprietary driver), Intel i7-13620H, 15.7 GB RAM
  • Ready or Not PPSA18605 v01.001.008
**@⁠everyoneexe** on GitHub · 2026-10-08 01:58 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1178) ### Summary `RenderContext::UnmapMemory` drains the entire GPU queue when a guest unmaps memory. I instrumented the trigger on Ready or Not (PPSA18605) and **100% of the drains came from the one condition that has nothing to do with the range being unmapped** — `HasPendingPriorityOperations()`. The two conditions that actually test whether the GPU owns that memory never fired once. The drain runs while `g_memory_operation_mutex` is held, so it stalls every guest thread that touches memory, not just rendering. ### Code `src/graphics/host_gpu/renderer/renderContext.cpp:101-116` @ `5a880808`: ```cpp const auto unmap = [this, vaddr, size] { // Check cache ownership on the GPU thread. Guest-memory callbacks can still // access a range with no cached data, so they must finish before it is unmapped. if (m_command_scheduler.Active() && (m_buffer_cache.IsRegionRegistered(vaddr, size) || m_texture_cache.IsRegionRegistered(vaddr, size) || m_command_scheduler.HasPendingPriorityOperations())) { // <-- not range-specific const auto tick = m_command_scheduler.CurrentTick(); m_command_scheduler.Finish(); m_command_scheduler.WaitPriorityOperations(tick); } ``` `CommandScheduler::Finish()` (`commandScheduler.cpp:184-191`) is submit + `m_master.Wait(CurrentTick() - 1)`, i.e. a full queue drain with an unbounded host wait. Lock context: `KernelMunmap` (`kernel/memory.cpp:2621-2625`) takes `g_memory_operation_mutex` and then reaches this through `UnmapMemory` -> `UnmapGpuRange` (`:92-97`) -> `SendCommandSync`. That mutex is `static std::recursive_mutex` (`memory.cpp:828`) and is taken by every memory ABI entry point (`memory.cpp:2259`: *"The ABI entry points and batch operations hold g_memory_operation_mutex"*). ### Measurements Instrumented build (committed, so the pipeline cache stays enabled), counters reported every 5 s. Linux, RTX 4070 Laptop, i7-13620H, 170 s sessions. **Which condition triggered each drain** — second session, totals over the whole run: ``` TOTAL drains = 38 buffer cache owns the range : 0 (0%) texture cache owns the range: 0 (0%) pending priority operation : 38 (100%) >>> priority-op ONLY : 38 (100%) ``` Per 5 s window, every drain is `PRIO_ONLY`: ``` [UNMAP] calls=11900 drains=4 total= 4.5 ms max= 4.0 ms | buf=0 tex=0 prio=4 PRIO_ONLY=4 [UNMAP] calls=2922 drains=2 total= 3.0 ms max= 1.6 ms | buf=0 tex=0 prio=2 PRIO_ONLY=2 [UNMAP] calls=910 drains=16 total= 98.4 ms max=12.9 ms | buf=0 tex=0 prio=16 PRIO_ONLY=16 [UNMAP] calls=166 drains=13 total= 89.1 ms max=14.0 ms | buf=0 tex=0 prio=13 PRIO_ONLY=13 [UNMAP] calls=9 drains=3 total= 13.2 ms max=12.8 ms | buf=0 tex=0 prio=3 PRIO_ONLY=3 ``` **Cost** — first session, same build, different phase: ``` TOTAL: unmaps=16054 drains=126 drain time=3.62 s longest single drain=998 ms ``` with these windows: ``` [UNMAP] calls=467 drains=4 drain_total=1037.2 ms max=997.0 ms <- 21% of a 5 s window [UNMAP] calls=41 drains=15 drain_total=1256.1 ms max=998.3 ms <- 25% [UNMAP] calls=34 drains=15 drain_total=1065.6 ms max=952.9 ms <- 21% [UNMAP] calls=6983 drains=1 drain_total= 1.1 ms <- harmless ``` So the drain is rare (126 of 16054 unmaps, 0.8%) but individual drains reach ~1 s, and they land in bursts. Cost varies a lot by phase — 3.62 s in one session, 0.21 s in another — but the trigger split was 100% `PRIO_ONLY` in both. ### Why this matters A ~1 s drain under `g_memory_operation_mutex` blocks every guest thread that calls any memory syscall for that whole second. In this title that includes the audio feeder, so it is audible as a dropout, not just a frame-time spike. It also makes the frame counter freeze: `Presenter::Impl::Present` needs `renderer.GetMutex()`, and the CP thread is parked inside the drain. ### Suggested direction The comment explains the intent: > Guest-memory callbacks can still access a range with no cached data, so they must finish before it is unmapped. That is a real hazard, but `HasPendingPriorityOperations()` answers *"is anything queued?"* rather than *"could anything queued touch this range?"*. Given the measurement, every drain in these sessions was for a range the GPU demonstrably did not own. Two options, in increasing effort: 1. Record the guest range (or a coarse bucket) alongside each deferred priority operation and only drain when a pending operation overlaps the range being unmapped. 2. If per-operation ranges are impractical, at least order the test so the cheap range checks short-circuit, and treat `HasPendingPriorityOperations()` as a drain reason only for operations enqueued without a known range. I have not attempted a fix — the correct scoping of a priority operation's range is maintainer territory, and I did not want to guess at the invariant the comment is protecting. ### Reproduction 1. Boot any title that unmaps frequently during load (Ready or Not PPSA18605 here; 16k unmaps in ~65 s of sampling). 2. Add counters around the branch at `renderContext.cpp:104`, splitting `IsRegionRegistered` (buffer), `IsRegionRegistered` (texture) and `HasPendingPriorityOperations`, plus a timer around `Finish()` + `WaitPriorityOperations`. 3. Report every 5 s from `RunGarbageCollector`. Happy to share the instrumentation diff, the raw logs, or to test a patch on Linux/NVIDIA. ### Environment - KytyPS5 `5a880808`, built from source, Release, clang 21 + lld, Linux (CachyOS, kernel 7.2.2) - RTX 4070 Laptop (proprietary driver), Intel i7-13620H, 15.7 GB RAM - Ready or Not PPSA18605 v01.001.008
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:42 +00:00
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1178
No description provided.