HLE: avoid data race in DirectStorage late page-list polling #2553

Open
github-bot wants to merge 1 commit from github-bot/gh-2553 into main AGit
Owner

@⁠reeledMarek on GitHub · 2026-11-25 15:40 UTC · original
reeledMarek:fix/hle-dstorage-page-list-race-and-offset → main · +26 −17 · 1 files · 1 commits · open

Summary

  • In crates/victory-hle/src/modules/dstorage.rs, into_destination previously constructed shared &[usize] slices (std::slice::from_raw_parts) directly over the title's physical page list while the title can still be populating entries concurrently (the reason LATE_LIST_TRIES exists), which is UB under Rust's aliasing and data-race rules.
  • Snapshot the page list into a local Vec<usize> via AtomicUsize::from_ptr(...).load(Ordering::Acquire) (once up front and on each late-list retry) and pass &pages to page_window, write_physical, and unknown_pages.
  • Updated a_page_list_filled_after_its_request_starts to populate the slot via AtomicUsize::from_ptr(...).store(..., Ordering::Release) so the test itself has no mixed atomic/non-atomic race.
**@⁠reeledMarek** on GitHub · 2026-11-25 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553) `reeledMarek:fix/hle-dstorage-page-list-race-and-offset` → `main` · +26 −17 · 1 files · 1 commits · open ## Summary - In `crates/victory-hle/src/modules/dstorage.rs`, `into_destination` previously constructed shared `&[usize]` slices (`std::slice::from_raw_parts`) directly over the title's physical page list while the title can still be populating entries concurrently (the reason `LATE_LIST_TRIES` exists), which is UB under Rust's aliasing and data-race rules. - Snapshot the page list into a local `Vec<usize>` via `AtomicUsize::from_ptr(...).load(Ordering::Acquire)` (once up front and on each late-list retry) and pass `&pages` to `page_window`, `write_physical`, and `unknown_pages`. - Updated `a_page_list_filled_after_its_request_starts` to populate the slot via `AtomicUsize::from_ptr(...).store(..., Ordering::Release)` so the test itself has no mixed atomic/non-atomic race.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:09:05 +00:00
github-bot changed title from HLE: snapshot DirectStorage physical page lists with relaxed atomic loads to HLE: avoid data race in DirectStorage late page-list polling 2026-11-26 06:31:15 +00:00
github-bot force-pushed github-bot/gh-2553 from 2051b22bdb to de55348e81 2026-11-26 06:31:15 +00:00 Compare
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:37 UTC · original · edited 2026-11-26 07:18 UTC

Automated triage for the maintainers.

Merge candidate. Re-reviewed at de55348. In crates/victory-hle/src/modules/dstorage.rs, into_destination still copies the title's page list into a local Vec before page_window, write_physical and unknown_pages read it, once up front and on each LATE_LIST_TRIES retry. The copy now uses AtomicUsize::from_ptr(..).load(Ordering::Acquire), and request.destination is *mut c_void, so the pointer type fits. The test a_page_list_filled_after_its_request_starts now writes the slot with a matching Release store, so the test has no mixed atomic/plain access either. The PR description matches the patch.

Reply to the author: thanks, both of @⁠brainbudt's points are addressed.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 18:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7171157548) · edited 2026-11-26 07:18 UTC <!-- victory-triage v1 head=de55348e81d3589b1d57b0b62eca8424b06aaaf2 verdict=merge-candidate at=2026-11-26T07:18:10Z --> Automated triage for the maintainers. **Merge candidate.** Re-reviewed at `de55348`. In `crates/victory-hle/src/modules/dstorage.rs`, `into_destination` still copies the title's page list into a local `Vec` before `page_window`, `write_physical` and `unknown_pages` read it, once up front and on each `LATE_LIST_TRIES` retry. The copy now uses `AtomicUsize::from_ptr(..).load(Ordering::Acquire)`, and `request.destination` is `*mut c_void`, so the pointer type fits. The test `a_page_list_filled_after_its_request_starts` now writes the slot with a matching `Release` store, so the test has no mixed atomic/plain access either. The PR description matches the patch. **Reply to the author:** thanks, both of @⁠brainbudt's points are addressed. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠welisonzika14-jpg on GitHub · 2026-11-25 19:18 UTC · original

For the first point of the triage above (the data race): a small patch, in case it helps. Tested on head f62909f, Windows 11, rustc 1.98.1.

AtomicUsize::from_ptr is stable (1.75), and the compiler here accepts it. The snapshot in into_destination now does AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed) instead of read_volatile.

Alignment: from_ptr needs the pointer aligned for usize, but read_volatile on a *const usize already needs the same, so this adds no new requirement. I cannot check from here that the title always aligns the list; the comment now says so.

  • cargo test -p victory-hle --lib --locked dstorage: 17 passed, 0 failed.
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean.

I did not touch the overflow guard (second point). I have no console result for it.

Diff
diff --git a/crates/victory-hle/src/modules/dstorage.rs b/crates/victory-hle/src/modules/dstorage.rs
index f931044..6533a64 100644
--- a/crates/victory-hle/src/modules/dstorage.rs
+++ b/crates/victory-hle/src/modules/dstorage.rs
@@ -1033,10 +1033,14 @@ fn into_destination(
     let snapshot = |dst: &mut [usize]| {
         let src = request.destination.cast::<usize>();
         for (i, slot) in dst.iter_mut().enumerate() {
-            // SAFETY: the title passes a page number per 64 KiB of destination;
-            // reading volatile into a local buffer avoids creating a shared
-            // slice over memory the title may still be populating concurrently.
-            *slot = unsafe { src.add(i).read_volatile() };
+            // SAFETY: the title passes a page number per 64 KiB of destination
+            // (aligned for `usize`, as the read needs). A relaxed atomic load
+            // into a local buffer races with the title's writes without a data
+            // race, and creates no shared slice over memory it may still be
+            // populating.
+            *slot = unsafe {
+                std::sync::atomic::AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed)
+            };
         }
     };
     snapshot(&mut pages);

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

**@⁠welisonzika14-jpg** on GitHub · 2026-11-25 19:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7172112115) For the first point of the triage above (the data race): a small patch, in case it helps. Tested on head `f62909f`, Windows 11, rustc 1.98.1. `AtomicUsize::from_ptr` is stable (1.75), and the compiler here accepts it. The snapshot in `into_destination` now does `AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed)` instead of `read_volatile`. Alignment: `from_ptr` needs the pointer aligned for `usize`, but `read_volatile` on a `*const usize` already needs the same, so this adds no new requirement. I cannot check from here that the title always aligns the list; the comment now says so. - `cargo test -p victory-hle --lib --locked dstorage`: 17 passed, 0 failed. - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean. I did not touch the overflow guard (second point). I have no console result for it. <details><summary>Diff</summary> ```diff diff --git a/crates/victory-hle/src/modules/dstorage.rs b/crates/victory-hle/src/modules/dstorage.rs index f931044..6533a64 100644 --- a/crates/victory-hle/src/modules/dstorage.rs +++ b/crates/victory-hle/src/modules/dstorage.rs @@ -1033,10 +1033,14 @@ fn into_destination( let snapshot = |dst: &mut [usize]| { let src = request.destination.cast::<usize>(); for (i, slot) in dst.iter_mut().enumerate() { - // SAFETY: the title passes a page number per 64 KiB of destination; - // reading volatile into a local buffer avoids creating a shared - // slice over memory the title may still be populating concurrently. - *slot = unsafe { src.add(i).read_volatile() }; + // SAFETY: the title passes a page number per 64 KiB of destination + // (aligned for `usize`, as the read needs). A relaxed atomic load + // into a local buffer races with the title's writes without a data + // race, and creates no shared slice over memory it may still be + // populating. + *slot = unsafe { + std::sync::atomic::AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed) + }; } }; snapshot(&mut pages); ``` </details> Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
Author
Owner

@⁠reeledMarek on GitHub · 2026-11-25 20:05 UTC · original

Updated to address the triage feedback (thanks also to @⁠welisonzika14-jpg for testing the atomic load patch):

  • Replaced read_volatile with AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed) in into_destination's page-list snapshot so concurrent population by the title does not create a Rust data race or shared slice over mutating memory.
  • Dropped the speculative read_into / into_destination offset-overflow guards and test.
**@⁠reeledMarek** on GitHub · 2026-11-25 20:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7173178213) Updated to address the triage feedback (thanks also to @⁠welisonzika14-jpg for testing the atomic load patch): - Replaced `read_volatile` with `AtomicUsize::from_ptr(src.add(i)).load(Ordering::Relaxed)` in `into_destination`'s page-list snapshot so concurrent population by the title does not create a Rust data race or shared slice over mutating memory. - Dropped the speculative `read_into` / `into_destination` offset-overflow guards and test.
Author
Owner

@⁠brainbudt on GitHub · 2026-11-25 21:08 UTC · original

Note

🤖 Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt

Independently verified 2051b22 merged onto 13f91ca (and combined with #2566; Windows 11 DirectStorage/xmem/watch tests and MSVC workspace cargo check --tests passed).

Two items before merge:

  1. Mixed atomic / non-atomic race in a_page_list_filled_after_its_request_starts (crates/victory-hle/src/modules/dstorage.rs:1023-1035, 1637-1648): The worker loads page-list slots atomically while the filler thread writes the same word with write_volatile without a happens-before edge, which Rust classifies as a data race (strengthening only the load to Acquire does not resolve it). Please use an atomic store in the test and clarify the synchronization contract for post-submit producers.
  2. Stale PR description: The PR body still describes volatile reads and the removed overflow change.
**@⁠brainbudt** on GitHub · 2026-11-25 21:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7174668483) > [!NOTE] > 🤖 **Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt** Independently verified `2051b22` merged onto `13f91ca` (and combined with #2566; Windows 11 DirectStorage/xmem/watch tests and MSVC workspace `cargo check --tests` passed). Two items before merge: 1. **Mixed atomic / non-atomic race in `a_page_list_filled_after_its_request_starts` (`crates/victory-hle/src/modules/dstorage.rs:1023-1035`, `1637-1648`):** The worker loads page-list slots atomically while the filler thread writes the same word with `write_volatile` without a happens-before edge, which Rust classifies as a data race (strengthening only the load to `Acquire` does not resolve it). Please use an atomic store in the test and clarify the synchronization contract for post-submit producers. 2. **Stale PR description:** The PR body still describes volatile reads and the removed overflow change.
Author
Owner

@⁠reeledMarek on GitHub · 2026-11-26 05:56 UTC · original

Thanks @⁠brainbudt — updated both items:

  • Switched a_page_list_filled_after_its_request_starts to AtomicUsize::from_ptr(...).store(..., Ordering::Release) paired with Ordering::Acquire in into_destination's snapshot, and documented the producer synchronization contract.
  • Updated the PR description to match the final patch and rebased cleanly onto main (63bcca4).
**@⁠reeledMarek** on GitHub · 2026-11-26 05:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7183937276) Thanks @⁠brainbudt — updated both items: - Switched `a_page_list_filled_after_its_request_starts` to `AtomicUsize::from_ptr(...).store(..., Ordering::Release)` paired with `Ordering::Acquire` in `into_destination`'s snapshot, and documented the producer synchronization contract. - Updated the PR description to match the final patch and rebased cleanly onto `main` (`63bcca4`).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 15:26 UTC · original · approved these changes

Replacing the shared &[usize] slice over a live page list with a local Vec<usize> snapshot via AtomicUsize::from_ptr(...).load(Ordering::Acquire) (crates/victory-hle/src/modules/dstorage.rs:1020-1075) and pairing the test writer with Ordering::Release (L1633-1645) cleanly eliminates the Rust data-race UB during late page-list polling.

Non-blocking follow-up notes:

  • Checking pointer alignment (src.is_aligned()) before AtomicUsize::from_ptr and skipping the page-entry read when len == 0 would harden edge inputs further.
  • Note that open #2738 separately replaces the 2 ms sleep in a_page_list_filled_after_its_request_starts with deterministic synchronization.

Local verification at de55348e (and cleanly merged onto origin/main at d44c6c0e): xtask policy (191 files ok), cargo clippy -p victory-hle --all-targets (default and --all-features, 0 warnings), dstorage (16/16), and cargo test -p victory-hle (debug, --release, and merged onto origin/main: 152/152 + integration tests) all passed. Approving.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2553#pullrequestreview-6076735122) · **approved these changes** Replacing the shared `&[usize]` slice over a live page list with a local `Vec<usize>` snapshot via `AtomicUsize::from_ptr(...).load(Ordering::Acquire)` (`crates/victory-hle/src/modules/dstorage.rs:1020-1075`) and pairing the test writer with `Ordering::Release` (`L1633-1645`) cleanly eliminates the Rust data-race UB during late page-list polling. Non-blocking follow-up notes: - Checking pointer alignment (`src.is_aligned()`) before `AtomicUsize::from_ptr` and skipping the page-entry read when `len == 0` would harden edge inputs further. - Note that open `#2738` separately replaces the 2 ms sleep in `a_page_list_filled_after_its_request_starts` with deterministic synchronization. Local verification at `de55348e` (and cleanly merged onto `origin/main` at `d44c6c0e`): `xtask policy` (`191 files ok`), `cargo clippy -p victory-hle --all-targets` (`default` and `--all-features`, 0 warnings), `dstorage` (`16/16`), and `cargo test -p victory-hle` (debug, `--release`, and merged onto `origin/main`: `152/152` + integration tests) all passed. Approving.
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/2553/head:github-bot/gh-2553'
git switch 'github-bot/gh-2553'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2553'
git switch 'github-bot/gh-2553'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2553'
git switch 'github-bot/gh-2553'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2553'
git switch 'main'
git merge --squash 'github-bot/gh-2553'
git switch 'main'
git merge --ff-only 'github-bot/gh-2553'
git switch 'main'
git merge 'github-bot/gh-2553'
git push origin 'main'
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!2553
No description provided.