Skip to content

HLE reuses freed physical page numbers - #1445

Open
BaggyALPHA wants to merge 3 commits into
Marvisak:mainfrom
BaggyALPHA:fix/reuse-physical-page-numbers
Open

BaggyALPHA wants to merge 3 commits into
Marvisak:mainfrom
BaggyALPHA:fix/reuse-physical-page-numbers

Conversation

@BaggyALPHA

@BaggyALPHA BaggyALPHA commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Addresses item 36 (VIC-BT-036) in #1249. The allocator now reuses freed page numbers and refuses a request if it cannot fit every number into the title's existing 21-bit field. A repeated free cannot add the same number twice. Existing mappings still refer to their original section when a number is reused for a new section.

I seeded the allocator just below the limit in an isolated test process and used the public allocation and free functions. Upstream returned an unrepresentable number on the second allocation despite the first having been freed. The patch reused the freed number. The regression also checks exhaustion with a live allocation, a duplicate free, and an old mapped view keeping its bytes after reuse. All 96 HLE tests, Clippy with all targets and features, and the asset-policy hook passed on Windows.

The follow-up checks the page-count limit before calculating the section size, so oversized requests clear the output count and leave the page array untouched. The synthetic regression now exercises allocation, mapping, aliasing, and physical writes at the limit. After reuse, the old mapping and an alias made from it retain their data, and writes by page number reach the new allocation. The oversized-request assertion failed before the follow-up and passes with it. Windows HLE tests (96), all-targets/all-features HLE Clippy, and the pre-commit asset-policy check passed.

AI-assisted (Codex).

@Daniel333313131

Copy link
Copy Markdown

Tested on AMD (RDNA3) together with #431, to see whether this PR removes the one GPU page fault that remains there. In three runs it did not.

Setup: AMD Radeon RX 7900 XTX 24 GB, driver 32.0.32029.2010; Ryzen 7 9800X3D, 32 GB; Windows 11 10.0.26300. Diag build of main ffe310e with this PR's head f89051f and #431's head 23ced70 merged. Each run used its own empty VICTORY_CACHE_DIR, VICTORY_D3D12_DRED=1, and VICTORY_KEYS set to Enter every 3 s from 8 s to 47 s. Each run was stopped after 180 s.

Background: with #431 alone, faults inside freed textures are gone on this GPU (#431 (comment)). What is left is a page fault where DRED names no resource. main shows the same kind in one of three runs.

run world ms/frame device removed DRED
#431 1 / 2 / 3 1037 / 936 / 929 169 s / none / none no resource named ×1
#1445 + #431 1 919 168 s page fault at 0x4fa396000, no resource named
#1445 + #431 2 1053 none reported – (but the log stops about 19 s before the run was stopped)
#1445 + #431 3 834 166 s page fault at 0x505a27000, no resource named

AI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the title, summarised the log lines and wrote this comment. The numbers come from the runs above.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The rebased head merges cleanly. In crates/victory-hle/src/modules/xmem.rs, XMemFreePhysicalPages now hands numbers to a free list through Physical::free, which only pushes a number that was actually allocated, so freeing twice cannot hand the number out twice. Physical::allocate reuses freed numbers before taking new ones and refuses a request that would go past the 21-bit page-number limit already described next to FRAMES on main. On refusal and for oversized counts, the function writes 0 to count and returns 0, which matches the failure path main already has. Reused numbers can come back out of order within one allocation. That is fine: runs groups pages by section and slice index, not by number. Existing views keep their own Arc<Section>, so a reused number does not change what an older mapping shows. The tests use a private allocator, so they do not change the global one. One small thing: there is a stray second blank line before XMemFreePhysicalPages. CI passes.

Reply to the author: thanks for the rebase. I reviewed the current head 3f88d1f. The conflict resolution keeps main's unknown_pages() and the wording of its doc comment, as you described. Your withdrawn note about watch.rs matches the code: free touches no mapping, and both map and unmap report remaps.


Generated by Claude Code

@BaggyALPHA

Copy link
Copy Markdown
Contributor Author

Follow-up for the triage concern: a2defb2 removes the child process. The limit regression seeds a private Physical allocator in-process, so parallel HLE tests cannot see the artificial limit. It still checks exhaustion, duplicate free, reuse, and distinct live Windows mappings for the old and new backing sections. A separate test exercises the public allocation API's oversized-request outputs.

Windows HLE library tests: 96 passed with default parallel scheduling. Strict Clippy with all targets and features, and the pre-commit asset-policy check, passed. Inputs were synthetic. No game run or relationship to the GPU page fault is claimed.

AI-assisted (OpenAI Codex).

@BaggyALPHA

Copy link
Copy Markdown
Contributor Author

Pre-merge concern worth a maintainer's eyes, found while investigating #2025: crates/victory-gpu/src/watch.rs lines 33-35 document an accepted gap — "memory freed and allocated again while a tracker holds it is not seen." On current main, XMemFreePhysicalPages never recycles a page number (next_page only increments), so that gap is currently unreachable through XMem. This PR's free-list reuse is the first thing that would make a freed-and-reallocated XMem page number reachable while some victory_gpu::watch::Tracked/Shadow still holds stale state for the old backing, if the title (or our own view-remapping) reuses a number quickly enough relative to an in-flight GPU submission's copy-back.

I have not demonstrated this actually happens — it would need either a live repro with real page churn (I attempted one for #2025 and didn't get far enough on NVIDIA hardware before hitting the unrelated world-hang device-removal) or a synthetic concurrency test exercising both crates together. Flagging it so it's weighed before merge rather than discovered after; happy to help verify if useful.

AI-assisted (Claude Code).

@BaggyALPHA

Copy link
Copy Markdown
Contributor Author

Withdrawing my own pre-merge concern above: page-number reuse cannot reach the watch.rs gap. Mechanism and a synthetic test below.

I flagged that this PR's free-list reuse would be the first thing to make watch.rs's documented caveat reachable — "memory freed and allocated again while a tracker holds it is not seen". I said then that I had not demonstrated it. Having now tested the gap directly and read the paths, I think the concern does not apply, and it should not sit on this PR unanswered.

The gap is real, and reporting the mapping is what closes it. A synthetic test in victory-gpu's own watch::tests (write-watched pool memory, no GPU, no game): track two pages, take them so they are guarded and clean, release the allocation, take the same address back with VirtualAlloc, then write to it.

  • The write is missed — the tracker still reads clean. Freeing destroys the guard with the pages, so the new allocation is read-write and the write faults nothing.
  • After title_memory_remapped for that range, the same take reports it changed.

So the caveat is exactly as the module describes, and the runtime's own mapping report is the thing that closes it.

Why reuse cannot trigger it. The gap needs the bytes behind an already-tracked address to change with no report. Reuse does not do that:

  • XMemFreePhysicalPages calls Physical::free, which removes the number from pages and pushes it onto free_pages. It touches no view, no mapping and no address, so nothing behind any tracked address changes.
  • Reuse only decides which backing a future map() resolves for that number. An existing view keeps its own section slice (views hold their Arc<Section>), which is the behaviour XMemFreePhysicalPages' own comment already describes for pages freed while still mapped.
  • The only places what an address shows changes are map() and unmap(), and both call victory_gpu::title_memory_remapped for the range — the call the test shows closes the gap.

So the sequence I was worried about (reuse, then a tracker missing the new owner's writes) needs a mapping step, and that step reports itself.

What I ran. cargo test -p victory-gpu --features diagnostics --locked on main 940d32f plus the test: 145 passed, 2 failed, against 144 passed, 2 failed for main alone in a separate target directory — the two being the capped-view-heap pair (FIND-9, what #2121 addresses). The new test passed 4 of 4 runs, three of them under default parallelism, one serial. RTX 3080, Windows 11 26300. Synthetic inputs only; no game files.

Two notes in the interest of completeness:

  1. This branch predates main's title_memory_aliased call in map(), so it has two notification sites where main has three. That is branch age, not a behaviour difference — the merge is clean and that call comes with it.
  2. The test is not part of this PR. It pins a caveat that is currently only a comment, so if it is wanted I am happy to send it as a test-only change against victory-gpu rather than smuggle it in here; say the word and I will, otherwise the result above stands as evidence.

AI-assisted (Claude Code); the numbers are from the runs described.

@BaggyALPHA
BaggyALPHA force-pushed the fix/reuse-physical-page-numbers branch from a2defb2 to 3f88d1f Compare November 25, 2026 20:06
@BaggyALPHA

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (13f91ca); the branch had gone conflicting as main moved. New head 3f88d1f, same three commits.

One conflict, in modules/xmem.rs: main has since added unknown_pages() (used by dstorage.rs) next to FRAMES, which this branch had moved to the top of the file beside PAGE_NUMBER_LIMIT. Resolved by keeping unknown_pages() and dropping the now-duplicate FRAMES, with main's fuller wording of its doc comment kept rather than mine, so the only change to that comment is where it sits.

On the rebased head: cargo test -p victory-hle --locked 148 passed, 0 failed, including freed_page_numbers_can_be_reused_at_the_limit and oversized_physical_request_clears_count_without_writing_page_numbers; cargo clippy -p victory-hle --all-targets --locked -- -D warnings clean; cargo xtask policy 184 files ok. Synthetic inputs only, no game files.

Nothing about the change itself is different — it is the same allocator, the same two tests, and the in-process limit test from the earlier follow-up. The concern I raised and then withdrew about page-number reuse reaching watch.rs's caveat still stands as withdrawn; main moving has not changed that reasoning, since free still touches no mapping.

AI-assisted (Claude Code).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
Steenax6 added a commit to Steenax6/VIctory-NVID that referenced this pull request Nov 26, 2026
…s run out

The title keeps a page number as 21-bit `number >> 4`, so numbers must stay
below 2^25; ours only counted up, from 0x100000, so after about 126 GiB of
allocations over a run the numbers would no longer fit and the title would
rebuild wrong ones. Freed numbers now come back, but only when no fresh
number fits, and the longest freed first: a number the title still holds
after freeing it (a DirectStorage request that reads its page list late)
then reaches no page or one handed out long after, not the next
allocation. A request that cannot get all its numbers gets none (count 0)
before its sections are made; a number freed twice comes back once.

After Marvisak/VIctory pull request Marvisak#1445 (BaggyALPHA; duplicate Marvisak#1935),
which hands freed numbers out first; fitted here to the sections of
VICTORY_SECTION_MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants