XMem: recycle freed physical page numbers on allocation #1935

Open
github-bot wants to merge 1 commit from github-bot/gh-1935 into main AGit
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 01:12 UTC · original
ashrafsdrop:mem/physical-page-recycling-036 → main · +91 −9 · 1 files · 1 commits · open

Resolves VIC-BT-036 from #1249:

Summary

  1. Physical Page Recycling:
    In \crates/victory-hle/src/modules/xmem.rs, physical page allocation previously grew \physical.next_page\ monotonically by \
  • FRAMES\ on every call to \XMemAllocatePhysicalPages. Titles that frequently allocate and release streaming buffers (or DirectStorage staging pages) could eventually exhaust or balloon page numbers beyond the 21-bit representation (
    umber >> 4).
    • Introduced a free set (\BTreeSet) on \Physical.
    • In \XMemAllocatePhysicalPages, candidate runs of
      \ contiguous frames are prioritized from \physical.free\ before expanding \physical.next_page.
    • In \XMemFreePhysicalPages, freed page numbers are inserted into \physical.free, and trailing high-water pages contiguous with \physical.next_page\ are trimmed and reclaimed immediately.
  1. Tests:
    Added unit test \physical_pages_recycles_freed_page_numbers\ verifying single and multi-page recycling and matching reuse of page identifiers.

Verification

  • \cargo test -p victory-hle: 144 passed, 0 failed.
  • \cargo clippy -p victory-hle --all-targets --all-features -- -D warnings: clean, 0 warnings.
  • \cargo run -p xtask -- policy: 182 files ok.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 01:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935) `ashrafsdrop:mem/physical-page-recycling-036` → `main` · +91 −9 · 1 files · 1 commits · open Resolves VIC-BT-036 from #1249: ### Summary 1. **Physical Page Recycling**: In \crates/victory-hle/src/modules/xmem.rs\, physical page allocation previously grew \physical.next_page\ monotonically by \ * FRAMES\ on every call to \XMemAllocatePhysicalPages\. Titles that frequently allocate and release streaming buffers (or DirectStorage staging pages) could eventually exhaust or balloon page numbers beyond the 21-bit representation (\ umber >> 4\). - Introduced a free set (\BTreeSet<usize>\) on \Physical\. - In \XMemAllocatePhysicalPages\, candidate runs of \ \ contiguous frames are prioritized from \physical.free\ before expanding \physical.next_page\. - In \XMemFreePhysicalPages\, freed page numbers are inserted into \physical.free\, and trailing high-water pages contiguous with \physical.next_page\ are trimmed and reclaimed immediately. 2. **Tests**: Added unit test \physical_pages_recycles_freed_page_numbers\ verifying single and multi-page recycling and matching reuse of page identifiers. ### Verification - \cargo test -p victory-hle\: 144 passed, 0 failed. - \cargo clippy -p victory-hle --all-targets --all-features -- -D warnings\: clean, 0 warnings. - \cargo run -p xtask -- policy\: 182 files ok.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:18 +00:00
github-bot force-pushed github-bot/gh-1935 from 286fa25859 to 935eff925f 2026-11-26 06:31:38 +00:00 Compare
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-25 02:19 UTC · original

Tested 01cc8c7 merged onto main 940d32f (clean), Windows 11 26300, Rust 1.98.1 MSVC. The new test is flaky in a full run:

cargo test -p victory-hle --lib --locked runs result
main 940d32f 10 10 × 143 passed
main + this PR 10 5 × physical_pages_recycles_freed_page_numbers FAILED, 5 × 144 passed
this test alone 3 passes

The failure:

panicked at crates\victory-hle\src\modules\xmem.rs:1307:9:
assertion `left == right` failed
  left: [1048752, 1048768, 1048784]
 right: [1048688, 1048704, 1048720]

Why: the page table (physical()) is process-global, and the other xmem tests (allocate(..) / XMemFreePhysicalPages around lines 1349–1435) allocate and free physical pages on other test threads at the same time. Their frees land in physical.free, and the first-fit search then picks their numbers, or they take this test's freed numbers first. So the exact-number assertions (one == original[1], new_pages == original) only hold when no other test runs in between.

Suggestion: move the run search and the free/trim logic into methods on Physical (e.g. fn take(&mut self, n) -> usize and fn give_back(&mut self, number)), and test them on a fresh Physical::default(). That makes the test deterministic and keeps the global path as it is. Alternatively, a lock that every test touching physical pages takes. Asserting "a subset of the freed numbers" would still race.

The change itself reads right to me: the run search only takes n contiguous numbers, and trimming the high-water mark keeps next_page low. I didn't find another table keyed by the page number that a reuse would confuse; views keep their own Slice and Arc<Section>.

Not tested: the title.

AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.

**@⁠AgneyaK** on GitHub · 2026-11-25 02:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935#issuecomment-7150583476) Tested `01cc8c7` merged onto `main` `940d32f` (clean), Windows 11 26300, Rust 1.98.1 MSVC. **The new test is flaky in a full run:** | `cargo test -p victory-hle --lib --locked` | runs | result | |---|---|---| | `main` `940d32f` | 10 | 10 × `143 passed` | | `main` + this PR | 10 | **5 × `physical_pages_recycles_freed_page_numbers` FAILED**, 5 × `144 passed` | | this test alone | 3 | passes | The failure: ``` panicked at crates\victory-hle\src\modules\xmem.rs:1307:9: assertion `left == right` failed left: [1048752, 1048768, 1048784] right: [1048688, 1048704, 1048720] ``` **Why:** the page table (`physical()`) is process-global, and the other `xmem` tests (`allocate(..)` / `XMemFreePhysicalPages` around lines 1349–1435) allocate and free physical pages on other test threads at the same time. Their frees land in `physical.free`, and the first-fit search then picks their numbers, or they take this test's freed numbers first. So the exact-number assertions (`one == original[1]`, `new_pages == original`) only hold when no other test runs in between. **Suggestion:** move the run search and the free/trim logic into methods on `Physical` (e.g. `fn take(&mut self, n) -> usize` and `fn give_back(&mut self, number)`), and test them on a fresh `Physical::default()`. That makes the test deterministic and keeps the global path as it is. Alternatively, a lock that every test touching physical pages takes. Asserting "a subset of the freed numbers" would still race. The change itself reads right to me: the run search only takes `n` contiguous numbers, and trimming the high-water mark keeps `next_page` low. I didn't find another table keyed by the page number that a reuse would confuse; views keep their own `Slice` and `Arc<Section>`. Not tested: the title. AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:19 UTC · original · edited 2026-11-26 07:20 UTC

Automated triage for the maintainers.

Merge candidate. Rebased with the same content as before. In crates/victory-hle/src/modules/xmem.rs:

  • XMemAllocatePhysicalPages gets its numbers from Physical::take, which reuses a freed contiguous run (in FRAMES steps) before advancing next_page.
  • XMemFreePhysicalPages gives back only numbers that were allocated.
  • give_back trims trailing numbers back down to next_page.

The test uses a local Physical, so it doesn't race the other xmem tests.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935#issuecomment-7156887743) · edited 2026-11-26 07:20 UTC <!-- victory-triage v1 head=935eff925f730ba1992247bf6166caa4a6ef28d8 verdict=merge-candidate at=2026-11-26T07:20:51Z --> Automated triage for the maintainers. **Merge candidate.** Rebased with the same content as before. In `crates/victory-hle/src/modules/xmem.rs`: - `XMemAllocatePhysicalPages` gets its numbers from `Physical::take`, which reuses a freed contiguous run (in `FRAMES` steps) before advancing `next_page`. - `XMemFreePhysicalPages` gives back only numbers that were allocated. - `give_back` trims trailing numbers back down to `next_page`. The test uses a local `Physical`, so it doesn't race the other xmem tests. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 10:40 UTC · original

Thanks @⁠AgneyaK and @⁠Marvisak for the spot-on review and diagnosis of the test concurrency race.

Updated in commit \85565c3:

  1. **Encapsulated Allocation and Recycling on \Physical**:

    • \Physical::take(&mut self, n: usize) -> usize: searches \self.free\ for candidate runs of
      \ contiguous frames before falling back to \self.next_page.
    • \Physical::give_back(&mut self, number: usize): records
      umber\ in \self.free\ and trims trailing high-water pages contiguous with \self.next_page.
    • \XMemAllocatePhysicalPages\ and \XMemFreePhysicalPages\ now delegate directly to \physical.take(n)\ and \physical.give_back(number).
  2. Deterministic Isolated Unit Test:

    • \physical_pages_recycles_freed_page_numbers\ now runs against a fresh, local \Physical::default()\ instance rather than the process-global \PHYSICAL\ mutex. This eliminates the race condition from concurrent test threads.
    • Verified with 5 consecutive runs of \cargo test -p victory-hle --lib --locked\ (144 passed, 0 failed in all runs).
  3. Rebased on Main:

    • Rebased cleanly over latest \main\ (\940d32f) picking up the GPU-less CI test skip.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 10:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935#issuecomment-7160023137) Thanks @⁠AgneyaK and @⁠Marvisak for the spot-on review and diagnosis of the test concurrency race. Updated in commit \85565c3\: 1. **Encapsulated Allocation and Recycling on \Physical\**: - \Physical::take(&mut self, n: usize) -> usize\: searches \self.free\ for candidate runs of \ \ contiguous frames before falling back to \self.next_page\. - \Physical::give_back(&mut self, number: usize)\: records \ umber\ in \self.free\ and trims trailing high-water pages contiguous with \self.next_page\. - \XMemAllocatePhysicalPages\ and \XMemFreePhysicalPages\ now delegate directly to \physical.take(n)\ and \physical.give_back(number)\. 2. **Deterministic Isolated Unit Test**: - \physical_pages_recycles_freed_page_numbers\ now runs against a fresh, local \Physical::default()\ instance rather than the process-global \PHYSICAL\ mutex. This eliminates the race condition from concurrent test threads. - Verified with 5 consecutive runs of \cargo test -p victory-hle --lib --locked\ (144 passed, 0 failed in all runs). 3. **Rebased on Main**: - Rebased cleanly over latest \main\ (\940d32f\) picking up the GPU-less CI test skip.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 02:10 UTC · original · reviewed

Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff.

XMem: recycle freed physical page numbers on allocation: PFN recycling in the memory manager — memory-pressure fix.

**@⁠catminou4** on GitHub · 2026-11-25 02:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935#pullrequestreview-6050615250) · **reviewed** Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff. XMem: recycle freed physical page numbers on allocation: PFN recycling in the memory manager — memory-pressure fix.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 12:39 UTC · original · approved these changes

Reviewed and verified at head 935eff92 against origin/main (63bcca43) on Windows 11 (debug & --release):

  • Design & correctness:
    • Physical::take and Physical::give_back (crates/victory-hle/src/modules/xmem.rs:409-455) cleanly encapsulate contiguous FRAMES-stepped (16) PFN run recycling and trailing high-water reclamation back to the base 0x10_0000 (never underflowing or returning page 0).
    • XMemFreePhysicalPages (xmem.rs:610-612) calls give_back only when physical.pages.remove(&number).is_some(), keeping free and pages strictly disjoint and safely ignoring double-frees, page 0, and unknown/unaligned numbers. Mapped views (views / shown) are keyed by Slice::key (Arc<Section> + slice index) rather than page numbers, so recycled PFNs cannot collide with still-mapped older sections, and all physical-page state remains serialized under PHYSICAL.
    • physical_pages_recycles_freed_page_numbers (xmem.rs:1373-1395) operates on a local Physical, avoiding cross-test flakiness.
    • Minor non-blocking note: Physical::take scans self.free in O(|free|) when searching for a contiguous run under fragmentation; fine for typical streaming workloads.
  • Verification:
    • Clean git merge-tree against origin/main (63bcca43); cargo xtask policy (191 files ok); cargo clippy -p victory-hle --all-targets --locked -- -D warnings clean with default features and --all-features.
    • cargo test -p victory-hle --locked: 3/3 consecutive parallel dev runs passed (153 unit + 2 integration each) and --release passed (153 unit + 2 integration).
    • Stress-tested XMemAllocatePhysicalPages / XMemFreePhysicalPages in --release with 8 threads x 80 iterations of concurrent multi-page allocation + partial free + full free plus isolated double-free/bogus-page checks: zero duplicate live PFNs, all PFNs aligned to FRAMES and >= 0x10_0000, and next_page retracted cleanly back to 0x10_0000.

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 12:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/1935#pullrequestreview-6074270952) · **approved these changes** Reviewed and verified at head `935eff92` against `origin/main` (`63bcca43`) on Windows 11 (debug & `--release`): - **Design & correctness**: - `Physical::take` and `Physical::give_back` (`crates/victory-hle/src/modules/xmem.rs:409-455`) cleanly encapsulate contiguous `FRAMES`-stepped (`16`) PFN run recycling and trailing high-water reclamation back to the base `0x10_0000` (never underflowing or returning page `0`). - `XMemFreePhysicalPages` (`xmem.rs:610-612`) calls `give_back` only when `physical.pages.remove(&number).is_some()`, keeping `free` and `pages` strictly disjoint and safely ignoring double-frees, page `0`, and unknown/unaligned numbers. Mapped views (`views` / `shown`) are keyed by `Slice::key` (`Arc<Section>` + slice index) rather than page numbers, so recycled PFNs cannot collide with still-mapped older sections, and all physical-page state remains serialized under `PHYSICAL`. - `physical_pages_recycles_freed_page_numbers` (`xmem.rs:1373-1395`) operates on a local `Physical`, avoiding cross-test flakiness. - Minor non-blocking note: `Physical::take` scans `self.free` in `O(|free|)` when searching for a contiguous run under fragmentation; fine for typical streaming workloads. - **Verification**: - Clean `git merge-tree` against `origin/main` (`63bcca43`); `cargo xtask policy` (`191 files ok`); `cargo clippy -p victory-hle --all-targets --locked -- -D warnings` clean with default features and `--all-features`. - `cargo test -p victory-hle --locked`: **3/3 consecutive parallel dev runs** passed (`153 unit + 2 integration` each) and **`--release`** passed (`153 unit + 2 integration`). - Stress-tested `XMemAllocatePhysicalPages` / `XMemFreePhysicalPages` in `--release` with 8 threads x 80 iterations of concurrent multi-page allocation + partial free + full free plus isolated double-free/bogus-page checks: zero duplicate live PFNs, all PFNs aligned to `FRAMES` and `>= 0x10_0000`, and `next_page` retracted cleanly back to `0x10_0000`. LGTM.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1935/head:github-bot/gh-1935'
git switch 'github-bot/gh-1935'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1935'
git switch 'github-bot/gh-1935'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1935'
git switch 'github-bot/gh-1935'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1935'
git switch 'main'
git merge --squash 'github-bot/gh-1935'
git switch 'main'
git merge --ff-only 'github-bot/gh-1935'
git switch 'main'
git merge 'github-bot/gh-1935'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1935
No description provided.