Skip to contentSkip to conversation

drm/i915: fix reference counting in i915_gem_create

PATCH In review Daniel Vetter 2 participants 9 messages Jul 24, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/i915: fix reference counting in i915_gem_create

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

---
 drivers/gpu/drm/i915/i915_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 49592d6..8e6ff52 100644
@@ -227,7 +227,7 @@ i915_gem_create(struct drm_file *file,
 	}
 
 	/* drop reference from allocate - handle holds it now */
-	drm_gem_object_unreference(&obj->base);
+	drm_gem_object_unreference_unlocked(&obj->base);
 	trace_i915_gem_object_create(obj);
 
 	*handle_p = handle;
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 2/3 drm/i915: fix the racy object accounting

Use an atomic_t for the count (since that is useful for leak detection in tests) and just rip out the object memory check.

---
 drivers/gpu/drm/i915/i915_debugfs.c |  5 ++---
 drivers/gpu/drm/i915/i915_drv.h     |  5 ++---
 drivers/gpu/drm/i915/i915_gem.c     | 24 ++++++------------------
 3 files changed, 10 insertions(+), 24 deletions(-)
drivers/gpu/drm/i915/i915_debugfs.c +2 -3
index 9d871c7..67d6699 100644
@@ -228,9 +228,8 @@ static int i915_gem_object_info(struct seq_file *m, void *data)
 	if (ret)
 		return ret;
 
-	seq_printf(m, "%u objects, %zu bytes\n",
-		   dev_priv->mm.object_count,
-		   dev_priv->mm.object_memory);
+	seq_printf(m, "%u objects\n",
+		   atomic_read(&dev_priv->mm.object_count));
 
 	size = count = mappable_size = mappable_count = 0;
 	count_objects(&dev_priv->mm.bound_list, global_list);
drivers/gpu/drm/i915/i915_drv.h +2 -3
index c9dd0cb..9e49f79 100644
@@ -925,9 +925,8 @@ struct i915_gem_mm {
 	/* storage for physical objects */
 	struct drm_i915_gem_phys_object *phys_objs[I915_MAX_PHYS_OBJECT];
 
-	/* accounting, useful for userland debugging */
-	size_t object_memory;
-	u32 object_count;
+	/* accounting, useful for leak detection in tests */
+	atomic_t object_count;
 };
 
 struct drm_i915_error_state_buf {
drivers/gpu/drm/i915/i915_gem.c +6 -18
index 8e6ff52..8020154 100644
@@ -71,21 +71,6 @@ static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
 	obj->fence_reg = I915_FENCE_REG_NONE;
 }
 
-/* some bookkeeping */
-static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
-				  size_t size)
-{
-	dev_priv->mm.object_count++;
-	dev_priv->mm.object_memory += size;
-}
-
-static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
-				     size_t size)
-{
-	dev_priv->mm.object_count--;
-	dev_priv->mm.object_memory -= size;
-}
-
 static int
 i915_gem_wait_for_error(struct i915_gpu_error *error)
 {
@@ -206,6 +191,7 @@ i915_gem_create(struct drm_file *file,
 		uint32_t *handle_p)
 {
 	struct drm_i915_gem_object *obj;
+	struct drm_i915_private *dev_priv = dev->dev_private;
 	int ret;
 	u32 handle;
 
@@ -221,7 +207,7 @@ i915_gem_create(struct drm_file *file,
 	ret = drm_gem_handle_create(file, &obj->base, &handle);
 	if (ret) {
 		drm_gem_object_release(&obj->base);
-		i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
+		atomic_dec(&dev_priv->mm.object_count);
 		i915_gem_object_free(obj);
 		return ret;
 	}
@@ -3864,6 +3850,8 @@ unlock:
 void i915_gem_object_init(struct drm_i915_gem_object *obj,
 			  const struct drm_i915_gem_object_ops *ops)
 {
+	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
+
 	INIT_LIST_HEAD(&obj->mm_list);
 	INIT_LIST_HEAD(&obj->global_list);
 	INIT_LIST_HEAD(&obj->ring_list);
@@ -3877,7 +3865,7 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
 	/* Avoid an unnecessary call to unbind on the first bind. */
 	obj->map_and_fenceable = true;
 
-	i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
+	atomic_inc(&dev_priv->mm.object_count);
 }
 
 static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
@@ -3983,7 +3971,7 @@ void i915_gem_free_object(struct drm_gem_object *gem_obj)
 		drm_prime_gem_destroy(&obj->base, NULL);
 
 	drm_gem_object_release(&obj->base);
-	i915_gem_info_remove_obj(dev_priv, obj->base.size);
+	atomic_dec(&dev_priv->mm.object_count);
 
 	kfree(obj->bit_17);
 	i915_gem_object_free(obj);
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 3/3 drm/i915: reuse i915_gem_free_object in the create error path

In reply to Daniel Vetter

Instead of partially duplicating code. This shouldn't ever cause a problem since by the time we call drm_gem_handle_create we must be able to handle the object destruction any time (e.g. when userspace races a concurrent gem close ioctl call).

---
 drivers/gpu/drm/i915/i915_gem.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +1 -4
index 8020154..5663a22 100644
@@ -191,7 +191,6 @@ i915_gem_create(struct drm_file *file,
 		uint32_t *handle_p)
 {
 	struct drm_i915_gem_object *obj;
-	struct drm_i915_private *dev_priv = dev->dev_private;
 	int ret;
 	u32 handle;
 
@@ -206,9 +205,7 @@ i915_gem_create(struct drm_file *file,
 
 	ret = drm_gem_handle_create(file, &obj->base, &handle);
 	if (ret) {
-		drm_gem_object_release(&obj->base);
-		atomic_dec(&dev_priv->mm.object_count);
-		i915_gem_object_free(obj);
+		i915_gem_free_object(&obj->base);
 		return ret;
 	}
 
Signature · 1 line
1.8.1.4
#3 lore raw
In reply to Daniel Vetter
Quoted 7 lines · Daniel Vetter

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

So there is a small race for a second thread on the fd to guess the new handle before the owner sees its. We should be able to push the transfer of ownership into the handle_create() routine to close this race with incurring any extra cost.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#4 lore raw

PATCH 1/1 drm/i915: reuse i915_gem_free_object in the create error path

In reply to Daniel Vetter

Instead of partially duplicating code. This shouldn't ever cause a problem since by the time we call drm_gem_handle_create we must be able to handle the object destruction any time (e.g. when userspace races a concurrent gem close ioctl call).

---
 drivers/gpu/drm/i915/i915_gem.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +1 -4
index 41695e9..c287e52 100644
@@ -191,7 +191,6 @@ i915_gem_create(struct drm_file *file,
 		uint32_t *handle_p)
 {
 	struct drm_i915_gem_object *obj;
-	struct drm_i915_private *dev_priv = dev->dev_private;
 	int ret;
 	u32 handle;
 
@@ -206,9 +205,7 @@ i915_gem_create(struct drm_file *file,
 
 	ret = drm_gem_handle_create(file, &obj->base, &handle);
 	if (ret) {
-		drm_gem_object_release(&obj->base);
-		atomic_dec(&dev_priv->mm.object_count);
-		i915_gem_object_free(obj);
+		i915_gem_free_object(&obj->base);
 		return ret;
 	}
 
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH 1/1 drm/i915: fix reference counting in i915_gem_create

In reply to Daniel Vetter

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.

---
 drivers/gpu/drm/i915/i915_gem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +3 -2
index 49592d6..56abcd1f 100644
@@ -226,10 +226,11 @@ i915_gem_create(struct drm_file *file,
 		return ret;
 	}
 
-	/* drop reference from allocate - handle holds it now */
-	drm_gem_object_unreference(&obj->base);
 	trace_i915_gem_object_create(obj);
 
+	/* drop reference from allocate - handle holds it now */
+	drm_gem_object_unreference_unlocked(&obj->base);
+
 	*handle_p = handle;
 	return 0;
 }
Signature · 1 line
1.8.1.4
#6 lore raw
Quoted 10 lines · Daniel Vetter

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.

Pass the size into the tracepoint, that gets rid of the racy read (the actual userspace side we've allowed ourselves to be racy before). And keeps the function clean, if you rewrite it like I suggested. -Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#7 lore raw
Quoted 9 lines · Chris Wilson
Quoted 8 lines · Daniel Vetter

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

Quoted 6 lines
Quoted 2 lines

v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.

Pass the size into the tracepoint, that gets rid of the racy read (the actual userspace side we've allowed ourselves to be racy before). And keeps the function clean, if you rewrite it like I suggested.

Well I honestly don't really see the upshot of the little diff you've pasted on irc. The bug is that we call the wrong unreference function and that we access the object after we've dropped the reference. -Daniel
--
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch

#8 lore raw
Quoted 13 lines · Daniel Vetter
Quoted 12 lines · Chris Wilson
Quoted 10 lines · Daniel Vetter

This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.

As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.

v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.

Quoted 7 lines
Quoted 3 lines

Pass the size into the tracepoint, that gets rid of the racy read (the actual userspace side we've allowed ourselves to be racy before). And keeps the function clean, if you rewrite it like I suggested.

Well I honestly don't really see the upshot of the little diff you've pasted on irc. The bug is that we call the wrong unreference function and that we access the object after we've dropped the reference.

Ignoring the typo, it is much neater code. The code that was there before my hack.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre