Skip to contentSkip to conversation

drm/i915: fix wait_for_pending_flips vs gpu hang deadlock

PATCH In review Daniel Vetter 3 participants 10 messages Sep 8, 2013 – Sep 9, 2013

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

PATCH 1/1 drm/i915: fix wait_for_pending_flips vs gpu hang deadlock

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

---
 drivers/gpu/drm/i915/i915_irq.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +18 -2
index 83cce0c..4fc54cb 100644
@@ -1506,8 +1506,16 @@ static void i915_error_work_func(struct work_struct *work)
 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
 				   reset_event);
 
+		/*
+		 * All state reset _must_ have completed before we update the
+		 * reset counter, for otherwise waiters might miss the reset
+		 * pending state and not properly drop locks, resuling in
+		 * deadlocks with the reset work.
+		 */
 		ret = i915_reset(dev);
 
+		intel_display_handle_reset(dev);
+
 		if (ret == 0) {
 			/*
 			 * After all the gem state is reset, increment the reset
@@ -1531,7 +1539,11 @@ static void i915_error_work_func(struct work_struct *work)
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
 
-		intel_display_handle_reset(dev);
+		/*
+		 * intel_display_handle_reset already wakes up all waiters on
+		 * the dev_priv->pending_flip_queue by (eventually) calling down
+		 * into do_finish_flip. So no need to wake them up again.
+		 */
 
 		wake_up_all(&dev_priv->gpu_error.reset_queue);
 	}
@@ -1654,10 +1666,14 @@ void i915_handle_error(struct drm_device *dev, bool wedged)
 
 		/*
 		 * Wakeup waiting processes so that the reset work item
-		 * doesn't deadlock trying to grab various locks.
+		 * doesn't deadlock trying to grab various locks. We must do
+		 * this for _all_ gpu waiters that might hold locks that the
+		 * reset work needs to acquire.
 		 */
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
+
+		wake_up_all(&dev_priv->pending_flip_queue);
 	}
 
 	/*
Signature · 1 line
1.8.4.rc3
#1 lore raw
Quoted 24 lines · Daniel Vetter

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Quoted 10 lines

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

I would favour the redundant wake_up() in order to move the common pre/post wake_ups to a common function: i915_error_wake_up() ?

Quoted 27 lines

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

---
 drivers/gpu/drm/i915/i915_irq.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +0 -0
index 83cce0c..4fc54cb 100644

drivers/gpu/drm/i915/i915_irq.c static void i915_error_work_func(struct work_struct *work) · line 1510

Quoted 5 lines
drivers/gpu/drm/i915/i915_irq.c +2 -0
@@ -1506,8 +1506,16 @@ static void i915_error_work_func(struct work_struct *work)
 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
 				   reset_event);
 
+		/*
+		 * All state reset _must_ have completed before we update the

_must_ be completed

Make it active, make it a command!

drivers/gpu/drm/i915/i915_irq.c static void i915_error_work_func(struct work_struct *work) · line 1512

Quoted 2 lines
drivers/gpu/drm/i915/i915_irq.c +2 -0
+		 * reset counter, for otherwise waiters might miss the reset
+		 * pending state and not properly drop locks, resuling in

resulting

Quoted 10 lines
drivers/gpu/drm/i915/i915_irq.c +4 -0
+		 * deadlocks with the reset work.
+		 */
 		ret = i915_reset(dev);
 
+		intel_display_handle_reset(dev);
+
 		if (ret == 0) {
 			/*
 			 * After all the gem state is reset, increment the reset

drivers/gpu/drm/i915/i915_irq.c static void i915_error_work_func(struct work_struct *work) · line 1546

Quoted 9 lines
drivers/gpu/drm/i915/i915_irq.c +5 -1
@@ -1531,7 +1539,11 @@ static void i915_error_work_func(struct work_struct *work)
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
 
-		intel_display_handle_reset(dev);
+		/*
+		 * intel_display_handle_reset already wakes up all waiters on
+		 * the dev_priv->pending_flip_queue by (eventually) calling down
+		 * into do_finish_flip. So no need to wake them up again.
+		 */

I'm favouring dropping this comment, since calling wake_up() after the flips have already completed should be a no-op.

i915_error_wake_up();

drivers/gpu/drm/i915/i915_irq.c void i915_handle_error(struct drm_device *dev, bool wedged) · line 1668

Quoted 7 lines
drivers/gpu/drm/i915/i915_irq.c +0 -0
 		wake_up_all(&dev_priv->gpu_error.reset_queue);
 	}
@@ -1654,10 +1666,14 @@ void i915_handle_error(struct drm_device *dev, bool wedged)
 
 		/*
 		 * Wakeup waiting processes so that the reset work item

Care to change this to explicitly reference i915_error_work_func() instead of "reset work item".

Wakeup processes waiting for completion events from the GPU so that the the reset work item, i915_error_work_func(), doesn't deadlock trying to grab the various locks being they hold. By bumping the reset counter first, the woken processes will see a reset in progress and back off, releasing their locks and then wait for the reset completion. We must do this for _all_ GPU waiters that might hold locks that i915_error_work_func() and the reset code needs to acquire.

works for me.

drivers/gpu/drm/i915/i915_irq.c void i915_handle_error(struct drm_device *dev, bool wedged) · line 1676

Quoted 9 lines
drivers/gpu/drm/i915/i915_irq.c +5 -1
-		 * doesn't deadlock trying to grab various locks.
+		 * doesn't deadlock trying to grab various locks. We must do
+		 * this for _all_ gpu waiters that might hold locks that the
+		 * reset work needs to acquire.
 		 */
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
+
+		wake_up_all(&dev_priv->pending_flip_queue);

i915_error_wake_up();

or i915_error_wake_up_mutexes()? (Though this is slightly misleading, but it is the intent.) Not 100% convinced that making using a common function is right semantically...
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#2 lore raw
Quoted 1 line · Chris Wilson

On Sun, Sep 08, 2013 at 01:30:17PM +0200, Daniel Vetter wrote:

[Snip]

I'll frob the comments a bit as suggested.

Quoted 11 lines

drivers/gpu/drm/i915/i915_irq.c void i915_handle_error(struct drm_device *dev, bool wedged) · line 1676

Quoted 8 lines
drivers/gpu/drm/i915/i915_irq.c +5 -0
+		 * doesn't deadlock trying to grab various locks. We must do
+		 * this for _all_ gpu waiters that might hold locks that the
+		 * reset work needs to acquire.
 		 */
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
+
+		wake_up_all(&dev_priv->pending_flip_queue);

i915_error_wake_up();

Quoted 3 lines

or i915_error_wake_up_mutexes()? (Though this is slightly misleading, but it is the intent.) Not 100% convinced that making using a common function is right semantically...

Yeah, I'm also a bit unhappy how unstructured this magic dance is. Semantically I think the two wake-ups aren't the same:

- The first wakeups (after we've set the pending reset flag) is just to get lock holders to bail out and drop their locks.

- The second set of wakupes (after the reset has completed) is to signal waiters that a state change has happened. I'm not really sure whether we need this second set of wake-ups (with the exceptoin of gpu_erro.reset_queue of cours) since waiters shouldn't be able to miss the reset pending state. And if they block after the reset has completed they should see the consistent post-reset state and nothing else.

Hm, on second thought we need the wake-ups to serve as an implicit barrier.

Another thing I've tried to ponder is whether we could enlist the help of lockdep somehow to mark up these critical sections. But I dit not see a way to do this so that lockdep would complain if we'd miss a wakup for a special lock ...

Hence why I've decided to stick with comments for now until I have a good idea to systematically ensure these deadlocks are gone. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#3 lore raw
Quoted 34 lines · Daniel Vetter
Quoted 1 line · Chris Wilson

On Sun, Sep 08, 2013 at 01:30:17PM +0200, Daniel Vetter wrote:

[Snip]

I'll frob the comments a bit as suggested.

Quoted 11 lines

drivers/gpu/drm/i915/i915_irq.c void i915_handle_error(struct drm_device *dev, bool wedged) · line 1676

Quoted 8 lines
drivers/gpu/drm/i915/i915_irq.c +5 -0
+		 * doesn't deadlock trying to grab various locks. We must do
+		 * this for _all_ gpu waiters that might hold locks that the
+		 * reset work needs to acquire.
 		 */
 		for_each_ring(ring, dev_priv, i)
 			wake_up_all(&ring->irq_queue);
+
+		wake_up_all(&dev_priv->pending_flip_queue);

i915_error_wake_up();

Quoted 3 lines

or i915_error_wake_up_mutexes()? (Though this is slightly misleading, but it is the intent.) Not 100% convinced that making using a common function is right semantically...

Yeah, I'm also a bit unhappy how unstructured this magic dance is. Semantically I think the two wake-ups aren't the same:

- The first wakeups (after we've set the pending reset flag) is just to get lock holders to bail out and drop their locks.

- The second set of wakupes (after the reset has completed) is to signal waiters that a state change has happened. I'm not really sure whether we need this second set of wake-ups (with the exceptoin of gpu_erro.reset_queue of cours) since waiters shouldn't be able to miss the reset pending state. And if they block after the reset has completed they should see the consistent post-reset state and nothing else.

Quoted 7 lines

Hm, on second thought we need the wake-ups to serve as an implicit barrier.

Another thing I've tried to ponder is whether we could enlist the help of lockdep somehow to mark up these critical sections. But I dit not see a way to do this so that lockdep would complain if we'd miss a wakup for a special lock ...

To elaborate a bit on why I think lockdep would be suitable: The entire time the pending reset flag is set is in a way a special critical section. Lockdep doesn't support acquire/release from different threads, but we could easily fix that by shuffling a few things around.

Blocking on the gpu otoh is like a read-lock on that gpu reset critical section. So to make locking sound we need to make sure we always have trylock semantics.

One way to do this would be by using a specila wait queue in addition to all the ones we already have just to signal gpu resets. And a special macro like i915_wait_event that adds the process to both waitqueues and encapsulates the reset pending handling.

With that lockdep would complain if we acquire a new lock in the reset handler or if we add a new gpu waiter. But atm lockdep doesn correctly report read vs. write lock deadlocks, so meh.

Also there's kinda no way to ensure waiters don't use one of the plaine wait_event macros. So I think overall not worth the effort. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#4 lore raw

PATCH 1/1 drm/i916: fix wait_for_pending_flips vs gpu hang deadlock

In reply to Chris Wilson

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

v2:
- Add comments to explain how the wake_up serves as memory barriers for the atomic_t reset counter.
- Improve the comments a bit as suggested by Chris Wilson.
- Extract the wake_up calls before/after the reset into a little i915_error_wake_up and unconditionally wake up the pending_flip_queue waiters, again as suggested by Chris Wilson.

---
 drivers/gpu/drm/i915/i915_irq.c | 55 ++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 14 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +41 -14
index 83cce0c..2331739 100644
@@ -1469,6 +1469,21 @@ static irqreturn_t ironlake_irq_handler(int irq, void *arg)
 	return ret;
 }
 
+static void i915_error_wake_up(struct drm_i915_private *dev_priv,
+			       bool reset_completed)
+{
+	struct intel_ring_buffer *ring;
+	int i;
+
+	for_each_ring(ring, dev_priv, i)
+		wake_up_all(&ring->irq_queue);
+
+	wake_up_all(&dev_priv->pending_flip_queue);
+
+	if (reset_completed)
+		wake_up_all(&dev_priv->gpu_error.reset_queue);
+}
+
 /**
  * i915_error_work_func - do process context error handling work
  * @work: work struct
@@ -1483,11 +1498,10 @@ static void i915_error_work_func(struct work_struct *work)
 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
 						    gpu_error);
 	struct drm_device *dev = dev_priv->dev;
-	struct intel_ring_buffer *ring;
 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
-	int i, ret;
+	int ret;
 
 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
 
@@ -1506,8 +1520,16 @@ static void i915_error_work_func(struct work_struct *work)
 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
 				   reset_event);
 
+		/*
+		 * All state reset _must_ be completed before we update the
+		 * reset counter, for otherwise waiters might miss the reset
+		 * pending state and not properly drop locks, resulting in
+		 * deadlocks with the reset work.
+		 */
 		ret = i915_reset(dev);
 
+		intel_display_handle_reset(dev);
+
 		if (ret == 0) {
 			/*
 			 * After all the gem state is reset, increment the reset
@@ -1528,12 +1550,11 @@ static void i915_error_work_func(struct work_struct *work)
 			atomic_set(&error->reset_counter, I915_WEDGED);
 		}
 
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
-
-		intel_display_handle_reset(dev);
-
-		wake_up_all(&dev_priv->gpu_error.reset_queue);
+		/*
+		 * Note: The wake_up also serves as a memory barrier so that
+		 * waiters see the update value of the reset counter atomic_t.
+		 */
+		i915_error_wake_up(dev_priv, true);
 	}
 }
 
@@ -1642,8 +1663,6 @@ static void i915_report_and_clear_eir(struct drm_device *dev)
 void i915_handle_error(struct drm_device *dev, bool wedged)
 {
 	struct drm_i915_private *dev_priv = dev->dev_private;
-	struct intel_ring_buffer *ring;
-	int i;
 
 	i915_capture_error_state(dev);
 	i915_report_and_clear_eir(dev);
@@ -1653,11 +1672,19 @@ void i915_handle_error(struct drm_device *dev, bool wedged)
 				&dev_priv->gpu_error.reset_counter);
 
 		/*
-		 * Wakeup waiting processes so that the reset work item
-		 * doesn't deadlock trying to grab various locks.
+		 * Wakeup waiting processes so that the reset work function
+		 * i915_error_work_func doesn't deadlock trying to grab various
+		 * locks. By bumping the reset counter first, the woken
+		 * processes will see a reset in progress and back off,
+		 * releasing their locks and then wait for the reset completion.
+		 * We must do this for _all_ gpu waiters that might hold locks
+		 * that the reset work needs to acquire.
+		 *
+		 * Note: The wake_up serves as the required memory barrier to
+		 * ensure that the waiters see the updated value of the reset
+		 * counter atomic_t.
 		 */
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
+		i915_error_wake_up(dev_priv, false);
 	}
 
 	/*
Signature · 1 line
1.8.4.rc3
Reviewed-by on v1 1/1
#5 lore raw

Re: [PATCH] drm/i916: fix wait_for_pending_flips vs gpu hang deadlock

Quoted 69 lines · Daniel Vetter

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

v2:
- Add comments to explain how the wake_up serves as memory barriers for the atomic_t reset counter.
- Improve the comments a bit as suggested by Chris Wilson.
- Extract the wake_up calls before/after the reset into a little i915_error_wake_up and unconditionally wake up the pending_flip_queue waiters, again as suggested by Chris Wilson.

---
 drivers/gpu/drm/i915/i915_irq.c | 55 ++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 14 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +0 -0
index 83cce0c..2331739 100644

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1476

Quoted 8 lines
drivers/gpu/drm/i915/i915_irq.c +5 -0
@@ -1469,6 +1469,21 @@ static irqreturn_t ironlake_irq_handler(int irq, void *arg)
 	return ret;
 }
 
+static void i915_error_wake_up(struct drm_i915_private *dev_priv,
+			       bool reset_completed)
+{
+	struct intel_ring_buffer *ring;
+	int i;

Might as well make that memory barrier explicit:
smb_mb__after_atomic_inc();

The cost of an additional mb is insignificant here.

Something like:

/* Notify all waiters for GPU completion events that reset state has
* been changed, and that they need to restart their wait after
* checking for potential errors.
*/

I don't mind repeating ourselves as this logic impacts faraway logic in i915_gem and intel_display etc.

/* Wakeup __wait_seqno() [dev->struct_mutex] */

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1480

Quoted 3 lines
drivers/gpu/drm/i915/i915_irq.c +3 -0
+	for_each_ring(ring, dev_priv, i)
+		wake_up_all(&ring->irq_queue);
+

/* Wakeup intel_crtc_wait_for_pending_flips() [dev->mode_config.lock] */

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1481

Quoted 2 lines
drivers/gpu/drm/i915/i915_irq.c +2 -0
+	wake_up_all(&dev_priv->pending_flip_queue);
+

/* Wakeup our error checking mutex after we have finished reseting the GPU,
i915_gem_wait_for_error() [no locks held by the waiters] */

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1485

Quoted 3 lines
drivers/gpu/drm/i915/i915_irq.c +3 -0
+	if (reset_completed)
+		wake_up_all(&dev_priv->gpu_error.reset_queue);
+}

With some additional comments to drive home i915_error_wake_up(),

-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#6 lore raw

Re: [Intel-gfx] [PATCH] drm/i916: fix wait_for_pending_flips vs gpu hang deadlock

Oh, and s/i916/i915/
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#7 lore raw

Re: [PATCH] drm/i916: fix wait_for_pending_flips vs gpu hang deadlock

In reply to Chris Wilson
Quoted 10 lines · Chris Wilson

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1476

Quoted 5 lines
drivers/gpu/drm/i915/i915_irq.c +5 -0
+static void i915_error_wake_up(struct drm_i915_private *dev_priv,
+                            bool reset_completed)
+{
+     struct intel_ring_buffer *ring;
+     int i;

Might as well make that memory barrier explicit:
smb_mb__after_atomic_inc();

The cost of an additional mb is insignificant here.

Imo the barrier nature between waker and wakee is established enough that we don't need to throw another barrier in here.

Quoted 10 lines

Something like:

/* Notify all waiters for GPU completion events that reset state has
* been changed, and that they need to restart their wait after
* checking for potential errors.
*/

I don't mind repeating ourselves as this logic impacts faraway logic in i915_gem and intel_display etc.

Quoted 6 lines

/* Wakeup __wait_seqno() [dev->struct_mutex] */

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1480

Quoted 3 lines
drivers/gpu/drm/i915/i915_irq.c +3 -0
+     for_each_ring(ring, dev_priv, i)
+             wake_up_all(&ring->irq_queue);
+

/* Wakeup intel_crtc_wait_for_pending_flips() [dev->mode_config.lock] */

Actually we only care about crtc->mutex.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     wake_up_all(&dev_priv->pending_flip_queue);
+

/* Wakeup our error checking mutex after we have finished reseting the GPU,
i915_gem_wait_for_error() [no locks held by the waiters] */

Quoted 6 lines

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t ironlake_irq_handler(int irq, void *arg) · line 1485

Quoted 3 lines
drivers/gpu/drm/i915/i915_irq.c +3 -0
+     if (reset_completed)
+             wake_up_all(&dev_priv->gpu_error.reset_queue);
+}

With some additional comments to drive home i915_error_wake_up(),

Applied the additional comments with slightly adjusted colors to make it clear that we care about the unlocking of the waiters so that the error handler can acquire the locks.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#8 lore raw

PATCH 1/1 drm/i915: fix wait_for_pending_flips vs gpu hang deadlock

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

v2:
- Add comments to explain how the wake_up serves as memory barriers for the atomic_t reset counter.
- Improve the comments a bit as suggested by Chris Wilson.
- Extract the wake_up calls before/after the reset into a little i915_error_wake_up and unconditionally wake up the pending_flip_queue waiters, again as suggested by Chris Wilson.

v3: Throw copious amounts of comments at i915_error_wake_up as suggested by Chris Wilson.

---
 drivers/gpu/drm/i915/i915_irq.c | 68 ++++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 14 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +54 -14
index 83cce0c..4b91228 100644
@@ -1469,6 +1469,34 @@ static irqreturn_t ironlake_irq_handler(int irq, void *arg)
 	return ret;
 }
 
+static void i915_error_wake_up(struct drm_i915_private *dev_priv,
+			       bool reset_completed)
+{
+	struct intel_ring_buffer *ring;
+	int i;
+
+	/*
+	 * Notify all waiters for GPU completion events that reset state has
+	 * been changed, and that they need to restart their wait after
+	 * checking for potential errors (and bail out to drop locks if there is
+	 * a gpu reset pending so that i915_error_work_func can acquire them).
+	 */
+
+	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
+	for_each_ring(ring, dev_priv, i)
+		wake_up_all(&ring->irq_queue);
+
+	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
+	wake_up_all(&dev_priv->pending_flip_queue);
+
+	/*
+	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
+	 * reset state is cleared.
+	 */
+	if (reset_completed)
+		wake_up_all(&dev_priv->gpu_error.reset_queue);
+}
+
 /**
  * i915_error_work_func - do process context error handling work
  * @work: work struct
@@ -1483,11 +1511,10 @@ static void i915_error_work_func(struct work_struct *work)
 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
 						    gpu_error);
 	struct drm_device *dev = dev_priv->dev;
-	struct intel_ring_buffer *ring;
 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
-	int i, ret;
+	int ret;
 
 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
 
@@ -1506,8 +1533,16 @@ static void i915_error_work_func(struct work_struct *work)
 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
 				   reset_event);
 
+		/*
+		 * All state reset _must_ be completed before we update the
+		 * reset counter, for otherwise waiters might miss the reset
+		 * pending state and not properly drop locks, resulting in
+		 * deadlocks with the reset work.
+		 */
 		ret = i915_reset(dev);
 
+		intel_display_handle_reset(dev);
+
 		if (ret == 0) {
 			/*
 			 * After all the gem state is reset, increment the reset
@@ -1528,12 +1563,11 @@ static void i915_error_work_func(struct work_struct *work)
 			atomic_set(&error->reset_counter, I915_WEDGED);
 		}
 
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
-
-		intel_display_handle_reset(dev);
-
-		wake_up_all(&dev_priv->gpu_error.reset_queue);
+		/*
+		 * Note: The wake_up also serves as a memory barrier so that
+		 * waiters see the update value of the reset counter atomic_t.
+		 */
+		i915_error_wake_up(dev_priv, true);
 	}
 }
 
@@ -1642,8 +1676,6 @@ static void i915_report_and_clear_eir(struct drm_device *dev)
 void i915_handle_error(struct drm_device *dev, bool wedged)
 {
 	struct drm_i915_private *dev_priv = dev->dev_private;
-	struct intel_ring_buffer *ring;
-	int i;
 
 	i915_capture_error_state(dev);
 	i915_report_and_clear_eir(dev);
@@ -1653,11 +1685,19 @@ void i915_handle_error(struct drm_device *dev, bool wedged)
 				&dev_priv->gpu_error.reset_counter);
 
 		/*
-		 * Wakeup waiting processes so that the reset work item
-		 * doesn't deadlock trying to grab various locks.
+		 * Wakeup waiting processes so that the reset work function
+		 * i915_error_work_func doesn't deadlock trying to grab various
+		 * locks. By bumping the reset counter first, the woken
+		 * processes will see a reset in progress and back off,
+		 * releasing their locks and then wait for the reset completion.
+		 * We must do this for _all_ gpu waiters that might hold locks
+		 * that the reset work needs to acquire.
+		 *
+		 * Note: The wake_up serves as the required memory barrier to
+		 * ensure that the waiters see the updated value of the reset
+		 * counter atomic_t.
 		 */
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
+		i915_error_wake_up(dev_priv, false);
 	}
 
 	/*
Signature · 1 line
1.8.4.rc3
#9 lore raw
Quoted 56 lines · Daniel Vetter

My g33 here seems to be shockingly good at hitting them all. This time around kms_flip/flip-vs-panning-vs-hang blows up:

intel_crtc_wait_for_pending_flips correctly checks for gpu hangs and if a gpu hang is pending aborts the wait for outstanding flips so that the setcrtc call will succeed and release the crtc mutex. And the gpu hang handler needs that lock in intel_display_handle_reset to be able to complete outstanding flips.

The problem is that we can race in two ways:
- Waiters on the dev_priv->pending_flip_queue aren't woken up after we've the reset as pending, but before we actually start the reset work. This means that the waiter doesn't notice the pending reset and hence will keep on hogging the locks.

Like with dev->struct_mutex and the ring->irq_queue wait queues we there need to wake up everyone that potentially holds a lock which the reset handler needs.

- intel_display_handle_reset was called _after_ we've already signalled the completion of the reset work. Which means a waiter could sneak in, grab the lock and never release it (since the pageflips won't ever get released).

Similar to resetting the gem state all the reset work must complete before we update the reset counter. Contrary to the gem reset we don't need to have a second explicit wake up call since that will have happened already when completing the pageflips. We also don't have any issues that the completion happens while the reset state is still pending - wait_for_pending_flips is only there to ensure we display the right frame. After a gpu hang&reset events such guarantees are out the window anyway. This is in contrast to the gem code where too-early wake-up would result in unnecessary restarting of ioctls.

Also, since we've gotten these various deadlocks and ordering constraints wrong so often throw copious amounts of comments at the code.

This deadlock regression has been introduced in the commit which added the pageflip reset logic to the gpu hang work:

commit 96a02917a0131e52efefde49c2784c0421d6c439

Author: Ville Syrjälä <[email protected]>
Date:   Mon Feb 18 19:08:49 2013 +0200

drm/i915: Finish page flips and update primary planes after a GPU reset

v2:
- Add comments to explain how the wake_up serves as memory barriers for the atomic_t reset counter.
- Improve the comments a bit as suggested by Chris Wilson.
- Extract the wake_up calls before/after the reset into a little i915_error_wake_up and unconditionally wake up the pending_flip_queue waiters, again as suggested by Chris Wilson.

Quoted 8 lines

v3: Throw copious amounts of comments at i915_error_wake_up as suggested by Chris Wilson.

Ok, smashed onto -fixes. Thanks a lot for your critical review. -Daniel

Quoted 127 lines
---
 drivers/gpu/drm/i915/i915_irq.c | 68 ++++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 14 deletions(-)
drivers/gpu/drm/i915/i915_irq.c +54 -14
index 83cce0c..4b91228 100644
@@ -1469,6 +1469,34 @@ static irqreturn_t ironlake_irq_handler(int irq, void *arg)
 	return ret;
 }
 
+static void i915_error_wake_up(struct drm_i915_private *dev_priv,
+			       bool reset_completed)
+{
+	struct intel_ring_buffer *ring;
+	int i;
+
+	/*
+	 * Notify all waiters for GPU completion events that reset state has
+	 * been changed, and that they need to restart their wait after
+	 * checking for potential errors (and bail out to drop locks if there is
+	 * a gpu reset pending so that i915_error_work_func can acquire them).
+	 */
+
+	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
+	for_each_ring(ring, dev_priv, i)
+		wake_up_all(&ring->irq_queue);
+
+	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
+	wake_up_all(&dev_priv->pending_flip_queue);
+
+	/*
+	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
+	 * reset state is cleared.
+	 */
+	if (reset_completed)
+		wake_up_all(&dev_priv->gpu_error.reset_queue);
+}
+
 /**
  * i915_error_work_func - do process context error handling work
  * @work: work struct
@@ -1483,11 +1511,10 @@ static void i915_error_work_func(struct work_struct *work)
 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
 						    gpu_error);
 	struct drm_device *dev = dev_priv->dev;
-	struct intel_ring_buffer *ring;
 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
-	int i, ret;
+	int ret;
 
 	kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
 
@@ -1506,8 +1533,16 @@ static void i915_error_work_func(struct work_struct *work)
 		kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
 				   reset_event);
 
+		/*
+		 * All state reset _must_ be completed before we update the
+		 * reset counter, for otherwise waiters might miss the reset
+		 * pending state and not properly drop locks, resulting in
+		 * deadlocks with the reset work.
+		 */
 		ret = i915_reset(dev);
 
+		intel_display_handle_reset(dev);
+
 		if (ret == 0) {
 			/*
 			 * After all the gem state is reset, increment the reset
@@ -1528,12 +1563,11 @@ static void i915_error_work_func(struct work_struct *work)
 			atomic_set(&error->reset_counter, I915_WEDGED);
 		}
 
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
-
-		intel_display_handle_reset(dev);
-
-		wake_up_all(&dev_priv->gpu_error.reset_queue);
+		/*
+		 * Note: The wake_up also serves as a memory barrier so that
+		 * waiters see the update value of the reset counter atomic_t.
+		 */
+		i915_error_wake_up(dev_priv, true);
 	}
 }
 
@@ -1642,8 +1676,6 @@ static void i915_report_and_clear_eir(struct drm_device *dev)
 void i915_handle_error(struct drm_device *dev, bool wedged)
 {
 	struct drm_i915_private *dev_priv = dev->dev_private;
-	struct intel_ring_buffer *ring;
-	int i;
 
 	i915_capture_error_state(dev);
 	i915_report_and_clear_eir(dev);
@@ -1653,11 +1685,19 @@ void i915_handle_error(struct drm_device *dev, bool wedged)
 				&dev_priv->gpu_error.reset_counter);
 
 		/*
-		 * Wakeup waiting processes so that the reset work item
-		 * doesn't deadlock trying to grab various locks.
+		 * Wakeup waiting processes so that the reset work function
+		 * i915_error_work_func doesn't deadlock trying to grab various
+		 * locks. By bumping the reset counter first, the woken
+		 * processes will see a reset in progress and back off,
+		 * releasing their locks and then wait for the reset completion.
+		 * We must do this for _all_ gpu waiters that might hold locks
+		 * that the reset work needs to acquire.
+		 *
+		 * Note: The wake_up serves as the required memory barrier to
+		 * ensure that the waiters see the updated value of the reset
+		 * counter atomic_t.
 		 */
-		for_each_ring(ring, dev_priv, i)
-			wake_up_all(&ring->irq_queue);
+		i915_error_wake_up(dev_priv, false);
 	}
 
 	/*
Signature · 1 line
1.8.4.rc3
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch