gfx/nvn: a lent command memory segment is freed when its list is written over, not when memory over it is granted

Games grant at least 512 bytes for a list measured smaller (max(S, 512)) and
write only its records, so the grant for one list can reach over the next
list, which is still called. The host freed that next list's lent segment at
the grant, and the next command buffer to run short recorded over its tail.

A segment is now freed when another recording writes records over its list's
guest bytes (hostseg_written, at each piece a recording closes); a recording's
own segments carry its serial (cmd_t.rec_id). With no segment lent, the check
is one atomic load. No game here lends from its own pool (Splatoon Raiders:
none in 21,300 frames), so no picture changes. t_nvn covers the kept list and
the reclaim.
This commit is contained in:
seedds committed 2026-11-14 02:32:35 +08:00
1 parent fb510cce07
commit 0d10378750
6 files changed
+80 -15

No files matched your search

+1
View File
@@ -55,6 +55,7 @@ The work folder's `run/drive.sh <name> <secs> "<sec> <control line>"…` boots i
- [The split shader tables differ from a game profile's](../../../docs/reference/packing.md#the-split-shader-tables-differ-from-a-game-profiles): NigoProc's `800215a0f8d0c44d` is a mesh shader, and every pack stopped until its row was split again ([issue #1263](https://github.com/seedds/nxnative/issues/1263)).
- [A control section's words read as a BAHS entry](../../../docs/reference/shaders.md#a-control-sections-words-read-as-a-bahs-entry): resolved, 2026-11-10. Three rows of `agl_resource.Nin_Ounce_NVN2.debug.sarc.zs` were no shaders (`cs_other` `68bbe98b3bfd9876` in `agl_common`, `ps_other` `18be578dfdaaffa0` in `agl_technique`, `vs_other` `7a4936fccf82094e` in `agl_technique_pfx`), and every NSP build stopped at the table check after 04df08d4 ([issue #1613](https://github.com/seedds/nxnative/issues/1613)). The tables were split again from the update: those rows and one fact went, the later rows of each package moved down one index, and nothing else changed.
- [Storage words read one by one](../../../docs/reference/shaders.md#storage-words-read-one-by-one): the lit pass on an M1, 70 → 16 ms.
- [A command memory grant over a list whose tail is lent](../../../docs/reference/nvn-objects.md#a-command-memory-grant-over-a-list-whose-tail-is-lent): the game grants more command memory than it writes; this host lends Raiders no command memory, so the bug never showed here.
- [A blocked audio device start freezes the game](../../../docs/reference/audio.md#a-blocked-audio-device-start-freezes-the-game) when the selected HDMI device refuses to play.
- [Goto elimination forcing loops around a whole program](../../../docs/reference/shaders.md#goto-elimination-forcing-loops-around-a-whole-program): the first shader build stalled for 5 h 40.
+25 -14
View File
@@ -992,9 +992,11 @@ static void cmd_new_segment(cmd_t *o, uint8_t *mem, size_t n) {
// (Do not use the whole game chunk as the range: SMBW sub-allocates many recordings out of one big chunk, and a later grant inside that chunk freed segments still in use.)
// Reclaim follows the game's command memory chunks, not a frame count: the game caches recorded command handles and CALLs them every frame until it re-records them itself (SMBW's object setup handles);
// once a handle was recorded into a host segment, the old "reclaim 8 frames after lending" rewrote the segment while the game still CALLed it (with overflow forced on the Mac, the replay read garbage commands: RGBA16F used as a depth format for a pipeline).
// A segment hangs off the recording that overflowed into it: that recording occupies [its first record, the write position at the overflow) in guest memory, and the game only gives
// those bytes out again once they are dead (when it re-records that material in place; packing the next recording right behind by used never touches them). So a segment remembers
// that guest range and is reclaimed when the game gives out memory overlapping it; segments overflowing from a host segment keep the same range.
// A segment hangs off the recording that overflowed into it: that recording occupies [its first record, the write position at the overflow) in guest memory, and the game only writes
// those bytes again once they are dead (when it re-records that material in place; packing the next recording right behind by used never touches them). So a segment remembers
// that guest range and is reclaimed when another recording writes records over it (hostseg_written); segments overflowing from a host segment keep the same range.
// A grant over the range is not enough: games grant at least 512 bytes for a list measured smaller and write only its records (Tears of the Kingdom's model lists, max(S, 512); the same
// pattern in Splatoon Raiders' agl), so a grant for one list can cover the next list, still called. Freeing at the grant lent that list's tail to the next overflow
// The segment pool grows on demand (SMBW opening course: many small command buffers each overflow once per frame, and the old fixed 64 segments × 1 MB ran out within 8 frames); abort only beyond HOSTSEG_CAP in total.
// A segment is HOSTSEG_SZ, with two exceptions (hostseg_size): an empty pool with a length of at most HOSTSEG_SMALL outside a recording gets that much rounded up to HOSTSEG_SMALL
// (Super Mario Odyssey hands out many such 512-byte grants before recording; at 256 KB each, and with no recording to reclaim them by, they reached the cap entering the Wooded
@@ -1006,8 +1008,10 @@ static void cmd_new_segment(cmd_t *o, uint8_t *mem, size_t n) {
#define HOSTSEG_SMALL (4u << 10)
#define HOSTSEG_CAP (1u << 30)
uint64_t dm_nvn_frame_count(void);
typedef struct { uint8_t *mem; size_t n; uint64_t frame; int busy; uintptr_t org_lo, org_hi; } hostseg_t; // org: the guest range of the recording it hangs off (0 = unknown, never reclaimed)
typedef struct { uint8_t *mem; size_t n; uint64_t frame; int busy; uintptr_t org_lo, org_hi; uint64_t rec_id; } hostseg_t; // org: the guest range of the recording it hangs off (0 = unknown, never reclaimed); rec_id: the recording that took it
static hostseg_t *g_hostseg; static int g_nhostseg, g_caphostseg; static uint64_t g_hostseg_bytes;
static _Atomic int g_hostseg_held; // busy segments with a guest range: 0 lets hostseg_written return without the lock (no game seen so far lends one from its own pool)
static _Atomic uint64_t g_rec_serial; // BeginRecording's serials (cmd_t rec_id)
static pthread_mutex_t g_hostseg_mu = PTHREAD_MUTEX_INITIALIZER;
static size_t hostseg_size(const cmd_t *o, int empty_pool, size_t want) {
if (empty_pool) return want && want <= HOSTSEG_SMALL && !o->recording ? HOSTSEG_SMALL : HOSTSEG_SZ;
@@ -1015,29 +1019,36 @@ static size_t hostseg_size(const cmd_t *o, int empty_pool, size_t want) {
}
// Take a segment of n bytes: origin = the current recording's guest bytes [first record, write position), at least the byte at the write position; if the write pointer is already in
// a host segment (overflowing again), keep that segment's origin
static uint8_t *hostseg_take(const cmd_t *o, size_t n) {
static uint8_t *hostseg_take(cmd_t *o, size_t n) {
static unsigned total; uint64_t f = dm_nvn_frame_count(); uint8_t *m = NULL;
if (o && o->recording) ctl_chunk_close(o); // the records written so far count as written (hostseg_written) before a free segment is chosen
uintptr_t lo = 0, hi = 0, cur = o ? (uintptr_t)o->cur : 0;
pthread_mutex_lock(&g_hostseg_mu);
int in_seg = 0; // by the segment's end too: a small segment can end where another one starts
for (int i = 0; i < g_nhostseg && cur; i++) if (cur >= (uintptr_t)g_hostseg[i].mem && o->end == g_hostseg[i].mem + g_hostseg[i].n) { lo = g_hostseg[i].org_lo; hi = g_hostseg[i].org_hi; in_seg = 1; break; }
if (!in_seg && o && o->recording && o->cur) { lo = o->recording == 1 && o->begin && (uintptr_t)o->begin <= cur ? (uintptr_t)o->begin : cur; hi = cur > lo ? cur : lo + 1; }
for (int i = 0; i < g_nhostseg && !m; i++)
if (!g_hostseg[i].busy && g_hostseg[i].n == n) { g_hostseg[i] = (hostseg_t){ g_hostseg[i].mem, n, f, 1, lo, hi }; m = g_hostseg[i].mem; }
if (!g_hostseg[i].busy && g_hostseg[i].n == n) { g_hostseg[i] = (hostseg_t){ g_hostseg[i].mem, n, f, 1, lo, hi, o ? o->rec_id : 0 }; m = g_hostseg[i].mem; }
if (!m && g_hostseg_bytes + n <= HOSTSEG_CAP && (m = malloc(n))) {
if (g_nhostseg == g_caphostseg) { g_caphostseg = g_caphostseg ? g_caphostseg * 2 : 64; g_hostseg = realloc(g_hostseg, sizeof *g_hostseg * (size_t)g_caphostseg); }
g_hostseg[g_nhostseg++] = (hostseg_t){ m, n, f, 1, lo, hi }; g_hostseg_bytes += n;
g_hostseg[g_nhostseg++] = (hostseg_t){ m, n, f, 1, lo, hi, o ? o->rec_id : 0 }; g_hostseg_bytes += n;
}
if (m && hi > lo) atomic_fetch_add_explicit(&g_hostseg_held, 1, memory_order_relaxed);
unsigned t = ++total; int ns = g_nhostseg; uint64_t bytes = g_hostseg_bytes;
pthread_mutex_unlock(&g_hostseg_mu);
if (!m) die("NVN command memory: the game's pool is empty and host-lent segments reached %u MB without being reclaimed by the game's blocks", HOSTSEG_CAP >> 20);
if (t == 1 || t % 1024 == 0) LOG("[nvn] the game's command memory ran short, the host lends a segment of %zu KB (%u times so far, frame %llu, segment pool %d segments, %llu KB)", n >> 10, t, (unsigned long long)f, ns, (unsigned long long)(bytes >> 10));
return m;
}
// The game gives out [lo, hi): segments whose recording's guest bytes overlap it are dead, reclaim them
static void hostseg_release(uintptr_t lo, uintptr_t hi) {
// Recording o wrote records at [p, p + n) (a piece it closes: ctl_chunk_close, cmd_alloc): segments of other recordings whose guest bytes these overlap are dead, reclaim them
static void hostseg_written(const cmd_t *o, const uint8_t *p, uint64_t n) {
if (!n || !atomic_load_explicit(&g_hostseg_held, memory_order_relaxed)) return;
uintptr_t lo = (uintptr_t)p, hi = lo + n;
pthread_mutex_lock(&g_hostseg_mu);
for (int i = 0; i < g_nhostseg; i++) if (g_hostseg[i].busy && g_hostseg[i].org_hi > lo && g_hostseg[i].org_lo < hi) g_hostseg[i].busy = 0;
for (int i = 0; i < g_nhostseg; i++) {
hostseg_t *s = &g_hostseg[i];
if (s->busy && s->org_hi > s->org_lo && s->rec_id != o->rec_id && s->org_hi > lo && s->org_lo < hi) { s->busy = 0; atomic_fetch_sub_explicit(&g_hostseg_held, 1, memory_order_relaxed); }
}
pthread_mutex_unlock(&g_hostseg_mu);
}
NVN(nvnCommandBufferAddCommandMemory, void, NVNcommandBuffer *c, const NVNmemoryPool *p, ptrdiff_t off, size_t n) {
@@ -1046,7 +1057,6 @@ NVN(nvnCommandBufferAddCommandMemory, void, NVNcommandBuffer *c, const NVNmemory
if (!pl || n < 64) { if (cs_on()) { pthread_mutex_lock(&g_cs_mu); g_cs.hostseg++; pthread_mutex_unlock(&g_cs_mu); } size_t sz = hostseg_size(CB(c), !pl, n); cmd_new_segment(CB(c), hostseg_take(CB(c), sz), sz); return; }
if (!pl->mem) { LOG("[RED] AddCommandMemory: pool has no CPU storage"); return; }
vpool_taken(pl);
hostseg_release((uintptr_t)(pl->mem + off), (uintptr_t)(pl->mem + off + n));
if (cs_on()) { pthread_mutex_lock(&g_cs_mu); g_cs.grants++; g_cs.grant_bytes += n; if (!g_cs.grant_min || n < g_cs.grant_min) g_cs.grant_min = n; if (n > g_cs.grant_max) g_cs.grant_max = n; pthread_mutex_unlock(&g_cs_mu); }
{ static int cblog = -1; if (cblog < 0) cblog = getenv("DM_NVN_CBLOG") != NULL; static uint64_t ng; if (cblog && (++ng <= 60 || ng % 500 == 0)) LOG("[nvn] AddCommandMemory #%llu cmd=%p pool %p +%#lx length %#zx (recording %d)", (unsigned long long)ng, (void *)c, (const void *)pl, (long)off, n, CB(c)->recording); }
cmd_new_segment(CB(c), pl->mem + off, n);
@@ -1090,7 +1100,7 @@ static void ctl_push(cmd_t *o, uint64_t e) {
}
static void ctl_flush(cmd_t *o) { if (o->ctl_q[1]) { ctl_put(o, o->ctl_q[0]); o->ctl_q[0] = o->ctl_q[1]; o->ctl_q[1] = 0; } }
static void ctl_chunk_close(cmd_t *o) {
if (o->chunk && o->cur > o->chunk) ctl_push(o, ctl_e(CTL_CHUNK, o->chunk, (uint64_t)(o->cur - o->chunk)));
if (o->chunk && o->cur > o->chunk) { hostseg_written(o, o->chunk, (uint64_t)(o->cur - o->chunk)); ctl_push(o, ctl_e(CTL_CHUNK, o->chunk, (uint64_t)(o->cur - o->chunk))); }
o->chunk = NULL;
}
NVN(nvnCommandBufferAddControlMemory, void, NVNcommandBuffer *c, void *m, size_t n) { ctl_set(CB(c), m, n); }
@@ -1112,8 +1122,8 @@ static uint8_t *cmd_alloc(cmd_t *o, uint32_t size) {
uint8_t *p = o->cur; o->cur += size; o->used += size; put_hdr(p, 0, size);
if (o->recording == 2) { o->begin = p; o->recording = 1; }
if (o->recording) {
if (o->chunk && (uint64_t)(o->cur - o->chunk) > CTL_CHUNK_MAX) { if (p > o->chunk) ctl_push(o, ctl_e(CTL_CHUNK, o->chunk, (uint64_t)(p - o->chunk))); o->chunk = NULL; }
if (size > CTL_CHUNK_MAX) ctl_push(o, ctl_e(CTL_CHUNK, p, CTL_ONE)); // a record longer than a piece can say gets an entry of its own
if (o->chunk && (uint64_t)(o->cur - o->chunk) > CTL_CHUNK_MAX) { if (p > o->chunk) { hostseg_written(o, o->chunk, (uint64_t)(p - o->chunk)); ctl_push(o, ctl_e(CTL_CHUNK, o->chunk, (uint64_t)(p - o->chunk))); } o->chunk = NULL; }
if (size > CTL_CHUNK_MAX) { hostseg_written(o, p, size); ctl_push(o, ctl_e(CTL_CHUNK, p, CTL_ONE)); } // a record longer than a piece can say gets an entry of its own
else if (!o->chunk) o->chunk = p;
ctl_flush(o);
}
@@ -1126,6 +1136,7 @@ NVN(nvnCommandBufferBeginRecording, void, NVNcommandBuffer *c) {
cmd_t *o = CB(c);
if (o->recording) LOG("[RED] BeginRecording re-entered");
o->recording = 2; // 2: recording, no command yet (cmd_alloc sets begin)
o->rec_id = atomic_fetch_add_explicit(&g_rec_serial, 1, memory_order_relaxed) + 1;
o->chunk = NULL; o->ctl_list = NULL; o->ctl_q[0] = o->ctl_q[1] = 0;
}
// The recording's last entry carries CTL_LAST; an empty recording's list is one empty piece. The handle is the list's first entry
+1
View File
@@ -95,6 +95,7 @@ typedef struct {
// for the next record, so that the game's control-memory callback never runs inside its own command-memory callback (hle_nvn.c ctl_push)
uint8_t *ctl_list; // The recording's first control entry once written: its handle (NULL before)
uint8_t *chunk; // Start of the recording's piece of records in the current segment not yet in a CTL_CHUNK entry (NULL = none open)
uint64_t rec_id; // The current or last recording's serial (hle_nvn.c BeginRecording): its own host-lent segments are not freed by its own writes (hostseg_written)
} cmd_t; FITS(cmd_t, NVNcommandBuffer);
// ---------- State objects (defaults follow the NVN / GL defaults) ----------
+44
View File
@@ -707,6 +707,50 @@ int main(void) {
EXPECT("lists checked", 126, lists);
P(nvnCommandBufferFinalize)(&c3); P(nvnMemoryPoolFinalize)(&mp);
}
// 5b''. A grant is not a write. Slot A (at 512) overflows its 64 bytes into a host-lent segment; slot B before it is then re-recorded with a grant of 512 bytes that
// reaches over A's bytes but writes only one record (max(S, 512), as above). A is still live: another buffer's overflow must not be handed A's segment
{
static uint8_t m[8192] __attribute__((aligned(4096))); NVNmemoryPoolBuilder mb; NVNmemoryPool mp; NVNcommandBuffer ca, cb, cx;
P(nvnMemoryPoolBuilderSetDefaults)(&mb); P(nvnMemoryPoolBuilderSetStorage)(&mb, m, sizeof m); P(nvnMemoryPoolInitialize)(&mp, &mb);
P(nvnCommandBufferInitialize)(&ca, &dev); P(nvnCommandBufferInitialize)(&cb, &dev); P(nvnCommandBufferInitialize)(&cx, &dev); // no callbacks: an overflow goes to a host segment
enum { NB = 40 };
P(nvnCommandBufferAddCommandMemory)(&ca, &mp, 512, 64); P(nvnCommandBufferBeginRecording)(&ca);
for (int i = 0; i < NB; i++) P(nvnCommandBufferBindUniformBuffer)(&ca, NVN_SHADER_STAGE_FRAGMENT, i & 15, UINT64_C(0x10000) * (uint64_t)(i + 1), 0x100);
NVNcommandHandle ha = P(nvnCommandBufferEndRecording)(&ca);
P(nvnCommandBufferAddCommandMemory)(&cb, &mp, 0, 1024); P(nvnCommandBufferBeginRecording)(&cb); // B's grant covers A's slot; B writes 24 bytes at 0
P(nvnCommandBufferBindUniformBuffer)(&cb, NVN_SHADER_STAGE_VERTEX, 1, 0x7770000, 0x100); P(nvnCommandBufferEndRecording)(&cb);
P(nvnCommandBufferAddCommandMemory)(&cx, &mp, 4096, 64); P(nvnCommandBufferBeginRecording)(&cx); // another overflow, which takes a free segment of the same size
for (int i = 0; i < NB; i++) P(nvnCommandBufferBindUniformBuffer)(&cx, NVN_SHADER_STAGE_COMPUTE, 0, 0xdead0000, 0x40);
P(nvnCommandBufferEndRecording)(&cx);
int good = 0; size_t ln; uint8_t *L = nvn_rec_linear(ha, &ln);
for (const uint8_t *p = L; p < L + ln; ) {
op_hdr h; memcpy(&h, p, sizeof h); if (!h.size) break;
if (h.op == OP_BIND_UBO || h.op == OP_BIND_UBO_SMALL) {
p_bind_buf tmp; const p_bind_buf *b = bind_buf_rec(h.op, p + 8, &tmp);
if (b->stage == NVN_SHADER_STAGE_FRAGMENT && b->index == (good & 15) && b->addr == UINT64_C(0x10000) * (uint64_t)(good + 1) && b->size == 0x100) good++;
else break;
}
p += h.size;
}
free(L);
EXPECT("live list keeps its lent segment after a wider grant", NB, good);
// A recorded again in place: its old records are overwritten, so its segment is free, and the next overflow of that size takes it instead of a new one
uint8_t *a_seg = NULL; pthread_mutex_lock(&g_hostseg_mu);
for (int i = 0; i < g_nhostseg; i++) if (g_hostseg[i].busy && g_hostseg[i].org_lo == (uintptr_t)(m + 512)) a_seg = g_hostseg[i].mem;
pthread_mutex_unlock(&g_hostseg_mu);
EXPECT("the lent segment is A's", 1, a_seg != NULL);
P(nvnCommandBufferAddCommandMemory)(&ca, &mp, 512, 64); P(nvnCommandBufferBeginRecording)(&ca);
P(nvnCommandBufferBindUniformBuffer)(&ca, NVN_SHADER_STAGE_FRAGMENT, 0, 0x5550000, 0x100); P(nvnCommandBufferEndRecording)(&ca);
int a_busy = 1, nseg = g_nhostseg; pthread_mutex_lock(&g_hostseg_mu);
for (int i = 0; i < g_nhostseg; i++) if (g_hostseg[i].mem == a_seg) a_busy = g_hostseg[i].busy;
pthread_mutex_unlock(&g_hostseg_mu);
EXPECT("re-recording A in place frees its segment", 0, a_busy);
P(nvnCommandBufferAddCommandMemory)(&cx, &mp, 4096, 64); P(nvnCommandBufferBeginRecording)(&cx);
for (int i = 0; i < NB; i++) P(nvnCommandBufferBindUniformBuffer)(&cx, NVN_SHADER_STAGE_COMPUTE, 0, 0xdead0000, 0x40);
P(nvnCommandBufferEndRecording)(&cx);
EXPECT("the next overflow reuses it: no new segment", nseg, g_nhostseg);
P(nvnCommandBufferFinalize)(&ca); P(nvnCommandBufferFinalize)(&cb); P(nvnCommandBufferFinalize)(&cx); P(nvnMemoryPoolFinalize)(&mp);
}
// 5b'''. Calls and copies (NVN): CallCommands copies the called list's pieces into the caller's list, as the driver copies a command set's GPU FIFO entries, so the called
// recording's control memory may be given out again once the call is recorded (Donkey Kong Bananza resets its control arena twice per command arena: when a call
// pointed at the called list, the caller replayed whatever the next recordings wrote there); CopyCommands copies the records, so its command memory may go too
+1
View File
@@ -651,6 +651,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- `[RED] NVN unimplemented function called: nvnCommandBufferSetFloatBlendClamp`; now `[RED] nvnCommandBufferSetFloatBlendClamp 1 (a float blend result that overflows becomes the largest finite value): Metal stores infinity`, once. → [Float blend clamp (`nvnCommandBufferSetFloatBlendClamp`)](../reference/nvn-objects.md#float-blend-clamp-nvncommandbuffersetfloatblendclamp)
- `[RED] NVN unimplemented function called: nvnCommandBufferSetRepresentativeFragmentTest`, once. → [Representative fragment test (`nvnCommandBufferSetRepresentativeFragmentTest`)](../reference/nvn-objects.md#representative-fragment-test-nvncommandbuffersetrepresentativefragmenttest)
- `FATAL NVN command memory: the game's pool is empty…`. → [Host-lent command memory segments](../reference/nvn-objects.md#host-lent-command-memory-segments)
- a recorded list that the game keeps calling replays another list's records from some point on: wrong uniform buffers or state bound, a scene that alternates between right and wrong lighting frame by frame. Only once the host lends segments (the line `the host lends a segment` in the log). → [A command memory grant over a list whose tail is lent](../reference/nvn-objects.md#a-command-memory-grant-over-a-list-whose-tail-is-lent)
- frames stop after about 15 s; a game thread spins in its command-memory callback (a ring allocator at `main+0x9cc120` waiting for fences that have all signalled). → [What `GetCommandMemoryUsed` counts](../reference/nvn-objects.md#what-getcommandmemoryused-counts)
- `CRASH sig=10` in `mtl_exec` after minutes of play, the fault address equal to `x19` and shaped like `0x1000000003`. → [Prerecorded lists in slots of their measured size](../reference/nvn-objects.md#prerecorded-lists-in-slots-of-their-measured-size)
- a model load runs out of its resource heap: `[RED] BufferInitialize: pool has no storage (pool 0x0 …)`, then `CRASH sig=11` in `h_nvnMemoryPoolGetFlags` (Model heaps sized by the host's command record sizes). → [A recording's end and links are in control memory](../reference/nvn-objects.md#a-recordings-end-and-links-are-in-control-memory)
+8 -1
View File
@@ -393,9 +393,16 @@ A game may tell NVN objects apart by their bytes, without an NVN call. A host re
- **Symptom:** `FATAL NVN command memory: the game's pool is empty…`.
- **Cause:** our command records are larger than real NVN commands, so command buffers the game sizes for hardware overflow; when the game's out-of-memory callback declines to add memory, the host lends a segment. Many small command buffers each overflowing once per frame exhausted a fixed pool of 64 × 1 MB.
- **Fix** (`hle_nvn.c`): the pool now grows on demand (256 KB segments, abort only beyond 1 GB; `hostseg_take`). A segment is released only when the game re-grants memory overlapping the guest bytes of the recording that overflowed into it (`hostseg_release` in `AddCommandMemory`): the game records setup blocks once and keeps `CallCommands`-ing them for many frames, so recycling segments after a fixed number of frames would overwrite live recordings. An empty-pool grant of at most 4 KB outside a recording gets a 4 KB segment, a record larger than 256 KB gets a segment that holds it, and the cap counts every segment's bytes (`hostseg_size`). Such a grant has no guest range, so its segment is never reclaimed. On hardware a null pool cannot be written to, so a game that grants one has run out of the command memory it measured for itself: the host's records were larger than the driver's ([Model heaps sized by the host's command record sizes](memory.md#model-heaps-sized-by-the-hosts-command-record-sizes)). That is fixed in the record sizes; the host does not try to recycle these segments by guessing which recording a grant replaces.
- **Fix** (`hle_nvn.c`): the pool now grows on demand (256 KB segments, abort only beyond 1 GB; `hostseg_take`). A segment is released only when another recording writes records over the guest bytes of the recording that overflowed into it (`hostseg_written`; a grant alone does not release it: [A command memory grant over a list whose tail is lent](#a-command-memory-grant-over-a-list-whose-tail-is-lent)): the game records setup blocks once and keeps `CallCommands`-ing them for many frames, so recycling segments after a fixed number of frames would overwrite live recordings. An empty-pool grant of at most 4 KB outside a recording gets a 4 KB segment, a record larger than 256 KB gets a segment that holds it, and the cap counts every segment's bytes (`hostseg_size`). Such a grant has no guest range, so its segment is never reclaimed. On hardware a null pool cannot be written to, so a game that grants one has run out of the command memory it measured for itself: the host's records were larger than the driver's ([Model heaps sized by the host's command record sizes](memory.md#model-heaps-sized-by-the-hosts-command-record-sizes)). That is fixed in the record sizes; the host does not try to recycle these segments by guessing which recording a grant replaces.
- **Found on:** [Super Mario Bros. Wonder](../../dm/games/010015100B514000/README.md) (the many small command buffers: its opening course, per the comment at `HOSTSEG_SZ` in `hle_nvn.c`), [Super Mario Odyssey](../../dm/games/0100000000010000/README.md) (it hands out many 512-byte grants before recording, and with no recording to reclaim them by, 256 KB each reached the cap entering the Wooded Kingdom: the 4 KB segments; 1.4.1 still reached it after 11 minutes of play, about 260,000 4 KB segments, because its display-list heap was full and each re-recording took a new one; fixed by the cause, [Model heaps sized by the host's command record sizes](memory.md#model-heaps-sized-by-the-hosts-command-record-sizes)).
### A command memory grant over a list whose tail is lent
- **Symptom:** a recorded list that the game keeps calling replays another list's records from some point on: wrong uniform buffers or state bound, a scene that alternates between right and wrong lighting frame by frame. Only once the host lends segments (the line `the host lends a segment` in the log).
- **Cause:** the host freed a lent segment as soon as the game granted command memory overlapping the guest bytes of the recording that ran into it. A grant is not a write: games grant at least 512 bytes for a list measured smaller and write only its records (Tears of the Kingdom's model lists, `max(S, 512)`, in `t_nvn`'s fixed-slot test; Splatoon Raiders' agl does the same), so the grant for one list can cover the next list, which is still called. Its tail went to the next command buffer that ran short and was recorded over.
- **Fix** (`hle_nvn.c`): a segment is freed when another recording writes records over its recording's guest bytes (`hostseg_written`, at each piece of records a recording closes: `ctl_chunk_close` and `cmd_alloc`); a recording's own segments carry its serial (`cmd_t.rec_id`) and its own writes leave them. With no segment lent, the check returns at once. `t_nvn` ("live list keeps its lent segment after a wider grant", "re-recording A in place frees its segment").
- **Found on:** [Splatoon Raiders](../../dm/games/0400380020056000/README.md), whose agl grants more command memory than it writes; with records large enough to run short, the terrain's 4,672-byte lighting buffer would be bound as a 256-byte material buffer on alternate frames. This host lends Raiders no segment (21,300 frames from a new save on 2026-11-14), and no logged run of another game lent one from a game pool, so the fix changed no picture here.
### What `GetCommandMemoryUsed` counts
Games read this count as a position in their own command memory and size their heaps and slots from it, so it must be what NVN counts.