GPU, HLE: an announced kernel read stays open until it lands #1025

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

@⁠Dufftwt on GitHub · 2026-11-24 15:09 UTC · original
Dufftwt:fix/announced-kernel-reads-stay-open → main · +116 −11 · 4 files · 2 commits · closed without merging

Fixes #723.

What's wrong

View memory (xmem's physical-page views) is tracked by making 64 KiB blocks read-only (guard, crates/victory-gpu/src/watch.rs). A system call can't write into a read-only block: ReadFile fails with ERROR_NOACCESS instead of faulting into our handler. So the runtime announces its kernel reads first with victory_gpu::kernel_will_write: ReadFile and fread in files.rs, raw DirectStorage reads in dstorage.rs.

kernel_will_write only opened the blocks. The next guard() covering them, from any thread, made them read-only again, and the submitting thread calls it at every refresh. If that came between the announcement and the kernel's write, the read failed. DirectStorage then returned the error to the title, and fread came back short. The window lasts as long as the read.

What the fix does

  • watch.rs keeps one block range per announced read still to land (Guards::announced).
    • kernel_will_write opens the blocks and records the range.
    • The new kernel_wrote removes it.
    • guard() still reports a block in a recorded range as written, but leaves it open.
    • Ranges, not one entry per block: fread announces the title's size times count, saturated, so a length can span the address space. The cost doesn't depend on the length, and few reads are in flight at a time.
  • The runtime calls kernel_wrote once each read returns: after ReadFile and fread in files.rs, and after the raw DirectStorage fill in dstorage.rs.
  • shortcut: an overlapped ReadFile that returns ERROR_IO_PENDING keeps its range for good, since its end isn't seen. They're then copied at every refresh, which is correct, just slower. Grand Theft Auto VI's reads go through the synchronous path.
  • watch::writing, which our own copy-back and texture writes use from the submitting thread, is unchanged and now pub(crate).

How it behaves after the fix

A kernel read into guarded view memory succeeds however many refreshes run on other threads before it lands. Once it's done, the next refresh guards the block again and copies what the read wrote.

Tests

an_announced_kernel_read_survives_a_refresh_before_it (watch.rs) replays the two threads' order on one thread, with a real pagefile view and a real file read:

  1. Guard the view.
  2. Announce the read with kernel_will_write.
  3. Refresh with guard(), as another thread's refresh does for view memory.
  4. Read 13 bytes from a temp file into the view: it must succeed.
  5. Call kernel_wrote: the next guard() must report the block and guard it again, so a further unannounced read fails.

an_announcement_as_long_as_the_address_space_is_cheap (watch.rs) announces and lands a read of saturated length, above user space so that no other test's blocks are in it.

I ran cargo test -p victory-gpu -p victory-hle -- --test-threads=1 on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes (victory-gpu: 52 passed, 1 ignored; victory-hle: 94 passed), also with the default parallel test threads.
  • The same steps on main (ffe310e, announcing with today's kernel_will_write): the read fails: the refresh guarded the block again under the read.
  • The saturated-length test on this PR's first commit (8d7e3a0, one map entry per block): it hadn't finished when the step timed out after 5 minutes. TG0000 found this by reading.
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings is clean, with and without --all-features.

Not tested: a title run, and overlapped reads (none known in the title).

Related: #393, #413, #464 and #490 change guard or title_memory_remapped without keeping an announced block open. #991 changes watch::open on nearby lines, so whichever merges second needs a rebase.

**@⁠Dufftwt** on GitHub · 2026-11-24 15:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025) `Dufftwt:fix/announced-kernel-reads-stay-open` → `main` · +116 −11 · 4 files · 2 commits · closed without merging Fixes #723. ## What's wrong View memory (xmem's physical-page views) is tracked by making 64 KiB blocks read-only (`guard`, `crates/victory-gpu/src/watch.rs`). A system call can't write into a read-only block: `ReadFile` fails with `ERROR_NOACCESS` instead of faulting into our handler. So the runtime announces its kernel reads first with `victory_gpu::kernel_will_write`: `ReadFile` and `fread` in `files.rs`, raw DirectStorage reads in `dstorage.rs`. `kernel_will_write` only opened the blocks. The next `guard()` covering them, from any thread, made them read-only again, and the submitting thread calls it at every refresh. If that came between the announcement and the kernel's write, the read failed. DirectStorage then returned the error to the title, and `fread` came back short. The window lasts as long as the read. ## What the fix does - `watch.rs` keeps one block range per announced read still to land (`Guards::announced`). - `kernel_will_write` opens the blocks and records the range. - The new `kernel_wrote` removes it. - `guard()` still reports a block in a recorded range as written, but leaves it open. - Ranges, not one entry per block: `fread` announces the title's size times count, saturated, so a length can span the address space. The cost doesn't depend on the length, and few reads are in flight at a time. - The runtime calls `kernel_wrote` once each read returns: after `ReadFile` and `fread` in `files.rs`, and after the raw DirectStorage fill in `dstorage.rs`. - `shortcut:` an overlapped `ReadFile` that returns `ERROR_IO_PENDING` keeps its range for good, since its end isn't seen. They're then copied at every refresh, which is correct, just slower. Grand Theft Auto VI's reads go through the synchronous path. - `watch::writing`, which our own copy-back and texture writes use from the submitting thread, is unchanged and now `pub(crate)`. ## How it behaves after the fix A kernel read into guarded view memory succeeds however many refreshes run on other threads before it lands. Once it's done, the next refresh guards the block again and copies what the read wrote. ## Tests `an_announced_kernel_read_survives_a_refresh_before_it` (`watch.rs`) replays the two threads' order on one thread, with a real pagefile view and a real file read: 1. Guard the view. 2. Announce the read with `kernel_will_write`. 3. Refresh with `guard()`, as another thread's refresh does for view memory. 4. Read 13 bytes from a temp file into the view: it must succeed. 5. Call `kernel_wrote`: the next `guard()` must report the block and guard it again, so a further unannounced read fails. `an_announcement_as_long_as_the_address_space_is_cheap` (`watch.rs`) announces and lands a read of saturated length, above user space so that no other test's blocks are in it. I ran `cargo test -p victory-gpu -p victory-hle -- --test-threads=1` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes (`victory-gpu`: 52 passed, 1 ignored; `victory-hle`: 94 passed), also with the default parallel test threads. - **The same steps on `main` (`ffe310e`, announcing with today's `kernel_will_write`):** the read fails: `the refresh guarded the block again under the read`. - **The saturated-length test on this PR's first commit (`8d7e3a0`, one map entry per block):** it hadn't finished when the step timed out after 5 minutes. TG0000 found this by reading. - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings` is clean, with and without `--all-features`. **Not tested:** a title run, and overlapped reads (none known in the title). Related: #393, #413, #464 and #490 change `guard` or `title_memory_remapped` without keeping an announced block open. #991 changes `watch::open` on nearby lines, so whichever merges second needs a rebase.
kernel_will_write only opened the guarded blocks of view memory a
ReadFile, fread or DirectStorage read was about to write. The next refresh
on any other thread guarded them again, and if that came before the kernel
wrote, the read failed with ERROR_NOACCESS.

Announced blocks are now counted until the runtime reports the read done
(kernel_wrote); guard() reports them written but leaves them open while
the count is not zero. An overlapped ReadFile still pending keeps its
blocks open.

Fixes #723.
kernel_will_write and kernel_wrote touched a map entry per 64 KiB block
of the announced range, under the guards lock. fread announces the
title's size times count, saturated, so a saturated length meant about
2^48 iterations and a map growing with them. One block range is now
kept per announcement, and guard() checks a block against the ranges
in flight (few at a time).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:17:32 +00:00
github-bot closed this pull request 2026-11-26 04:17:33 +00:00
Author
Owner

@⁠ThomasHFWright on GitHub · 2026-11-24 15:15 UTC · original

Independent Wine validation of 8d7e3a0827198055555f372a4fc58022585d882e on base ffe310e, using Rust 1.98.1 and the Windows MSVC target. Linux host, Wine 11.22, RTX 5090 / NVIDIA 620.73.04, vkd3d-proton 30b0eb9bc12dc9f.

The new watch::tests::an_announced_kernel_read_survives_a_refresh_before_it passes under Wine, including the final check that an unannounced read fails after completion and re-guarding. Negative control: I retained the new test/API but changed kernel_will_write back to the original open-only writing(at, len) and made kernel_wrote a no-op. It failed at the refresh guarded the block again under the read. The control edits were then removed.

Full workspace unit/integration suite, all features, all 14 generated Windows test executables, offline and offscreen:

Tree Passed Failed Ignored
ffe310e baseline 341 5 6
This PR 342 5 6

GPU: 53 passed / 1 failed / 1 ignored; HLE: 93 passed / 1 failed. The failures are unchanged from baseline: empty pipeline library (#347), missing Wine Winsock export (#217), the shader-checksum rejection expectation, the nested Cargo fixture build, and Windows xtask's attempt to launch host Git. Native Linux xtask separately passes all 4 tests. The runtime/DLL configuration was identical in both full runs; native d3dcompiler_47 was supplied for the GPU shader fixtures.

Windows-target workspace Clippy with all targets/features and -D warnings passes. The changes were tested in an isolated checkout; no game data was used.

Not tested: gameplay, pending/overlapped I/O completion, native Windows/WARP, or Wine doctests. Aggregate counts include existing tests that return early when optional console compilers or the DXIL validator are missing; the new memory-guard regression has no such skip.

AI-assisted testing with Codex.

**@⁠ThomasHFWright** on GitHub · 2026-11-24 15:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7135734725) Independent Wine validation of `8d7e3a0827198055555f372a4fc58022585d882e` on base `ffe310e`, using Rust 1.98.1 and the Windows MSVC target. Linux host, Wine 11.22, RTX 5090 / NVIDIA 620.73.04, vkd3d-proton `30b0eb9bc12dc9f`. The new `watch::tests::an_announced_kernel_read_survives_a_refresh_before_it` **passes under Wine**, including the final check that an unannounced read fails after completion and re-guarding. Negative control: I retained the new test/API but changed `kernel_will_write` back to the original open-only `writing(at, len)` and made `kernel_wrote` a no-op. It failed at `the refresh guarded the block again under the read`. The control edits were then removed. Full workspace unit/integration suite, all features, all 14 generated Windows test executables, offline and offscreen: | Tree | Passed | Failed | Ignored | | --- | ---: | ---: | ---: | | `ffe310e` baseline | 341 | 5 | 6 | | This PR | 342 | 5 | 6 | GPU: **53 passed / 1 failed / 1 ignored**; HLE: **93 passed / 1 failed**. The failures are unchanged from baseline: empty pipeline library (#347), missing Wine Winsock export (#217), the shader-checksum rejection expectation, the nested Cargo fixture build, and Windows xtask's attempt to launch host Git. Native Linux xtask separately passes all 4 tests. The runtime/DLL configuration was identical in both full runs; native `d3dcompiler_47` was supplied for the GPU shader fixtures. Windows-target workspace Clippy with all targets/features and `-D warnings` passes. The changes were tested in an isolated checkout; no game data was used. Not tested: gameplay, pending/overlapped I/O completion, native Windows/WARP, or Wine doctests. Aggregate counts include existing tests that return early when optional console compilers or the DXIL validator are missing; the new memory-guard regression has no such skip. AI-assisted testing with Codex.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:40 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 8d7e3a0: cargo test -p victory-gpu -p victory-hle passes, 146 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7136413851) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 8d7e3a0: `cargo test -p victory-gpu -p victory-hle` passes, 146 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠TG0000 on GitHub · 2026-11-24 16:26 UTC · original

Thanks for this, holding the blocks open until the read lands makes sense. One defect at 8d7e3a0, found by reading, not run:

kernel_will_write and kernel_wrote (watch.rs) loop over every 64 KiB block of [at, at + len) and touch a BTreeMap entry for each, with the guards lock held. fread passes size.saturating_mul(count), and the comment in writing notes that this length is the title's and may be saturated. With a saturated length that is about 2^48 iterations: a hang, with memory growing. On main writing stays cheap there because open is a range query.

Suggestions: keep the announced reads as ranges (a list of (start, end), which guard checks), or clamp the loop to blocks that can be guarded (the mapped range, or the span of protected). The existing test that calls writing(top, PAGE) near the top of the address space would be a good place for kernel_will_write(at, usize::MAX).

A smaller point, already marked as a shortcut: an overlapped ReadFile that stays pending leaves its blocks announced for good, so they are copied at every refresh. If such reads are frequent, that cost grows over a session. I did not measure it.

AI-assisted (Claude Code).

**@⁠TG0000** on GitHub · 2026-11-24 16:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7137678088) Thanks for this, holding the blocks open until the read lands makes sense. One defect at 8d7e3a0, found by reading, not run: `kernel_will_write` and `kernel_wrote` (watch.rs) loop over **every 64 KiB block** of `[at, at + len)` and touch a `BTreeMap` entry for each, with the guards lock held. `fread` passes `size.saturating_mul(count)`, and the comment in `writing` notes that this length is the title's and may be saturated. With a saturated length that is about 2^48 iterations: a hang, with memory growing. On main `writing` stays cheap there because `open` is a range query. Suggestions: keep the announced reads as ranges (a list of `(start, end)`, which `guard` checks), or clamp the loop to blocks that can be guarded (the mapped range, or the span of `protected`). The existing test that calls `writing(top, PAGE)` near the top of the address space would be a good place for `kernel_will_write(at, usize::MAX)`. A smaller point, already marked as a shortcut: an overlapped `ReadFile` that stays pending leaves its blocks announced for good, so they are copied at every refresh. If such reads are frequent, that cost grows over a session. I did not measure it. _AI-assisted (Claude Code)._
Author
Owner

@⁠Dufftwt on GitHub · 2026-11-24 16:37 UTC · original

@⁠TG0000 good catch, thanks: confirmed and fixed in ece73bb.

  • Guards::announced now holds one block range per announced read ([first, end)). kernel_will_write pushes one, kernel_wrote removes it, and guard() checks a block against the ranges in flight. The cost no longer depends on the length.
  • New test, an_announcement_as_long_as_the_address_space_is_cheap: it announces and lands a read of saturated length. I put it above user space rather than in the test near the top: an announcement that covered user space would keep the other tests' blocks open while they run in parallel.
  • On 8d7e3a0 that test hadn't finished when the CI step timed out after 5 minutes. With the fix it passes instantly, and so does everything else (victory-gpu 52 passed, 1 ignored; victory-hle 94), with the default parallel test threads. Windows CI on windows-latest.

On the pending overlapped ReadFile: agreed. It now leaves one range per read instead of one entry per block, but the range stays, and its blocks are copied at every refresh. No overlapped reads are known in the title yet. If some show up, the clean answer is to remove the range where their completion is seen, not to keep it forever.

**@⁠Dufftwt** on GitHub · 2026-11-24 16:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7137944153) @⁠TG0000 good catch, thanks: confirmed and fixed in `ece73bb`. - `Guards::announced` now holds one block range per announced read (`[first, end)`). `kernel_will_write` pushes one, `kernel_wrote` removes it, and `guard()` checks a block against the ranges in flight. The cost no longer depends on the length. - New test, `an_announcement_as_long_as_the_address_space_is_cheap`: it announces and lands a read of saturated length. I put it above user space rather than in the test near the top: an announcement that covered user space would keep the other tests' blocks open while they run in parallel. - On `8d7e3a0` that test hadn't finished when the CI step timed out after 5 minutes. With the fix it passes instantly, and so does everything else (`victory-gpu` 52 passed, 1 ignored; `victory-hle` 94), with the default parallel test threads. Windows CI on `windows-latest`. On the pending overlapped `ReadFile`: agreed. It now leaves one range per read instead of one entry per block, but the range stays, and its blocks are copied at every refresh. No overlapped reads are known in the title yet. If some show up, the clean answer is to remove the range where their completion is seen, not to keep it forever.
Author
Owner

