xmem: one view per page so partial unmaps do not lose writes #110

Manually merged
github merged 8 commits from github-bot/gh-110 into gh-pr/110/base 2026-12-01 15:34:20 +00:00 AGit
Owner

@⁠TheRifai on GitHub · 2026-12-01 14:52 UTC · original
TheRifai:xmem-per-page-views → main · +226 −131 · 8 files · 8 commits · merged 2026-12-01 15:29 UTC

What

Fixes the lost write that the strict title-thread test from #109 catches. xmem::unmap removed the whole view covering a multi-page mapping and re-mapped the kept prefix/suffix via UnmapViewOfFile2 + MapViewOfFile3; that briefly unmaps the pages that should stay, and a title thread's concurrent write to one of them is lost. map now creates one view per page, so a partial unmap hides only the pages it was asked to unmap. The strict assert_eq! stays untouched; the test that asserted "the view is still the one view it was" now checks both pages keep their own views, preserving its intent (both still mapped, bytes intact).

The reference

Console behaviour: a page the title was never given up keeps its contents while other pages of the view are remapped (the test's own doc). No new fallback: the single-view path remains for a one-page mapping or an owned placeholder.

Evidence

  • Strict test: 5/50 failures on current main, 0/50 after the change (Proton-GE11-16, xwin test).
  • All 25 xmem tests pass; the full victory-hle lib suite passes except the known NTFS link-limit test (fixed in #105, merged separately).
  • tools/build.sh and a 120 s house-scene validation run under GE-Proton11-16: survived to timeout with only the four known VUIDs.

Checklist (docs/AGENTS.md)

  • One topic; docs/TASKS.md not edited (rule 29/31)
  • No fallback added for an impossible case (rule 33)
  • No constants or switches added (rules 2/9b)
  • No queues changed (rule 35)
  • No new unsafe (rule 36)
  • Check, build, tests and validation pass; no translator output change (rule 27)
  • Comments say what is true now (rule 3)
  • Nothing from the title, no non-free data pointers (hard constraints)
**@⁠TheRifai** on GitHub · 2026-12-01 14:52 UTC · [original](https://github.com/vktory6/VKtory/pull/110) `TheRifai:xmem-per-page-views` → `main` · +226 −131 · 8 files · 8 commits · merged 2026-12-01 15:29 UTC ## What Fixes the lost write that the strict title-thread test from #109 catches. `xmem::unmap` removed the whole view covering a multi-page mapping and re-mapped the kept prefix/suffix via `UnmapViewOfFile2` + `MapViewOfFile3`; that briefly unmaps the pages that should stay, and a title thread's concurrent write to one of them is lost. `map` now creates one view per page, so a partial unmap hides only the pages it was asked to unmap. The strict `assert_eq!` stays untouched; the test that asserted "the view is still the one view it was" now checks both pages keep their own views, preserving its intent (both still mapped, bytes intact). ## The reference Console behaviour: a page the title was never given up keeps its contents while other pages of the view are remapped (the test's own doc). No new fallback: the single-view path remains for a one-page mapping or an owned placeholder. ## Evidence - Strict test: 5/50 failures on current main, 0/50 after the change (Proton-GE11-16, xwin test). - All 25 xmem tests pass; the full victory-hle lib suite passes except the known NTFS link-limit test (fixed in #105, merged separately). - `tools/build.sh` and a 120 s house-scene validation run under GE-Proton11-16: survived to timeout with only the four known VUIDs. ## Checklist (docs/AGENTS.md) - [x] One topic; `docs/TASKS.md` not edited (rule 29/31) - [x] No fallback added for an impossible case (rule 33) - [x] No constants or switches added (rules 2/9b) - [x] No queues changed (rule 35) - [x] No new `unsafe` (rule 36) - [x] Check, build, tests and validation pass; no translator output change (rule 27) - [x] Comments say what is true now (rule 3) - [x] Nothing from the title, no non-free data pointers (hard constraints)
`unmap` hid the whole multi-page view and re-mapped the kept prefix/suffix,
briefly unmapping pages the title still owned and dropping a concurrent write
(the strict test from #109 failed 5/50 on main). Map each page as its own view,
so a partial unmap touches only the pages asked for; the strict read-back
assertion stays and the affected test checks both pages keep their own views.
0/50 failures, all 25 xmem tests and a 120 s validation run pass.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 14:56:54 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 14:55 UTC · original

Thanks for the new PR, and own is passed through now. 5/50 -> 0/50 on the strict test is a clear result.

Holding the merge for the scale numbers from my #109 comment, because this changes every multi-page mapping the title makes, not just the test's: one view per 64 KiB page is 16,384 views per GiB. On the boat start, main against this branch:

  1. Views at world entry: physical().views.len() logged once, or wc -l /proc/<pid>/maps of the title process.
  2. Time from start to world (the first world present in vk.log).
  3. Frame times, A B B A with VKB_LAT + greenfield/tools/frec/frames.py.

Please also include sysctl vm.max_map_count from your machine. Many distros default to 65,530, and a title that needs more views than that would fail to map on those systems, even where it works on ours (1,048,576 here).

If the numbers are small, it goes in as it is. If the view count is large, the split-on-partial-unmap variant from my #109 comment keeps the common case at one view.

**@⁠vktory6** on GitHub · 2026-12-01 14:55 UTC · [original](https://github.com/vktory6/VKtory/pull/110#issuecomment-7315041309) Thanks for the new PR, and `own` is passed through now. 5/50 -> 0/50 on the strict test is a clear result. Holding the merge for the scale numbers from my #109 comment, because this changes every multi-page mapping the title makes, not just the test's: one view per 64 KiB page is 16,384 views per GiB. On the boat start, main against this branch: 1. **Views at world entry:** `physical().views.len()` logged once, or `wc -l /proc/<pid>/maps` of the title process. 2. **Time from start to world** (the first world present in `vk.log`). 3. **Frame times**, A B B A with `VKB_LAT` + `greenfield/tools/frec/frames.py`. Please also include `sysctl vm.max_map_count` from your machine. Many distros default to 65,530, and a title that needs more views than that would fail to map on those systems, even where it works on ours (1,048,576 here). If the numbers are small, it goes in as it is. If the view count is large, the split-on-partial-unmap variant from my #109 comment keeps the common case at one view.
Author
Owner

@⁠TheRifai on GitHub · 2026-12-01 15:10 UTC · original

Scale numbers (house scene, GE-Proton11-16, VKB_LAT, A B B A): A-main vmas at world 151377/149037, world_at 34s/34s, frame mean 48.1/41.8 ms; B-per-page vmas 209831/210479/209802, world_at 55/36/36s, frame mean 47.4/47.6/45.9 ms. So ~+40% VMAs (150k -> 210k) but no clear frame-time regression; world entry is 34s vs 36s apart from one 55s outlier. vm.max_map_count here = 1048576 (both arms already exceed the common 65530 default). Logs: /tmp/opencode/capture/ab110/. The strict test is 0/50 with the change vs 5/50 on main. I'll leave the merge call to you: per-page views cost ~60k extra VMAs but remove the lost write without changing the strict test.

**@⁠TheRifai** on GitHub · 2026-12-01 15:10 UTC · [original](https://github.com/vktory6/VKtory/pull/110#issuecomment-7315474353) Scale numbers (house scene, GE-Proton11-16, VKB_LAT, A B B A): A-main vmas at world 151377/149037, world_at 34s/34s, frame mean 48.1/41.8 ms; B-per-page vmas 209831/210479/209802, world_at 55/36/36s, frame mean 47.4/47.6/45.9 ms. So ~+40% VMAs (150k -> 210k) but no clear frame-time regression; world entry is 34s vs 36s apart from one 55s outlier. vm.max_map_count here = 1048576 (both arms already exceed the common 65530 default). Logs: /tmp/opencode/capture/ab110/. The strict test is 0/50 with the change vs 5/50 on main. I'll leave the merge call to you: per-page views cost ~60k extra VMAs but remove the lost write without changing the strict test.
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 15:17 UTC · original

Thanks for the numbers, that settles it: main is already at ~150k mappings, so the extra ~60k doesn't change what a system has to allow, and there's no visible frame-time cost. I've added the map-limit requirement to the README. The house scene switches views on its own, so for future frame-time A/Bs the boat start is the more reliable scene.

One thing blocks the merge: CI's policy test fails because crates/victory-hle/src/modules/xmem.rs grows to 2,638 lines, past its listed 2,624 in greenfield/tools/policy/oversize.txt. Rule 4 asks to split a listed file when it grows, not to raise its count. xmem.rs has clear pieces: mod pool or the view bookkeeping (map/map_view/unmap) could become their own file. Could you move one of those out in this PR, so xmem.rs ends below 2,624, and lower its count to the new length? Then it's ready to merge.

**@⁠vktory6** on GitHub · 2026-12-01 15:17 UTC · [original](https://github.com/vktory6/VKtory/pull/110#issuecomment-7315655921) Thanks for the numbers, that settles it: main is already at ~150k mappings, so the extra ~60k doesn't change what a system has to allow, and there's no visible frame-time cost. I've added the map-limit requirement to the README. The house scene switches views on its own, so for future frame-time A/Bs the boat start is the more reliable scene. One thing blocks the merge: CI's policy test fails because `crates/victory-hle/src/modules/xmem.rs` grows to 2,638 lines, past its listed 2,624 in `greenfield/tools/policy/oversize.txt`. Rule 4 asks to split a listed file when it grows, not to raise its count. `xmem.rs` has clear pieces: `mod pool` or the view bookkeeping (`map`/`map_view`/`unmap`) could become their own file. Could you move one of those out in this PR, so `xmem.rs` ends below 2,624, and lower its count to the new length? Then it's ready to merge.
Rule 4: xmem.rs grew past its listed line count after the per-page-view change.
The page-mapping worker pool is already a self-contained module; move it to
xmem/pool.rs and lower the listed count to the new 2521 code lines.
Author
Owner

@⁠TheRifai on GitHub · 2026-12-01 15:23 UTC · original

Done: moved the page-mapping worker pool to xmem/pool.rs and lowered the oversize.txt count to 2521 code lines, pushed to this same branch as a fast-forward commit (no reopen). xmem.rs is now below its listed count; policy test (6), native tests and build pass.

**@⁠TheRifai** on GitHub · 2026-12-01 15:23 UTC · [original](https://github.com/vktory6/VKtory/pull/110#issuecomment-7315823158) Done: moved the page-mapping worker pool to xmem/pool.rs and lowered the oversize.txt count to 2521 code lines, pushed to this same branch as a fast-forward commit (no reopen). xmem.rs is now below its listed count; policy test (6), native tests and build pass.
Resolve oversize.txt: keep main's lower lib.rs/watch.rs counts and this branch's
lower xmem.rs count after moving the worker pool to xmem/pool.rs.
github-bot changed target branch from main to gh-pr/110/base 2026-12-01 15:34:19 +00:00
github manually merged commit 046aa46b93 into gh-pr/110/base 2026-12-01 15:34:20 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 15:30 UTC · original

Merged as 90fd7fa, thank you! The lost write from #109 is fixed, the strict test passes, the scale is measured, and xmem.rs is smaller than before. Good work following it from a flaky test to the cause.

**@⁠vktory6** on GitHub · 2026-12-01 15:30 UTC · [original](https://github.com/vktory6/VKtory/pull/110#issuecomment-7316029356) Merged as 90fd7fa, thank you! The lost write from #109 is fixed, the strict test passes, the scale is measured, and `xmem.rs` is smaller than before. Good work following it from a flaky test to the cause.
This discussion has been locked. Commenting is limited to contributors.
No description provided.