From f22a100ef024a98245bb19f09e9cba18ce2d9908 Mon Sep 17 00:00:00 2001 From: umceko Date: Sun, 12 Jul 2026 05:32:30 +0300 Subject: [PATCH] Fix main queue synchronization with monado copy_mutex duplicated the pthread_mutex_t of vk.queue.mutex into main_queue->mutex by value, which creates an independent mutex: monado submissions on IPC client threads (comp_swapchain image transitions, vkDeviceWaitIdle in image_cleanup) did not exclude wivrn's layer_commit and encoder submissions on the same VkQueue. This intermittently corrupts driver state when a client creates or destroys swapchains while streaming, observed as SIGSEGV inside vkQueueSubmit2 and as uncaught vk::SystemError from layer_commit on NVIDIA. Initialize monado's queue mutexes and redirect vk.queue.mutex to the main queue os_mutex so every submitter takes the same lock. --- server/compositor/compositor.cpp | 16 ++++---------- server/utils/wivrn_vk_bundle.h | 38 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/server/compositor/compositor.cpp b/server/compositor/compositor.cpp index 74d9621c..d8eba3f9 100644 --- a/server/compositor/compositor.cpp +++ b/server/compositor/compositor.cpp @@ -81,17 +81,6 @@ struct method_trait namespace { -os_mutex copy_mutex(std::mutex & m) -{ - return { - .mutex = *m.native_handle(), -#ifndef NDEBUG - .initialized = true, - .recursive = false, -#endif - }; -} - const comp_swapchain_image & get_layer_image(const comp_layer & layer, uint32_t swapchain_index, uint32_t image_index) { return reinterpret_cast(comp_layer_get_swapchain(&layer, swapchain_index))->images[image_index]; @@ -723,8 +712,11 @@ compositor::compositor(wivrn_session & session) : // vk_init_from_given assumes a graphics queue was provided c_base->vk.graphics_queue = nullptr; + // Monado submits to the main queue from IPC client threads under + // main_queue->mutex: our own submissions must take the same lock. + vk::detail::resultCheck(vk::Result(vk_init_mutex(&c_base->vk)), "vk_init_mutex"); if (c_base->vk.main_queue) - c_base->vk.main_queue->mutex = copy_mutex(vk.queue.mutex); + vk.queue.mutex.share(c_base->vk.main_queue->mutex.mutex); { comp_vulkan_formats formats{}; diff --git a/server/utils/wivrn_vk_bundle.h b/server/utils/wivrn_vk_bundle.h index 572bec6c..3fcd5d57 100644 --- a/server/utils/wivrn_vk_bundle.h +++ b/server/utils/wivrn_vk_bundle.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -62,9 +63,44 @@ uint64_t vk_handle(const T & handle) struct vk_bundle { + // Lockable that can be redirected to a pthread_mutex_t owned by monado, + // so that all VkQueue submitters take the same lock. + class queue_mutex + { + pthread_mutex_t storage = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_t * mutex = &storage; + + public: + queue_mutex() = default; + queue_mutex(const queue_mutex &) = delete; + queue_mutex & operator=(const queue_mutex &) = delete; + ~queue_mutex() + { + pthread_mutex_destroy(&storage); + } + void lock() + { + pthread_mutex_lock(mutex); + } + bool try_lock() + { + return pthread_mutex_trylock(mutex) == 0; + } + void unlock() + { + pthread_mutex_unlock(mutex); + } + // external must be initialized, not currently locked, and + // outlive the last lock() + void share(pthread_mutex_t & external) + { + mutex = &external; + } + }; + struct queue_data { - std::mutex mutex; + queue_mutex mutex; vk::raii::Queue queue = nullptr; uint32_t family_index = vk::QueueFamilyIgnored; operator bool() const