Skip to contentSkip to conversation

drm/prime: Unbreak virtgpu dma-buf export

PATCH In review Rob Clark 4 participants 9 messages Mar 22, 2024 – Mar 28, 2024

Subsystems (3)
lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/prime: Unbreak virtgpu dma-buf export

From: Rob Clark <[email protected]>

virtgpu "vram" GEM objects do not implement obj->get_sg_table(). But they also don't use drm_gem_map_dma_buf(). In fact they may not even have guest visible pages. But it is perfectly fine to export and share with other virtual devices.

Fixes 207395da5a97 ("drm/prime: reject DMA-BUF attach when get_sg_table is missing")
---
 drivers/gpu/drm/drm_prime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +6 -1
index 7352bde299d5..64dd6276e828 100644
@@ -582,7 +582,12 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
 	struct drm_gem_object *obj = dma_buf->priv;
 
-	if (!obj->funcs->get_sg_table)
+	/*
+	 * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
+	 * that implement their own ->map_dma_buf() do not.
+	 */
+	if ((dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf) &&
+	    !obj->funcs->get_sg_table)
 		return -ENOSYS;
 
 	return drm_gem_pin(obj);
Signature · 1 line
2.44.0
#1 lore raw

It also fixes importing virtgpu blobs into real hardware, for instance amdgpu for DRI_PRIME rendering.

Quoted 36 lines · Rob Clark

From: Rob Clark <[email protected]>

virtgpu "vram" GEM objects do not implement obj->get_sg_table(). But they also don't use drm_gem_map_dma_buf(). In fact they may not even have guest visible pages. But it is perfectly fine to export and share with other virtual devices.

Fixes 207395da5a97 ("drm/prime: reject DMA-BUF attach when get_sg_table is missing")
---
 drivers/gpu/drm/drm_prime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +6 -2
index 7352bde299d5..64dd6276e828 100644
@@ -582,7 +582,12 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
        struct drm_gem_object *obj = dma_buf->priv;
 
-       if (!obj->funcs->get_sg_table)
+       /*
+        * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
+        * that implement their own ->map_dma_buf() do not.
+        */
+       if ((dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf) &&
+           !obj->funcs->get_sg_table)
                return -ENOSYS;
 
        return drm_gem_pin(obj);
--

2.44.0

#2 lore raw
In reply to Rob Clark

It also fixes importing virtgpu blobs into real hardware, for instance amdgpu for DRI_PRIME rendering.

Quoted 37 lines · Rob Clark

From: Rob Clark <[email protected]>

virtgpu "vram" GEM objects do not implement obj->get_sg_table(). But they also don't use drm_gem_map_dma_buf(). In fact they may not even have guest visible pages. But it is perfectly fine to export and share with other virtual devices.

Fixes 207395da5a97 ("drm/prime: reject DMA-BUF attach when get_sg_table

is missing")

---
 drivers/gpu/drm/drm_prime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +6 -2
index 7352bde299d5..64dd6276e828 100644
@@ -582,7 +582,12 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
        struct drm_gem_object *obj = dma_buf->priv;
 
-       if (!obj->funcs->get_sg_table)
+       /*
+        * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
+        * that implement their own ->map_dma_buf() do not.
+        */
+       if ((dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf) &&
+           !obj->funcs->get_sg_table)
                return -ENOSYS;
 
        return drm_gem_pin(obj);
--

2.44.0

author
#3 lore raw

This is actually a bit concerning.. importing a host page backed buffer without guest mapping into a passthru device probably doesn't work and should be rejected earlier.

I do think we should relax the restriction (either taking my patch or reverting the commit it fixes) until we work this out properly (because the original patch is a regression), but importing a buffer without guest pages into a passthru device can't possibly work properly. Maybe it works by chance if the host buffer is mapped to the guest, but that is not guaranteed.

BR,
-R

Quoted 40 lines · Dominik Behr

It also fixes importing virtgpu blobs into real hardware, for instance amdgpu for DRI_PRIME rendering.

Quoted 36 lines · Rob Clark

From: Rob Clark <[email protected]>

virtgpu "vram" GEM objects do not implement obj->get_sg_table(). But they also don't use drm_gem_map_dma_buf(). In fact they may not even have guest visible pages. But it is perfectly fine to export and share with other virtual devices.

Fixes 207395da5a97 ("drm/prime: reject DMA-BUF attach when get_sg_table is missing")
---
 drivers/gpu/drm/drm_prime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +6 -2
index 7352bde299d5..64dd6276e828 100644
@@ -582,7 +582,12 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
        struct drm_gem_object *obj = dma_buf->priv;
 
-       if (!obj->funcs->get_sg_table)
+       /*
+        * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
+        * that implement their own ->map_dma_buf() do not.
+        */
+       if ((dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf) &&
+           !obj->funcs->get_sg_table)
                return -ENOSYS;
 
        return drm_gem_pin(obj);
--

2.44.0

#4 lore raw

It is mapped, via iommu interface (vfio-iommu) when map_dma is called. --
Dominik

Quoted 56 lines · Rob Clark

This is actually a bit concerning.. importing a host page backed buffer without guest mapping into a passthru device probably doesn't work and should be rejected earlier.

I do think we should relax the restriction (either taking my patch or reverting the commit it fixes) until we work this out properly (because the original patch is a regression), but importing a buffer without guest pages into a passthru device can't possibly work properly. Maybe it works by chance if the host buffer is mapped to the guest, but that is not guaranteed.

BR,
-R

Quoted 40 lines · Dominik Behr

It also fixes importing virtgpu blobs into real hardware, for instance amdgpu for DRI_PRIME rendering.

Quoted 36 lines · Rob Clark

From: Rob Clark <[email protected]>

virtgpu "vram" GEM objects do not implement obj->get_sg_table(). But they also don't use drm_gem_map_dma_buf(). In fact they may not even have guest visible pages. But it is perfectly fine to export and share with other virtual devices.

Fixes 207395da5a97 ("drm/prime: reject DMA-BUF attach when get_sg_table is missing")
---
 drivers/gpu/drm/drm_prime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +6 -2
index 7352bde299d5..64dd6276e828 100644
@@ -582,7 +582,12 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 {
        struct drm_gem_object *obj = dma_buf->priv;
 
-       if (!obj->funcs->get_sg_table)
+       /*
+        * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
+        * that implement their own ->map_dma_buf() do not.
+        */
+       if ((dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf) &&
+           !obj->funcs->get_sg_table)
                return -ENOSYS;
 
        return drm_gem_pin(obj);
--

2.44.0

#7 lore raw
Quoted 1 line · Rob Clark

any chance I could talk you into pushing to drm-misc-fixes?

Oh sorry, I thought you had access… Pushed with a minor edit to remove unnecessary parentheses to make checkpatch happy!

author
#8 lore raw
Quoted 7 lines · Simon Ser
Quoted 1 line · Rob Clark

any chance I could talk you into pushing to drm-misc-fixes?

Oh sorry, I thought you had access… Pushed with a minor edit to remove unnecessary parentheses to make checkpatch happy!

Thanks!

BR,
-R