Skip to contentSkip to conversation

dma-buf/sw_sync: Avoid recursive lock during fence signal

PATCH In review Rob Clark 3 participants 4 messages Aug 18, 2023 – Aug 28, 2023

Subsystems (3)
lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH v2 1/1 dma-buf/sw_sync: Avoid recursive lock during fence signal

From: Rob Clark <[email protected]>

If a signal callback releases the sw_sync fence, that will trigger a deadlock as the timeline_fence_release recurses onto the fence->lock (used both for signaling and the the timeline tree).

To avoid that, temporarily hold an extra reference to the signalled fences until after we drop the lock.

(This is an alternative implementation of https://patchwork.kernel.org/patch/11664717/
which avoids some potential UAF issues with the original patch.)

v2: Remove now obsolete comment, use list_move_tail() and

    list_del_init()
Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")
---
 drivers/dma-buf/sw_sync.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/dma-buf/sw_sync.c +9 -9
index 63f0aeb66db6..f0a35277fd84 100644
@@ -191,6 +191,7 @@ static const struct dma_fence_ops timeline_fence_ops = {
  */
 static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 {
+	LIST_HEAD(signalled);
 	struct sync_pt *pt, *next;
 
 	trace_sync_timeline(obj);
@@ -203,21 +204,20 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 		if (!timeline_fence_signaled(&pt->base))
 			break;
 
-		list_del_init(&pt->link);
+		dma_fence_get(&pt->base);
+
+		list_move_tail(&pt->link, &signalled);
 		rb_erase(&pt->node, &obj->pt_tree);
 
-		/*
-		 * A signal callback may release the last reference to this
-		 * fence, causing it to be freed. That operation has to be
-		 * last to avoid a use after free inside this loop, and must
-		 * be after we remove the fence from the timeline in order to
-		 * prevent deadlocking on timeline->lock inside
-		 * timeline_fence_release().
-		 */
 		dma_fence_signal_locked(&pt->base);
 	}
 
 	spin_unlock_irq(&obj->lock);
+
+	list_for_each_entry_safe(pt, next, &signalled, link) {
+		list_del_init(&pt->link);
+		dma_fence_put(&pt->base);
+	}
 }
 
 /**
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw

Am 18.08.23 um 16:59 schrieb Rob Clark:

Quoted 12 lines

From: Rob Clark <[email protected]>

If a signal callback releases the sw_sync fence, that will trigger a deadlock as the timeline_fence_release recurses onto the fence->lock (used both for signaling and the the timeline tree).

To avoid that, temporarily hold an extra reference to the signalled fences until after we drop the lock.

(This is an alternative implementation of https://patchwork.kernel.org/patch/11664717/
which avoids some potential UAF issues with the original patch.)

Quoted 6 lines

v2: Remove now obsolete comment, use list_move_tail() and

     list_del_init()
Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")
Quoted 46 lines
---
  drivers/dma-buf/sw_sync.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
drivers/dma-buf/sw_sync.c +9 -9
index 63f0aeb66db6..f0a35277fd84 100644
@@ -191,6 +191,7 @@ static const struct dma_fence_ops timeline_fence_ops = {
   */
  static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  {
+	LIST_HEAD(signalled);
  	struct sync_pt *pt, *next;
 
  	trace_sync_timeline(obj);
@@ -203,21 +204,20 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  		if (!timeline_fence_signaled(&pt->base))
  			break;
 
-		list_del_init(&pt->link);
+		dma_fence_get(&pt->base);
+
+		list_move_tail(&pt->link, &signalled);
  		rb_erase(&pt->node, &obj->pt_tree);
 
-		/*
-		 * A signal callback may release the last reference to this
-		 * fence, causing it to be freed. That operation has to be
-		 * last to avoid a use after free inside this loop, and must
-		 * be after we remove the fence from the timeline in order to
-		 * prevent deadlocking on timeline->lock inside
-		 * timeline_fence_release().
-		 */
  		dma_fence_signal_locked(&pt->base);
  	}
 
  	spin_unlock_irq(&obj->lock);
+
+	list_for_each_entry_safe(pt, next, &signalled, link) {
+		list_del_init(&pt->link);
+		dma_fence_put(&pt->base);
+	}
  }
 
  /**
author
#2 lore raw
Quoted 14 lines · Christian König

Am 18.08.23 um 16:59 schrieb Rob Clark:

Quoted 12 lines

From: Rob Clark <[email protected]>

If a signal callback releases the sw_sync fence, that will trigger a deadlock as the timeline_fence_release recurses onto the fence->lock (used both for signaling and the the timeline tree).

To avoid that, temporarily hold an extra reference to the signalled fences until after we drop the lock.

(This is an alternative implementation of https://patchwork.kernel.org/patch/11664717/
which avoids some potential UAF issues with the original patch.)

Quoted 8 lines
Quoted 6 lines

v2: Remove now obsolete comment, use list_move_tail() and

     list_del_init()
Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")

Thanks, any chance you could take this via drm-misc?

BR,
-R

Quoted 48 lines
Quoted 47 lines
---
  drivers/dma-buf/sw_sync.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
drivers/dma-buf/sw_sync.c +9 -9
index 63f0aeb66db6..f0a35277fd84 100644
@@ -191,6 +191,7 @@ static const struct dma_fence_ops timeline_fence_ops = {
   */
  static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  {
+     LIST_HEAD(signalled);
      struct sync_pt *pt, *next;
 
      trace_sync_timeline(obj);
@@ -203,21 +204,20 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
              if (!timeline_fence_signaled(&pt->base))
                      break;
 
-             list_del_init(&pt->link);
+             dma_fence_get(&pt->base);
+
+             list_move_tail(&pt->link, &signalled);
              rb_erase(&pt->node, &obj->pt_tree);
 
-             /*
-              * A signal callback may release the last reference to this
-              * fence, causing it to be freed. That operation has to be
-              * last to avoid a use after free inside this loop, and must
-              * be after we remove the fence from the timeline in order to
-              * prevent deadlocking on timeline->lock inside
-              * timeline_fence_release().
-              */
              dma_fence_signal_locked(&pt->base);
      }
 
      spin_unlock_irq(&obj->lock);
+
+     list_for_each_entry_safe(pt, next, &signalled, link) {
+             list_del_init(&pt->link);
+             dma_fence_put(&pt->base);
+     }
  }
 
  /**
maintainer
#3 lore raw

Am 22.08.23 um 19:15 schrieb Rob Clark:

Quoted 15 lines
Quoted 13 lines · Christian König

Am 18.08.23 um 16:59 schrieb Rob Clark:

Quoted 12 lines

From: Rob Clark <[email protected]>

If a signal callback releases the sw_sync fence, that will trigger a deadlock as the timeline_fence_release recurses onto the fence->lock (used both for signaling and the the timeline tree).

To avoid that, temporarily hold an extra reference to the signalled fences until after we drop the lock.

(This is an alternative implementation of https://patchwork.kernel.org/patch/11664717/
which avoids some potential UAF issues with the original patch.)

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

v2: Remove now obsolete comment, use list_move_tail() and

      list_del_init()
Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")

Thanks, any chance you could take this via drm-misc?

I've already pushed this quite a while ago.

At the moment I have problem answering because AMD has a new security policy which makes it impossible to push patches and access mails at the same time.

We are working with our IT to get this fixed, but at the moment its eating my time.

Sorry for the delay,
Christian.

Quoted 50 lines

BR,
-R

Quoted 46 lines
Quoted 46 lines
---
   drivers/dma-buf/sw_sync.c | 18 +++++++++---------
   1 file changed, 9 insertions(+), 9 deletions(-)
drivers/dma-buf/sw_sync.c +9 -9
index 63f0aeb66db6..f0a35277fd84 100644
@@ -191,6 +191,7 @@ static const struct dma_fence_ops timeline_fence_ops = {
    */
   static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
   {
+     LIST_HEAD(signalled);
       struct sync_pt *pt, *next;
 
       trace_sync_timeline(obj);
@@ -203,21 +204,20 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
               if (!timeline_fence_signaled(&pt->base))
                       break;
 
-             list_del_init(&pt->link);
+             dma_fence_get(&pt->base);
+
+             list_move_tail(&pt->link, &signalled);
               rb_erase(&pt->node, &obj->pt_tree);
 
-             /*
-              * A signal callback may release the last reference to this
-              * fence, causing it to be freed. That operation has to be
-              * last to avoid a use after free inside this loop, and must
-              * be after we remove the fence from the timeline in order to
-              * prevent deadlocking on timeline->lock inside
-              * timeline_fence_release().
-              */
               dma_fence_signal_locked(&pt->base);
       }
 
       spin_unlock_irq(&obj->lock);
+
+     list_for_each_entry_safe(pt, next, &signalled, link) {
+             list_del_init(&pt->link);
+             dma_fence_put(&pt->base);
+     }
   }
 
   /**