Repository navigation
file_system: the title's ReadFile commits first-touch physical pages before the kernel writes them - #9166
file_system: the title's ReadFile commits first-touch physical pages before the kernel writes them#9166Etr0p wants to merge 7 commits into
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.
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.
…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.
…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.
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.
…before the kernel writes them ItsNotRexed#7556 commits an xmem physical page on the title's first touch, from a vectored handler. A kernel write raises no user-mode fault, so the title's ReadFile into a physical page not committed yet failed with ERROR_NOACCESS: on a retail boot, 52,566 of 53,248 ReadFile calls in 120 s, every destination a MEM_RESERVE mapped view. ReadFile, ReadFileEx and ReadFileScatter now go through file shims that call rt::kernel_write::before on the destination first. That hook (kernel_write_hook.h, header-only, since file_system.cpp is built into executables without xmem.cpp) is set by xmem when it installs its first-touch handler, to commit_physical_range. xmem_test now links file_system.cpp and checks, on five mapped physical pages nothing touched: the host's ReadFile fails with ERROR_NOACCESS (control); through the shims, a synchronous read, a read through the file-l1-1-0 entry that straddles two pages, an overlapped read on an asynchronous handle and a ReadFileEx all succeed with the file's bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Not exercised here: |
|
Verified head
AI-assisted. |
Grimkaaah
left a comment
There was a problem hiding this comment.
Independent Linux CPU review
What changed: file_system shims ReadFile/ReadFileEx/ReadFileScatter to call kernel_write::before so xmem can commit physical pages before the kernel writes the destination.
Checked: diff + new kernel_write_hook.h; xmem_test now links file_system.cpp (Windows). Logic matches the ERROR_NOACCESS first-touch failure mode described.
No game files. Portable CPU execution of the Windows shim not available on this Linux host.
|
Tested 44b7276 merged with main 867d922 under Wine:
So the control is a Windows-only expectation. Under Wine it could be reported as a skip (e.g. with On: Linux, Ubuntu 24.04, cross-built with clang-cl 19.1.1 + xwin SDK (RelWithDebInfo), run through Wine 9.0; CPU only, no game files. AI-assisted (Claude Code). |
Wine's ReadFile writes the buffer from user mode, so the first-touch handler runs and the read succeeds: the kernel write the control guards against doesn't happen there. The control is reported as a skip under Wine (same wine_get_version check as the other tests) and still checked on Windows; every read through the shims stays active. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@GeremC thanks for running it under Wine. Pushed Tested on native Windows 11 (26200, MSVC 14.44, RelWithDebInfo): AI-assisted (Claude Code). |
|
checked: #916688be3d0 | base: PR head (includes #7556e85ebec) | on: native Windows 11 26300 x64, MSVC 19.44, SDK 10.0.26100, RelWithDebInfo | result: stock The extra check opens an 8 KiB synthetic file with The stock test checks registration of ReadFileScatter. This exercises the registered call with an actual kernel write. Consider adding this case to the existing fixture. Wine and retail boot were not part of this check. Synthetic inputs only. AI-assisted (Codex). agent: yes |
|
AI-assisted (Claude Code). A retail measurement for the "not tested: DirectStorage's own reads" line above. Local test copy:
So the guess is right: Not covered: Wine, clang-cl, zlib requests (they go through |
|
Tested 88be3d0 merged with main 5d74c27 under Wine 9.0 (Ubuntu 24.04, clang-cl 19.1.1 + xwin cross-build, CPU only, no game files): the Wine path works.
AI-assisted (Claude Code). |
Claimed in the coordination issue: #8868 (comment). The measurement it comes from: #7556 (comment)
Depends on: #7556. This branch is #7556's head
e85ebec7dplus one commit,44b7276f4. @Cattn, if you'd rather fold this into #7556, say so and I'll close this one.What and why
#7556 commits an xmem physical page on the title's first touch: the touch faults, and a vectored handler commits the page. A write by the kernel raises no user-mode fault. So when the title
ReadFiles into a physical page it hasn't touched yet, the read fails withERROR_NOACCESSand the handler never runs. On my retail boot, almost everyReadFileof the title failed this way, and every failing destination was a mapped view still inMEM_RESERVE.With this change,
ReadFile,ReadFileExandReadFileScatter(kernel32, kernelbase,api-ms-win-core-file-l1-1-0) go through file shims. Each shim prepares the destination first, then calls the host function:rt::kernel_write::before(destination, size). xmem sets that hook tocommit_physical_rangewhen it installs its first-touch handler. Memory xmem doesn't own is left alone, ascommit_physical_rangealready does.runtime/shims/kernel_write_hook.h) becausefile_system.cppis built into several executables that don't linkxmem.cpp.Note for #9045 (slot-133 shadows): it adds
ReadFileshims to the same table for another reason (opening pages it write-protects). Whichever lands second calls both helpers from one shim. That merge is mechanical.Tested
Windows 11 26200, MSVC 14.44, Ninja RelWithDebInfo. CPU only, synthetic data.
xmem_test: OK, 220 checks. It now linksfile_system.cppand resolves the shims through the registry, as the title's imports do. On five mapped physical pages nothing has touched:ReadFilefails withERROR_NOACCESS, and the page stays uncommitted.api-ms-win-core-file-l1-1-0entry that straddles two untouched pages. An overlapped read on an asynchronous handle. AReadFileEx.file_system_test: 446 checks, 0 failures.Retail measurement (counts only)
RTX 5070 Ti Laptop, NVIDIA 621.24, my own copy of Xbox 1.0.2.1. I used a local boot stack that includes #7556 (and #9045), with and without this change. 120 s per run, and the
ReadFilefailures were counted by a local pass-through shim.ReadFilecalls in 120 sReadFilefailedERROR_NOACCESS, on uncommitted physical pages)ERROR_INVALID_PARAMETER, not a physical page; present before too)Not tested: Wine, clang-cl, and DirectStorage's own reads. #7180's
dstorage.cppReadFiles straight into a request's destination, so it should callrt::kernel_write::beforetoo.AI-assisted: yes (Claude Code).
🤖 Generated with Claude Code