nouveau: add exported GEM handles to the global list
authorDor Askayo <dor.askayo@gmail.com>
Fri, 11 Aug 2023 21:14:23 +0000 (00:14 +0300)
committerMarge Bot <emma+marge@anholt.net>
Wed, 16 Aug 2023 10:28:22 +0000 (10:28 +0000)
Adding GEM handles to the global list is necessary to allow
maintaining a single reference count for handles that are shared
between multiple buffer objects.

Since exported handles can end up being shared with other buffer
objects, as in the case that drmPrimeHandleToFD() and gbm_bo_import()
are called externally to Mesa, they too must be added to the global
list.

Unfortunately, doing this properly requires a new libdrm API. Use
the best possible option for now.

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/9552
Signed-off-by: Dor Askayo <dor.askayo@gmail.com>
Acked-by: Karol Herbst <git@karolherbst.de>
Cc: mesa-stable
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24648>

src/gallium/drivers/nouveau/nouveau_screen.c

index 2b37fa09839ba2ad336c2aec7785d3ca4edbb49f..ed5759231ea6ec3470f2dc8f3c5fb0efcfa77cd8 100644 (file)
@@ -145,6 +145,21 @@ nouveau_screen_bo_get_handle(struct pipe_screen *pscreen,
    if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
       return nouveau_bo_name_get(bo, &whandle->handle) == 0;
    } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
+      int fd;
+      int ret;
+
+      /* The handle is exported in this case, but the global list of
+       * handles is in libdrm and there is no libdrm API to add
+       * handles to the list without additional side effects. The
+       * closest API available also gets a fd for the handle, which
+       * is not necessary in this case. Call it and close the fd.
+       */
+      ret = nouveau_bo_set_prime(bo, &fd);
+      if (ret != 0)
+        return false;
+
+      close(fd);
+
       whandle->handle = bo->handle;
       return true;
    } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {