Repository navigation
Conversation
XMemAllocatePhysicalPages backed each call with one SEC_COMMIT section, so the title's ~6.1 GiB page-pool request after boot stop 17 was charged in full at once and fails with ERROR_COMMITMENT_LIMIT on a 16 GB machine. Mapping the pages right away (as the title does) would charge most of it within seconds too, so the commit now follows first access. The section is SEC_RESERVE. A vectored handler commits the faulting 64 KiB page through a reserve-only view of the whole section when the address lies in a live mapping, so every view of that page sees it. Faults on committed pages whose protection forbids the access, and faults outside the mapped pages, go on to the next handler, and a failed commit is not swallowed. rt::xmem::commit_physical_range commits a range up front for shims that hand guest memory to the kernel. Reported by controldebils and rafaelpssz in ItsNotRexed#7333.
|
Independent Windows test of Setup: Windows 11 x64, MSVC 19.44.35220 (
AI-assisted review/test. |
A writer that copies into physical pages through its own view of their section (a DirectStorage destination given as page numbers, ItsNotRexed#7652) sees reserve-only memory for pages the title has not touched yet, and no fault handler can resolve that address. commit_physical_pages commits the given pages through each section's own view, one VirtualAlloc per contiguous run, so such a writer can commit before it copies. Raised by nurtrino in ItsNotRexed#7599.
tupacfan04
left a comment
There was a problem hiding this comment.
The race is still present at head abd85df49.
runtime/shims/xmem.cpp:455-469 moves the MappedPage out of g_mapped before acquiring FaultLockExclusive:
MappedPage mapped = std::move(it->second); // :458
{
const FaultLockExclusive lock; // :459-462
g_mapped.erase(it);
}The first-touch fault path holds FaultLockShared and reads the same entry (:542-545), then dereferences it->second.section->scratch at :497. In the move window the entry’s section is already moved-from.
Deterministic instrumentation (pause after the move) observes:
fault_seen=1 fault_section=0 faulted=1
The threaded faults + concurrent map/unmap run on the unmodified new-head binary hangs past the timeout (RACE_NEW_EXIT=-1), while fault-only and map-only runs complete. map_page also publishes the new view before inserting the g_mapped entry (:474-490), so a fault in that window finds no entry and falls through.
Expected: take the exclusive lock before moving/erasing (or copy under it), and publish/register the map atomically; add a threaded fault-vs-unmap regression. Synthetic only; no game files. AI-assisted (OpenCode).
…record unmap_page moved the record out of g_mapped before taking the exclusive lock, so a first-touch fault on that page could read a moved-from section, and map_page made the view live before recording it, so a fault in between was not recognised. unmap_page now holds the exclusive lock from the lookup to the erase, and map_page holds it across mapping the view and recording it. xmem_test adds four threads writing into a window while 256 slices of pages are mapped and unmapped under them. Reported by tupacfan04 in the review of ItsNotRexed#7556.
|
@tupacfan04, thanks, both races were real. Fixed in 75c2b87:
RTX 4070, Windows 11, MSVC 14.43 RelWithDebInfo: with the fix, AI-assisted: yes |
|
Tested
AI-assisted. |
…ting address The handler queried the protection at the start of the 64 KiB page, so a write to a 4 KiB part the title had made read-only looked allowed and was resumed, faulting again forever instead of reaching the title's own handler. It now looks the record up by the page and the protection up by the faulting address. xmem_test writes to a read-only 4 KiB part of a read-write page and expects the fault.
|
Tested Built Without the fix: the new test asks for 1 GiB more than the free commit, which the old |
The commit-on-touch checks expect hardware faults from plain loads and stores inside __try. MSVC catches those as it is; clang-cl only with asynchronous exceptions, so without /EHa the expected fault escaped and xmem_test crashed. Same option as tiny_dll. Reported by AirKyzzZ in ItsNotRexed#7556.
|
@AirKyzzZ, thanks for the clang-cl run and the bisect. Fixed in e85ebec: AI-assisted: yes |
Claimed in the coordination issue: #7333 (comment) (plan updated in #7333 (comment)), from @controldebils's finding #7333 (comment). @rafaelpssz measured the same failure and that commit-on-map is not enough (#7333 (comment)); thanks to both.
Depends on: none (based on
mainonly). Touches the samexmem.cppfunctions as #7492, #7550, #7580, #7331 and #7499; whichever lands second may need a small rebase aroundmap_page/view_protect.#7652 (DirectStorage DestinationIsPhysicalPages): its
rt::xmem::write_physical_pageswrites through its own section view, so on top of this PR it must callrt::xmem::commit_physical_pages(page_array, count)before copying (pages the title has not touched are reserve-only here). Raised by @nurtrino in #7599 (comment).commit_physical_pages(second commit) commits pages by page number through each section's own view, oneVirtualAllocper contiguous run;xmem_testchecks that two never-mapped pages committed this way show as committed once mapped while a third stays reserved, and that a null array or unknown page number fails (201 checks OK,ctest -R "xmem|loader"5/5, RTX 4070).#7712 (g_windows/g_mapped cleanup on window reuse): merges cleanly with this PR, but its
clear_window_rangeerasesg_mappedentries underg_phys_mutexonly. On top of this PR, everyg_mappedinsert/erase must also holdg_fault_lockexclusively (FaultLockExclusive), because the fault handler readsg_mappedunder the shared lock withoutg_phys_mutex. Whichever lands second adds that one line. The same applies to the claimed shimVirtualFreeon xmem reservations (#7694 (comment)).What and why
XMemAllocatePhysicalPages(from #6919) backed each call with one pagefile-backedSEC_COMMITsection, so the whole request was charged at once. Right after boot stop 17 the title asks for 99,776 pages (about 6.1 GiB) in one call; on a 16 GB machine with a fixed page file that fails withERROR_COMMITMENT_LIMIT(1450) orERROR_COMMITMENT_MINIMUM(1455) and the boot stops a little later. The title then maps nearly all of those pages in one go, so committing at map time charges almost the same amount a few seconds later.Now the commit follows first access:
SEC_RESERVE. Each section also keeps one reserve-onlyFILE_MAP_WRITEview of itself (address space, no commit), through which pages are committed, so the title's view protection never matters and a committed page is visible through every view of it.g_mappedrecords, for each mapped 64 KiB address, the page, its section and the offset, under an SRW lock (exclusive for map/unmap, shared on the fault path; unmap removes the entry before unmapping and restores it if the unmap fails).XMemAllocatePhysicalPages, takes an access violation only when its address is in a live mapping and the view's page is stillMEM_RESERVE: it commits that page through the section's own view and resumes. A fault on a committed page resumes only when the view's protection now permits the access (two threads faulting the same page); every other fault (read-only orPAGE_NOACCESSviews, runtime: the CPU half of Xbox physical pages, with write-fault dirty tracking #5381-style write tracking, addresses outside the mapped pages) goes to the next handler, and a failed commit is not swallowed, so a real out-of-memory still faults where it happened. It is first in the chain so that--trace's logging handler does not record every first touch.rt::xmem::commit_physical_range(address, size)(newruntime/shims/xmem.h) commits a range up front and ignores memory that is not a mapped physical page. It is for shims that hand guest memory to the kernel or a driver, where an uncommitted page returnsERROR_NOACCESSinstead of faulting. Onmainno shim does kernel I/O into guest memory; d3d12_xs: device slot 133 (CreatePlacedResourceX) makes a buffer on the title's own pages (VK_EXT_external_memory_host) #7218 (host-memory import, which requiresMEM_COMMIT) and a DirectStorage shim (runtime: console DirectStorage (DStorageGetFactory) with file-to-memory reads #4348) should call it before using title pages. A titleReadFilestraight into a physical page it has never touched is the remaining gap; I'll follow up with a pre-commit in aReadFileshim if a trace shows the title doing that.Unchanged: the 64 KiB page model, free/unmap semantics,
view_protect,XMemVirtualAlloc. The doc comment ofXMemAllocatePhysicalPagesnow says "charged on touch" instead of "at once".Tested
Windows 11, NVIDIA GeForce RTX 4070 (driver 617.42), Ryzen 7 7800X3D, 31 GB RAM with a system-managed page file, MSVC 14.43 RelWithDebInfo.
test_physical_pages_commit_on_touchinxmem_test: allocate (free system commit + 1 GiB) of pages, map 1,024 of them and write 512, checking systemCommitTotal(a pagefile-backed section is not charged toPrivateUsage): allocating and mapping charge nothing, writing charges about the 512 pages, values read back and an untouched page reads zero; two pages mapped into a second window share data both ways; a write to a committed read-only page and a fault outside the mapped pages still reach the next handler;commit_physical_rangecommits an untouched page soReadFileinto it succeeds, and skips memory it doesn't own.xmem_test: 191 checks OK (201 with the second commit).ctest -R "xmem|loader": 5/5 (loader,pe_loader,pe_loader_bounds,loader_tls,xmem). After rebasing on37398f98d:xmempasses again.main'sxmem.cpp: 2 failures, "allocating those pages does not charge them" and "writing to 512 of them charges about those 512 pages". On this machine the oversizedSEC_COMMITitself succeeded because Windows grew the system-managed page file; with a fixed page file it fails as in the finding.LEONIDA_D3D12=vulkan, on a local integration tree (main-era boot stand-ins, not proposed here) with thisxmem.cppdropped in versusmain's: both reach the same stop with 2,246 D3D12 calls. Peak system commit during the run: +12.39 GiB withmain'sxmem.cpp, +5.45 GiB with this PR. Time to that stop: 3.5 to 3.7 s with this PR, 3.8 s withmain, alternating runs.Expect small textual merges with the open
xmem.cppPRs nearmap_page(#7499 cache modifiers, #7331 rollback of failed batches); they don't change what this PR does.AI-assisted: yes
Checklist
docs/design.md, with how it was confirmed. (n/a: no translator change)