Module: Mesa
Branch: main
Commit: 1eb2f0d41e14fa12f0b47129b82fd58a2cd25ea2
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=1eb2f0d41e14fa12f0b47129b82fd58a2cd25ea2

Author: Mike Blumenkrantz <[email protected]>
Date:   Wed Nov 17 16:42:50 2021 -0500

zink: demote BAR allocations to device-local on oom

ideally this shouldn't happen, but it's better than crashing even if
it may crash later from attempting to map

Reviewed-by: Dave Airlie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13849>

---

 src/gallium/drivers/zink/zink_bo.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/gallium/drivers/zink/zink_bo.c 
b/src/gallium/drivers/zink/zink_bo.c
index 862369ec2ec..083d6d9fa50 100644
--- a/src/gallium/drivers/zink/zink_bo.c
+++ b/src/gallium/drivers/zink/zink_bo.c
@@ -234,7 +234,7 @@ bo_create_internal(struct zink_screen *screen,
                    unsigned flags,
                    const void *pNext)
 {
-   struct zink_bo *bo;
+   struct zink_bo *bo = NULL;
    bool init_pb_cache;
 
    /* too big for vk alloc */
@@ -247,6 +247,7 @@ bo_create_internal(struct zink_screen *screen,
    mai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    mai.pNext = pNext;
    mai.allocationSize = size;
+demote:
    mai.memoryTypeIndex = screen->heap_map[heap];
    if (screen->info.mem_props.memoryTypes[mai.memoryTypeIndex].propertyFlags & 
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
       alignment = MAX2(alignment, 
screen->info.props.limits.minMemoryMapAlignment);
@@ -261,21 +262,28 @@ bo_create_internal(struct zink_screen *screen,
    /* all non-suballocated bo can cache */
    init_pb_cache = !pNext;
 
-   bo = CALLOC(1, sizeof(struct zink_bo) + init_pb_cache * sizeof(struct 
pb_cache_entry));
+   if (!bo)
+      bo = CALLOC(1, sizeof(struct zink_bo) + init_pb_cache * sizeof(struct 
pb_cache_entry));
    if (!bo) {
       return NULL;
    }
 
+   VkResult ret = VKSCR(AllocateMemory)(screen->dev, &mai, NULL, &bo->mem);
+   if (!zink_screen_handle_vkresult(screen, ret)) {
+      if (heap == ZINK_HEAP_DEVICE_LOCAL_VISIBLE) {
+         heap = ZINK_HEAP_DEVICE_LOCAL;
+         mesa_loge("zink: %p couldn't allocate memory! from BAR heap: retrying 
as device-local", bo);
+         goto demote;
+      }
+      mesa_loge("zink: couldn't allocate memory! from heap %u", heap);
+      goto fail;
+   }
+
    if (init_pb_cache) {
       bo->u.real.use_reusable_pool = true;
       pb_cache_init_entry(&screen->pb.bo_cache, bo->cache_entry, &bo->base, 
heap);
    }
 
-   VkResult ret = VKSCR(AllocateMemory)(screen->dev, &mai, NULL, &bo->mem);
-   if (!zink_screen_handle_vkresult(screen, ret)) {
-      mesa_loge("zink: couldn't allocate memory!");
-      goto fail;
-   }
 
    simple_mtx_init(&bo->lock, mtx_plain);
    pipe_reference_init(&bo->base.reference, 1);

Reply via email to