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

Manually merged
github merged 1 commit from gh-pr/244/head into gh-pr/244/base 2026-11-26 04:17:20 +00:00
Owner

@⁠Cattn on GitHub · 2026-11-23 21:46 UTC · original
Cattn:fix-lazy-page-race → main · +52 −2 · 1 files · 1 commits · merged 2026-11-23 23:16 UTC

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.

**@⁠Cattn** on GitHub · 2026-11-23 21:46 UTC · [original](https://github.com/xcl-dev/xcl/pull/244) `Cattn:fix-lazy-page-race` → `main` · +52 −2 · 1 files · 1 commits · merged 2026-11-23 23:16 UTC ## 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`.
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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:17:18 +00:00
github manually merged commit 77905f6b50 into gh-pr/244/base 2026-11-26 04:17:20 +00:00
Author
Owner

@⁠gw1337x on GitHub · 2026-11-23 21:49 UTC · original

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).

**@⁠gw1337x** on GitHub · 2026-11-23 21:49 UTC · [original](https://github.com/xcl-dev/xcl/pull/244#issuecomment-7114970919) 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).
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 22:02 UTC · original

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

  • The race. Every race test aborts main (63b9930) with an unhandled access violation, 10 of 10 runs each:

    • a two-thread test I wrote: a barrier per page, one thread writes, the other reads, 512 pages;
    • this PR's threads_first_touching_one_lazy_page_together_all_go_on;
    • #250's threads_racing_on_a_lazy_page_all_go_on.

    Merged onto main, this PR (77905f6) passes all three, 10 of 10 runs each. So does #250.

  • The lost race, made deterministic. The test calls handler with an access-violation record for a page another thread has just committed. Main answers CONTINUE_SEARCH for both the write and the read: the crash. This PR answers CONTINUE_EXECUTION for both.

  • Where the two differ: a genuine fault inside a committed lazy page. Take a lazy page that was committed and then had 4 KiB of it made read-only. A write there faults again on every retry.

    • This PR checks the protection at the faulting address against the access, so the write still crashes. A real write in a test takes the access violation.
    • #250 checks only that the 64 KiB page's base is PAGE_READWRITE, so it retries the write forever. The same real write hangs until killed.
  • Both refuse execute accesses and pages that are only reserved.

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

**@⁠Kalmarv** on GitHub · 2026-11-23 22:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/244#issuecomment-7115239207) 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** - **The race.** Every race test aborts main (`63b9930`) with an unhandled access violation, 10 of 10 runs each: - a two-thread test I wrote: a barrier per page, one thread writes, the other reads, 512 pages; - this PR's `threads_first_touching_one_lazy_page_together_all_go_on`; - #250's `threads_racing_on_a_lazy_page_all_go_on`. Merged onto main, this PR (`77905f6`) passes all three, 10 of 10 runs each. So does #250. - **The lost race, made deterministic.** The test calls `handler` with an access-violation record for a page another thread has just committed. Main answers `CONTINUE_SEARCH` for both the write and the read: the crash. This PR answers `CONTINUE_EXECUTION` for both. - **Where the two differ: a genuine fault inside a committed lazy page.** Take a lazy page that was committed and then had 4 KiB of it made read-only. A write there faults again on every retry. - This PR checks the protection at the faulting address against the access, so the write still crashes. A real write in a test takes the access violation. - #250 checks only that the 64 KiB page's base is `PAGE_READWRITE`, so it retries the write forever. The same real write hangs until killed. - Both refuse execute accesses and pages that are only reserved. **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
Author
Owner

@⁠mariuslozo-boop on GitHub · 2026-11-23 22:03 UTC · original

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.

**@⁠mariuslozo-boop** on GitHub · 2026-11-23 22:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/244#issuecomment-7115258369) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.