All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chris Wilson <chris@chris-wilson.co.uk>
To: intel-gfx@lists.freedesktop.org
Subject: [PATCH 06/22] drm/i915: Hold a reference to the active HW context
Date: Mon, 18 Mar 2019 09:51:48 +0000	[thread overview]
Message-ID: <20190318095204.9913-6-chris@chris-wilson.co.uk> (raw)
In-Reply-To: <20190318095204.9913-1-chris@chris-wilson.co.uk>

For virtual engines, we need to keep the HW context alive while it
remains in use. For regular HW contexts, they are created and kept alive
until the end of the GEM context. For simplicity, generalise the
requirements and keep an active reference to each HW context.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
---
 drivers/gpu/drm/i915/i915_gem_context.c      |  2 +-
 drivers/gpu/drm/i915/intel_context.c         |  6 ++++++
 drivers/gpu/drm/i915/intel_context.h         | 11 +++++++++++
 drivers/gpu/drm/i915/intel_context_types.h   |  6 +++++-
 drivers/gpu/drm/i915/intel_lrc.c             |  4 +++-
 drivers/gpu/drm/i915/intel_ringbuffer.c      |  4 +++-
 drivers/gpu/drm/i915/selftests/mock_engine.c |  7 ++++++-
 7 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_gem_context.c b/drivers/gpu/drm/i915/i915_gem_context.c
index 21208a865380..d776d43707e0 100644
--- a/drivers/gpu/drm/i915/i915_gem_context.c
+++ b/drivers/gpu/drm/i915/i915_gem_context.c
@@ -232,7 +232,7 @@ static void i915_gem_context_free(struct i915_gem_context *ctx)
 	i915_ppgtt_put(ctx->ppgtt);
 
 	rbtree_postorder_for_each_entry_safe(it, n, &ctx->hw_contexts, node)
-		it->ops->destroy(it);
+		intel_context_put(it);
 
 	kfree(ctx->name);
 	put_pid(ctx->pid);
diff --git a/drivers/gpu/drm/i915/intel_context.c b/drivers/gpu/drm/i915/intel_context.c
index 0ab894a058f6..8931e0fee873 100644
--- a/drivers/gpu/drm/i915/intel_context.c
+++ b/drivers/gpu/drm/i915/intel_context.c
@@ -172,6 +172,7 @@ intel_context_pin(struct i915_gem_context *ctx,
 		list_add(&ce->active_link, &ctx->active_engines);
 		mutex_unlock(&ctx->mutex);
 
+		intel_context_get(ce);
 		smp_mb__before_atomic(); /* flush pin before it is visible */
 	}
 
@@ -192,6 +193,7 @@ void intel_context_unpin(struct intel_context *ce)
 		return;
 
 	/* We may be called from inside intel_context_pin() to evict another */
+	intel_context_get(ce);
 	mutex_lock_nested(&ce->pin_mutex, SINGLE_DEPTH_NESTING);
 
 	if (likely(atomic_dec_and_test(&ce->pin_count))) {
@@ -202,9 +204,11 @@ void intel_context_unpin(struct intel_context *ce)
 		mutex_unlock(&ce->gem_context->mutex);
 
 		i915_gem_context_put(ce->gem_context);
+		intel_context_put(ce);
 	}
 
 	mutex_unlock(&ce->pin_mutex);
+	intel_context_put(ce);
 }
 
 static void intel_context_retire(struct i915_active_request *active,
@@ -221,6 +225,8 @@ intel_context_init(struct intel_context *ce,
 		   struct i915_gem_context *ctx,
 		   struct intel_engine_cs *engine)
 {
+	kref_init(&ce->ref);
+
 	ce->gem_context = ctx;
 	ce->engine = engine;
 	ce->ops = engine->cops;
diff --git a/drivers/gpu/drm/i915/intel_context.h b/drivers/gpu/drm/i915/intel_context.h
index 9546d932406a..ebc861b1a49e 100644
--- a/drivers/gpu/drm/i915/intel_context.h
+++ b/drivers/gpu/drm/i915/intel_context.h
@@ -73,4 +73,15 @@ static inline void __intel_context_pin(struct intel_context *ce)
 
 void intel_context_unpin(struct intel_context *ce);
 
+static inline struct intel_context *intel_context_get(struct intel_context *ce)
+{
+	kref_get(&ce->ref);
+	return ce;
+}
+
+static inline void intel_context_put(struct intel_context *ce)
+{
+	kref_put(&ce->ref, ce->ops->destroy);
+}
+
 #endif /* __INTEL_CONTEXT_H__ */
diff --git a/drivers/gpu/drm/i915/intel_context_types.h b/drivers/gpu/drm/i915/intel_context_types.h
index 6dc9b4b9067b..624729a35875 100644
--- a/drivers/gpu/drm/i915/intel_context_types.h
+++ b/drivers/gpu/drm/i915/intel_context_types.h
@@ -7,6 +7,7 @@
 #ifndef __INTEL_CONTEXT_TYPES__
 #define __INTEL_CONTEXT_TYPES__
 
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
 #include <linux/rbtree.h>
@@ -22,7 +23,8 @@ struct intel_ring;
 struct intel_context_ops {
 	int (*pin)(struct intel_context *ce);
 	void (*unpin)(struct intel_context *ce);
-	void (*destroy)(struct intel_context *ce);
+
+	void (*destroy)(struct kref *kref);
 };
 
 /*
@@ -36,6 +38,8 @@ struct intel_sseu {
 };
 
 struct intel_context {
+	struct kref ref;
+
 	struct i915_gem_context *gem_context;
 	struct intel_engine_cs *engine;
 	struct intel_engine_cs *active;
diff --git a/drivers/gpu/drm/i915/intel_lrc.c b/drivers/gpu/drm/i915/intel_lrc.c
index 13f5545fc1d2..fbf67105f040 100644
--- a/drivers/gpu/drm/i915/intel_lrc.c
+++ b/drivers/gpu/drm/i915/intel_lrc.c
@@ -1242,8 +1242,10 @@ static void __execlists_context_fini(struct intel_context *ce)
 	i915_gem_object_put(ce->state->obj);
 }
 
-static void execlists_context_destroy(struct intel_context *ce)
+static void execlists_context_destroy(struct kref *kref)
 {
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+
 	GEM_BUG_ON(intel_context_is_pinned(ce));
 
 	if (ce->state)
diff --git a/drivers/gpu/drm/i915/intel_ringbuffer.c b/drivers/gpu/drm/i915/intel_ringbuffer.c
index 6d60bc258feb..35fdebd67e5f 100644
--- a/drivers/gpu/drm/i915/intel_ringbuffer.c
+++ b/drivers/gpu/drm/i915/intel_ringbuffer.c
@@ -1345,8 +1345,10 @@ static void __ring_context_fini(struct intel_context *ce)
 	i915_gem_object_put(ce->state->obj);
 }
 
-static void ring_context_destroy(struct intel_context *ce)
+static void ring_context_destroy(struct kref *ref)
 {
+	struct intel_context *ce = container_of(ref, typeof(*ce), ref);
+
 	GEM_BUG_ON(intel_context_is_pinned(ce));
 
 	if (ce->state)
diff --git a/drivers/gpu/drm/i915/selftests/mock_engine.c b/drivers/gpu/drm/i915/selftests/mock_engine.c
index 7641b74ada98..639d36eb904a 100644
--- a/drivers/gpu/drm/i915/selftests/mock_engine.c
+++ b/drivers/gpu/drm/i915/selftests/mock_engine.c
@@ -128,12 +128,16 @@ static void mock_context_unpin(struct intel_context *ce)
 	mock_timeline_unpin(ce->ring->timeline);
 }
 
-static void mock_context_destroy(struct intel_context *ce)
+static void mock_context_destroy(struct kref *ref)
 {
+	struct intel_context *ce = container_of(ref, typeof(*ce), ref);
+
 	GEM_BUG_ON(intel_context_is_pinned(ce));
 
 	if (ce->ring)
 		mock_ring_free(ce->ring);
+
+	intel_context_free(ce);
 }
 
 static int mock_context_pin(struct intel_context *ce)
@@ -151,6 +155,7 @@ static int mock_context_pin(struct intel_context *ce)
 static const struct intel_context_ops mock_context_ops = {
 	.pin = mock_context_pin,
 	.unpin = mock_context_unpin,
+
 	.destroy = mock_context_destroy,
 };
 
-- 
2.20.1

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  parent reply	other threads:[~2019-03-18 10:11 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-18  9:51 [PATCH 01/22] drm/i915: Flush pages on acquisition Chris Wilson
2019-03-18  9:51 ` [PATCH 02/22] drm/i915: Move intel_engine_mask_t around for use by i915_request_types.h Chris Wilson
2019-03-18 10:21   ` Tvrtko Ursulin
2019-03-18 10:40     ` Chris Wilson
2019-03-18 10:48       ` Tvrtko Ursulin
2019-03-18 13:57         ` Chris Wilson
2019-03-18  9:51 ` [PATCH 03/22] drm/i915: Sanity check mmap length against object size Chris Wilson
2019-03-25  0:38   ` Sasha Levin
2019-03-18  9:51 ` [PATCH 04/22] drm/i915: Hold a ref to the ring while retiring Chris Wilson
2019-03-18 10:31   ` Tvrtko Ursulin
2019-03-18 10:37     ` Chris Wilson
2019-03-18 10:46       ` Tvrtko Ursulin
2019-03-18 10:56         ` Chris Wilson
2019-03-18 13:25           ` Tvrtko Ursulin
2019-03-18  9:51 ` [PATCH 05/22] drm/i915: Lock the gem_context->active_list while dropping the link Chris Wilson
2019-03-18 10:39   ` Tvrtko Ursulin
2019-03-18 10:45     ` Chris Wilson
2019-03-18 10:50       ` Tvrtko Ursulin
2019-03-18 10:54   ` Chris Wilson
2019-03-18  9:51 ` Chris Wilson [this message]
2019-03-18 12:54   ` [PATCH 06/22] drm/i915: Hold a reference to the active HW context Tvrtko Ursulin
2019-03-18 12:56     ` Chris Wilson
2019-03-18 12:57       ` Chris Wilson
2019-03-18 13:29         ` Tvrtko Ursulin
2019-03-18  9:51 ` [PATCH 07/22] drm/i915: Stop needlessly acquiring wakeref for debugfs/drop_caches_set Chris Wilson
2019-03-18 13:08   ` Tvrtko Ursulin
2019-03-18  9:51 ` [PATCH 08/22] drm/i915/selftests: Provide stub reset functions Chris Wilson
2019-03-18  9:51 ` [PATCH 09/22] drm/i915: Switch to use HWS indices rather than addresses Chris Wilson
2019-03-18 13:21   ` Tvrtko Ursulin
2019-03-18  9:51 ` [PATCH 10/22] drm/i915: Separate GEM context construction and registration to userspace Chris Wilson
2019-03-18 16:22   ` Tvrtko Ursulin
2019-03-18 16:30     ` Chris Wilson
2019-03-18 16:32       ` Chris Wilson
2019-03-18 16:46         ` Tvrtko Ursulin
2019-03-18  9:51 ` [PATCH 11/22] drm/i915: Introduce a mutex for file_priv->context_idr Chris Wilson
2019-03-18 16:28   ` Tvrtko Ursulin
2019-03-18 16:35     ` Chris Wilson
2019-03-18 16:45       ` Tvrtko Ursulin
2019-03-18 21:10         ` Chris Wilson
2019-03-18  9:51 ` [PATCH 12/22] drm/i915: Introduce the i915_user_extension_method Chris Wilson
2019-03-18  9:51 ` [PATCH 13/22] drm/i915: Create/destroy VM (ppGTT) for use with contexts Chris Wilson
2019-03-18  9:51 ` [PATCH 14/22] drm/i915: Extend CONTEXT_CREATE to set parameters upon construction Chris Wilson
2019-03-18  9:51 ` [PATCH 15/22] drm/i915: Allow contexts to share a single timeline across all engines Chris Wilson
2019-03-18  9:51 ` [PATCH 16/22] drm/i915: Allow userspace to clone contexts on creation Chris Wilson
2019-03-18  9:51 ` [PATCH 17/22] drm/i915: Allow a context to define its set of engines Chris Wilson
2019-03-18  9:52 ` [PATCH 18/22] drm/i915: Extend I915_CONTEXT_PARAM_SSEU to support local ctx->engine[] Chris Wilson
2019-03-18  9:52 ` [PATCH 19/22] drm/i915: Load balancing across a virtual engine Chris Wilson
2019-03-18  9:52 ` [PATCH 20/22] drm/i915: Extend execution fence to support a callback Chris Wilson
2019-03-18  9:52 ` [PATCH 21/22] drm/i915/execlists: Virtual engine bonding Chris Wilson
2019-03-18  9:52 ` [PATCH 22/22] drm/i915: Allow specification of parallel execbuf Chris Wilson
2019-03-18 17:10 ` ✗ Fi.CI.CHECKPATCH: warning for series starting with [01/22] drm/i915: Flush pages on acquisition Patchwork
2019-03-18 17:20 ` ✗ Fi.CI.SPARSE: " Patchwork
2019-03-18 17:36 ` ✗ Fi.CI.BAT: failure " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190318095204.9913-6-chris@chris-wilson.co.uk \
    --to=chris@chris-wilson.co.uk \
    --cc=intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.