Skip to contentSkip to conversation

drm: document DRM_IOCTL_PRIME_HANDLE_TO_FD and PRIME_FD_TO_HANDLE

PATCH In review Simon Ser 4 participants 9 messages Feb 16, 2023 – Feb 20, 2023

Subsystems (2)
lore
Share & tools
lore

Simon Ser

author
#0 lore raw

PATCH 1/2 drm: document DRM_IOCTL_PRIME_HANDLE_TO_FD and PRIME_FD_TO_HANDLE

Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/uapi/drm/drm.h +17 -0
index 4cb956a52aee..54b2313c8332 100644
@@ -1012,7 +1012,24 @@ extern "C" {
 #define DRM_IOCTL_UNLOCK		DRM_IOW( 0x2b, struct drm_lock)
 #define DRM_IOCTL_FINISH		DRM_IOW( 0x2c, struct drm_lock)
 
+/**
+ * DRM_IOCTL_PRIME_HANDLE_TO_FD - Convert a GEM handle to a DMA-BUF FD.
+ *
+ * User-space sets &drm_prime_handle.handle with the GEM handle to export and
+ * &drm_prime_handle.flags, and gets back a DMA-BUF file descriptor in
+ * &drm_prime_handle.fd.
+ */
 #define DRM_IOCTL_PRIME_HANDLE_TO_FD    DRM_IOWR(0x2d, struct drm_prime_handle)
+/**
+ * DRM_IOCTL_PRIME_FD_TO_HANDLE - Convert a DMA-BUF FD to a GEM handle.
+ *
+ * User-space sets &drm_prime_handle.fd with a DMA-BUF file descriptor to
+ * import, and gets back a GEM handle in &drm_prime_handle.handle.
+ * &drm_prime_handle.flags is unused.
+ *
+ * If an existing GEM handle refers to the memory object backing the DMA-BUF,
+ * that GEM handle is returned.
+ */
 #define DRM_IOCTL_PRIME_FD_TO_HANDLE    DRM_IOWR(0x2e, struct drm_prime_handle)
 
 #define DRM_IOCTL_AGP_ACQUIRE		DRM_IO(  0x30)
Signature · 1 line
2.39.1
author
#1 lore raw

PATCH 2/2 drm: document DRM_IOCTL_GEM_CLOSE

This is a bit tricky, because of the ref'counting considerations. See also [1] for more discussion about this topic. Since this is kernel docs, I've decided to elaborate a bit less on the user-space details.

[1]: https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110

Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/uapi/drm/drm.h +13 -0
index 54b2313c8332..4829f1fa9570 100644
@@ -972,6 +972,19 @@ extern "C" {
 #define DRM_IOCTL_GET_STATS             DRM_IOR( 0x06, struct drm_stats)
 #define DRM_IOCTL_SET_VERSION		DRM_IOWR(0x07, struct drm_set_version)
 #define DRM_IOCTL_MODESET_CTL           DRM_IOW(0x08, struct drm_modeset_ctl)
+/**
+ * DRM_IOCTL_GEM_CLOSE - Close a GEM handle.
+ *
+ * GEM handles are not reference-counted by the kernel. User-space is
+ * responsible for managing their lifetime. For example, if user-space imports
+ * the same memory object twice on the same DRM file description, the same GEM
+ * handle is returned by both imports, and user-space needs to ensure
+ * &DRM_IOCTL_GEM_CLOSE is performed once only. The same situation can happen
+ * when a memory object is allocated, then exported and imported again on the
+ * same DRM file description. The &DRM_IOCTL_MODE_GETFB2 IOCTL is an exception
+ * and always returns fresh new GEM handles even if an existing GEM handle
+ * already refers to the same memory object before the IOCTL is performed.
+ */
 #define DRM_IOCTL_GEM_CLOSE		DRM_IOW (0x09, struct drm_gem_close)
 #define DRM_IOCTL_GEM_FLINK		DRM_IOWR(0x0a, struct drm_gem_flink)
 #define DRM_IOCTL_GEM_OPEN		DRM_IOWR(0x0b, struct drm_gem_open)
Signature · 1 line
2.39.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm: document DRM_IOCTL_GEM_CLOSE

Quoted 25 lines · Simon Ser

This is a bit tricky, because of the ref'counting considerations. See also [1] for more discussion about this topic. Since this is kernel docs, I've decided to elaborate a bit less on the user-space details.

[1]: https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110

Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/uapi/drm/drm.h +2 -0
index 54b2313c8332..4829f1fa9570 100644
@@ -972,6 +972,19 @@ extern "C" {
 #define DRM_IOCTL_GET_STATS             DRM_IOR( 0x06, struct drm_stats)
 #define DRM_IOCTL_SET_VERSION		DRM_IOWR(0x07, struct drm_set_version)
 #define DRM_IOCTL_MODESET_CTL           DRM_IOW(0x08, struct drm_modeset_ctl)
+/**
+ * DRM_IOCTL_GEM_CLOSE - Close a GEM handle.

include/uapi/drm/drm.h extern "C" { · line 986

Quoted 10 lines
include/uapi/drm/drm.h +10 -0
+ *
+ * GEM handles are not reference-counted by the kernel. User-space is
+ * responsible for managing their lifetime. For example, if user-space imports
+ * the same memory object twice on the same DRM file description, the same GEM
+ * handle is returned by both imports, and user-space needs to ensure
+ * &DRM_IOCTL_GEM_CLOSE is performed once only. The same situation can happen
+ * when a memory object is allocated, then exported and imported again on the
+ * same DRM file description. The &DRM_IOCTL_MODE_GETFB2 IOCTL is an exception
+ * and always returns fresh new GEM handles even if an existing GEM handle
+ * already refers to the same memory object before the IOCTL is performed.

I'd duplicate the relevant parts into each ioctl doc too, just to increase the chances people notice these caveats. But that's stuf for these other patches. for this one:

btw do the links from on ioctl text to the other work? -Daniel

Quoted 8 lines
include/uapi/drm/drm.h +1 -0
+ */
 #define DRM_IOCTL_GEM_CLOSE		DRM_IOW (0x09, struct drm_gem_close)
 #define DRM_IOCTL_GEM_FLINK		DRM_IOWR(0x0a, struct drm_gem_flink)
 #define DRM_IOCTL_GEM_OPEN		DRM_IOWR(0x0b, struct drm_gem_open)
Signature · 1 line
2.39.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#3 lore raw

Re: [PATCH 2/2] drm: document DRM_IOCTL_GEM_CLOSE

Quoted 41 lines · On Thu, 16 Feb 2023 20:48:05 +0100 Daniel Vetter <[email protected]> wrote:
Quoted 25 lines · Simon Ser

This is a bit tricky, because of the ref'counting considerations. See also [1] for more discussion about this topic. Since this is kernel docs, I've decided to elaborate a bit less on the user-space details.

[1]: https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110

Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/uapi/drm/drm.h +2 -0
index 54b2313c8332..4829f1fa9570 100644
@@ -972,6 +972,19 @@ extern "C" {
 #define DRM_IOCTL_GET_STATS             DRM_IOR( 0x06, struct drm_stats)
 #define DRM_IOCTL_SET_VERSION		DRM_IOWR(0x07, struct drm_set_version)
 #define DRM_IOCTL_MODESET_CTL           DRM_IOW(0x08, struct drm_modeset_ctl)
+/**
+ * DRM_IOCTL_GEM_CLOSE - Close a GEM handle.

include/uapi/drm/drm.h extern "C" { · line 986

Quoted 10 lines
include/uapi/drm/drm.h +10 -0
+ *
+ * GEM handles are not reference-counted by the kernel. User-space is
+ * responsible for managing their lifetime. For example, if user-space imports
+ * the same memory object twice on the same DRM file description, the same GEM
+ * handle is returned by both imports, and user-space needs to ensure
+ * &DRM_IOCTL_GEM_CLOSE is performed once only. The same situation can happen
+ * when a memory object is allocated, then exported and imported again on the
+ * same DRM file description. The &DRM_IOCTL_MODE_GETFB2 IOCTL is an exception
+ * and always returns fresh new GEM handles even if an existing GEM handle
+ * already refers to the same memory object before the IOCTL is performed.  

I'd duplicate the relevant parts into each ioctl doc too, just to increase the chances people notice these caveats. But that's stuf for these other patches. for this one:

Quoted 4 lines

btw do the links from on ioctl text to the other work? -Daniel

This one

I agree with Daniel's comments to both patches, and nothing else came to my mind.

Thanks,
pq

Quoted 10 lines
Quoted 9 lines
include/uapi/drm/drm.h +1 -0
+ */
 #define DRM_IOCTL_GEM_CLOSE		DRM_IOW (0x09, struct drm_gem_close)
 #define DRM_IOCTL_GEM_FLINK		DRM_IOWR(0x0a, struct drm_gem_flink)
 #define DRM_IOCTL_GEM_OPEN		DRM_IOWR(0x0b, struct drm_gem_open)
Signature · 1 line
2.39.1
author
#4 lore raw

Re: [PATCH 2/2] drm: document DRM_IOCTL_GEM_CLOSE

In reply to Daniel Vetter
Quoted 2 lines · Daniel Vetter

I'd duplicate the relevant parts into each ioctl doc too, just to increase the chances people notice these caveats.

Hm, I'd rather not duplicate, this makes it very easy for each version to go out-of-sync with the rest. I will add links.

Quoted 4 lines

But that's stuf for these other
patches. for this one:

Thanks!

Quoted 1 line

btw do the links from on ioctl text to the other work?

Yes, they work fine.

#5 lore raw
In reply to Simon Ser
Quoted 13 lines · Simon Ser
Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/uapi/drm/drm.h +0 -0
index 4cb956a52aee..54b2313c8332 100644

include/uapi/drm/drm.h extern "C" { · line 1020

Quoted 9 lines
include/uapi/drm/drm.h +6 -0
@@ -1012,7 +1012,24 @@ extern "C" {
 #define DRM_IOCTL_UNLOCK		DRM_IOW( 0x2b, struct drm_lock)
 #define DRM_IOCTL_FINISH		DRM_IOW( 0x2c, struct drm_lock)
 
+/**
+ * DRM_IOCTL_PRIME_HANDLE_TO_FD - Convert a GEM handle to a DMA-BUF FD.
+ *
+ * User-space sets &drm_prime_handle.handle with the GEM handle to export and
+ * &drm_prime_handle.flags, and gets back a DMA-BUF file descriptor in
+ * &drm_prime_handle.fd.

I think it'd be good to mention here that export could fail for any number of driver specific reason, e.g. because the driver doesn't support dma-buf export at all, or it's not supported for this specific gem handle because it's a special one. And therefore generic clients need to be able to cope with some kind of fallback path ideally.

Also I think it'd be good to mention that export support is announced with the DRM_PRIME_CAP_EXPORT flag in the DRM_CAP_PRIME GETCAP ioctl capability.

include/uapi/drm/drm.h extern "C" { · line 1028

Quoted 8 lines
include/uapi/drm/drm.h +7 -0
+ */
 #define DRM_IOCTL_PRIME_HANDLE_TO_FD    DRM_IOWR(0x2d, struct drm_prime_handle)
+/**
+ * DRM_IOCTL_PRIME_FD_TO_HANDLE - Convert a DMA-BUF FD to a GEM handle.
+ *
+ * User-space sets &drm_prime_handle.fd with a DMA-BUF file descriptor to
+ * import, and gets back a GEM handle in &drm_prime_handle.handle.
+ * &drm_prime_handle.flags is unused.

Same here, some mention that import can fail for any reason (many drivers do not support importing to anything else than the exporting device). Also DRM_PRIME_CAP_IMPORT for querying.

include/uapi/drm/drm.h extern "C" { · line 1031

Quoted 3 lines
include/uapi/drm/drm.h +3 -0
+ *
+ * If an existing GEM handle refers to the memory object backing the DMA-BUF,
+ * that GEM handle is returned.

I think we need to state clearly that this means importers who get unknown buffers must have a userspace lookup structure to catch these handle deduplications and refcount objects properly.

With these additions: Acked-by: Daniel Vetter <[email protected]>

Quoted 8 lines
include/uapi/drm/drm.h +1 -0
+ */
 #define DRM_IOCTL_PRIME_FD_TO_HANDLE    DRM_IOWR(0x2e, struct drm_prime_handle)
 
 #define DRM_IOCTL_AGP_ACQUIRE		DRM_IO(  0x30)
Signature · 1 line
2.39.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#6 lore raw
In reply to Simon Ser

Hi Simon

Quoted 21 lines · Simon Ser
Cc Daniel Vetter <[email protected]>
Cc Pekka Paalanen <[email protected]>
Cc Daniel Stone <[email protected]>
---
 include/uapi/drm/drm.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/uapi/drm/drm.h +4 -0
index 4cb956a52aee..54b2313c8332 100644
@@ -1012,7 +1012,24 @@ extern "C" {
 #define DRM_IOCTL_UNLOCK               DRM_IOW( 0x2b, struct drm_lock)
 #define DRM_IOCTL_FINISH               DRM_IOW( 0x2c, struct drm_lock)
 
+/**
+ * DRM_IOCTL_PRIME_HANDLE_TO_FD - Convert a GEM handle to a DMA-BUF FD.
+ *
+ * User-space sets &drm_prime_handle.handle with the GEM handle to export and

include/uapi/drm/drm.h extern "C" { · line 1028

Quoted 10 lines
include/uapi/drm/drm.h +9 -0
+ * &drm_prime_handle.flags, and gets back a DMA-BUF file descriptor in
+ * &drm_prime_handle.fd.
+ */
 #define DRM_IOCTL_PRIME_HANDLE_TO_FD    DRM_IOWR(0x2d, struct drm_prime_handle)
+/**
+ * DRM_IOCTL_PRIME_FD_TO_HANDLE - Convert a DMA-BUF FD to a GEM handle.
+ *
+ * User-space sets &drm_prime_handle.fd with a DMA-BUF file descriptor to
+ * import, and gets back a GEM handle in &drm_prime_handle.handle.
+ * &drm_prime_handle.flags is unused.

Is it worth explicitly stating that the handle would be released via DRM_IOCTL_GEM_CLOSE? I've had userspace developers query how to release imported handles in the past.

Dave

Quoted 11 lines
include/uapi/drm/drm.h +4 -1
+ *
+ * If an existing GEM handle refers to the memory object backing the DMA-BUF,
+ * that GEM handle is returned.
+ */
 #define DRM_IOCTL_PRIME_FD_TO_HANDLE    DRM_IOWR(0x2e, struct drm_prime_handle)
 
 #define DRM_IOCTL_AGP_ACQUIRE          DRM_IO(  0x30)
--

2.39.1

author
#7 lore raw
Quoted 7 lines · Dave Stevenson
Quoted 3 lines

+ * User-space sets &drm_prime_handle.fd with a DMA-BUF file descriptor to
+ * import, and gets back a GEM handle in &drm_prime_handle.handle.
+ * &drm_prime_handle.flags is unused.

Is it worth explicitly stating that the handle would be released via DRM_IOCTL_GEM_CLOSE? I've had userspace developers query how to release imported handles in the past.

v2 spells this out I think.

#8 lore raw
Quoted 7 lines · Simon Ser
Quoted 4 lines · Dave Stevenson
Quoted 3 lines

+ * User-space sets &drm_prime_handle.fd with a DMA-BUF file descriptor to
+ * import, and gets back a GEM handle in &drm_prime_handle.handle.
+ * &drm_prime_handle.flags is unused.

Quoted 5 lines
Quoted 3 lines

Is it worth explicitly stating that the handle would be released via DRM_IOCTL_GEM_CLOSE? I've had userspace developers query how to release imported handles in the past.

v2 spells this out I think.

It does - thanks.
I was reading back through my emails from Friday and the weekend, and hadn't noticed v2 :-/ Sorry for the noise.

Dave