All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Vetter <daniel.vetter@ffwll.ch>
To: DRI Development <dri-devel@lists.freedesktop.org>
Cc: Daniel Vetter <daniel.vetter@intel.com>,
	Daniel Vetter <daniel.vetter@ffwll.ch>
Subject: [PATCH] drm/gem: Use kref_get_unless_zero for the weak mmap references
Date: Thu, 15 Oct 2015 11:33:43 +0200	[thread overview]
Message-ID: <1444901623-18918-1-git-send-email-daniel.vetter@ffwll.ch> (raw)
In-Reply-To: <1444894601-5200-12-git-send-email-daniel.vetter@ffwll.ch>

Compared to wrapping the final kref_put with dev->struct_mutex this
allows us to only acquire the offset manager look both in the final
cleanup and in the lookup. Which has the upside that no locks leak out
of the core abstractions. But it means that we need to hold a
temporary reference to the object while checking mmap constraints, to
make sure the object doesn't disappear. Extended the critical region
would have worked too, but would result in more leaky locking.

Also, this is the final bit which required dev->struct_mutex in gem
core, now modern drivers can be completely struct_mutex free!

This needs a new drm_vma_offset_exact_lookup_locked and makes both
drm_vma_offset_exact_lookup and drm_vma_offset_lookup unused.

v2: Don't leak object references in failure paths (David).

Cc: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
---
 drivers/gpu/drm/drm_gem.c         | 30 +++++++++++++++++------------
 drivers/gpu/drm/drm_vma_manager.c | 40 ++++++++++++---------------------------
 include/drm/drm_vma_manager.h     | 22 ++++++---------------
 3 files changed, 36 insertions(+), 56 deletions(-)

diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index ab8ea42264f4..663fadbe979e 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -862,30 +862,36 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 {
 	struct drm_file *priv = filp->private_data;
 	struct drm_device *dev = priv->minor->dev;
-	struct drm_gem_object *obj;
+	struct drm_gem_object *obj = NULL;
 	struct drm_vma_offset_node *node;
 	int ret;
 
 	if (drm_device_is_unplugged(dev))
 		return -ENODEV;
 
-	mutex_lock(&dev->struct_mutex);
+	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
+	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
+						  vma->vm_pgoff,
+						  vma_pages(vma));
+	if (likely(node)) {
+		obj = container_of(node, struct drm_gem_object, vma_node);
+		if (!kref_get_unless_zero(&obj->refcount))
+			obj = NULL;
+	}
+	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
 
-	node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
-					   vma->vm_pgoff,
-					   vma_pages(vma));
-	if (!node) {
-		mutex_unlock(&dev->struct_mutex);
+	if (!obj)
 		return -EINVAL;
-	} else if (!drm_vma_node_is_allowed(node, filp)) {
-		mutex_unlock(&dev->struct_mutex);
+
+	if (!drm_vma_node_is_allowed(node, filp)) {
+		drm_gem_object_unreference_unlocked(obj);
 		return -EACCES;
 	}
 
-	obj = container_of(node, struct drm_gem_object, vma_node);
-	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
+	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
+			       vma);
 
-	mutex_unlock(&dev->struct_mutex);
+	drm_gem_object_unreference_unlocked(obj);
 
 	return ret;
 }
