Skip to contentSkip to conversation

drm: Don't complain too much about struct_mutex.

PATCH In review Daniel Vetter 5 participants 8 messages Jul 15, 2017 – Jul 18, 2017

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

PATCH 1/1 drm: Don't complain too much about struct_mutex.

For modern drivers the DRM core doesn't use struct_mutex at all, which means it's defacto a driver-private lock. But since we still need it for legacy drivers we can't initialize it in drivers, which means all the different instances share one lockdep key. Despite that they might be placed in totally different places in the locking hierarchy.

This results in a lot of bogus lockdep splats when running stuff on systems with multiple gpus. Partially remedy the situation by only doing might_lock checks on drivers that do use struct_mutex still for gem locking.

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Cc Hans de Goede <[email protected]>
Cc Ben Skeggs <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 drivers/gpu/drm/drm_gem.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +5 -3
index 8dc11064253d..9663a79dd363 100644
@@ -826,13 +826,15 @@ drm_gem_object_put_unlocked(struct drm_gem_object *obj)
 		return;
 
 	dev = obj->dev;
-	might_lock(&dev->struct_mutex);
 
 	if (dev->driver->gem_free_object_unlocked)
 		kref_put(&obj->refcount, drm_gem_object_free);
-	else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
+	else {
+		might_lock(&dev->struct_mutex);
+		if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
 				&dev->struct_mutex))
-		mutex_unlock(&dev->struct_mutex);
+			mutex_unlock(&dev->struct_mutex);
+	}
 }
 EXPORT_SYMBOL(drm_gem_object_put_unlocked);
 
Signature · 6 lines
2.13.2

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

Quoting Daniel Vetter (2017-07-15 10:53:28)

Quoted 11 lines

For modern drivers the DRM core doesn't use struct_mutex at all, which means it's defacto a driver-private lock. But since we still need it for legacy drivers we can't initialize it in drivers, which means all the different instances share one lockdep key. Despite that they might be placed in totally different places in the locking hierarchy.

This results in a lot of bogus lockdep splats when running stuff on systems with multiple gpus. Partially remedy the situation by only doing might_lock checks on drivers that do use struct_mutex still for gem locking.

Quoted 5 lines

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

I placed it in drm_driver which is static to get around that, did you see the patch adding drm_driver_class? I didn't choose that path since this might_lock clearly belongs to kref_put_mutex...

Similarly might_lock can also include might_sleep

+#define lock_is_mutex(lock) \
+ __builtin_types_compatible_p(typeof(*lock), struct mutex)
+

 # define might_lock(lock)                                              \
 do {                                                                   \
+       might_sleep_if(lock_is_mutex(lock));    
Quoted 22 lines
Cc Hans de Goede <[email protected]>
Cc Ben Skeggs <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 drivers/gpu/drm/drm_gem.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -1
index 8dc11064253d..9663a79dd363 100644
@@ -826,13 +826,15 @@ drm_gem_object_put_unlocked(struct drm_gem_object *obj)
                return;
 
        dev = obj->dev;
-       might_lock(&dev->struct_mutex);
 
        if (dev->driver->gem_free_object_unlocked)
                kref_put(&obj->refcount, drm_gem_object_free);

drivers/gpu/drm/drm_gem.c drm_gem_object_put_unlocked(struct drm_gem_object *obj) · line 839

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +5 -2
-       else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
+       else {
+               might_lock(&dev->struct_mutex);
+               if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
                                &dev->struct_mutex))
-               mutex_unlock(&dev->struct_mutex);
+                       mutex_unlock(&dev->struct_mutex);
+       }
 }
 EXPORT_SYMBOL(drm_gem_object_put_unlocked);

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#2 lore raw

✓ Fi.CI.BAT: success for drm: Don't complain too much about struct_mutex.

In reply to Daniel Vetter
== Series Details ==

== Series Details ==

Series: drm: Don't complain too much about struct_mutex.
URL : https://patchwork.freedesktop.org/series/27342/
State : success

== Summary ==

Series 27342v1 drm: Don't complain too much about struct_mutex.
https://patchwork.freedesktop.org/api/1.0/series/27342/revisions/1/mbox/

Test gem_exec_flush:

        Subgroup basic-batch-kernel-default-uc:
                fail       -> PASS       (fi-snb-2600) fdo#100007
Test kms_pipe_crc_basic:
        Subgroup hang-read-crc-pipe-a:
                pass       -> DMESG-WARN (fi-pnv-d510) fdo#101597
        Subgroup suspend-read-crc-pipe-b:
                pass       -> DMESG-WARN (fi-byt-j1900) fdo#101705

fdo#100007 https://bugs.freedesktop.org/show_bug.cgi?id=100007
fdo#101597 https://bugs.freedesktop.org/show_bug.cgi?id=101597
fdo#101705 https://bugs.freedesktop.org/show_bug.cgi?id=101705

fi-bdw-5557u     total:279  pass:268  dwarn:0   dfail:0   fail:0   skip:11  time:442s
fi-bdw-gvtdvm    total:279  pass:265  dwarn:0   dfail:0   fail:0   skip:14  time:435s
fi-blb-e6850     total:279  pass:224  dwarn:1   dfail:0   fail:0   skip:54  time:357s
fi-bsw-n3050     total:279  pass:243  dwarn:0   dfail:0   fail:0   skip:36  time:524s
fi-bxt-j4205     total:279  pass:260  dwarn:0   dfail:0   fail:0   skip:19  time:507s
fi-byt-j1900     total:279  pass:254  dwarn:1   dfail:0   fail:0   skip:24  time:491s
fi-byt-n2820     total:279  pass:250  dwarn:1   dfail:0   fail:0   skip:28  time:484s
fi-glk-2a        total:279  pass:260  dwarn:0   dfail:0   fail:0   skip:19  time:593s
fi-hsw-4770      total:279  pass:263  dwarn:0   dfail:0   fail:0   skip:16  time:433s
fi-hsw-4770r     total:279  pass:263  dwarn:0   dfail:0   fail:0   skip:16  time:419s
fi-ilk-650       total:279  pass:229  dwarn:0   dfail:0   fail:0   skip:50  time:426s
fi-ivb-3520m     total:279  pass:261  dwarn:0   dfail:0   fail:0   skip:18  time:503s
fi-ivb-3770      total:279  pass:261  dwarn:0   dfail:0   fail:0   skip:18  time:471s
fi-kbl-7500u     total:279  pass:261  dwarn:0   dfail:0   fail:0   skip:18  time:465s
fi-kbl-7560u     total:279  pass:269  dwarn:0   dfail:0   fail:0   skip:10  time:571s
fi-kbl-r         total:279  pass:260  dwarn:1   dfail:0   fail:0   skip:18  time:585s
fi-pnv-d510      total:279  pass:222  dwarn:2   dfail:0   fail:0   skip:55  time:564s
fi-skl-6260u     total:279  pass:269  dwarn:0   dfail:0   fail:0   skip:10  time:465s
fi-skl-6700hq    total:279  pass:262  dwarn:0   dfail:0   fail:0   skip:17  time:585s
fi-skl-6700k     total:279  pass:257  dwarn:4   dfail:0   fail:0   skip:18  time:471s
fi-skl-6770hq    total:279  pass:269  dwarn:0   dfail:0   fail:0   skip:10  time:483s
fi-skl-gvtdvm    total:279  pass:266  dwarn:0   dfail:0   fail:0   skip:13  time:436s
fi-skl-x1585l    total:279  pass:268  dwarn:0   dfail:0   fail:0   skip:11  time:472s
fi-snb-2520m     total:279  pass:251  dwarn:0   dfail:0   fail:0   skip:28  time:538s
fi-snb-2600      total:279  pass:250  dwarn:0   dfail:0   fail:0   skip:29  time:420s

bd5641a8649223257233c4e1c45fc94abec1cdbe drm-tip: 2017y-07m-14d-19h-18m-56s UTC integration manifest e83e212 drm: Don't complain too much about struct_mutex.

== Logs ==

For more details see: https://intel-gfx-ci.01.org/CI/Patchwork_5201/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by on v1 1/1
#3 lore raw
In reply to Daniel Vetter

Quoting Daniel Vetter (2017-07-15 10:53:28)

Quoted 17 lines

For modern drivers the DRM core doesn't use struct_mutex at all, which means it's defacto a driver-private lock. But since we still need it for legacy drivers we can't initialize it in drivers, which means all the different instances share one lockdep key. Despite that they might be placed in totally different places in the locking hierarchy.

This results in a lot of bogus lockdep splats when running stuff on systems with multiple gpus. Partially remedy the situation by only doing might_lock checks on drivers that do use struct_mutex still for gem locking.

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Quoted 6 lines
Cc Hans de Goede <[email protected]>
Cc Ben Skeggs <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>

But fwiw, the patch isn't wrong and fixes a false positive, so

Quoted 8 lines
 drivers/gpu/drm/drm_gem.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -0
index 8dc11064253d..9663a79dd363 100644

drivers/gpu/drm/drm_gem.c drm_gem_object_put_unlocked(struct drm_gem_object *obj) · line 830

Quoted 6 lines
drivers/gpu/drm/drm_gem.c +0 -1
@@ -826,13 +826,15 @@ drm_gem_object_put_unlocked(struct drm_gem_object *obj)
                return;
 
        dev = obj->dev;
-       might_lock(&dev->struct_mutex);
 
        if (dev->driver->gem_free_object_unlocked)

coding-style nit, if one branch needs {} they all need {}. (The real reason why I didn't want to move might_lock around in this function ;)

drivers/gpu/drm/drm_gem.c drm_gem_object_put_unlocked(struct drm_gem_object *obj) · line 839

Quoted 11 lines
drivers/gpu/drm/drm_gem.c +5 -2
                kref_put(&obj->refcount, drm_gem_object_free);
-       else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
+       else {
+               might_lock(&dev->struct_mutex);
+               if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
                                &dev->struct_mutex))
-               mutex_unlock(&dev->struct_mutex);
+                       mutex_unlock(&dev->struct_mutex);
+       }
 }
 EXPORT_SYMBOL(drm_gem_object_put_unlocked);

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#4 lore raw
Quoted 38 lines · Chris Wilson

Quoting Daniel Vetter (2017-07-15 10:53:28)

Quoted 17 lines

For modern drivers the DRM core doesn't use struct_mutex at all, which means it's defacto a driver-private lock. But since we still need it for legacy drivers we can't initialize it in drivers, which means all the different instances share one lockdep key. Despite that they might be placed in totally different places in the locking hierarchy.

This results in a lot of bogus lockdep splats when running stuff on systems with multiple gpus. Partially remedy the situation by only doing might_lock checks on drivers that do use struct_mutex still for gem locking.

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Quoted 6 lines
Cc Hans de Goede <[email protected]>
Cc Ben Skeggs <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>

But fwiw, the patch isn't wrong and fixes a false positive, so

Quoted 10 lines
 drivers/gpu/drm/drm_gem.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -0
index 8dc11064253d..9663a79dd363 100644
@@ -826,13 +826,15 @@ drm_gem_object_put_unlocked(struct drm_gem_object *obj)
                return;
 
Quoted 8 lines

drivers/gpu/drm/drm_gem.c drm_gem_object_put_unlocked(struct drm_gem_object *obj) · line 830

Quoted 4 lines
drivers/gpu/drm/drm_gem.c +0 -1
        dev = obj->dev;
-       might_lock(&dev->struct_mutex);
 
        if (dev->driver->gem_free_object_unlocked)

