kernel: retry a fault on memory our own map says is committed #534

Closed
github-bot wants to merge 1 commit from gh-pr/534/head into gh-pr/534/base
Owner

@⁠CheesyPoofs346 on GitHub · 2026-09-09 07:29 UTC · original
CheesyPoofs346:kernel-transient-map-fault-retry → main · +38 −0 · 4 files · 1 commits · closed without merging

Problem. Remapping guest direct memory is not atomic. MapBacking and UnmapBacking go through Windows placeholders, so the pages are briefly absent, and a guest thread touching them in that window takes an access violation on memory that is committed both before and after the remap. This was observed directly: two VirtualQuery calls one line apart disagreed about the same range, reporting MEM_RESERVE and then MEM_COMMIT. The exception handler treated it as a fatal access violation and aborted.

Change. When the fault address is reported as committed by this emulator's own guest map, retry the faulting instruction instead of aborting. The retry is bounded to 4096 attempts per address, so an address that is genuinely bad still fails rather than spinning, and it sits behind --no-retry-transient-map-faults for anyone who would rather see the abort.

Effect. GTA V previously crashed reproducibly while streaming during the first mission; with this it completed that mission with zero faults. No performance change; performance was not measured.

Limitations, and why this is a draft.

  • This mitigates the race, it does not remove it. The remap is still non-atomic; the fix converts a crash into a retry. A reviewer may reasonably prefer making MapBacking/UnmapBacking atomic, or serialising guest access against the remap, and treat this as a stopgap.
  • A retry on a committed-looking address can in principle mask a genuine access violation whose page happens to be committed. The per-address bound limits how long that can go unnoticed but does not rule it out.
  • Verified on Windows only. The placeholder mechanism this works around is Windows-specific, but the retry path is not conditionally compiled.
  • No automated regression: reproducing it needs the specific streaming workload that triggered it, and the window is timing-dependent.

Verification. Builds clean. Exercised by the GTA V run described above; the diagnosis rests on the contradictory VirtualQuery pair, which is direct evidence of the window rather than an inference from the crash alone.

References

Microsoft Windows API, VirtualAlloc2 / MapViewOfFile3 placeholder semantics: splitting or replacing a placeholder mapping unmaps the region before the new view is mapped, leaving the address range temporarily inaccessible to other threads.

**@⁠CheesyPoofs346** on GitHub · 2026-09-09 07:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/534) `CheesyPoofs346:kernel-transient-map-fault-retry` → `main` · +38 −0 · 4 files · 1 commits · closed without merging **Problem.** Remapping guest direct memory is not atomic. `MapBacking` and `UnmapBacking` go through Windows placeholders, so the pages are briefly absent, and a guest thread touching them in that window takes an access violation on memory that is committed both before and after the remap. This was observed directly: two `VirtualQuery` calls one line apart disagreed about the same range, reporting `MEM_RESERVE` and then `MEM_COMMIT`. The exception handler treated it as a fatal access violation and aborted. **Change.** When the fault address is reported as committed by this emulator's own guest map, retry the faulting instruction instead of aborting. The retry is bounded to 4096 attempts per address, so an address that is genuinely bad still fails rather than spinning, and it sits behind `--no-retry-transient-map-faults` for anyone who would rather see the abort. **Effect.** GTA V previously crashed reproducibly while streaming during the first mission; with this it completed that mission with zero faults. No performance change; performance was not measured. **Limitations, and why this is a draft.** - **This mitigates the race, it does not remove it.** The remap is still non-atomic; the fix converts a crash into a retry. A reviewer may reasonably prefer making `MapBacking`/`UnmapBacking` atomic, or serialising guest access against the remap, and treat this as a stopgap. - A retry on a committed-looking address can in principle mask a genuine access violation whose page happens to be committed. The per-address bound limits how long that can go unnoticed but does not rule it out. - Verified on Windows only. The placeholder mechanism this works around is Windows-specific, but the retry path is not conditionally compiled. - No automated regression: reproducing it needs the specific streaming workload that triggered it, and the window is timing-dependent. **Verification.** Builds clean. Exercised by the GTA V run described above; the diagnosis rests on the contradictory `VirtualQuery` pair, which is direct evidence of the window rather than an inference from the crash alone. **References** Microsoft Windows API, `VirtualAlloc2` / `MapViewOfFile3` placeholder semantics: splitting or replacing a placeholder mapping unmaps the region before the new view is mapped, leaving the address range temporarily inaccessible to other threads.
Remapping guest direct memory is not atomic: MapBacking and UnmapBacking go
through Windows placeholders, so the pages are briefly absent. A guest thread
touching them in that window faults on memory that is committed both before and
after - observed directly as two VirtualQuery calls one line apart disagreeing
about the same range, MEM_RESERVE then MEM_COMMIT.

Retry the faulting instruction when this emulator's own guest map reports the
address as committed, bounded to 4096 attempts per address so a genuinely bad
address still fails loudly. Behind --no-retry-transient-map-faults for anyone
who would rather see the abort.

This mitigates the race rather than removing it; the remap remains non-atomic.

Co-Authored-By: Claude Opus 5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:24:40 +00:00
github-bot closed this pull request 2026-11-26 09:24:40 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.