xmem: keep tiny reservations per page; map_whole only for large runs #93

Closed
github-bot wants to merge 6 commits from gh-pr/93/head into gh-pr/93/base
Owner

@⁠dutch2005 on GitHub · 2026-11-25 13:51 UTC · original
fix/xmem-whole-threshold → main · +138 −1 · 2 files · 6 commits · closed without merging

Why

Fixes issue #8. On Windows 11 / RDNA2, five victory-hle xmem tests fail because map_whole takes a tiny reservation (three 64 KiB pages) down the whole-reservation path. Unmapping the middle page then reports MEM_COMMIT (4096) where the tests expect MEM_RESERVE (8192), and the address of the split placeholders differs.

What

  • map_whole only makes a new whole view for reservations above 64 pages. The threshold stays in the known match, so a later small call into a range already shown whole still goes to that view.
  • The end of the range is computed with checked_mul/checked_add.
  • map_alias with no pages returns true before the whole-range check (#99, ySKELLETONX); a zero-length alias next to a reservation shown whole is a no-op.
  • The whole-view tests release their view and reservation and hold a test mutex; .cargo/config.toml sets RUST_TEST_THREADS=1 for the global xmem/GPU state.

Review fixes (all on this branch)

  • ySKELLETONX found the first threshold broke the title at startup on the RX 9070 XT (jmal423: os error 487 ~1 s in): the GPU heap is mapped in several calls, and a later 16-page call was refused after the first call showed the reservation whole. Their fix and a_small_run_into_a_reservation_shown_whole_is_mapped were taken from fork PR #1 (commit by ySKELLETONX preserved).
  • gut0to found the new tests leaked xmem state into later tests; release_whole now removes the bookkeeping, unmaps the view and releases the reservation.
  • The allocate doc comment is back on allocate.

Testing

On an RX 6700 XT, Windows 11, Rust 1.98.1:

  • cargo test -p victory-hle --lib xmem -- --test-threads=1 — 16 passed, 0 failed.
  • 30 default-thread runs of the xmem suite: 0 failures (was ~9/40 after the first version).
  • cargo test -p victory-hle --lib — full crate passes.
  • Mutation coverage on the xmem diff: 7 mutants caught, 1 equivalent. The surviving mutant disables the pages.len() <= 64 guard; manual mutation confirmed all 16 xmem tests still pass, so the guard is not observable in this test suite. It is retained because the title's multi-call heap mapping needs it, and the startup-crash regression test covers the other side.

Independent confirmation: RX 6950 XT (MamaLeVrai) 103 passed, 0 failed, 3/3 runs; RX 7600 XT (ySKELLETONX) fixes #91 and reports 0 failures in 40 parallel runs with the startup fix.

Risk

Tiny reservations no longer make a new in-place whole view; the large GPU heap mappings still do. A title run on the RX 9070 XT with the startup fix would confirm the crash is gone; jmal423 was asked in the review thread.

**@⁠dutch2005** on GitHub · 2026-11-25 13:51 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93) `fix/xmem-whole-threshold` → `main` · +138 −1 · 2 files · 6 commits · closed without merging ## Why Fixes issue #8. On Windows 11 / RDNA2, five `victory-hle` xmem tests fail because `map_whole` takes a tiny reservation (three 64 KiB pages) down the whole-reservation path. Unmapping the middle page then reports `MEM_COMMIT` (4096) where the tests expect `MEM_RESERVE` (8192), and the address of the split placeholders differs. ## What - `map_whole` only makes a **new** whole view for reservations above 64 pages. The threshold stays in the `known` match, so a later small call into a range **already shown whole** still goes to that view. - The end of the range is computed with `checked_mul`/`checked_add`. - `map_alias` with no pages returns true before the whole-range check (`#99`, ySKELLETONX); a zero-length alias next to a reservation shown whole is a no-op. - The whole-view tests release their view and reservation and hold a test mutex; `.cargo/config.toml` sets `RUST_TEST_THREADS=1` for the global xmem/GPU state. ## Review fixes (all on this branch) - ySKELLETONX found the first threshold broke the title at startup on the RX 9070 XT (jmal423: `os error 487` ~1 s in): the GPU heap is mapped in several calls, and a later 16-page call was refused after the first call showed the reservation whole. Their fix and `a_small_run_into_a_reservation_shown_whole_is_mapped` were taken from fork PR #1 (commit by ySKELLETONX preserved). - gut0to found the new tests leaked xmem state into later tests; `release_whole` now removes the bookkeeping, unmaps the view and releases the reservation. - The `allocate` doc comment is back on `allocate`. ## Testing On an RX 6700 XT, Windows 11, Rust 1.98.1: - `cargo test -p victory-hle --lib xmem -- --test-threads=1` — **16 passed, 0 failed**. - 30 default-thread runs of the xmem suite: **0 failures** (was ~9/40 after the first version). - `cargo test -p victory-hle --lib` — full crate passes. - Mutation coverage on the xmem diff: **7 mutants caught, 1 equivalent**. The surviving mutant disables the `pages.len() <= 64` guard; manual mutation confirmed all 16 xmem tests still pass, so the guard is not observable in this test suite. It is retained because the title's multi-call heap mapping needs it, and the startup-crash regression test covers the other side. Independent confirmation: RX 6950 XT (MamaLeVrai) 103 passed, 0 failed, 3/3 runs; RX 7600 XT (ySKELLETONX) fixes #91 and reports 0 failures in 40 parallel runs with the startup fix. ## Risk Tiny reservations no longer make a *new* in-place whole view; the large GPU heap mappings still do. A title run on the RX 9070 XT with the startup fix would confirm the crash is gone; jmal423 was asked in the review thread.
A three-page reservation took the whole-reservation path, so unmapping its middle page left it committed (issue #8, five tests). map_whole now applies above 64 pages, the threshold its debug line already used; the end is checked for overflow.
Mutation testing showed that replacing map_whole with false survived: every test used a tiny reservation. A 65-page run now asserts the whole view and its bytes, so disabling the optimisation fails the suite.
Review found that the new whole-reservation test left a 65-page range behind, so a zero-length map_alias at the start of a whole range was refused in parallel runs. Take #99's early return (ySKELLETONX), add the zero-length case to the large-run test, and move the allocate doc comment back to allocate. 0 failures in 20 parallel xmem runs.
The title maps its GPU heap in several calls. Once a large first call
shows the reservation whole, the tiny-run threshold also refused later
runs of 64 pages or fewer into it, and those went to the per-page path,
which needs placeholders the reservation no longer has. Mapping failed
with error 487 at startup (0xa10000, 16 pages, on an RX 9070 XT, reported
on #93). The threshold now only keeps tiny runs from making a new whole
view.
The whole-view tests leaked their view and reservation into later tests and raced on the global section pool. Remove the bookkeeping, unmap the view, release the reservation, and hold a test mutex; set RUST_TEST_THREADS=1 in .cargo/config.toml. 0 failures in 30 parallel runs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:35:34 +00:00
github-bot closed this pull request 2026-11-26 03:35:34 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 14:01 UTC · original

Tested on the machine of issue #8 and #2 (RX 6950 XT, Windows 11 build 26300, Rust 1.98.1): this PR merged on main 71652a2 (head 68d437d, no conflict), cargo test --release -p victory-hle --lib, 3 runs.

main 71652a2 with this PR
victory-hle 97 passed, 5 failed (the five xmem tests of #8), 3 runs of 3 103 passed, 0 failed, 3 runs of 3

The six new tests are in the 103. victory-shader is not touched by the PR and still has its 2 failures (see #92). Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 14:01 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7164636565) Tested on the machine of issue #8 and #2 (RX 6950 XT, Windows 11 build 26300, Rust 1.98.1): this PR merged on `main` `71652a2` (head `68d437d`, no conflict), `cargo test --release -p victory-hle --lib`, 3 runs. | | `main` `71652a2` | with this PR | |---|---|---| | victory-hle | 97 passed, **5 failed** (the five xmem tests of #8), 3 runs of 3 | **103 passed, 0 failed**, 3 runs of 3 | The six new tests are in the 103. `victory-shader` is not touched by the PR and still has its 2 failures (see #92). Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
Author
Owner

@⁠ySKELLETONX on GitHub · 2026-11-25 14:03 UTC · original

Tested on an RX 7600 XT 16 GB (RDNA3), Windows 11 Pro build 26300, Rust 1.98.1, at 68d437d. It fixes the 5 xmem failures I reported in #91, and the full victory-hle crate passes (103 passed, 1 ignored).

One problem: with the default parallel runner, mapping_no_pages_changes_nothing fails in about 9 of 40 runs of cargo test -p victory-hle --lib xmem, at assert!(map_alias(at as u64, a as u64, 0)). It never fails with --test-threads=1 or with --skip a_large_run_of_pages_is_shown_whole.

The cause: the new test leaves a 65-page reservation shown whole. When it lands directly after the other test's 2-page reservation, map_alias(a + 2 * PAGE, a, 0) is refused because in_whole is true for an empty range at the start of a whole range. #99 makes map_alias with len == 0 return true up front. With #99 on top of this PR: 0 failures in 60 parallel runs. You can also take the 4 lines into this PR, whichever you prefer.

Small nit: the new test was inserted between /// \n` pages of one new allocation.andfn allocate, so that doc comment is now attached to a_large_run_of_pages_is_shown_whole`.

**@⁠ySKELLETONX** on GitHub · 2026-11-25 14:03 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7164686091) Tested on an RX 7600 XT 16 GB (RDNA3), Windows 11 Pro build 26300, Rust 1.98.1, at `68d437d`. It fixes the 5 xmem failures I reported in #91, and the full `victory-hle` crate passes (103 passed, 1 ignored). One problem: with the default parallel runner, `mapping_no_pages_changes_nothing` fails in about 9 of 40 runs of `cargo test -p victory-hle --lib xmem`, at `assert!(map_alias(at as u64, a as u64, 0))`. It never fails with `--test-threads=1` or with `--skip a_large_run_of_pages_is_shown_whole`. The cause: the new test leaves a 65-page reservation shown whole. When it lands directly after the other test's 2-page reservation, `map_alias(a + 2 * PAGE, a, 0)` is refused because `in_whole` is true for an empty range at the start of a whole range. #99 makes `map_alias` with `len == 0` return true up front. With #99 on top of this PR: 0 failures in 60 parallel runs. You can also take the 4 lines into this PR, whichever you prefer. Small nit: the new test was inserted between `/// \`n\` pages of one new allocation.` and `fn allocate`, so that doc comment is now attached to `a_large_run_of_pages_is_shown_whole`.
Author
Owner

@⁠jmal423 on GitHub · 2026-11-25 14:34 UTC · original

Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (on top of 01aadd1 + #39, Series X target):

The title crashes ~1s after launch with os error 487 (Attempt to access invalid address) inside xmem:

INFO victory_hle::modules::xmem: a reservation of physical pages shown whole at=0x10000 len=193986560
WARN victory_hle::modules::xmem: mapping physical pages failed (not a placeholder?) address=0xa10000 len=1048576 error=Attempt to access invalid address. (os error 487)
VIctory: first-chance exception 0x80000003 at victory-image-b99f876b74d4a77f.dll+0x19f4f58

Dropping #93 (keeping #39) resolves the startup crash.

**@⁠jmal423** on GitHub · 2026-11-25 14:34 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7165492924) Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (on top of `01aadd1` + `#39`, Series X target): The title crashes ~1s after launch with `os error 487` (`Attempt to access invalid address`) inside `xmem`: ``` INFO victory_hle::modules::xmem: a reservation of physical pages shown whole at=0x10000 len=193986560 WARN victory_hle::modules::xmem: mapping physical pages failed (not a placeholder?) address=0xa10000 len=1048576 error=Attempt to access invalid address. (os error 487) VIctory: first-chance exception 0x80000003 at victory-image-b99f876b74d4a77f.dll+0x19f4f58 ``` Dropping `#93` (keeping `#39`) resolves the startup crash.
Author
Owner

@⁠ySKELLETONX on GitHub · 2026-11-25 14:37 UTC · original

@⁠jmal423's crash comes from the threshold: pages.len() <= 64 returns before map_whole checks for a whole view that already covers the range. The title maps the GPU heap in several calls. After the first one shows the reservation whole, the 16-page call at 0xa10000 is refused, goes to the per-page path, and fails with error 487 because the whole view replaced the placeholders there.

Fix and regression test (70 of 80 pages mapped whole, then the last 10): dutch2005/VIctory-Belobug-AMD#1, against this PR's branch. The test fails at 0cbedb9 with the same null return and passes with the fix. victory-hle: 104 passed. xmem: 0 failures in 40 parallel runs. The threshold now applies only when a run would make a new whole view, so the #8 tests keep the per-page path.

@⁠jmal423, if you can run the title with that branch on the 9070 XT, that would confirm it.

**@⁠ySKELLETONX** on GitHub · 2026-11-25 14:37 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7165578171) @⁠jmal423's crash comes from the threshold: `pages.len() <= 64` returns before `map_whole` checks for a whole view that already covers the range. The title maps the GPU heap in several calls. After the first one shows the reservation whole, the 16-page call at `0xa10000` is refused, goes to the per-page path, and fails with error 487 because the whole view replaced the placeholders there. Fix and regression test (70 of 80 pages mapped whole, then the last 10): dutch2005/VIctory-Belobug-AMD#1, against this PR's branch. The test fails at `0cbedb9` with the same null return and passes with the fix. `victory-hle`: 104 passed. xmem: 0 failures in 40 parallel runs. The threshold now applies only when a run would make a *new* whole view, so the #8 tests keep the per-page path. @⁠jmal423, if you can run the title with that branch on the 9070 XT, that would confirm it.
Author
Owner

@⁠ySKELLETONX on GitHub · 2026-11-25 15:57 UTC · original

On gut0to's .cargo/config.toml comment: confirmed here. At 30132fc, cargo xtask policy fails with no such command: xtask, so the pre-commit hook (cargo xtask policy --staged) refuses every commit. dutch2005/VIctory-Belobug-AMD#2 puts the alias back beside [env]. With this PR's test mutex, victory-hle had 0 failures in 30 runs at --test-threads=16, so the workspace-wide RUST_TEST_THREADS=1 may not be needed (details in that PR).

**@⁠ySKELLETONX** on GitHub · 2026-11-25 15:57 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7167468237) On gut0to's `.cargo/config.toml` comment: confirmed here. At `30132fc`, `cargo xtask policy` fails with `no such command: xtask`, so the pre-commit hook (`cargo xtask policy --staged`) refuses every commit. dutch2005/VIctory-Belobug-AMD#2 puts the alias back beside `[env]`. With this PR's test mutex, `victory-hle` had 0 failures in 30 runs at `--test-threads=16`, so the workspace-wide `RUST_TEST_THREADS=1` may not be needed (details in that PR).
Author
Owner

@⁠dutch2005 on GitHub · 2026-11-25 15:57 UTC · original

Updated fix/xmem-whole-threshold following @⁠gut0to and @⁠ySKELLETONX's reviews:

  • Incorporated @⁠ySKELLETONX's tail-mapping fix for subsequent small XMemMapPhysicalPages calls inside a reservation already shown whole (f3c7be2, fixing @⁠jmal423's RX 9070 XT startup crash) plus map_alias(..., 0) early-out.
  • Kept [alias] xtask = "run --quiet --package xtask --" alongside [env] RUST_TEST_THREADS = "1" in .cargo/config.toml (@⁠gut0to).
  • Cleaned up release_whole (unmaps the whole view and releases the reservation before freeing pages, with // SAFETY: comment) and serialized whole-view tests via WHOLE_TEST.

Verification (after final edit)

  • cargo test -p victory-hle --lib --locked: 104 passed, 0 failed, 1 ignored
  • cargo xtask policy --staged: 2 files ok
  • cargo mutants --in-diff (victory-hle, modules::xmem::tests): 7 caught, 1 documented non-observable missed (xmem.rs:720 pages.len() <= 64 guard — as noted in docs/mutation-testing.md, unit tests use the shared physical pool unless isolated to a fresh section; kept for title multi-call GPU heap mappings)
**@⁠dutch2005** on GitHub · 2026-11-25 15:57 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#issuecomment-7167469760) Updated `fix/xmem-whole-threshold` following @⁠gut0to and @⁠ySKELLETONX's reviews: - Incorporated @⁠ySKELLETONX's tail-mapping fix for subsequent small `XMemMapPhysicalPages` calls inside a reservation already shown whole (`f3c7be2`, fixing @⁠jmal423's RX 9070 XT startup crash) plus `map_alias(..., 0)` early-out. - Kept `[alias] xtask = "run --quiet --package xtask --"` alongside `[env] RUST_TEST_THREADS = "1"` in `.cargo/config.toml` (@⁠gut0to). - Cleaned up `release_whole` (unmaps the whole view and releases the reservation before freeing pages, with `// SAFETY:` comment) and serialized whole-view tests via `WHOLE_TEST`. ### Verification (after final edit) - `cargo test -p victory-hle --lib --locked`: **104 passed, 0 failed, 1 ignored** - `cargo xtask policy --staged`: **2 files ok** - `cargo mutants --in-diff` (`victory-hle`, `modules::xmem::tests`): **7 caught, 1 documented non-observable missed** (`xmem.rs:720` `pages.len() <= 64` guard — as noted in `docs/mutation-testing.md`, unit tests use the shared physical pool unless isolated to a fresh section; kept for title multi-call GPU heap mappings)
@ -1406,0 +1506,4 @@
assert_eq!(rest.cast::<u8>().read(), 9);
}
release_whole(reservation, N * PAGE);
assert_eq!(XMemFreePhysicalPages(N, pages.as_ptr()), 1);
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:49 UTC · original

Could this test unmap the whole view and release reservation before freeing the pages? XMemFreePhysicalPages only removes the page-table entries; it leaves the mapped view in physical.views/physical.whole and the virtual reservation allocated. That leaks xmem state and address space into later tests, especially when the suite runs in parallel. AI-assisted review.

**@⁠gut0to** on GitHub · 2026-11-25 14:49 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#discussion_r4696640328) Could this test unmap the whole view and release reservation before freeing the pages? XMemFreePhysicalPages only removes the page-table entries; it leaves the mapped view in physical.views/physical.whole and the virtual reservation allocated. That leaks xmem state and address space into later tests, especially when the suite runs in parallel. AI-assisted review.
@ -3,0 +4,4 @@
# The xmem page pool and the GPU differential tests are global state shared
# between tests. Run test binaries with one thread by default, as the hardware
# runs already do; `--test-threads` on the command line still wins.
[env]
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 15:31 UTC · original

Could this keep the existing xtask alias alongside the test-thread setting? Replacing the [alias] table removes cargo xtask, which the README's policy check and install-hooks instructions rely on. AI-assisted review; I checked the current Cargo config against those README commands.

**@⁠gut0to** on GitHub · 2026-11-25 15:31 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#discussion_r4697190812) Could this keep the existing xtask alias alongside the test-thread setting? Replacing the [alias] table removes cargo xtask, which the README's policy check and install-hooks instructions rely on. AI-assisted review; I checked the current Cargo config against those README commands.
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:49 UTC · original · reviewed

AI-assisted review: I checked the cleanup behavior in XMemFreePhysicalPages.

**@⁠gut0to** on GitHub · 2026-11-25 14:49 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#pullrequestreview-6059866790) · **reviewed** AI-assisted review: I checked the cleanup behavior in XMemFreePhysicalPages.
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 15:31 UTC · original · reviewed

AI-assisted review: I compared the updated Cargo config with the repository's documented contributor commands.

**@⁠gut0to** on GitHub · 2026-11-25 15:31 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/93#pullrequestreview-6060543349) · **reviewed** AI-assisted review: I compared the updated Cargo config with the repository's documented contributor commands.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.