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
1 change: 1 addition & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
- [Memory pools nothing gave an address to stay out of the raw pool table](../../../docs/reference/memory.md#memory-pools-nothing-gave-an-address-to-stay-out-of-the-raw-pool-table)
- [Shaders report which textures they read through a pool](../../../docs/reference/memory.md#shaders-report-which-textures-they-read-through-a-pool)
- [Repeated GPU out-of-memory failures end the game](../../../docs/reference/memory.md#repeated-gpu-out-of-memory-failures-end-the-game)
- [Pools that hold shader code made resident to key the shaders](../../../docs/reference/memory.md#pools-that-hold-shader-code-made-resident-to-key-the-shaders): 222 pools, 334 MB the GPU never reads.
- [Texture IDs of one storage share one packaged texture](../../../docs/reference/nvn-objects.md#texture-ids-of-one-storage-share-one-packaged-texture)
- [Finalizing a copy of a texture object](../../../docs/reference/nvn-objects.md#finalizing-a-copy-of-a-texture-object)
- [Dynamic resolution that never moves, and draw passes that wait for each other](../../../docs/reference/frame-rate.md#dynamic-resolution-that-never-moves-and-draw-passes-that-wait-for-each-other)
Expand Down
4 changes: 2 additions & 2 deletions dm/gfx/nvn/hle_nvn.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ void nvn_ctl_lens(const void *control, uint32_t *nctl, uint32_t *ncode) {
*nctl = own >= 0x760 && own < 0x880 ? own : 0x880;
}
static int keys_hash(const NVNshaderData *sd, int mapped, uint8_t out[8]) {
uint32_t ncode = 0, nctl; void *mb; uint64_t mo;
uint32_t ncode = 0, nctl;
if (!sd->control) return 0;
nvn_ctl_lens(sd->control, &nctl, &ncode);
int code = sd->data && ncode && ncode < (1u << 24);
if (mapped && !(code && mtl_addr(sd->data, ncode, &mb, &mo))) return 0;
if (mapped && !(code && mtl_pool_holds(sd->data, ncode))) return 0; // its code in a memory pool; the pool is not made resident for that (the GPU runs the translated shader)
uint8_t md[CC_SHA256_DIGEST_LENGTH]; CC_SHA256_CTX hc; CC_SHA256_Init(&hc);
// Profile key shader_key_skip: bytes of the control section hashed as zeros (shader_split.py key_ctl). Batman: Arkham Knight's engine shares one control section among shaders whose
// sections differ only in GLSLC's compile information (+0x778..+0x791, +0x7d0..+0x7e7): it hands a program the section of an earlier shader with the same rest, so the bytes there name another shader
Expand Down
1 change: 1 addition & 0 deletions dm/gfx/nvn/nvn_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void mtl_alias_drop_pool(const void *pool);
void mtl_sp_phys_gone(const void *mem, uint64_t size);
void mtl_pool_gpu_written(const void *pool); // the GPU writes this pool: its uncompressed textures are no longer taken from storage (tex_cpu_content)
int mtl_addr(uint64_t addr, uint64_t len, void **buf, uint64_t *off); // 1 on success; buf is a (__bridge) id<MTLBuffer>
int mtl_pool_holds(uint64_t addr, uint64_t len); // 1 when the range lies in one memory pool (as mtl_addr finds it); does not make the pool resident

typedef struct {
uint32_t pso, cps, shaders, dss;
Expand Down
3 changes: 3 additions & 0 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
while (j < g_naddr && g_addr[j].lo <= hi) { lo = MIN(lo, g_addr[j].lo); hi = MAX(hi, g_addr[j].hi); j++; }
if (i == j && g_naddr == g_addr_cap) { g_addr_cap = g_addr_cap ? g_addr_cap * 2 : 256; g_addr = realloc_or_die(g_addr, (size_t)g_addr_cap * sizeof *g_addr, "the addressed range table"); }
if (j != i + 1) memmove(g_addr + i + 1, g_addr + j, (size_t)(g_naddr - j) * sizeof *g_addr);
g_naddr += 1 - (j - i); g_addr[i].lo = lo; g_addr[i].hi = hi;

Check failure on line 592 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Out of bound access to memory preceding the heap area [clang-analyzer-security.ArrayBound] (3 in this file, the baseline has 2)
atomic_fetch_add(&g_pool_gen, 1); // the table rebuilds before the next draw that uses it
}
pthread_mutex_unlock(&g_mu);
Expand Down Expand Up @@ -731,6 +731,9 @@
pthread_mutex_lock(&g_mu); int ok = big_of(addr) >= 0 || pool_find(addr, len) >= 0; pthread_mutex_unlock(&g_mu);
return ok;
}
// Whether [addr, addr + len) lies in one memory pool, as mtl_addr finds it, without making the pool resident. hle_nvn.c keys_hash checks this way that a shader's code is mapped:
// through mtl_addr it made every pool holding shader code resident, which the GPU never reads, since the shaders run translated (Ocarina of Time: 222 pools, 334 MB)
int mtl_pool_holds(uint64_t addr, uint64_t len) { pthread_mutex_lock(&g_mu); int i = pool_find(addr, len); pthread_mutex_unlock(&g_mu); return i >= 0; }
int mtl_addr(uint64_t addr, uint64_t len, void **buf, uint64_t *off) {
const pc_ent *c = pc_get(addr, len);
if (c) { *buf = (__bridge void *)c->buf; *off = addr - c->base; return 1; }
Expand Down Expand Up @@ -2100,7 +2103,7 @@
}
static void pkg_share_put(const tex_t *t, NvPkg *pk) {
alias_key k = tex_alias_key(t); NvPkgRef *r = [NvPkgRef new]; r->pkg = pk;
pthread_mutex_lock(&g_smu); hm_put_id(&g_pkg_share, &k, r); pthread_mutex_unlock(&g_smu);

Check failure on line 2106 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Potential leak of an object stored into 'r' [clang-analyzer-osx.cocoa.RetainCount] (2 in this file, the baseline has 1)
}
// A sparse texture's CPU-write check walks its page table (nvn_sparse_since). A submission sees the CPU writes and the mappings made before it (as NVN's: a map is a CPU call,
// not a command), so the replay checks each one once a submission (sub: the submission, g_sub_serial, of its last check). Games remap streamed pages all the time, so the
Expand Down
2 changes: 2 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ t_tex_fit_root: t_tex_fit_root.m $(MTL_T_DEP)
.PHONY: check_t_tex_fit_root
check_t_tex_fit_root: t_tex_fit_root
MTL_DEBUG_LAYER=1 ./t_tex_fit_root
t_pool_holds: t_pool_holds.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_pool_holds.m $(MTL_T_SRC) $(FW)
t_pool_chunks: t_pool_chunks.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_pool_chunks.m $(MTL_T_SRC) $(FW)
t_sparse_tiles: t_sparse_tiles.m $(MTL_T_DEP)
Expand Down
31 changes: 31 additions & 0 deletions dm/gfx/tests/t_pool_holds.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// mtl_pool_holds answers whether a range lies in one memory pool as mtl_addr finds it, without making the pool resident: hle_nvn.c keys_hash asks it whether a shader's code is
// mapped, and through mtl_addr every pool holding shader code went into the residency set though the GPU never reads it (Ocarina of Time: 222 pools, 334 MB)
#include "../nvn/nvn_mtl.m"
void *front_metal_layer(void) { return NULL; }
module_t *module_of(const void *p) { (void)p; return NULL; }
module_t *g_mod; int g_nmod;
void svc_thread_dump(void) {}
uint64_t now_ns(void) { return bp_now(); }
static int checks, failed;
#define CHECK(label, c) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", label, __LINE__); } } while (0)
static int pool_resident(uint64_t a) { pthread_mutex_lock(&g_mu); int i = pool_find(a, 1), r = i >= 0 && g_pools[i].res; pthread_mutex_unlock(&g_mu); return r; }
int main(void) { @autoreleasepool {
mtl_init();
const size_t n = 4u << 20;
uint8_t *mem = mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (mem == MAP_FAILED) abort();
uint64_t base = (uint64_t)(uintptr_t)mem;
mtl_pool_add(mem, n);
int res0 = g_pool_res_n;
CHECK("a range inside the pool is held", mtl_pool_holds(base + 4096, 2048));
CHECK("the pool's last bytes are held", mtl_pool_holds(base + n - 16, 16));
CHECK("a range past the pool's end is not", !mtl_pool_holds(base + n - 16, 32));
CHECK("an address outside every pool is not", !mtl_pool_holds(base + n + 4096, 16));
CHECK("asking makes no pool resident", g_pool_res_n == res0 && !pool_resident(base));
void *mb = NULL; uint64_t mo = 0;
CHECK("mtl_addr finds the same range", mtl_addr(base + 4096, 2048, &mb, &mo) && mo == 4096);
CHECK("mtl_addr makes the pool resident (what keys_hash did)", g_pool_res_n == res0 + 1 && pool_resident(base));
mtl_pool_del(mem);
CHECK("a removed pool holds nothing", !mtl_pool_holds(base + 4096, 2048));
printf("t_pool_holds: %d checks, %d failed\n", checks, failed);
return failed != 0;
} }
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- on an iPhone the app is ended by the system (a jetsam event: no crash report, no `[dm] CRASH` line) once its footprint reaches the limit: about 6 GB on a 12 GB iPhone with the increased memory limit (6,143 MiB measured in LiveContainer), 3.3 GB without it. The log stops mid-frame. → [iOS ends the app once the guest's touched pages reach its memory limit](../reference/memory.md#ios-ends-the-app-once-the-guests-touched-pages-reach-its-memory-limit)
- a Switch 2 game goes past Metal's recommended working set while its first 3D scene loads: `Insufficient Memory` command buffer errors, then a hung Mac. Ocarina of Time on a 16 GB Mac: 12 GB and more wired, once a kernel panic in `IOGPUGroupMemory.cpp` at 14.7 GB wired. → [Memory pools nothing gave an address to stay out of the raw pool table](../reference/memory.md#memory-pools-nothing-gave-an-address-to-stay-out-of-the-raw-pool-table)
- a game whose shaders read texture handles from memory wires far past the working set while a scene loads, with `Insufficient Memory` errors or a panic, and stutters when new textures stream in. With a limit on how many it makes, surfaces in shadow go near black instead. → [Shaders report which textures they read through a pool](../reference/memory.md#shaders-report-which-textures-they-read-through-a-pool)
- the residency set holds every memory pool a program's shader code lies in: in Ocarina of Time, 222 pools and 334 MB added through `mtl_addr` as the game sets its programs (the residency line's additions by source line). → [Pools that hold shader code made resident to key the shaders](../reference/memory.md#pools-that-hold-shader-code-made-resident-to-key-the-shaders)
- the Mac grows unresponsive for hours, then panics, with the host still submitting work that fails. → [Repeated GPU out-of-memory failures end the game](../reference/memory.md#repeated-gpu-out-of-memory-failures-end-the-game)

## Threads
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ The guest address space, heaps, pools, virtual address memory and the allocators
- **Reading the numbers:** `currentAllocatedSize` ("GPU allocated") read 16 GB on this game while `vmmap` showed 3.9 GB of Metal textures and targets. Use `vmmap -summary` (`owned unmapped (graphics)`, `IOAccelerator (graphics)`) for what memory holds.
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md).

### Pools that hold shader code made resident to key the shaders

- **Symptom:** the residency set holds every memory pool a program's shader code lies in: in Ocarina of Time, 222 pools and 334 MB added through `mtl_addr` as the game sets its programs (the residency line's additions by source line).
- **Cause:** `keys_hash` (`hle_nvn.c`) checks that a shader's code is mapped in a pool before it hashes it, and asked `mtl_addr`, which also makes the pool resident. The GPU never reads that code: the shaders run translated.
- **Fix:** general (`nvn_mtl.m` `mtl_pool_holds`): the same pool lookup as `mtl_addr`, without the residency. A pool the GPU does read through a binding or an address is made resident there, as before. `t_pool_holds` checks it.
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md), whose GPU working set is over an iPad's 8 GB.

### Repeated GPU out-of-memory failures end the game

- **Symptom:** the Mac grows unresponsive for hours, then panics, with the host still submitting work that fails.
Expand Down
Loading