Skip to content

kernelx: keep physical page release local to the page ID - #3608

Merged
throny merged 2 commits into
xcl-dev:mainfrom
Foniass:ready/608
Dec 1, 2026
Merged

throny merged 2 commits into
xcl-dev:mainfrom
Foniass:ready/608

Conversation

@Foniass

@Foniass Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Replaces #608 so it can be merged as is: it carries Babyhamsta's commit from #608, rebased onto main with the conflict resolved (author kept), plus one test-only commit. All credit for the fix goes to Babyhamsta.

Freeing one physical page twice could retire a sibling the title still owned. A freed page could also continue resolving to its home until the last page in the allocation was released. Related PR #187 addressed when XCL returns backing memory.

Rebased onto main 6bfb5c51. Since this PR's old base, main added the mirrors bookkeeping (mirrors::page_freed), the lazy unmark from #1221 and source_going to FreeTitlePhysicalPages. The rebase keeps all three, run once for each page that the call frees. A repeated free of a page that is already freed now does nothing.

What is in it

  • kernelx: keep physical page release local to the page ID (Babyhamsta, crates/xcl/src/hle/kernelx.rs): released page IDs are tracked with a bitmap per allocation. page_home (and with it map, read, write, physical_pages_live and the GPU-view page record) refuses a freed ID while its siblings are live. FreeTitlePhysicalPages takes AWAY and then PHYS_BLOCKS, the same order as map and read, and marks the pages freed in the bitmap. It then drops the away mapping, the lazy mark, the GPU-view pages and the Away::title entries, only for the pages this call freed. A block's home is released after both locks are dropped. Repeated frees keep returning 1. Tests: the bitmap across a word boundary, and the read, write and map of a freed page and its live siblings.
  • Tests: a page freed while mapped away loses its mapping, its block stays (crates/xcl/src/hle/kernelx.rs, test only): maps one page away from home, frees another page twice, then frees the mapped page. It checks that physical_page_at clears, that the address keeps its bytes, and that the last page still reads. This covers the review question about the away-mapping cleanup.

Tested

  • RTX 5070 Ti (NVIDIA 621.14), Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug for the tests, release for the smoke runs. Head 95b00f6f, clean tree.
  • cargo build --workspace: ok, no new warnings.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 2907 passed, 0 failed, 51 ignored across 43 test binaries (xcl lib: 2759 passed, 0 failed, 31 ignored). The 3 physical-page tests ran on the GPU (no SKIPPED).
  • 28 SKIPPED on this adapter, all outside kernelx:
    • 22 no wave64 (the gpu::recomp::lower / lower_dxil GPU comparisons, dispatch_chain_native_wave64_builds_whole, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware);
    • 2 no heap windows;
    • 2 no d3d12SDKLayers.dll (debug-layer title runs);
    • 1 no D3D12 debug layer;
    • 1 driver cache (needs Developer Mode).
  • Fails on main, passes here. Both runtime tests were run on main with only the tests added:
    • repeated_free_then_freeing_a_page_mapped_away_keeps_the_last_sibling fails at the page still allocated keeps its block: on main, the repeated free plus one more free releases the block while a page is still allocated.
    • repeated_physical_page_release_preserves_live_siblings fails at !read_physical_pages(&[first], 0, &mut bytes): a freed page is still read.
  • Mutation: with away.at.remove(&at) taken out of FreeTitlePhysicalPages, the new test fails at a freed page is no longer mapped there, and the other kernelx tests pass.
  • Retail title, release, 60 s, main first: the PR showed 1594 frames against main's 1548 (103 %), with a peak commit of 18002 MB against 18726 MB. 0 device removals and 0 crashes in both runs. The title made tens of thousands of physical-page map calls in both runs (MapTitlePhysicalPages / XMemMapPhysicalPagesScatter, which go through the changed page_home), and the queue's page-mapping calls ran too.

Regression run

main:

xcl-smoke v1
commit: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1548 (PresentX 1548)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18726 MB

this PR's head:

xcl-smoke v1
commit: 95b00f6f54b635f86453e58e3fac7b67056c26a2
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1594 (PresentX 1594)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18002 MB

Not tested

  • Whether the title frees physical pages in these runs, or frees one twice: XCL logs no count of FreeTitlePhysicalPages calls, so the runs show only that the map paths still work.
  • AMD and Intel GPUs. An earlier AMD RDNA4 run on the old head is in this PR's comments.
  • Playable world loading.
  • Two threads freeing and mapping at once. The lock order (AWAY → PHYS_BLOCKS, no other lock taken under PHYS_BLOCKS) was checked by reading the code only.

AI use

Codex (GPT-6.2 Sol) implemented the fix and regression tests. Codex (GPT-6) reviewed the diff, checked related PRs, ran the smoke comparison, and prepared the original description. For Foniass, Claude Code (Claude Opus 5.5) did the rebase onto main and resolved the conflict in FreeTitlePhysicalPages. It also reviewed the rebased code, wrote the added test commit, ran the test suite, the fail-on-main check, the mutation check and the smoke pair above, and updated this description.

🤖 Generated with Claude Code

Babyhamsta and others added 2 commits December 1, 2026 19:38
Track released IDs within each allocation so a duplicate cannot consume a live sibling. Reject released IDs in home lookups while preserving repeated lookup of live pages.

Assisted-by: Codex (GPT-6.2 Sol)
The previous commit drops a freed page's mapping away from home only when
that free released the page, and no kernelx test reached that path (a
review question on xcl-dev#608). This test maps one page of a block away from
home, frees another page twice, then frees the mapped page: the mapping
goes (the address keeps its bytes), and the block stays for the page
still allocated. On main the repeated free releases that page's block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH label Dec 1, 2026
@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: 95b00f6

@github-actions github-actions Bot added the needs-1st-check Passes gates; needs first independent merge-check (#3041) label Dec 1, 2026
@fromthemoonx

fromthemoonx commented Dec 1, 2026 •

Copy link
Copy Markdown

merge-check
base: 6bfb5c5
head: 95b00f6
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2907 passed, 0 failed, 51 ignored (base: 2904 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none
review: read the whole diff (both commits, kernelx.rs, +159 -35). No correctness problem found.

  • PhysicalBlock keeps a freed bitmap per allocation. A second free of a page does nothing (free_page returns false), so live no longer drops for it, and page_home refuses a freed page while its siblings live.
  • Locks: FreeTitlePhysicalPages is the only place that holds both AWAY and PHYS_BLOCKS, in that order. page_home and AllocateTitlePhysicalPages take PHYS_BLOCKS alone and briefly, and map / read take AWAY before calling page_home. So I see no lock-order inversion. The block homes are released after both locks are dropped.
  • The away cleanup (lazy::unmark, note_mapping_at, mirrors::page_freed, Away::title) now runs only for pages this call freed. Before, it ran for every page in the array, including repeats.
  • The three new tests, 30 of 30 alone each, none skipped (UMA here):
    • physical_page_liveness_crosses_bitmap_words
    • repeated_physical_page_release_preserves_live_siblings
    • repeated_free_then_freeing_a_page_mapped_away_keeps_the_last_sibling
  • Mutation: free_page without its already-freed check (a repeat free counts again, as on main) fails all three. Good.
  • hle::kernelx::tests:: at --test-threads=8, 30 runs on each tree. The failures are the same tests on both, so not this PR:
    • this merge: 24 of 30 ok, 3 ended in a segmentation fault; the others failed a_page_whose_move_fails_stays_mapped_where_it_was (4) and an_address_keeps_the_page_the_title_mapped_there_last (1);
    • main 6bfb5c5: 22 of 30 ok, 1 segmentation fault; the others failed a_page_whose_move_fails_stays_mapped_where_it_was (4), an_address_keeps_the_page_the_title_mapped_there_last (4) and changed_gpu_view_pages_are_told_once_in_order (1).
  • Babyhamsta's commit 8ad21c61 keeps its author.
  • No retail title on this machine.
    performance: FreeTitlePhysicalPages now holds AWAY while it walks the blocks; one bitmap check per freed page. Not measured.
    verdict: safe to merge
    hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Dec 1, 2026
@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: f07e426
head: 95b00f6 (same as the claim)
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2907 passed, 0 failed, 51 ignored; 25 SKIPPED lines (no wave64 / heap windows / driver-cache test on this adapter), no test binary ended abnormally
review: none found after reading the whole diff (248 lines). PHYS_BLOCKS entries move from a (count, live, home) tuple to a PhysicalBlock with a per-page freed bitmap, so freeing a page twice (or a page already freed) no longer decrements the live count and no longer frees siblings or the block early; page_home now checks the single page. FreeTitlePhysicalPages takes AWAY then PHYS_BLOCKS (stated as the same order as map/read; I did not verify that every other site uses it, a deadlock risk if one takes them reversed), collects freed pages first, then unmaps and releases the block homes after dropping both locks. Behaviour change: pages not live are skipped, so away/mirror cleanup now runs only for pages actually freed. Tests: a unit test of the bitmap across word boundaries (runs everywhere) and two integration tests through the real Allocate/Free/Map entry points, which return early with skip_test("no UMA") when the UMA arena is missing, so those two can pass without running their bodies. Scope: one logical change.
performance: frees do a bitmap update and a map lookup per page, same order as before; memory is 1 bit per page per block (negligible). No per-frame cost. Reasoned, not measured.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300; Claude Code, Claude Sonnet 5.5 ran the build and the suite and read the diff; Claude Opus 5.5 checked this report

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Dec 1, 2026
@throny
throny merged commit 5ed2c33 into xcl-dev:main Dec 1, 2026
3 checks passed
@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merged at the checked head after a combined trial of #3600, #3493, #3608 and #3607 on main f07e426d (RTX 4080 16 GB, cargo test --workspace --no-fail-fast -- --test-threads=4: 2909 passed, 0 failed, 51 ignored, 25 SKIPPED lines). Two merge checks each.

AI use: Claude Code (Claude Opus 5.5).

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

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants