D3D12.X: reject PAGE_GUARD/overflow in cpu_writes and handle stale fence wakeups #1369

Open
b3IKK wants to merge 1 commit from upstream-pr/1379 into main
b3IKK commented 2026-11-24 15:24:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T15:24:31Z.


Fixes two bugs in crates/xcl/src/hle/d3d12x/cpu_writes.rs:

  • writable() and write_now() omitted the PAGE_GUARD check and overflow-checked arithmetic that write_u64_now() already performed, so a PAGE_GUARD page reported writable and could fault the worker thread on store, and large va + len values overflowed usize.
  • The worker reused one auto-reset event across jobs with a single WaitForSingleObject call: if an earlier job timed out and its fence completed later while waiting on a subsequent job, the stale signal woke the worker prematurely and applied bottom-of-pipe writes before the GPU finished.

This shares one is_writable_committed() helper across writable(), write_now(), and write_u64_now(), and extracts wait_for_fence() which resets the event before arming and loops until GetCompletedValue() >= value or the wait deadline expires.

What is in it

  • crates/xcl/src/hle/d3d12x/cpu_writes.rs: unify PAGE_GUARD + checked bounds in is_writable_committed(); add wait_for_fence() deadline loop with ResetEvent; add unit tests writable_and_write_now_reject_guard_page_and_overflow (verified failing before the fix) and wait_for_fence_ignores_stale_wakeup_from_timed_out_fence.

Since the last push: rebased onto main 06c9f55. main already has u64_writable_now with the PAGE_GUARD and alignment checks, so only the dword path and writable() share the new is_writable_committed; the u64 path stays main's. The wait_for_fence change merged unchanged.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 1267 passed, 0 failed, 46 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required))
  • cargo test -p xcl --lib cpu_writes: 5 passed, 0 failed (writable_and_write_now_reject_guard_page_and_overflow failed before the fix)

Regression run

main (426a5a7, clean tree, no local changes):

xcl-smoke v1
commit: 426a5a745db4d5e4b037449ede0b70cb342f5192
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15158 MB

PR (aaf89e0, clean tree, no local changes):

xcl-smoke v1
commit: aaf89e0c80efb787311646ace7ff3a3022ce841b
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1644 (PresentX 1644)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 16189 MB

(The main block is 426a5a7, an ancestor of the current main, from the same machine and data folder.)

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Claude Fable 5.2: rebased onto main 06c9f55, resolved the conflicts, ran the suite and the scripts/smoke.ps1 pair at the head above.
<!-- greyfinch-sync:ivall/xcl:issue:5475086600 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1379). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T15:24:31Z. --- Fixes two bugs in `crates/xcl/src/hle/d3d12x/cpu_writes.rs`: - `writable()` and `write_now()` omitted the `PAGE_GUARD` check and overflow-checked arithmetic that `write_u64_now()` already performed, so a `PAGE_GUARD` page reported writable and could fault the worker thread on store, and large `va + len` values overflowed `usize`. - The worker reused one auto-reset event across jobs with a single `WaitForSingleObject` call: if an earlier job timed out and its fence completed later while waiting on a subsequent job, the stale signal woke the worker prematurely and applied bottom-of-pipe writes before the GPU finished. This shares one `is_writable_committed()` helper across `writable()`, `write_now()`, and `write_u64_now()`, and extracts `wait_for_fence()` which resets the event before arming and loops until `GetCompletedValue() >= value` or the wait deadline expires. **What is in it** - `crates/xcl/src/hle/d3d12x/cpu_writes.rs`: unify `PAGE_GUARD` + checked bounds in `is_writable_committed()`; add `wait_for_fence()` deadline loop with `ResetEvent`; add unit tests `writable_and_write_now_reject_guard_page_and_overflow` (verified failing before the fix) and `wait_for_fence_ignores_stale_wakeup_from_timed_out_fence`. **Since the last push**: rebased onto `main` `06c9f55`. `main` already has `u64_writable_now` with the `PAGE_GUARD` and alignment checks, so only the dword path and `writable()` share the new `is_writable_committed`; the u64 path stays `main`'s. The `wait_for_fence` change merged unchanged. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): **1267 passed, 0 failed, 46 ignored** (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, `no heap windows`, and `cannot disable the driver cache (Windows Developer Mode is required)`) - `cargo test -p xcl --lib cpu_writes`: 5 passed, 0 failed (`writable_and_write_now_reject_guard_page_and_overflow` failed before the fix) **Regression run** main (`426a5a7`, clean tree, no local changes): ``` xcl-smoke v1 commit: 426a5a745db4d5e4b037449ede0b70cb342f5192 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1642 (PresentX 1642) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15158 MB ``` PR (`aaf89e0`, clean tree, no local changes): ``` xcl-smoke v1 commit: aaf89e0c80efb787311646ace7ff3a3022ce841b gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1644 (PresentX 1644) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 16189 MB ``` (The main block is `426a5a7`, an ancestor of the current `main`, from the same machine and data folder.) **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Claude Fable 5.2: rebased onto `main` `06c9f55`, resolved the conflicts, ran the suite and the `scripts/smoke.ps1` pair at the head above.
futurepro62 commented 2026-11-24 16:07:08 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:32:30Z.


Full-suite run, since this PR has no test report yet. Tested exact head 7b3fc47 (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: writable_and_write_now_reject_guard_page_and_overflow, wait_for_fence_ignores_stale_wakeup_from_timed_out_fence.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7136201131 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1379#issuecomment-7136201131). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T15:32:30Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `7b3fc47` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `writable_and_write_now_reject_guard_page_and_overflow`, `wait_for_fence_ignores_stale_wakeup_from_timed_out_fence`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **753 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 17:06:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T16:49:43Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7138269170 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1379#issuecomment-7138269170). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T16:49:43Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
DavidM1911 commented 2026-11-26 05:05:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:03:19Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7183139114 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1379#issuecomment-7183139114). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T05:03:19Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-12-01 02:31:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:31:57Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7298717169 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1379#issuecomment-7298717169). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:31:57Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:34:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:34:07Z.


Triage (#1607) – head aaf89e0

#1819 (opened later) has the same PAGE_GUARD/overflow helper and a ResetEvent before arming the fence. It also moves to per-queue workers and 64-bit stores, but it does not loop on GetCompletedValue after a stale wakeup the way this PR's wait_for_fence does. Main still has neither fix.
In cpu_writes.rs, write_now drops the uma.in_committed_window fast path that writable() keeps. Every write in unified memory now costs a VirtualQuery, and a range writable() accepted can be dropped by write_now if VirtualQuery reports it as more than one region.
The branch conflicts heavily with main, where write_now became write_now_in and fence reads go through real::fence_completed.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301906454 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1379#issuecomment-7301906454). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:34:07Z. --- **Triage (#1607)** – head aaf89e0 #1819 (opened later) has the same `PAGE_GUARD`/overflow helper and a `ResetEvent` before arming the fence. It also moves to per-queue workers and 64-bit stores, but it does not loop on `GetCompletedValue` after a stale wakeup the way this PR's `wait_for_fence` does. Main still has neither fix. In `cpu_writes.rs`, `write_now` drops the `uma.in_committed_window` fast path that `writable()` keeps. Every write in unified memory now costs a VirtualQuery, and a range `writable()` accepted can be dropped by `write_now` if VirtualQuery reports it as more than one region. The branch conflicts heavily with main, where `write_now` became `write_now_in` and fence reads go through `real::fence_completed`. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1379:upstream-pr/1379'
git switch 'upstream-pr/1379'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1379'
git switch 'upstream-pr/1379'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1379'
git switch 'upstream-pr/1379'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1379'
git switch 'main'
git merge --squash 'upstream-pr/1379'
git switch 'main'
git merge --ff-only 'upstream-pr/1379'
git switch 'main'
git merge 'upstream-pr/1379'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!1369
No description provided.