kernelx: require contiguous physical pages to share one allocation #1134
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1143"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T12:32:15Z.
AllocateTitlePhysicalPagesassigns 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_pagesonly checked that page numbers were consecutive and thatpages[n - 1]had a home, soMapTitlePhysicalPages(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_hometo require that all consecutive pages belong to one livePHYS_BLOCKSentry (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, andis_sparseassume a single backing reservation of one memory kind.It also handles
home == atinsidemap_pages's per-page loop so an address-given call (e.g.MapTitlePhysicalPages(H1, 2, [p1, p2])spanning two allocations) keepsp1committed at its own home instead of copyingH1onto itself and decommitting it viamove_home_away/give_back_homes.What is in it
crates/xcl/src/hle/kernelx.rs: addcontiguous_home_in/contiguous_homeand use it inmap_pages; handlehome == atinsidemap_pages's per-page loop; addconsecutive_pages_from_separate_allocations_do_not_map_as_one_home,contiguous_home_requires_one_allocation, andmapping_page_at_its_own_home_in_mixed_call_keeps_it_committed.Tested
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: 2no 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, andmapping_page_at_its_own_home_in_mixed_call_keeps_it_committedall passRegression run
main:
PR:
Not tested
AI use
kernelx.rs, wrote the fix and regression tests, addressed @pvfreis's review finding onhome == atin the per-page loop, ran the test suite andscripts/smoke.ps1regression runs, and drafted this PR description.Imported from ivall/xcl. Originally posted by pvfreis on 2026-11-24T12:45:49Z.
Review: COMMENTED
Review of
81d047aagainst its merge base47efa2e(the head ofmainwhen 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 onmain:map_pagestookpage_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-1224onmain), andAllocateTitlePhysicalPagesnumbers pages from one global counter (:1171), so two allocations with consecutive numbers were committed and returned as[H1, H1 + n * PHYS_PAGE)bycommit_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 ofFRAMES, an index past the block, a block with nothing live all returnNone), and it takesPHYS_BLOCKSunderAWAYin the same orderpage_homealready does.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 ofNULL, buthome == Some(va)changes too. Scenario:MapTitlePhysicalPages(H1, 2, [p1, p2])withp1,p2from two allocations andH1the home ofp1in a sparse window.homeis nowNone, so the loop runs withat == H1:H1is committed (:1306),copy_nonoverlapping(home, at)is called with the same range as source and destination (:1311; undefined behaviour forcopy_nonoverlapping, though harmless in practice),move_home_awayqueuesH1(:1312,:946-948),p1is recorded as away atH1(:1321-1322), andgive_back_homesdecommitsH1when the call ends (:1222,:973-976): the page is recorded at an address that was just decommitted.mainhas 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 onmainreturned the wrong memory forp2instead). Fix: in the loop, treatat == page_home(p)as "at home" (commit the home, no copy, nomove_home_away), or say under Not tested that the address-given case is not covered; the new test only calls withNULL(:2171).Overlap with open PRs on the same functions.
2a3c7f4) changes the value type ofPHYS_BLOCKSfrom the tuple to aPhysicalBlockwith a per-page freed bitmap.contiguous_home_intakes&BTreeMap<usize, (usize, usize, usize)>(:859) and its unit test builds tuples (:2194-2198), so whichever merges second has to be rebased.away.title.insert(home + i * PHYS_PAGE, p)to the at-home branch ofmap_pages, the branch whose condition this PR changes.MapTitlePhysicalPages,page_home,PHYS_BLOCKS,FreeTitlePhysicalPages).Question: when two allocations' homes happen to be adjacent,
mainmappedMapTitlePhysicalPages(NULL, n, ...)over both and every page was atH1 + i * PHYS_PAGE; this head refuses that withERROR_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_homereturns 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: onmainthe call at:2171returnsh1.Against CONTRIBUTING: the sections are all there, both
xcl-smokeblocks name the right commits (47efa2eonmain,81d047afor the head), 101 added / 3 removed lines in one file, one commit with the subject in the repository's style. Only thePR descriptioncheck had reported on this head when read;CIhad 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.
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T12:53:50Z.
Independent verification on current
main384f7f7(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.
Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T13:58:40Z.
Thanks @pvfreis for the thorough review! Addressed in \
Imported from ivall/xcl. Originally posted by pvfreis on 2026-11-24T14:06:31Z.
Follow-up at
076d027(rebased onto930e8b4). I read the diff only; nothing was built or run here.at(kernelx.rs:1280-1291) is brought back from wherever it was away, has its home committed again throughhome_againand is taken out ofaway.gone. It is not recorded inAWAY. 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 inAWAY. Without the new branch, the assertion at:2252would fail.PHYS_BLOCKSvalue type thatcontiguous_home_in(:859) takes. #1055 and #1102 removeatfromaway.titleat the top of this loop and write it back on every path that leaves a page atat, so the newhome == atbranch will need that insert too when they meet.Reply by Claude Code (Claude Opus 5.5); checked by a person before posting.
Imported from ivall/xcl. Originally posted by JoaoPauluu on 2026-11-24T14:34:49Z.
A run on Linux under Wine.
Tested
x86_64-pc-windows-gnu.076d027as it is (based on930e8b4), 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
AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T03:41:06Z.
Re-verification on RTX 3060 Ti (Windows 11): still a merge conflict with current
mainon the new head — needs another rebase before it can be tested.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:54:52Z.
Triage (#1607) – head
076d027#1635 fixes the same bug in
map_pages: consecutive page numbers from twoAllocateTitlePhysicalPagescalls are treated as one home block. Main still has the unfixed check.The two PRs handle it differently. Here such a run is treated as scattered pages: refused with ERROR_NOT_SUPPORTED when no address is given, mapped page by page when one is, and a page mapped at its own home stays committed. #1635 returns ERROR_INVALID_PARAMETER for any consecutive run that crosses a block, even when the caller gives an address.
This PR was opened first. The test
consecutive_pages_from_separate_allocations_do_not_map_as_one_homerelies on the allocator producing non-adjacent homes within 32 tries.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.