Skip to contentSkip to conversation

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

PATCH Superseded Rob Clark 2 participants 3 messages Aug 17, 2023 – Aug 18, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 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.)

Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")
---
 drivers/dma-buf/sw_sync.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/dma-buf/sw_sync.c +10 -0
index 63f0aeb66db6..ceb6a0408624 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,9 +204,13 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 		if (!timeline_fence_signaled(&pt->base))
 			break;
 
+		dma_fence_get(&pt->base);
+
 		list_del_init(&pt->link);
 		rb_erase(&pt->node, &obj->pt_tree);
 
+		list_add_tail(&pt->link, &signalled);
+
 		/*
 		 * A signal callback may release the last reference to this
 		 * fence, causing it to be freed. That operation has to be
@@ -218,6 +223,11 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 	}
 
 	spin_unlock_irq(&obj->lock);
+
+	list_for_each_entry_safe(pt, next, &signalled, link) {
+		list_del(&pt->link);
+		dma_fence_put(&pt->base);
+	}
 }
 
 /**
Signature · 1 line
2.41.0
maintainer
#1 lore raw

Am 17.08.23 um 23:37 schrieb Rob Clark:

Quoted 32 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.)

Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")
---
  drivers/dma-buf/sw_sync.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/dma-buf/sw_sync.c +1 -0
index 63f0aeb66db6..ceb6a0408624 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);

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 207

Quoted 4 lines
drivers/dma-buf/sw_sync.c +1 -0
@@ -203,9 +204,13 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  		if (!timeline_fence_signaled(&pt->base))
  			break;
 
+		dma_fence_get(&pt->base);

Question is why don't have the fences a reference on the list in the first place?

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 212

Quoted 5 lines
drivers/dma-buf/sw_sync.c +2 -0
+
  		list_del_init(&pt->link);
  		rb_erase(&pt->node, &obj->pt_tree);
 
+		list_add_tail(&pt->link, &signalled);

Instead of list_del()/list_add_tail() you could also use list_move_tail() here.

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 228

Quoted 11 lines
drivers/dma-buf/sw_sync.c +4 -0
+
  		/*
  		 * A signal callback may release the last reference to this
  		 * fence, causing it to be freed. That operation has to be
@@ -218,6 +223,11 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  	}
 
  	spin_unlock_irq(&obj->lock);
+
+	list_for_each_entry_safe(pt, next, &signalled, link) {
+		list_del(&pt->link);

You must use list_del_init() here or otherwise the pt->link will keep pointing to the prev/next entries and the list_empty() check in timeline_fence_release() will fail and potentially corrupt things.

Regards,
Christian.

Quoted 5 lines
drivers/dma-buf/sw_sync.c +2 -0
+		dma_fence_put(&pt->base);
+	}
  }
 
  /**
author
#2 lore raw
Quoted 37 lines · Christian König

Am 17.08.23 um 23:37 schrieb Rob Clark:

Quoted 35 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.)

Fixes d3c6dd1fb30d ("dma-buf/sw_sync: Synchronize signal vs syncpt free")
---
  drivers/dma-buf/sw_sync.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/dma-buf/sw_sync.c +1 -0
index 63f0aeb66db6..ceb6a0408624 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,9 +204,13 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
              if (!timeline_fence_signaled(&pt->base))
                      break;
 
Quoted 4 lines

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 207

Quoted 1 line
drivers/dma-buf/sw_sync.c +1 -0
+             dma_fence_get(&pt->base);

Question is why don't have the fences a reference on the list in the first place?

As best I can tell, it is because the fences hold a reference to the timeline, so a reference in the other direction would cause a loop.

BR,
-R

Quoted 34 lines

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 212

Quoted 5 lines
drivers/dma-buf/sw_sync.c +2 -0
+
              list_del_init(&pt->link);
              rb_erase(&pt->node, &obj->pt_tree);
 
+             list_add_tail(&pt->link, &signalled);

Instead of list_del()/list_add_tail() you could also use list_move_tail() here.

drivers/dma-buf/sw_sync.c static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) · line 228

Quoted 11 lines
drivers/dma-buf/sw_sync.c +4 -0
+
              /*
               * A signal callback may release the last reference to this
               * fence, causing it to be freed. That operation has to be
@@ -218,6 +223,11 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
      }
 
      spin_unlock_irq(&obj->lock);
+
+     list_for_each_entry_safe(pt, next, &signalled, link) {
+             list_del(&pt->link);

You must use list_del_init() here or otherwise the pt->link will keep pointing to the prev/next entries and the list_empty() check in timeline_fence_release() will fail and potentially corrupt things.

Regards,
Christian.

Quoted 5 lines
drivers/dma-buf/sw_sync.c +2 -0
+             dma_fence_put(&pt->base);
+     }
  }
 
  /**