startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2677

Merged
lukaslw-lab merged 2 commits from upstream-pr/2685 into main 2026-11-26 03:19:41 +00:00
lukaslw-lab commented 2026-11-26 02:00:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-26T02:00:48Z.


Change

Fixes #2684 (DirectStorage destination check costs ~10 ms of kernel time per request).

destination_writable (#1672) keeps its verdicts but no longer calls VirtualQuery. It probes one byte per 4 KiB page of the declared capacity with an atomic OR of zero under a structured exception handler.

  • local_log.h: x2w_local::probe_writable, the write twin of probe_readable (same bounds, same guard re-arm). An OR of zero faults where a write would and changes no byte, also against a concurrent writer.
  • xcrdapi.cpp: destination_writable calls commit_lazy as before, then probe_writable.
  • docs/startup-runtime.md: the destination section describes the probe and its cost.
  • Precedent: probe_readable already avoids VirtualQuery for the same reason. Its comment says that on the title's multi-gigabyte regions one query takes about a millisecond because it walks the region. #1766 (slot 133 placements) and #2611 (unplaced memory SRVs) avoid the same scan in the D3D12 stand-in.
  • Not used: QueryWorkingSetEx reports protection only for resident pages, so it would still need the VirtualQuery fallback; region information has no current protection.
  • Overlap: draft #2603 adds a probe cache next to probe_readable in local_log.h (text conflict only, if it lands first). #2245 answers readability of unmapped memory from xmem; this probe faults only on a refusal.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti; dev and release; commit 975f4872 (on b2564017).

ctest --preset dev     --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'
ctest --preset release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'

70/70 passed in each, with no capability skips.

  • startup_dstorage_destination: the existing read-only, guard, no-access and short-commit refusals and the writable and copy-on-write successes all pass unchanged. New: the guard page is still PAGE_GUARD after its refusal; a writable destination with a three-page capacity and one page of data succeeds with the two probed, unwritten pages unchanged.
  • Negative controls (local mutations, not in this PR): without the guard re-arm the test fails guard page lost its guard bit; with an OR of 1 instead of 0 it fails the new case (a byte past the data changed) and the four refusal cases (first page was written).
  • Startup A/B (release, warm cache, same switches; only XCRDAPI.dll differs; stand-in CPU time from !runaway every 20 s):
VirtualQuery check Probe
Requests 3,254 → ~9,900: stand-in kernel time ~73 s ~1.2 s
Kernel time per request 10–12 ms 0.1–0.2 ms
First load complete (16,618 blocks inflated) 110 s 70 s
25k / 50k blocks inflated 170 / 180 s 130 / 150 s
destination not writable refusals, device removal 0, none 0, none

The confirm presses landed at different times in the two runs, so the end-to-end times are indicative; the per-request kernel time does not depend on them.

Evidence and follow-up

  • Fixtures are source-built (VirtualAlloc and a pagefile-backed mapping in the test); no title data.
  • Cost: every page up to the declared capacity is touched, so a page past the written bytes would be faulted in, and a copy-on-write page gets its private copy just before the copy instead of during it. Commit does not change: commit_lazy already covered the whole capacity. A local counter (not in this PR) measured this in one startup run (220 s, first load and the streaming that follows): 81,873 destination checks, 1,595,238 probed pages, 0 pages past the last written page. Every declared capacity ended in the same page as the data.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (docs/startup-runtime.md)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: the change, tests and this description were written with Claude; I ran the startup A/B.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6617834061 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2685). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-26T02:00:48Z. --- ## Change Fixes #2684 (DirectStorage destination check costs ~10 ms of kernel time per request). `destination_writable` (#1672) keeps its verdicts but no longer calls `VirtualQuery`. It probes one byte per 4 KiB page of the declared capacity with an atomic OR of zero under a structured exception handler. - `local_log.h`: `x2w_local::probe_writable`, the write twin of `probe_readable` (same bounds, same guard re-arm). An OR of zero faults where a write would and changes no byte, also against a concurrent writer. - `xcrdapi.cpp`: `destination_writable` calls `commit_lazy` as before, then `probe_writable`. - `docs/startup-runtime.md`: the destination section describes the probe and its cost. - Precedent: `probe_readable` already avoids `VirtualQuery` for the same reason. Its comment says that on the title's multi-gigabyte regions one query takes about a millisecond because it walks the region. #1766 (slot 133 placements) and #2611 (unplaced memory SRVs) avoid the same scan in the D3D12 stand-in. - Not used: `QueryWorkingSetEx` reports protection only for resident pages, so it would still need the `VirtualQuery` fallback; region information has no current protection. - Overlap: draft #2603 adds a probe cache next to `probe_readable` in `local_log.h` (text conflict only, if it lands first). #2245 answers readability of unmapped memory from xmem; this probe faults only on a refusal. ## Verification Windows 10 22H2, i7-4790K, RTX 3080 Ti; dev and release; commit `975f4872` (on `b2564017`). ``` ctest --preset dev --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save' ctest --preset release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save' ``` 70/70 passed in each, with no capability skips. - `startup_dstorage_destination`: the existing read-only, guard, no-access and short-commit refusals and the writable and copy-on-write successes all pass unchanged. New: the guard page is still `PAGE_GUARD` after its refusal; a writable destination with a three-page capacity and one page of data succeeds with the two probed, unwritten pages unchanged. - Negative controls (local mutations, not in this PR): without the guard re-arm the test fails `guard page lost its guard bit`; with an OR of 1 instead of 0 it fails the new case (`a byte past the data changed`) and the four refusal cases (`first page was written`). - Startup A/B (release, warm cache, same switches; only `XCRDAPI.dll` differs; stand-in CPU time from `!runaway` every 20 s): | | `VirtualQuery` check | Probe | |---|---|---| | Requests 3,254 → ~9,900: stand-in kernel time | ~73 s | ~1.2 s | | Kernel time per request | 10–12 ms | 0.1–0.2 ms | | First load complete (16,618 blocks inflated) | 110 s | 70 s | | 25k / 50k blocks inflated | 170 / 180 s | 130 / 150 s | | `destination not writable` refusals, device removal | 0, none | 0, none | The confirm presses landed at different times in the two runs, so the end-to-end times are indicative; the per-request kernel time does not depend on them. ## Evidence and follow-up - Fixtures are source-built (`VirtualAlloc` and a pagefile-backed mapping in the test); no title data. - Cost: every page up to the declared capacity is touched, so a page past the written bytes would be faulted in, and a copy-on-write page gets its private copy just before the copy instead of during it. Commit does not change: `commit_lazy` already covered the whole capacity. A local counter (not in this PR) measured this in one startup run (220 s, first load and the streaming that follows): 81,873 destination checks, 1,595,238 probed pages, **0** pages past the last written page. Every declared capacity ended in the same page as the data. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. (`docs/startup-runtime.md`) - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: the change, tests and this description were written with Claude; I ran the startup A/B. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
R0Y00 commented 2026-11-26 03:03:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T02:38:36Z.

Review: CHANGES_REQUESTED


The measured VirtualQuery cost is worth fixing. I ran a source-built MSVC /O2 witness against this head. With a vectored observer, VirtualQuery produced 0 guard notifications. probe_writable refused the same protected page and restored PAGE_GUARD, but the observer saw 1 first-chance guard exception before the local handler. A second case used a FILE_MAP_COPY page followed by PAGE_NOACCESS. The probe stopped after 4096 of 8192 bytes yet QueryWorkingSetEx showed the first page changed from shared to private, with the byte unchanged. Please keep non-touch validation for unproven page ranges. Any fast path needs a stable classification that establishes ordinary writable backing across the full range. Preserve the queue failure and no-touch controls, including zero guard notifications and an earlier copy-on-write page remaining shared when a later page refuses the request. See the Microsoft guard-page and vectored-exception-handler documentation.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6067824197 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2685#pullrequestreview-6067824197). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-26T02:38:36Z. **Review:** CHANGES_REQUESTED --- The measured VirtualQuery cost is worth fixing. I ran a source-built MSVC /O2 witness against this head. With a vectored observer, VirtualQuery produced 0 guard notifications. probe_writable refused the same protected page and restored PAGE_GUARD, but the observer saw 1 first-chance guard exception before the local handler. A second case used a FILE_MAP_COPY page followed by PAGE_NOACCESS. The probe stopped after 4096 of 8192 bytes yet QueryWorkingSetEx showed the first page changed from shared to private, with the byte unchanged. Please keep non-touch validation for unproven page ranges. Any fast path needs a stable classification that establishes ordinary writable backing across the full range. Preserve the queue failure and no-touch controls, including zero guard notifications and an earlier copy-on-write page remaining shared when a later page refuses the request. See the Microsoft guard-page and vectored-exception-handler documentation.
upstream-sync closed this pull request 2026-11-26 04:04:19 +00:00
R0Y00 commented 2026-11-26 04:04:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T03:19:29Z.

Review: APPROVED


This head addresses both previous refusals. The Windows Release queue test passed all 15 cases, including zero first-chance guard notifications, the refused copy-on-write request retaining its shared page, and a cold writable destination completing through the fallback. A separate MSVC /O2 witness ran the exact working-set helper across 513 resident pages. It accepted the interval, accepted the first 512 pages, then refused the interval after PAGE_GUARD was set on page 513. The guard remained armed and the observer saw zero exceptions.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6068106386 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2685#pullrequestreview-6068106386). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-26T03:19:29Z. **Review:** APPROVED --- This head addresses both previous refusals. The Windows Release queue test passed all 15 cases, including zero first-chance guard notifications, the refused copy-on-write request retaining its shared page, and a cold writable destination completing through the fallback. A separate MSVC /O2 witness ran the exact working-set helper across 513 resident pages. It accepted the interval, accepted the first 512 pages, then refused the interval after PAGE_GUARD was set on page 513. The guard remained armed and the observer saw zero exceptions.
lukaslw-lab commented 2026-11-26 04:04:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-26T03:08:09Z.


Thanks for the witness. Both findings reproduce here: the new test below fails on the previous head's DLL with the check raised a guard-page exception and first page became private. fe6d764d removes the probe. Nothing reads or writes destination bytes any more.

  • Fast path: one QueryWorkingSetEx per 512 pages of the declared capacity. It accepts the destination only when every page is valid in the working set, not Bad, PAGE_READWRITE or PAGE_EXECUTE_READWRITE, and has no PAGE_GUARD bit. Win32Protection is read only when Valid is set (PSAPI_WORKING_SET_EX_BLOCK).
  • Anything else (not resident, copy-on-write, guard, no-access, uncommitted) goes to the unchanged VirtualQuery walk from main, so no destination is accepted that main would refuse.
Case (startup_dstorage_destination) Previous head This head
Guard middle page: guard notifications seen by a first vectored handler 1 0
Copy-on-write page, then PAGE_NOACCESS: first page Shared after the refusal no (private) yes, bytes unchanged
Read-only / no-access / uncommitted middle or last page refused, queue keeps working pass pass
Committed, never-touched destination (fallback path) accepted — pass

Load cost of the main executable's first load: one release build of main de75613f plus this branch, only XCRDAPI.dll swapped between the two runs (Windows 10 22H2, i7-4790K, RTX 3080 Ti, warm shader cache, same switches). Per-thread CPU time from !runaway every 20 s, paired with the storage heartbeat:

First load (requests 0 → ~9,900) main check this head
Stand-in kernel time, 0–100 s 70.1 s 1.6 s
Kernel time per request, per 20 s window 8.3–16.3 ms 0.1–0.3 ms
  • A local counter (not in this PR) counted 36,130 destinations on the fast path and 32,649 on the VirtualQuery walk over 170 s. The walks that remained cost about 9 µs each, the fast path about 23 µs.
  • After the first load both arms spend 4–8 ms of kernel time per request; that part is decompression page faults, not this check.

Limits:

  • The classification is a snapshot, as the VirtualQuery walk on main is: a protection change by another thread between the check and the copy is not covered by either.
  • The explicit PAGE_GUARD test in the fast path is not pinned by the guard case: on Windows 10 22H2 the guarded page already reports Valid = 0 and falls back. I kept it because the documentation does not say a guarded page cannot be valid.

ctest --preset dev|release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save': 70/70 each.

This reply was prepared with AI assistance (Claude); the runs are mine.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7181426664 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2685#issuecomment-7181426664). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-26T03:08:09Z. --- Thanks for the witness. Both findings reproduce here: the new test below fails on the previous head's DLL with `the check raised a guard-page exception` and `first page became private`. `fe6d764d` removes the probe. Nothing reads or writes destination bytes any more. - **Fast path:** one `QueryWorkingSetEx` per 512 pages of the declared capacity. It accepts the destination only when every page is valid in the working set, not `Bad`, `PAGE_READWRITE` or `PAGE_EXECUTE_READWRITE`, and has no `PAGE_GUARD` bit. `Win32Protection` is read only when `Valid` is set ([PSAPI_WORKING_SET_EX_BLOCK](https://learn.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-psapi_working_set_ex_block)). - **Anything else** (not resident, copy-on-write, guard, no-access, uncommitted) goes to the unchanged `VirtualQuery` walk from `main`, so no destination is accepted that `main` would refuse. | Case (`startup_dstorage_destination`) | Previous head | This head | |---|---|---| | Guard middle page: guard notifications seen by a first vectored handler | 1 | 0 | | Copy-on-write page, then `PAGE_NOACCESS`: first page `Shared` after the refusal | no (private) | yes, bytes unchanged | | Read-only / no-access / uncommitted middle or last page refused, queue keeps working | pass | pass | | Committed, never-touched destination (fallback path) accepted | — | pass | Load cost of the main executable's first load: one release build of `main` `de75613f` plus this branch, only XCRDAPI.dll swapped between the two runs (Windows 10 22H2, i7-4790K, RTX 3080 Ti, warm shader cache, same switches). Per-thread CPU time from `!runaway` every 20 s, paired with the storage heartbeat: | First load (requests 0 → ~9,900) | `main` check | this head | |---|---|---| | Stand-in kernel time, 0–100 s | 70.1 s | 1.6 s | | Kernel time per request, per 20 s window | 8.3–16.3 ms | 0.1–0.3 ms | - A local counter (not in this PR) counted 36,130 destinations on the fast path and 32,649 on the `VirtualQuery` walk over 170 s. The walks that remained cost about 9 µs each, the fast path about 23 µs. - After the first load both arms spend 4–8 ms of kernel time per request; that part is decompression page faults, not this check. Limits: - The classification is a snapshot, as the `VirtualQuery` walk on `main` is: a protection change by another thread between the check and the copy is not covered by either. - The explicit `PAGE_GUARD` test in the fast path is not pinned by the guard case: on Windows 10 22H2 the guarded page already reports `Valid = 0` and falls back. I kept it because the documentation does not say a guarded page cannot be valid. `ctest --preset dev|release --no-tests=error -R 'storage|dstorage|linear_buffer|xcrdapi|save'`: 70/70 each. This reply was prepared with AI assistance (Claude); the runs are mine.
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
R0Y00/Xbox2Windows!2677
No description provided.