diff mbox

[2/3] dma-buf/sw_sync: clean up list before signaling the fence

Message ID 20170729152217.8362-2-gustavo@padovan.org (mailing list archive)
State New, archived
Headers show

Commit Message

Gustavo Padovan July 29, 2017, 3:22 p.m. UTC
From: Gustavo Padovan <gustavo.padovan@collabora.com>

If userspace already dropped its own reference by closing the sw_sync
fence fd we might end up in a deadlock where
dma_fence_is_signaled_locked() will trigger the release of the fence and
thus try to hold the lock to remove the fence from the list.

dma_fence_is_signaled_locked() tries to release/free the fence and hold
the lock in the process.

We fix that by changing the order operation and clean up the list and
rb-tree first.

v2: Drop the fence get/put dance and manipulate the list first (Chris Wilson)

Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.com>
---
 drivers/dma-buf/sw_sync.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

Chris Wilson July 30, 2017, 9:10 a.m. UTC | #1
Quoting Gustavo Padovan (2017-07-29 16:22:16)
> From: Gustavo Padovan <gustavo.padovan@collabora.com>
> 
> If userspace already dropped its own reference by closing the sw_sync
> fence fd we might end up in a deadlock where
> dma_fence_is_signaled_locked() will trigger the release of the fence and
> thus try to hold the lock to remove the fence from the list.
> 
> dma_fence_is_signaled_locked() tries to release/free the fence and hold
> the lock in the process.
> 
> We fix that by changing the order operation and clean up the list and
> rb-tree first.
> 
> v2: Drop the fence get/put dance and manipulate the list first (Chris Wilson)
> 
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.com>
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
-Chris
Gustavo Padovan July 31, 2017, 7:32 p.m. UTC | #2
2017-07-30 Chris Wilson <chris@chris-wilson.co.uk>:

> Quoting Gustavo Padovan (2017-07-29 16:22:16)
> > From: Gustavo Padovan <gustavo.padovan@collabora.com>
> > 
> > If userspace already dropped its own reference by closing the sw_sync
> > fence fd we might end up in a deadlock where
> > dma_fence_is_signaled_locked() will trigger the release of the fence and
> > thus try to hold the lock to remove the fence from the list.
> > 
> > dma_fence_is_signaled_locked() tries to release/free the fence and hold
> > the lock in the process.
> > 
> > We fix that by changing the order operation and clean up the list and
> > rb-tree first.
> > 
> > v2: Drop the fence get/put dance and manipulate the list first (Chris Wilson)
> > 
> > Cc: Chris Wilson <chris@chris-wilson.co.uk>
> > Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.com>
> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>

Thanks for reviewing. Pushed to drm-misc-next.

	Gustavo
diff mbox

Patch

diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
index ef0cc08..38cc738 100644
--- a/drivers/dma-buf/sw_sync.c
+++ b/drivers/dma-buf/sw_sync.c
@@ -213,11 +213,21 @@  static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
 	obj->value += inc;
 
 	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-		if (!dma_fence_is_signaled_locked(&pt->base))
+		if (!timeline_fence_signaled(&pt->base))
 			break;
 
 		list_del_init(&pt->link);
 		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);