Skip to content

fix(kyty): populate direct memory mappings on Windows too - #286

Closed
talha-devp wants to merge 2 commits into
simnJS:AnyKytyfrom
talha-devp:fix/windows-populate-direct-memory
Closed

talha-devp wants to merge 2 commits into
simnJS:AnyKytyfrom
talha-devp:fix/windows-populate-direct-memory

Conversation

@talha-devp

Copy link
Copy Markdown
Contributor

What

On Windows, direct memory is a pagefile-backed section mapped on demand, so the first write to each 4 KiB page of a fresh mapping takes a demand-zero page fault on the writing thread. #163 (afaf3ef8b4) removed these faults on Linux; Windows kept them.

Hades II tiles its textures with the CPU into its 6 GiB GPU pool while its loader streams them. On Windows that loader takes about 19,000 page faults a second, so it falls behind, and the 16 MiB staging buffers it releases pile up in the game's 512 MiB CPU upload pool. Once the pool is full, the next staging allocation fails silently and the game copies the texture to NULL: the LoadTaskScheduler crash of #219.

Kyty builds on Windows now populate each new direct mapping the way the Linux populate backing does. While a quarter of host memory stays available afterwards, the readable pages of the new view are read once, on several threads (256 MiB chunks), so they are resident when the game first writes them. Reading, not writing, leaves the pages unmarked for the write tracking, as on Linux.

KYTY_DIRECT_MEMORY_PAGES now applies on Windows too: unset or populate populates, demand keeps the old demand paging, and private (the Linux tmpfs) stops with a message. The log names the choice, as on Linux. The direct-pages check of guest_memory_tests gets a Windows variant that checks residency with QueryWorkingSetEx, registered as guest_direct_memory_pages_populate and _demand. Like its Linux variant, it is not in the CI's shim test pattern.

How it was found: an outside reader polled the game's pool and loader state with ReadProcessMemory every 0.5 to 2 ms, which leaves the timing alone (a debugger hides the crash). The GPU fences always completed at once, and the loader's queue grew because tiling each 16 MiB texture was slow; most samples of the loader sat on its first store into each new block. Upstream main does not hit this, because its GPU driver imports guest memory into Vulkan, which keeps it resident (7.3 GB working set against 1.3 GB on AnyKyty).

Tested

Windows 10 22H2 (19045), Ryzen 5 5600, RTX 4060 (NVIDIA 621.14), 32 GB. AnyKyty 7de61e3451, WinLibs GCC 15.2 and LLVM 21.1.8, Release, as the Windows job builds it.

Hades II (PPSA36082), cold launches of 60 s replaying the same controller recording:

AnyKyty 7de61e3451 with this PR upstream main 47139cd910
Launches that crash at start 15 of 15 0 of 10 (2,900 to 3,960 flips each) 1 of 8 (earlier)
Page faults while the textures load 18,810/s 180 to 400/s 563/s
Peak use of the 512 MiB upload pool 503 MiB, then the crash 359 to 388 MiB 359 MiB
Working set 1.3 GB 9.1 GB 7.3 GB

Five Kyty switches (KYTY_LABELS_AROUND_GPU_WRITES=0, KYTY_ASYNC_PIPELINES=0, KYTY_SUSPEND_POINT_DEPTH=1, KYTY_WRITE_FAULT_BLOCKS=0, KYTY_REPLAY_GPU_PAGE_READS=0 with KYTY_REPLAY_AFTER_READBACK=0) did not change the crash on the base.

Cost: Hades maps about 7.8 GiB of direct memory at start, which now becomes resident in about 0.9 s (the 6 GiB pool at about 7 GB/s; read from the working set over time). Later mappings that would leave less than a quarter of host memory available stay demand-paged, as on Linux.

Tests on the same machine, Windows build with -DBUILD_TESTING=ON: guest_direct_memory_pages_populate and guest_direct_memory_pages_demand pass; ctest -R '^kyty_shim_.*$' passes (98 of 98; kyty_shim_rt_corpus, kyty_shim_bda_load_forms and kyty_shim_wave_split_written_reads skip). guest_memory fails at its first check (CheckResidentMappingBookkeepingStaysSmall, "resident direct mapping bookkeeping: -992.12 MiB per GiB mapped"); it fails the same way on the base 7de61e3451 without this change, and it is not in the CI's test pattern.

tools/check_conventions.py --base origin/AnyKyty and tools/check_kyty_settings.py pass. Not tested on Linux: the Linux code is unchanged and the CI builds it.

Checklist

On Windows, direct memory is a pagefile-backed section mapped on
demand, so the first write to each 4 KiB page of a fresh mapping takes
a demand-zero page fault on the writing thread. afaf3ef removed
these faults on Linux; Windows kept them. Hades II tiles its textures
with the CPU into its 6 GiB GPU pool while its loader streams them:
about 19,000 faults a second slow the thread that releases the 16 MiB
staging buffers, the 512 MiB upload pool fills, and the game crashes
before its first frame on a memcpy to NULL after a failed allocation
(simnJS#219).

Kyty builds on Windows now populate each new direct mapping the way
the Linux populate backing does: while a quarter of host memory stays
available afterwards, the readable pages of the new view are read once
on several threads, so they are resident when the game first writes
them. KYTY_DIRECT_MEMORY_PAGES=demand keeps the old demand paging;
private needs a Linux host and stops on Windows. The direct-pages
memory test now also runs on Windows, checking residency with
QueryWorkingSetEx.

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

simnJS commented Nov 25, 2026

Copy link
Copy Markdown
Owner

The Windows CI build passed, and the Hades II fault-rate measurements explain the benefit. I found a lifetime gap in the prefault loop. MapDirectRecorded has already published the mapping and released its mutation guard when TouchReadablePages calls VirtualQuery. Another mapping or protection change can invalidate that result before the worker's volatile read and cause an access violation in the host. Linux uses madvise, which can return an error if the range disappears. Could you use an OS-mediated prefetch or hold a guarantee covering the lifetime and readability of the range for the entire touch, and cover concurrent mapping changes in the Windows tests? The two residency tests currently cover stable mappings.

The Windows populate ran after MapDirectRecorded had released its
mutation guard, then checked each region with VirtualQuery and read its
pages directly. Another thread could unmap or protect the range between
the check and the read, and the host took an access violation.

Each page is now read through ReadProcessMemory, which returns an error
instead of faulting when the page is gone or no longer readable; the
populate stops at the first such page. A new Windows test,
guest_direct_memory_pages_changing, remaps a 64 MiB direct mapping while
another thread protects and unmaps parts of it. It crashed 3 of 3 runs
before this change and passes 5 of 5 after it.

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

Copy link
Copy Markdown
Contributor Author

Thanks, you're right. I reproduced it before changing anything: the new test crashed with 0xc0000005 in 3 of 3 runs on the old prefault.

20f6840 now reads one byte of each page through ReadProcessMemory on the current process instead of VirtualQuery and a direct read. When a page has been unmapped or made unreadable, the kernel returns an error instead of faulting, and the populate stops at the first such page. Nothing is held across the touch, so other mapping calls don't wait on it.

New Windows test guest_direct_memory_pages_changing: it remaps a 64 MiB direct mapping 40 times with populate on, while another thread keeps protecting (none/read-write) and unmapping 2 MiB pieces of it.

old prefault (2b52d30) 20f6840
guest_direct_memory_pages_changing crashed 3/3 (0xc0000005) passed 5/5
guest_direct_memory_pages_populate (every page resident, checked with QueryWorkingSetEx) passed passed 5/5
guest_direct_memory_pages_demand passed passed 5/5
Hades II, 60 s with the pad replay — 3/3 ran, 4,650–5,100 frames presented, no crash (base 7de61e3 crashed in every launch)

guest_memory still fails as it does on the base. The THIRD_PARTY_KYTY.md row now describes the Windows populate.

@simnJS

simnJS commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Merged into AnyKyty as 62fb28865c and eb5103d101 (authorship kept), after a green CI on Linux, sanitizers and Windows. Thanks for the quick fix and the reproduction, @talha-devp!

@simnJS simnJS closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hades II (PPSA36082) crashes at start on Windows with the Kyty backend: null write in LoadTaskScheduler

2 participants