asahi: Make BO import path failures more robust
authorAsahi Lina <lina@asahilina.net>
Wed, 29 Mar 2023 09:48:53 +0000 (18:48 +0900)
committerMarge Bot <emma+marge@anholt.net>
Fri, 7 Apr 2023 03:23:04 +0000 (03:23 +0000)
These operations can fail for complex reasons through no fault of mesa,
so we should have proper runtime checks for them even in release builds.

Signed-off-by: Asahi Lina <lina@asahilina.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22353>

src/asahi/lib/agx_device.c

index 959b03c..f4effe2 100644 (file)
@@ -148,7 +148,11 @@ agx_bo_import(struct agx_device *dev, int fd)
    pthread_mutex_lock(&dev->bo_map_lock);
 
    ret = drmPrimeFDToHandle(dev->fd, fd, &gem_handle);
-   assert(!ret);
+   if (ret) {
+      fprintf(stderr, "import failed: Could not map fd %d to handle\n", fd);
+      pthread_mutex_unlock(&dev->bo_map_lock);
+      return NULL;
+   }
 
    bo = agx_lookup_bo(dev, gem_handle);
    dev->max_handle = MAX2(dev->max_handle, gem_handle);
@@ -170,9 +174,9 @@ agx_bo_import(struct agx_device *dev, int fd)
             stderr,
             "import failed: BO is not a multiple of the page size (0x%llx bytes)\n",
             (long long)bo->size);
-         pthread_mutex_unlock(&dev->bo_map_lock);
-         return NULL;
+         goto error;
       }
+
       bo->flags = AGX_BO_SHARED | AGX_BO_SHAREABLE;
       bo->handle = gem_handle;
       bo->prime_fd = dup(fd);
@@ -186,10 +190,21 @@ agx_bo_import(struct agx_device *dev, int fd)
          &dev->main_heap, bo->size + dev->guard_size, dev->params.vm_page_size);
       simple_mtx_unlock(&dev->vma_lock);
 
+      if (!bo->ptr.gpu) {
+         fprintf(
+            stderr,
+            "import failed: Could not allocate from VMA heap (0x%llx bytes)\n",
+            (long long)bo->size);
+         abort();
+      }
+
       ret =
          agx_bo_bind(dev, bo, bo->ptr.gpu, ASAHI_BIND_READ | ASAHI_BIND_WRITE);
-      assert(!ret);
-
+      if (ret) {
+         fprintf(stderr, "import failed: Could not bind BO at 0x%llx\n",
+                 (long long)bo->ptr.gpu);
+         abort();
+      }
    } else {
       /* bo->refcnt == 0 can happen if the BO
        * was being released but agx_bo_import() acquired the
@@ -209,6 +224,11 @@ agx_bo_import(struct agx_device *dev, int fd)
    pthread_mutex_unlock(&dev->bo_map_lock);
 
    return bo;
+
+error:
+   memset(bo, 0, sizeof(*bo));
+   pthread_mutex_unlock(&dev->bo_map_lock);
+   return NULL;
 }
 
 int