xmem: watch single section views by page protection (opt-in, #472) #573
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/573"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_WATCHonly 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:
PAGE_READONLYand clears its mark (rule 1 intracking.rs).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.Kernel writes into a protected view fail instead of faulting, so a new export,
TuxBoxPrepareKernelWrite, opens a range first. xcrdapi calls it beforeReadFileinto 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 toREASON_SECTIONas today; covering those, by protecting every alias, is the next step if this pays off.Checks
a_single_section_view_is_watched_by_protection(stores, reads, a preparedWriteProcessMemory, a second view making the page untracked, and unwatch restoring read-write).cargo clippy -D warningson xmem, xcrdapi and d3d12, andcargo fmt --check, are clean.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
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 againstTUXBOX_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.Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-11-30T06:20:34Z.
Extended in
533b88c5andf73b79c3: 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 toREASON_SECTION. Pool keeps an inverted mappings index (views_of), maintained only throughset_mappingandclear_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.Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T06:43:09Z.
@NathansDomainSolutions a few things from building an integration branch of
533b88c5one5d2e913(with other open PRs), read and not yet run in the game:check --no-gpu: clippy stops atsection_watch.rs:110(unnecessary_fold). Please don't take its suggestion ofall(): it short-circuits, so later views wouldn't be protected. AlsoTUXBOX_XMEM_SECTION_WATCHneeds a line indocs/ENVIRONMENT.mdfor the hygiene test.VirtualQuery, which stops at a protection change. A page you makePAGE_READONLYcould 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_rangealso wantsPAGE_READWRITE, though the import path takes its hold first, which should disarm the page.chunked_private_span…fails in 5 of 40 loop runs with this and 7 of 40 one5d2e913without 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.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.rsare 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:
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.VirtualQueryregion #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.unnecessary_foldclippy stop (keep the non-short-circuiting form, with anallowand a reason) and thedocs/ENVIRONMENT.mdline.Once the A/B shows the gain, I'm happy to merge it as opt-in.
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, main8ece312fplus this PR at the state we carried (533b88c5with the clippy fix) and other open PRs). Both stopped at the same place, before the world, and neither run without it has done so.In both:
command list cannot record placed texture publication failed: HRESULT(0x800703E6)(ERROR_NOACCESS), thenPresentX failed planes=1 HRESULT=0x800703e6, then the title's own 30 s watchdog (exe+0x23CF683,+0x2215C35). None of 90a, 91a, 91b, 87a logs aPresentX failed.What I read, not measured:
write_mapped(placement/writeback.rs) first callsheap::backing::try_write_xmem_bytes, which ends inwrite_host_slice(heap/backing.rs), a plainWriteProcessMemory. Theprepare_kernel_writecall 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 ofwrite_host_slicewould 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.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_NOACCESSfrom placed texture publication. Nick's reading fits:write_mappedcallstry_write_xmem_bytes, which reacheswrite_host_slice, beforeprepare_kernel_writeopens the page. Please open the page on that path, and on any other caller ofwrite_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.Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-12-01T00:39:37Z.
Pushed
d4953e5(rebased on main), for theERROR_NOACCESSNick found.write_host_sliceis the one function every XMem write path reaches:try_write_xmem_bytes, its per-page retry, andXmemPages::writein publication. It now callsTuxBoxPrepareKernelWriteon the host view beforeWriteProcessMemory. That call doesn't fault, so the watch's handler never opened the page.a_write_through_a_watched_section_view_opens_the_page_firstmaps a section view, protects it read-only the way the watch does, and writes throughtry_write_xmem_byteswith 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 --changedon 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.)
Imported from brcly/TuxBox. Originally posted by NathansDomainSolutions on 2026-12-01T01:01:25Z.
Pushed
ac25f8efor the #389 point. I took the second option: open the window in that path instead of only showing it doesn't matter.root_view_addressused to measure the window from oneVirtualQueryregion. A page the watch keeps read-only splits that region, so the window shrank. The newcommitted_runkeeps 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.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 noERROR_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.)