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

Open
reeledMarek wants to merge 1 commit from upstream-pr/2553 into main
reeledMarek commented 2026-11-25 15:40:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:40:13Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6608188583 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:40:13Z. --- ## 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.
Marvisak commented 2026-11-25 19:06:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:37:07Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171157548 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7171157548). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:37:07Z. --- <!-- 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)_
welisonzika14-jpg commented 2026-11-25 20:06:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by welisonzika14-jpg on 2026-11-25T19:18:18Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7172112115 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7172112115). Originally posted by [welisonzika14-jpg](https://github.com/welisonzika14-jpg) on 2026-11-25T19:18:18Z. --- 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.
reeledMarek commented 2026-11-25 20:06:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T20:05:30Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7173178213 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7173178213). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T20:05:30Z. --- 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.
upstream-sync changed title from [PR #2553] HLE: avoid data race in DirectStorage late page-list polling and guard read_into offset overflow to [PR #2553] HLE: snapshot DirectStorage physical page lists with relaxed atomic loads 2026-11-25 21:02:36 +00:00
brainbudt commented 2026-11-25 22:04:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by brainbudt on 2026-11-25T21:08:35Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7174668483 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7174668483). Originally posted by [brainbudt](https://github.com/brainbudt) on 2026-11-25T21:08:35Z. --- > [!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.
upstream-sync changed title from [PR #2553] HLE: snapshot DirectStorage physical page lists with relaxed atomic loads to [PR #2553] HLE: avoid data race in DirectStorage late page-list polling 2026-11-26 06:02:30 +00:00
reeledMarek commented 2026-11-26 06:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T05:56:04Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7183937276 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#issuecomment-7183937276). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T05:56:04Z. --- 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`).
fuzeofficlal commented 2026-11-26 16:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T15:26:28Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6076735122 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2553#pullrequestreview-6076735122). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T15:26:28Z. **Review:** APPROVED --- 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 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/2553:upstream-pr/2553'
git switch 'upstream-pr/2553'

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/2553'
git switch 'upstream-pr/2553'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2553'
git switch 'upstream-pr/2553'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2553'
git switch 'main'
git merge --squash 'upstream-pr/2553'
git switch 'main'
git merge --ff-only 'upstream-pr/2553'
git switch 'main'
git merge 'upstream-pr/2553'
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
Marvisak/VIctory!2551
No description provided.