From 632f59fcbfc790a7546ceeac30c480b66901404c Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 21 Jan 2021 11:11:38 +0100 Subject: [PATCH] xf86drm: warn about GEM handle reference counting Users need to be careful when using drmPrimeHandleToFD or drmPrimeFDToHandle directly. Mention GBM as a solution. See [1] for an example mistake. [1]: https://gitlab.freedesktop.org/drm/nouveau/-/issues/43#note_772661 Signed-off-by: Simon Ser Reviewed-by: Daniel Vetter --- xf86drm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/xf86drm.h b/xf86drm.h index 7b85079a..9fc06ab8 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -813,6 +813,24 @@ extern char *drmGetDeviceNameFromFd(int fd); extern char *drmGetDeviceNameFromFd2(int fd); extern int drmGetNodeTypeFromFd(int fd); +/* Convert between GEM handles and DMA-BUF file descriptors. + * + * Warning: since GEM handles are not reference-counted and are unique per + * DRM file description, the caller is expected to perform its own reference + * counting. drmPrimeFDToHandle is guaranteed to return the same handle for + * different FDs if they reference the same underlying buffer object. This + * could even be a buffer object originally created on the same DRM FD. + * + * When sharing a DRM FD with an API such as EGL or GBM, the caller must not + * use drmPrimeHandleToFD nor drmPrimeFDToHandle. A single user-space + * reference-counting implementation is necessary to avoid double-closing GEM + * handles. + * + * Two processes can't share the same DRM FD and both use it to create or + * import GEM handles, even when using a single user-space reference-counting + * implementation like GBM, because GBM doesn't share its state between + * processes. + */ extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd); extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);