Recycle freed XMem physical page numbers before 21-bit limit #409
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
needs-logs
old-names
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/TaynanGT--Vice-Tay!409
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-409"
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?
@safesearch77 on GitHub · 2026-11-28 13:25 UTC · original
safesearch77:fix/xmem-recycle-page-numbers→main· +60204 −5433 · 206 files · 661 commits · closed without mergingXMemAllocatePhysicalPagespreviously incrementedPages::next_pagemonotonically. Because the title stores page numbers in 21 bits asnumber >> 4(FRAMES), crossingPAGE_LIMIT(1 << 25, roughly 124 GiB of cumulative allocations over a long session) truncates returned page numbers, causing unknown-page or live-page aliasing faults.Pages::takenow continues handing out sequential numbers whilenext_page + n * FRAMES <= PAGE_LIMIT, then falls back tofree_runto find the lowest contiguous run ofnfreed page slots within[FIRST_PAGE, PAGE_LIMIT). That fallback collects and sorts active page keys (O(P log P)), which only fires once cumulative allocations pass ~124 GiB. If no contiguous run fits,XMemAllocatePhysicalPageslogs a warning, sets*count = 0, and returns0cleanly (dropping the newly createdArc<Section>).Ported from
trustno1-coder/VIctory-NVIDIA@108a8971(byilluminatedfellow, fromavatarmanz/VIctory-7900XTX#316).Verification
cargo +1.98.1 test --locked --target x86_64-pc-windows-msvc -p victory-hle --lib -- modules::xmem(Wine):19 passed; 0 failed; 1 ignored(map_bench), all pure CPU/Win32 memory tests, includingpage_numbers_freed_are_given_again_past_the_title_s_21_bits(covers sequential boundary up toPAGE_LIMIT, lowest-gap reuse, fragmentation,usize::MAXoverflow, and(p >> 4 & 0x1f_ffff) << 4 == pround-trip).cargo +1.98.1 clippy --locked --target x86_64-pc-windows-msvc -p victory-hle --all-targets -- -D warnings: clean (0warnings).cargo +1.98.1 xtask policy:asset policy: 322 files ok.@Kal0s-design on GitHub · 2026-11-28 16:16 UTC · original
Read through this one. The
xmemtests pass on native Windows too (19 passed, 1 ignored atba5a8bd), since the description only had Wine.Things I checked and that look right:
free_runwith sorted keys and thePAGE_LIMITsentinel, and every number it can return stays a multiple of 16 below1 << 25, so>> 4and<< 4round-trip.Physical::viewsholds theSlice(and so the section), not the number. A stale view keeps showing the old section.Section, and itsDropcallstitle_memory_section_gone, so nothing is left registered.One thing to keep in mind, not a blocker: past the limit every
XMemAllocatePhysicalPagescollects and sorts all live page keys while holding thePAGESwrite lock, and the map/fault paths take the read side of it. With ~100k live pages that's a noticeable pause per allocation if the title allocates often by then. Keeping the freed numbers in aBTreeSet(filled inXMemFreePhysicalPagesonce past the limit) would avoid the scan. I don't know how often the title allocates physical pages that late in a session, so it may never matter.AI-assisted: yes
@TaynanGT on GitHub · 2026-11-29 11:35 UTC · original
Thanks for the native Windows check. Reusing freed page numbers needs to preserve live mappings and reject allocations that cross PAGE_LIMIT. The native test result complements the Wine coverage; please keep the limit and fragmented-free-run cases explicit.
@safesearch77 on GitHub · 2026-11-29 13:20 UTC · original
Thanks for the review. Adding the explicit tests you asked for turned up a real bug in the reuse path, so this update fixes it too.
The bug. After the sequential numbers run out,
take()scans for the lowest free run up toPAGE_LIMIT. The numbers fromnext_pageup to the limit were never given out, so they count as free. A run can therefore start in freed numbers just belownext_pageand continue into that never-given tail.next_pagedidn't move, so a later small allocation that still fit the tail got the same numbers again from the sequential path.XMemAllocatePhysicalPagestheninserted over the liveSlice. The earlier page number silently moved to the new section, and freeing either allocation dropped the other's page.It's only reachable once the 21-bit space has been used up (about 124 GiB of cumulative physical allocations), which is the situation this PR exists for. A test found it. No game run has hit it. The same
take()is in VIctory-NVIDIA and in #439 (cfd5ebd), and the patch applies to #439 unchanged.The fix (2 lines in
Pages::take): when a freed run is given,next_pagemoves past the run's end (self.next_page = self.next_page.max(first + len)). That keeps the invariant that no live number is at or pastnext_page. A refusal (None) changes nothing.Tests, one per point you raised, all on a
Pagesof their own that holds live entries (Slices of real sections):page_numbers_given_again_take_the_lowest_fit_around_live_pages: covers fragmented free runs (gaps of 1 and 3 between live numbers). Allocations of 2, 1, 2 and 1 pages getf(4),f(2),f(9)andf(6), so each takes the lowest run that fits. Every pre-existing entry still points at its own section and index, andnext_pageis unchanged.page_numbers_that_would_cross_the_title_s_21_bits_are_refused: only 2 never-given numbers are left belowPAGE_LIMIT, and no freed run of 1,024 exists.take(1024)returnsNoneand leavesnext_page, the map's size and every live entry unchanged. The 2 numbers left are still given afterwards.page_numbers_given_again_from_the_unused_tail_are_not_given_twice: the regression test for the bug above. It fails without the fix (0x1ffe000 was given while still live) and passes with it.Each test gives numbers the way
XMemAllocatePhysicalPagesdoes. It asserts that no number given is already live and that each survives the title's>> 4/<< 4round trip.Verified under Wine:
victory-hle --lib -- modules::xmem22/22 on this branch (19 before plus 3 new, 1 ignored), and 23/23 with the patch applied on #439 (the new test fails there without the fix).cargo clippy -p victory-hle --all-targets -- -D warningsandcargo xtask policy(322 files ok) are clean. The new tests are CPU-only. I haven't run them natively on Windows.@Kal0s-design's
BTreeSetidea for avoiding the key scan past the limit is a good follow-up. I've left it out to keep this change to the correctness fix.AI-assisted: yes (Claude), reviewed locally before posting.
@TaynanGT on GitHub · 2026-11-29 13:24 UTC · original
Thanks for finding the duplicate page-number allocation and adding the three regression cases. I integrated the follow-up locally; moving next_page past the reused run preserves the invariant that live numbers cannot be issued again. Native Windows tests are still pending. I agree that optimizing the free-run search should stay separate from this correctness fix.
@TaynanGT on GitHub · 2026-11-30 17:08 UTC · original
mainhas moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the currentmainand open a new PR; I'll review it there.Pull request closed