Skip to contentSkip to conversation

drm/syncobj: make lockdep complain on WAIT_FOR_SUBMIT v3

PATCH In review Christian König 3 participants 3 messages Jan 19, 2021

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

PATCH 1/1 drm/syncobj: make lockdep complain on WAIT_FOR_SUBMIT v3

DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT can't be used when we hold locks since we are basically waiting for userspace to do something.

Holding a lock while doing so can trivial deadlock with page faults etc...

So make lockdep complain when a driver tries to do this.

v2: Add lockdep_assert_none_held() macro.
v3: Add might_sleep() and also use lockdep_assert_none_held() in the
IOCTL path.

---
 drivers/gpu/drm/drm_syncobj.c | 12 ++++++++++++
 include/linux/lockdep.h       |  5 +++++
 2 files changed, 17 insertions(+)
drivers/gpu/drm/drm_syncobj.c +12 -0
index 6e74e6745eca..9ecfec1e5567 100644
@@ -387,6 +387,15 @@ int drm_syncobj_find_fence(struct drm_file *file_private,
 	if (!syncobj)
 		return -ENOENT;
 
+	/* Waiting for userspace with locks help is illegal cause that can
+	 * trivial deadlock with page faults for example. Make lockdep complain
+	 * about it early on.
+	 */
+	if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
+		might_sleep();
+		lockdep_assert_none_held_once();
+	}
+
 	*fence = drm_syncobj_fence_get(syncobj);
 	drm_syncobj_put(syncobj);
 
@@ -940,6 +949,9 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
 	uint64_t *points;
 	uint32_t signaled_count, i;
 
+	if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)
+		lockdep_assert_none_held_once();
+
 	points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
 	if (points == NULL)
 		return -ENOMEM;
include/linux/lockdep.h +5 -0
index b9e9adec73e8..6eb117c0d0f3 100644
@@ -310,6 +310,10 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
 		WARN_ON_ONCE(debug_locks && !lockdep_is_held(l));	\
 	} while (0)
 
+#define lockdep_assert_none_held_once()	do {				\
+		WARN_ON_ONCE(debug_locks && current->lockdep_depth);	\
+	} while (0)
+
 #define lockdep_recursing(tsk)	((tsk)->lockdep_recursion)
 
 #define lockdep_pin_lock(l)	lock_pin_lock(&(l)->dep_map)
@@ -387,6 +391,7 @@ extern int lockdep_is_held(const void *);
 #define lockdep_assert_held_write(l)	do { (void)(l); } while (0)
 #define lockdep_assert_held_read(l)		do { (void)(l); } while (0)
 #define lockdep_assert_held_once(l)		do { (void)(l); } while (0)
+#define lockdep_assert_none_held_once()	do { } while (0)
 
 #define lockdep_recursing(tsk)			(0)
 
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Christian König

DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT can't be used when we hold locks since we are basically waiting for userspace to do something.

Holding a lock while doing so can trivial deadlock with page faults etc...

So make lockdep complain when a driver tries to do this.

v2: Add lockdep_assert_none_held() macro.
v3: Add might_sleep() and also use lockdep_assert_none_held() in the
IOCTL path.

---
 drivers/gpu/drm/drm_syncobj.c | 12 ++++++++++++
 include/linux/lockdep.h       |  5 +++++
 2 files changed, 17 insertions(+)
drivers/gpu/drm/drm_syncobj.c +1 -0
index 6e74e6745eca..9ecfec1e5567 100644
@@ -387,6 +387,15 @@ int drm_syncobj_find_fence(struct drm_file *file_private,
        if (!syncobj)
                return -ENOENT;
 
+       /* Waiting for userspace with locks help is illegal cause that can

drivers/gpu/drm/drm_syncobj.c int drm_syncobj_find_fence(struct drm_file *file_private, · line 400

Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +8 -0
+        * trivial deadlock with page faults for example. Make lockdep complain
+        * about it early on.
+        */
+       if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
+               might_sleep();
+               lockdep_assert_none_held_once();
+       }
+
        *fence = drm_syncobj_fence_get(syncobj);
        drm_syncobj_put(syncobj);

I noticed a use-after-free here that goes back to 5.2, sending out a patch for that.

Quoted 27 lines
drivers/gpu/drm/drm_syncobj.c +3 -0
@@ -940,6 +949,9 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
        uint64_t *points;
        uint32_t signaled_count, i;
 
+       if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)
+               lockdep_assert_none_held_once();
+
        points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
        if (points == NULL)
                return -ENOMEM;
include/linux/lockdep.h +4 -0
index b9e9adec73e8..6eb117c0d0f3 100644
@@ -310,6 +310,10 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
                WARN_ON_ONCE(debug_locks && !lockdep_is_held(l));       \
        } while (0)
 
+#define lockdep_assert_none_held_once()        do {                            \
+               WARN_ON_ONCE(debug_locks && current->lockdep_depth);    \
+       } while (0)
+
 #define lockdep_recursing(tsk) ((tsk)->lockdep_recursion)
 
 #define lockdep_pin_lock(l)    lock_pin_lock(&(l)->dep_map)

include/linux/lockdep.h extern int lockdep_is_held(const void *); · line 396

Quoted 6 lines
include/linux/lockdep.h +1 -0
@@ -387,6 +391,7 @@ extern int lockdep_is_held(const void *);
 #define lockdep_assert_held_write(l)   do { (void)(l); } while (0)
 #define lockdep_assert_held_read(l)            do { (void)(l); } while (0)
 #define lockdep_assert_held_once(l)            do { (void)(l); } while (0)
+#define lockdep_assert_none_held_once()        do { } while (0)
 
 #define lockdep_recursing(tsk)                 (0)

ofc needs ack from Peter, but drm parts look all good to me.

Quoted 4 lines
include/linux/lockdep.h +0 -1
--

2.25.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer Acked-by
#2 lore raw
Quoted 21 lines · Daniel Vetter
Quoted 21 lines
include/linux/lockdep.h +5 -0
index b9e9adec73e8..6eb117c0d0f3 100644
@@ -310,6 +310,10 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
                WARN_ON_ONCE(debug_locks && !lockdep_is_held(l));       \
        } while (0)
 
+#define lockdep_assert_none_held_once()        do {                            \
+               WARN_ON_ONCE(debug_locks && current->lockdep_depth);    \
+       } while (0)
+
 #define lockdep_recursing(tsk) ((tsk)->lockdep_recursion)
 
 #define lockdep_pin_lock(l)    lock_pin_lock(&(l)->dep_map)
@@ -387,6 +391,7 @@ extern int lockdep_is_held(const void *);
 #define lockdep_assert_held_write(l)   do { (void)(l); } while (0)
 #define lockdep_assert_held_read(l)            do { (void)(l); } while (0)
 #define lockdep_assert_held_once(l)            do { (void)(l); } while (0)
+#define lockdep_assert_none_held_once()        do { } while (0)
 
Quoted 3 lines

include/linux/lockdep.h extern int lockdep_is_held(const void *); · line 396

Quoted 1 line
include/linux/lockdep.h +0 -0
 #define lockdep_recursing(tsk)                 (0)

ofc needs ack from Peter, but drm parts look all good to me.

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