coding-style nit, if one branch needs {} they all need {}. (The real reason why I didn't want to move might_lock around in this function ;)

Fixed while applying, thanks for your review.
-Daniel

Quoted 16 lines

drivers/gpu/drm/drm_gem.c drm_gem_object_put_unlocked(struct drm_gem_object *obj) · line 839

Quoted 12 lines
drivers/gpu/drm/drm_gem.c +5 -2
                kref_put(&obj->refcount, drm_gem_object_free);
-       else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
+       else {
+               might_lock(&dev->struct_mutex);
+               if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
                                &dev->struct_mutex))
-               mutex_unlock(&dev->struct_mutex);
+                       mutex_unlock(&dev->struct_mutex);
+       }
 }
 EXPORT_SYMBOL(drm_gem_object_put_unlocked);

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

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw
In reply to Daniel Vetter
Quoted 5 lines · Daniel Vetter

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Would something like the below work? Its not pretty, but would give each user of drm_dev_init() its own key.

drivers/gpu/drm/drm_drv.c +5 -4
index 37b8ad3e30d8..06cc32012728 100644
@@ -478,9 +478,10 @@ static void drm_fs_inode_free(struct inode *inode)
  * RETURNS:
  * 0 on success, or error code on failure.
  */
-int drm_dev_init(struct drm_device *dev,
-		 struct drm_driver *driver,
-		 struct device *parent)
+int __drm_dev_init(struct drm_device *dev,
+		   struct drm_driver *driver,
+		   struct device *parent,
+		   struct lock_class_key *key)
 {
 	int ret;
 
@@ -496,7 +497,7 @@ int drm_dev_init(struct drm_device *dev,
 
 	spin_lock_init(&dev->buf_lock);
 	spin_lock_init(&dev->event_lock);
-	mutex_init(&dev->struct_mutex);
+	__mutex_init(&dev->struct_mutex, "&dev->struct_mutex", key);
 	mutex_init(&dev->filelist_mutex);
 	mutex_init(&dev->ctxlist_mutex);
 	mutex_init(&dev->master_mutex);
include/drm/drm_drv.h +11 -3
index 53b98321df9b..cda11e97024e 100644
@@ -531,9 +531,17 @@ void drm_printk(const char *level, unsigned int category,
 		const char *format, ...);
 extern unsigned int drm_debug;
 
-int drm_dev_init(struct drm_device *dev,
-		 struct drm_driver *driver,
-		 struct device *parent);
+int __drm_dev_init(struct drm_device *dev,
+		   struct drm_driver *driver,
+		   struct device *parent,
+		   struct lock_class_key *key);
+
+#define drm_dev_init(_dev, _driver, _parent)			\
+({								\
+	static struct lock_class_key __key;			\
+	__drm_dev_init((_dev), (_driver), (_parent), &__key);	\
+})
+
 void drm_dev_fini(struct drm_device *dev);
 
 struct drm_device *drm_dev_alloc(struct drm_driver *driver,

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

#6 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Daniel Vetter

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Would something like the below work? Its not pretty, but would give each user of drm_dev_init() its own key.

We're very close to getting rid of struct_mutex, neither nouveau.ko nor i915.ko use it, and only about 4 drivers really still need it (out of about 30+). I think the minimal patch is enough, and then not initializing the mutex to prevent more users croping up again in the future.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw
Quoted 8 lines · Daniel Vetter
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Daniel Vetter

A more complete solution would be to do the mutex_init in the drm core only for legacy drivers, plus add it to each modern driver that still needs it, which would also give each its own lockdep key. Trying to do that dynamically doesn't work, because lockdep requires it's keys to be statically allocated.

Quoted 8 lines
Quoted 2 lines

Would something like the below work? Its not pretty, but would give each user of drm_dev_init() its own key.

We're very close to getting rid of struct_mutex, neither nouveau.ko nor i915.ko use it, and only about 4 drivers really still need it (out of about 30+). I think the minimal patch is enough, and then not initializing the mutex to prevent more users croping up again in the future.

Whatever you want of course. I was just going by that one paragraphy I quoted :-)
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel