From a0587b59e7814b485286967a5a82b87dad2f3ad6 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 08:39:29 +0300 Subject: [PATCH] heap_lazy also commits the chunks a Vulkan import widens into when the driver's host pointer alignment is above 4 KiB, and the memory.c comment names mem_heap_commit for save writes Co-Authored-By: Claude Opus 5.5 --- runtime/memory.c | 4 ++-- runtime/vk_memory.c | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/runtime/memory.c b/runtime/memory.c index 5399432e..9f7c496a 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -210,8 +210,8 @@ static void commit(uint64_t a, uint64_t n, const char *what) { // SetHeapSize adds stays reserved: the first read or write of a reserved page faults, and heap_fault commits its // LAZY_CHUNK and retries. Freshly committed pages read as zero, as the Switch's heap does. Access by the kernel or the GPU // driver does not fault: a file read into the heap, a save written from it and a Vulkan import of it would fail on a -// reserved page. All of them call mem_thaw first, so mem_thaw commits the reserved chunks of its range too -// (heap_commit_range). Linux backs mprotect'ed pages on first use by itself and keeps committing eagerly. +// reserved page. File reads and Vulkan imports call mem_thaw first, so mem_thaw commits the reserved chunks of its +// range too (heap_commit_range); save writes only need the pages to exist and call mem_heap_commit. Linux backs mprotect'ed pages on first use by itself and keeps committing eagerly. // // lazy_map holds one byte per chunk, set once the chunk is committed, so neither path asks the OS for the page state: // a VirtualQuery costs tens of microseconds here, and the write faults of frozen pool regions (thaw_fault) come through diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 5f777b11..c21ae243 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -128,6 +128,9 @@ static int import_pool(VkCtx *c, PoolRec *r, uint64_t addr, uint64_t size) { r->import_lo = addr & ~(al - 1); r->import_hi = (addr + size + al - 1) & ~(al - 1); uint64_t n = r->import_hi - r->import_lo; + // TOTK_HEAP_LAZY: the caller's mem_thaw committed the pool's own chunks; an alignment above 4 KiB can widen the + // import into a neighbouring chunk the game never touched, and the driver refuses reserved pages. + mem_heap_commit(r->import_lo, n); VkMemoryHostPointerPropertiesEXT hp = {.sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; VkResult res = p_host_props ? p_host_props(c->device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, (void *)(uintptr_t)r->import_lo, &hp)