Scope UMA migration locks by pool and protect closing pool IDs #392

Open
github-bot wants to merge 3 commits from github-bot/gh-392 into main AGit
Owner

@⁠lucalocastro on GitHub · 2026-12-03 23:54 UTC · original
lucalocastro:codex/uma-fault-latency → main · +439 −42 · 3 files · 3 commits · open

A CPU fault can wait for a worker staging a migration in an unrelated physical pool. There is also a window during teardown when an allocator can reuse a numeric pool ID before the previous owner has finished clearing its metadata.

  • Lock only the participating pools, once each in ID order. A waiting reader rechecks its mapping. Two aliases of the same pool still synchronize. Release the locks before the driver kick.
  • Reserve a closing pool ID until teardown completes. Other pools can be allocated while the driver releases the old backing outside the global state mutex.

Evidence

In a synthetic Windows test with a forced 2 ms writer hold, median access time for an unrelated pool fell from 2.0125 to 0.0075 ms (P99 from 2.1716 to 0.0143 ms). Two views of the same backing page still waited about 2.01 ms and saw the writer's value. With no writer the median was 0.0013 ms in both arms. Five paired runs, 240 samples per case per run, originally measured on an integration checkout based on eb51227. In-game frame-time impact is open.

The close/reuse fixture passed in 100 isolated Windows processes on the original combined branch. It covers both an unmapped address and a new section at the same virtual address, pool ID and offset. A competing allocation runs during teardown. The test uses real Windows views and simulates the driver free callback. After splitting the branch, the UMA unit tests and all three isolated lock, rebind and reuse tests passed. Earlier full-suite runs on this Windows machine stopped at descriptor::tests::views_die_with_their_resource.

One A/B comparison requested

Compare A, the common base 1152b9c, with B, this PR tip 2403d00. Both use the same fixed read-ahead. The separate opt-in experiment is #397.

Please repeat the same save and scripted inputs in A/B/B/A order. Cover a city traversal, a steady interior and repeated save reloads or streaming transitions with background loading active. Include RT off/on, CPU-limited and GPU-limited settings, the normal title budget and a verified 512 MiB cap where supported. Reports from different Radeon generations and VRAM capacities are welcome. A paging-disabled comparison is a useful control. Keep driver, cache state, frame cap and frame generation settings matched. Report cold loads separately; for steady play, warm up one minute and record the next three.

Please attach CPU, GPU/VRAM, OS, driver, Wine/Proton, resolution, commit, actual title budget and per-run P50/P95/P99 frame times, FPS and frames over 50/100 ms. Raw frame-time samples and CPU submit time help establish whether the lock affects slow frames. For repeated load/unload, report completed cycles, the first failure, logs and any visual corruption, dropped draws, hangs or device loss. Neutral results are useful too.

**@⁠lucalocastro** on GitHub · 2026-12-03 23:54 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/392) `lucalocastro:codex/uma-fault-latency` → `main` · +439 −42 · 3 files · 3 commits · open A CPU fault can wait for a worker staging a migration in an unrelated physical pool. There is also a window during teardown when an allocator can reuse a numeric pool ID before the previous owner has finished clearing its metadata. - Lock only the participating pools, once each in ID order. A waiting reader rechecks its mapping. Two aliases of the same pool still synchronize. Release the locks before the driver kick. - Reserve a closing pool ID until teardown completes. Other pools can be allocated while the driver releases the old backing outside the global state mutex. ## Evidence In a synthetic Windows test with a forced 2 ms writer hold, median access time for an unrelated pool fell from **2.0125 to 0.0075 ms** (P99 from **2.1716 to 0.0143 ms**). Two views of the same backing page still waited about 2.01 ms and saw the writer's value. With no writer the median was 0.0013 ms in both arms. Five paired runs, 240 samples per case per run, originally measured on an integration checkout based on `eb51227`. In-game frame-time impact is open. The close/reuse fixture passed in 100 isolated Windows processes on the original combined branch. It covers both an unmapped address and a new section at the same virtual address, pool ID and offset. A competing allocation runs during teardown. The test uses real Windows views and simulates the driver free callback. After splitting the branch, the UMA unit tests and all three isolated lock, rebind and reuse tests passed. Earlier full-suite runs on this Windows machine stopped at `descriptor::tests::views_die_with_their_resource`. ## One A/B comparison requested Compare **A**, the common base `1152b9c`, with **B**, this PR tip `2403d00`. Both use the same fixed read-ahead. The separate opt-in experiment is #397. Please repeat the same save and scripted inputs in A/B/B/A order. Cover a city traversal, a steady interior and repeated save reloads or streaming transitions with background loading active. Include RT off/on, CPU-limited and GPU-limited settings, the normal title budget and a verified 512 MiB cap where supported. Reports from different Radeon generations and VRAM capacities are welcome. A paging-disabled comparison is a useful control. Keep driver, cache state, frame cap and frame generation settings matched. Report cold loads separately; for steady play, warm up one minute and record the next three. Please attach CPU, GPU/VRAM, OS, driver, Wine/Proton, resolution, commit, actual title budget and per-run P50/P95/P99 frame times, FPS and frames over 50/100 ms. Raw frame-time samples and CPU submit time help establish whether the lock affects slow frames. For repeated load/unload, report completed cycles, the first failure, logs and any visual corruption, dropped draws, hangs or device loss. Neutral results are useful too.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 23:57:20 +00:00
github-bot changed title from Scope UMA migration locks by pool and test adaptive CPU read-ahead to Scope UMA migration locks by pool and protect closing pool IDs 2026-12-04 00:07:54 +00:00
github-bot force-pushed github-bot/gh-392 from 268f2df04f to 2403d00164 2026-12-04 00:07:55 +00:00 Compare
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 02:23 UTC · original

Tested 2403d00 natively on Windows 11 (MSVC) with one NVIDIA desktop card, against the merge base 1152b9c.

Worth saying up front that um::load only picks an AMD Vulkan device, so none of the real fault, residency or teardown paths run here. Nothing on this machine runs tick, place or the kick either, so the residency.rs side is review only. What I could check is the lock and ID logic through the synthetic tests.

  • materialize_rechecks_pool_after_rebind, materialize_during_pool_close_and_reuse and closing_pool_reserves_id_until_cleanup_finishes each passed 20 out of 20, one process per run.
  • materialize_while_batch_holds_moving gives about the same numbers as the description. The unrelated pool with a 2 ms hold has a median around 0.007 ms, the alias still waits about 2.01 ms and sees the writer's value.
  • Full cargo test -p victory-gpu --lib (skipping views_die_with_their_resource as before) is 340 passed, 5 failed, 26 ignored on the PR and 339 / 5 / 23 on the base. Same 5 failures on both, the difference is just the new tests.
  • No new clippy warnings with --all-targets.

I also ran a throwaway stress test (not pushed) with a thread doing 1 ms write holds on pool A while two threads hammer faults on A and on an unrelated pool B for 3 s. On the base B's worst fault matched A's at 1.2 to 1.7 ms. On the PR it was 0.1 to 0.37 ms and B's mean went from about 3.8 to 1.45 µs. A second writer taking A and B together never deadlocked. Three threads looping insert_pool and close_pool (millions of cycles, 5 runs) never got an ID that was still inside its free callback. That only goes through the same seam as your unit test, not the real pool() and pool_gone.

From reading I didn't find an ordering problem. tick is the only writer, takes the pool locks in index order and only does driver calls and page flags under them. materialize holds at most one read lock and drops the old one before waiting on a rebound pool.

One small thing. close_pool removes the ID from closing only after free returns, so an unwind there would leave it reserved for good. Nothing in that path looks likely to panic, but a drop guard would cover it.

Not from this PR, but pool_to_card drops the state mutex and later calls pool_closing and pool_opened on the same ID without the reservation, so a pool_gone racing with a promotion could still leave stale lazy or residency metadata for a reused ID.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 02:23 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/392#issuecomment-7398889743) Tested 2403d00 natively on Windows 11 (MSVC) with one NVIDIA desktop card, against the merge base 1152b9c. Worth saying up front that `um::load` only picks an AMD Vulkan device, so none of the real fault, residency or teardown paths run here. Nothing on this machine runs tick, place or the kick either, so the residency.rs side is review only. What I could check is the lock and ID logic through the synthetic tests. - `materialize_rechecks_pool_after_rebind`, `materialize_during_pool_close_and_reuse` and `closing_pool_reserves_id_until_cleanup_finishes` each passed 20 out of 20, one process per run. - `materialize_while_batch_holds_moving` gives about the same numbers as the description. The unrelated pool with a 2 ms hold has a median around 0.007 ms, the alias still waits about 2.01 ms and sees the writer's value. - Full `cargo test -p victory-gpu --lib` (skipping `views_die_with_their_resource` as before) is 340 passed, 5 failed, 26 ignored on the PR and 339 / 5 / 23 on the base. Same 5 failures on both, the difference is just the new tests. - No new clippy warnings with `--all-targets`. I also ran a throwaway stress test (not pushed) with a thread doing 1 ms write holds on pool A while two threads hammer faults on A and on an unrelated pool B for 3 s. On the base B's worst fault matched A's at 1.2 to 1.7 ms. On the PR it was 0.1 to 0.37 ms and B's mean went from about 3.8 to 1.45 µs. A second writer taking A and B together never deadlocked. Three threads looping insert_pool and close_pool (millions of cycles, 5 runs) never got an ID that was still inside its free callback. That only goes through the same seam as your unit test, not the real pool() and pool_gone. From reading I didn't find an ordering problem. tick is the only writer, takes the pool locks in index order and only does driver calls and page flags under them. materialize holds at most one read lock and drops the old one before waiting on a rebound pool. One small thing. close_pool removes the ID from `closing` only after `free` returns, so an unwind there would leave it reserved for good. Nothing in that path looks likely to panic, but a drop guard would cover it. Not from this PR, but pool_to_card drops the state mutex and later calls pool_closing and pool_opened on the same ID without the reservation, so a pool_gone racing with a promotion could still leave stale lazy or residency metadata for a reused ID. AI-assisted: yes
Author
Owner

@⁠fsrajner on GitHub · 2026-12-04 08:14 UTC · original

Tested on top of main together with the other PRs listed below, with no measurable change in this scene. That is expected for a lock-scope change in a GPU-bound scene. It ran 4 × 2-minute sessions on this build with no hangs or errors.

Setup: RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, RADV from this repo's setup.sh. Series X, 3840x2160 output, FSR 4 Quality (4.1.2), RT at defaults, headless gamescope. Same save (the intro); the game stops 120 s after the first press. The figures come from the 5 s stats: frame windows after loading. 2 runs per variant; main = 0fd7614. The PR build is main + #382 + #392 + #393 + #408 + #445 + #369 together. #398 was left out because it conflicts with #369 in the residency planning. The #369 merge conflicted only with local counters.

variant ms/frame (runs) 5 s windows > 40 ms worst 5% worst window long frames (longest)
main 47 (48, 45) 86.5% 59.3 ms 62.8 ms 1184 (275 ms)
PR build, defaults 46 (46, 46) 85.7% 61.3 ms 82.9 ms 1176 (254 ms)
PR build + VICTORY_TEXTURE_BARRIERS=1 46 (46, 46) 88.9% 59.0 ms 61.5 ms 1137 (172 ms)
PR build + VICTORY_CARD_KEPT_MB=9216 45 (44, 46) 83.8% 63.4 ms 65.1 ms 1171 (201 ms)

The first run of main and of the PR build each began by re-preparing shaders after a translator change. That is likely where the 48 ms run and the 82.9 ms worst window come from. Otherwise the spread between runs is about ±1 ms/frame, so all of these are within noise. In this scene the card is the limit (about 21-24 ms of GPU wait per frame).

**@⁠fsrajner** on GitHub · 2026-12-04 08:14 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/392#issuecomment-7405689144) Tested on top of main together with the other PRs listed below, with no measurable change in this scene. That is expected for a lock-scope change in a GPU-bound scene. It ran 4 × 2-minute sessions on this build with no hangs or errors. **Setup:** RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, RADV from this repo's `setup.sh`. Series X, 3840x2160 output, FSR 4 Quality (4.1.2), RT at defaults, headless gamescope. Same save (the intro); the game stops 120 s after the first press. The figures come from the 5 s `stats: frame` windows after loading. 2 runs per variant; main = 0fd7614. The PR build is main + #382 + #392 + #393 + #408 + #445 + #369 together. #398 was left out because it conflicts with #369 in the residency planning. The #369 merge conflicted only with local counters. | variant | ms/frame (runs) | 5 s windows > 40 ms | worst 5% | worst window | long frames (longest) | |---|---|---|---|---|---| | main | 47 (48, 45) | 86.5% | 59.3 ms | 62.8 ms | 1184 (275 ms) | | PR build, defaults | 46 (46, 46) | 85.7% | 61.3 ms | 82.9 ms | 1176 (254 ms) | | PR build + `VICTORY_TEXTURE_BARRIERS=1` | 46 (46, 46) | 88.9% | 59.0 ms | 61.5 ms | 1137 (172 ms) | | PR build + `VICTORY_CARD_KEPT_MB=9216` | 45 (44, 46) | 83.8% | 63.4 ms | 65.1 ms | 1171 (201 ms) | The first run of main and of the PR build each began by re-preparing shaders after a translator change. That is likely where the 48 ms run and the 82.9 ms worst window come from. Otherwise the spread between runs is about ±1 ms/frame, so all of these are within noise. In this scene the card is the limit (about 21-24 ms of GPU wait per frame).
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 11:28 UTC · original

The shape is right and I could not break it: a fault path takes at most one read guard and only after releasing the page lock (um/lazy.rs:700 after :697), so it cannot invert against the writer's ascending order; moving_pools[id] cannot go out of bounds because only id < MOST_POOLS pools are registered (um/residency.rs:178, :510); and materialize re-checks the pool id after re-taking the page lock and drops the stale guard (:688-690), which is the case that would otherwise use a closed pool's lock.

Two asks:

  • Keep the six deleted lines of rationale at crates/victory-gpu/src/um/residency.rs:791-796. They are the only place that records why the lock is dropped before the kernel kick (every thread that first-touched any page waited for the whole move), and that ordering is now the entire point of the patch.
  • The pool.dead && pool.views == 0 teardown in unalias (um.rs:1399-1405) still frees under the state mutex and never touches closing — safe, but exactly the shape close_pool exists to stop, and the new rule's comment does not mention it.

Merging note: this and #398 both edit the lazy::moving() line and add a row at the same spot in docs/SETTINGS.md; whoever lands second rebases.

**@⁠Luxuride** on GitHub · 2026-12-04 11:28 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/392#issuecomment-7410143016) The shape is right and I could not break it: a fault path takes at most one read guard and only after releasing the page lock (`um/lazy.rs:700` after `:697`), so it cannot invert against the writer's ascending order; `moving_pools[id]` cannot go out of bounds because only `id < MOST_POOLS` pools are registered (`um/residency.rs:178`, `:510`); and `materialize` re-checks the pool id after re-taking the page lock and drops the stale guard (`:688-690`), which is the case that would otherwise use a closed pool's lock. Two asks: - Keep the six deleted lines of rationale at `crates/victory-gpu/src/um/residency.rs:791-796`. They are the only place that records *why* the lock is dropped before the kernel kick (every thread that first-touched any page waited for the whole move), and that ordering is now the entire point of the patch. - The `pool.dead && pool.views == 0` teardown in `unalias` (`um.rs:1399-1405`) still frees under the state mutex and never touches `closing` — safe, but exactly the shape `close_pool` exists to stop, and the new rule's comment does not mention it. Merging note: this and #398 both edit the `lazy::moving()` line and add a row at the same spot in `docs/SETTINGS.md`; whoever lands second rebases.
Author
Owner

@⁠lucalocastro on GitHub · 2026-12-04 12:04 UTC · original · edited 2026-12-04 12:09 UTC

@⁠Kal0s-design @⁠Luxuride I’ve prepared a local commit that releases the closing reservation through a drop guard and adds a regression test for unwinding from free. I restored the six lines explaining why the guards are dropped before kick. I also documented the last-view path in forget: the caller holds the state mutex throughout free_pool, so the allocator cannot reuse the ID during that cleanup. The pool_to_card follow-up is separate.

@⁠fsrajner Thanks for testing on AMD. The combined build showed no measurable frame-time gain in the intro. The contention improvement reported on the synthetic test is specific to faults on an unrelated pool.

The changes are now on this PR in 8105929cf1. I checked the patch against the PR head; I haven’t run the Rust tests on this revision.

**@⁠lucalocastro** on GitHub · 2026-12-04 12:04 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/392#issuecomment-7410947793) · edited 2026-12-04 12:09 UTC @⁠Kal0s-design @⁠Luxuride I’ve prepared a local commit that releases the `closing` reservation through a drop guard and adds a regression test for unwinding from `free`. I restored the six lines explaining why the guards are dropped before `kick`. I also documented the last-view path in `forget`: the caller holds the state mutex throughout `free_pool`, so the allocator cannot reuse the ID during that cleanup. The `pool_to_card` follow-up is separate. @⁠fsrajner Thanks for testing on AMD. The combined build showed no measurable frame-time gain in the intro. The contention improvement reported on the synthetic test is specific to faults on an unrelated pool. The changes are now on this PR in 8105929cf1fad1a81690cb790221eb44ebf60337. I checked the patch against the PR head; I haven’t run the Rust tests on this revision.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/392/head:github-bot/gh-392'
git switch 'github-bot/gh-392'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-392'
git switch 'github-bot/gh-392'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-392'
git switch 'github-bot/gh-392'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-392'
git switch 'main'
git merge --squash 'github-bot/gh-392'
git switch 'main'
git merge --ff-only 'github-bot/gh-392'
git switch 'main'
git merge 'github-bot/gh-392'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.