kernelx: unmark lazy away mappings in FreeTitlePhysicalPages #1221

Open
github-bot wants to merge 1 commit from github-bot/gh-1221 into main AGit
Owner

@⁠b3IKK on GitHub · 2026-11-24 13:22 UTC · original
b3IKK:kernelx/free-physical-pages-lazy-unmark → main · +64 −0 · 1 files · 1 commits · open

When a fresh physical page is mapped into a plain MEM_RESERVE region via MapTitlePhysicalPages, map_pages marks that address in lazy::LAZY. Both unmap_away and move_away call lazy::unmark(at), but FreeTitlePhysicalPages removed the entry from AWAY without unmarking it—leaving a stale entry in lazy::LAZY that could silently commit the address (even after VirtualFree and VA reuse) on a later lazy::ensure or page fault, and could also cause silent data loss when another page B is later mapped at r, written, and mapped back home (unmap_away(r) returns early on the stale lazy::unmark(r) without copying r back to B's home).

This calls lazy::unmark(at) when FreeTitlePhysicalPages removes an away mapping.

What is in it

  • crates/xcl/src/hle/kernelx.rs: call lazy::unmark(at) when removing an away mapping in FreeTitlePhysicalPages; add regression tests free_physical_pages_unmarks_lazy_away_mapping and free_physical_pages_stale_lazy_mark_does_not_lose_reused_reservation_writes.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 743 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib free_physical_pages_: 2 passed, 0 failed

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 34035fc1b010450d4c2cb04835fd03c01aabb8fc
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1640 (PresentX 1640)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14479 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited kernelx.rs, wrote the fix and regression tests (including the data-loss sequence suggested in review), ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 13:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/1221) `b3IKK:kernelx/free-physical-pages-lazy-unmark` → `main` · +64 −0 · 1 files · 1 commits · open When a fresh physical page is mapped into a plain `MEM_RESERVE` region via `MapTitlePhysicalPages`, `map_pages` marks that address in `lazy::LAZY`. Both `unmap_away` and `move_away` call `lazy::unmark(at)`, but `FreeTitlePhysicalPages` removed the entry from `AWAY` without unmarking it—leaving a stale entry in `lazy::LAZY` that could silently commit the address (even after `VirtualFree` and VA reuse) on a later `lazy::ensure` or page fault, and could also cause silent data loss when another page B is later mapped at `r`, written, and mapped back home (`unmap_away(r)` returns early on the stale `lazy::unmark(r)` without copying `r` back to B's home). This calls `lazy::unmark(at)` when `FreeTitlePhysicalPages` removes an away mapping. **What is in it** - `crates/xcl/src/hle/kernelx.rs`: call `lazy::unmark(at)` when removing an away mapping in `FreeTitlePhysicalPages`; add regression tests `free_physical_pages_unmarks_lazy_away_mapping` and `free_physical_pages_stale_lazy_mark_does_not_lose_reused_reservation_writes`. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 743 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib free_physical_pages_`: 2 passed, 0 failed **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 34035fc1b010450d4c2cb04835fd03c01aabb8fc gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1640 (PresentX 1640) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14479 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `kernelx.rs`, wrote the fix and regression tests (including the data-loss sequence suggested in review), ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
When a fresh physical page is mapped into a plain MEM_RESERVE region
via MapTitlePhysicalPages, map_pages marks that address in lazy::LAZY.
unmap_away and move_away both call lazy::unmark(at), but
FreeTitlePhysicalPages removed the entry from AWAY without unmarking
it, leaving a stale entry in lazy::LAZY that could silently commit the
address (even after VirtualFree / VA reuse) on a later ensure or fault.

Call lazy::unmark(at) when FreeTitlePhysicalPages removes an away
mapping, with a regression test.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:47:32 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 13:47 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib kernelx: 33 passed, 0 failed.

Note: the full d3d12x lib filter currently aborts with 0xc0000005 on unmodified main too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.

**@⁠DavidM1911** on GitHub · 2026-11-24 13:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/1221#issuecomment-7133338420) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib kernelx: 33 passed, 0 failed. Note: the full `d3d12x` lib filter currently aborts with 0xc0000005 on unmodified `main` too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.
Author
Owner

@⁠pvfreis on GitHub · 2026-11-24 14:23 UTC · original

The fix is right, and on main (930e8b4) the stale mark does more than commit r later: it loses data. Read only; nothing built or run here. Sequence, all in kernelx.rs on main:

  1. A never-used page A is mapped at a reservation r away from home: lazy::mark(r) (:1284-1287). FreeTitlePhysicalPages([A]) drops the mapping but not the mark (:1169-1175).
  2. Page B is mapped home and written, then mapped at r: r is committed and B's home is copied to it (:1290-1296); the home is given back at the end of the call. r is committed, so nothing faults and the mark stays.
  3. The title writes at r, then maps B home again: commit_homes brings the home back as zeros, and unmap_away(r) returns early because lazy::unmark(r) is true (:1061-1066), so r is never copied home. B reads zeros.

A test of that sequence (assert B's home holds what was written at r) fails on main and should pass with the one-line fix here, which would cover the data loss as well as the stray commit your test checks.

Reply by Claude Code (Claude Opus 5.5); checked by a person before posting.

**@⁠pvfreis** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/xcl-dev/xcl/pull/1221#issuecomment-7134301767) The fix is right, and on `main` (`930e8b4`) the stale mark does more than commit `r` later: it loses data. Read only; nothing built or run here. Sequence, all in `kernelx.rs` on `main`: 1. A never-used page A is mapped at a reservation `r` away from home: `lazy::mark(r)` (`:1284-1287`). `FreeTitlePhysicalPages([A])` drops the mapping but not the mark (`:1169-1175`). 2. Page B is mapped home and written, then mapped at `r`: `r` is committed and B's home is copied to it (`:1290-1296`); the home is given back at the end of the call. `r` is committed, so nothing faults and the mark stays. 3. The title writes at `r`, then maps B home again: `commit_homes` brings the home back as zeros, and `unmap_away(r)` returns early because `lazy::unmark(r)` is true (`:1061-1066`), so `r` is never copied home. B reads zeros. A test of that sequence (assert B's home holds what was written at `r`) fails on `main` and should pass with the one-line fix here, which would cover the data loss as well as the stray commit your test checks. Reply by Claude Code (Claude Opus 5.5); checked by a person before posting.
Author
Owner

@⁠b3IKK on GitHub · 2026-11-24 18:54 UTC · original

Great catch @⁠pvfreis — added \ ree_physical_pages_stale_lazy_mark_does_not_lose_reused_reservation_writes\ in \34035fc\ covering that exact page A -> free -> page B away write -> map home data-loss sequence, and re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.

**@⁠b3IKK** on GitHub · 2026-11-24 18:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/1221#issuecomment-7141310338) Great catch @⁠pvfreis — added \ ree_physical_pages_stale_lazy_mark_does_not_lose_reused_reservation_writes\ in \34035fc\ covering that exact page A -> free -> page B away write -> map home data-loss sequence, and re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 04:05 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 04:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/1221#issuecomment-7182286307) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1221/head:github-bot/gh-1221'
git switch 'github-bot/gh-1221'

Merge

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