Repository navigation
runtime: bring XMem page-run fixes into cpp-migration - #2918
MasterDomino merged 20 commits into
Conversation
The region query reports placeholders with Flags bit 0x100 on Windows. Wine leaves it 0, so alias_placeholders refused every split or coalesce and queue slot 31 batches failed with ERROR_INVALID_ADDRESS whenever a run was not exactly one placeholder. Probe once: if the bit is missing and VirtualFree with MEM_PRESERVE_PLACEHOLDER refuses a plain reservation and leaves it whole, offer any reserved region to the split. A plain one then fails the split instead of being released. Where the bit is reported nothing changes.
…get control xmem_placeholder_split maps two pool pages into the middle of an XMem reservation through X2WMapPageBatch and checks the bytes, a write through the destination and the reserved neighbours. A batch onto a committed page in the same reservation must fail and keep its bytes. xmem_placeholder_split_control disables the fallback and passes only when the split step fails; it skips where the OS reports the placeholder bit.
Add x2w_xmem_copy_to_destination. It checks that the whole destination capacity is committed and writable, and copies the data, both under memory.mutex. A run break (unmapping a page of a shared run and mapping its other pages again) also runs under that mutex, so it can neither make the check see a transient reservation nor start during the copy. Lazily mapped pages of both ranges are committed before the mutex is taken. A fault under the mutex is passed on: the lazy commit handler now skips mapping threads, as retry_remapped_page already did. The region walk moves from XCRDAPI's destination_writable to x2w_local::committed_writable, so both DLLs share one implementation.
A plain-memory destination is now checked and written by xmem.dll under
its mapping mutex when the export is there. Before, the VirtualQuery
pre-check could run while an XMem run break had the destination's page
unmapped for a moment, and refuse a destination that was mapped before
and after. Without xmem.dll the check and the copy run in XCRDAPI as
before.
A copy that faults on its source now fails the request ("copy faulted
(source not readable)") instead of raising in the stand-in.
startup_dstorage_break_window holds an unmap of page 5 of an 8-page run in the window where the run is apart (the xmem test-controls build's X2W_XMEM_TEST_HOLD_WINDOW), checks that page 2 reads MEM_RESERVE there, and submits a memory-source request into page 2. The request must not complete within the 1000 ms hold, then complete with S_OK with every byte in place after the release. It also covers a request with no break and a read-only destination refused on the xmem path. startup_dstorage_break_window_control builds XCRDAPI with the export lookup's name changed, so it judges the destination with its own VirtualQuery inside the window. It must report "refused while the run was broken".
A map into the middle of a larger placeholder always fails first, so every single-page XMemMapPhysicalPages call made two MapViewOfFile3 attempts. Under Wine each attempt is a server round trip whose cost grows with the view count. Split first when the target is not an exact placeholder; fall back to the map-first path if the split or the map fails. X2W_XMEM_TEST_MAP_FIRST=1 keeps the old order for tests. x2w_xmem_map_counts reports the attempts.
With X2W_XMEM_SHARED_VIEWS=1, unmapping a page inside a run unmaps the whole run and maps its other pages again. A touch of those pages in between is retried by the fault handler, but a region query or a kernel copy does not fault: it reads the pages as reserved, or fails. x2w_xmem_stable_call(fn, context) runs fn when no break begins or ends around it, else again under the mapping mutex. XMemVirtualQuery, and through x2w_local::outside_run_breaks the stand-ins' x2w_local::writable, measure_placed_buffer (slot 133) and snapshot_samplers, now answer that way. Without shared views no break begins, so fn runs once as before. xmem_run_break_readers holds a break open with the test-controls build and checks that each reader waits for it and then sees the page committed; its control build runs the stand-in readers directly and they answer inside the window.
The title queries its own pages through its VirtualQueryEx import. With X2W_XMEM_SHARED_VIEWS=1 xmem.dll now points that import at a wrapper that runs the query through x2w_xmem_stable_call, so a page of a run that is being taken apart is not reported as reserved. The module is pinned only when an import was changed (the pin is undone with the import if it is refused), so a main module without the import can still unload the shim before its first mapping call. xmem_run_break_readers checks the wrapped import of its own executable inside a held break.
retry_remapped_page names the break a fault belongs to as seen | 1. When the handler reads the generation after the break has ended (even), that names the next break, so a fault on the same page in that next break is not retried and the access violation ends the process. Name the break that just ended instead: (seen - 1) | 1. Back-to-back breaks over one page made this show up: with a reader on page 20 and page 21 toggled, the hold-ignored child of xmem_page_runs_check.py crashed in 6 of 6 runs once map calls also open a break, and passed 6 of 6 with this. Test build: X2W_XMEM_TEST_HOLD_RETRY=<name> holds the handler before it reads the break. xmem_run_break_readers gets a case that faults in break A, is held until A ends, and faults on the same page in break B. The xmem_run_break_readers_next_break_control test runs it without the hold.
…after a break Without X2W_XMEM_SHARED_VIEWS, every unmap still counts in the break generation (begin_break is not gated on the switch), so x2w_xmem_stable_call made default-mode readers wait for memory.mutex. Each page has a view of its own there, and no break takes a page away from a reader, so stable_call now runs fn directly until shared views are on. The comment, and the message of "xmem: answer non-faulting readers outside run breaks", saying no break begins without the switch were wrong. XMemVirtualQuery and the VirtualQueryEx import wrapper answered into the caller's buffer inside stable_call. On its mutex path the lazy-commit handler passes faults on, so a buffer in an uncommitted mapped slot was not committed there. Both now answer into a local copy and write it out after the call. A refused pin of the VirtualQueryEx wrapper is logged with its error and the restore count. measure_placed_buffer keeps the query count of a pass that ran again. The docs describe stable_call and the load-time read of the switch. "xmem: answer the main module's VirtualQueryEx outside run breaks" said the title queries its own pages through VirtualQueryEx. An import scan shows only that the executable under test imports it. How often it is called was not measured. Checks in xmem_run_break_readers: a break that begins while fn runs makes it run again under the mutex (pins the after-check); default mode answers inside an unmap (xmem_run_break_readers_default); lazy commit with the output in an uncommitted slot (xmem_run_break_readers_lazy); the reader's 300 ms now count from its start.
Start-up maps nearly every page with a call of its own (45595 of 45602 runs had one page), and under Wine each map costs more the more views the process has. With X2W_XMEM_SHARED_VIEWS=1, a mapping call whose pages form one run now joins the recorded run that ends at its address. It joins only when its first page is the next slot of the section behind that run's last page, and both lie in one reservation of the shim. The new pages' range is split to a placeholder of its own, the run is unmapped inside a break (begin_break, as for an unmap), the two placeholders are joined, and one view is mapped over both. A refused step before the unmap changes nothing. After it, the run is mapped again as it was (map_recorded) and the new pages get a view of their own. If the run cannot be restored, its pages are forgotten and the call fails, as in an unmap. Default mode is unchanged. Test build: X2W_XMEM_TEST_NO_MERGE=1 turns the merge off; X2W_XMEM_TEST_FAULT stages 4 (joined map) and 5 (joining the placeholders) refuse a step. xmem_cross_call_merge covers 32 single-page calls -> one view, an unmap in the middle, slots in reverse order (32 views), a reader held in the window, both refused steps, and two reservations. xmem_cross_call_merge_control keeps the old one-view-per-call layout under NO_MERGE. xmem_cross_call_merge_deployed shows the deployed shim ignores that switch. xmem_cross_call_merge_default covers default mode. xmem_page_runs_check.py expected "Mapping the page again does not join the pieces: three views". The page now joins the lower piece (two views). The three-view layout stays asserted by xmem_cross_call_merge_control. xmem_page_runs_device_check maps the upper view first and the single pages in reverse slot order, so its view boundaries stay.
An unmap inside a run takes the whole run apart and maps its other pages again, and a cross-call merge does the same to the run it joins. Nothing bounded the joined run, so after loading every title unmap inside a run of thousands of pages cost one long break: 46-72 ms each in a gate run under Wine, with readers waiting 63 s in all and presents at 2.4/s. merge_with_previous now leaves a page on its own when the joined run would be longer than max_merged_run_pages (64); the next pages join that one. The test build's X2W_XMEM_TEST_MERGE_CAP replaces the cap for a timing mode. Measured under Wine with every byte of the run written (xmem_cross_call_merge_check timing, median of 5): an unmap inside a run of 64 pages takes 0.28 ms, of 512 pages 2.1 ms, of 4096 pages 13 ms; joining a page to a run of 63 takes 1.5 ms, about what a page mapped on its own costs (1.4 ms). The fixture process has a few views, the title process tens of thousands. xmem_cross_call_merge gets a capped case: 192 single-page calls make three views of 64 pages, and an unmap held in its window in the middle one takes apart 64 pages while the outer two stay mapped. The control, deployed and default modes now print their pass line at the end of main, only with no failed check.
begin_break replaced the recorded ranges, so retry_remapped_page saw only the last break. A handler that ran after a later break on another run had begun found its address outside those ranges and passed the fault on. Joins made that the normal case: one permissive XMemMapPhysicalPages call joins the CPU run and then its GPU mirror run under one memory.mutex hold, and each map call opens a break of its own. RemapWindow now keeps the ranges of the last 64 breaks with their id (the odd generation). The handler names the newest break whose ranges hold the address, so a fault in a later break of the same page is still retried once more. xmem_run_break_readers_two_runs faults in break A on run 1 and holds the handler until break B on a disjoint run 2 is held open. xmem_run_break_readers_mirror does the same across the CPU join and the GPU mirror join of one call (X2W_XMEM_PERMISSIVE, X2W_XMEM_GPU_MIRROR). With one kept break both end the process with an access violation (exit 5). The RemapWindow and hold_window comments now also name the merge.
…e buffer Inside a run break a kernel copy into or out of a page of the run fails at once, with no fault to retry. Measured under Wine with the break held open (xmem_run_break_readers ReadFile_import/WriteFile_import, before this change): ReadFile fails with ERROR_NOACCESS and leaves the file position at 0, WriteFile fails with ERROR_INVALID_USER_BUFFER. With X2W_XMEM_SHARED_VIEWS=1 at load, the main module's ReadFile and WriteFile imports go through a wrapper. A synchronous call that fails with one of those errors while a break was in progress, or began or ended around it, and whose buffer lies in a run, waits for memory.mutex and runs again, at most 4 times in all. Other failures return at once as before: a read into a reserved page in no run fails at once (io_outside_runs). With lazy commit the ReadFile wrapper calls read_file_committed inside (lazy_read_file). Overlapped I/O, a call that a break cuts short after part of the copy, and I/O from other modules stay uncovered.
…lines at the end XMemVirtualQuery copied all 64 bytes of the caller's buffer before the query and wrote all 64 back after it. era_query writes 49 (the MBI and the tail byte at 0x30), so bytes 49-63 were read and rewritten where they never were before, and a buffer of 49-63 bytes that ends at a page end could fault. The caller's buffer is no longer read; on success tail_offset + 1 bytes go back, on failure none. xmem_run_break_readers_default prints its pass line at the end of main, only with no failed check, so a crash after the cases does not read as a pass. xmem_page_runs_device_check prints a check_failed line before the map-order returns, which were silent and looked like a refused device.
…g's edge The cap of 64 pages (max_merged_run_pages) is checked only where a page joins the run before it (merge_with_previous). A run mapped by one call (map_run in map_pages_locked) is not capped, and neither are the survivors map_recorded maps again after an unmap inside such a run. Both are as long as before the cap; a single large call is the title's own choice and was not the regression. The comment above the cap and docs/startup-runtime.md said the cap bounds every later break of a run; they now say it bounds the runs that joining builds, and name the two uncapped paths. xmem_run_break_readers_ring_edge holds a fault in break A before its handler reads the breaks, ends A, then makes 63 or 64 later breaks (map and unmap a lone page). After 63, A is still recorded and the fault is retried; after 64, A's slot (RemapWindow::kept) holds the newest break and the fault is passed on, which the reader's own __except catches.
…into codex/merged-page-runs-cpp-migration
MasterDomino
left a comment
There was a problem hiding this comment.
Review of head f6020249 on cpp-migration 40365770, with a Windows run that includes the test family left out of the description's filter.
What this is: git range-diff shows the 17 commits of #2911 (4a43a173), unchanged, plus one fixture commit, f6020249.
Environment: Windows 11 build 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010) and WARP 10.0.26100.9818, MSVC 19.44.35221, release preset, D3D12 debug layer not enabled.
ctest --preset release --no-tests=error -j 4 --timeout 300 -R '^(xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first|shim_(mapped_pages|page_runs)|page_runs_device)|startup_dstorage_|linear_buffer_backing_|native_direct_container)'56 tests: 53 passed, 1 skipped (77, xmem_placeholder_split_control, because Windows reports the placeholder bit), and 2 failed:
| test | this PR f6020249 |
base 40365770 |
|---|---|---|
xmem_page_runs_device_{warp,hardware} |
pass | pass |
linear_buffer_backing_share_{warp,hardware} |
fail | pass |
The failing row is share xmem split FAIL: slot 133 -> 0x00000000, copied (share 0x80070032, feature 0x00000000, 131072 bytes in 1 refreshes, staging 1). That is the same failure I reported on #2905 and @Not-Saii reported on #2911. allocate_split maps alloc_pages - 1 pages and then one more page with a second call, and expects a view boundary inside the buffer. With shared views, the second call now joins the first. The description's filter does not include linear_buffer_backing_, so the 23/1/0 result does not cover it.
f6020249: this fixture change is correct as far as it goes. A join only attaches a call to the run that ends at its address, so mapping the single pages from high to low leaves every page in its own view whatever slot it got, and xmem_page_runs_device passes again on WARP and on hardware. The assertion is unchanged. It would fix one of the two failures on #2911 as well, so it may be better offered to @JohnConnorNPC there.
Requested before this lands
linear_buffer_backing_sharefails on both adapters with this PR and passes on the base. Either the joins must not change that case's layout, or the fixture must build its boundary in a way joins cannot merge. Then a Windows run oflinear_buffer_backing_is needed.- Ordering with #2911. These are #2911's commits unchanged, and #2911 is still under review against
main. It has the two Windows failures, @Not-Saii's four open points (the DirectStorage copy bypassing the #2685 resident check undermemory.mutex, the 64-break history now that every join is a break, joins being active on native Windows through-LazyMemory, and the one-page placeholder check under Wine), and the smaller items. Landing the same commits incpp-migrationfirst would bypass that review. I would hold this until #2911 settles, then bring over what lands there, or let @R0Y00 decide whethercpp-migrationshould carry it earlier.
For context: the -LazyMemory write fault on Windows predates these commits. On #2905 I measured it at the same executable offset with and without the PR, and @pigalot's runs on #2911 show the same. It is not a reason to hold this PR, but this PR does not fix it either.
.github/workflows/windows.yml runs only for pull requests into main, so this PR has no CI. The local runs above and in the description are the only evidence.
AI-assisted; the commands above were run locally.
Same change as f602024 in R0Y00#2918.
|
Thanks for carrying these commits into For anyone repeating our start-up runs on this branch: we used one more local patch, attached below. It is a diagnostic aid, not part of #2911, and not proposed for merge. It changes only
Where it was run: only on Linux, under Proton Experimental (wine-11.0), with the clang-cl cross build. It was never run on
proxy-fallback.diffdiff --git a/src/startup_stubs/runtime_proxy.cpp b/src/startup_stubs/runtime_proxy.cpp
index 7c9a1704..3b366bd5 100644
--- a/src/startup_stubs/runtime_proxy.cpp
+++ b/src/startup_stubs/runtime_proxy.cpp
@@ -322,11 +322,35 @@ void fill_slots(std::index_sequence<I...>)
((slot_table[I] = reinterpret_cast<void*>(&forward<static_cast<int>(I)>)), ...);
}
+// Each (provider, interface) the title asks the system runtime for, logged once with the runtime's answer,
+// failures included: a refused area is otherwise invisible, since only answered providers are proxied.
+void log_query_once(const GUID* provider, const GUID* iid, HRESULT hr)
+{
+ if (!x2w_local::readable(provider, sizeof(*provider))) return;
+ const unsigned long iid1 = iid && x2w_local::readable(iid, sizeof(*iid)) ? iid->Data1 : 0;
+ static SRWLOCK lock = SRWLOCK_INIT;
+ static GUID seen[64][2];
+ static int count;
+ AcquireSRWLockExclusive(&lock);
+ bool fresh = count < 64;
+ for (int i = 0; i < count && fresh; ++i)
+ fresh = !(std::memcmp(&seen[i][0], provider, sizeof(GUID)) == 0 && seen[i][1].Data1 == iid1);
+ if (fresh) {
+ seen[count][0] = *provider;
+ seen[count][1].Data1 = iid1;
+ ++count;
+ }
+ ReleaseSRWLockExclusive(&lock);
+ if (fresh)
+ x2w_local::logf("x2w-runtime query %08lx iid %08lx: system runtime 0x%08lx", provider->Data1, iid1, static_cast<unsigned long>(hr));
+}
+
HRESULT __stdcall proxy_query(const GUID* provider, const GUID* iid, void** out)
{
const QueryApi query = load_real_query();
if (!query) return E_FAIL;
const HRESULT hr = query(provider, iid, out);
+ log_query_once(provider, iid, hr);
const bool output_writable = FAILED(hr) && fake_user() && x2w_local::writable(out, sizeof(*out));
const bool pair_readable = output_writable && x2w_local::readable(provider, sizeof(*provider)) &&
x2w_local::readable(iid, sizeof(*iid));
@@ -338,7 +362,9 @@ HRESULT __stdcall proxy_query(const GUID* provider, const GUID* iid, void** out)
return S_OK;
}
const ApiPair* local = nullptr;
- if (output_writable && InterlockedCompareExchange(&local_active, 0, 0)) {
+ // With X2W_FAKE_USER (output_writable requires it), a refused area falls back to its local object
+ // even when the system runtime itself initialised.
+ if (output_writable) {
*out = nullptr;
if (!pair_readable) return hr;
for (const auto& pair : local_pairs) {This comment was AI-assisted. |
Same change as 27778f4 in R0Y00#2918.
|
@MasterDomino thanks for the Windows run. #2911 now carries both fixture changes from this PR unchanged, with the same patch-ids:
The order of #2918 and #2911 is for the maintainers. This comment was AI-assisted. |
|
merging under my own discretion until Royoo isnt back |
|
Reverting on AI-assisted. |
#2931) This reverts commit ee76dfa. Those are the 17 XMem commits of #2911, which is still under review against main, plus two fixture changes. Keeping them off cpp-migration keeps the branch to the native compiler work. When #2911 lands on main, merging main into cpp-migration brings in the reviewed version. Every file #2918 touched is restored to its state at cd752a8. The later #2917 touches none of them. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Start-up runs with lazy memory off, as promised in #2931. This PR has since been reverted on Setup: two matched pairs of 420 s runs, the first pair with the PR first and the second with the base first. Windows 11 build 26300.9974, AMD Radeon RX 9070 XT, driver 32.0.32029.2010,
AI-assisted; the commands above were run locally. |
Change
Bring the 17 XMem commits from JohnConnorNPC's
memory/merged-page-runsintocpp-migration. This preserves the native DXIL compiler and adds the Wine placeholder fallback, DirectStorage copy synchronization during XMem run breaks, and opt-in joining of adjacent XMem mappings.The integration adds one fixture correction. The single-page device case maps high addresses first so it constructs two separate views regardless of backing-slot allocation order. Before the correction, both device tests saw one view where the fixture expected two. The assertion and placed-resource checks are unchanged.
Refs #2911.
Verification
Tested
f602024969c28e70cfd15eae2280a25a3d2d7a46against base40365770on Windows 10 Pro 22H2 (19045.6456), MSVC 19.31.31105, and Python 3.12.14. The build passed. CTest reported 23 passed, 1 skipped, 0 failed. The placed-buffer cases passed on WARP (systemd3d10warp.dll10.0.19041.5794) and NVIDIA GeForce GTX 1080 Ti (32.0.15.8278). The fixture did not enable the D3D12 debug layer. The placeholder fallback control skipped because native Windows reports the placeholder bit.Verification used the repository's source-built fixtures. The no-merge and DirectStorage run-break controls passed. Wine results for the incoming branch are recorded in #2911. The Windows Actions workflow is configured for PRs targeting
main, so this PR'scpp-migrationbase does not trigger that workflow.AI-assisted integration and fixture correction.