kernelx: require contiguous physical pages to share one allocation #1143

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

@⁠b3IKK on GitHub · 2026-11-24 12:32 UTC · original
b3IKK:kernelx/contiguous-physical-page-home → main · +147 −4 · 1 files · 1 commits · open

AllocateTitlePhysicalPages assigns consecutive page numbers from a global counter (NEXT_PAGE_INDEX) even across separate allocations whose home blocks are not adjacent in virtual memory (e.g. separated by another UMA allocation or falling back to plain memory). map_pages only checked that page numbers were consecutive and that pages[n - 1] had a home, so MapTitlePhysicalPages(NULL, ...) on pages spanning two separate allocations committed and returned [H1, H1 + n * PHYS_PAGE), overlapping unrelated memory next to the first block.

This adds contiguous_home_in / contiguous_home to require that all consecutive pages belong to one live PHYS_BLOCKS entry (using checked arithmetic) before treating them as a single contiguous home block. Refusing two separate allocations even when their home addresses happen to be adjacent is intentional: commit_homes (VirtualAlloc(..., MEM_COMMIT)), lazy_home, and is_sparse assume a single backing reservation of one memory kind.

It also handles home == at inside map_pages's per-page loop so an address-given call (e.g. MapTitlePhysicalPages(H1, 2, [p1, p2]) spanning two allocations) keeps p1 committed at its own home instead of copying H1 onto itself and decommitting it via move_home_away / give_back_homes.

What is in it

  • crates/xcl/src/hle/kernelx.rs: add contiguous_home_in / contiguous_home and use it in map_pages; handle home == at inside map_pages's per-page loop; add consecutive_pages_from_separate_allocations_do_not_map_as_one_home, contiguous_home_requires_one_allocation, and mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed.

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): 754 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: consecutive_pages_from_separate_allocations_do_not_map_as_one_home, contiguous_home_requires_one_allocation, and mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed all pass

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: 076d027b706764e5e45c2c7f68fdb6a8a550e3e5
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14471 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, addressed @⁠pvfreis's review finding on home == at in the per-page loop, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 12:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143) `b3IKK:kernelx/contiguous-physical-page-home` → `main` · +147 −4 · 1 files · 1 commits · open `AllocateTitlePhysicalPages` assigns consecutive page numbers from a global counter (`NEXT_PAGE_INDEX`) even across separate allocations whose home blocks are not adjacent in virtual memory (e.g. separated by another UMA allocation or falling back to plain memory). `map_pages` only checked that page numbers were consecutive and that `pages[n - 1]` had a home, so `MapTitlePhysicalPages(NULL, ...)` on pages spanning two separate allocations committed and returned `[H1, H1 + n * PHYS_PAGE)`, overlapping unrelated memory next to the first block. This adds `contiguous_home_in` / `contiguous_home` to require that all consecutive pages belong to one live `PHYS_BLOCKS` entry (using checked arithmetic) before treating them as a single contiguous home block. Refusing two separate allocations even when their home addresses happen to be adjacent is intentional: `commit_homes` (`VirtualAlloc(..., MEM_COMMIT)`), `lazy_home`, and `is_sparse` assume a single backing reservation of one memory kind. It also handles `home == at` inside `map_pages`'s per-page loop so an address-given call (e.g. `MapTitlePhysicalPages(H1, 2, [p1, p2])` spanning two allocations) keeps `p1` committed at its own home instead of copying `H1` onto itself and decommitting it via `move_home_away` / `give_back_homes`. **What is in it** - `crates/xcl/src/hle/kernelx.rs`: add `contiguous_home_in` / `contiguous_home` and use it in `map_pages`; handle `home == at` inside `map_pages`'s per-page loop; add `consecutive_pages_from_separate_allocations_do_not_map_as_one_home`, `contiguous_home_requires_one_allocation`, and `mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed`. **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`): 754 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`: `consecutive_pages_from_separate_allocations_do_not_map_as_one_home`, `contiguous_home_requires_one_allocation`, and `mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed` all pass **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: 076d027b706764e5e45c2c7f68fdb6a8a550e3e5 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1642 (PresentX 1642) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14471 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, addressed @⁠pvfreis's review finding on `home == at` in the per-page loop, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
AllocateTitlePhysicalPages assigns consecutive page numbers from a
global counter (NEXT_PAGE_INDEX) even across separate allocations whose
home blocks are not adjacent in virtual memory. map_pages only checked
that page numbers were consecutive and that the last page had a home,
so MapTitlePhysicalPages(NULL, ...) on pages from two separate
allocations committed and returned the first block's home over
unrelated memory next to it.

Check that all pages in the slice fall within one live PHYS_BLOCKS
entry (contiguous_home) with checked arithmetic before treating them as
a single contiguous home block (even when two separate allocations
happen to land adjacent, commit_homes / VirtualAlloc(MEM_COMMIT) and
lazy_home / is_sparse assume a single backing reservation).

Also handle `home == at` inside map_pages's per-page loop so an
address-given call that maps a page at its own home keeps it committed
at home instead of copying it onto itself and decommitting it via
move_home_away / give_back_homes.

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

@⁠DavidM1911 on GitHub · 2026-11-24 12:53 UTC · original

Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib kernelx: 34 passed, 0 failed, 1 ignored — the contiguous-physical-pages change is covered by the kernelx suite (scatter_matches_a_model_of_physical_pages, physical_pages_in_uma, etc.).

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

**@⁠DavidM1911** on GitHub · 2026-11-24 12:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143#issuecomment-7132067276) Independent verification on current `main` `384f7f7` (branch already contains it — clean). **Machine:** Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug. - `cargo test -p xcl --lib kernelx`: **34 passed, 0 failed, 1 ignored** — the contiguous-physical-pages change is covered by the kernelx suite (`scatter_matches_a_model_of_physical_pages`, `physical_pages_in_uma`, etc.). AI use: test runs by Devin (Cognition, SWE-2); no code changes.
Author
Owner

@⁠b3IKK on GitHub · 2026-11-24 13:58 UTC · original

Thanks @⁠pvfreis for the thorough review! Addressed in \

**@⁠b3IKK** on GitHub · 2026-11-24 13:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143#issuecomment-7133633345) Thanks @⁠pvfreis for the thorough review! Addressed in \
Author
Owner

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

Follow-up at 076d027 (rebased onto 930e8b4). I read the diff only; nothing was built or run here.

  • Point 1 is fixed. In the per-page loop, a page whose home is at (kernelx.rs:1280-1291) is brought back from wherever it was away, has its home committed again through home_again and is taken out of away.gone. It is not recorded in AWAY. So the page is no longer copied onto itself, and its home is not decommitted at the end of the call. mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed (:2229) asserts that the page stays committed, keeps its data and is not in AWAY. Without the new branch, the assertion at :2252 would fail.
  • My question is answered: the description now says the refusal of adjacent homes from separate allocations is intentional.
  • The overlaps still apply. #608 changes the PHYS_BLOCKS value type that contiguous_home_in (:859) takes. #1055 and #1102 remove at from away.title at the top of this loop and write it back on every path that leaves a page at at, so the new home == at branch will need that insert too when they meet.

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

**@⁠pvfreis** on GitHub · 2026-11-24 14:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143#issuecomment-7133851724) Follow-up at `076d027` (rebased onto `930e8b4`). I read the diff only; nothing was built or run here. - Point 1 is fixed. In the per-page loop, a page whose home is `at` (`kernelx.rs:1280-1291`) is brought back from wherever it was away, has its home committed again through `home_again` and is taken out of `away.gone`. It is not recorded in `AWAY`. So the page is no longer copied onto itself, and its home is not decommitted at the end of the call. `mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed` (`:2229`) asserts that the page stays committed, keeps its data and is not in `AWAY`. Without the new branch, the assertion at `:2252` would fail. - My question is answered: the description now says the refusal of adjacent homes from separate allocations is intentional. - The overlaps still apply. #608 changes the `PHYS_BLOCKS` value type that `contiguous_home_in` (`:859`) takes. #1055 and #1102 remove `at` from `away.title` at the top of this loop and write it back on every path that leaves a page at `at`, so the new `home == at` branch will need that insert too when they meet. Reply by Claude Code (Claude Opus 5.5); checked by a person before posting.
Author
Owner

@⁠JoaoPauluu on GitHub · 2026-11-24 14:34 UTC · original

A run on Linux under Wine.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 076d027 as it is (based on 930e8b4), no uncommitted changes. The lib test binary runs under Wine, one test per process (--exact). The page and memory calls under kernelx are Wine's here.
  • hle::kernelx::tests::consecutive_pages_from_separate_allocations_do_not_map_as_one_home: ok, no SKIPPED.
  • hle::kernelx::tests::contiguous_home_requires_one_allocation: ok, no SKIPPED.
  • hle::kernelx::tests::mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed: ok, no SKIPPED.

Not tested

  • whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows.

AI use

  • Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
**@⁠JoaoPauluu** on GitHub · 2026-11-24 14:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143#issuecomment-7134607483) A run on Linux under Wine. **Tested** - RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for `x86_64-pc-windows-gnu`. `076d027` as it is (based on `930e8b4`), no uncommitted changes. The lib test binary runs under Wine, one test per process (`--exact`). The page and memory calls under kernelx are Wine's here. - `hle::kernelx::tests::consecutive_pages_from_separate_allocations_do_not_map_as_one_home`: **ok**, no SKIPPED. - `hle::kernelx::tests::contiguous_home_requires_one_allocation`: **ok**, no SKIPPED. - `hle::kernelx::tests::mapping_page_at_its_own_home_in_mixed_call_keeps_it_committed`: **ok**, no SKIPPED. **Not tested** - whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows. **AI use** - Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 03:41 UTC · original

Re-verification on RTX 3060 Ti (Windows 11): still a merge conflict with current main on the new head — needs another rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-26 03:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/1143#issuecomment-7181934029) Re-verification on RTX 3060 Ti (Windows 11): still a **merge conflict with current `main`** on the new head — needs another rebase before it can be tested.
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/hle/kernelx.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1143/head:github-bot/gh-1143'
git switch 'github-bot/gh-1143'

Merge

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