Skip to contentSkip to conversation

i915/pmu: Wire GuC backend to per-client busyness

PATCH New Ashutosh Dixit 3 participants 4 messages Sep 1, 2022 – Sep 2, 2022

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

PATCH 1/2 i915/pmu: Wire GuC backend to per-client busyness

From: John Harrison <[email protected]>

GuC provides engine_id and last_switch_in ticks for an active context in the pphwsp. The context image provides a 32 bit total ticks which is the accumulated by the context (a.k.a. context[CTX_TIMESTAMP]). This information is used to calculate the context busyness as follows:

If the engine_id is valid, then busyness is the sum of accumulated total ticks and active ticks. Active ticks is calculated with current gt time as reference.

If engine_id is invalid, busyness is equal to accumulated total ticks.

Since KMD (CPU) retrieves busyness data from 2 sources - GPU and GuC, a potential race was highlighted in an earlier review that can lead to double accounting of busyness. While the solution to this is a wip, busyness is still usable for platforms running GuC submission.

Remaining work: Enable and test context busyness for virtual_parent_context_ops and virtual_child_context_ops.

v2: (Tvrtko)
- Use COPS_RUNTIME_ACTIVE_TOTAL
- Add code comment for the race
- Undo local variables initializations

v3:
- Add support for virtual engines based on
https://patchwork.freedesktop.org/series/105227/

v4:
- Update commit message with remaining work.
- Rebase

---
 drivers/gpu/drm/i915/gt/intel_context.c       | 12 +++-
 drivers/gpu/drm/i915/gt/intel_context.h       |  6 +-
 drivers/gpu/drm/i915/gt/intel_context_types.h |  6 ++
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |  5 ++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 65 ++++++++++++++++++-
 drivers/gpu/drm/i915/i915_drm_client.c        |  6 +-
 6 files changed, 89 insertions(+), 11 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +10 -2
index 654a092ed3d6..e2d70a9fdac0 100644
@@ -576,16 +576,24 @@ void intel_context_bind_parent_child(struct intel_context *parent,
 	child->parallel.parent = parent;
 }
 
-u64 intel_context_get_total_runtime_ns(const struct intel_context *ce)
+u64 intel_context_get_total_runtime_ns(struct intel_context *ce)
 {
 	u64 total, active;
 
+	if (ce->ops->update_stats)
+		ce->ops->update_stats(ce);
+
 	total = ce->stats.runtime.total;
 	if (ce->ops->flags & COPS_RUNTIME_CYCLES)
 		total *= ce->engine->gt->clock_period_ns;
 
 	active = READ_ONCE(ce->stats.active);
-	if (active)
+	/*
+	 * When COPS_RUNTIME_ACTIVE_TOTAL is set for ce->cops, the backend
+	 * already provides the total active time of the context, so skip this
+	 * calculation when this flag is set.
+	 */
+	if (active && !(ce->ops->flags & COPS_RUNTIME_ACTIVE_TOTAL))
 		active = intel_context_clock() - active;
 
 	return total + active;
drivers/gpu/drm/i915/gt/intel_context.h +3 -3
index 8e2d70630c49..3d1d7436c1a4 100644
@@ -58,7 +58,7 @@ static inline bool intel_context_is_parent(struct intel_context *ce)
 	return !!ce->parallel.number_children;
 }
 
-static inline bool intel_context_is_pinned(struct intel_context *ce);
+static inline bool intel_context_is_pinned(const struct intel_context *ce);
 
 static inline struct intel_context *
 intel_context_to_parent(struct intel_context *ce)
@@ -118,7 +118,7 @@ static inline int intel_context_lock_pinned(struct intel_context *ce)
  * Returns: true if the context is currently pinned for use by the GPU.
  */
 static inline bool
-intel_context_is_pinned(struct intel_context *ce)
+intel_context_is_pinned(const struct intel_context *ce)
 {
 	return atomic_read(&ce->pin_count);
 }
@@ -362,7 +362,7 @@ intel_context_clear_nopreempt(struct intel_context *ce)
 	clear_bit(CONTEXT_NOPREEMPT, &ce->flags);
 }
 
-u64 intel_context_get_total_runtime_ns(const struct intel_context *ce);
+u64 intel_context_get_total_runtime_ns(struct intel_context *ce);
 u64 intel_context_get_avg_runtime_ns(struct intel_context *ce);
 
 static inline u64 intel_context_clock(void)
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 04eacae1aca5..f7ff4c7d81c7 100644
@@ -38,6 +38,9 @@ struct intel_context_ops {
 #define COPS_RUNTIME_CYCLES_BIT 1
 #define COPS_RUNTIME_CYCLES BIT(COPS_RUNTIME_CYCLES_BIT)
 
+#define COPS_RUNTIME_ACTIVE_TOTAL_BIT 2
+#define COPS_RUNTIME_ACTIVE_TOTAL BIT(COPS_RUNTIME_ACTIVE_TOTAL_BIT)
+
 	int (*alloc)(struct intel_context *ce);
 
 	void (*revoke)(struct intel_context *ce, struct i915_request *rq,
@@ -56,6 +59,8 @@ struct intel_context_ops {
 
 	void (*sched_disable)(struct intel_context *ce);
 
+	void (*update_stats)(struct intel_context *ce);
+
 	void (*reset)(struct intel_context *ce);
 	void (*destroy)(struct kref *kref);
 
@@ -148,6 +153,7 @@ struct intel_context {
 			struct ewma_runtime avg;
 			u64 total;
 			u32 last;
+			u64 start_gt_clk;
 			I915_SELFTEST_DECLARE(u32 num_underflow);
 			I915_SELFTEST_DECLARE(u32 max_underflow);
 		} runtime;
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +5 -0
index 323b055e5db9..c7b54f1631b9 100644
@@ -196,6 +196,11 @@ static inline u8 guc_class_to_engine_class(u8 guc_class)
 	return guc_class_engine_class_map[guc_class];
 }
 
+/* Per context engine usage stats: */
+#define PPHWSP_GUC_CONTEXT_USAGE_STAMP_LO	(0x500 / sizeof(u32))
+#define PPHWSP_GUC_CONTEXT_USAGE_STAMP_HI	(PPHWSP_GUC_CONTEXT_USAGE_STAMP_LO + 1)
+#define PPHWSP_GUC_CONTEXT_USAGE_ENGINE_ID	(PPHWSP_GUC_CONTEXT_USAGE_STAMP_HI + 1)
+
 /* Work item for submitting workloads into work queue of GuC. */
 struct guc_wq_item {
 	u32 header;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +64 -1

126 lines not on this page · raw message

drivers/gpu/drm/i915/i915_drm_client.c +1 -5
index b09d1d386574..8d81119fff14 100644
@@ -147,11 +147,7 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
 		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
 	seq_printf(m, "drm-client-id:\t%u\n", client->id);
 
-	/*
-	 * Temporarily skip showing client engine information with GuC submission till
-	 * fetching engine busyness is implemented in the GuC submission backend
-	 */
-	if (GRAPHICS_VER(i915) < 8 || intel_uc_uses_guc_submission(&i915->gt0.uc))
+	if (GRAPHICS_VER(i915) < 8)
 		return;
 
 	for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v2 2/2 Fix per client busyness locking

1. Do all ce->stats updates and reads under guc->timestamp.lock
2. Pin context image before reading
3. Merge __guc_context_update_clks and guc_context_update_stats into a single function
4. Call lrc_update_runtime() unconditionally in guc_context_update_stats
5. Seems no need to update ce->stats.active with this approach

Some of the above steps may not be correct or complete but the idea is to discuss/improve the original patch.

v2 (Address review comments from Umesh):
* Don't pin context when called from intel_context_get_total_runtime_ns
* Don't pin context in lrc_update_runtime_locked
* ce->stats.active is now used to cache previous active value
* Move lrc_update_runtime to else in __guc_context_update_stats

Cc Umesh Nerlige Ramappa <[email protected]>
---
 drivers/gpu/drm/i915/gt/intel_context.c       |  9 +--
 drivers/gpu/drm/i915/gt/intel_context_types.h |  2 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 57 ++++++++++++-------
 3 files changed, 40 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -7
index e2d70a9fdac0..51dce98003ec 100644
@@ -581,19 +581,14 @@ u64 intel_context_get_total_runtime_ns(struct intel_context *ce)
 	u64 total, active;
 
 	if (ce->ops->update_stats)
-		ce->ops->update_stats(ce);
+		return ce->ops->update_stats(ce, false);
 
 	total = ce->stats.runtime.total;
 	if (ce->ops->flags & COPS_RUNTIME_CYCLES)
 		total *= ce->engine->gt->clock_period_ns;
 
 	active = READ_ONCE(ce->stats.active);
-	/*
-	 * When COPS_RUNTIME_ACTIVE_TOTAL is set for ce->cops, the backend
-	 * already provides the total active time of the context, so skip this
-	 * calculation when this flag is set.
-	 */
-	if (active && !(ce->ops->flags & COPS_RUNTIME_ACTIVE_TOTAL))
+	if (active)
 		active = intel_context_clock() - active;
 
 	return total + active;
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -1
index f7ff4c7d81c7..06dad9eeecdb 100644
@@ -59,7 +59,7 @@ struct intel_context_ops {
 
 	void (*sched_disable)(struct intel_context *ce);
 
-	void (*update_stats)(struct intel_context *ce);
+	u64 (*update_stats)(struct intel_context *ce, bool pin);
 
 	void (*reset)(struct intel_context *ce);
 	void (*destroy)(struct kref *kref);
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +37 -20

103 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [Intel-gfx] [RFC PATCH v2 2/2] Fix per client busyness locking

reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Ashutosh,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on drm-tip/drm-tip]

url:    https://github.com/intel-lab-lkp/linux/commits/Ashutosh-Dixit/i915-pmu-Wire-GuC-backend-to-per-client-busyness/20220902-075534
base:   git://anongit.freedesktop.org/drm/drm-tip drm-tip
config: i386-randconfig-a004 (https://download.01.org/0day-ci/archive/20220902/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/b1db15edb2416229fdde749425de6a6ae7a4b94a
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Ashutosh-Dixit/i915-pmu-Wire-GuC-backend-to-per-client-busyness/20220902-075534 git checkout b1db15edb2416229fdde749425de6a6ae7a4b94a
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/gpu/drm/i915/

If you fix the issue, kindly add following tag where applicable

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1512:5: warning: no previous prototype for function 'guc_context_update_stats' [-Wmissing-prototypes]
   u64 guc_context_update_stats(struct intel_context *ce, bool pin)
       ^

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1512:1: note: declare 'static' if the function is not intended to be used outside of this translation unit u64 guc_context_update_stats(struct intel_context *ce, bool pin)
^
static
1 warning generated.

vim +/guc_context_update_stats +1512 drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c

1511

> 1512	u64 guc_context_update_stats(struct intel_context *ce, bool pin)

1513 {
1514 struct intel_guc *guc = ce_to_guc(ce);
1515 unsigned long flags;
1516 u64 ret;
1517
1518 if (pin) {
1519 intel_context_pin(ce);
1520 } else {
1521 /* Return previously computed value */
1522 spin_lock_irqsave(&guc->timestamp.lock, flags);
1523 ret = ce->stats.runtime.total + ce->stats.active;
1524 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1525 return ret;
1526 }
1527
1528 spin_lock_irqsave(&guc->timestamp.lock, flags);
1529 ret = __guc_context_update_stats(ce);
1530 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1531
1532 intel_context_unpin(ce);
1533 return ret;
1534 }
1535

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#3 lore raw

[Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [1/2] i915/pmu: Wire GuC backend to per-client busyness

In reply to Ashutosh Dixit
== Series Details ==

== Series Details ==

Series: series starting with [1/2] i915/pmu: Wire GuC backend to per-client busyness
URL : https://patchwork.freedesktop.org/series/108053/
State : failure

== Summary ==

Error: make failed
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
CC [M] drivers/gpu/drm/i915/gt/uc/intel_guc_submission.o
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1512:5: error: no previous prototype for ‘guc_context_update_stats’ [-Werror=missing-prototypes]
u64 guc_context_update_stats(struct intel_context *ce, bool pin)

     ^~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
scripts/Makefile.build:249: recipe for target 'drivers/gpu/drm/i915/gt/uc/intel_guc_submission.o' failed make[4]: *** [drivers/gpu/drm/i915/gt/uc/intel_guc_submission.o] Error 1 scripts/Makefile.build:465: recipe for target 'drivers/gpu/drm/i915' failed
make[3]: *** [drivers/gpu/drm/i915] Error 2
scripts/Makefile.build:465: recipe for target 'drivers/gpu/drm' failed
make[2]: *** [drivers/gpu/drm] Error 2
scripts/Makefile.build:465: recipe for target 'drivers/gpu' failed
make[1]: *** [drivers/gpu] Error 2
Makefile:1853: recipe for target 'drivers' failed
make: *** [drivers] Error 2