Repository navigation
Serialize dirty-page protection with NVN pool retirement - #686
Closed
widmonstertony wants to merge 1 commit into
Closed
widmonstertony wants to merge 1 commit into
widmonstertony wants to merge 1 commit into
Conversation
Keep the range descriptor live until the final mprotect so retirement cannot restore writable pages and unpublish the slot before protection starts. The signal handler and host pre-write path remain lock-free with respect to the slot mutex. Extract the shared tracker fix from fork commit 587d520. A staged lookup/retirement regression fails with EFAULT on current main and succeeds with the fix; include it in the CPU-only CI list.
seedds
added a commit
that referenced
this pull request
Oct 30, 2026
…st mprotect (PR #686) nvn_dirty_protect looked up a live range without the slot mutex. Pool retirement could finish between that lookup and the first page-state change: it restored the protected pages to writable and marked the range dead, then protect set the retired storage read-only with no live range left to release it (a host read() into it failed with EFAULT; a game write faulted with no owner). In the same window a new pool could reuse the slot, and protect computed its pages from the new pool's base and state array. Protect now holds g_slot_mu, which pool creation and retirement already hold, through its last mprotect. The page-fault and host pre-write paths still take no lock, so a BUSY page they hold finishes while protect waits. t_dirty_retire_race stages the retirement between protect's lookup and its first page-state exchange by wrapping atomic_compare_exchange_strong, as t_dirty_race wraps atomic_store; it fails with EFAULT without the mutex and passes with it, also under ASan/UBSan and TSan. It joins the CPU-only CI list. Co-authored-by: Xinpei Tan <[email protected]>
Owner
|
Landed on main as af616f5, with you as co-author. Thanks for the clear diagnosis and the staged reproducer. Two changes on landing: the test now sets up the race by redefining |
Know-It-All-Brother
pushed a commit
to Know-It-All-Brother/nxnative
that referenced
this pull request
Nov 9, 2026
…st mprotect (PR seedds#686) nvn_dirty_protect looked up a live range without the slot mutex. Pool retirement could finish between that lookup and the first page-state change: it restored the protected pages to writable and marked the range dead, then protect set the retired storage read-only with no live range left to release it (a host read() into it failed with EFAULT; a game write faulted with no owner). In the same window a new pool could reuse the slot, and protect computed its pages from the new pool's base and state array. Protect now holds g_slot_mu, which pool creation and retirement already hold, through its last mprotect. The page-fault and host pre-write paths still take no lock, so a BUSY page they hold finishes while protect waits. t_dirty_retire_race stages the retirement between protect's lookup and its first page-state exchange by wrapping atomic_compare_exchange_strong, as t_dirty_race wraps atomic_store; it fails with EFAULT without the mutex and passes with it, also under ASan/UBSan and TSan. It joins the CPU-only CI list. Co-authored-by: Xinpei Tan <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Pool retirement can finish between
nvn_dirty_protectfinding a live range and changing its first page to BUSY. Retirement restores write access and unpublishes the range; protect then makes the retired storage read-only. A subsequent hostread()into that storage fails withEFAULT, and the tracker no longer owns the page.Hold the existing pool-slot mutex from protect's lookup through its final
mprotect. Pool creation and retirement already use that mutex. The fault handler and host pre-write path remain independent of that mutex, so they can complete a BUSY transition while protection waits.This extracts the shared tracker fix from fork commit 587d5206, discussed in #310. It is independent of the older renderer, deployment baseline, tvOS and game-specific changes. This is a CPU-side pool-lifetime race present on current
main; it does not assert that an additional Metal 4 encoder race or any particular game's black screen is fixed.Checked
85578fc9from currentmain.read()returns -1 witherrno=EFAULT.t_dirty_raceandt_dirty_slotspass. The new regression also passes ASan/UBSan and ThreadSanitizer. It is included in the CPU-only CI list and the normal graphics run list.make -C dm/gfx/tests GAME=010015100B514000 -j2 runandmake -C dm/host/tests GAME=010015100B514000 -j2 runpass. These were run on710559cf; the final base increment85578fc9changes NSP extraction/tools/docs only, with no host, graphics or iOS source changes.710559cf: 608 discovered, OK with 17 skipped. On the final85578fc9base with the existing .NET runtime on PATH: 613 discovered, 596 passed, 16 skipped and 1 environment failure:test_extractor_streams_populated_and_empty_filesystemscannot rundotnet runbecause this machine has no .NET SDK. The same test fails identically on pristine85578fc9without this PR (exit 155, “No .NET SDKs were found”). The new NSP scan tests pass 7/7, macOS bundle tests pass 44/44. This is not an all-green local tools result; .NET SDK 10 CI validation remains open. Python lint ratchet: 392 existing findings, 0 new findings.Gameplay validation remains open
This is a draft for focused review. No game was automatically launched: the device owner explicitly handles gameplay testing. The deterministic reproducer and synthetic GPU suite establish the tracker invariant, not a new five-game gameplay result or an iPhone runtime result.
regress.py impact upstream/main..HEADreports 3 affected profiles with usage manifests, 0 broad-only, 1 unaffected and 110 unknown. The affected manifests are Breath of the Wild (01007EF00011E000), Pokémon Scarlet (0100A3D008C5C000) and Tears of the Kingdom (0100F2C0115B6000); all use the NVN pool/texture submission path. Fresh gameplay checks of those titles, and NFS: Hot Pursuit Remastered plus Animal Crossing to cover pool churn and CPU texture uploads, remain unrun. Reviewers' current-device tests would be useful before marking ready. Protection calls now serialize across pools through the existing global slot mutex; gameplay throughput and lock contention have not been benchmarked. The change claims correctness, not a speed improvement.Full usage-impact report (static, not a gameplay result)
regress.py impact
upstream/main..HEAD3 games use what the change touches, 0 only through a broad rule (every game of its API), 1 not affected, 110 unknown (no usage manifest); 6 files changed, 11 NVN / HLE entry points reached through the host's callers, 1 rules of dm/tools/impact_map.tsv
01007EF00011E0000100A3D008C5C0000100F2C0115B60000100535012974000Unknown (no usage.tsv): 110 profiles (--all lists them).
Why
api:nvn← dm/gfx/nvn/nvn_dirty.c: NVN on Metal: every NVN gamehle:nvnBootstrapLoader← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnDeviceGetProcAddress← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnQueueBuilderSetQueuePriority← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnQueueFinish← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnQueuePresentTexture← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnQueueSubmitCommands← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnTextureInitialize← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnTextureReadTexels← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnTextureReadTexelsStrided← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvnWindowAcquireTexture← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statnvn:nvn_lookup← dm/gfx/nvn/nvn_dirty.c: calls reach it from nvn_dirty_protect, nvn_dirty_statDocs
docs/reference/nvn-objects.mdunder “A retired pool remains read-only”; symptom index regenerated.