Linux under Wine: holes known from the runtime's placeholders, written pages asked of the kernel (PAGEMAP_SCAN) #475

Open
github-bot wants to merge 2 commits from github-bot/gh-475 into main AGit
Owner

@⁠fsrajner on GitHub · 2026-11-27 16:20 UTC · original
fsrajner:pr/linux-wine-memory-askings → main · +508 −8 · 8 files · 2 commits · open

Two changes for Linux under Wine (Proton). They complement #440: that PR takes Wine's server out of mapping title pages; this one takes Wine out of asking what the title wrote. Measured on an RX 9070 XT with RADV, Proton Experimental (Wine 11) and vkd3d-proton.

Under Wine, the thread that submits the title's work spent about 30% of its time in Wine's emulation of memory queries, all under Wine's one virtual-memory lock:

  • NtQueryVirtualMemory: about 17.6%, about 30 µs each against about 1 µs on Windows.
  • NtGetWriteWatch: about 9.2%, about 20 ms per GB in the world.

1. Holes of the runtime's own placeholders are known without asking the system

Wine does not answer NtQueryVirtualMemory with MemoryRegionInformationEx (class 7), so watch::placeholder() always said no under Wine. As a result:

  • no hole was ever held;
  • every range with a hole was asked about block by block at every refresh (2,500 to 4,300 VirtualQuery calls a frame);
  • those ranges never took the light path.

Three watch tests fail under Wine for this reason: holes_are_known_until_the_runtime_tells, no_mapping_is_lost_between_askers and what_is_left_alone_is_no_known_hole.

The fix:

  • xmem tells the GPU side of its placeholder reservations (title_memory_placeholder).
  • placeholder() answers from that, plus the block's state, and asks the system only about other memory.
  • Releases forget them.
  • The watch tests register their placeholders the way xmem does. All 51 pass under Wine.

This is correct on Windows too. There, the answer is the same as the system's.

2. Written pages asked of the kernel directly (PAGEMAP_SCAN). Wine only, opt-in

Proton already keeps MEM_WRITE_WATCH memory write-protected with userfaultfd in asynchronous mode, and answers GetWriteWatch with the PAGEMAP_SCAN ioctl. The runtime now makes that ioctl itself, which avoids Wine's lock and its bookkeeping.

  • wine.rs:
    • Native Linux system calls from our code. Proton's WINE_HACK_NATIVE_SYSCALLS=1 lets system calls through from code at 0x7001_0000_0000 and above, so a small stub is copied there.
    • Switch: VICTORY_LINUX_SYSCALLS=1.
    • Self-test: getpid compared with /proc/self/stat read natively. Wine opens files in its server, so Z:\proc\self is the server's, not ours.
  • pagemap.rs:
    • Switch: VICTORY_LINUX_SCAN=1.
    • kernel_written() uses PAGEMAP_SCAN with PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC, which asks and re-protects in one step.
    • It is used only inside one reservation known to be write-watched (xmem, upload). Anything else is asked as before, so no scan can stop part way after re-protecting.
  • Smaller changes, for Wine only:
    • texture::committed answers from the views the runtime mapped, since sections are committed whole there (VICTORY_COMMITTED_BY_VIEWS).
    • copystat's thread times are off by default (VICTORY_THREAD_TIMES), because each one is a costly trip under Wine.
    • The asking-group limits can be set: VICTORY_AHEAD_GAP_KIB and VICTORY_AHEAD_MAX_MIB.

Everything is behind on_wine() or a switch, so nothing changes on Windows.

Measured

Setup:

  • The title's first world frames, 40 frames after 8 warm-up frames.
  • Started from a restored save snapshot, with an automated start press.
  • Headless gamescope.
  • 3+3 interleaved runs; medians are shown. Run-to-run variance is large on this machine.
before after
Change 1 (holes) 309 ms/frame 125 ms/frame
Change 2 (VICTORY_LINUX_SYSCALLS=1 VICTORY_LINUX_SCAN=1 WINE_HACK_NATIVE_SYSCALLS=1) 207 ms/frame 82 ms/frame

Hole questions dropped from about 2,500–4,300 to about 40–100 a frame.

Caveat: these runs had renderer port 1 on, which gives a black picture under RADV (#440's VICTORY_PORT_HOME addresses that). Both changes are on the CPU side and do the same work either way. With port 1 off and a picture, the game ran 400 world frames at 145 ms/frame on the same machine, with ray tracing and FSR 4, while another VM was loading the host.

Tests

All tests below ran under Wine (Proton Experimental):

  • watch:: (51), pagemap::, wine::, inplace, shadow::, relay_tests, queue, texture: 134 passed, with the new switches both on and off.
  • The new tests:
    • native calls work and answer as Linux does;
    • a written page is found once, then not again until written again;
    • a scan that is not inside one write-watched reservation is not taken.

🤖 Generated with Claude Code

**@⁠fsrajner** on GitHub · 2026-11-27 16:20 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/475) `fsrajner:pr/linux-wine-memory-askings` → `main` · +508 −8 · 8 files · 2 commits · open Two changes for Linux under Wine (Proton). They complement #440: that PR takes Wine's server out of mapping title pages; this one takes Wine out of asking *what the title wrote*. Measured on an RX 9070 XT with RADV, Proton Experimental (Wine 11) and vkd3d-proton. Under Wine, the thread that submits the title's work spent about 30% of its time in Wine's emulation of memory queries, all under Wine's one virtual-memory lock: - NtQueryVirtualMemory: about 17.6%, about 30 µs each against about 1 µs on Windows. - NtGetWriteWatch: about 9.2%, about 20 ms per GB in the world. ## 1. Holes of the runtime's own placeholders are known without asking the system Wine does not answer `NtQueryVirtualMemory` with `MemoryRegionInformationEx` (class 7), so `watch::placeholder()` always said no under Wine. As a result: - no hole was ever held; - every range with a hole was asked about block by block at every refresh (2,500 to 4,300 `VirtualQuery` calls a frame); - those ranges never took the light path. Three `watch` tests fail under Wine for this reason: `holes_are_known_until_the_runtime_tells`, `no_mapping_is_lost_between_askers` and `what_is_left_alone_is_no_known_hole`. The fix: - `xmem` tells the GPU side of its placeholder reservations (`title_memory_placeholder`). - `placeholder()` answers from that, plus the block's state, and asks the system only about other memory. - Releases forget them. - The `watch` tests register their placeholders the way `xmem` does. All 51 pass under Wine. This is correct on Windows too. There, the answer is the same as the system's. ## 2. Written pages asked of the kernel directly (`PAGEMAP_SCAN`). Wine only, opt-in Proton already keeps `MEM_WRITE_WATCH` memory write-protected with userfaultfd in asynchronous mode, and answers `GetWriteWatch` with the `PAGEMAP_SCAN` ioctl. The runtime now makes that ioctl itself, which avoids Wine's lock and its bookkeeping. - `wine.rs`: - Native Linux system calls from our code. Proton's `WINE_HACK_NATIVE_SYSCALLS=1` lets system calls through from code at 0x7001_0000_0000 and above, so a small stub is copied there. - Switch: `VICTORY_LINUX_SYSCALLS=1`. - Self-test: `getpid` compared with `/proc/self/stat` read natively. Wine opens files in its server, so `Z:\proc\self` is the server's, not ours. - `pagemap.rs`: - Switch: `VICTORY_LINUX_SCAN=1`. - `kernel_written()` uses `PAGEMAP_SCAN` with `PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC`, which asks and re-protects in one step. - It is used only inside one reservation known to be write-watched (`xmem`, `upload`). Anything else is asked as before, so no scan can stop part way after re-protecting. - Smaller changes, for Wine only: - `texture::committed` answers from the views the runtime mapped, since sections are committed whole there (`VICTORY_COMMITTED_BY_VIEWS`). - copystat's thread times are off by default (`VICTORY_THREAD_TIMES`), because each one is a costly trip under Wine. - The asking-group limits can be set: `VICTORY_AHEAD_GAP_KIB` and `VICTORY_AHEAD_MAX_MIB`. Everything is behind `on_wine()` or a switch, so nothing changes on Windows. ## Measured Setup: - The title's first world frames, 40 frames after 8 warm-up frames. - Started from a restored save snapshot, with an automated start press. - Headless gamescope. - 3+3 interleaved runs; medians are shown. Run-to-run variance is large on this machine. | | before | after | |---|---|---| | Change 1 (holes) | 309 ms/frame | **125 ms/frame** | | Change 2 (`VICTORY_LINUX_SYSCALLS=1 VICTORY_LINUX_SCAN=1 WINE_HACK_NATIVE_SYSCALLS=1`) | 207 ms/frame | **82 ms/frame** | Hole questions dropped from about 2,500–4,300 to about 40–100 a frame. Caveat: these runs had renderer port 1 on, which gives a black picture under RADV (#440's `VICTORY_PORT_HOME` addresses that). Both changes are on the CPU side and do the same work either way. With port 1 off and a picture, the game ran 400 world frames at 145 ms/frame on the same machine, with ray tracing and FSR 4, while another VM was loading the host. ## Tests All tests below ran under Wine (Proton Experimental): - `watch::` (51), `pagemap::`, `wine::`, `inplace`, `shadow::`, `relay_tests`, `queue`, `texture`: 134 passed, with the new switches both on and off. - The new tests: - native calls work and answer as Linux does; - a written page is found once, then not again until written again; - a scan that is not inside one write-watched reservation is not taken. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Wine does not answer NtQueryVirtualMemory MemoryRegionInformationEx, so
placeholder() always said no there: no hole was ever held, every range
with a hole was asked about block by block at every refresh (~1,900
VirtualQuery a frame at ~30 us under Wine) and never took the light path.
xmem now tells the GPU side of its placeholder reservations
(title_memory_placeholder); placeholder() answers from that and the
block's state, and asks the system only of others. Releases forget them.
The watch tests register their placeholders as xmem does; under Wine
holes_are_known_until_the_runtime_tells, no_mapping_is_lost_between_askers
and what_is_left_alone_is_no_known_hole now pass (51/51).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 9baf20a707d308de855867687deaaa29f2c1023d)
Under Wine the submitting thread spent ~30% of its time in Wine's
emulation of memory queries, under its one virtual-memory lock.

- wine.rs: on_wine()/switch(); native Linux system calls from our code,
  under Proton's WINE_HACK_NATIVE_SYSCALLS=1 (a stub copied to
  0x7001_0000_0000+, where Proton lets system calls through), with
  VICTORY_LINUX_SYSCALLS=1; self-tested (getpid against /proc/self/stat
  read natively: Wine opens files in its server, whose "self" that is).
- pagemap.rs, VICTORY_LINUX_SCAN=1: kernel_written() asks the kernel's
  PAGEMAP_SCAN (PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC) on the
  userfaultfd write watch Proton already keeps for MEM_WRITE_WATCH memory:
  written and protected again in one step, without Wine's lock. Only
  inside one reservation known to be write-watched (xmem, upload);
  anything else is asked as before. Tests under Wine.
- texture::committed under Wine from the views the runtime mapped
  (sections are committed whole there; VICTORY_COMMITTED_BY_VIEWS).
- copystat's per-submission thread times off under Wine
  (VICTORY_THREAD_TIMES; each is a costly trip there).
- VICTORY_AHEAD_GAP_KIB / VICTORY_AHEAD_MAX_MIB for the asking groups.

Measured (RX 9070 XT, Proton Experimental, RADV; title's first world
frames, 3+3 interleaved runs, medians): 207 -> 82 ms a frame with
VICTORY_LINUX_SYSCALLS=1 VICTORY_LINUX_SCAN=1 WINE_HACK_NATIVE_SYSCALLS=1.
Off by default; on Windows nothing changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 16:22:46 +00:00
Author
Owner

@⁠UwUCammie on GitHub · 2026-11-27 16:49 UTC · original · edited 2026-11-27 16:56 UTC

One fallback case stood out in pagemap::written. If an early PAGEMAP_SCAN batch returns pages and re-protects them, a later error drops the accumulated pages and falls back to GetWriteWatch. The fallback can miss those pages because their written state was already reset. The kernel can also apply protection changes to part of one scan before it encounters a VMA that fails PM_SCAN_CHECK_WPASYNC.

Could the fallback conservatively include the affected prefix, or refresh the entire watched span when a mutating scan fails? The Linux documentation calls out partial walks. Kernel docs

I tried the gap knob separately from the native scan. A generated fixture on this Wine 11.22 host returns the same two dirty pages with either one joined GetWriteWatch call or two narrow calls. At a 1 MiB clean gap, the medians were 1.14 µs joined and 2.09 µs split. At 8 MiB, they were 2.32 µs joined and 2.10 µs split. I’ve queued an 8 MiB versus 1 MiB comparison in the full cold 720p run. The generated fixture measures two sparse writes, so this leaves the gameplay effect open. Probe and setup

**@⁠UwUCammie** on GitHub · 2026-11-27 16:49 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/475#issuecomment-7223715906) · edited 2026-11-27 16:56 UTC One fallback case stood out in `pagemap::written`. If an early `PAGEMAP_SCAN` batch returns pages and re-protects them, a later error drops the accumulated `pages` and falls back to `GetWriteWatch`. The fallback can miss those pages because their written state was already reset. The kernel can also apply protection changes to part of one scan before it encounters a VMA that fails `PM_SCAN_CHECK_WPASYNC`. Could the fallback conservatively include the affected prefix, or refresh the entire watched span when a mutating scan fails? The Linux documentation calls out partial walks. [Kernel docs](https://github.com/torvalds/linux/blob/v6.18/Documentation/admin-guide/mm/pagemap.rst#pagemap-scan-ioctl) I tried the gap knob separately from the native scan. A generated fixture on this Wine 11.22 host returns the same two dirty pages with either one joined `GetWriteWatch` call or two narrow calls. At a 1 MiB clean gap, the medians were 1.14 µs joined and 2.09 µs split. At 8 MiB, they were 2.32 µs joined and 2.10 µs split. I’ve queued an 8 MiB versus 1 MiB comparison in the full cold 720p run. The generated fixture measures two sparse writes, so this leaves the gameplay effect open. [Probe and setup](https://github.com/UwUCammie/victory-amd/commit/f2a34e1e)
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/475/head:github-bot/gh-475'
git switch 'github-bot/gh-475'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-475'
git switch 'github-bot/gh-475'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-475'
git switch 'github-bot/gh-475'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-475'
git switch 'main'
git merge --squash 'github-bot/gh-475'
git switch 'main'
git merge --ff-only 'github-bot/gh-475'
git switch 'main'
git merge 'github-bot/gh-475'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.