Skip to content

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

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/dstorage-destination-probe
Nov 26, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/dstorage-destination-probe

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

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.

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

…h VirtualQuery

destination_writable walked the destination with VirtualQuery. On the title's
multi-gigabyte regions each query scans to the end of the region, which cost the
storage worker about 10 ms of kernel time per request during the first load.

Probe one byte per page with an atomic OR of zero under SEH instead
(x2w_local::probe_writable, the write twin of probe_readable). It faults where a
write would, changes no byte, and re-arms a guard page it trips. The test now
also checks the guard bit after the refusal and that probed pages past the data
keep their bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Review of R0Y00#2685: the write probe raised a first-chance guard-page exception
that a vectored handler sees, and it gave a copy-on-write page its private
copy even when a later page refused the request.

Remove probe_writable. destination_writable now reads page attributes only:
one QueryWorkingSetEx per 512 pages accepts a destination whose pages are
all valid, PAGE_READWRITE or PAGE_EXECUTE_READWRITE and not guarded; any
other page goes to the VirtualQuery walk from main, unchanged.

The test adds a first vectored handler that must see no guard-page
exception on any refusal, a copy-on-write page followed by a no-access page
whose first page must stay shared, and a committed but untouched
destination that takes the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

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.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit 9caa698 into R0Y00:main Nov 26, 2026
5 checks passed
@lukaslw-lab
lukaslw-lab deleted the runtime/dstorage-destination-probe branch November 26, 2026 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup-runtime: the DirectStorage destination check costs ~10 ms of kernel time per request (VirtualQuery over large regions)

2 participants