kernelx: forget a graphics reservation before Windows can hand its address out #2772

Open
github-bot wants to merge 1 commit from github-bot/gh-2772 into nvidia-dev AGit
Owner

@⁠inf1425 on GitHub · 2026-11-26 11:38 UTC · original
inf1425:kernelx/graphics-release-before-windows → nvidia-dev · +72 −11 · 1 files · 1 commits · draft

VirtualFree of a whole graphics reservation (MEM_RELEASE, size 0) unpairs it before Windows releases it. Three other cleanup steps only ran after the release: removing it from GRAPHICS_RESERVATIONS, forgetting the title's page mappings there (forget_title_mappings), and clearing the places of shown pages kept there (mirrors::place_gone). Once Windows has released the range, it can give the same address to another thread, and those late steps then wiped the new reservation's records too. The new reservation was no longer a graphics reservation, and its GPU-view pages showed nothing. Its own later release then skipped the unpairing, because the record was gone, which left a stale pair that kept the next CPU view at that address from pairing. In a title, this happens whenever one thread releases graphics memory while another reserves it. Found while checking the nvidia-dev test failures reported in #42.

The cleanup now runs before Windows is called, as the sparse and unified-memory release paths in the same function already do. If Windows refuses the release, the reservation's record is put back. Sparse is still told about the shown pages (remapped) after the call.

What is in it

  • crates/xcl/src/hle/kernelx.rs, VirtualFree: the graphics-reservation cleanup moves before the Windows call, and the record is restored on failure.
  • crates/xcl/src/hle/kernelx.rs, tests: AFTER_WINDOWS_RELEASE, a test-only hook that runs right after Windows released memory on this thread, and a_reservation_made_where_a_graphics_reservation_just_went_keeps_its_own_record. The test reserves the released address again from the hook, maps a page there, and checks that the reservation and the mapping survive the release. It fails with the old order ("the new reservation is still a graphics reservation").

Tested

  • NVIDIA GeForce RTX 4060 Laptop GPU (8 GB, driver 616.64), Ryzen 7 7840HS, Windows 11 build 26300, Rust 1.95.0, debug build.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at this PR's head: 1746 passed, 5 failed, 48 ignored. The 5 failures are the ones reported in #42: four are fixed by #2770 and #2771, and the mesh test is in #2632.
  • SKIPPED GPU tests (26): 21 need wave64; 3 are f16 ops the per-lane form refuses (#1426); 2 need XCL_HEAP_WINDOWS=1.
  • cargo test -p xcl --lib hle::kernelx -- --test-threads=4, repeated:
    • unmodified nvidia-dev: 10 failures in 40 runs, across 3 tests;
    • unmodified, without the test #2773 fixes: a_page_leaving_plain_memory_has_the_tiles_over_it_linked_again failed 3 times in 40 runs, and cpu_view's pairing assert failed once in 24;
    • this PR, without the test #2773 fixes: 0 failures in 80 runs;
    • with #2773 as well: 0 failures in 100 runs.
  • With #2770, #2771 and #2773 merged on top: 1750 passed, 1 failed (the #2632 mesh test), 48 ignored, in 2 full runs.

Regression run
This changes runtime code. Smoke blocks from nvidia-dev and from this head will be added once a title is available; until then this stays a draft.

main:

PR:

Not tested

  • AMD, other NVIDIA cards, Linux/Wine, a release build, and any retail title (smoke run pending). The race was only observed in the unit tests. I have no retail evidence that a title hits it.

AI use

  • Claude Code (Claude Opus 5.5) traced the flaky tests to this ordering using temporary logging (not part of the PR), wrote the fix and the test, and ran the tests and stress runs. I read every line of the diff.
**@⁠inf1425** on GitHub · 2026-11-26 11:38 UTC · [original](https://github.com/xcl-dev/xcl/pull/2772) `inf1425:kernelx/graphics-release-before-windows` → `nvidia-dev` · +72 −11 · 1 files · 1 commits · draft `VirtualFree` of a whole graphics reservation (`MEM_RELEASE`, size 0) unpairs it before Windows releases it. Three other cleanup steps only ran after the release: removing it from `GRAPHICS_RESERVATIONS`, forgetting the title's page mappings there (`forget_title_mappings`), and clearing the places of shown pages kept there (`mirrors::place_gone`). Once Windows has released the range, it can give the same address to another thread, and those late steps then wiped the new reservation's records too. The new reservation was no longer a graphics reservation, and its GPU-view pages showed nothing. Its own later release then skipped the unpairing, because the record was gone, which left a stale pair that kept the next CPU view at that address from pairing. In a title, this happens whenever one thread releases graphics memory while another reserves it. Found while checking the `nvidia-dev` test failures reported in #42. The cleanup now runs before Windows is called, as the sparse and unified-memory release paths in the same function already do. If Windows refuses the release, the reservation's record is put back. Sparse is still told about the shown pages (`remapped`) after the call. **What is in it** - `crates/xcl/src/hle/kernelx.rs`, `VirtualFree`: the graphics-reservation cleanup moves before the Windows call, and the record is restored on failure. - `crates/xcl/src/hle/kernelx.rs`, tests: `AFTER_WINDOWS_RELEASE`, a test-only hook that runs right after Windows released memory on this thread, and `a_reservation_made_where_a_graphics_reservation_just_went_keeps_its_own_record`. The test reserves the released address again from the hook, maps a page there, and checks that the reservation and the mapping survive the release. It fails with the old order ("the new reservation is still a graphics reservation"). **Tested** - NVIDIA GeForce RTX 4060 Laptop GPU (8 GB, driver 616.64), Ryzen 7 7840HS, Windows 11 build 26300, Rust 1.95.0, debug build. - `cargo test --workspace --no-fail-fast -- --test-threads=4` at this PR's head: 1746 passed, 5 failed, 48 ignored. The 5 failures are the ones reported in #42: four are fixed by #2770 and #2771, and the mesh test is in #2632. - SKIPPED GPU tests (26): 21 need wave64; 3 are f16 ops the per-lane form refuses (#1426); 2 need `XCL_HEAP_WINDOWS=1`. - `cargo test -p xcl --lib hle::kernelx -- --test-threads=4`, repeated: - unmodified `nvidia-dev`: 10 failures in 40 runs, across 3 tests; - unmodified, without the test #2773 fixes: `a_page_leaving_plain_memory_has_the_tiles_over_it_linked_again` failed 3 times in 40 runs, and `cpu_view`'s pairing assert failed once in 24; - this PR, without the test #2773 fixes: 0 failures in 80 runs; - with #2773 as well: 0 failures in 100 runs. - With #2770, #2771 and #2773 merged on top: 1750 passed, 1 failed (the #2632 mesh test), 48 ignored, in 2 full runs. **Regression run** This changes runtime code. Smoke blocks from `nvidia-dev` and from this head will be added once a title is available; until then this stays a draft. main: PR: **Not tested** - AMD, other NVIDIA cards, Linux/Wine, a release build, and any retail title (smoke run pending). The race was only observed in the unit tests. I have no retail evidence that a title hits it. **AI use** - Claude Code (Claude Opus 5.5) traced the flaky tests to this ordering using temporary logging (not part of the PR), wrote the fix and the test, and ran the tests and stress runs. I read every line of the diff.
VirtualFree of a whole graphics reservation (MEM_RELEASE, size 0)
unpaired it before Windows released it, but removed it from
GRAPHICS_RESERVATIONS, forgot the title's page mappings there
(forget_title_mappings) and cleared the places of shown pages kept
there (mirrors::place_gone) only afterwards. Once Windows has released
the range, another thread can be given the same address; that
bookkeeping then took the new reservation's record, mappings and
places too. The new reservation stopped being a graphics reservation,
its GPU-view pages showed nothing, and its later release skipped the
unpairing (no record), leaving a stale pair that kept the next CPU view
at that address from pairing.

The kernelx tests hit this in parallel runs (reserve_watched memory is
released and reserved by several tests at once): with
--test-threads=4, memory_under_a_shown_page_is_told_to_sparse_before_it_goes,
a_page_leaving_plain_memory_has_the_tiles_over_it_linked_again (both
`Shows::Nothing` right after apply_batches) and cpu_view's pairing
assert failed in about 1 of 13 runs of hle::kernelx; with this change,
0 in 80 runs.

The bookkeeping now happens before Windows is called, as the sparse and
unified-memory release paths above already do; if Windows refuses the
release, the reservation's record is put back. The shown pages are
still told to sparse (remapped) after the call.

New test: a_reservation_made_where_a_graphics_reservation_just_went_keeps_its_own_record
reserves the released address again from a test-only hook that runs
right after Windows released it (AFTER_WINDOWS_RELEASE), maps a page
there, and checks both survive. It fails with the old order ("the new
reservation is still a graphics reservation").

Assisted-by: Claude Code (Claude Opus 5.5)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:40:52 +00:00
Author
Owner

@⁠Anon64 on GitHub · 2026-11-26 12:00 UTC · original

Regression run of 18b34a9 on a newer retail title: no device removal, no crash, frames and commit within the limits.

Setup: RTX 3060 12 GB (NVIDIA 581.80), i9-14900KF, Windows 11 26200, release builds of main b49be18 (this PR's base) and of this PR's exact head, scripts/smoke.ps1 -Game <title> for 60 s with XCL_NETWORK=0 and A pressed every 4 s from 20 s.

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 3060
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1586 (PresentX 1587)
presents per 2 s (last 3): 60 47 54
device removals: 0
crashes: 0
peak commit: 19185 MB

this PR:

xcl-smoke v1
commit: 18b34a9d992f02b5b3c12c3799e9add8acb847d8
gpu: NVIDIA GeForce RTX 3060
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1661 (PresentX 1661)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19587 MB

105 % of main's frames, 102 % of its peak commit. One run each.

Not tested: the suite on this head; whether the title reaches the reservation path this PR changes in its first 60 s; AMD.

AI use: Claude Code (Claude Opus 5.5) ran these runs on this machine and wrote this report.

**@⁠Anon64** on GitHub · 2026-11-26 12:00 UTC · [original](https://github.com/xcl-dev/xcl/pull/2772#issuecomment-7191457271) **Regression run of `18b34a9` on a newer retail title: no device removal, no crash, frames and commit within the limits.** **Setup:** RTX 3060 12 GB (NVIDIA 581.80), i9-14900KF, Windows 11 26200, release builds of main `b49be18` (this PR's base) and of this PR's exact head, `scripts/smoke.ps1 -Game <title>` for 60 s with `XCL_NETWORK=0` and A pressed every 4 s from 20 s. main: ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 3060 title: 21222967 seconds: 60 exit: 0x0 frames shown: 1586 (PresentX 1587) presents per 2 s (last 3): 60 47 54 device removals: 0 crashes: 0 peak commit: 19185 MB ``` this PR: ``` xcl-smoke v1 commit: 18b34a9d992f02b5b3c12c3799e9add8acb847d8 gpu: NVIDIA GeForce RTX 3060 title: 21222967 seconds: 60 exit: 0x0 frames shown: 1661 (PresentX 1661) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19587 MB ``` 105 % of main's frames, 102 % of its peak commit. One run each. **Not tested:** the suite on this head; whether the title reaches the reservation path this PR changes in its first 60 s; AMD. AI use: Claude Code (Claude Opus 5.5) ran these runs on this machine and wrote this report.
Author
Owner

@⁠Prime1997 on GitHub · 2026-11-26 12:40 UTC · original · reviewed

Needs changes: preserve title-page mappings when Windows rejects a release. On head 18b34a9d992f02b5b3c12c3799e9add8acb847d8, the pre-release cleanup clears the title mapping before calling Windows, but failure restores only the graphics-reservation record. Please preserve/restore the mapping on failure while retaining the successful-release address-reuse fix.

I verified this with the same test-only CPU fixture added to the exact NVIDIA base and PR head; runtime code was unchanged. It maps one owned physical page into a watched reservation, calls VirtualFree(r, 0, MEM_RELEASE | MEM_DECOMMIT), and checks the mapping survives rejection. On both revisions Windows rejected the call, VirtualQuery showed the allocation and page state remained, and cleanup assertions passed. Only the head lost the mapping: title_at(r) was None instead of Some(16).

Revision + test-only fixture Passed Failed Ignored Skipped
Base af2cecc40886a8682ecf3267f5328c6dc4b66ad7 1 0 0 0
Head 18b34a9d992f02b5b3c12c3799e9add8acb847d8 0 1 0 0

Both built with cargo test --locked --offline -p xcl --lib --no-run --message-format=json --target-dir <isolated-target>, then ran <test-exe> hle::kernelx::tests::a_rejected_graphics_release_keeps_the_title_page_mapping --exact --test-threads=1 --nocapture. Only private executable/target paths are placeholders. Fresh processes used XCL_UMA_WINDOWS=4 XCL_UMA_MB=16 XCL_COHERENT_MB=16 XCL_HEAP_WINDOWS=0.

Windows 11 Pro 26200, i5-10400, 32 GiB RAM, Rust 1.100.0 MSVC debug tests. The fixture has no skip branch and cleans its owned allocation before the final assertion. This tests rejected-call bookkeeping; no GPU, full-suite, successful-release race or retail-title result is claimed. Test overlay SHA-256: base 21c7aa67a5514a7a1483330c8a58091ae6d42e87eff0d43b1c866578c39c8b52, head 34abd3a8a8514bcb76569e14f2d5bb39f0cf6007d72cff1bad563a9f4f771e8a (identical added test, different patch context).

AI use: Codex (GPT-6) reviewed the source, prepared the fixture, ran the paired checks and independently audited the raw results. Optional temporary Discord for coordinating contributor work, testing and comparing results.

**@⁠Prime1997** on GitHub · 2026-11-26 12:40 UTC · [original](https://github.com/xcl-dev/xcl/pull/2772#pullrequestreview-6074283919) · **reviewed** **Needs changes: preserve title-page mappings when Windows rejects a release.** On head `18b34a9d992f02b5b3c12c3799e9add8acb847d8`, [the pre-release cleanup](https://github.com/xcl-dev/xcl/blob/18b34a9d992f02b5b3c12c3799e9add8acb847d8/crates/xcl/src/hle/kernelx.rs#L1261-L1282) clears the title mapping before calling Windows, but failure restores only the graphics-reservation record. Please preserve/restore the mapping on failure while retaining the successful-release address-reuse fix. I verified this with the same test-only CPU fixture added to the exact NVIDIA base and PR head; runtime code was unchanged. It maps one owned physical page into a watched reservation, calls `VirtualFree(r, 0, MEM_RELEASE | MEM_DECOMMIT)`, and checks the mapping survives rejection. On both revisions Windows rejected the call, `VirtualQuery` showed the allocation and page state remained, and cleanup assertions passed. Only the head lost the mapping: `title_at(r)` was `None` instead of `Some(16)`. | Revision + test-only fixture | Passed | Failed | Ignored | Skipped | | --- | ---: | ---: | ---: | ---: | | Base `af2cecc40886a8682ecf3267f5328c6dc4b66ad7` | 1 | 0 | 0 | 0 | | Head `18b34a9d992f02b5b3c12c3799e9add8acb847d8` | 0 | 1 | 0 | 0 | Both built with `cargo test --locked --offline -p xcl --lib --no-run --message-format=json --target-dir <isolated-target>`, then ran `<test-exe> hle::kernelx::tests::a_rejected_graphics_release_keeps_the_title_page_mapping --exact --test-threads=1 --nocapture`. Only private executable/target paths are placeholders. Fresh processes used `XCL_UMA_WINDOWS=4 XCL_UMA_MB=16 XCL_COHERENT_MB=16 XCL_HEAP_WINDOWS=0`. Windows 11 Pro 26200, i5-10400, 32 GiB RAM, Rust 1.100.0 MSVC debug tests. The fixture has no skip branch and cleans its owned allocation before the final assertion. This tests rejected-call bookkeeping; no GPU, full-suite, successful-release race or retail-title result is claimed. Test overlay SHA-256: base `21c7aa67a5514a7a1483330c8a58091ae6d42e87eff0d43b1c866578c39c8b52`, head `34abd3a8a8514bcb76569e14f2d5bb39f0cf6007d72cff1bad563a9f4f771e8a` (identical added test, different patch context). AI use: Codex (GPT-6) reviewed the source, prepared the fixture, ran the paired checks and independently audited the raw results. Optional temporary [Discord](https://discord.gg/gK3y7WRRY) for coordinating contributor work, testing and comparing results.
Author
Owner

@⁠Prime1997 on GitHub · 2026-11-26 13:19 UTC · original · reviewed

Follow-up to the paired base/head regression report. Exact PR head 18b34a9d992f02b5b3c12c3799e9add8acb847d8. GitHub rejected editing that review, so this adds the new measured repair results separately.

Additional controlled repair check, 2026-11-26: a local, uncommitted repair keeps the existing ownership locks across the Windows call and commits title/pair/shown-page removal only after success. It preserves the successful-release reuse protection. This has not been pushed to this PR.

Same five CPU fixtures, each in a fresh process Passed Failed Ignored Skipped
PR runtime unchanged, new regression fixtures added 2 3 expected 0 0
PR head plus local repair and identical fixtures 5 0 0 0

The three red failures were lost title mapping, changed shown-page metadata, and lost CPU/GPU pairing after a rejected release. Each fixture checked the retained OS allocation and completed its owned-allocation cleanup before the final failing assertion. The existing successful address-reuse and CPU-view decommit tests passed on both variants.

Exact filters, using cargo test --locked --offline -p xcl --lib --no-run --message-format=json --target-dir <isolated-target>, then <test-exe> <exact-name> --exact --test-threads=1 --nocapture; XCL_UMA_WINDOWS=4 XCL_UMA_MB=16 XCL_COHERENT_MB=16 XCL_HEAP_WINDOWS=0:

  • hle::kernelx::tests::a_rejected_graphics_release_keeps_the_title_page_mapping
  • hle::kernelx::tests::a_rejected_graphics_release_keeps_a_shown_page_place
  • hle::kernelx::tests::a_rejected_cpu_view_release_keeps_its_pair_and_shown_page
  • hle::kernelx::tests::a_reservation_made_where_a_graphics_reservation_just_went_keeps_its_own_record
  • hle::kernelx::tests::a_decommit_or_release_of_a_cpu_view_is_told_to_sparse_first

Repair overlay SHA-256: 888c2a6c45813f0424fd16749d324797afa40c68eaeb411148daed15efcdf422. Test-only red overlay: 6064921abe6461844c7b3a2b7bd06d3e40653c4400718b236abd327f996ab29c. These finite CPU tests validate the measured rejected-call ownership behavior; they do not establish contention safety, GPU-cache recreation, latest-main integration, or retail gameplay. Both variants are based on the exact PR head above.

AI use: Codex (GPT-6) reviewed the source, prepared the fixture, ran the paired checks and independently audited the raw results. Optional temporary Discord for coordinating contributor work, testing and comparing results.

**@⁠Prime1997** on GitHub · 2026-11-26 13:19 UTC · [original](https://github.com/xcl-dev/xcl/pull/2772#pullrequestreview-6074814507) · **reviewed** Follow-up to [the paired base/head regression report](https://git.gtahub.st/github/xcl-dev--xcl/pulls/2772). Exact PR head `18b34a9d992f02b5b3c12c3799e9add8acb847d8`. GitHub rejected editing that review, so this adds the new measured repair results separately. **Additional controlled repair check, 2026-11-26:** a local, uncommitted repair keeps the existing ownership locks across the Windows call and commits title/pair/shown-page removal only after success. It preserves the successful-release reuse protection. This has not been pushed to this PR. | Same five CPU fixtures, each in a fresh process | Passed | Failed | Ignored | Skipped | | --- | ---: | ---: | ---: | ---: | | PR runtime unchanged, new regression fixtures added | 2 | 3 expected | 0 | 0 | | PR head plus local repair and identical fixtures | 5 | 0 | 0 | 0 | The three red failures were lost title mapping, changed shown-page metadata, and lost CPU/GPU pairing after a rejected release. Each fixture checked the retained OS allocation and completed its owned-allocation cleanup before the final failing assertion. The existing successful address-reuse and CPU-view decommit tests passed on both variants. Exact filters, using `cargo test --locked --offline -p xcl --lib --no-run --message-format=json --target-dir <isolated-target>`, then `<test-exe> <exact-name> --exact --test-threads=1 --nocapture`; `XCL_UMA_WINDOWS=4 XCL_UMA_MB=16 XCL_COHERENT_MB=16 XCL_HEAP_WINDOWS=0`: - `hle::kernelx::tests::a_rejected_graphics_release_keeps_the_title_page_mapping` - `hle::kernelx::tests::a_rejected_graphics_release_keeps_a_shown_page_place` - `hle::kernelx::tests::a_rejected_cpu_view_release_keeps_its_pair_and_shown_page` - `hle::kernelx::tests::a_reservation_made_where_a_graphics_reservation_just_went_keeps_its_own_record` - `hle::kernelx::tests::a_decommit_or_release_of_a_cpu_view_is_told_to_sparse_first` Repair overlay SHA-256: `888c2a6c45813f0424fd16749d324797afa40c68eaeb411148daed15efcdf422`. Test-only red overlay: `6064921abe6461844c7b3a2b7bd06d3e40653c4400718b236abd327f996ab29c`. These finite CPU tests validate the measured rejected-call ownership behavior; they do not establish contention safety, GPU-cache recreation, latest-main integration, or retail gameplay. Both variants are based on the exact PR head above. AI use: Codex (GPT-6) reviewed the source, prepared the fixture, ran the paired checks and independently audited the raw results. Optional temporary [Discord](https://discord.gg/gK3y7WRRY) for coordinating contributor work, testing and comparing results.
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/2772/head:github-bot/gh-2772'
git switch 'github-bot/gh-2772'

Merge

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