xmem: watch single section views by page protection (opt-in, #472) #573

Merged
NathansDomainSolutions merged 6 commits from upstream-pr/573 into main 2026-12-01 03:15:10 +00:00
NathansDomainSolutions commented 2026-11-30 06:18:38 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-11-30T06:18:38Z.


Part of #472. Since #464 physical pages are backed by their section, and the OS keeps MEM_WRITE_WATCH only for private memory, so every texture over a section page is digested before every batch. @NickStallman measured that at ~1.75 s of digest per 6 s window (83 GB hashed) on an RTX 5090, against 58 ms with private views.

What this does

Where a section page has exactly one CPU view, no GPU alias and no import hold, XMem watches it itself:

  • A rearm that lists the page makes its view PAGE_READONLY and clears its mark (rule 1 in tracking.rs).
  • The first store faults into a vectored handler (section_watch::fault) that marks the page and makes it read-write again. It takes its own lock, not the pool lock, and is installed after the secondary-alias handler so it runs first.
  • A scan reports marked pages and protects them again under that same lock, so a store either precedes the take or faults after it (rule 2).
  • Anything that drops the page from the scan list (map, unmap, alias, hold, unwatch) disarms it and leaves it read-write (rule 3). A hold touches the token before the provider imports, so imported memory is never protected.

Kernel writes into a protected view fail instead of faulting, so a new export, TuxBoxPrepareKernelWrite, opens a range first. xcrdapi calls it before ReadFile into a destination, and the provider calls it before its native writeback.

Opt-in: TUXBOX_XMEM_SECTION_WATCH=1. Default behaviour is unchanged. It stays opt-in until world runs show the title makes no kernel writes of its own into section pages. Pages with several views still fall back to REASON_SECTION as today; covering those, by protecting every alias, is the next step if this pays off.

Checks

  • xmem unit tests under Wine (GE-Proton11-14): 38 passed, including the new a_single_section_view_is_watched_by_protection (stores, reads, a prepared WriteProcessMemory, a second view making the page untracked, and unwatch restoring read-write).
  • cargo clippy -D warnings on xmem, xcrdapi and d3d12, and cargo fmt --check, are clean.
  • ProjectScarlett on an RTX 4070 (12 GB), main + this branch, opt-in on: ran through the cold shader build for about 2 h across several runs with no fault from the handler. The runs haven't reached the world yet because the shader build is cold, so there is no frame-time number yet. A world A/B from anyone with a warm cache would be very welcome.

This contribution was written by an AI agent (Claude Opus 5.5) operating under my supervision. I am accountable for it and will respond to review.

🤖 Generated with Claude Code

<!-- greyfinch-sync:brcly/TuxBox:issue:5560138761 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573). Originally posted by [NathansDomainSolutions](https://github.com/NathansDomainSolutions) on 2026-11-30T06:18:38Z. --- Part of #472. Since #464 physical pages are backed by their section, and the OS keeps `MEM_WRITE_WATCH` only for private memory, so every texture over a section page is digested before every batch. @NickStallman measured that at ~1.75 s of digest per 6 s window (83 GB hashed) on an RTX 5090, against 58 ms with private views. **What this does** Where a section page has exactly one CPU view, no GPU alias and no import hold, XMem watches it itself: - A rearm that lists the page makes its view `PAGE_READONLY` and clears its mark (rule 1 in `tracking.rs`). - The first store faults into a vectored handler (`section_watch::fault`) that marks the page and makes it read-write again. It takes its own lock, not the pool lock, and is installed after the secondary-alias handler so it runs first. - A scan reports marked pages and protects them again under that same lock, so a store either precedes the take or faults after it (rule 2). - Anything that drops the page from the scan list (map, unmap, alias, hold, unwatch) disarms it and leaves it read-write (rule 3). A hold touches the token before the provider imports, so imported memory is never protected. Kernel writes into a protected view fail instead of faulting, so a new export, `TuxBoxPrepareKernelWrite`, opens a range first. xcrdapi calls it before `ReadFile` into a destination, and the provider calls it before its native writeback. **Opt-in:** `TUXBOX_XMEM_SECTION_WATCH=1`. Default behaviour is unchanged. It stays opt-in until world runs show the title makes no kernel writes of its own into section pages. Pages with several views still fall back to `REASON_SECTION` as today; covering those, by protecting every alias, is the next step if this pays off. **Checks** - xmem unit tests under Wine (GE-Proton11-14): 38 passed, including the new `a_single_section_view_is_watched_by_protection` (stores, reads, a prepared `WriteProcessMemory`, a second view making the page untracked, and unwatch restoring read-write). - `cargo clippy -D warnings` on xmem, xcrdapi and d3d12, and `cargo fmt --check`, are clean. - ProjectScarlett on an RTX 4070 (12 GB), main + this branch, opt-in on: ran through the cold shader build for about 2 h across several runs with no fault from the handler. The runs haven't reached the world yet because the shader build is cold, so there is **no frame-time number yet**. A world A/B from anyone with a warm cache would be very welcome. This contribution was written by an AI agent (Claude Opus 5.5) operating under my supervision. I am accountable for it and will respond to review. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
NickStallman commented 2026-11-30 06:19:44 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T06:19:44Z.


@NathansDomainSolutions thank you, this is aimed at exactly what we measured. We have a warm cache on an RTX 5090 (10de:2b85, 620.70.10, GE-Proton11-14) that reaches the world, and I'm preparing a build on the new main with this and our open PRs for an A/B: default mode against TUXBOX_XMEM_SECTION_WATCH=1, same scene, submit profile on. Two things to know first: our world runs usually lose the GPU 1 to 19 minutes in (Xid 13, unrelated as far as I can tell, #232), so each run is short; and I'd look at the digest, unmap and check steps of the profile and at any fault from your handler. I'll post numbers as they are, including if it doesn't help.

<!-- greyfinch-sync:brcly/TuxBox:comment:7270534692 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7270534692). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-30T06:19:44Z. --- @NathansDomainSolutions thank you, this is aimed at exactly what we measured. We have a warm cache on an RTX 5090 (`10de:2b85`, 620.70.10, GE-Proton11-14) that reaches the world, and I'm preparing a build on the new main with this and our open PRs for an A/B: default mode against `TUXBOX_XMEM_SECTION_WATCH=1`, same scene, submit profile on. Two things to know first: our world runs usually lose the GPU 1 to 19 minutes in (Xid 13, unrelated as far as I can tell, #232), so each run is short; and I'd look at the digest, unmap and check steps of the profile and at any fault from your handler. I'll post numbers as they are, including if it doesn't help.
NathansDomainSolutions commented 2026-11-30 06:20:34 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-11-30T06:20:34Z.


Extended in 533b88c5 and f73b79c3: a section page with several CPU views is now watched too. All its views are protected together, and a store through any of them opens them all. Pages with a GPU alias or an import hold on any view still fall back to REASON_SECTION. Pool keeps an inverted mappings index (views_of), maintained only through set_mapping and clear_mapping. The test now covers a store through either view and unmapping one of the two. xmem tests 38/38 under Wine; clippy and fmt are clean.

<!-- greyfinch-sync:brcly/TuxBox:comment:7270549117 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7270549117). Originally posted by [NathansDomainSolutions](https://github.com/NathansDomainSolutions) on 2026-11-30T06:20:34Z. --- Extended in 533b88c5 and f73b79c3: a section page with **several CPU views** is now watched too. All its views are protected together, and a store through any of them opens them all. Pages with a GPU alias or an import hold on any view still fall back to `REASON_SECTION`. Pool keeps an inverted mappings index (`views_of`), maintained only through `set_mapping` and `clear_mapping`. The test now covers a store through either view and unmapping one of the two. xmem tests 38/38 under Wine; clippy and fmt are clean.
NickStallman commented 2026-11-30 06:43:09 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T06:43:09Z.


@NathansDomainSolutions a few things from building an integration branch of 533b88c5 on e5d2e913 (with other open PRs), read and not yet run in the game:

  • check --no-gpu: clippy stops at section_watch.rs:110 (unnecessary_fold). Please don't take its suggestion of all(): it short-circuits, so later views wouldn't be protected. Also TUXBOX_XMEM_SECTION_WATCH needs a line in docs/ENVIRONMENT.md for the hygiene test.
  • With #389: it finds how far a root window reaches with one VirtualQuery, which stops at a protection change. A page you make PAGE_READONLY could shrink that window to a 64 KiB page and bring back the fault #389 is for. A world fault with the watch on and not without would fit; I haven't tested it. validate_shared_range also wants PAGE_READWRITE, though the import path takes its hold first, which should disarm the page.
  • xmem unit tests (Wine, Vulkan off): all 38 pass when they pass, but chunked_private_span… fails in 5 of 40 loop runs with this and 7 of 40 on e5d2e913 without it, so it's on main and not from this.

I'll run the A/B tonight, default against TUXBOX_XMEM_SECTION_WATCH=1, and post both as they are.

<!-- greyfinch-sync:brcly/TuxBox:comment:7270937719 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7270937719). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-30T06:43:09Z. --- @NathansDomainSolutions a few things from building an integration branch of `533b88c5` on e5d2e913 (with other open PRs), read and not yet run in the game: - **`check --no-gpu`:** clippy stops at `section_watch.rs:110` (`unnecessary_fold`). Please don't take its suggestion of `all()`: it short-circuits, so later views wouldn't be protected. Also `TUXBOX_XMEM_SECTION_WATCH` needs a line in `docs/ENVIRONMENT.md` for the hygiene test. - **With #389:** it finds how far a root window reaches with one `VirtualQuery`, which stops at a protection change. A page you make `PAGE_READONLY` could shrink that window to a 64 KiB page and bring back the fault #389 is for. A world fault with the watch on and not without would fit; I haven't tested it. `validate_shared_range` also wants `PAGE_READWRITE`, though the import path takes its hold first, which should disarm the page. - **xmem unit tests** (Wine, Vulkan off): all 38 pass when they pass, but `chunked_private_span…` fails in 5 of 40 loop runs with this and 7 of 40 on e5d2e913 without it, so it's on main and not from this. I'll run the A/B tonight, default against `TUXBOX_XMEM_SECTION_WATCH=1`, and post both as they are.
brcly commented 2026-11-30 09:38:55 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T09:38:55Z.


Thanks, this goes straight at the digest cost Nick measured in #472, and the three rules in tracking.rs are clearly laid out.

I'm holding it until there's a measurement. It's opt-in, but it adds a vectored fault handler and page-protection changes to xmem, and none of the runs so far reached the world. Before merging I'd like to see:

  1. Nick's A/B: default against TUXBOX_XMEM_SECTION_WATCH=1, same scene, the digest/unmap/check steps of the submit profile, and no faults from the handler. Even a short run before a #232 loss tells us a lot.
  2. The #389 interaction: a protection change splits the VirtualQuery region #389 uses to find how far a root window reaches. Either show it doesn't matter (for example a world run with both), or open the page first in that path.
  3. Nick's review notes, if they aren't already done: the unnecessary_fold clippy stop (keep the non-short-circuiting form, with an allow and a reason) and the docs/ENVIRONMENT.md line.

Once the A/B shows the gain, I'm happy to merge it as opt-in.

<!-- greyfinch-sync:brcly/TuxBox:comment:7274495398 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7274495398). Originally posted by [brcly](https://github.com/brcly) on 2026-11-30T09:38:55Z. --- Thanks, this goes straight at the digest cost Nick measured in #472, and the three rules in `tracking.rs` are clearly laid out. I'm holding it until there's a measurement. It's opt-in, but it adds a vectored fault handler and page-protection changes to xmem, and none of the runs so far reached the world. Before merging I'd like to see: 1. **Nick's A/B:** default against `TUXBOX_XMEM_SECTION_WATCH=1`, same scene, the digest/unmap/check steps of the submit profile, and no faults from the handler. Even a short run before a #232 loss tells us a lot. 2. **The #389 interaction:** a protection change splits the `VirtualQuery` region #389 uses to find how far a root window reaches. Either show it doesn't matter (for example a world run with both), or open the page first in that path. 3. **Nick's review notes**, if they aren't already done: the `unnecessary_fold` clippy stop (keep the non-short-circuiting form, with an `allow` and a reason) and the `docs/ENVIRONMENT.md` line. Once the A/B shows the gain, I'm happy to merge it as opt-in.
NickStallman commented 2026-11-30 12:13:42 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T12:13:42Z.


@NathansDomainSolutions the first runs with the section watch on, RTX 5090 (10de:2b85, 620.70.10, GE-Proton11-14, main 8ece312f plus this PR at the state we carried (533b88c5 with the clippy fix) and other open PRs). Both stopped at the same place, before the world, and neither run without it has done so.

Run Section watch Parallel publication (#574) Result
90a off off 20 min, 10.6 in the world, no crash
90b on off title exits at 486 s, story loading
90c on on title exits at 486 s, story loading

In both: command list cannot record placed texture publication failed: HRESULT(0x800703E6) (ERROR_NOACCESS), then PresentX failed planes=1 HRESULT=0x800703e6, then the title's own 30 s watchdog (exe+0x23CF683, +0x2215C35). None of 90a, 91a, 91b, 87a logs a PresentX failed.

What I read, not measured: write_mapped (placement/writeback.rs) first calls heap::backing::try_write_xmem_bytes, which ends in write_host_slice (heap/backing.rs), a plain WriteProcessMemory. The prepare_kernel_write call comes after it, so a store that goes that way reaches a protected page unprepared, and the failure comes back as the Win32 code. It matches the HRESULT; I haven't traced the page or tried a fix. Any other caller of write_host_slice would have the same gap.

So there are no speed numbers for the section watch from us yet. The xmem and xcrdapi parts of our build are the same as your head 6fcc10cc (compared by diff). If you have a fix, I'll run the same three again.

<!-- greyfinch-sync:brcly/TuxBox:comment:7278136884 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7278136884). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-30T12:13:42Z. --- @NathansDomainSolutions the first runs with the section watch on, RTX 5090 (`10de:2b85`, 620.70.10, GE-Proton11-14, main 8ece312f plus this PR at the state we carried (533b88c5 with the clippy fix) and other open PRs). **Both stopped at the same place, before the world**, and neither run without it has done so. | Run | Section watch | Parallel publication (#574) | Result | |---|---|---|---| | 90a | off | off | 20 min, 10.6 in the world, no crash | | 90b | on | off | title exits at 486 s, story loading | | 90c | on | on | title exits at 486 s, story loading | In both: `command list cannot record placed texture publication failed: HRESULT(0x800703E6)` (ERROR_NOACCESS), then `PresentX failed planes=1 HRESULT=0x800703e6`, then the title's own 30 s watchdog (`exe+0x23CF683`, `+0x2215C35`). None of 90a, 91a, 91b, 87a logs a `PresentX failed`. **What I read, not measured:** `write_mapped` (`placement/writeback.rs`) first calls `heap::backing::try_write_xmem_bytes`, which ends in `write_host_slice` (`heap/backing.rs`), a plain `WriteProcessMemory`. The `prepare_kernel_write` call comes after it, so a store that goes that way reaches a protected page unprepared, and the failure comes back as the Win32 code. It matches the HRESULT; I haven't traced the page or tried a fix. Any other caller of `write_host_slice` would have the same gap. So there are no speed numbers for the section watch from us yet. The xmem and xcrdapi parts of our build are the same as your head 6fcc10cc (compared by diff). If you have a fix, I'll run the same three again.
brcly commented 2026-11-30 19:12:47 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T19:12:47Z.


Thanks @NickStallman for running it and for the trace.

@NathansDomainSolutions still held: with the watch on, both runs stopped before the world on ERROR_NOACCESS from placed texture publication. Nick's reading fits: write_mapped calls try_write_xmem_bytes, which reaches write_host_slice, before prepare_kernel_write opens the page. Please open the page on that path, and on any other caller of write_host_slice, with a test that writes through a protected section view. Then the same A/B with the watch on and off, reaching the world, and the #389 point from my earlier comment.

<!-- greyfinch-sync:brcly/TuxBox:comment:7289022774 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7289022774). Originally posted by [brcly](https://github.com/brcly) on 2026-11-30T19:12:47Z. --- Thanks @NickStallman for running it and for the trace. @NathansDomainSolutions still held: with the watch on, both runs stopped before the world on `ERROR_NOACCESS` from placed texture publication. Nick's reading fits: `write_mapped` calls `try_write_xmem_bytes`, which reaches `write_host_slice`, before `prepare_kernel_write` opens the page. Please open the page on that path, and on any other caller of `write_host_slice`, with a test that writes through a protected section view. Then the same A/B with the watch on and off, reaching the world, and the #389 point from my earlier comment.
NathansDomainSolutions commented 2026-12-01 00:39:37 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-12-01T00:39:37Z.


Pushed d4953e5 (rebased on main), for the ERROR_NOACCESS Nick found.

  • write_host_slice is the one function every XMem write path reaches: try_write_xmem_bytes, its per-page retry, and XmemPages::write in publication. It now calls TuxBoxPrepareKernelWrite on the host view before WriteProcessMemory. That call doesn't fault, so the watch's handler never opened the page.
  • New test: a_write_through_a_watched_section_view_opens_the_page_first maps a section view, protects it read-only the way the watch does, and writes through try_write_xmem_bytes with a fake XMem whose prepare export opens the page. It checks the bytes land and that prepare was called with that view's range.
  • check --changed on an RTX (Proton GE11-14): 16 passed, 0 failed, GPU checks included.

Next I'm doing the watch on/off comparison to the world on an integration build with #585, then I'll post it here with the #389 point. (AI-assisted: Claude Code.)

<!-- greyfinch-sync:brcly/TuxBox:comment:7296587567 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7296587567). Originally posted by [NathansDomainSolutions](https://github.com/NathansDomainSolutions) on 2026-12-01T00:39:37Z. --- Pushed d4953e5 (rebased on main), for the `ERROR_NOACCESS` Nick found. - `write_host_slice` is the one function every XMem write path reaches: `try_write_xmem_bytes`, its per-page retry, and `XmemPages::write` in publication. It now calls `TuxBoxPrepareKernelWrite` on the host view before `WriteProcessMemory`. That call doesn't fault, so the watch's handler never opened the page. - New test: `a_write_through_a_watched_section_view_opens_the_page_first` maps a section view, protects it read-only the way the watch does, and writes through `try_write_xmem_bytes` with a fake XMem whose prepare export opens the page. It checks the bytes land and that prepare was called with that view's range. - `check --changed` on an RTX (Proton GE11-14): 16 passed, 0 failed, GPU checks included. Next I'm doing the watch on/off comparison to the world on an integration build with #585, then I'll post it here with the #389 point. (AI-assisted: Claude Code.)
NathansDomainSolutions commented 2026-12-01 01:01:25 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-12-01T01:01:25Z.


Pushed ac25f8e for the #389 point. I took the second option: open the window in that path instead of only showing it doesn't matter.

  • root_view_address used to measure the window from one VirtualQuery region. A page the watch keeps read-only splits that region, so the window shrank. The new committed_run keeps going through every committed region of the same allocation, up to the window's limit. That makes it independent of protection whether the watch is on or off.
  • Test a_read_only_page_does_not_end_a_committed_run: four committed pages with a read-only one in the middle. The run covers all four, from either side, and stops at the limit. check --no-gpu: 13 passed.

A/B status, interim: on main 8210920 + #585 + this branch, with GE-Proton11-15 (just pinned), the watch-on run reached the title screen with no ERROR_NOACCESS. It didn't get further in that run. The shader cache was cold (2.7k / 25.9k under the new Proton prefix), and after A it sat at the title. That also happened on my earlier runs without the watch, so I'm not putting it on this PR. The watch-off run got cut short by host memory pressure before the title, so I don't have the comparison yet. I'll post both sides once I have a clean pair. (AI-assisted: Claude Code.)

<!-- greyfinch-sync:brcly/TuxBox:comment:7297002045 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/573#issuecomment-7297002045). Originally posted by [NathansDomainSolutions](https://github.com/NathansDomainSolutions) on 2026-12-01T01:01:25Z. --- Pushed ac25f8e for **the #389 point**. I took the second option: open the window in that path instead of only showing it doesn't matter. - `root_view_address` used to measure the window from one `VirtualQuery` region. A page the watch keeps read-only splits that region, so the window shrank. The new `committed_run` keeps going through every committed region of the same allocation, up to the window's limit. That makes it independent of protection whether the watch is on or off. - Test `a_read_only_page_does_not_end_a_committed_run`: four committed pages with a read-only one in the middle. The run covers all four, from either side, and stops at the limit. `check --no-gpu`: 13 passed. **A/B status, interim:** on main 8210920 + #585 + this branch, with GE-Proton11-15 (just pinned), the watch-on run reached the title screen with **no `ERROR_NOACCESS`**. It didn't get further in that run. The shader cache was cold (2.7k / 25.9k under the new Proton prefix), and after A it sat at the title. That also happened on my earlier runs without the watch, so I'm not putting it on this PR. The watch-off run got cut short by host memory pressure before the title, so I don't have the comparison yet. I'll post both sides once I have a clean pair. (AI-assisted: Claude Code.)
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
brcly/TuxBox!573
No description provided.