drm/i915: Make LRC (un)pinning work on context and engine
Subsystems (2)
PATCH v2 1/3 drm/i915: Make LRC (un)pinning work on context and engine
From: Tvrtko Ursulin <[email protected]>
Previously intel_lr_context_(un)pin were operating on requests which is in conflict with their names.
If we make them take a context and an engine, it makes the names make more sense and it also makes future fixes possible.
v2: Rebase for default_context/kernel_context change.
--- drivers/gpu/drm/i915/i915_gem.c | 2 +- drivers/gpu/drm/i915/intel_lrc.c | 49 ++++++++++++++++++++-------------------- drivers/gpu/drm/i915/intel_lrc.h | 3 ++- 3 files changed, 28 insertions(+), 26 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +1 -1
@@ -2680,7 +2680,7 @@ void i915_gem_request_free(struct kref *req_ref)if (ctx) {if (i915.enable_execlists && ctx != req->i915->kernel_context) -intel_lr_context_unpin (req); +intel_lr_context_unpin (ctx, req->ring);i915_gem_context_unreference (ctx); }
drivers/gpu/drm/i915/intel_lrc.c +25 -24
109 lines not on this page · raw message
drivers/gpu/drm/i915/intel_lrc.h +2 -1
@@ -101,7 +101,8 @@ void intel_lr_context_free(struct intel_context *ctx);uint32_t intel_lr_context_size (struct intel_engine_cs *ring);int intel_lr_context_deferred_alloc (struct intel_context *ctx,struct intel_engine_cs *ring); -void intel_lr_context_unpin (struct drm_i915_gem_request *req); +void intel_lr_context_unpin (struct intel_context *ctx, +struct intel_engine_cs *engine);void intel_lr_context_reset (struct drm_device *dev,struct intel_context *ctx); uint64_t intel_lr_context_descriptor(struct intel_context *ctx,
Signature · 6 lines
1.9.1 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 2/3 drm/i915: Make LRC pinning own a reference to the context
From: Tvrtko Ursulin <[email protected]>
Will simplify the following fix and sounds logical.
v2: Add some whitespace to separate logic better. (Chris Wilson)
--- drivers/gpu/drm/i915/intel_lrc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/i915/intel_lrc.c +4 -0
@@ -1103,6 +1103,8 @@ static int intel_lr_context_pin(struct intel_context *ctx, ret =intel_lr_context_do_pin (ctx, engine);if (ret) goto reset_pin_count; + +i915_gem_context_reference (ctx); }return ret;
@@ -1128,6 +1130,8 @@ void intel_lr_context_unpin(struct intel_context *ctx, ctx->engine[engine->id].lrc_vma =NULL ; ctx->engine[engine->id].lrc_desc =0 ; ctx->engine[engine->id].lrc_reg_state =NULL ; + +i915_gem_context_unreference (ctx); } }
Signature · 6 lines
1.9.1 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH v3 3/3 drm/i915: Fix premature LRC unpin in GuC mode
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
In GuC mode LRC pinning lifetime depends exclusively on the request liftime. Since that is terminated by the seqno update that opens up a race condition between GPU finishing writing out the context image and the driver unpinning the LRC.
To extend the LRC lifetime we will employ a similar approach to what legacy ringbuffer submission does.
We will start tracking the last submitted context per engine and keep it pinned until it is replaced by another one.
Note that the driver unload path is a bit fragile and could benefit greatly from efforts to unify the legacy and exec list submission code paths.
At the moment i915_gem_context_fini has special casing for the two which are potentialy not needed, and also depends on i915_gem_cleanup_ringbuffer running before itself.
v2:
* Move pinning into engine->emit_request and actually fix the reference/unreference logic. (Chris Wilson)
* ring->dev can be NULL on driver unload so use a different route towards it.
v3:
* Rebase.
* Handle the reset path. (Chris Wilson)
* Exclude default context from the pinning - it is impossible to get it right before default context special casing in general is eliminated.
Issue: VIZ-4277
Cc: Chris Wilson <[email protected]>
Cc: Nick Hoath <[email protected]>
--- drivers/gpu/drm/i915/i915_gem_context.c | 15 +++++++++++---- drivers/gpu/drm/i915/intel_lrc.c | 13 ++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/i915_gem_context.c +11 -4
@@ -341,10 +341,14 @@ void i915_gem_context_reset(struct drm_device *dev)struct intel_context *lctx = ring->last_context;if (lctx) { -if (lctx->legacy_hw_ctx.rcs_state && i ==RCS ) -i915_gem_object_ggtt_unpin (lctx->legacy_hw_ctx.rcs_state); +if (!i915.enable_execlists) { +if (lctx->legacy_hw_ctx.rcs_state && i ==RCS ) +i915_gem_object_ggtt_unpin (lctx->legacy_hw_ctx.rcs_state); -i915_gem_context_unreference (lctx); +i915_gem_context_unreference (lctx); + }else { +intel_lr_context_unpin (lctx, ring); + } ring->last_context =NULL ; } }
@@ -432,7 +436,10 @@ void i915_gem_context_fini(struct drm_device *dev)struct intel_engine_cs *ring = &dev_priv->ring[i];if (ring->last_context) { -i915_gem_context_unreference (ring->last_context); +if (i915.enable_execlists) +intel_lr_context_unpin (ring->last_context, ring); +else +i915_gem_context_unreference (ring->last_context); ring->last_context =NULL ; } }
drivers/gpu/drm/i915/intel_lrc.c +12 -1
@@ -1118,7 +1118,7 @@ void intel_lr_context_unpin(struct intel_context *ctx, {struct drm_i915_gem_object *ctx_obj = ctx->engine[engine->id].state; -WARN_ON (!mutex_is_locked (&engine->dev->struct_mutex)); +WARN_ON (!mutex_is_locked (&ctx->i915->dev->struct_mutex));if (WARN_ON_ONCE (!ctx_obj))return ;
@@ -1857,6 +1857,17 @@ static int gen8_emit_request(struct drm_i915_gem_request *request)u32 cmd;int ret; +if (ring->last_context != request->ctx) { +if (ring->last_context) +intel_lr_context_unpin (ring->last_context, ring); +if (request->ctx != request->i915->kernel_context) { +intel_lr_context_pin (request->ctx, ring); + ring->last_context = request->ctx; + }else { + ring->last_context =NULL ; + } + } +/* * Reserve space for 2 NOOPs at the end of each request to be * used as a workaround for not being allowed to do lite
Signature · 6 lines
1.9.1 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [PATCH v3 3/3] drm/i915: Fix premature LRC unpin in GuC mode
Quoted 28 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
In GuC mode LRC pinning lifetime depends exclusively on the request liftime. Since that is terminated by the seqno update that opens up a race condition between GPU finishing writing out the context image and the driver unpinning the LRC.
To extend the LRC lifetime we will employ a similar approach to what legacy ringbuffer submission does.
We will start tracking the last submitted context per engine and keep it pinned until it is replaced by another one.
Note that the driver unload path is a bit fragile and could benefit greatly from efforts to unify the legacy and exec list submission code paths.
At the moment
i915_gem_context_finihas special casing for the two which are potentialy not needed, and also depends oni915_gem_cleanup_ringbufferrunning before itself.v2:
* Move pinning intoengine->emit_requestand actually fix the reference/unreference logic. (Chris Wilson)*
ring->devcan be NULL on driver unload so use a different route towards it.
Quoted 6 lines
v3:
* Rebase.
* Handle the reset path. (Chris Wilson)
* Exclude default context from the pinning - it is impossible to get it right before default context special casing in general is eliminated.
Since you have the refactoring in place, eliminating the special case is trivial. So patch 2.5?
Looks like http://patchwork.freedesktop.org/patch/69972/ but you already have half of that patch in place.
Also if we take a step to add patch 2.1:
drivers/gpu/drm/i915/i915_gem_context.c +14 -17
@@ -321,6 +321,14 @@ err_destroy:return ERR_PTR (ret); } +static void i915_gem_context_unpin (struct intel_context *ctx, +struct intel_engine_cs *rcs) +{ +if (rcs->id ==RCS && ctx->legacy_hw_ctx.rcs_state) +i915_gem_object_ggtt_unpin (ctx->legacy_hw_ctx.rcs_state); +i915_gem_context_unreference (ctx); +} +void i915_gem_context_reset (struct drm_device *dev) {struct drm_i915_private *dev_priv = dev->dev_private;
@@ -338,13 +346,9 @@ void i915_gem_context_reset(struct drm_device *dev)for (i =0 ; i <I915_NUM_RINGS ; i++) {struct intel_engine_cs *ring = &dev_priv->ring[i]; -struct intel_context *lctx = ring->last_context; -if (lctx) { -if (lctx->legacy_hw_ctx.rcs_state && i ==RCS ) -i915_gem_object_ggtt_unpin (lctx->legacy_hw_ctx.rcs_state); - -i915_gem_context_unreference (lctx); +if (ring->last_context) { +i915_gem_context_unpin (ring->last_context, ring); ring->last_context =NULL ; }
@@ -424,25 +428,18 @@ void i915_gem_context_fini(struct drm_device *dev)* to offset the do_switch part, so that i915_gem_context_unreference() * can then free the base object correctly. */ WARN_ON (!dev_priv->ring[RCS ].last_context); -if (dev_priv->ring[RCS ].last_context == dctx) { -/* Fake switch to NULL context */ -WARN_ON (dctx->legacy_hw_ctx.rcs_state->active); -i915_gem_object_ggtt_unpin (dctx->legacy_hw_ctx.rcs_state); -i915_gem_context_unreference (dctx); - dev_priv->ring[RCS ].last_context =NULL ; - } -i915_gem_object_ggtt_unpin (dctx->legacy_hw_ctx.rcs_state); }for (i =0 ; i <I915_NUM_RINGS ; i++) {struct intel_engine_cs *ring = &dev_priv->ring[i]; -if (ring->last_context) -i915_gem_context_unreference (ring->last_context); +if (ring->last_context) { +i915_gem_context_unpin (ring->last_context, ring); + ring->last_context =NULL ; + } ring->default_context =NULL ; - ring->last_context =NULL ; }i915_gem_context_unreference (dctx);
Then we just end up with
if (i915.enable_execlists)
intel_lr_context_unpin(ring->last_context, ring); else i915_gem_context_unpin(ring->last_context, ring);
which is much less messy in this patch and one step closer to unifying engine->pin_context()/engine->unpin_context() vfuns.
The only tricky part there is having to disentangle the legacy paths, but doing so should allow us to see clearly that the sequence is just
intel_gpu_reset();for_each_engine() unpin(engine->last_context);unpin(dev_priv->default_context);
After a few more passes, we should be able to call the reset and cleanup functions higher up (as part of the stopping the GPU upon suspend procedure before tearing down the sw structs) at which point we only need the unpin(default_context) here.
-Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [PATCH v3 3/3] drm/i915: Fix premature LRC unpin in GuC mode
Quoted 8 lines · Chris Wilson
intel_gpu_reset();for_each_engine()unpin(engine->last_context);unpin(dev_priv->default_context);After a few more passes, we should be able to call the reset and cleanup functions higher up (as part of the stopping the GPU upon suspend procedure before tearing down the sw structs) at which point we only need the
unpin(default_context)here.
unref(dev_priv->default_context) !
whether or not the individual engines have a pin on it will be left to themselves to decide.
-Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [PATCH v3 3/3] drm/i915: Fix premature LRC unpin in GuC mode
In reply to Chris WilsonQuoted 123 lines · Chris Wilson
Quoted 28 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
In GuC mode LRC pinning lifetime depends exclusively on the request liftime. Since that is terminated by the seqno update that opens up a race condition between GPU finishing writing out the context image and the driver unpinning the LRC.
To extend the LRC lifetime we will employ a similar approach to what legacy ringbuffer submission does.
We will start tracking the last submitted context per engine and keep it pinned until it is replaced by another one.
Note that the driver unload path is a bit fragile and could benefit greatly from efforts to unify the legacy and exec list submission code paths.
At the moment
i915_gem_context_finihas special casing for the two which are potentialy not needed, and also depends oni915_gem_cleanup_ringbufferrunning before itself.v2:
* Move pinning intoengine->emit_requestand actually fix the reference/unreference logic. (Chris Wilson)*
ring->devcan be NULL on driver unload so use a different route towards it.Quoted 6 lines
v3:
* Rebase.
* Handle the reset path. (Chris Wilson)
* Exclude default context from the pinning - it is impossible to get it right before default context special casing in general is eliminated.Since you have the refactoring in place, eliminating the special case is trivial. So patch 2.5?
Looks like http://patchwork.freedesktop.org/patch/69972/ but you already have half of that patch in place.
Also if we take a step to add patch 2.1:
drivers/gpu/drm/i915/i915_gem_context.c +14
-17@@ -321,6 +321,14 @@ err_destroy:return ERR_PTR (ret); } +static void i915_gem_context_unpin (struct intel_context *ctx, +struct intel_engine_cs *rcs) +{ +if (rcs->id ==RCS && ctx->legacy_hw_ctx.rcs_state) +i915_gem_object_ggtt_unpin (ctx->legacy_hw_ctx.rcs_state); +i915_gem_context_unreference (ctx); +} +void i915_gem_context_reset (struct drm_device *dev) {struct drm_i915_private *dev_priv = dev->dev_private;@@ -338,13 +346,9 @@ void i915_gem_context_reset(struct drm_device *dev)for (i =0 ; i <I915_NUM_RINGS ; i++) {struct intel_engine_cs *ring = &dev_priv->ring[i]; -struct intel_context *lctx = ring->last_context; -if (lctx) { -if (lctx->legacy_hw_ctx.rcs_state && i ==RCS ) -i915_gem_object_ggtt_unpin (lctx->legacy_hw_ctx.rcs_state); - -i915_gem_context_unreference (lctx); +if (ring->last_context) { +i915_gem_context_unpin (ring->last_context, ring); ring->last_context =NULL ; }@@ -424,25 +428,18 @@ void i915_gem_context_fini(struct drm_device *dev)* to offset the do_switch part, so that i915_gem_context_unreference() * can then free the base object correctly. */ WARN_ON (!dev_priv->ring[RCS ].last_context); -if (dev_priv->ring[RCS ].last_context == dctx) { -/* Fake switch to NULL context */ -WARN_ON (dctx->legacy_hw_ctx.rcs_state->active); -i915_gem_object_ggtt_unpin (dctx->legacy_hw_ctx.rcs_state); -i915_gem_context_unreference (dctx); - dev_priv->ring[RCS ].last_context =NULL ; - } -i915_gem_object_ggtt_unpin (dctx->legacy_hw_ctx.rcs_state); }for (i =0 ; i <I915_NUM_RINGS ; i++) {struct intel_engine_cs *ring = &dev_priv->ring[i]; -if (ring->last_context) -i915_gem_context_unreference (ring->last_context); +if (ring->last_context) { +i915_gem_context_unpin (ring->last_context, ring); + ring->last_context =NULL ; + } ring->default_context =NULL ; - ring->last_context =NULL ; }i915_gem_context_unreference (dctx);Then we just end up with
if (i915.enable_execlists)
intel_lr_context_unpin(ring->last_context, ring); else i915_gem_context_unpin(ring->last_context, ring);which is much less messy in this patch and one step closer to unifying engine->pin_context()/engine->unpin_context() vfuns.
The only tricky part there is having to disentangle the legacy paths, but doing so should allow us to see clearly that the sequence is just
Quoted 8 lines
intel_gpu_reset();for_each_engine()unpin(engine->last_context);unpin(dev_priv->default_context);After a few more passes, we should be able to call the reset and cleanup functions higher up (as part of the stopping the GPU upon suspend procedure before tearing down the sw structs) at which point we only need the
unpin(default_context)here.
I was confused by the current code which does the reset after unpinning:
...
i915_gem_reset(dev); simulated = dev_priv->gpu_error.stop_rings != 0; ret = intel_gpu_reset(dev);
...
What is right then?
Sounds bad to be unpinning with the GPU in unknown state. But obviously it has been like this for who knows how long. So I have no idea. :(
Asking since GuC is at the moment struggling with reset, but then again that might be something completely else...
Regards,
Tvrtko
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [PATCH v3 3/3] drm/i915: Fix premature LRC unpin in GuC mode
Quoted 8 lines · Tvrtko Ursulin
I was confused by the current code which does the reset after unpinning:
...
i915_gem_reset(dev); simulated = dev_priv->gpu_error.stop_rings != 0; ret = intel_gpu_reset(dev);
Quoted 4 lines
...
What is right then?
Yeah, it would be better to do the sw reset after the hw reset so that the GPU is a known state when we tear down everything.
Quoted 3 lines
Sounds bad to be unpinning with the GPU in unknown state. But obviously it has been like this for who knows how long. So I have no idea. :(
i915_reset() is only called on a hung GPU, and I expect we simply haven't stressed the system enough with an active-vs-hung pair of engines to be able to see stray writes and whatnot.
The downside is that if we jiggle the i915_gem_reset() we have to ask the awkward question of what to do with state when reset fails? We should keep it around because the hw state is unknown - but that can be a significant amount of memory trapped.
-Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx