Skip to content

Lazy physical pages: a thread that loses the race to commit a page retries - #244

Merged
ivall merged 1 commit into
ivall:mainfrom
Cattn:fix-lazy-page-race
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
Cattn:fix-lazy-page-race

Conversation

@Cattn

@Cattn Cattn commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem

kernelx::lazy (38b05b7) commits a lazy physical page from a vectored handler on first touch. When two threads fault on the same lazy page at the same time:

  1. both fault;
  2. one takes the write lock, removes the page from LAZY and commits it;
  3. the other then finds the page gone (contains is false, or commit's remove returns false) and returns EXCEPTION_CONTINUE_SEARCH.

So the second access ends the process as an unhandled access violation, although the page is committed by then.

Seen with a retail title on main while it loads its world: two threads crash within 1 ms at the same instruction, writing two addresses in one 64 KiB page:

unhandled exception 0xc0000005 in <title>+0x13936 (write at address 0x1f3ef412da0) ...
unhandled exception 0xc0000005 in <title>+0x13936 (write at address 0x1f3ef4119f0) ...

Change

lazy.rs only. The handler also lets the faulting instruction run again when the faulting address is now committed and its protection allows the access: reads and writes only, not execute or guard pages. The retry then succeeds. A fault whose access is still not allowed goes on to the next handler as before.

Testing

  • New test threads_first_touching_one_lazy_page_together_all_go_on: 8 threads first-touch each of 256 lazy pages at the same moment through a barrier, and every write is checked afterwards.
    • Without the change, the test process ends with 0xc0000005 (3 of 3 runs).
    • With it, the test passes (3 of 3 runs).
  • cargo test --release -p xcl --lib kernelx::: 19 passed.
  • RTX 4070, Ryzen 7 7800X3D, Windows 11: with the change, the title no longer stops at this point; one run went 170 s through world loading, ended by XCL_RUN_SECONDS.

Not related to #236, whose race is between upload scanning and decommit in sparse.rs.

…tries

Two threads that first touch the same lazy page together both fault. The
first commits the page and removes it from the lazy set; the second then
finds it gone and returned EXCEPTION_CONTINUE_SEARCH, so its access ended
the process as an unhandled access violation although the page was
committed by then.

The handler now lets the faulting instruction run again when the page is
committed and its protection allows the access (read or write; not execute
or guard pages). A test has 8 threads first-touch 256 lazy pages at the
same moment: before, the test process ends with 0xc0000005 (3 of 3 runs);
after, it passes (3 of 3).
@gw1337x

gw1337x commented Nov 23, 2026

Copy link
Copy Markdown

Confirming on GTA VI (retail), RX 7800 XT, main d4f17ca: without this change the title crashed in every run once it loaded its world (3 of 3, at 70–150 s) – two or three threads at +0x13936 / +0x1398b / +0x2926872, writing into the same 64 KiB page in the same millisecond. With an equivalent retry applied it passed that point every time (3 of 3) and ran until the next, unrelated wall (video memory, ~110 s).

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I compared this with #250, which fixes the same race. Both fix it. They differ in one case, a genuine fault, and this PR handles that case correctly.

Findings

Tested

  • Windows 11, MSVC, debug, CPU only (no device). Synthetic pages only.
  • main 63b9930; this PR merged onto it, with no conflicts.
  • Every test ran under a debugger that passes first-chance exceptions on, which lazy.rs relies on, and catches the second-chance one.
  • The tests above are local only: not pushed, and not proposed for this PR.

Not tested

  • A title.
  • Wine.
  • Pages a title maps read-only (commit makes every lazy page PAGE_READWRITE, before and after this PR).

🤖 Claude Opus 5.5

@mariuslozo-boop

Copy link
Copy Markdown

Test report on NVIDIA with this PR's head, on top of current main.

Setup: RTX 5070 Ti (16 GB), driver 616.92, Ryzen 7 7700X / 32 GB RAM, Windows 11 26300, release builds. Retail GTA VI 1.0.2.1 with XCL_NETWORK=0. One run each.

build result
main 60565c4 crash at 136.9 s: two threads, +0x13936 (write at 0xc434223b0) and +0x1398b (write at 0xc43420000), same millisecond, same 64 KiB page
main 7a033c8 crash at 175.6 s, same kind: two threads, +0x35a4999 (write at 0xc43450000) and +0x35a47bc (write at 0xc43450010), one 64 KiB page
main 7a033c8 + this PR (77905f6, merged locally, no conflicts) no access violation; the title ran on to 184.4 s and then stopped at a different wall (below)
  • In both crash dumps, the faulting page is committed and read-write when the dump was written, shortly after the fault. That fits the losing thread finding the page already committed.
  • This looks like the same kind of wall gw1337x reported (out of memory; they saw it at ~110 s on AMD). From 181.8 s, 346 CreatePlacedResourceX … a resource of its own (0x8007000e) lines (354 0x8007000e lines counting 8 view failures), 2D textures from 4×4 to 1024×1024, most of them 512×512. The title then hits a breakpoint (0x80000003 at +0x193e00f, with rax=0x8007000e). That looks unrelated to this PR.

Not tested: AMD, the PR's own test (cargo test), more than one run per build.

AI-assisted: Claude Code (Claude Opus 5.5) ran the builds and the game and wrote this report.

@ivall
ivall merged commit 276cdd5 into ivall:main Nov 23, 2026
1 check passed
forbesfields pushed a commit to forbesfields/xcl that referenced this pull request Nov 23, 2026
Handle 16-byte constant-buffer records, upload intro textures from their CPU view, and decode BC1 BCPack streams. Share bounded texture pages, batch upload staging, and tolerate pages decommitted during streaming reads.

Includes work from ivall/xcl ivall#208, ivall#244, ivall#252, ivall#254, ivall#269 and ivall#283. DirectStorage late destination pages are already in the base.

Assisted-by: OpenAI Codex (gpt-6.2-sol)
theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
The fork, rebuilt on current upstream main with only what upstream does not have: the launcher's folder picker, click-to-capture mouse handling with F8 and Page Up / Page Down, a heap per DXC compile thread, the limit on waits for pipelines still being built, the installer, build.ps1, BUILDING.md, the release workflow and the README. Everything else the fork carried is in upstream now (ivall#195 in parts, ivall#211, ivall#244, ivall#252, ivall#283, ivall#289) or was a diagnostic.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants