startup: prove slot 133 placements without walking the allocation #1759
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1766"
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 rasptum on 2026-11-24T18:15:37Z.
Problem
Device slot 133 (
device_place_resource_xinsrc/startup_stubs/d3d12_xs_device.cpp) aliases a buffer at a title address only if the memory is private and every byte from the base of the allocation holding the address to the end of the buffer is committed in that allocation. To decide this, it measured the whole committed run from the base, oneVirtualQueryper region, and compared the buffer with that length.VirtualQueryreportsRegionSizeby scanning page-table entries from the queried address to the end of the run of identical attributes. On a large private region with touched pages, each call therefore costs milliseconds.Synthetic measurements, Windows 10 19045, ms per call:
The cost follows the distance to the next attribute change, so even a walk that stops at the end of the buffer pays it in full when the buffer starts inside a large region.
Change
src/startup_stubs/placed_buffer_range.h(new, testable without a device) proves each placement at call time and keeps nothing between calls:QueryVirtualMemoryInformation(MemoryRegionInfo)(Windows 10 1607 and later, resolved withGetProcAddress) reports the base, size and committed bytes of the allocation holding the address. These come from the allocation's bookkeeping, without the scan: about 0.0004 ms per call on the blocks above. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size.The decision equals the full walk's in every case. A page decommitted, released or replaced directly through the Windows API is seen by the next placement. Unchanged: 64 KiB offset alignment,
MEM_PRIVATEonly, the acceleration-structure refusal, existing-heap reuse, and slot 133's log lines.The five-second
x2w-d3d12 views notedline instderr.txtends withplaced buffers <n> (region-information proofs <n>, region queries <n>).docs/startup-runtime.mddescribes this.Committed-bytes exactness: for private allocations, the committed-bytes value matched a full
VirtualQuerycount of committed pages in every case tried:PAGE_NOACCESSandPAGE_GUARD;MEM_RESET;A mapped view reports flags 0x8 with no committed bytes, and a free address makes the call fail. Both take the walk.
History: the first version of this PR cached measured extents and validated hits with
VirtualQueryat the buffer's first and last byte. Review showed that this does not prove a contiguous committed prefix, so the cache was removed. A walk stopped at the buffer end was exact, but slower than the original code in a startup run, for the reason above. This version keeps that walk only as the fallback.Measured in a run
In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343
VirtualQuerycalls in total).Tests
startup_placed_buffer_rangeruns on the CPU only, with no device. It compares every alias or refusal, base and type with the full walk (the old code, kept in the test as the reference) on the same memory at the same moment. Each case places a buffer, then changes the memory, then places again, and also asserts the expected decision.MEM_WRITE_WATCH,MEM_TOP_DOWNandPAGE_EXECUTE_READWRITEallocations, proven by region information;MEM_PHYSICAL) reservation, with nothing committed;MEM_LARGE_PAGESwhen the process may lock pages (otherwise reported as not run).VirtualQuerycall. This is a call count, not a time. The check prints the time per placement: full walk 4.7 ms, bounded walk 4.6 ms, region proof 0.0004 ms (dev;releasewas similar).Command (Windows 10 19045, MSVC 19.44, RTX 3060;
_warptests use WARP,_hardwaretests the RTX 3060), run in bothdevandrelease:Observed: 12 of 12 passed, 0 failed, 0 skipped, in both presets. Besides the new check and its two controls, the selection covers the existing device tests that exercise slot 133 and the neighbouring placement checks:
startup_placed_vertex_{warp,hardware},xmem_page_runs_device_{warp,hardware},cpu_placed_draw_{warp,hardware}andstartup_placement_{alloc,publish,publish_shrink_control}. They pass unchanged.tests/docs_links_check.pypasses. The branch merges current main (db924b76).Negative controls
Both controls are registered with
WILL_FAIL. Each exits 1 only when it fails for its stated reason and nothing else fails, and exits 77 (a capability skip) if an allocation or mapping is refused.startup_placed_buffer_range_region_only_control(--mutate region-only) trusts the allocation size without the committed-bytes check. Observed: 10 placements differ from the full walk, all in the partly committed cases (2, 4, 5, 8, the heap block and the AWE reservation), and none elsewhere.startup_placed_buffer_range_walk_only_control(--mutate walk-only) never uses the region information. Observed: decisions all match, and only the call count of the 512 MiB case fails (0 of 64 region proofs, 128VirtualQuerycalls).Not covered
VirtualQuerycost (129 of 3,388 placements in the run above).MEM_LARGE_PAGESneeds the lock-pages privilege and was not run here. An AWE region with physical pages mapped needs the same privilege and was not tried; an AWE reservation reports the private flag with no committed bytes and takes the walk.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T18:38:02Z.
The first and last VirtualQuery results do not establish a contiguous committed range. For example, after caching a private three-page allocation, the middle page can be decommitted while the first and last pages remain committed with the same AllocationBase. The uncached walk stops at the gap, but still_valid accepts both endpoints and returns the old extent. A gap before the requested start also changes the original walk's answer. Before using a hit, prove the full committed prefix needed by slot 133 or use reliable invalidation tied to the allocator lifecycle. Please add a source-built middle-page decommit/recommit control and compare the alias/refusal decision with the uncached path. Keep the same-base replacement controls.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:05:41Z.
Review: COMMENTED
Peer review of head
229f7b1350f3057bc75c3f5d0eacf11d422376fc(PRODUCTION_REVIEW_ONLY, technical comment only).One P2 finding (a documented limitation that is wider than documented). No P1.
P2:
src/startup_stubs/placed_allocation_cache.h,measure()/still_valid()(lines ~66-70 and ~101-113).The uncached walk counts committed bytes contiguously from the allocation base and stops at the first hole. A cache hit only re-queries the region at the buffer's first byte and the region at its last byte. Pages decommitted between the base and the first queried region are therefore not noticed either, not only pages between the two queried regions as docs/startup-runtime.md states. The cached
extent.bytesstays too large, so slot 133 accepts a placement the uncached path refuses (offset > allocation_bytesgivesE_INVALIDARG).Reproduction (local, mingw-w64 build of
placed_allocation_cache.hrun under wine):VirtualAlloc1 MiB committed.measure(p+0x80000, 0x10000)remembers the extent (0x100000).VirtualFree(p+0x10000, 0x10000, MEM_DECOMMIT).measure(p+0x80000, 0x10000)is a cache hit and returns bytes=0x100000.measure_allocation()on the same address returns bytes=0x10000, so the original code returnsE_INVALIDARG.Impact: the placement then reaches
CreatePlacedResource, which the code comment says throws inside D3D12Core, instead of being skipped. The comment in the docs says no stand-in decommits part of an allocation, so this is low-likelihood. Either widen the doc sentence to "anywhere before the buffer's last byte", or add a cheap guard (for example drop the entry onVirtualFreeorMEM_DECOMMIThooks, or bound the cache lifetime).Everything else checked without findings:
upper_bound, thenlast - base < bytes) and the overflow guard onstart + span - 1.remember()drops overlapping entries, so a block replaced at the same base is handled.E_POINTER/descchecks beforedesc->Widthis read.Local mutation (restored with
git checkout -- .): on a cache hit,out = knownchanged toout = known; out.bytes += 0x10000;.startup_placed_allocation_cache_checkfails with "258 failure(s)" ("partly committed: inside the larger extent: expected bytes=0x200000, got 0x210000"). A second mutation,last - base < byteschanged tostart - base < bytes, survives. It is masked by the last-byte region query instill_valid(), so the behaviour is equivalent; I am not asking for a new test for it.Commands run:
git diff --check <merge-base>...HEAD: cleantests/bidi_controls_check.py: passtests/startup_placed_allocation_cache_check.cpp, run under wine: default run passes ("all cases match the uncached path", 256 of 256 accepted, 2 walks, 256 saved).--mutate noneand--mutate address-onlyfail for the stated reasons.229f7b13, targetstartup_placed_allocation_cache_check, regex^startup_placed_allocation_cache:--preset dev): 3/3 tests passed/O2(--preset release): 3/3 tests passedmain(git merge-tree).AI-assisted; commands listed were executed locally.
[PR #1766] startup: cache placed-buffer allocation extents (slot 133)to [PR #1766] startup: prove slot 133 placements without walking the allocationImported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:32:15Z.
Review: CHANGES_REQUESTED
The endpoint checks can both pass after a middle page of the same MEM_PRIVATE allocation is decommitted. For a three-page fully committed allocation, warm the cache, decommit the middle page, then query the original full span: the old walk stops after page one, but the proposed hit still reports the old full extent. A gap can also sit before the placement start because the alias decision relies on the committed run from AllocationBase. Please require a current contiguous committed range from the base, or tie the cache to tracked allocation generations and page-state invalidation. Retain the existing walk for addresses without that lifetime contract. Add a source-built interior-decommit control with committed endpoints; both dispositions must agree with the original walk.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:46:49Z.
Review: APPROVED
Rechecked the replacement without a persistent extent cache. The Windows Release range test and both negative controls pass on this exact head. A partially committed allocation now walks from AllocationBase, so the middle decommit and gap before placement agree with the old walk.
Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-24T21:36:25Z.
Thanks, you are right. Checking only the endpoints did not prove a contiguous committed prefix. After a private allocation was cached, decommitting a middle page left both endpoints committed with the same AllocationBase, so the cache kept accepting a buffer after the gap that the uncached walk refuses. A gap before the requested start went unseen in the same way.
What changed: the cache is gone, and nothing is kept between placements. Lifecycle invalidation cannot be made complete, because a consumer can call
VirtualFree(MEM_DECOMMIT), or release and reallocate memory, directly through the Windows API, and the stand-ins never see that.Each placement is now proven at call time (
src/startup_stubs/placed_buffer_range.h):QueryVirtualMemoryInformation(MemoryRegionInfo)reports the base, size and committed bytes of the allocation holding the address. When the allocation is private and its committed bytes equal its size, every page is committed, so the full walk's committed run is the whole allocation. The decision then follows exactly from base and size.In a local startup run on Windows 10 (RTX 3060), the time from launch to the first interactive screen was 72.6 s, against 81 to 94 s with the earlier cache and 197 to 252 s with the bounded walk alone. Of 3,388 placements, 3,259 were proven by region information and 129 fell back to the walk (343
VirtualQuerycalls in total).Why not only the bounded walk: in a startup run it was slower than the original code.
VirtualQueryscans page-table entries up to the next attribute change, which costs milliseconds on large touched regions (synthetic: about 5 ms per GiB with one touched page per 64 KiB). The region-information call does not scan; it costs about 0.0004 ms. Its committed-bytes count matched a fullVirtualQuerycount for private allocations after decommit and recommit, double decommit, commit of committed pages, guard and no-access pages,MEM_RESET, reserve-then-commit and placeholder pieces.startup_placed_buffer_range(replacesstartup_placed_allocation_cache) compares every alias or refusal, base and type with the full walk on the same memory. Each case places a buffer first, then changes the memory, then places again:VirtualQuerycall.The controls (
WILL_FAIL) must fail only for their stated reason.--mutate region-onlytrusts the allocation size without the committed-bytes check and fails 10 placements, all in the partly committed cases.--mutate walk-onlynever uses the region information and fails only the call count.@tedin7: your repro is a case in
startup_placed_buffer_rangewith exactly those numbers. The endpoint-only cache at229f7b13accepted the second placement, while the current code refuses it, the same answer as the full walk. It is refused because a decommitted page makes the allocation's committed bytes smaller than its size, so the placement falls back to the walk from the base, which stops at the gap.Windows 10 19045, MSVC 19.44, RTX 3060 (WARP for
_warp),devandrelease, head20f8e1d5(merges current main): 12 of 12 passed, 0 failed, 0 skipped.AI-assisted (Claude Code).