Skip to content

kernelx: handle self-home/reused-home mappings in map_pages and unmap away pages on VirtualFree - #2544

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-kernelx-map-pages-virtualfree
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-kernelx-map-pages-virtualfree

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

Fixes two physical-page mapping bookkeeping bugs in crates/xcl/src/hle/kernelx.rs: self-home / reused-home decommits and UB in map_pages, and dangling AWAY / LAZY entries left behind when VirtualFree decommits or releases sparse UMA or plain MEM_PRIVATE reservations. Part of #1833, Part of #1566, Part of #697.

What is in it

  • crates/xcl/src/hle/kernelx.rs (map_pages): resolve page_home(p) safely right after skipping p == 0; when a non-consecutive multi-page mapping places a page at its own home (at == home), commit the home via commit_homes(home, 1), drop home from away.gone, unmap any prior away mapping directly back to home, and skip the away-copy path (avoiding ptr::copy_nonoverlapping(home, home, ...) UB, bogus (home, p) AWAY insertion, and subsequent give_back_homes decommit).
  • crates/xcl/src/hle/kernelx.rs (map_pages): remove at from away.gone whenever another page is mapped onto at (across move_away, the lazy path, and the commit/copy path before move_home_away) so give_back_homes never decommits a reused home within the same multi-page or XMemMapPhysicalPagesScatter call.
  • crates/xcl/src/hle/kernelx.rs (VirtualFree, unmap_away_in, unmap_away_range, plain_free_range, unmap_away_for_plain_free): unmap overlapping AWAY/LAZY pages (copying committed data back to home and clearing lazy marks) before decommitting or releasing sparse UMA reservations or validated plain MEM_PRIVATE reservations, with a zero-syscall fast path when no away candidate overlaps and strict VirtualQuery validation so invalid VirtualFree calls leave AWAY intact.
  • crates/xcl/src/hle/kernelx.rs (hle::kernelx::tests): added 3 pure-CPU 0-skip unit tests (map_pages_at_own_home_preserves_data_without_away_entry, scatter_reusing_moved_home_does_not_decommit_new_mapping, and virtual_free_unmaps_away_pages_in_plain_and_sparse_reservations).

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: Windows 11 Home (build 26200), NVIDIA GeForce RTX 4060 8 GB (driver 621.14, 32-lane waves), Intel Core i5-11400F (6C/12T), 32 GB RAM, rustc 1.100.0, DXC 1.9.2609.5, --release.
  • Failing-first on unmodified e1d84b7d: all 3 new hle::kernelx::tests failed cleanly (0 passed; 3 failed: self-home page must remain committed, reused home (from home) must stay committed, partial plain MEM_DECOMMIT must unmap away page).
  • Post-fix targeted cargo test --release -p xcl --lib -- hle::kernelx::tests:: --nocapture: 43 passed, 0 failed, 1 ignored (scatter_timing), 0 skips on both RTX 4060 and XCL_ADAPTER=warp.
  • cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture: 37 test binaries, 1426 passed, 0 failed, 46 ignored (SKIPPED GPU/host tests: 29 pre-existing on 32-lane RTX 4060 without D3D12 debug layer — 21 no wave64, 3 per-lane wave32 refused, 3 no D3D12 debug layer, 1 no heap windows, 1 cannot disable driver cache; 0 skipped in hle::kernelx::tests).

Regression run

main:

xcl-smoke v1
commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1540 (PresentX 1540)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 13969 MB

PR:

xcl-smoke v1
commit: 19abb354ddcbc98bec7b6a2eb54e4d18ac9ab9df
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1560 (PresentX 1561)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14019 MB

Not tested

  • 64-lane AMD hardware (wave64 GPU tests skipped on 32-lane RTX 4060) and D3D12 debug-layer tests (SDK layers DLL not installed on host).
  • scatter_timing (#[ignore] benchmark test in hle::kernelx::tests).

AI use

  • Google Antigravity (Gemini 4 Neon) implemented the map_pages and VirtualFree fixes, added the 3 unit tests, verified failing-first and post-fix test suites and paired 60 s smoke runs, and drafted this PR description.

… away pages on VirtualFree

Fix two physical-page bookkeeping bugs in kernelx:

1. map_pages self-home and reused-home handling:
   - In non-consecutive multi-page MapTitlePhysicalPages calls where a page maps
     to its own home (at == home), commit the home, clear it from away.gone, and
     unmap any prior away mapping directly back to home instead of falling into
     ptr::copy_nonoverlapping(home, home, ...) and queuing the live home in
     away.gone for decommit.
   - Whenever a page is mapped onto an address at (via move_away, lazy, or the
     commit/copy path before move_home_away), clear at from away.gone so
     give_back_homes never decommits a home reused within the same multi-page
     or scatter call.

2. VirtualFree unmapping of away-mapped physical pages:
   - Unmap overlapping AWAY/LAZY entries (preserving committed bytes back to
     home and clearing lazy marks) before decommitting or releasing sparse UMA
     or plain MEM_PRIVATE reservations.
   - Validate plain MEM_PRIVATE ranges with a zero-syscall fast path when no
     away candidate overlaps, so invalid VirtualFree calls leave AWAY intact.

Part of xcl-dev#1833
Part of xcl-dev#1566
Part of xcl-dev#697

Assisted-by: Google Antigravity (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 19abb35 (on its base e1d84b7).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 1414 passed, 0 failed, 44 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, dispatch_chain_native_wave64_builds_whole, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: map_pages_at_own_home_preserves_data_without_away_entry, scatter_reusing_moved_home_does_not_decommit_new_mapping, virtual_free_unmaps_away_pages_in_plain_and_sparse_reservations.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 1425 passed, 0 failed, 46 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main e1d84b7 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests, const_and_null_mask_operands_agree_with_hlsl_on_the_gpu and mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@xpatfussy69x

Copy link
Copy Markdown

Test run on exact head 19abb35 (19abb354ddcbc98bec7b6a2eb54e4d18ac9ab9df, on its base e1d84b7, current main), clean tree.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (driver 32.0.16.2124, 32-lane waves only), AMD Ryzen 5 7500F, Windows 11, Rust 1.100.0; debug build for the tests, release for the smoke runs
  • cargo test --workspace -- --test-threads=4: 1426 passed, 0 failed, 46 ignored
  • SKIPPED (25): 21 × no wave64; 1 × no heap windows; 3 × per-lane form refused V_MUL_F16 / V_PK_MUL_F16 / V_CVT_F16_F32 (on main too)
  • cargo test -p xcl --lib map_pages_at_own_home: 1 passed, 0 failed
  • This PR's new tests in the workspace run:
    • map_pages_at_own_home_preserves_data_without_away_entry: ok
    • scatter_reusing_moved_home_does_not_decommit_new_mapping: ok
    • virtual_free_unmaps_away_pages_in_plain_and_sparse_reservations: ok

Regression run (60 s, default settings, a retail title)

main:

xcl-smoke v1
commit: e1d84b7d9153f86a97d2ec5d209d196046fc0bea
gpu: NVIDIA GeForce RTX 3060
title: c3f791f3
seconds: 60
exit: 0x0
frames shown: 1091 (PresentX 1091)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14814 MB

this PR:

xcl-smoke v1
commit: 19abb354ddcbc98bec7b6a2eb54e4d18ac9ab9df
gpu: NVIDIA GeForce RTX 3060
title: c3f791f3
seconds: 60
exit: 0x0
frames shown: 1152 (PresentX 1152)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14791 MB

No crash or device removal; frames 106 % of main's, peak commit 100 %.

Not tested

  • Whether the title maps pages at their own home or reuses a moved home in this run: a regression check only
  • AMD / native wave64

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and both smoke runs on the machine above and wrote this report.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of map_pages_at_own_home_preserves_data_without_away_entry, scatter_reusing_moved_home_does_not_decommit_new_mapping, virtual_free_unmaps_away_pages_in_plain_and_sparse_reservations: 3 passed. CPU-side, fine under the Wine lane.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@luckyteagirl luckyteagirl 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.

Could the plain MEM_DECOMMIT path preserve host pages outside the requested range? With a committed 64 KiB away mapping in a plain MEM_PRIVATE reservation, VirtualFree(base + 4096, 4096, MEM_DECOMMIT) now selects the enclosing away page. unmap_away calls release_at, which decommits the full PHYS_PAGE (64 KiB) before the wrapper forwards the original 4 KiB call. On main, the plain path forwards that call directly to native VirtualFree.

I checked the Windows granularity separately: decommitting only the middle 4 KiB of a committed 64 KiB reservation left the 0/4K/8K offsets MEM_COMMIT / MEM_RESERVE / MEM_COMMIT; decommitting the full 64 KiB left all three MEM_RESERVE. Could this path clear the away bookkeeping and preserve its home copy while letting the native call retire only the requested plain pages? The added “partial” test requests PHYS_PAGE, so a sub-page decommit with committed sentinels on both sides would cover this difference.

Exact-head source trace at 19abb354ddcbc98bec7b6a2eb54e4d18ac9ab9df and native Win32 API probe only; I did not run XCL's wrapper or a title. AI-assisted review with Codex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants