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

Merged
Cattn merged 1 commit from upstream-pr/244 into main 2026-11-23 23:16:51 +00:00
Cattn commented 2026-11-23 21:46:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Cattn on 2026-11-23T21:46:30Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6569478821 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/244). Originally posted by [Cattn](https://github.com/Cattn) on 2026-11-23T21:46:30Z. --- ## 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`.
upstream-sync closed this pull request 2026-11-24 06:57:49 +00:00
gw1337x commented 2026-11-24 07:04:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gw1337x on 2026-11-23T21:49:43Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7114970919 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/244#issuecomment-7114970919). Originally posted by [gw1337x](https://github.com/gw1337x) on 2026-11-23T21:49:43Z. --- 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 commented 2026-11-24 07:04:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T22:02:05Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7115239207 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/244#issuecomment-7115239207). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T22:02:05Z. --- 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
mariuslozo-boop commented 2026-11-24 07:04:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by mariuslozo-boop on 2026-11-23T22:03:00Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7115258369 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/244#issuecomment-7115258369). Originally posted by [mariuslozo-boop](https://github.com/mariuslozo-boop) on 2026-11-23T22:03:00Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!241
No description provided.