Skip to content

GPU: free memory is not the runtime's to watch (fixes a world-load deadlock under Wine) - #1849

Open
crashbashash wants to merge 1 commit into
Marvisak:mainfrom
crashbashash:fix/watch-free-memory
Open

crashbashash wants to merge 1 commit into
Marvisak:mainfrom
crashbashash:fix/watch-free-memory

Conversation

@crashbashash

Copy link
Copy Markdown

Under Wine/Proton, main at 70564ae stops at the end of loading or just after entering the world: no frames, no audio, the title idle, no GPU fault. Six of six runs here (RX 6800 XT, RADV, GE-Proton11-14). This is the cause and a one-condition fix.

What happens

  1. shadow::loose() makes a 4 MiB window copy for an address outside every known buffer. For a null address that window is 0x0..0x400000, and Shadow::new asks watch::track(0, 4 MiB, true) to track it.
  2. runtime_memory(0, true) accepts it: GetWriteWatch fails, and the view test is "anything but committed private memory", which free memory (MEM_FREE) passes.
  3. The window's blocks get guarded. On Windows there is usually nothing committed below 4 MiB, so the VirtualProtect calls just fail (I haven't checked on Windows). Under Wine the process heap lives there (here, heap writes faulted on guarded blocks from 0x140000 to 0x260000), and its pages become read-only.
  4. From then on, any heap write faults into on_write. When the faulting thread already holds GUARDS (it allocates inside Tracked::take, writing(), or on_write itself), on_write waits for the lock its own thread holds, forever. Audio (apu::XApuEnqueueCommand), GPU submission (Host::execute → Tracked::take) and the title's workers then queue behind it.

Evidence

  • Stacks taken from outside the process (gdb; in-process dumpers hang on this lock), unwound on the Windows side and symbolized with the build's PDB: a title thread in xmem::map_alias → watch::title_memory_remapped faulting into on_write → Mutex::lock_contended; others waiting in on_write (audio) and Tracked::take (GPU submission).
  • A local diagnostic build that detects a write fault on a thread holding GUARDS logged faults at 0x1400d8 (RtlEnterCriticalSection on the process heap's lock) and 0x1de770 (ucrtbase memcpy into the heap), with the lock taken in Tracked::take, writing() and on_write. With those faults let through, the title ran 5 minutes in the world.
  • Logged at track(): 0x0+0x400000 from shadow.rs:205, VirtualQuery state 0x10000 (MEM_FREE).

The change

runtime_memory also requires State != MEM_FREE. Placeholders and views, which it is meant to accept, are reserved or mapped, never free. New test free_memory_is_not_tracked (track(0, 4 MiB, true) is None): it fails without the change and passes with it. All 7 watch tests pass under Wine.

In the title

70564ae + the watchdog hook (#454) + three of #994's Proton commits (4828716, e5a8c2f, d469fe9), shaders and pipelines precached with translate --cache --pipelines, RX 6800 XT, RADV / Mesa 26.2.6, GE-Proton11-14:

runs reached the world stalled
without this change 6 1 6 (five at the end of loading, one seconds after entering)
with this change 3 3 0 (5 min in the world each: two stopped by me, one ended in the crash below)

With it, the opening renders: sky, skyline, characters and credits, 2,500–3,600 draws per frame, 4.8–6.2 s per frame. One of the three runs ended at 5 minutes in an access violation in title code (+0x35a4245, reading -1, no VIctory frames on the stack); the other two didn't, so I take it to be unrelated.

Not tested on Windows. The change only turns away free memory, so there it should only mean fewer VirtualProtect calls that fail anyway.

AI-assisted (Claude Code). No game data attached.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Free memory is not the runtime's to watch (fixes a world-load…): stops the runtime treating free pages as its own — likely a crash/OOM fix on world load. Windows-only.

@astrosteveo

Copy link
Copy Markdown

Tested on an RTX 5080, Wine 11.22, on 940d32f plus open PRs. free_memory_is_not_tracked fails without the change and passes with it, in MinGW and MSVC builds.

I forced the hang by allocating plain memory at 0x200000 after track(0, 4 MiB): it became read-only, and a write with the guard lock held never returned. With the change it stays writable.

On this Wine the process heap was above 4 GiB in 13 of 13 probes, so the null window wouldn't reach it here.

A gap that remains: runtime_memory checks only the first page, so private committed memory later in a range that starts in a reservation is still guarded (shown with a synthetic test; I don't know if the title hits it). Not tested: Windows, the title. AI-assisted (Claude).

crashbashash added a commit to crashbashash/VIctory-proton that referenced this pull request Nov 25, 2026
runtime_memory looked at the first page only, so a range starting in a
reservation with plain committed memory further in was still guarded,
and a write there blocked whoever allocated it (as in free memory).
Each region of the range is checked now (VirtualQuery walk); a range
with any free or plain committed region is not tracked and is copied
whole instead. track runs when a copy is made, not per submission.

Reported by astrosteveo on Marvisak#1849.
@coil111

coil111 commented Nov 25, 2026

Copy link
Copy Markdown

Tested on Linux/Proton with an AMD RDNA4 card: without this PR, current main freezes at the end of loading in the same way the description says; with it, the title reaches the world and keeps running.

Setup: RX 9070 XT (RDNA4), Mesa RADV, Gentoo, kernel 7.3.4, 64 GB RAM. Proton Experimental experimental-11.0-20261112 (its Wine; vkd3d-proton 2576c0b4a; DXVK dxgi). Diagnostics build of main 940d32f, cross-built with cargo-xwin (x86_64-pc-windows-msvc). Title 1.0.2.1, Series X, pipelines prebuilt with translate --cache --pipelines.

What the freeze looked like here (940d32f alone):

  • The log stops mid-load, with no error, no exception and no GPU fault. The kernel log shows no amdgpu reset either.
  • The process is still alive: ~22 GB resident, 96 threads, all of them idle (no CPU time over several seconds).
  • The window title (frame rate and draw count) stopped updating at the same moment the log did.
  • winedbg could not attach to the process (error 87), so I have no stacks of my own. The symptoms match the description, but I have not shown it is the same lock.

With this pr the 7 watch tests pass under this Wine (cargo xwin test -p victory-gpu -- watch, Wine as the runner), including the new free_memory_is_not_tracked. In the title, the run went from loading into the world without stopping.

crashbashash added a commit to crashbashash/VIctory-proton that referenced this pull request Nov 25, 2026
runtime_memory looked at the first page only, so a range starting in a
reservation with plain committed memory further in was still guarded,
and a write there blocked whoever allocated it (as in free memory).
Each region of the range is checked now (VirtualQuery walk); a range
with any free or plain committed region is not tracked and is copied
whole instead. track runs when a copy is made, not per submission.

Reported by astrosteveo on Marvisak#1849.
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
A title buffer object's copy is, with VICTORY_IN_PLACE=1, the title's
memory too: placed from the 64 KiB boundary at or below the buffer
(views find their bytes by address), past its end by 256 bytes where the
allocation has them. A placed buffer's end only needs to be committed
pages, not 64 KiB. A remap drops in-place title buffers over it as well.

Buffers over many separately mapped 64 KiB views (MEM_MAPPED, one
allocation each) are still copied: one heap cannot span them, and tile
mappings into heaps opened over process memory were not reliable under
vkd3d-proton here.

Measured (RTX 5080, proton-cachyos 11.0, the title loading its world,
with Marvisak#259, Marvisak#177 and Marvisak#1849 on top for NVIDIA under Wine): 3,335 title
buffers and 2,117 loose windows in place, none refused, no device
errors; 8 large buffers (378 MB) over separate views still copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

runtime_pages in crates/victory-gpu/src/watch.rs no longer counts MEM_FREE regions as the runtime's GPU memory, so free parts of a tracked range (for example the 4 MiB window around a null address) are never guarded. I checked this against origin/main. Since 280b150, the region walk counts free memory as "ours". Under Wine the process heap is later committed there and gets guarded, and a heap write made while holding the guard lock deadlocks. The change only moves free pages into the untracked set, which Shadow::update_free copies whole each use, so correctness on Windows is unchanged. Reserved memory and placeholders are still tracked. The new test free_memory_is_not_tracked reserves and releases a block and fails without the change. Several independent Wine/Proton runs in the thread (NVIDIA and AMD) report that the freeze at world entry is gone.

One thing to keep an eye on: free pages inside a tracked range are now copied whole (as failing reads) on every use instead of counted as zero pages, so a range with large free parts may cost a bit more per frame.

CI: passing.


Generated by Claude Code

@crashbashash

Copy link
Copy Markdown
Author

Thanks for testing this on NVIDIA, and for the gap.

You're right: runtime_memory looks only at the first page, so a range that starts in a reservation with plain committed memory further in is still guarded. That's older than this PR, so I've kept the fix separate to leave this one as reviewed: #2345 checks every region of the range, with your case as a test (a reserved block plus a committed one is refused; the reserved block alone is still tracked).

I don't know whether the title hits it either; in our runs the change made no measurable difference.

AI-assisted (Claude Code). No game data attached.

@Impre-visible

Copy link
Copy Markdown

Test report, as asked in #1921: five runs with this PR on the setup where main froze at world entry.

Setup: Linux (Arch, kernel 6.18 LTS), GTX 1660 6 GB, NVIDIA 620.73.04 (open modules), Wine from GE-Proton11-6 with vkd3d-proton 2576c0b. Tree: main as fetched this morning (after 86a5ed6) plus a few other open PRs (#1722, #1769, #1583's branch, #1523, #454) and this PR at de95e51. VICTORY_NO_WATCHDOG=1, 15-minute limit per run, one run at a time.

runs that reached the world froze at world entry Xid in the kernel log
same tree without this PR 3 3 0
with this PR 5 0 0

All five runs with the PR ran to the 15-minute limit in the world. Two started from warm shader and pipeline caches, three from empty ones. Per run: 788 to 855 compute pipelines made, last frame 2,834 to 3,002, frame time in the world about 1.3 s once loading settles (one run 2.4 s at the cut), 0.3 to 24 s during loading.

The freeze without the PR was the one described in #1921: no log line and no present any more, sync_heaps stuck inside Viewed::set with the other threads behind SHADER_HEAPS.

Not part of this PR, but for the record on this card with that tree: 153 compute pipelines refused per run (wave64 on a 32-lane host) and 149 to 195 no dynamic slots for its tables drops per run (#1548 is not in it).

Not tested: Windows, AMD, anything past the first playable scene (the runs only press Enter).

🤖 Generated with Claude Code

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
…ain's per-region runtime_pages

# Conflicts:
#	crates/victory-gpu/src/watch.rs
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
The previous main (old upstream base) is the first parent; the second is
upstream main with the current heads of our pull requests merged in:
Marvisak#2143, Marvisak#1849 (carried onto main's per-region runtime_pages), Marvisak#259, Marvisak#491,
Marvisak#2153, Marvisak#2099, Marvisak#2071 (main covers it), Marvisak#2162, Marvisak#2218, Marvisak#2088, Marvisak#2389, Marvisak#177's
element-type key on main's three handle maps, and the README. Marvisak#2112
(opt-in, in place) stays in the renderer branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A loose copy of title memory for a null address covers 0..4 MiB, and
`runtime_pages` takes free memory for the runtime's (its `shortcut:`),
so free parts of the window are tracked and guarded once something is
committed there. On Windows nothing is. Under Wine the process heap is:
its pages became read-only, and a heap write by a thread holding the
guard lock (in `Tracked::take`, `writing` or the fault handler itself)
faulted into the handler, which waited for that lock forever: the title
stopped at the end of loading.

The region check now leaves free memory out, as it leaves out plain
committed memory and images; reserved memory still counts (the
shortcut keeps that part).

Test: a block reserved and released (free memory at a known address)
is not tracked. It fails without the change; it only counts if the
block is still free afterwards, since other tests allocate meanwhile.
@crashbashash

Copy link
Copy Markdown
Author

Rebased onto main (67e8ae1). Since 280b150 checks every region of a tracked range, this PR is down to one condition in that check: free memory is not the runtime's. 280b150 still counts free memory as the runtime's (its shortcut: note says so). Under Wine that's the deadlock this PR was opened for: Wine's process heap gets committed where the range was free, and the heap's writes fault into the guard handler while a thread holds the guard lock.

The test changed with it. The old one tracked the window of a null address, but with the region walk that window is no longer all free under Wine: Wine maps read-only locale files there, which count as views. The new test reserves a block and releases it, so it has free memory at a known address, and checks that it isn't tracked. It fails without the change.

Tests: victory-gpu under Wine (GE-Proton11-14 with vkd3d-proton, RX 6800 XT): 193 passed, 3 failed. The same 3 fail on main there: a_texture_written_in_one_format_reads_in_another_through_a_placed_view (castable formats, #2101) and the two pipeline::library blob tests. cargo clippy is clean with and without --all-features.

@Impre-visible, thanks for the five runs on the GTX 1660. That's the clearest evidence this PR has had.

AI-assisted (Claude Code). No game data attached.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
tippythetaidum added a commit to tippythetaidum/VIppy that referenced this pull request Nov 25, 2026
…r the structure builder started)

Two runs with VICTORY_BVH_BUILDER=1 froze 3 and 18 s after the builder
started: CPU time stopped, the card idle, no device loss. Thread dumps:
threads waiting in watch::install::on_write for the guard lock (the
submission thread, the title's main thread, a pipeline worker, and a
D3D12 background thread inside the driver's shader compiler), and others
waiting for the process heap's lock (audio, XInput). The faulting write
on one of them was at 0x5a2e30, inside the process heap (0x590000).

is_view took a range with free memory in it for a view and noted its
blocks absent, to be guarded once mapped; whatever the process allocated
there later was guarded with it, the heap too. The heap's own code then
faulted while holding its lock, the fault waited for the guard lock, and
the guard lock's holder for the heap. Free memory now is no view and is
not noted absent (placeholders still are), as Marvisak/VIctory#1849 found
under Wine. New test free_memory_is_not_tracked fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
networkchannel pushed a commit to Leucodermique/VIce that referenced this pull request Nov 25, 2026
…r the structure builder started)

Two runs with VICTORY_BVH_BUILDER=1 froze 3 and 18 s after the builder
started: CPU time stopped, the card idle, no device loss. Thread dumps:
threads waiting in watch::install::on_write for the guard lock (the
submission thread, the title's main thread, a pipeline worker, and a
D3D12 background thread inside the driver's shader compiler), and others
waiting for the process heap's lock (audio, XInput). The faulting write
on one of them was at 0x5a2e30, inside the process heap (0x590000).

is_view took a range with free memory in it for a view and noted its
blocks absent, to be guarded once mapped; whatever the process allocated
there later was guarded with it, the heap too. The heap's own code then
faulted while holding its lock, the fault waited for the guard lock, and
the guard lock's holder for the heap. Free memory now is no view and is
not noted absent (placeholders still are), as Marvisak/VIctory#1849 found
under Wine. New test free_memory_is_not_tracked fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…s to watch (fixes a world-load deadlock under Wine)
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
What this branch does about Wine: our run-time shaders as DXIL, Marvisak#1849,
Marvisak#2249 and Marvisak#2723 merged, lower.rs's larger limit, why Marvisak#1010 stays, and
the Wine PRs not taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified 4f6bacef7f53e781bf0a48eca8cd223402092d76 merged cleanly onto 63bcca43 on Windows 11 with both an RTX 5090 (VICTORY_D3D12_DEBUG=1) and WARP (--features diagnostics, VICTORY_D3D12_DEBUG=1).

Code review

  • Adding info.State != MEM_FREE in runtime_pages (crates/victory-gpu/src/watch.rs:333-368, condition at watch.rs:361) cleanly excludes free address space from being treated as runtime GPU memory while keeping write-watched pools (watched(...)), MEM_RESERVE reservations/placeholders, and MEM_COMMIT | MEM_MAPPED views tracked, and keeping MEM_IMAGE and plain MEM_COMMIT | MEM_PRIVATE excluded.
  • Because any 64 KiB page touching a MEM_FREE region is added to other and removed by runtime.difference(&other) (watch.rs:364-367), a range straddling a reservation and adjacent free space leaves the free page untracked and never registers it in g.protected, preventing future allocations into that free region from faulting into on_write while GUARDS is held.

Verification

  • cargo xtask policy: 191 files ok.
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --all-features): clean.
  • RTX 5090 + VICTORY_D3D12_DEBUG=1: watch:: 16 passed, 0 failed (including free_memory_is_not_tracked at watch.rs:1049).
  • WARP + --features diagnostics + VICTORY_D3D12_DEBUG=1: watch:: 16 passed, 0 failed.
  • Additional probe on both RTX 5090 and WARP verifying: (1) a 128 KiB MEM_FREE range returns None from track_copy, (2) a mixed [MEM_RESERVE 64 KiB | MEM_FREE 64 KiB] range tracks page 0 and puts page 1 in untracked without ever inserting page 1 into g.protected, (3) committing plain PAGE_READWRITE memory at that formerly free page 1 and writing to it while holding guards() completes with no fault or deadlock, and (4) a write-watched pool remains 100% tracked with zero untracked bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants