Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
make GAME="$GAME" -j3 t_perf_order
./t_perf_order --cpu 2>&1 | tee ../../../logs/t_perf_order.log
make GAME="$GAME" -j3 perf-order-tsan 2>&1 | tee ../../../logs/t_perf_order-tsan.log
tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_tex_layout t_pace t_fstep t_link_ticks t_cpu_tex t_hmap t_cover t_pso_skey t_refl t_rt_range_ro t_present_rescue t_gpuprof t_sparse_layout t_timestamp t_rt_depth_ro t_rt_range_shape t_ub t_pso_pending t_vk_desc"
tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_dirty_retire_race t_tex_layout t_pace t_fstep t_link_ticks t_cpu_tex t_hmap t_cover t_pso_skey t_refl t_rt_range_ro t_present_rescue t_gpuprof t_sparse_layout t_timestamp t_rt_depth_ro t_rt_range_shape t_ub t_pso_pending t_vk_desc"
make GAME="$GAME" -j3 $tests 2>&1 | tee ../../../logs/gfx-build.log
failed=0
for t in $tests; do
Expand Down
11 changes: 9 additions & 2 deletions dm/gfx/nvn/nvn_dirty.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static _Atomic uint64_t g_adds_total;
static _Atomic int g_live, g_live_peak; // number of registered pools and peak (for the memory readout line)
void nvn_dirty_stat(int *live, int *peak, int *cap) { *live = atomic_load(&g_live); *peak = atomic_load(&g_live_peak); *cap = MAX_RANGE; }
static _Atomic int g_overflow_logged;
static pthread_mutex_t g_slot_mu = PTHREAD_MUTEX_INITIALIZER; // Pool creation / destruction (guest threads, possibly concurrent) pick slots under a mutex; the page-fault handling path does not touch it
static pthread_mutex_t g_slot_mu = PTHREAD_MUTEX_INITIALIZER; // Pool creation, retirement and protection serialize descriptor lifetime; the page-fault path does not touch this mutex
static _Atomic uint64_t g_mp_calls, g_mp_pages, g_mp_ticks; // This module's mprotect: call count, pages covered, time spent (mach timebase, safe in the page-fault path too)
static void mp(uintptr_t a, size_t len, int prot) {
uint64_t t = mach_absolute_time(); mprotect((void *)a, len, prot);
Expand Down Expand Up @@ -177,8 +177,14 @@ int nvn_dirty_tracked(const void *addr, size_t len) {
const void *nvn_dirty_span(const void *addr, size_t len) { return len ? find_span((uintptr_t)addr, len) : NULL; }
// Set read-only: mark the pages BUSY first and mark them PROT only after mprotect has landed (for the order and the invariant see the state notes at the top of the file)
void nvn_dirty_protect(const void *addr, size_t len) {
// Keep this descriptor live through the last mprotect. Retirement restores
// RW under this same mutex; it must not finish between lookup and PG_BUSY.
pthread_mutex_lock(&g_slot_mu);
range_t *r = len ? find_span((uintptr_t)addr, len) : NULL;
if (!r) return;
if (!r) { pthread_mutex_unlock(&g_slot_mu); return; }
#ifdef NVN_DIRTY_PROTECT_LOOKUP_HOOK
NVN_DIRTY_PROTECT_LOOKUP_HOOK();
#endif
uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift;
// Consecutive pages are merged into one mprotect (per-page calls reach thousands per frame on the first upload of large textures; character select page startup frame 5: 4888 calls).
// Pages already PROT / BUSY break the consecutive run: do not protect again, and do not touch pages another thread is changing.
Expand All @@ -192,6 +198,7 @@ void nvn_dirty_protect(const void *addr, size_t len) {
if (!took && open) { mp(r->base + (run << g_shift), (i - run) << g_shift, PROT_READ); for (uintptr_t k = run; k < i; k++) atomic_store(&r->state[k], PG_PROT); open = 0; }
if (!took && i <= q && st == PG_BUSY && busy_wait(r, i)) i--; // this page again
}
pthread_mutex_unlock(&g_slot_mu);
}
uint64_t nvn_dirty_now(void) { return atomic_load(&g_now); }
int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_t stamp) {
Expand Down
2 changes: 2 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ t_dirty_slots: t_dirty_slots.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_
clang $(CFLAGS) -o $@ t_dirty_slots.c ../../host/crash.c ../nvn/nvn_dirty.c
t_dirty_race: t_dirty_race.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_dirty.h $(GAME_H)
clang $(CFLAGS) -o $@ t_dirty_race.c ../../host/crash.c
t_dirty_retire_race: t_dirty_retire_race.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_dirty.h $(GAME_H)
clang $(CFLAGS) -o $@ t_dirty_retire_race.c ../../host/crash.c
t_builtin: t_builtin.m ../nvn/nvn_builtin.h
clang -O1 -g -Wall -fobjc-arc -o $@ t_builtin.m -framework Metal -framework Foundation
t_vin: t_vin.m
Expand Down
51 changes: 51 additions & 0 deletions dm/gfx/tests/t_dirty_retire_race.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Retiring a pool after protect's lookup must leave its storage writable.
static void protect_lookup_hook(void);
#define NVN_DIRTY_PROTECT_LOOKUP_HOOK() protect_lookup_hook()
#include "../nvn/nvn_dirty.c"
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

module_t *g_mod;
int g_nmod;
module_t *module_of(const void *p) { (void)p; return NULL; }

static void *page;
static _Atomic int go, removing, removed;
static void protect_lookup_hook(void) {
atomic_store(&go, 1);
while (!atomic_load(&removing)) usleep(100);
// Give the remover the same lookup-to-state window as the failing control.
// With the fix it blocks on the slot mutex until protection finishes.
usleep(100000);
}
static void *retire(void *unused) {
(void)unused;
while (!atomic_load(&go)) usleep(100);
atomic_store(&removing, 1);
nvn_dirty_remove_range(page);
atomic_store(&removed, 1);
return NULL;
}
int main(void) {
size_t size = (size_t)getpagesize();
page = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (page == MAP_FAILED) return 2;
nvn_dirty_add_range(page, size);
pthread_t worker;
if (pthread_create(&worker, NULL, retire, NULL)) return 2;
nvn_dirty_protect(page, size);
pthread_join(worker, NULL);
int fd = open("/dev/zero", O_RDONLY);
if (fd < 0) return 2;
ssize_t bytes = read(fd, page, 16);
int read_error = bytes < 0 ? errno : 0;
int untracked = !nvn_dirty_tracked(page, size);
printf("retirement completed: %d; untracked: %d; write into retired pool: %zd bytes (expected 16), errno=%d\n",
atomic_load(&removed), untracked, bytes, read_error);
close(fd);
// Also let the negative control exit cleanly instead of faulting on cleanup.
mprotect(page, size, PROT_READ | PROT_WRITE);
munmap(page, size);
return bytes == 16 && atomic_load(&removed) && untracked ? 0 : 1;
}
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- packaged textures re-uploaded at every use with unchanged content; upload staging and frame times rising. → [Writes beside a packaged texture in a shared host page](../reference/nvn-objects.md#writes-beside-a-packaged-texture-in-a-shared-host-page)
- about one run in four stalled early (around the opening movie or the title), with no log output, not even the time-limit report. → [Dirty-page tracker race and early stalls](../reference/nvn-objects.md#dirty-page-tracker-race-and-early-stalls)
- a CPU-written texture stays stale after later writes; letters can be missing from a glyph cache. → [Dirty-page writes missed during upload](../reference/nvn-objects.md#dirty-page-writes-missed-during-upload)
- a host system call writing into storage after its NVN pool was finalized fails with `EFAULT`; a direct write can fault after the tracker stopped owning the page. → [A retired pool remains read-only](../reference/nvn-objects.md#a-retired-pool-remains-read-only)
- `[RED] NVN unimplemented function called: nvnSamplerBuilderSetBorderColorui` during renderer initialization. → [Integer sampler border colours](../reference/nvn-objects.md#integer-sampler-border-colours)
- `[RED] sampler ID 4096 not registered` (or another ID past the sampler pool), once, from the first frames on. → [Bound handles are the driver's 32-bit words](../reference/nvn-objects.md#bound-handles-are-the-drivers-32-bit-words)
- device / subsystem setup returns quietly and the next stage never starts. → [Version and capability checks that fail quietly](../reference/nvn-objects.md#version-and-capability-checks-that-fail-quietly)
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/nvn-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ Games read this count as a position in their own command memory and size their h

## NVN entry points

### A retired pool remains read-only

- **Symptom:** a host system call writing into storage after its NVN pool was finalized fails with `EFAULT`; a direct write can fault after the tracker stopped owning the page.
- **Cause:** `nvn_dirty_protect` found a live range without holding the slot mutex. Pool retirement could restore its pages to writable and mark the range dead before protect changed the page state to BUSY. Protect then set the retired storage read-only, with no live descriptor left to release it. The same window allowed the range slot to be reused during protection.
- **Fix:** hold the pool-slot mutex across protect's lookup, page-state transitions and final `mprotect`. Pool creation and retirement already hold that mutex. The page-fault and host pre-write paths still take no slot mutex, so they can finish a BUSY transition while protect waits.
- **Found on:** the iOS 16/17 fork's shared dirty tracker, extracted from compatibility commit `587d5206`; the lookup/retirement race is also present on current `main`. This is independent of the Metal renderer and GPU generation; no game's reported picture failure is attributed to it without a reproducing game trace.
- **Check:** `t_dirty_retire_race` starts retirement between the live-range lookup and the first protection-state update, then writes 16 bytes through `read()`. With the lookup hook but without the fix, retirement completes and the write returns -1. With the fix, retirement completes after protection, unregisters the pool and the write returns 16. Existing `t_dirty_race` and `t_dirty_slots` cover page-state publication and shared/reused pool slots.

### Integer sampler border colours

- **Symptom:** `[RED] NVN unimplemented function called: nvnSamplerBuilderSetBorderColorui` during renderer initialization.
Expand Down