Skip to contentSkip to conversation

drm/i915/contexts: fix list corruption

PATCH In review Ben Widawsky 2 participants 10 messages Aug 14, 2012

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

PATCH 1/4 drm/i915/contexts: fix list corruption

After reset we unconditionally reinitialize lists. If the context switch hasn't yet completed before the suspend the default context object will end up on lists that are going to go away when we resume.

The patch forces the context switch to be synchronous before suspend assuring that the active/inactive tracking is correct at the time of resume.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

---
 drivers/gpu/drm/i915/i915_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +2 -2
index 0514593..31054fa 100644
@@ -2273,11 +2273,11 @@ int i915_gpu_idle(struct drm_device *dev)
 
 	/* Flush everything onto the inactive list. */
 	for_each_ring(ring, dev_priv, i) {
-		ret = i915_ring_idle(ring);
+		ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
 		if (ret)
 			return ret;
 
-		ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
+		ret = i915_ring_idle(ring);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
1.7.11.4
#1 lore raw

PATCH 2/4 drm/i915/contexts: Add forced switches

A force parameter for switch currently only has one use, the first time we load the default context.

Slightly hand-wavy explanation: We want to get the default context actually loaded so that the GPU has some real state to load (instead of garbage) after a reset or resume

Therefore, the benefit to adding a parameter instead of trying to determine when to force is that we can strictly limit when such switches occur.

As a +1 to me:
This existed in earlier versions of the patch series, but got removed as part of the review process. The reason before was the same, and the same person who convinced me to remove it before has convinced me to re-add it. :-)

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

---
 drivers/gpu/drm/i915/i915_gem_context.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/i915_gem_context.c +8 -8
index 5c2d354..3945e79 100644
@@ -97,7 +97,7 @@
 
 static struct i915_hw_context *
 i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id);
-static int do_switch(struct i915_hw_context *to);
+static int do_switch(struct i915_hw_context *to, bool force);
 
 static int get_context_size(struct drm_device *dev)
 {
@@ -225,7 +225,7 @@ static int create_default_context(struct drm_i915_private *dev_priv)
 	if (ret)
 		goto err_destroy;
 
-	ret = do_switch(ctx);
+	ret = do_switch(ctx, false);
 	if (ret)
 		goto err_unpin;
 
@@ -362,7 +362,7 @@ mi_set_context(struct intel_ring_buffer *ring,
 	return ret;
 }
 
-static int do_switch(struct i915_hw_context *to)
+static int do_switch(struct i915_hw_context *to, bool force)
 {
 	struct intel_ring_buffer *ring = to->ring;
 	struct drm_i915_gem_object *from_obj = ring->last_context_obj;
@@ -371,7 +371,7 @@ static int do_switch(struct i915_hw_context *to)
 
 	BUG_ON(from_obj != NULL && from_obj->pin_count == 0);
 
-	if (from_obj == to->obj)
+	if (!force && (from_obj == to->obj))
 		return 0;
 
 	ret = i915_gem_object_pin(to->obj, CONTEXT_ALIGN, false);
@@ -392,10 +392,10 @@ static int do_switch(struct i915_hw_context *to)
 	if (!to->obj->has_global_gtt_mapping)
 		i915_gem_gtt_bind_object(to->obj, to->obj->cache_level);
 
-	if (!to->is_initialized || is_default_context(to))
-		hw_flags |= MI_RESTORE_INHIBIT;
-	else if (WARN_ON_ONCE(from_obj == to->obj)) /* not yet expected */
+	if (force) {
 		hw_flags |= MI_FORCE_RESTORE;
+	} else if (!to->is_initialized || is_default_context(to))
+		hw_flags |= MI_RESTORE_INHIBIT;
 
 	ret = mi_set_context(ring, to, hw_flags);
 	if (ret) {
@@ -471,7 +471,7 @@ int i915_switch_context(struct intel_ring_buffer *ring,
 			return -ENOENT;
 	}
 
-	return do_switch(to);
+	return do_switch(to, false);
 }
 
 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
Signature · 1 line
1.7.11.4
#2 lore raw

PATCH 3/4 drm/i915/contexts: Serialize default context init

In reply to Ben Widawsky

This is possible with the new force paramter in do_switch. As stated in that patch, the goal is to get a real context stored at the time of initialization.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

---
 drivers/gpu/drm/i915/i915_gem_context.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/i915/i915_gem_context.c +4 -0
index 3945e79..c96d6f2 100644
@@ -229,6 +229,10 @@ static int create_default_context(struct drm_i915_private *dev_priv)
 	if (ret)
 		goto err_unpin;
 
+	ret = do_switch(ctx, true);
+	if (ret)
+		goto err_unpin;
+
 	DRM_DEBUG_DRIVER("Default HW context loaded\n");
 	return 0;
 
Signature · 1 line
1.7.11.4
#3 lore raw

Re: [PATCH 3/4] drm/i915/contexts: Serialize default context init

Quoted 7 lines · Ben Widawsky

This is possible with the new force paramter in do_switch. As stated in that patch, the goal is to get a real context stored at the time of initialization.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

I'm missing the rationalisation for this pair of patches... For instance, I can't see how this closes the hole we have upon resume where ring->context_obj == DEFAULT_CONTEXT but CCID is 0.
-Chris

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

Re: [PATCH 3/4] drm/i915/contexts: Serialize default context init

Quoted 5 lines · Chris Wilson
Quoted 4 lines · Ben Widawsky

This is possible with the new force paramter in do_switch. As stated in that patch, the goal is to get a real context stored at the time of initialization.

Quoted 8 lines
Quoted 3 lines

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

I'm missing the rationalisation for this pair of patches... For instance, I can't see how this closes the hole we have upon resume where ring->context_obj == DEFAULT_CONTEXT but CCID is 0.
-Chris

Yeah this doesn't fix that problem. The problem this is trying to solve is suspend/resume before any context switch actually occurs. Basically jam the default context obj in, and this allows us to force restore it on resume. However, as you point out, I guess that force restore is missing. Let me think a bit more/chat on IRC.

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#5 lore raw

PATCH 4/4 drm/i915: Cleanup instdone state when idle

In reply to Ben Widawsky

The previous state is bogus when we've gone into idle. Actually there would be a known state of idle (ie. all units are done), but since it differs for every platform, we can just set 0, and let the hangcheck progress as normal.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

---
 drivers/gpu/drm/i915/i915_gem.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/i915/i915_gem.c +3 -0
index 31054fa..5f116a1 100644
@@ -3624,6 +3624,9 @@ i915_gem_idle(struct drm_device *dev)
 	/* Cancel the retire work handler, which should be idle now. */
 	cancel_delayed_work_sync(&dev_priv->mm.retire_work);
 
+	dev_priv->last_instdone = 0;
+	dev_priv->last_instdone1 = 0;
+
 	return 0;
 }
 
Signature · 1 line
1.7.11.4
#6 lore raw

Re: [PATCH 4/4] drm/i915: Cleanup instdone state when idle

Quoted 8 lines · Ben Widawsky

The previous state is bogus when we've gone into idle. Actually there would be a known state of idle (ie. all units are done), but since it differs for every platform, we can just set 0, and let the hangcheck progress as normal.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

I think you have a point, but I don't think this should be just on idle. Everytime we kick off the hangcheck, we should not be comparing to stale state.
-Chris

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

Re: [PATCH 4/4] drm/i915: Cleanup instdone state when idle

Quoted 6 lines · Chris Wilson
Quoted 5 lines · Ben Widawsky

The previous state is bogus when we've gone into idle. Actually there would be a known state of idle (ie. all units are done), but since it differs for every platform, we can just set 0, and let the hangcheck progress as normal.

Quoted 8 lines
Quoted 3 lines

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

I think you have a point, but I don't think this should be just on idle. Everytime we kick off the hangcheck, we should not be comparing to stale state.
-Chris

Are you suggesting this is a patch which already exists somewhere?

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#8 lore raw

Re: [PATCH 4/4] drm/i915: Cleanup instdone state when idle

Quoted 14 lines · Ben Widawsky
Quoted 13 lines · Chris Wilson
Quoted 10 lines · Ben Widawsky

The previous state is bogus when we've gone into idle. Actually there
would be a known state of idle (ie. all units are done), but since it
differs for every platform, we can just set 0, and let the hangcheck progress as normal.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

Quoted 7 lines
Quoted 5 lines

I think you have a point, but I don't think this should be just on idle.
Everytime we kick off the hangcheck, we should not be comparing to stale
state.

Are you suggesting this is a patch which already exists somewhere?

Not yet. :) Just that I agree with you that this a problem, and it is a bigger problem than first thought.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#9 lore raw
In reply to Ben Widawsky
Quoted 11 lines · Ben Widawsky

After reset we unconditionally reinitialize lists. If the context switch hasn't yet completed before the suspend the default context object will end up on lists that are going to go away when we resume.

The patch forces the context switch to be synchronous before suspend assuring that the active/inactive tracking is correct at the time of resume.

References: https://bugs.freedesktop.org/show_bug.cgi?id=52429

-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre