From be7a46c3c5cadaa5d9f593832f4ca8fc0033d0a0 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: (cherry picked from commit f3de6f17c180dc31d21daea08a8f987d273481c3) --- .pick_status.json | 2 +- src/virtio/vulkan/vn_image.c | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index 42e1020dee1..602a476c362 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -9394,7 +9394,7 @@ "description": "venus: fix image reqs cache store locking", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "b51ff22fbe8c9786f968e102ef8d223f415fcaba", "notes": null diff --git a/src/virtio/vulkan/vn_image.c b/src/virtio/vulkan/vn_image.c index e7a5a791895..ccb7ca0ad26 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); }