Skip to content

Initialize dirty tracking before concurrent pool registration - #2225

Closed
akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/dirty-init-20261110
Closed

akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/dirty-init-20261110

Conversation

@akratch

@akratch akratch commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What and why

Concurrent first memory-pool registrations could observe an early initialization flag before dirty-tracker page geometry and both write hooks existed. The controlled reproduction leaves the second pool untracked; simultaneous first calls also race on the plain flag.

Publish initialization with pthread_once before registering any range. The signal handler does not enter the once. Page protection, generation stamps, pool lifetime and shader behavior are unchanged. This was found in an OOT source review and reproduced synthetically; it is not attributed to the reported terrain or lighting artifacts.

See Concurrent first memory pools see incomplete dirty tracking.

Checked

  • Fresh-process CPU fixture: original code fails 6 of 22 controlled checks; fixed code passes 22 controlled and 75 eight-thread checks, both normally and under ThreadSanitizer. The original concurrent control reports a race on inited. The standalone checks below ran on 2d35c083dd95b3c42f3b7f2fc6c98fddd8f4b389 against exact base 4c574a6f055217ac0b6d5fbeccd1eb22b86dbe90; the negative control replaced only nvn_dirty.c with that base version and forced a rebuild. The integrated version e6fabaa695fcacb2ccfb12076bdb3acffa0e33b4 independently passed the same checks plus ten fresh controlled runs and ten TSan concurrent runs; those repeats are separate evidence.
  • The fixture pauses the real getpagesize initialization seam and observes pthread_once entry without replacing its behavior. It does not reset tracker state. Actual protected writes and host pre-writes exercise the production crash hooks.
  • Existing dirty-race, dirty-slots and dirty-retirement fixtures pass. Host memory-dirty passes shared-page recommit, copyback and concurrent-protection checks.
  • A full Mac host build passes (unchanged libvpx/dav1d build archives reused; current host sources compiled). Six docs tests pass. Focused LLVM 23.1.5 analysis of production and fixture reports zero findings; the existing baseline is unchanged.
  • Apple silicon, macOS 27.2 (26B123), Xcode 27.2. No GPU, gameplay or iOS run. Scope is first-use CPU synchronization; the deterministic seam and sanitizer controls establish this defect independently of a game schedule. No frame-rate improvement is claimed.
  • regress.py impact: Breath of the Wild, Pokémon Scarlet and Tears of the Kingdom use the affected pool entry points; 153 profiles lack usage manifests. Game regression runs remain pending.

Docs

  • Generic finding, OOT Found-on link and regenerated symptom index.
  • No game files, keys or extracted game data in the change or fixtures.

Concurrent first registrations could observe the early inited flag before page geometry and hooks were ready. Publish initialization with pthread_once and cover partial initialization, actual fault/pre-write tracking, and simultaneous first calls with a fresh-process CPU/TSan fixture.

Co-authored-by: Adam Kratch <70729875+akratch@users.noreply.github.com>
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation ci Continuous integration and repository settings (.github) game: 0400486024DEC000 A game labels Nov 10, 2026
seedds added a commit that referenced this pull request Nov 14, 2026
…2225)

The page size, the timebase and both hooks (page fault, host pre-write) were set by the first nvn_dirty_add_range behind a plain flag, published before the geometry and the hooks and with no lock. A pool registered on another thread at that moment could get zero geometry or return before the hooks existed, and a host write into guest memory on another thread (hle_fs.c reading a file) read crash.c's hook table while that registration grew it, unsynchronized. They are now set by a load-time constructor, on one thread before main, so every registration, fault and host pre-write sees them complete; a game with no memory pool runs hooks that scan an empty table.

t_dirty_init (new, with tsan-dirty-init, both in CI): eight threads register their first pools at once while another keeps doing host pre-writes; each pool must be tracked and see a direct write (fault hook) and a host write (pre-write hook). This code: 20/20 runs, 10/10 under ThreadSanitizer. Main's nvn_dirty.c under ThreadSanitizer: 5/5 runs report the race on inited, one earlier run the hook-table race at crash.c:139; without it 20/20 pass (the window is too narrow to hit unforced). t_dirty_slots, t_dirty_race, t_dirty_retire_race pass. Not run: a game, a full host build, iOS.

Open: TSan also reports a race between reusing a pool slot (nvn_dirty_add_range writes the plain base/end) and a fault or pre-write scan that read the slot as live before it was retired; that is the lock-free table's design and is not changed here.

The diagnosis is PR #2225's (the early flag, with a pthread_once fix and a fixture); the load-time setup, the hook-table race and this test are new here.

Co-authored-by: akratch <70729875+akratch@users.noreply.github.com>
@seedds

seedds commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thank you — the diagnosis is right, and it landed on main as de2018e, with you credited as co-author.

It landed in a different form. The first pool registration was also where both hooks were added to crash.c's hook table, which dm_guest_write_prepare reads without a lock from any thread (hle_fs.c reading a file into guest memory, for example). pthread_once orders the callers of nvn_dirty_add_range, but not those readers. So the setup (page size, timebase, both hooks) now runs in a load-time constructor, on one thread before main, and there is no lazy path left.

t_dirty_init is a shorter check: eight threads register their first pools while another keeps doing host pre-writes, plain and under TSan (both in CI). Main's code fails it under TSan in 5 of 5 runs, on inited and in one run on crash.c:139. The new code passes 10 of 10. The fixture that pauses at getpagesize and the docs entry were left out: the flag they test is gone, and no game has shown the symptom.

One more finding from this: TSan also reports a race when a pool slot is reused while a fault or pre-write scan is still reading it. That is part of the lock-free table's design and is listed as open in the commit message.

@seedds seedds closed this Nov 14, 2026
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…y pools are published (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…earlier case is in flight

cut_boundary_case expects a commit "with nothing running", which the queue reads from g_seg_inflight. An earlier case's mtl_wait can
return on the GPU's own signal of its fence (the early waiter, post_complete) before that command buffer's feedback handler has counted it
out of g_seg_inflight; the handler comes about 0.3 ms after the GPU's end. The case then saw work still running and failed: on mkw-fin and
every branch after it, from the first commit git bisect named, 2d35c08 (pull request seedds#2225: dirty tracking set up once before memory pools
are published), which moved that timing; the base 352e4a6 passes. The host's count is right, the case read it too early.

The case now waits for the count to reach 0 (up to 5 s) and checks that it did. t_feedback: 279 checks, 0 failed, in three runs of three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…y pools are published (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…earlier case is in flight

cut_boundary_case expects a commit "with nothing running", which the queue reads from g_seg_inflight. An earlier case's mtl_wait can
return on the GPU's own signal of its fence (the early waiter, post_complete) before that command buffer's feedback handler has counted it
out of g_seg_inflight; the handler comes about 0.3 ms after the GPU's end. The case then saw work still running and failed: on mkw-fin and
every branch after it, from the first commit git bisect named, b3e07bb (pull request seedds#2225: dirty tracking set up once before memory pools
are published), which moved that timing; the base 352e4a6 passes. The host's count is right, the case read it too early.

The case now waits for the count to reach 0 (up to 5 s) and checks that it did. t_feedback: 279 checks, 0 failed, in three runs of three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…y pools are published (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…earlier case is in flight

cut_boundary_case expects a commit "with nothing running", which the queue reads from g_seg_inflight. An earlier case's mtl_wait can
return on the GPU's own signal of its fence (the early waiter, post_complete) before that command buffer's feedback handler has counted it
out of g_seg_inflight; the handler comes about 0.3 ms after the GPU's end. The case then saw work still running and failed: on mkw-fin and
every branch after it, from the first commit git bisect named, b3e07bb (pull request seedds#2225: dirty tracking set up once before memory pools
are published), which moved that timing; the base 352e4a6 passes. The host's count is right, the case read it too early.

The case now waits for the count to reach 0 (up to 5 s) and checks that it did. t_feedback: 279 checks, 0 failed, in three runs of three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continuous integration and repository settings (.github) docs Documentation game: 0400486024DEC000 A game nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants