Skip to content

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

Draft
inf1425 wants to merge 1 commit into
xcl-dev:nvidia-devfrom
inf1425:kernelx/graphics-release-before-windows
Draft

inf1425 wants to merge 1 commit into
xcl-dev:nvidia-devfrom
inf1425:kernelx/graphics-release-before-windows

Conversation

@inf1425

@inf1425 inf1425 commented Nov 26, 2026 •

Copy link
Copy Markdown

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

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.

…dress out

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)
@Anon64

Anon64 commented Nov 26, 2026

Copy link
Copy Markdown

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.

@Prime1997 Prime1997 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Prime1997 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants