Skip to contentSkip to conversation

drm: prime: fix refcounting on the dmabuf import error path

PATCH In review Imre Deak 2 participants 4 messages Apr 9, 2013 – Apr 11, 2013

Subsystems (7)
lore
Share & tools
lore

Imre Deak

author
#0 lore raw

PATCH 1/2 drm: prime: fix refcounting on the dmabuf import error path

In commit be8a42ae60 we inroduced a refcount problem, where on the drm_gem_prime_fd_to_handle() error path we'll call dma_buf_put() for self imported dma buffers.

Fix this by taking a reference on the dma buffer in the .gem_import hook instead of assuming the caller had taken one. Besides fixing the bug this is also more logical.

---
 drivers/gpu/drm/drm_prime.c                |    8 +++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    4 +++-
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 ++++-
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c  |    1 -
 drivers/gpu/drm/udl/udl_gem.c              |    4 ++++
 5 files changed, 18 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_prime.c +7 -1
index 25d0218..bba45f6 100644
@@ -268,7 +268,6 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
 			 * refcount on gem itself instead of f_count of dmabuf.
 			 */
 			drm_gem_object_reference(obj);
-			dma_buf_put(dma_buf);
 			return obj;
 		}
 	}
@@ -277,6 +276,8 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
 	if (IS_ERR(attach))
 		return ERR_PTR(PTR_ERR(attach));
 
+	get_dma_buf(dma_buf);
+
 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR_OR_NULL(sgt)) {
 		ret = PTR_ERR(sgt);
@@ -297,6 +298,8 @@ fail_unmap:
 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
+	dma_buf_put(dma_buf);
+
 	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL(drm_gem_prime_import);
@@ -339,6 +342,9 @@ int drm_gem_prime_fd_to_handle(struct drm_device *dev,
 		goto fail;
 
 	mutex_unlock(&file_priv->prime.lock);
+
+	dma_buf_put(dma_buf);
+
 	return 0;
 
 fail:
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +3 -1
index ba0a3aa..ff7f2a8 100644
@@ -235,7 +235,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
 			 * refcount on gem itself instead of f_count of dmabuf.
 			 */
 			drm_gem_object_reference(obj);
-			dma_buf_put(dma_buf);
 			return obj;
 		}
 	}
@@ -244,6 +243,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
 	if (IS_ERR(attach))
 		return ERR_PTR(-EINVAL);
 
+	get_dma_buf(dma_buf);
 
 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR_OR_NULL(sgt)) {
@@ -298,6 +298,8 @@ err_unmap_attach:
 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
 err_buf_detach:
 	dma_buf_detach(dma_buf, attach);
+	dma_buf_put(dma_buf);
+
 	return ERR_PTR(ret);
 }
 
drivers/gpu/drm/i915/i915_gem_dmabuf.c +4 -1
index c6dfc14..30485e9 100644
@@ -272,7 +272,6 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
 			 * refcount on gem itself instead of f_count of dmabuf.
 			 */
 			drm_gem_object_reference(&obj->base);
-			dma_buf_put(dma_buf);
 			return &obj->base;
 		}
 	}
@@ -282,6 +281,8 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
 	if (IS_ERR(attach))
 		return ERR_CAST(attach);
 
+	get_dma_buf(dma_buf);
+
 	obj = i915_gem_object_alloc(dev);
 	if (obj == NULL) {
 		ret = -ENOMEM;
@@ -301,5 +302,7 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
 
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
+	dma_buf_put(dma_buf);
+
 	return ERR_PTR(ret);
 }
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +0 -1
index ac74d1b..1bdf7e1 100644
@@ -212,7 +212,6 @@ struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
 			 * refcount on gem itself instead of f_count of dmabuf.
 			 */
 			drm_gem_object_reference(obj);
-			dma_buf_put(buffer);
 			return obj;
 		}
 	}
drivers/gpu/drm/udl/udl_gem.c +4 -0
index 3816270..ef034fa 100644
@@ -303,6 +303,8 @@ struct drm_gem_object *udl_gem_prime_import(struct drm_device *dev,
 	if (IS_ERR(attach))
 		return ERR_CAST(attach);
 
+	get_dma_buf(dma_buf);
+
 	sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sg)) {
 		ret = PTR_ERR(sg);
@@ -322,5 +324,7 @@ fail_unmap:
 	dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
+	dma_buf_put(dma_buf);
+
 	return ERR_PTR(ret);
 }
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 2/2 drm: prime: fix lookup of existing imports for self imported buffers

Since atm we don't take a reference on the dma buf pointer when we add it to the import lookup table the dma buf can vanish leaving the stale pointer behind. This can in turn lead to returning stale GEM handles when userspace imports a newly exported buffer.

Fix this by keeping a reference on the dma buffer whenever we have a pointer to it in the lookup table.

Reference: https://bugs.freedesktop.org/show_bug.cgi?id=59229

---
 drivers/gpu/drm/drm_prime.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_prime.c +2 -0
index bba45f6..e4e1a69 100644
@@ -501,6 +501,7 @@ int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv
 	if (!member)
 		return -ENOMEM;
 
+	get_dma_buf(dma_buf);
 	member->dma_buf = dma_buf;
 	member->handle = handle;
 	list_add(&member->entry, &prime_fpriv->head);
@@ -529,6 +530,7 @@ void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_f
 	mutex_lock(&prime_fpriv->lock);
 	list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
 		if (member->dma_buf == dma_buf) {
+			dma_buf_put(dma_buf);
 			list_del(&member->entry);
 			kfree(member);
 		}
Signature · 1 line
1.7.10.4
maintainer
#2 lore raw

Re: [PATCH 2/2] drm: prime: fix lookup of existing imports for self imported buffers

Quoted 4 lines

Since atm we don't take a reference on the dma buf pointer when we add it to the import lookup table the dma buf can vanish leaving the stale pointer behind. This can in turn lead to returning stale GEM handles when userspace imports a newly exported buffer.

I sent a bunch of patches to prime months ago, maybe go back and dig them out

they might fix some of these issues,

I think danvet bikeshedded my will to care at the time due to lack of proper locking,
the fact is the patches didn't change the locking, but I could probably go back and find them.

Hopefully you haven't gone and reinvented that work.

Dave.

author
#3 lore raw

Re: [PATCH 2/2] drm: prime: fix lookup of existing imports for self imported buffers

Quoted 8 lines · Dave Airlie
Quoted 4 lines

Since atm we don't take a reference on the dma buf pointer when we add it to the import lookup table the dma buf can vanish leaving the stale pointer behind. This can in turn lead to returning stale GEM handles when userspace imports a newly exported buffer.

I sent a bunch of patches to prime months ago, maybe go back and dig them out

Quoted 8 lines

they might fix some of these issues,

I think danvet bikeshedded my will to care at the time due to lack of proper locking,
the fact is the patches didn't change the locking, but I could probably go back and find them.

Hopefully you haven't gone and reinvented that work.

Yes, I checked it with the i-g-t/prime_self_import test case and it's passing with your

"drm/prime: keep a reference from the handle to exported dma-buf (v2.1)"

patch, so we can drop this one.

--Imre