@⁠465d6a0eac on GitHub · 2026-11-24 17:49 UTC · original

Thank you for this, @⁠Dufftwt! Many pull requests here are still waiting to be merged, so I've ported this one to 465d6a0eac/AgentRecompVI, a VIctory fork where AI agents review and merge each other's work: https://github.com/465d6a0eac/AgentRecompVI/pull/36. You're credited as the author there. This stays the original.

Test results

  • Windows 11, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088, 32-lane waves): together with the rest of this batch, cargo clippy (both variants) and cargo test --workspace pass (429 passed, 0 failed).
  • In the world (the first mission's scene), the whole batch took the title from 0.1–0.2 fps (3,300–6,400 ms of GPU time per frame) to 0.8–1.2 fps (190–310 ms). The screen is still black there, and the GPU still hangs after about 3 minutes (one command list over 2 s), so this is not playable yet.
**@⁠465d6a0eac** on GitHub · 2026-11-24 17:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7139763346) Thank you for this, @⁠Dufftwt! Many pull requests here are still waiting to be merged, so I've ported this one to [465d6a0eac/AgentRecompVI](https://github.com/465d6a0eac/AgentRecompVI), a VIctory fork where AI agents review and merge each other's work: https://github.com/465d6a0eac/AgentRecompVI/pull/36. You're credited as the author there. This stays the original. **Test results** - Windows 11, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088, 32-lane waves): together with the rest of this batch, `cargo clippy` (both variants) and `cargo test --workspace` pass (429 passed, 0 failed). - In the world (the first mission's scene), the whole batch took the title from 0.1–0.2 fps (3,300–6,400 ms of GPU time per frame) to 0.8–1.2 fps (190–310 ms). The screen is still black there, and the GPU still hangs after about 3 minutes (one command list over 2 s), so this is not playable yet.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:30 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Already in main: an announced kernel write returns a guard that keeps its blocks open until dropped (watch::Writing, used by files.rs and dstorage.rs).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1025#issuecomment-7148580806) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Already in main: an announced kernel write returns a guard that keeps its blocks open until dropped (watch::Writing, used by files.rs and dstorage.rs). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1025
No description provided.