Skip to content

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

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:kernelx/contiguous-physical-page-home
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:kernelx/contiguous-physical-page-home

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@pvfreis pvfreis left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 81d047a against its merge base 47efa2e (the head of main when this was read), by reading the code; nothing was built or run here, and no script of my own was run. The PR had no comments or reviews when read, so nothing below repeats an earlier point. The bug it describes is real on main: map_pages took page_home(pages[0]) as the home of the whole array when the numbers were consecutive and the last page had any home (crates/xcl/src/hle/kernelx.rs:1223-1224 on main), and AllocateTitlePhysicalPages numbers pages from one global counter (:1171), so two allocations with consecutive numbers were committed and returned as [H1, H1 + n * PHYS_PAGE) by commit_homes (:995-1012). contiguous_home_in (:859-868) closes that: I found no defect in its arithmetic or its bounds (one page, page 0, a non-multiple of FRAMES, an index past the block, a block with nothing live all return None), and it takes PHYS_BLOCKS under AWAY in the same order page_home already does.

  1. CONFIRMED by tracing the code (minor): with an address given, the same array now takes the "mapped away" path at its own home (kernelx.rs:1241, then :1271-1322). The description speaks only of NULL, but home == Some(va) changes too. Scenario: MapTitlePhysicalPages(H1, 2, [p1, p2]) with p1, p2 from two allocations and H1 the home of p1 in a sparse window. home is now None, so the loop runs with at == H1: H1 is committed (:1306), copy_nonoverlapping(home, at) is called with the same range as source and destination (:1311; undefined behaviour for copy_nonoverlapping, though harmless in practice), move_home_away queues H1 (:1312, :946-948), p1 is recorded as away at H1 (:1321-1322), and give_back_homes decommits H1 when the call ends (:1222, :973-976): the page is recorded at an address that was just decommitted. main has the same path for non-consecutive pages mapped at the first one's home, so this is not new in kind; the PR sends one more case into it (which on main returned the wrong memory for p2 instead). Fix: in the loop, treat at == page_home(p) as "at home" (commit the home, no copy, no move_home_away), or say under Not tested that the address-given case is not covered; the new test only calls with NULL (:2171).

  2. Overlap with open PRs on the same functions.

    • #608 (2a3c7f4) changes the value type of PHYS_BLOCKS from the tuple to a PhysicalBlock with a per-page freed bitmap. contiguous_home_in takes &BTreeMap<usize, (usize, usize, usize)> (:859) and its unit test builds tuples (:2194-2198), so whichever merges second has to be rebased.
    • #1055 and #1102 add away.title.insert(home + i * PHYS_PAGE, p) to the at-home branch of map_pages, the branch whose condition this PR changes.
    • No other open or merged PR found for this defect (searched "physical pages", MapTitlePhysicalPages, page_home, PHYS_BLOCKS, FreeTitlePhysicalPages).

Question: when two allocations' homes happen to be adjacent, main mapped MapTitlePhysicalPages(NULL, n, ...) over both and every page was at H1 + i * PHYS_PAGE; this head refuses that with ERROR_NOT_SUPPORTED (kernelx.rs:867, asserted at :2197-2198, :2206). Is refusing that case intended? If so, it would help to say so in the description.

About the test: consecutive_pages_from_separate_allocations_do_not_map_as_one_home returns without checking anything when unified memory is absent (:2140), and panics rather than skips if 32 tries do not produce the layout (:2167). It does test what the description claims: on main the call at :2171 returns h1.

Against CONTRIBUTING: the sections are all there, both xcl-smoke blocks name the right commits (47efa2e on main, 81d047a for the head), 101 added / 3 removed lines in one file, one commit with the subject in the repository's style. Only the PR description check had reported on this head when read; CI had not.

Verdict: the fix is correct for the case it describes and small enough to merge; finding 1 is minor and can be a follow-up, and #608 needs a rebase on one side.

Review by Claude Code (written by Claude Fable 5.2, checked and edited with Claude Opus 5.5); each point checked by a person before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

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.

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)
@b3IKK
b3IKK force-pushed the kernelx/contiguous-physical-page-home branch from 81d047a to 076d027 Compare November 24, 2026 13:58
@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @pvfreis for the thorough review! Addressed in \

@pvfreis

pvfreis commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

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

@JoaoPauluu

Copy link
Copy Markdown

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 Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #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.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 25, 2026
Port upstream xcl-dev#1143 (head 076d027): add contiguous_home_in /
contiguous_home so consecutive page numbers resolve to a single host
block only when they belong to one live PHYS_BLOCKS entry. The
NULL-address MapTitlePhysicalPages path copies that block whole and
falls back to the per-page loop otherwise, so consecutive numbers from
two allocations never merge into one block over unrelated memory.
Add contiguous_home_requires_one_allocation (ported) plus an end-to-end
test for one-block mapping, cross-allocation split, and unmap/free
neighbor preservation. The upstream home == at handling has no twin
counterpart (Linux homes are private malloc, targets always gfx-pool).
@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants