Skip to contentSkip to conversation

drm/i915: Take care of ring->last_context on ctx destroy

PATCH In review Mika Kuoppala 3 participants 3 messages Dec 4, 2015

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

PATCH 1/1 drm/i915: Take care of ring->last_context on ctx destroy

If the context being destroyed have been last in the ring, the ring->last_context will be left dangling.

Later, the unpinning will happen for last_context, and as it was already freed, we corrupt memory by doing so.

This regression introduced in
commit 6d65ba943a2d1e4292a07ca7ddb6c5138b9efa5d

Author: Nick Hoath <[email protected]>
Date:   Tue Dec 1 14:48:57 2015 +0000

drm/i915: Extend LRC pinning to cover GPU context writeback

Fix this by clearing the ring->last_context if it is the context being destroyed.

Cc Nick Hoath <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Gordon <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Alex Dai <[email protected]>
---
 drivers/gpu/drm/i915/intel_lrc.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/i915/intel_lrc.c +3 -0
index c3504a0..5c26fde 100644
@@ -2432,6 +2432,9 @@ intel_lr_context_clean_ring(struct intel_context *ctx,
 		}
 	}
 
+	if (ring->last_context == ctx)
+		ring->last_context = NULL;
+
 	WARN_ON(ctx->engine[ring->id].pin_count);
 	intel_ringbuffer_free(ringbuf);
 	drm_gem_object_unreference(&ctx_obj->base);
Signature · 6 lines
2.5.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw
Quoted 31 lines · Mika Kuoppala

If the context being destroyed have been last in the ring, the ring->last_context will be left dangling.

Later, the unpinning will happen for last_context, and as it was already freed, we corrupt memory by doing so.

This regression introduced in
commit 6d65ba943a2d1e4292a07ca7ddb6c5138b9efa5d

Author: Nick Hoath <[email protected]>
Date:   Tue Dec 1 14:48:57 2015 +0000

drm/i915: Extend LRC pinning to cover GPU context writeback

Fix this by clearing the ring->last_context if it is the context being destroyed.

Cc Nick Hoath <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Gordon <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Alex Dai <[email protected]>
---
 drivers/gpu/drm/i915/intel_lrc.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/i915/intel_lrc.c +0 -0
index c3504a0..5c26fde 100644

drivers/gpu/drm/i915/intel_lrc.c intel_lr_context_clean_ring(struct intel_context *ctx, · line 2436

Quoted 5 lines
drivers/gpu/drm/i915/intel_lrc.c +2 -0
@@ -2432,6 +2432,9 @@ intel_lr_context_clean_ring(struct intel_context *ctx,
 		}
 	}
 
+	if (ring->last_context == ctx)
+		ring->last_context = NULL;

This should be plainly impossible. ring->last_context better have a reference on it, and if that's not the case we need to fix that. Nick, can you please take a look into this asap? It's causing a BAT regression so meanwhile I'll revert your patch.

Thanks, Daniel

Quoted 11 lines
drivers/gpu/drm/i915/intel_lrc.c +1 -0
+
 	WARN_ON(ctx->engine[ring->id].pin_count);
 	intel_ringbuffer_free(ringbuf);
 	drm_gem_object_unreference(&ctx_obj->base);
Signature · 6 lines
2.5.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 1/1 Revert "drm/i915: Extend LRC pinning to cover GPU context writeback"

In reply to Mika Kuoppala

This reverts commit 6d65ba943a2d1e4292a07ca7ddb6c5138b9efa5d.

Mika Kuoppala traced down a use-after-free crash in module unload to this commit, because ring->last_context is leaked beyond when the context gets destroyed. Mika submitted a quick fix to patch that up in the context destruction code, but that's too much of a hack.

The right fix is instead for the ring to hold a full reference onto it's last context, like we do for legacy contexts.

Since this is causing a regression in BAT it gets reverted before we can close this.

Cc Nick Hoath <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Gordon <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Alex Dai <[email protected]>
Cc Mika Kuoppala <[email protected]>
---
 drivers/gpu/drm/i915/i915_drv.h  |   1 -
 drivers/gpu/drm/i915/i915_gem.c  |   7 +-
 drivers/gpu/drm/i915/intel_lrc.c | 136 +++++++--------------------------------
 drivers/gpu/drm/i915/intel_lrc.h |   1 -
 4 files changed, 27 insertions(+), 118 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +0 -1
index f95f36767649..5e276f5d61da 100644
@@ -884,7 +884,6 @@ struct intel_context {
 	struct {
 		struct drm_i915_gem_object *state;
 		struct intel_ringbuffer *ringbuf;
-		bool dirty;
 		int pin_count;
 	} engine[I915_NUM_RINGS];
 
drivers/gpu/drm/i915/i915_gem.c +4 -3
index ef4dbe3d442d..a531cb83295c 100644
@@ -1354,9 +1354,6 @@ static void i915_gem_request_retire(struct drm_i915_gem_request *request)
 {
 	trace_i915_gem_request_retire(request);
 
-	if (i915.enable_execlists)
-		intel_lr_context_complete_check(request);
-
 	/* We know the GPU must have read the request to have
 	 * sent us the seqno + interrupt, so use the position
 	 * of tail of the request to update the last known position
@@ -2767,6 +2764,10 @@ static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
 					struct drm_i915_gem_request,
 					execlist_link);
 			list_del(&submit_req->execlist_link);
+
+			if (submit_req->ctx != ring->default_context)
+				intel_lr_context_unpin(submit_req);
+
 			i915_gem_request_unreference(submit_req);
 		}
 		spin_unlock_irq(&ring->execlist_lock);
drivers/gpu/drm/i915/intel_lrc.c +23 -113

192 lines not on this page · raw message

drivers/gpu/drm/i915/intel_lrc.h +0 -1
index fd1b6b43bacc..0b821b91723a 100644
@@ -91,7 +91,6 @@ void intel_lr_context_reset(struct drm_device *dev,
 			struct intel_context *ctx);
 uint64_t intel_lr_context_descriptor(struct intel_context *ctx,
 				     struct intel_engine_cs *ring);
-void intel_lr_context_complete_check(struct drm_i915_gem_request *req);
 
 /* Execlists */
 int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists);
Signature · 6 lines
2.5.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx