drm/i915: Lock out execlist tasklet while peeking inside for busy-stats

"Linux Kernel Mailing List" <[email protected]> Fri, 16 Feb 2018 17:15:05 +0000 (UTC)
Newsgroups gmane.linux.kernel.commits.head
Message-ID <[email protected]>
Web:        https://git.kernel.org/torvalds/c/edb76b01ac1629bfe17158bea56fcc16bfb57854
Commit:     edb76b01ac1629bfe17158bea56fcc16bfb57854
Parent:     117172c8f9d40ba1de8cb35c6e614422faa03330
Refname:    refs/heads/master
Author:     Chris Wilson <[email protected]>
AuthorDate: Tue Feb 13 09:57:44 2018 +0000
Committer:  Rodrigo Vivi <[email protected]>
CommitDate: Tue Feb 13 16:55:55 2018 -0800

    drm/i915: Lock out execlist tasklet while peeking inside for busy-stats
    
    In order to prevent a race condition where we may end up overaccounting
    the active state and leaving the busy-stats believing the GPU is 100%
    busy, lock out the tasklet while we reconstruct the busy state. There is
    no direct spinlock guard for the execlists->port[], so we need to
    utilise tasklet_disable() as a synchronous barrier to prevent it, the
    only writer to execlists->port[], from running at the same time as the
    enable.
    
    Fixes: 4900727d35bb ("drm/i915/pmu: Reconstruct active state on starting busy-stats")
    Signed-off-by: Chris Wilson <[email protected]>
    Cc: Tvrtko Ursulin <[email protected]>
    Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
    Reviewed-by: Tvrtko Ursulin <[email protected]>
    (cherry picked from commit 99e48bf98dd036090b480a12c39e8b971731247e)
    Signed-off-by: Rodrigo Vivi <[email protected]>
    Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
---
 drivers/gpu/drm/i915/intel_engine_cs.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/i915/intel_engine_cs.c b/drivers/gpu/drm/i915/intel_engine_cs.c
index acc661aa9c0c..fa960cfd2764 100644
--- a/drivers/gpu/drm/i915/intel_engine_cs.c
+++ b/drivers/gpu/drm/i915/intel_engine_cs.c
@@ -1945,16 +1945,22 @@ intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
  */
 int intel_enable_engine_stats(struct intel_engine_cs *engine)
 {
+	struct intel_engine_execlists *execlists = &engine->execlists;
 	unsigned long flags;
+	int err = 0;
 
 	if (!intel_engine_supports_stats(engine))
 		return -ENODEV;
 
+	tasklet_disable(&execlists->tasklet);
 	spin_lock_irqsave(&engine->stats.lock, flags);
-	if (engine->stats.enabled == ~0)
-		goto busy;
+
+	if (unlikely(engine->stats.enabled == ~0)) {
+		err = -EBUSY;
+		goto unlock;
+	}
+
 	if (engine->stats.enabled++ == 0) {
-		struct intel_engine_execlists *execlists = &engine->execlists;
 		const struct execlist_port *port = execlists->port;
 		unsigned int num_ports = execlists_num_ports(execlists);
 
@@ -1969,14 +1975,12 @@ int intel_enable_engine_stats(struct intel_engine_cs *engine)
 		if (engine->stats.active)
 			engine->stats.start = engine->stats.enabled_at;
 	}
-	spin_unlock_irqrestore(&engine->stats.lock, flags);
 
-	return 0;