diff --git a/drivers/gpu/drm/drm_vma_manager.c b/drivers/gpu/drm/drm_vma_manager.c
index 68c1f32fb086..2f2ecde8285b 100644
--- a/drivers/gpu/drm/drm_vma_manager.c
+++ b/drivers/gpu/drm/drm_vma_manager.c
@@ -112,7 +112,7 @@ void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
 EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
 
 /**
- * drm_vma_offset_lookup() - Find node in offset space
+ * drm_vma_offset_lookup_locked() - Find node in offset space
  * @mgr: Manager object
  * @start: Start address for object (page-based)
  * @pages: Size of object (page-based)
@@ -122,37 +122,21 @@ EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
  * region and the given node will be returned, as long as the node spans the
  * whole requested area (given the size in number of pages as @pages).
  *
- * RETURNS:
- * Returns NULL if no suitable node can be found. Otherwise, the best match
- * is returned. It's the caller's responsibility to make sure the node doesn't
- * get destroyed before the caller can access it.
- */
-struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
-						  unsigned long start,
-						  unsigned long pages)
-{
-	struct drm_vma_offset_node *node;
-
-	read_lock(&mgr->vm_lock);
-	node = drm_vma_offset_lookup_locked(mgr, start, pages);
-	read_unlock(&mgr->vm_lock);
-
-	return node;
-}
-EXPORT_SYMBOL(drm_vma_offset_lookup);
-
-/**
- * drm_vma_offset_lookup_locked() - Find node in offset space
- * @mgr: Manager object
- * @start: Start address for object (page-based)
- * @pages: Size of object (page-based)
+ * Note that before lookup the vma offset manager lookup lock must be acquired
+ * with drm_vma_offset_lock_lookup(). See there for an example. This can then be
+ * used to implement weakly referenced lookups using kref_get_unless_zero().
  *
- * Same as drm_vma_offset_lookup() but requires the caller to lock offset lookup
- * manually. See drm_vma_offset_lock_lookup() for an example.
+ * Example:
+ *     drm_vma_offset_lock_lookup(mgr);
+ *     node = drm_vma_offset_lookup_locked(mgr);
+ *     if (node)
+ *         kref_get_unless_zero(container_of(node, sth, entr));
+ *     drm_vma_offset_unlock_lookup(mgr);
  *
  * RETURNS:
  * Returns NULL if no suitable node can be found. Otherwise, the best match
- * is returned.
+ * is returned. It's the caller's responsibility to make sure the node doesn't
+ * get destroyed before the caller can access it.
  */
 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
 							 unsigned long start,
diff --git a/include/drm/drm_vma_manager.h b/include/drm/drm_vma_manager.h
index 74f3b38c43c1..2f63dd5e05eb 100644
--- a/include/drm/drm_vma_manager.h
+++ b/include/drm/drm_vma_manager.h
@@ -54,9 +54,6 @@ void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
 				 unsigned long page_offset, unsigned long size);
 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
 
-struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
-						  unsigned long start,
-						  unsigned long pages);
 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
 							   unsigned long start,
 							   unsigned long pages);
@@ -71,25 +68,25 @@ bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
 			     struct file *filp);
 
 /**
- * drm_vma_offset_exact_lookup() - Look up node by exact address
+ * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
  * @mgr: Manager object
  * @start: Start address (page-based, not byte-based)
  * @pages: Size of object (page-based)
  *
- * Same as drm_vma_offset_lookup() but does not allow any offset into the node.
+ * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
  * It only returns the exact object with the given start address.
  *
  * RETURNS:
  * Node at exact start address @start.
  */
 static inline struct drm_vma_offset_node *
-drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr,
-			    unsigned long start,
-			    unsigned long pages)
+drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
+				   unsigned long start,
+				   unsigned long pages)
 {
 	struct drm_vma_offset_node *node;
 
-	node = drm_vma_offset_lookup(mgr, start, pages);
+	node = drm_vma_offset_lookup_locked(mgr, start, pages);
 	return (node && node->vm_node.start == start) ? node : NULL;
 }
 
@@ -108,13 +105,6 @@ drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr,
  * not call any other VMA helpers while holding this lock.
  *
  * Note: You're in atomic-context while holding this lock!
- *
- * Example:
- *     drm_vma_offset_lock_lookup(mgr);
- *     node = drm_vma_offset_lookup_locked(mgr);
- *     if (node)
- *         kref_get_unless_zero(container_of(node, sth, entr));
- *     drm_vma_offset_unlock_lookup(mgr);
  */
 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
 {
-- 
2.5.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2015-10-15  9:30 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-15  7:36 [PATCH 00/25] dev->struct_mutex crusade (cont'd) Daniel Vetter
2015-10-15  7:36 ` [PATCH 01/25] drm/armada: Plug leak in dumb_map_offset Daniel Vetter
2015-10-15  7:36 ` [PATCH 02/25] drm/armada: Don't grab dev->struct_mutex for in mmap offset ioctl Daniel Vetter
2015-10-15  7:36 ` [PATCH 03/25] drm/armada: Drop struct_mutex from cursor paths Daniel Vetter
2015-10-15  7:36 ` [PATCH 04/25] drm/armada: Use a private mutex to protect priv->linear Daniel Vetter
2015-10-15  7:36 ` [PATCH 05/25] drm/tegra: don't take dev->struct_mutex in mmap offset ioctl Daniel Vetter
2015-10-15 12:35   ` [PATCH] " Daniel Vetter
2015-10-15 12:40     ` Patrik Jakobsson
2015-10-21 11:23     ` Thierry Reding
2015-10-15  7:36 ` [PATCH 06/25] drm/tegra: Use drm_gem_object_reference_unlocked Daniel Vetter
2015-10-21 11:25   ` Thierry Reding
2015-10-15  7:36 ` [PATCH 07/25] drm/vgem: Drop vgem_drm_gem_mmap Daniel Vetter
2015-10-15  7:36 ` [PATCH 08/25] drm/gem: Drop struct_mutex requirement from drm_gem_mmap_obj Daniel Vetter
2015-10-15  8:34   ` David Herrmann
2015-10-15  7:36 ` [PATCH 09/25] drm/gem: Check locking in drm_gem_object_unreference Daniel Vetter
2015-10-15  8:36   ` David Herrmann
2015-10-15  8:55     ` Daniel Vetter
2015-10-15  8:56       ` David Herrmann
2015-10-15  7:36 ` [PATCH 10/25] drm/gem: Use container_of in drm_gem_object_free Daniel Vetter
2015-10-15  8:36   ` David Herrmann
2015-10-15  7:36 ` [PATCH 11/25] drm/gem: Use kref_get_unless_zero for the weak mmap references Daniel Vetter
2015-10-15  8:46   ` David Herrmann
2015-10-15  9:33   ` Daniel Vetter [this message]
2015-10-15 10:06     ` [PATCH] " Chris Wilson
2015-10-15 11:11       ` Daniel Vetter
2015-10-15 12:31         ` Daniel Vetter
2015-10-15  7:36 ` [PATCH 12/25] drm/gma500: Use correct unref in the gem bo create function Daniel Vetter
2015-10-15 12:48   ` Patrik Jakobsson
2015-10-15  7:36 ` [PATCH 13/25] drm/gma500: Drop dev->struct_mutex from modeset code Daniel Vetter
2015-10-15 13:07   ` Patrik Jakobsson
2015-10-15  7:36 ` [PATCH 14/25] drm/gma500: Drop dev->struct_mutex from fbdev init/teardown code Daniel Vetter
2015-10-15 13:24   ` Patrik Jakobsson
2015-10-15  7:36 ` [PATCH 15/25] drm/gma500: Drop dev->struct_mutex from mmap offset function Daniel Vetter
2015-10-15 17:04   ` Patrik Jakobsson
2015-10-15  7:36 ` [PATCH 16/25] drm/gma500: Add driver private mutex for the fault handler Daniel Vetter
2015-10-15  7:36 ` [PATCH 17/25] drm/nouveau: Drop dev->struct_mutex from fbdev init Daniel Vetter
2015-10-15  7:36 ` [PATCH 18/25] drm/radeon: Use rdev->gem.mutex to protect hyperz/cmask owners Daniel Vetter
2015-10-15  8:27   ` Christian König
2015-10-15 14:24     ` Alex Deucher
2015-10-15  7:36 ` [PATCH 19/25] drm/exynos: Drop dev->struct_mutex from mmap offset function Daniel Vetter
2015-10-15  7:36 ` [PATCH 20/25] drm/exynos: drop struct_mutex from exynos_gem_map_sgt_with_dma Daniel Vetter
2015-10-15  7:36 ` [PATCH 21/25] drm/exynos: drop struct_mutex from exynos_drm_gem_get_ioctl Daniel Vetter
2015-10-15  7:36 ` [PATCH 22/25] drm/exynos: drop struct_mutex from fbdev setup Daniel Vetter
2015-10-15  7:36 ` [PATCH 23/25] drm/vgem: Simplify dum_map Daniel Vetter
2015-10-15  7:36 ` [PATCH 24/25] drm/vgem: Move get_pages to gem_create Daniel Vetter
2015-10-15  7:36 ` [PATCH 25/25] drm/vgem: Drop dev->struct_mutex Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1444901623-18918-1-git-send-email-daniel.vetter@ffwll.ch \
    --to=daniel.vetter@ffwll.ch \
    --cc=daniel.vetter@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.