startup: probe DirectStorage destinations instead of walking them with VirtualQuery #2677
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2685"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 callsVirtualQuery. 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 ofprobe_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_writablecallscommit_lazyas before, thenprobe_writable.docs/startup-runtime.md: the destination section describes the probe and its cost.probe_readablealready avoidsVirtualQueryfor 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.QueryWorkingSetExreports protection only for resident pages, so it would still need theVirtualQueryfallback; region information has no current protection.probe_readableinlocal_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(onb2564017).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 stillPAGE_GUARDafter its refusal; a writable destination with a three-page capacity and one page of data succeeds with the two probed, unwritten pages unchanged.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).XCRDAPI.dlldiffers; stand-in CPU time from!runawayevery 20 s):VirtualQuerycheckdestination not writablerefusals, device removalThe 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
VirtualAllocand a pagefile-backed mapping in the test); no title data.commit_lazyalready 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
docs/startup-runtime.md)AI-assisted: the change, tests and this description were written with Claude; I ran the startup A/B.
🤖 Generated with Claude Code
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.
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.
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 exceptionandfirst page became private.fe6d764dremoves the probe. Nothing reads or writes destination bytes any more.QueryWorkingSetExper 512 pages of the declared capacity. It accepts the destination only when every page is valid in the working set, notBad,PAGE_READWRITEorPAGE_EXECUTE_READWRITE, and has noPAGE_GUARDbit.Win32Protectionis read only whenValidis set (PSAPI_WORKING_SET_EX_BLOCK).VirtualQuerywalk frommain, so no destination is accepted thatmainwould refuse.startup_dstorage_destination)PAGE_NOACCESS: first pageSharedafter the refusalLoad cost of the main executable's first load: one release build of
mainde75613fplus 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!runawayevery 20 s, paired with the storage heartbeat:maincheckVirtualQuerywalk over 170 s. The walks that remained cost about 9 µs each, the fast path about 23 µs.Limits:
VirtualQuerywalk onmainis: a protection change by another thread between the check and the copy is not covered by either.PAGE_GUARDtest in the fast path is not pinned by the guard case: on Windows 10 22H2 the guarded page already reportsValid = 0and 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.