From 36a83bd78fcacdca0fd69311869840f463f58cf6 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 7 Nov 2026 20:05:49 +0300 Subject: [PATCH] Keep pool memory and texture records until the submission being recorded is done vk_pool_fini freed import, physical and virtual pools right after vkQueueWaitIdle, which covers submitted work only; with the synchronous replay another game thread may be recording a submission that names the pool. Every pool kind now retires through the completion queue at the later of the recording's value and the next one. destroy_tex freed the texture record while other threads still held &e->pub (render targets for a pass, storage bindings writing wr_fence through it): they read a stale VkImage and cached views by that handle. The record is now freed with its image's stamp (vk_defer_free), and views of a dropped entry are deferred at once instead of cached. Co-Authored-By: Claude Opus 5.5 --- runtime/vk.h | 6 ++++++ runtime/vk_exec.c | 1 + runtime/vk_exec_draw.c | 18 +++++++++++----- runtime/vk_hdr.c | 2 +- runtime/vk_memory.c | 48 ++++++++++++++++++++++++++++++------------ runtime/vk_texture.c | 8 ++++++- 6 files changed, 63 insertions(+), 20 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 0dfd738d..91a67370 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -329,6 +329,9 @@ void vk_defer_image(VkImage img, VmaAllocation alloc); // alloc NULL: vkDestro void vk_defer_view(VkImageView view); void vk_exec_image_gone(VkImage image); // destroy_tex, texture lock held: the replay's views of image go with it void vk_defer_buffer(VkBuffer buf, VmaAllocation alloc); +// free(p) once the GPU is past the submission being recorded, like the deferrals above: host records that a recording +// may still read through a pointer taken before another thread dropped them (vk_texture.c destroy_tex). +void vk_defer_free(void *p); // Device-local scratch for the upload batch (storage, transfer src and dst), from a pool of its own (vk_memory.c). VkResult vk_scratch_buffer(VkDeviceSize size, VkBuffer *buf, VmaAllocation *alloc); // Staging and scratch bytes the open upload batch holds until it completes. Large texture loads submit the batch early @@ -414,6 +417,9 @@ typedef struct { uint8_t scale; // 1, or TOTK_SHADOW_SCALE for a shadow map made larger than the game's (vk_texture.c // shadow_scale_of): width and height above are the larger ones, the game's regions // are multiplied by it + uint8_t gone; // the entry was dropped (vk_texture.c destroy_tex, under the texture lock): image and + // record live until the submission being recorded is done, and nothing may be cached + // by its image handle, which the driver can hand out again after that } VkTexImage; // The window texture the backend presents (vk_backend.c): *image and *format become the float picture the game copied // into it, when the last draw into it was that copy and the swapchain is HDR10. diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index b35831c8..6408d2be 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -183,6 +183,7 @@ static void memory_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 src, VkAcce // be made (logged). Replay thread. static int bc_standin(const VkTexImage *img, uint32_t level, VkFormat fmt, BcStandIn *out) { static int warned; + if (img->gone) return 0; // dropped since the lookup: a stand-in kept by its handle would outlive it plat_mutex_lock(&bcs_lock); int full = nbcs == BC_STANDINS; for (int i = 0; i < nbcs; i++) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index eee270c9..7bede297 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -593,6 +593,10 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v, if (img->block_views) ci.pNext = &use; // the image's COLOR_ATTACHMENT usage is its block views' (vk_texture.c) VkImageView view; if (vkCreateImageView(vk_ctx()->device, &ci, NULL, &view) != VK_SUCCESS) return VK_NULL_HANDLE; + if (img->gone) { // dropped since the lookup: its views were purged and a new one would outlive the image + vk_defer_view(view); + return view; + } ViewNode *n = malloc(sizeof *n); if (!n) rt_fatal("vk: out of memory"); *n = (ViewNode){nvn_hm_get(&views_by_image, &img->image), k, view}; @@ -619,11 +623,15 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV if (blocks) ci.pNext = &use; // not the BC image's sampling and transfer usages if (vkCreateImageView(vk_ctx()->device, &ci, NULL, &view) != VK_SUCCESS) rt_fatal("vk: render target view (format %d) could not be created", (int)format); - ViewNode *n = malloc(sizeof *n); - if (!n) rt_fatal("vk: out of memory"); - *n = (ViewNode){nvn_hm_get(&views_by_image, &img->image), k, view}; - nvn_hm_put(&views_by_image, &img->image, n); - nvn_hm_put(&view_of, &k, (void *)view); + if (img->gone) { // dropped since resolve_target (get_view) + vk_defer_view(view); + } else { + ViewNode *n = malloc(sizeof *n); + if (!n) rt_fatal("vk: out of memory"); + *n = (ViewNode){nvn_hm_get(&views_by_image, &img->image), k, view}; + nvn_hm_put(&views_by_image, &img->image, n); + nvn_hm_put(&view_of, &k, (void *)view); + } } vk_texture_unlock(); return view; diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c index e8480bb2..806ffd1b 100644 --- a/runtime/vk_hdr.c +++ b/runtime/vk_hdr.c @@ -272,7 +272,7 @@ void vk_hdr_draw(VkExec *ex, const VkPipeInfo *info) { } if (ntex == 1 && slot >= 0 && slot < 128 && ex->tex[1][slot]) { const VkTexImage *s = vk_texture_by_id(NVN_HANDLE_TEX(ex->tex[1][slot]), NULL, NULL); - if (s && is_float(s->format) && s->width == t->width && s->height == t->height && s->samples == 1) + if (s && !s->gone && is_float(s->format) && s->width == t->width && s->height == t->height && s->samples == 1) src = s->image, fmt = s->format; } plat_mutex_lock(&rec_lock); diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 22b04e1c..3e30682f 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -78,8 +78,9 @@ static int shadow_on; // shadow mode: imported pools get shad static int shadow_pool(VkCtx *c, PoolRec *r, uint64_t addr, uint64_t size); static void shadow_touch(PoolRec *r); static void shadow_forget(PoolRec *r); -static void mirror_retiring(PoolRec *r); // vk_pool_fini: kept until mirror_retire (or vk_mem_shutdown) -static void mirror_retire(void *arg); // completion thread: destroys a finalized mirrored pool's record +static void pool_retiring(PoolRec *r); // vk_pool_fini: kept until pool_retire (or vk_mem_shutdown) +static void pool_retire(void *arg); // completion thread: destroys a finalized pool's record +static uint64_t retire_value(void); // the timeline value after which a finalized pool's record may go static void grow(void **arr, int *cap, size_t elem); static PlatMutex pool_lock = PLAT_MUTEX_INIT; @@ -333,10 +334,14 @@ void vk_pool_fini(pool_t *p) { vk_queue_unlock(); } vk_mem_collect(); - // A mirrored pool's record and buffers outlive the submission being recorded, whose sync copies and write-backs may - // name them (the game frees a pool only once the GPU is done with it, but its CPU writes may still be queued). - if (r->mbuf) mirror_retiring(r), vk_completion_add(vk_sync_issued() + 1, mirror_retire, r); - else destroy_rec(c, r); + // The record, its buffers and its memory outlive the submission being recorded, of any pool kind. The wait above + // covers submitted work only. With the synchronous replay another game thread may be recording right now, and that + // recording may already name this pool: a mirrored pool's sync copies and write-backs, an import pool's buffer + // through a texture upload or a released cache entry (vk_texture.c slot_resolve), a physical pool's memory under + // the images vk_texture_range_gone just deferred to that submission. The game frees a pool only once its own GPU + // work is done, so only the port's work can name it. Freed here, the submission ran with freed memory. + pool_retiring(r); + vk_completion_add(retire_value(), pool_retire, r); } // Requests were checked by the front end (nvn_api_core.c, MNG totk_vpool.c); apply them in order. @@ -1398,19 +1403,19 @@ static void mirror_forget(PoolRec *r) { } } -// Finalized mirrored pools waiting for mirror_retire; vk_mem_shutdown destroys those whose callback never ran. +// Finalized pools waiting for pool_retire; vk_mem_shutdown destroys those whose callback never ran. static PoolRec **retiring; static int nretiring, cretiring; static PlatMutex retire_lock = PLAT_MUTEX_INIT; -static void mirror_retiring(PoolRec *r) { +static void pool_retiring(PoolRec *r) { plat_mutex_lock(&retire_lock); if (nretiring == cretiring) grow((void **)&retiring, &cretiring, sizeof *retiring); retiring[nretiring++] = r; plat_mutex_unlock(&retire_lock); } -static void mirror_retire(void *arg) { +static void pool_retire(void *arg) { plat_mutex_lock(&retire_lock); for (int i = 0; i < nretiring; i++) if (retiring[i] == arg) { retiring[i] = retiring[--nretiring]; break; } @@ -2163,7 +2168,7 @@ typedef struct { VkDeviceSize held; // staging and scratch bytes freed when the batch completes (vk_upload_held) } Batch; -enum { DEAD_IMAGE = 1, DEAD_IMAGE_VMA, DEAD_VIEW, DEAD_BUFFER_VMA }; +enum { DEAD_IMAGE = 1, DEAD_IMAGE_VMA, DEAD_VIEW, DEAD_BUFFER_VMA, DEAD_HOST }; // DEAD_HOST: free(handle) typedef struct { int kind; uint64_t handle; VmaAllocation alloc; uint64_t seq, tl; } Dead; static PlatRMutex cmd_lock; @@ -2222,6 +2227,10 @@ static void collect_locked(VkCtx *c) { for (int i = 0; i < ndead; i++) { Dead *d = &dead[i]; if (d->seq > done_seq || d->tl > tl) { dead[k++] = *d; continue; } + if (d->kind == DEAD_HOST) { + free((void *)(uintptr_t)d->handle); + continue; + } rec_check_handle(d->kind == DEAD_VIEW ? REC_VIEW : d->kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, d->handle, 1, d->tl, tl); switch (d->kind) { @@ -2424,10 +2433,11 @@ static void defer(int kind, uint64_t handle, VmaAllocation alloc) { // Recomp and Savage Wind Recomp stamp every deferral with the submission being recorded (runtime/nvn/vulkan/ // textures.c defer: vk_pending_seq). uint64_t tl = tl_noted; - if (tl > vk_sync_issued() && tl_noted_here != tl) n_cross_defer++, cross_defer_tl = tl; + if (kind != DEAD_HOST && tl > vk_sync_issued() && tl_noted_here != tl) n_cross_defer++, cross_defer_tl = tl; dead[ndead++] = (Dead){kind, handle, alloc, open_active ? open_b.seq : next_seq, tl}; - rec_check_handle(kind == DEAD_VIEW ? REC_VIEW : kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, handle, 0, tl, - vk_sync_fence_done()); + if (kind != DEAD_HOST) + rec_check_handle(kind == DEAD_VIEW ? REC_VIEW : kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, handle, 0, tl, + vk_sync_fence_done()); plat_rmutex_unlock(&cmd_lock); } @@ -2446,6 +2456,7 @@ void vk_defer_view(VkImageView view) { if (view) defer(DEAD_VIEW, (uint64_t)(uin void vk_defer_buffer(VkBuffer buf, VmaAllocation alloc) { if (buf) defer(DEAD_BUFFER_VMA, (uint64_t)(uintptr_t)buf, alloc); } +void vk_defer_free(void *p) { if (p) defer(DEAD_HOST, (uint64_t)(uintptr_t)p, NULL); } // The device fault report: no lock, since a thread that holds cmd_lock may be waiting for the report's own lock // (vk_upload_flush reports a failed submit with cmd_lock held). @@ -2466,6 +2477,17 @@ void vk_mem_note_fence(uint64_t timeline_value) { plat_rmutex_unlock(&cmd_lock); } +// The later of the submission being recorded (tl_noted, noted from its start: vk_sync.c vk_sync_submit) and the next +// one to be submitted. +static uint64_t retire_value(void) { + plat_once(&cmd_once, cmd_init, NULL); + plat_rmutex_lock(&cmd_lock); + uint64_t v = vk_sync_issued() + 1; + if (tl_noted > v) v = tl_noted; + plat_rmutex_unlock(&cmd_lock); + return v; +} + void vk_mem_collect(void) { VkCtx *c = vk_ctx(); if (!c) return; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 3f72948c..d461f561 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -597,10 +597,16 @@ static void destroy_tex(Tex *e) { if (p->next_at == e) { p->next_at = e->next_at; break; } } if (e->released) nreleased--, rel_unlink(e); + // Another thread may hold &e->pub, taken under the lock and used after it (vk_texture_get, vk_texture_by_id: render + // targets for a whole pass, vk_exec.c resolve_target, vk_texture_storage_bound_locked writes through it). Freed + // here, that thread read a stale VkImage from freed memory, made views of it and cached them by that handle. The + // record goes with the image, once the submission being recorded is done; gone keeps views of it out of the caches. + e->pub.gone = 1; vk_exec_image_gone(e->pub.image); vk_defer_image(e->pub.image, e->alloc); free(e->snap); - free(e); + e->snap = NULL; + vk_defer_free(e); } // Released entries with another descriptor at the same address are replaced; past RELEASED_MAX released entries the