Skip to contentSkip to conversation

drm/prime: reject DMA-BUF attach when get_sg_table is missing

PATCH In review Simon Ser 4 participants 10 messages Mar 2, 2023 – Mar 20, 2024

Subsystems (2)
lore
Share & tools
lore

Simon Ser

author
#0 lore raw

PATCH v2 1/2 drm/prime: reject DMA-BUF attach when get_sg_table is missing

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
 drivers/gpu/drm/drm_prime.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +5 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  * Optional pinning of buffers is handled at dma-buf attach and detach time in
  * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
  * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected.
  *
  * For kernel-internal access there's drm_gem_dmabuf_vmap() and
  * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
 	struct drm_gem_object *obj = dma_buf->priv;
 
+	if (!obj->funcs->get_sg_table)
+		return -EOPNOTSUPP;
+
 	return drm_gem_pin(obj);
 }
 EXPORT_SYMBOL(drm_gem_map_attach);
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v2 2/2 drm/vram-helper: turn on PRIME import/export

We don't populate drm_driver.gem_prime_import_sg_table so only DMA-BUFs exported from our own device can be imported. We don't populate drm_gem_object_funcs.get_sg_table so DMA-BUFs cannot be imported into another device. Still, this is useful to user-space to share buffers between processes and between API boundaries (e.g. wlroots hard-requires PRIME import/export support).

v2: expand commit message

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
 include/drm/drm_gem_vram_helper.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/drm/drm_gem_vram_helper.h +3 -1
index d3e8920c0b64..f4aab64411d8 100644
@@ -160,7 +160,9 @@ void drm_gem_vram_simple_display_pipe_cleanup_fb(
 	.debugfs_init             = drm_vram_mm_debugfs_init, \
 	.dumb_create		  = drm_gem_vram_driver_dumb_create, \
 	.dumb_map_offset	  = drm_gem_ttm_dumb_map_offset, \
-	.gem_prime_mmap		  = drm_gem_prime_mmap
+	.gem_prime_mmap		  = drm_gem_prime_mmap, \
+	.prime_handle_to_fd	  = drm_gem_prime_handle_to_fd, \
+	.prime_fd_to_handle	  = drm_gem_prime_fd_to_handle
 
 /*
  *  VRAM memory manager
Signature · 1 line
2.39.2
maintainer Acked-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] drm/vram-helper: turn on PRIME import/export

Am 02.03.23 um 15:35 schrieb Simon Ser:

Quoted 25 lines

We don't populate drm_driver.gem_prime_import_sg_table so only DMA-BUFs exported from our own device can be imported. We don't populate drm_gem_object_funcs.get_sg_table so DMA-BUFs cannot be imported into another device. Still, this is useful to user-space to share buffers between processes and between API boundaries (e.g. wlroots hard-requires PRIME import/export support).

v2: expand commit message

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
  include/drm/drm_gem_vram_helper.h | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
include/drm/drm_gem_vram_helper.h +0 -0
index d3e8920c0b64..f4aab64411d8 100644

include/drm/drm_gem_vram_helper.h void drm_gem_vram_simple_display_pipe_cleanup_fb( · line 165

Quoted 7 lines
include/drm/drm_gem_vram_helper.h +3 -1
@@ -160,7 +160,9 @@ void drm_gem_vram_simple_display_pipe_cleanup_fb(
  	.debugfs_init             = drm_vram_mm_debugfs_init, \
  	.dumb_create		  = drm_gem_vram_driver_dumb_create, \
  	.dumb_map_offset	  = drm_gem_ttm_dumb_map_offset, \
-	.gem_prime_mmap		  = drm_gem_prime_mmap
+	.gem_prime_mmap		  = drm_gem_prime_mmap, \
+	.prime_handle_to_fd	  = drm_gem_prime_handle_to_fd, \
+	.prime_fd_to_handle	  = drm_gem_prime_fd_to_handle
Quoted 3 lines
include/drm/drm_gem_vram_helper.h +0 -0
  /*
   *  VRAM memory manager
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#3 lore raw
In reply to Simon Ser

Hi

Am 02.03.23 um 15:35 schrieb Simon Ser:

Quoted 36 lines

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
  drivers/gpu/drm/drm_prime.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +2 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
   * Optional pinning of buffers is handled at dma-buf attach and detach time in
   * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
   * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected.
   *
   * For kernel-internal access there's drm_gem_dmabuf_vmap() and
   * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by

drivers/gpu/drm/drm_prime.c int drm_gem_map_attach(struct dma_buf *dma_buf, · line 588

Quoted 5 lines
drivers/gpu/drm/drm_prime.c +2 -0
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
  {
  	struct drm_gem_object *obj = dma_buf->priv;
 
+	if (!obj->funcs->get_sg_table)
+		return -EOPNOTSUPP;

-ENOSYS please.

Best regards
Thomas

Quoted 4 lines
drivers/gpu/drm/drm_prime.c +1 -0
+
  	return drm_gem_pin(obj);
  }
  EXPORT_SYMBOL(drm_gem_map_attach);
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
maintainer Acked-by
#4 lore raw

Hi

Am 02.03.23 um 16:03 schrieb Thomas Zimmermann:

Quoted 39 lines

Hi

Am 02.03.23 um 15:35 schrieb Simon Ser:

Quoted 36 lines

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>

---
  drivers/gpu/drm/drm_prime.c | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +1 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device 
 *dev, void *data,
    * Optional pinning of buffers is handled at dma-buf attach and detach time in
    * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
    * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If 

&drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected. *
   * For kernel-internal access there's drm_gem_dmabuf_vmap() and

Quoted 8 lines

drivers/gpu/drm/drm_prime.c int drm_gem_map_attach(struct dma_buf *dma_buf, · line 587

Quoted 6 lines

   * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by

drivers/gpu/drm/drm_prime.c +2 -0
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
   {
       struct drm_gem_object *obj = dma_buf->priv;
+    if (!obj->funcs->get_sg_table)
+        return -EOPNOTSUPP;

-ENOSYS please.

With this changed:

Quoted 9 lines

Best regards
Thomas

Quoted 4 lines
drivers/gpu/drm/drm_prime.c +1 -0
+
       return drm_gem_pin(obj);
   }
   EXPORT_SYMBOL(drm_gem_map_attach);
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#5 lore raw
In reply to Simon Ser

Am 02.03.23 um 15:35 schrieb Simon Ser:

Quoted 10 lines

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Quoted 7 lines
Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>

With Thomas comment addressed: Reviewed-by: Christian König <[email protected]> for the series.

Quoted 28 lines
---
  drivers/gpu/drm/drm_prime.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +5 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
   * Optional pinning of buffers is handled at dma-buf attach and detach time in
   * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
   * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected.
   *
   * For kernel-internal access there's drm_gem_dmabuf_vmap() and
   * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
  {
  	struct drm_gem_object *obj = dma_buf->priv;
 
+	if (!obj->funcs->get_sg_table)
+		return -EOPNOTSUPP;
+
  	return drm_gem_pin(obj);
  }
  EXPORT_SYMBOL(drm_gem_map_attach);
#6 lore raw
In reply to Simon Ser

Hi Simon

Am 02.03.23 um 15:35 schrieb Simon Ser:

Quoted 9 lines

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Is there a v3 of this patchset? It was Acked with the one errno code changed.

Best regards
Thomas

Quoted 36 lines
Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
  drivers/gpu/drm/drm_prime.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +5 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
   * Optional pinning of buffers is handled at dma-buf attach and detach time in
   * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
   * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected.
   *
   * For kernel-internal access there's drm_gem_dmabuf_vmap() and
   * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
  {
  	struct drm_gem_object *obj = dma_buf->priv;
 
+	if (!obj->funcs->get_sg_table)
+		return -EOPNOTSUPP;
+
  	return drm_gem_pin(obj);
  }
  EXPORT_SYMBOL(drm_gem_map_attach);
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author
#7 lore raw

Hi,

Quoted 2 lines · Thomas Zimmermann

Is there a v3 of this patchset? It was Acked with the one errno code changed.

Since this was a minor change, I did it locally and pushed the patch to drm-misc-next already.

Simon

#8 lore raw

Hi

Am 09.06.23 um 15:04 schrieb Simon Ser:

Quoted 9 lines

Hi,

Quoted 2 lines · Thomas Zimmermann

Is there a v3 of this patchset? It was Acked with the one errno code changed.

Since this was a minor change, I did it locally and pushed the patch to drm-misc-next already.

Found it. Thanks for committing and sorry for the noise.

Best regards
Thomas

Quoted 2 lines

Simon

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#9 lore raw
In reply to Simon Ser
Quoted 37 lines · Simon Ser

drm_gem_map_dma_buf() requires drm_gem_object_funcs.get_sg_table to be implemented, or else WARNs.

Allow drivers to leave this hook unimplemented to implement purely local DMA-BUFs (ie, DMA-BUFs which cannot be imported anywhere else but the device which allocated them). In that case, reject imports to other devices in drm_gem_map_attach().

v2: new patch

Cc Daniel Vetter <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tian Tao <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Christian König <[email protected]>
Cc Hans de Goede <[email protected]>
---
 drivers/gpu/drm/drm_prime.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +2 -1
index f924b8b4ab6b..ab1d21d63a03 100644
@@ -544,7 +544,8 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  * Optional pinning of buffers is handled at dma-buf attach and detach time in
  * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
  * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
- * &drm_gem_object_funcs.get_sg_table.
+ * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
+ * unimplemented, exports into another device are rejected.
  *
  * For kernel-internal access there's drm_gem_dmabuf_vmap() and
  * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by

drivers/gpu/drm/drm_prime.c int drm_gem_map_attach(struct dma_buf *dma_buf, · line 588

Quoted 5 lines
drivers/gpu/drm/drm_prime.c +2 -0
@@ -583,6 +584,9 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
        struct drm_gem_object *obj = dma_buf->priv;
 
+       if (!obj->funcs->get_sg_table)
+               return -EOPNOTSUPP;

This breaks virtgpu, where buffers may not necessarily have guest backing pages, but still may be shared with other virtual devices (because the host side buffer _does_ have backing pages)

BR,
-R

Quoted 8 lines
drivers/gpu/drm/drm_prime.c +1 -1
+
        return drm_gem_pin(obj);
 }
 EXPORT_SYMBOL(drm_gem_map_attach);
--

2.39.2