diff --git a/runtime/src/vulkan_title_image_pool.h b/runtime/src/vulkan_title_image_pool.h index 94308f6871..146ee65951 100644 --- a/runtime/src/vulkan_title_image_pool.h +++ b/runtime/src/vulkan_title_image_pool.h @@ -3,11 +3,13 @@ #include #include +#include #include #include #include #include #include +#include #include namespace leonida { @@ -19,13 +21,16 @@ struct TitleImagePage { VkDeviceMemory memory{}; PFN_vkFreeMemory free_memory{}; std::uint32_t type = 0, capacity = 0, allocated = 0; + std::atomic free_slots{0}; std::mutex mutex; std::vector returned; ~TitleImagePage() { - if (memory) free_memory(device, memory, nullptr); + if (memory && free_memory) free_memory(device, memory, nullptr); } + bool has_free() const { return free_slots.load(std::memory_order_acquire) != 0; } + bool take(std::uint32_t& slot) { std::lock_guard lock(mutex); if (!returned.empty()) { @@ -36,12 +41,14 @@ struct TitleImagePage { } else { return false; } + free_slots.fetch_sub(1, std::memory_order_relaxed); return true; } void release(std::uint32_t slot) { std::lock_guard lock(mutex); returned.push_back(slot); // the page reserves capacity at creation + free_slots.fetch_add(1, std::memory_order_release); } }; @@ -56,7 +63,7 @@ struct PooledTitleImage { PFN_vkDestroyImage destroy_image{}; ~PooledTitleImage() { - if (image) destroy_image(page->device, image, nullptr); + if (image && page && destroy_image) destroy_image(page->device, image, nullptr); if (page) page->release(slot); } }; @@ -70,6 +77,8 @@ class TitleImagePool { PFN_vkBindImageMemory bind_image, PFN_vkDestroyImage destroy_image, PFN_vkFreeMemory free_memory, Allocate allocate) { constexpr VkDeviceSize largest_slot = VkDeviceSize{2} << 20; + if (!image || !bind_image || !destroy_image || !free_memory) return {}; + if (req.alignment && !std::has_single_bit(req.alignment)) return {}; const VkDeviceSize needed = std::max(req.size, req.alignment); if (!needed || needed > largest_slot || !req.memoryTypeBits) return {}; // Eight size classes per power of two keep padding below one eighth (besides required alignment). @@ -77,31 +86,51 @@ class TitleImagePool { const VkDeviceSize stride = (needed + quantum - 1) & ~(quantum - 1); const VkDeviceSize page_size = std::min(VkDeviceSize{8} << 20, 256 * stride); auto allocation = std::make_unique(); - std::lock_guard lock(mutex_); - auto& pages = classes_[stride]; - std::erase_if(pages, [](const auto& page) { return page.expired(); }); - for (const auto& weak : pages) { - auto page = weak.lock(); - if (page && (req.memoryTypeBits & (1u << page->type)) && page->take(allocation->slot)) { - allocation->page = std::move(page); - break; + { + std::lock_guard lock(mutex_); + if (const auto it = classes_.find(stride); it != classes_.end()) { + auto& pages = it->second; + std::erase_if(pages, [](const auto& page) { return page.expired(); }); + for (const auto& weak : pages) { + auto page = weak.lock(); + if (page && page->type < 32u && (req.memoryTypeBits & (1u << page->type)) && + page->has_free() && page->take(allocation->slot)) { + allocation->page = std::move(page); + break; + } + } + if (pages.empty()) classes_.erase(it); } } + bool created_page = false; if (!allocation->page) { auto page = std::make_shared(); page->device = device; page->free_memory = free_memory; page->capacity = static_cast(page_size / stride); + if (!page->capacity) return {}; + page->free_slots.store(page->capacity, std::memory_order_relaxed); page->returned.reserve(page->capacity); + // Allocate outside mutex_ so vkAllocateMemory never stalls concurrent binds on other classes/pages. page->memory = allocate(page_size, page->type); - if (!page->memory) return {}; - pages.push_back(page); - page->take(allocation->slot); + if (!page->memory || page->type >= 32u || !(req.memoryTypeBits & (1u << page->type))) return {}; + if (!page->take(allocation->slot)) return {}; allocation->page = std::move(page); + created_page = true; } if (bind_image(device, image, allocation->page->memory, stride * allocation->slot) != VK_SUCCESS) return {}; allocation->destroy_image = destroy_image; allocation->image = image; + if (created_page) { + // Publish only after bind succeeded; if weak_ptr storage hits OOM, keep the already-bound image intact. + try { + std::lock_guard lock(mutex_); + auto& pages = classes_[stride]; + std::erase_if(pages, [](const auto& page) { return page.expired(); }); + pages.push_back(allocation->page); + } catch (const std::bad_alloc&) { + } + } return allocation; } diff --git a/runtime/test/vulkan_title_image_pool_test.cpp b/runtime/test/vulkan_title_image_pool_test.cpp index 9666e70c02..efa1b92b49 100644 --- a/runtime/test/vulkan_title_image_pool_test.cpp +++ b/runtime/test/vulkan_title_image_pool_test.cpp @@ -2,18 +2,47 @@ #define VK_NO_PROTOTYPES #include "../src/vulkan_title_image_pool.h" +#include +#include +#include #include +#include +#include +#include namespace { -unsigned allocated, freed, destroyed, next_type; +std::atomic allocated{0}, freed{0}, destroyed{0}; +unsigned next_type; bool fail_alloc, fail_bind; VkDeviceSize last_offset; + +// Optional hook invoked during bind_image for concurrency testing. +std::mutex hook_mutex; +std::condition_variable hook_cv; +std::atomic pause_in_bind{false}; +bool entered_bind = false; +bool resume_bind = false; + VKAPI_ATTR VkResult VKAPI_CALL bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize offset) { last_offset = offset; + if (pause_in_bind.load(std::memory_order_acquire)) { + std::unique_lock lock(hook_mutex); + if (pause_in_bind.load(std::memory_order_relaxed) && !entered_bind) { + entered_bind = true; + hook_cv.notify_all(); + hook_cv.wait_for(lock, std::chrono::seconds(2), [] { return resume_bind; }); + } + } return fail_bind ? VK_ERROR_OUT_OF_DEVICE_MEMORY : VK_SUCCESS; } +VKAPI_ATTR VkResult VKAPI_CALL noop_bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize) { return VK_SUCCESS; } VKAPI_ATTR void VKAPI_CALL destroy_image(VkDevice, VkImage, const VkAllocationCallbacks*) { ++destroyed; } VKAPI_ATTR void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) { ++freed; } + +VKAPI_ATTR VkResult VKAPI_CALL stress_bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize offset) { + // Reject every 17th bind to exercise slot return and fresh-page cleanup under contention. + return (offset / 4096) % 17 == 16 ? VK_ERROR_OUT_OF_DEVICE_MEMORY : VK_SUCCESS; +} #define CHECK(cond) do { if (!(cond)) { std::fprintf(stderr, "failed at line %d: %s\n", __LINE__, #cond); return 1; } } while (false) } @@ -67,5 +96,190 @@ int main() { a = make(); b = make(); CHECK(a && b && a->page == b->page && last_offset % req.alignment == 0); CHECK(last_offset >= req.size && last_offset <= req.size + req.size / 8); - std::puts("title image pool alignment, type separation, retirement, reuse and fallback OK"); + a.reset(); b.reset(); + CHECK(freed == 5 && destroyed == 12); + + // Guard null handles/procs and non-power-of-two alignments without calling allocate. + { + const unsigned alloc_before = allocated.load(); + const VkImage valid_img = (VkImage)(std::uintptr_t(++next_image)); + const VkMemoryRequirements good_req{4096, 4096, 1}; + const auto no_alloc = [](VkDeviceSize, std::uint32_t& type) -> VkDeviceMemory { + type = 0; + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }; + CHECK(!pool.bind(VK_NULL_HANDLE, VK_NULL_HANDLE, good_req, bind_image, destroy_image, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, good_req, nullptr, destroy_image, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, good_req, bind_image, nullptr, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, good_req, bind_image, destroy_image, nullptr, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, VkMemoryRequirements{4096, 3, 1}, + bind_image, destroy_image, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, VkMemoryRequirements{4096, 1000, 1}, + bind_image, destroy_image, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, VkMemoryRequirements{0, 0, 1}, + bind_image, destroy_image, free_memory, no_alloc)); + CHECK(!pool.bind(VK_NULL_HANDLE, valid_img, VkMemoryRequirements{4096, 4096, 0}, + bind_image, destroy_image, free_memory, no_alloc)); + CHECK(allocated.load() == alloc_before); + } + + // Out-of-range (>= 32) or incompatible memory type returned by allocate() must fail and free memory (no UB/leak). + { + const unsigned alloc_before = allocated.load(); + const unsigned freed_before = freed.load(); + const unsigned destroyed_before = destroyed.load(); + req = {4096, 4096, 2u}; // only memory type 1 allowed + next_type = 32; // out-of-range: would shift UB on (1u << 32) + CHECK(!make()); + CHECK(allocated.load() == alloc_before + 1 && freed.load() == freed_before + 1); + next_type = 0; // incompatible with memoryTypeBits == 2 + CHECK(!make()); + CHECK(allocated.load() == alloc_before + 2 && freed.load() == freed_before + 2); + CHECK(destroyed.load() == destroyed_before); + // Subsequent valid allocation succeeds on a fresh page (neither bad page was published). + next_type = 1; + auto ok = make(); + CHECK(ok && ok->page->type == 1 && allocated.load() == alloc_before + 3); + ok.reset(); + CHECK(freed.load() == freed_before + 3 && destroyed.load() == destroyed_before + 1); + } + + // Failed bind_image on a fresh page must immediately free the page without publishing it. + { + const unsigned alloc_before = allocated.load(); + const unsigned freed_before = freed.load(); + req = {8192, 4096, 1u}; + next_type = 0; + fail_bind = true; + CHECK(!make()); + CHECK(allocated.load() == alloc_before + 1 && freed.load() == freed_before + 1); + fail_bind = false; + } + + // Concurrency proof 1: mutex_ is NOT held across allocate() — a second thread completes bind() while + // thread 1 is paused inside allocate(). (Holding mutex_ across allocate() would stall/deadlock here.) + { + std::mutex m; + std::condition_variable cv; + bool in_allocate = false; + bool other_finished = false; + std::unique_ptr first_img; + std::thread t1([&] { + const VkMemoryRequirements r1{16384, 4096, 1u}; + const auto img1 = (VkImage)(std::uintptr_t(10001)); + first_img = pool.bind(VK_NULL_HANDLE, img1, r1, noop_bind_image, destroy_image, free_memory, + [&](VkDeviceSize, std::uint32_t& type) { + type = 0; + std::unique_lock lk(m); + in_allocate = true; + cv.notify_all(); + cv.wait_for(lk, std::chrono::seconds(2), [&] { return other_finished; }); + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }); + }); + { + std::unique_lock lk(m); + CHECK(cv.wait_for(lk, std::chrono::seconds(2), [&] { return in_allocate; })); + } + const VkMemoryRequirements r2{32768, 4096, 1u}; + const auto img2 = (VkImage)(std::uintptr_t(10002)); + auto second_img = pool.bind(VK_NULL_HANDLE, img2, r2, noop_bind_image, destroy_image, free_memory, + [](VkDeviceSize, std::uint32_t& type) { + type = 0; + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }); + CHECK(second_img != nullptr); + { + std::lock_guard lk(m); + other_finished = true; + } + cv.notify_all(); + t1.join(); + CHECK(first_img != nullptr); + first_img.reset(); + second_img.reset(); + } + + // Concurrency proof 2: mutex_ is NOT held across bind_image() — a second thread completes bind() while + // thread 1 is paused inside bind_image(). + { + { + std::lock_guard lk(hook_mutex); + entered_bind = false; + resume_bind = false; + } + pause_in_bind.store(true, std::memory_order_release); + std::unique_ptr first_img; + std::thread t1([&] { + const VkMemoryRequirements r1{65536, 4096, 1u}; + const auto img1 = (VkImage)(std::uintptr_t(20001)); + first_img = pool.bind(VK_NULL_HANDLE, img1, r1, bind_image, destroy_image, free_memory, + [](VkDeviceSize, std::uint32_t& type) { + type = 0; + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }); + }); + { + std::unique_lock lk(hook_mutex); + CHECK(hook_cv.wait_for(lk, std::chrono::seconds(2), [] { return entered_bind; })); + } + const VkMemoryRequirements r2{65536, 4096, 1u}; + const auto img2 = (VkImage)(std::uintptr_t(20002)); + auto second_img = pool.bind(VK_NULL_HANDLE, img2, r2, noop_bind_image, destroy_image, free_memory, + [](VkDeviceSize, std::uint32_t& type) { + type = 0; + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }); + CHECK(second_img != nullptr); + { + std::lock_guard lk(hook_mutex); + resume_bind = true; + } + hook_cv.notify_all(); + t1.join(); + pause_in_bind.store(false, std::memory_order_release); + CHECK(first_img != nullptr); + first_img.reset(); + second_img.reset(); + } + + // Multi-threaded TSan stress: concurrent binds, full-page lock-free skips, retirements, and bind failures. + { + const unsigned alloc_before = allocated.load(); + const unsigned freed_before = freed.load(); + const unsigned destroyed_before = destroyed.load(); + std::atomic succeeded{0}; + std::atomic img_id{30000}; + std::vector workers; + for (int t = 0; t < 4; ++t) { + workers.emplace_back([&, t] { + std::vector> held; + held.reserve(32); + const VkDeviceSize sizes[3] = {4096, 65536, 512u << 10}; + for (int i = 0; i < 120; ++i) { + const std::uint32_t mtype = static_cast((i + t) & 1); + const VkMemoryRequirements r{sizes[(i + t) % 3], 4096, 1u << mtype}; + const auto img = (VkImage)(img_id.fetch_add(1, std::memory_order_relaxed)); + auto p = pool.bind(VK_NULL_HANDLE, img, r, stress_bind_image, destroy_image, free_memory, + [mtype](VkDeviceSize, std::uint32_t& type) { + type = mtype; + return (VkDeviceMemory)(std::uintptr_t(++allocated)); + }); + if (p) { + ++succeeded; + held.push_back(std::move(p)); + if (held.size() > 16) { + held.erase(held.begin()); + } + } + } + }); + } + for (auto& w : workers) w.join(); + CHECK(allocated.load() - alloc_before > 0); + CHECK(freed.load() - freed_before == allocated.load() - alloc_before); + CHECK(destroyed.load() - destroyed_before == succeeded.load()); + } + + std::puts("title image pool alignment, type separation, retirement, reuse, concurrency and guards OK"); }