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

Open
fsrajner wants to merge 2 commits from upstream-pr/475 into main
fsrajner commented 2026-11-27 16:20:36 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by fsrajner on 2026-11-27T16:20:36Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:5529818589 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/475). Originally posted by [fsrajner](https://github.com/fsrajner) on 2026-11-27T16:20:36Z. --- 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)
UwUCammie commented 2026-11-27 16:49:42 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by UwUCammie on 2026-11-27T16:49:42Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7223715906 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/475#issuecomment-7223715906). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-27T16:49:42Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/475:upstream-pr/475'
git switch 'upstream-pr/475'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/475'
git switch 'upstream-pr/475'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/475'
git switch 'upstream-pr/475'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/475'
git switch 'main'
git merge --squash 'upstream-pr/475'
git switch 'main'
git merge --ff-only 'upstream-pr/475'
git switch 'main'
git merge 'upstream-pr/475'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
avatarmanz/VIctory-7900XTX!475
No description provided.