From f3de6f17c180dc31d21daea08a8f987d273481c3 Mon Sep 17 00:00:00 2001 From: Juston Li Date: Fri, 2 Feb 2024 13:29:23 -0800 Subject: [PATCH] venus: fix image reqs cache store locking lock the entire scope when storing image reqs cache entry to prevent entry being added between the split locks. Fixes: b51ff22fbe8 ("venus: support caching image memory requirements") Signed-off-by: Juston Li Part-of: --- src/virtio/vulkan/vn_image.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/virtio/vulkan/vn_image.c b/src/virtio/vulkan/vn_image.c index 6f2f986eaf6..db3e27b3f81 100644 --- a/src/virtio/vulkan/vn_image.c +++ b/src/virtio/vulkan/vn_image.c @@ -231,8 +231,15 @@ vn_image_store_reqs_in_cache(struct vn_device *dev, assert(cache->ht); simple_mtx_lock(&cache->mutex); - uint32_t cache_entry_count = _mesa_hash_table_num_entries(cache->ht); - if (cache_entry_count == IMAGE_REQS_CACHE_MAX_ENTRIES) { + + /* Check if entry was added before lock */ + if (_mesa_hash_table_search(cache->ht, key)) { + simple_mtx_unlock(&cache->mutex); + return; + } + + if (_mesa_hash_table_num_entries(cache->ht) == + IMAGE_REQS_CACHE_MAX_ENTRIES) { /* Evict/use the last entry in the lru list for this new entry */ cache_entry = list_last_entry(&cache->lru, struct vn_image_reqs_cache_entry, head); @@ -242,11 +249,11 @@ vn_image_store_reqs_in_cache(struct vn_device *dev, } else { cache_entry = vk_zalloc(alloc, sizeof(*cache_entry), VN_DEFAULT_ALIGN, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!cache_entry) { + simple_mtx_unlock(&cache->mutex); + return; + } } - simple_mtx_unlock(&cache->mutex); - - if (!cache_entry) - return; for (uint32_t i = 0; i < plane_count; i++) cache_entry->requirements[i] = requirements[i]; @@ -254,12 +261,10 @@ vn_image_store_reqs_in_cache(struct vn_device *dev, memcpy(cache_entry->key, key, SHA1_DIGEST_LENGTH); cache_entry->plane_count = plane_count; - simple_mtx_lock(&cache->mutex); - if (!_mesa_hash_table_search(cache->ht, cache_entry->key)) { - _mesa_hash_table_insert(dev->image_reqs_cache.ht, cache_entry->key, - cache_entry); - list_add(&cache_entry->head, &cache->lru); - } + _mesa_hash_table_insert(dev->image_reqs_cache.ht, cache_entry->key, + cache_entry); + list_add(&cache_entry->head, &cache->lru); + simple_mtx_unlock(&cache->mutex); }