Skip to contentSkip to conversation

drm: Don't reference objects in the flink name idr

PATCH In review Kristian Høgsberg 2 participants 4 messages Dec 3, 2013

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

PATCH 1/1 drm: Don't reference objects in the flink name idr

There's no reason to keep a reference to objects in the name idr. Each handle to an object has a reference to the object and just before we destroy the last handle we take the object out of the name idr. Thus, if an object is in the name idr, there's at least one reference to the object.

Or to put it another way, the name idr reference will never keep the object alive. It just looks like it, which is confusing.

---
 drivers/gpu/drm/drm_gem.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -15
index 4761ade..3ff39bb 100644
@@ -175,11 +175,6 @@ drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
 	mutex_unlock(&filp->prime.lock);
 }
 
-static void drm_gem_object_ref_bug(struct kref *list_kref)
-{
-	BUG();
-}
-
 /**
  * Called after the last handle to the object has been closed
  *
@@ -195,13 +190,6 @@ static void drm_gem_object_handle_free(struct drm_gem_object *obj)
 	if (obj->name) {
 		idr_remove(&dev->object_name_idr, obj->name);
 		obj->name = 0;
-		/*
-		 * The object name held a reference to this object, drop
-		 * that now.
-		*
-		* This cannot be the last reference, since the handle holds one too.
-		 */
-		kref_put(&obj->refcount, drm_gem_object_ref_bug);
 	}
 }
 
@@ -602,9 +590,6 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 			goto err;
 
 		obj->name = ret;
-
-		/* Allocate a reference for the name table.  */
-		drm_gem_object_reference(obj);
 	}
 
 	args->name = (uint64_t) obj->name;
Signature · 6 lines
1.8.3.1

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 10 lines · Kristian Høgsberg

There's no reason to keep a reference to objects in the name idr. Each handle to an object has a reference to the object and just before we destroy the last handle we take the object out of the name idr. Thus, if an object is in the name idr, there's at least one reference to the object.

Or to put it another way, the name idr reference will never keep the object alive. It just looks like it, which is confusing.

I expect this to blow up when you race gem_close ioctl calls with flink open. i-g-t/gem_flink_close tests actually have been written specifically to exercise these races.
-Daniel

Quoted 51 lines
---
 drivers/gpu/drm/drm_gem.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -15
index 4761ade..3ff39bb 100644
@@ -175,11 +175,6 @@ drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
 	mutex_unlock(&filp->prime.lock);
 }
 
-static void drm_gem_object_ref_bug(struct kref *list_kref)
-{
-	BUG();
-}
-
 /**
  * Called after the last handle to the object has been closed
  *
@@ -195,13 +190,6 @@ static void drm_gem_object_handle_free(struct drm_gem_object *obj)
 	if (obj->name) {
 		idr_remove(&dev->object_name_idr, obj->name);
 		obj->name = 0;
-		/*
-		 * The object name held a reference to this object, drop
-		 * that now.
-		*
-		* This cannot be the last reference, since the handle holds one too.
-		 */
-		kref_put(&obj->refcount, drm_gem_object_ref_bug);
 	}
 }
 
@@ -602,9 +590,6 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 			goto err;
 
 		obj->name = ret;
-
-		/* Allocate a reference for the name table.  */
-		drm_gem_object_reference(obj);
 	}
 
 	args->name = (uint64_t) obj->name;
Signature · 6 lines
1.8.3.1

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#2 lore raw
Quoted 10 lines · Daniel Vetter
Quoted 9 lines · Kristian Høgsberg

There's no reason to keep a reference to objects in the name idr. Each handle to an object has a reference to the object and just before we destroy the last handle we take the object out of the name idr. Thus, if an object is in the name idr, there's at least one reference to the object.

Or to put it another way, the name idr reference will never keep the object alive. It just looks like it, which is confusing.

Quoted 6 lines
Quoted 1 line

I expect this to blow up when you race gem_close ioctl calls with flink open. i-g-t/gem_flink_close tests actually have been written specifically to exercise these races.
-Daniel

Can you be more specific about what race you see? The one thing that could go wrong is that the last handle is delete after we enter drm_gem_flink_ioctl() and look up the object but before taking the object_name_lock, but that's handled by checking obj->handle_count under the lock. Deleting handles and removing the name is always done under the object_name_lock from drm_gem_object_handle_unreference_unlocked().

Kristian

Quoted 47 lines
Quoted 47 lines
---
 drivers/gpu/drm/drm_gem.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -16
index 4761ade..3ff39bb 100644
@@ -175,11 +175,6 @@ drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
      mutex_unlock(&filp->prime.lock);
 }
 
-static void drm_gem_object_ref_bug(struct kref *list_kref)
-{
-     BUG();
-}
-
 /**
  * Called after the last handle to the object has been closed
  *
@@ -195,13 +190,6 @@ static void drm_gem_object_handle_free(struct drm_gem_object *obj)
      if (obj->name) {
              idr_remove(&dev->object_name_idr, obj->name);
              obj->name = 0;
-             /*
-              * The object name held a reference to this object, drop
-              * that now.
-             *
-             * This cannot be the last reference, since the handle holds one too.
-              */
-             kref_put(&obj->refcount, drm_gem_object_ref_bug);
      }
 }
 
@@ -602,9 +590,6 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,
                      goto err;
 
              obj->name = ret;
-
-             /* Allocate a reference for the name table.  */
-             drm_gem_object_reference(obj);
      }
 
      args->name = (uint64_t) obj->name;
--

1.8.3.1

Quoted 9 lines
Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

--
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

Reviewed-by
#3 lore raw
Quoted 18 lines · Kristian Høgsberg
Quoted 10 lines · Daniel Vetter
Quoted 9 lines · Kristian Høgsberg

There's no reason to keep a reference to objects in the name idr. Each handle to an object has a reference to the object and just before we destroy the last handle we take the object out of the name idr. Thus, if an object is in the name idr, there's at least one reference to the object.

Or to put it another way, the name idr reference will never keep the object alive. It just looks like it, which is confusing.

Quoted 6 lines
Quoted 1 line

I expect this to blow up when you race gem_close ioctl calls with flink open. i-g-t/gem_flink_close tests actually have been written specifically to exercise these races.
-Daniel

Quoted 7 lines

Can you be more specific about what race you see? The one thing that could go wrong is that the last handle is delete after we enter drm_gem_flink_ioctl() and look up the object but before taking the object_name_lock, but that's handled by checking obj->handle_count under the lock. Deleting handles and removing the name is always done under the object_name_lock from drm_gem_object_handle_unreference_unlocked().

Too many nightmares around lifetime rules, I've imagined some monster that isn't ther and stand corrected.

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch