Skip to contentSkip to conversation

drm/msm: Decouple IFPC disable and sysprof

PATCH Superseded Rob Clark 5 participants 18 messages Nov 2 – Nov 3

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Rob Clark

author maintainer
#0 lore raw

v2 cover drm/msm: Decouple IFPC disable and sysprof

Convert IFPC-disable into it's own refcnt so that multiple threads can request IFPC to be disabled without stomping on each other.

Extracted out of gen8 GPU state capture series[1], as other than fixing a missing IFPC disable in debugfs state capture path, it is unrelated.

[1] https://patchwork.freedesktop.org/series/176505/

v2:
- Fix double acquire of gmu->lock
- Fix missing a8xx hw_init() path
- Link to v1: https://lore.kernel.org/all/[email protected]/

Rob Clark (4):
drm/msm: Rework IFPC disable
drm/msm: Add cleanup help for disabling IFPC drm/msm/debugfs: Use cleanup.h for gpu state dump drm/msm: Inhibit IFPC for state snapshot

 drivers/gpu/drm/msm/adreno/a6xx_gmu.c | 32 +++++++++++++++------------
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 12 ++++------
 drivers/gpu/drm/msm/adreno/a6xx_gpu.h |  3 ++-
 drivers/gpu/drm/msm/adreno/a8xx_gpu.c |  6 +----
 drivers/gpu/drm/msm/msm_debugfs.c     | 31 +++++++++++---------------
 drivers/gpu/drm/msm/msm_gpu.c         |  1 +
 drivers/gpu/drm/msm/msm_gpu.h         | 30 +++++++++++++++++++++----
 drivers/gpu/drm/msm/msm_perfcntr.c    |  7 +-----
 drivers/gpu/drm/msm/msm_submitqueue.c | 10 +++++----
 9 files changed, 72 insertions(+), 60 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH v2 1/4 drm/msm: Rework IFPC disable

Previously this was coupled to SYSPROF level, which made sense at the time. But there are other places that we need to inhibit IFPC. So decouple it from the sysprof refcount.

Now it has it's own refcount, so multiple threads can vote to inhibit IFPC safely. The renamed gpu->ifpc_update() simply applies the new state by setting or clearing an OOB request.

---
 drivers/gpu/drm/msm/adreno/a6xx_gmu.c | 32 +++++++++++++++------------
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 12 ++++------
 drivers/gpu/drm/msm/adreno/a6xx_gpu.h |  3 ++-
 drivers/gpu/drm/msm/adreno/a8xx_gpu.c |  6 +----
 drivers/gpu/drm/msm/msm_gpu.c         |  1 +
 drivers/gpu/drm/msm/msm_gpu.h         | 26 ++++++++++++++++++----
 drivers/gpu/drm/msm/msm_perfcntr.c    |  6 ++---
 drivers/gpu/drm/msm/msm_submitqueue.c | 10 +++++----
 8 files changed, 56 insertions(+), 40 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +18 -14
index 662f09bec9d5..bba5468db42e 100644
@@ -2128,29 +2128,33 @@ static int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev,
 	return irq;
 }
 
-void a6xx_gmu_sysprof_setup(struct msm_gpu *gpu, bool force_on)
+void a6xx_gmu_ifpc_update_locked(struct msm_gpu *gpu)
 {
-	bool sysprof = msm_gpu_sysprof_no_ifpc(gpu) || force_on;
 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
 	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
 	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
+	bool force_on = refcount_read(&gpu->ifpc_disable) > 1;
 
-	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
-	if (!pm_runtime_get_if_active(&gpu->pdev->dev))
-		return;
-
-	mutex_lock(&gmu->lock);
+	WARN_ON_ONCE(!mutex_is_locked(&gmu->lock));
 
-	/*
-	 * 'Perfcounter select' register values are lost during IFPC collapse. To avoid that,
-	 * use the currently unused perfcounter oob vote to block IFPC when sysprof is active
-	 */
-	if (sysprof && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	if (force_on && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
 		a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-	else if (!sysprof && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	else if (!force_on && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
 		a6xx_gmu_clear_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
+}
 
-	mutex_unlock(&gmu->lock);
+void a6xx_gmu_ifpc_update(struct msm_gpu *gpu)
+{
+	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
+	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
+	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
+
+	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
+	if (pm_runtime_get_if_active(&gpu->pdev->dev) <= 0)
+		return;
+
+	scoped_guard (mutex, &gmu->lock)
+		a6xx_gmu_ifpc_update_locked(gpu);
 
 	pm_runtime_put(&gpu->pdev->dev);
 }
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +4 -8
index 09d76cdf059f..bf71129e33a9 100644
@@ -1565,11 +1565,7 @@ static int hw_init(struct msm_gpu *gpu)
 		a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_BOOT_SLUMBER);
 	}
 
-	if (!ret && msm_gpu_sysprof_no_ifpc(gpu)) {
-		ret = a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-		if (!ret)
-			set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status);
-	}
+	a6xx_gmu_ifpc_update_locked(gpu);
 
 	return ret;
 }
@@ -2851,7 +2847,7 @@ const struct adreno_gpu_funcs a6xx_gpu_funcs = {
 		.create_private_vm = a6xx_create_private_vm,
 		.get_rptr = a6xx_get_rptr,
 		.progress = a6xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
 		.perfcntr_configure = a6xx_perfcntr_configure,
 	},
 	.init = a6xx_gpu_init,
@@ -2922,7 +2918,7 @@ const struct adreno_gpu_funcs a7xx_gpu_funcs = {
 		.create_private_vm = a6xx_create_private_vm,
 		.get_rptr = a6xx_get_rptr,
 		.progress = a6xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
 		.perfcntr_configure = a6xx_perfcntr_configure,
 	},
 	.init = a6xx_gpu_init,
@@ -2953,7 +2949,7 @@ const struct adreno_gpu_funcs a8xx_gpu_funcs = {
 		.create_private_vm = a6xx_create_private_vm,
 		.get_rptr = a6xx_get_rptr,
 		.progress = a8xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
 		.perfcntr_configure = a6xx_perfcntr_configure,
 		.perfcntr_flush = a8xx_perfcntr_flush,
 	},
drivers/gpu/drm/msm/adreno/a6xx_gpu.h +2 -1
index 81d21acccea1..cb0ac27dacba 100644
@@ -298,7 +298,8 @@ void a6xx_gmu_clear_oob(struct a6xx_gmu *gmu, enum a6xx_gmu_oob_state state);
 int a6xx_gmu_init(struct a6xx_gpu *a6xx_gpu, struct device_node *node);
 int a6xx_gmu_wrapper_init(struct a6xx_gpu *a6xx_gpu, struct device_node *node);
 void a6xx_gmu_remove(struct a6xx_gpu *a6xx_gpu);
-void a6xx_gmu_sysprof_setup(struct msm_gpu *gpu, bool force_on);
+void a6xx_gmu_ifpc_update_locked(struct msm_gpu *gpu);
+void a6xx_gmu_ifpc_update(struct msm_gpu *gpu);
 
 void a6xx_preempt_init(struct msm_gpu *gpu);
 void a6xx_preempt_hw_init(struct msm_gpu *gpu);
drivers/gpu/drm/msm/adreno/a8xx_gpu.c +1 -5
index e7d04c865968..ecf15a8f68de 100644
@@ -846,11 +846,7 @@ static int hw_init(struct msm_gpu *gpu)
 	 */
 	a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_GPU_SET);
 
-	if (!ret && msm_gpu_sysprof_no_perfcntr_zap(gpu)) {
-		ret = a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-		if (!ret)
-			set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status);
-	}
+	a6xx_gmu_ifpc_update_locked(gpu);
 
 	return ret;
 }
drivers/gpu/drm/msm/msm_gpu.c +1 -0
index fb07803f5aba..37f9e93daa43 100644
@@ -1012,6 +1012,7 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
 	gpu->nr_rings = nr_rings;
 
 	refcount_set(&gpu->sysprof_active, 1);
+	refcount_set(&gpu->ifpc_disable, 1);
 
 	mutex_init(&gpu->perfcntr_lock);
 
drivers/gpu/drm/msm/msm_gpu.h +22 -4
index d27d54bdb7a7..0334a0f604ac 100644
@@ -93,7 +93,7 @@ struct msm_gpu_funcs {
 	 * for cmdstream that is buffered in this FIFO upstream of the CP fw.
 	 */
 	bool (*progress)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
-	void (*sysprof_setup)(struct msm_gpu *gpu, bool force_on);
+	void (*ifpc_update)(struct msm_gpu *gpu);
 
 	/* Configure perfcntr SELect regs: */
 	void (*perfcntr_configure)(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
@@ -186,6 +186,14 @@ struct msm_gpu {
 	 */
 	refcount_t sysprof_active;
 
+	/**
+	 * ifpc_disable:
+	 *
+	 * Count of votes to disable IFPC (1-based, values above 1 mean IFPC
+	 * is disabled).
+	 */
+	refcount_t ifpc_disable;
+
 	/**
 	 * lock:
 	 *
@@ -375,10 +383,20 @@ msm_gpu_sysprof_no_perfcntr_zap(struct msm_gpu *gpu)
 		(gpu->perfcntrs && READ_ONCE(gpu->perfcntrs->stream));
 }
 
-static inline bool
-msm_gpu_sysprof_no_ifpc(struct msm_gpu *gpu)
+static inline void
+msm_gpu_disable_ifpc_get(struct msm_gpu *gpu)
+{
+	refcount_inc(&gpu->ifpc_disable);
+	if (gpu->funcs->ifpc_update)
+		gpu->funcs->ifpc_update(gpu);
+}
+
+static inline void
+msm_gpu_disable_ifpc_put(struct msm_gpu *gpu)
 {
-	return refcount_read(&gpu->sysprof_active) > 1;
+	refcount_dec(&gpu->ifpc_disable);
+	if (gpu->funcs->ifpc_update)
+		gpu->funcs->ifpc_update(gpu);
 }
 
 /*
drivers/gpu/drm/msm/msm_perfcntr.c +2 -4
index ce65b1160955..867dd476ed88 100644
@@ -259,8 +259,7 @@ sample_worker(struct kthread_work *work)
 	}
 
 	/* Inhibit IFPC while accessing registers: */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, true);
+	msm_gpu_disable_ifpc_get(gpu);
 
 	if (gpu->funcs->perfcntr_flush)
 		gpu->funcs->perfcntr_flush(gpu);
@@ -297,8 +296,7 @@ sample_worker(struct kthread_work *work)
 	}
 
 	/* Re-enable IFPC: */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, false);
+	msm_gpu_disable_ifpc_put(gpu);
 
 	smp_store_release(&stream->fifo.head, head);
 	wake_up_all(&stream->poll_wq);
drivers/gpu/drm/msm/msm_submitqueue.c +6 -4
index d307cdd17d90..8434a09319bc 100644
@@ -24,7 +24,10 @@ int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sy
 		pm_runtime_get_sync(&gpu->pdev->dev);
 		fallthrough;
 	case 1:
+		/* Disable zapping perfcntrs on context switch: */
 		refcount_inc(&gpu->sysprof_active);
+		/* Disable IFPC: */
+		msm_gpu_disable_ifpc_get(gpu);
 		fallthrough;
 	case 0:
 		break;
@@ -36,16 +39,15 @@ int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sy
 		pm_runtime_put_autosuspend(&gpu->pdev->dev);
 		fallthrough;
 	case 1:
+		/* Drop our vote for inhibiting IFPC: */
+		msm_gpu_disable_ifpc_put(gpu);
+		/* Drop our vote for disabling perfcntr zapping: */
 		refcount_dec(&gpu->sysprof_active);
 		fallthrough;
 	case 0:
 		break;
 	}
 
-	/* Some gpu families require additional setup for sysprof */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, false);
-
 	ctx->sysprof = sysprof;
 
 	return 0;
Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH v2 2/4 drm/msm: Add cleanup help for disabling IFPC

In reply to Rob Clark

Simplify error path cleanup for IFPC-disable critical sections.

Note that this moves where we re-enable IFPC until after the wake_up_all(), but that is harmless.

---
 drivers/gpu/drm/msm/msm_gpu.h      | 4 ++++
 drivers/gpu/drm/msm/msm_perfcntr.c | 5 +----
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/msm/msm_gpu.h +4 -0
index 0334a0f604ac..e7912d2180a5 100644
@@ -399,6 +399,10 @@ msm_gpu_disable_ifpc_put(struct msm_gpu *gpu)
 		gpu->funcs->ifpc_update(gpu);
 }
 
+DEFINE_GUARD(ifpc_disable, struct msm_gpu *,
+	     msm_gpu_disable_ifpc_get(_T),
+	     msm_gpu_disable_ifpc_put(_T))
+
 /*
  * The number of priority levels provided by drm gpu scheduler.  The
  * DRM_SCHED_PRIORITY_KERNEL priority level is treated specially in some
drivers/gpu/drm/msm/msm_perfcntr.c +1 -4
index 867dd476ed88..995857273aca 100644
@@ -259,7 +259,7 @@ sample_worker(struct kthread_work *work)
 	}
 
 	/* Inhibit IFPC while accessing registers: */
-	msm_gpu_disable_ifpc_get(gpu);
+	guard(ifpc_disable)(gpu);
 
 	if (gpu->funcs->perfcntr_flush)
 		gpu->funcs->perfcntr_flush(gpu);
@@ -295,9 +295,6 @@ sample_worker(struct kthread_work *work)
 		}
 	}
 
-	/* Re-enable IFPC: */
-	msm_gpu_disable_ifpc_put(gpu);
-
 	smp_store_release(&stream->fifo.head, head);
 	wake_up_all(&stream->poll_wq);
 }
Signature · 1 line
2.56.0
author maintainer
#8 lore raw

PATCH v2 3/4 drm/msm/debugfs: Use cleanup.h for gpu state dump

In reply to Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
 drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +12 -19
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
 	struct drm_device *dev = inode->i_private;
 	struct msm_drm_private *priv = dev->dev_private;
 	struct msm_gpu *gpu = priv->gpu;
-	struct msm_gpu_show_priv *show_priv;
 	int ret;
 
 	if (!gpu || !gpu->funcs->gpu_state_get)
 		return -ENODEV;
 
-	show_priv = kmalloc_obj(*show_priv);
+	struct msm_gpu_show_priv *show_priv __free(kfree)
+		= kmalloc_obj(*show_priv);
 	if (!show_priv)
 		return -ENOMEM;
 
-	ret = mutex_lock_interruptible(&gpu->lock);
-	if (ret)
-		goto free_priv;
-
-	pm_runtime_get_sync(&gpu->pdev->dev);
-	msm_gpu_hw_init(gpu);
-	show_priv->state = gpu->funcs->gpu_state_get(gpu);
-	pm_runtime_put_sync(&gpu->pdev->dev);
-
-	mutex_unlock(&gpu->lock);
+	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+			msm_gpu_hw_init(gpu);
+			show_priv->state = gpu->funcs->gpu_state_get(gpu);
+		}
+	}
 
 	if (IS_ERR(show_priv->state)) {
-		ret = PTR_ERR(show_priv->state);
-		goto free_priv;
+		return PTR_ERR(show_priv->state);
 	}
 
 	show_priv->dev = dev;
 
 	ret = single_open(file, msm_gpu_show, show_priv);
 	if (ret)
-		goto free_priv;
+		return ret;
 
-	return 0;
+	retain_and_null_ptr(show_priv);
 
-free_priv:
-	kfree(show_priv);
-	return ret;
+	return 0;
 }
 
 static const struct file_operations msm_gpu_fops = {
Signature · 1 line
2.56.0
author maintainer
#14 lore raw

PATCH v2 4/4 drm/msm: Inhibit IFPC for state snapshot

In reply to Rob Clark

We need the GPU to stay awake for this.

---
 drivers/gpu/drm/msm/msm_debugfs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/msm/msm_debugfs.c +2 -0
index aa59b28a3392..29209c23fcfa 100644
@@ -81,6 +81,8 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
 		return -ENOMEM;
 
 	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		guard(ifpc_disable)(gpu);
+
 		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
 			msm_gpu_hw_init(gpu);
 			show_priv->state = gpu->funcs->gpu_state_get(gpu);
Signature · 1 line
2.56.0
#15 lore raw

Re: [PATCH v2 4/4] drm/msm: Inhibit IFPC for state snapshot

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] drm/msm: premature GMU_OOB_PERFCOUNTER_SET before msm_gpu_hw_init() - [High] drm/msm/adreno: stale…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] drm/msm: premature GMU_OOB_PERFCOUNTER_SET before msm_gpu_hw_init()
- [High] drm/msm/adreno: stale GMU_STATUS_OOB_PERF_SET across a6xx_gmu_force_off()
- [High] drm/msm: pm_runtime_active outside gpu->lock in msm_gpu_open() blocks recovery --

commit c139e04b0443554ac873bfd8c98898fb001e8ce3
Author: Rob Clark <[email protected]>

drm/msm: Inhibit IFPC for state snapshot

This commit adds an ifpc_disable guard in msm_gpu_open() to keep the GPU awake while capturing the debugfs GPU state snapshot.

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 84

Quoted 9 lines
drivers/gpu/drm/msm/msm_debugfs.c +1 -0
index 9373ac19ee953..89d5c52e04678 100644
@@ -81,6 +81,8 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
 		return -ENOMEM;
 
 	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		guard(ifpc_disable)(gpu);

[Severity: High]
As reported by Sashiko on v1 [1], can placing guard(ifpc_disable)(gpu) before msm_gpu_hw_init(gpu) send a premature GMU_OOB_PERFCOUNTER_SET vote before the initial GMU_OOB_GPU_SET handshake completes?

When msm_gpu_open() is called while the GPU is runtime-suspended, scoped_guard(pm_runtime_active, &gpu->pdev->dev) invokes a6xx_gmu_pm_resume(), which sets gpu->needs_hw_init = true, boots the GMU firmware, and transitions &gpu->pdev->dev to RPM_ACTIVE without running hw_init().

Immediately afterward, guard(ifpc_disable)(gpu) calls:

msm_gpu_disable_ifpc_get(gpu)

    -> a6xx_gmu_ifpc_update(gpu)
      -> a6xx_gmu_ifpc_update_locked(gpu)

In drivers/gpu/drm/msm/adreno/a6xx_gmu.c:a6xx_gmu_ifpc_update():

	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
	if (pm_runtime_get_if_active(&gpu->pdev->dev) <= 0)
		return;

	scoped_guard (mutex, &gmu->lock)
		a6xx_gmu_ifpc_update_locked(gpu);

Because the outer pm_runtime_active guard already resumed the device, pm_runtime_get_if_active() succeeds even though gpu->needs_hw_init is still true. a6xx_gmu_ifpc_update_locked() then sets GMU_STATUS_OOB_PERF_SET in gmu->status and asserts GMU_OOB_PERFCOUNTER_SET before msm_gpu_hw_init() -> hw_init() sets and clears GMU_OOB_GPU_SET (and GMU_OOB_BOOT_SLUMBER on legacy GMUs). If mutex_intr fails with -EINTR, GMU_OOB_PERFCOUNTER_SET is asserted and cleared without msm_gpu_hw_init() ever running.

Furthermore, when msm_gpu_hw_init() subsequently runs hw_init() in drivers/gpu/drm/msm/adreno/a6xx_gpu.c:

	a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_GPU_SET);

	if (a6xx_gpu->gmu.legacy) {
		/* Take the GMU out of its special boot mode */
		a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_BOOT_SLUMBER);
	}

	a6xx_gmu_ifpc_update_locked(gpu);

test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status) in a6xx_gmu_ifpc_update_locked() finds the bit already set and becomes a no-op instead of asserting GMU_OOB_PERFCOUNTER_SET after GMU_OOB_GPU_SET is cleared. As noted in commit d34b6919798c ("drm/msm/a6xx: Correct OOB usage"), issuing GMU_OOB_PERFCOUNTER_SET before the GMU_OOB_GPU_SET handshake completes confuses the GMU firmware internal power state.

The same sequence also happens in drivers/gpu/drm/msm/msm_submitqueue.c:msm_context_set_sysprof() when sysprof == 2 wakes a suspended GPU:

	case 2:
		pm_runtime_get_sync(&gpu->pdev->dev);
		fallthrough;
	case 1:
		/* Disable zapping perfcntrs on context switch: */
		refcount_inc(&gpu->sysprof_active);
		/* Disable IFPC: */
		msm_gpu_disable_ifpc_get(gpu);

[Severity: High]
In a related path, can GMU_STATUS_OOB_PERF_SET remain stale across a forced GMU power-off in a6xx_gmu_force_off() and leave IFPC uninhibited after recovery?

In drivers/gpu/drm/msm/adreno/a6xx_gmu.c:a6xx_gmu_ifpc_update_locked():

	if (force_on && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
		a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
	else if (!force_on && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
		a6xx_gmu_clear_oob(gmu, GMU_OOB_PERFCOUNTER_SET);

Prior to commit 15aefb4a3ef6 ("drm/msm: Rework IFPC disable"), hw_init() in a6xx_gpu.c and a8xx_gpu.c unconditionally called a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET) whenever IFPC was disabled, regardless of the prior state of GMU_STATUS_OOB_PERF_SET. Now hw_init() calls a6xx_gmu_ifpc_update_locked(gpu), which skips a6xx_gmu_set_oob() if GMU_STATUS_OOB_PERF_SET is already set.

However, GMU_STATUS_OOB_PERF_SET is only cleared on the graceful shutdown path in a6xx_gmu_shutdown(). When a6xx_gmu_stop() takes the gmu->hung branch to a6xx_gmu_force_off(gmu), or when a6xx_gmu_shutdown() jumps to force_off on an OOB failure, a6xx_gmu_force_off() and a6xx_rpmh_stop() clear GMU_STATUS_FW_START but do not clear GMU_STATUS_OOB_PERF_SET:

	/* Reset GPU core blocks */
	a6xx_gpu_sw_reset(gpu, true);

	a6xx_rpmh_stop(gmu);

If a hang recovery or forced GMU shutdown occurs while gpu->ifpc_disable > 1, won't GMU_STATUS_OOB_PERF_SET stay set in gmu->status across the hardware reset, causing a6xx_gmu_ifpc_update_locked() in hw_init() to skip re-asserting GMU_OOB_PERFCOUNTER_SET on the restarted GMU?

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 88

Quoted 4 lines
drivers/gpu/drm/msm/msm_debugfs.c +1 -0
+
 		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
 			msm_gpu_hw_init(gpu);
 			show_priv->state = gpu->funcs->gpu_state_get(gpu);

[Severity: High]
Does holding pm_runtime_active outside gpu->lock in msm_gpu_open() prevent the GPU from suspending during hang recovery in a6xx_recover() and a8xx_recover()?

Prior to commit 8504c24acea0 ("drm/msm/debugfs: Use cleanup.h for gpu state dump"), msm_gpu_open() acquired gpu->lock first and called pm_runtime_get_sync() and pm_runtime_put_sync() inside gpu->lock. With scoped_guard(pm_runtime_active, &gpu->pdev->dev) outside scoped_cond_guard(mutex_intr, return -EINTR, &gpu->lock), msm_gpu_open() acquires an active runtime PM reference before waiting on gpu->lock and releases it asynchronously via pm_runtime_put() after unlocking gpu->lock.

During hang recovery, recover_worker() holds gpu->lock and calls a6xx_recover() (or a8xx_recover()), which drops its runtime PM references expecting the device usage count to reach zero so the GMU suspends and the CX GDSC collapses:

	/* Drop the rpm refcount from active submits */
	if (active_submits)
		pm_runtime_put(&gpu->pdev->dev);

	/* And the final one from recover worker */
	pm_runtime_put_sync(&gpu->pdev->dev);

	if (!wait_for_completion_timeout(&gmu->pd_gate, msecs_to_jiffies(1000)))
		DRM_DEV_ERROR(&gpu->pdev->dev, "cx gdsc didn't collapse\n");

If a user or debugging tool opens the debugfs gpu file while recover_worker() holds gpu->lock (or right before recover_worker() takes gpu->lock while the asynchronous pm_runtime_put() from msm_gpu_open() is still pending), won't the extra runtime PM reference keep the usage count above zero, causing gmu->pd_gate to time out with "cx gdsc didn't collapse" and leaving gpu->needs_hw_init false so msm_gpu_hw_init() skips re-initializing the hung GPU?

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#2 lore raw

Re: [PATCH v2 1/4] drm/msm: Rework IFPC disable

In reply to Rob Clark
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] drm/msm/adreno: stale GMU_STATUS_OOB_PERF_SET after a6xx_gmu_force_off() - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] drm/msm/adreno: stale GMU_STATUS_OOB_PERF_SET after a6xx_gmu_force_off()
- [Medium] drm/msm/adreno: premature GMU_OOB_PERFCOUNTER_SET in a6xx_gmu_ifpc_update()
- [Medium] drm/msm/adreno: unhandled OOB errors in a6xx_gmu_ifpc_update_locked() --

commit 15aefb4a3ef6c261aac3a46fc7b4e9cde2215554
Author: Rob Clark <[email protected]>

drm/msm: Rework IFPC disable

This commit decouples IFPC inhibition from the sysprof refcount by introducing a dedicated gpu->ifpc_disable refcount and replacing gpu->funcs->sysprof_setup() with gpu->funcs->ifpc_update().

Quoted 25 lines
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +3 -8
index 662f09bec9d52..bba5468db42e9 100644
@@ -2128,29 +2128,33 @@ static int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev,
 	return irq;
 }
 
-void a6xx_gmu_sysprof_setup(struct msm_gpu *gpu, bool force_on)
+void a6xx_gmu_ifpc_update_locked(struct msm_gpu *gpu)
 {
-	bool sysprof = msm_gpu_sysprof_no_ifpc(gpu) || force_on;
 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
 	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
 	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
+	bool force_on = refcount_read(&gpu->ifpc_disable) > 1;
 
-	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
-	if (!pm_runtime_get_if_active(&gpu->pdev->dev))
-		return;
-
-	mutex_lock(&gmu->lock);
+	WARN_ON_ONCE(!mutex_is_locked(&gmu->lock));
 
-	/*

drivers/gpu/drm/msm/adreno/a6xx_gmu.c static int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev, · line 2144

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +3 -5
-	 * 'Perfcounter select' register values are lost during IFPC collapse. To avoid that,
-	 * use the currently unused perfcounter oob vote to block IFPC when sysprof is active
-	 */
-	if (sysprof && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	if (force_on && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
 		a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-	else if (!sysprof && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	else if (!force_on && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
 		a6xx_gmu_clear_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
+}

[Severity: High]
As reported by Sashiko on v1 [1], can a6xx_gmu_ifpc_update_locked() skip re-asserting GMU_OOB_PERFCOUNTER_SET during hw_init() after a forced GMU shutdown?

GMU_STATUS_OOB_PERF_SET in gmu->status is normally cleared during a graceful shutdown in a6xx_gmu_shutdown(). However, if a6xx_gmu_stop() takes the a6xx_gmu_force_off() path because gmu->hung is true (or if a6xx_gmu_shutdown() jumps to force_off when GMU_OOB_GPU_SET fails), a6xx_gmu_force_off() resets the GMU CM3 core and powers down RPMh without clearing GMU_STATUS_OOB_PERF_SET:

drivers/gpu/drm/msm/adreno/a6xx_gmu.c:a6xx_gmu_stop() {
	if (gmu->hung)
		a6xx_gmu_force_off(gmu);
	else
		a6xx_gmu_shutdown(gmu);
}

Previously, hw_init() in a6xx_gpu.c and a8xx_gpu.c unconditionally called a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET) and set GMU_STATUS_OOB_PERF_SET when IFPC inhibition was active. With hw_init() now calling a6xx_gmu_ifpc_update_locked(), test_and_set_bit() sees the stale GMU_STATUS_OOB_PERF_SET bit and skips sending GMU_OOB_PERFCOUNTER_SET to the freshly reset GMU, leaving IFPC enabled in hardware while gpu->ifpc_disable > 1.

Quoted 7 lines
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +5 -1
-	mutex_unlock(&gmu->lock);
+void a6xx_gmu_ifpc_update(struct msm_gpu *gpu)
+{
+	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
+	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
+	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;

drivers/gpu/drm/msm/adreno/a6xx_gmu.c static int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev, · line 2160

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +7 -0
+
+	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
+	if (pm_runtime_get_if_active(&gpu->pdev->dev) <= 0)
+		return;
+
+	scoped_guard (mutex, &gmu->lock)
+		a6xx_gmu_ifpc_update_locked(gpu);
 
 	pm_runtime_put(&gpu->pdev->dev);
 }

[Severity: Medium]
Can a6xx_gmu_ifpc_update() send GMU_OOB_PERFCOUNTER_SET after runtime resume before hw_init() has completed the initial GMU_OOB_GPU_SET handshake?

When the GPU is runtime-resumed via adreno_runtime_resume() -> a6xx_gmu_pm_resume(), the GMU is booted and gpu->needs_hw_init is set to true, while msm_gpu_hw_init() is deferred until later. At that point, pm_runtime_get_if_active(&gpu->pdev->dev) returns 1 even though hw_init() has not run yet.

In msm_context_set_sysprof():

drivers/gpu/drm/msm/msm_submitqueue.c:msm_context_set_sysprof() {
	case 2:
		pm_runtime_get_sync(&gpu->pdev->dev);
		fallthrough;
	case 1:
		/* Disable zapping perfcntrs on context switch: */
		refcount_inc(&gpu->sysprof_active);
		/* Disable IFPC: */
		msm_gpu_disable_ifpc_get(gpu);
}

If the GPU was runtime-suspended, pm_runtime_get_sync() wakes the GMU with gpu->needs_hw_init == true, and msm_gpu_disable_ifpc_get() immediately calls a6xx_gmu_ifpc_update() -> a6xx_gmu_ifpc_update_locked().

This issues GMU_OOB_PERFCOUNTER_SET before hw_init() performs the initial GMU_OOB_GPU_SET handshake (which commit d34b6919798c noted confuses GMU firmware state), and sets GMU_STATUS_OOB_PERF_SET in gmu->status so that the subsequent hw_init() call to a6xx_gmu_ifpc_update_locked() skips sending GMU_OOB_PERFCOUNTER_SET after clearing GMU_OOB_GPU_SET.

Should a6xx_gmu_ifpc_update_locked() (or a6xx_gmu_ifpc_update()) check !gpu->needs_hw_init before touching GMU OOB state?

Quoted 7 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -0
index 92e8c8abb86da..773c1727dd31a 100644
@@ -1537,11 +1537,7 @@ static int hw_init(struct msm_gpu *gpu)
 		a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_BOOT_SLUMBER);
 	}

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static int hw_init(struct msm_gpu *gpu) · line 1543

Quoted 10 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +1 -5
 
-	if (!ret && msm_gpu_sysprof_no_ifpc(gpu)) {
-		ret = a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-		if (!ret)
-			set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status);
-	}
+	a6xx_gmu_ifpc_update_locked(gpu);
 
 	return ret;
 }

[Severity: Medium]
As reported by Sashiko on v1 [2], is it intentional that hw_init() in both a6xx_gpu.c and a8xx_gpu.c now calls a6xx_gmu_ifpc_update_locked() at the out label even when ret != 0, and ignores errors from a6xx_gmu_set_oob()?

Previously, if adreno_hw_init() or CP initialization failed and jumped to out with a non-zero ret, GMU_OOB_PERFCOUNTER_SET was skipped because of the !ret check. In addition, if a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET) timed out, GMU_STATUS_OOB_PERF_SET was not set and the error was returned from hw_init().

With this change, a6xx_gmu_ifpc_update_locked() sets GMU_STATUS_OOB_PERF_SET via test_and_set_bit() before calling a6xx_gmu_set_oob() and discards its return value, leaving GMU_STATUS_OOB_PERF_SET set even if the OOB request fails.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/4] drm/msm: Rework IFPC disable

In reply to Rob Clark
Quoted 10 lines · Rob Clark

Previously this was coupled to SYSPROF level, which made sense at the time. But there are other places that we need to inhibit IFPC. So decouple it from the sysprof refcount.

Now it has it's own refcount, so multiple threads can vote to inhibit IFPC safely. The renamed gpu->ifpc_update() simply applies the new state by setting or clearing an OOB request.

---

Sashiko still has some comments

Konrad

#9 lore raw

Re: [PATCH v2 3/4] drm/msm/debugfs: Use cleanup.h for gpu state dump

In reply to Rob Clark
Quoted 34 lines · Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
 drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +2 -7
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
 	struct drm_device *dev = inode->i_private;
 	struct msm_drm_private *priv = dev->dev_private;
 	struct msm_gpu *gpu = priv->gpu;
-	struct msm_gpu_show_priv *show_priv;
 	int ret;
 
 	if (!gpu || !gpu->funcs->gpu_state_get)
 		return -ENODEV;
 
-	show_priv = kmalloc_obj(*show_priv);
+	struct msm_gpu_show_priv *show_priv __free(kfree)
+		= kmalloc_obj(*show_priv);
 	if (!show_priv)
 		return -ENOMEM;
 
-	ret = mutex_lock_interruptible(&gpu->lock);
-	if (ret)
-		goto free_priv;
-
-	pm_runtime_get_sync(&gpu->pdev->dev);

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 87

Quoted 10 lines
drivers/gpu/drm/msm/msm_debugfs.c +5 -5
-	msm_gpu_hw_init(gpu);
-	show_priv->state = gpu->funcs->gpu_state_get(gpu);
-	pm_runtime_put_sync(&gpu->pdev->dev);
-
-	mutex_unlock(&gpu->lock);
+	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+			msm_gpu_hw_init(gpu);
+			show_priv->state = gpu->funcs->gpu_state_get(gpu);
+		}

My clanker tells me we can't grab the PM reference before a mutex, as the recovery mechanism depends on the rpm count being appropriate to let it drop it (and kill gx this way)

Konrad

Reviewed-by on v2 1/4
#4 lore raw

Re: [PATCH v2 1/4] drm/msm: Rework IFPC disable

In reply to Rob Clark
Quoted 260 lines · Rob Clark

Previously this was coupled to SYSPROF level, which made sense at the time. But there are other places that we need to inhibit IFPC. So decouple it from the sysprof refcount.

Now it has it's own refcount, so multiple threads can vote to inhibit IFPC safely. The renamed gpu->ifpc_update() simply applies the new state by setting or clearing an OOB request.

---
  drivers/gpu/drm/msm/adreno/a6xx_gmu.c | 32 +++++++++++++++------------
  drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 12 ++++------
  drivers/gpu/drm/msm/adreno/a6xx_gpu.h |  3 ++-
  drivers/gpu/drm/msm/adreno/a8xx_gpu.c |  6 +----
  drivers/gpu/drm/msm/msm_gpu.c         |  1 +
  drivers/gpu/drm/msm/msm_gpu.h         | 26 ++++++++++++++++++----
  drivers/gpu/drm/msm/msm_perfcntr.c    |  6 ++---
  drivers/gpu/drm/msm/msm_submitqueue.c | 10 +++++----
  8 files changed, 56 insertions(+), 40 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gmu.c +18 -14
index 662f09bec9d5..bba5468db42e 100644
@@ -2128,29 +2128,33 @@ static int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev,
  	return irq;
  }
 
-void a6xx_gmu_sysprof_setup(struct msm_gpu *gpu, bool force_on)
+void a6xx_gmu_ifpc_update_locked(struct msm_gpu *gpu)
  {
-	bool sysprof = msm_gpu_sysprof_no_ifpc(gpu) || force_on;
  	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
+	bool force_on = refcount_read(&gpu->ifpc_disable) > 1;
 
-	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
-	if (!pm_runtime_get_if_active(&gpu->pdev->dev))
-		return;
-
-	mutex_lock(&gmu->lock);
+	WARN_ON_ONCE(!mutex_is_locked(&gmu->lock));
 
-	/*
-	 * 'Perfcounter select' register values are lost during IFPC collapse. To avoid that,
-	 * use the currently unused perfcounter oob vote to block IFPC when sysprof is active
-	 */
-	if (sysprof && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	if (force_on && !test_and_set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
  		a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-	else if (!sysprof && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
+	else if (!force_on && test_and_clear_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status))
  		a6xx_gmu_clear_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
+}
 
-	mutex_unlock(&gmu->lock);
+void a6xx_gmu_ifpc_update(struct msm_gpu *gpu)
+{
+	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
+	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
+	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
+
+	/* Nothing to do if GPU is suspended. We will handle this during GMU resume */
+	if (pm_runtime_get_if_active(&gpu->pdev->dev) <= 0)
+		return;
+
+	scoped_guard (mutex, &gmu->lock)
+		a6xx_gmu_ifpc_update_locked(gpu);
 
  	pm_runtime_put(&gpu->pdev->dev);
  }
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +4 -8
index 09d76cdf059f..bf71129e33a9 100644
@@ -1565,11 +1565,7 @@ static int hw_init(struct msm_gpu *gpu)
  		a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_BOOT_SLUMBER);
  	}
 
-	if (!ret && msm_gpu_sysprof_no_ifpc(gpu)) {
-		ret = a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-		if (!ret)
-			set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status);
-	}
+	a6xx_gmu_ifpc_update_locked(gpu);
 
  	return ret;
  }
@@ -2851,7 +2847,7 @@ const struct adreno_gpu_funcs a6xx_gpu_funcs = {
  		.create_private_vm = a6xx_create_private_vm,
  		.get_rptr = a6xx_get_rptr,
  		.progress = a6xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
  		.perfcntr_configure = a6xx_perfcntr_configure,
  	},
  	.init = a6xx_gpu_init,
@@ -2922,7 +2918,7 @@ const struct adreno_gpu_funcs a7xx_gpu_funcs = {
  		.create_private_vm = a6xx_create_private_vm,
  		.get_rptr = a6xx_get_rptr,
  		.progress = a6xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
  		.perfcntr_configure = a6xx_perfcntr_configure,
  	},
  	.init = a6xx_gpu_init,
@@ -2953,7 +2949,7 @@ const struct adreno_gpu_funcs a8xx_gpu_funcs = {
  		.create_private_vm = a6xx_create_private_vm,
  		.get_rptr = a6xx_get_rptr,
  		.progress = a8xx_progress,
-		.sysprof_setup = a6xx_gmu_sysprof_setup,
+		.ifpc_update = a6xx_gmu_ifpc_update,
  		.perfcntr_configure = a6xx_perfcntr_configure,
  		.perfcntr_flush = a8xx_perfcntr_flush,
  	},
drivers/gpu/drm/msm/adreno/a6xx_gpu.h +2 -1
index 81d21acccea1..cb0ac27dacba 100644
@@ -298,7 +298,8 @@ void a6xx_gmu_clear_oob(struct a6xx_gmu *gmu, enum a6xx_gmu_oob_state state);
  int a6xx_gmu_init(struct a6xx_gpu *a6xx_gpu, struct device_node *node);
  int a6xx_gmu_wrapper_init(struct a6xx_gpu *a6xx_gpu, struct device_node *node);
  void a6xx_gmu_remove(struct a6xx_gpu *a6xx_gpu);
-void a6xx_gmu_sysprof_setup(struct msm_gpu *gpu, bool force_on);
+void a6xx_gmu_ifpc_update_locked(struct msm_gpu *gpu);
+void a6xx_gmu_ifpc_update(struct msm_gpu *gpu);
 
  void a6xx_preempt_init(struct msm_gpu *gpu);
  void a6xx_preempt_hw_init(struct msm_gpu *gpu);
drivers/gpu/drm/msm/adreno/a8xx_gpu.c +1 -5
index e7d04c865968..ecf15a8f68de 100644
@@ -846,11 +846,7 @@ static int hw_init(struct msm_gpu *gpu)
  	 */
  	a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_GPU_SET);
 
-	if (!ret && msm_gpu_sysprof_no_perfcntr_zap(gpu)) {
-		ret = a6xx_gmu_set_oob(gmu, GMU_OOB_PERFCOUNTER_SET);
-		if (!ret)
-			set_bit(GMU_STATUS_OOB_PERF_SET, &gmu->status);
-	}
+	a6xx_gmu_ifpc_update_locked(gpu);
 
  	return ret;
  }
drivers/gpu/drm/msm/msm_gpu.c +1 -0
index fb07803f5aba..37f9e93daa43 100644
@@ -1012,6 +1012,7 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  	gpu->nr_rings = nr_rings;
 
  	refcount_set(&gpu->sysprof_active, 1);
+	refcount_set(&gpu->ifpc_disable, 1);
 
  	mutex_init(&gpu->perfcntr_lock);
 
drivers/gpu/drm/msm/msm_gpu.h +22 -4
index d27d54bdb7a7..0334a0f604ac 100644
@@ -93,7 +93,7 @@ struct msm_gpu_funcs {
  	 * for cmdstream that is buffered in this FIFO upstream of the CP fw.
  	 */
  	bool (*progress)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
-	void (*sysprof_setup)(struct msm_gpu *gpu, bool force_on);
+	void (*ifpc_update)(struct msm_gpu *gpu);
 
  	/* Configure perfcntr SELect regs: */
  	void (*perfcntr_configure)(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
@@ -186,6 +186,14 @@ struct msm_gpu {
  	 */
  	refcount_t sysprof_active;
 
+	/**
+	 * ifpc_disable:
+	 *
+	 * Count of votes to disable IFPC (1-based, values above 1 mean IFPC
+	 * is disabled).
+	 */
+	refcount_t ifpc_disable;
+
  	/**
  	 * lock:
  	 *
@@ -375,10 +383,20 @@ msm_gpu_sysprof_no_perfcntr_zap(struct msm_gpu *gpu)
  		(gpu->perfcntrs && READ_ONCE(gpu->perfcntrs->stream));
  }
 
-static inline bool
-msm_gpu_sysprof_no_ifpc(struct msm_gpu *gpu)
+static inline void
+msm_gpu_disable_ifpc_get(struct msm_gpu *gpu)
+{
+	refcount_inc(&gpu->ifpc_disable);
+	if (gpu->funcs->ifpc_update)
+		gpu->funcs->ifpc_update(gpu);
+}
+
+static inline void
+msm_gpu_disable_ifpc_put(struct msm_gpu *gpu)
  {
-	return refcount_read(&gpu->sysprof_active) > 1;
+	refcount_dec(&gpu->ifpc_disable);
+	if (gpu->funcs->ifpc_update)
+		gpu->funcs->ifpc_update(gpu);
  }
 
  /*
drivers/gpu/drm/msm/msm_perfcntr.c +2 -4
index ce65b1160955..867dd476ed88 100644
@@ -259,8 +259,7 @@ sample_worker(struct kthread_work *work)
  	}
 
  	/* Inhibit IFPC while accessing registers: */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, true);
+	msm_gpu_disable_ifpc_get(gpu);
 
  	if (gpu->funcs->perfcntr_flush)
  		gpu->funcs->perfcntr_flush(gpu);
@@ -297,8 +296,7 @@ sample_worker(struct kthread_work *work)
  	}
 
  	/* Re-enable IFPC: */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, false);
+	msm_gpu_disable_ifpc_put(gpu);
 
  	smp_store_release(&stream->fifo.head, head);
  	wake_up_all(&stream->poll_wq);
drivers/gpu/drm/msm/msm_submitqueue.c +6 -0
index d307cdd17d90..8434a09319bc 100644
@@ -24,7 +24,10 @@ int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sy
  		pm_runtime_get_sync(&gpu->pdev->dev);
  		fallthrough;
  	case 1:
+		/* Disable zapping perfcntrs on context switch: */
  		refcount_inc(&gpu->sysprof_active);
+		/* Disable IFPC: */
+		msm_gpu_disable_ifpc_get(gpu);
  		fallthrough;
  	case 0:
  		break;
@@ -36,16 +39,15 @@ int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sy
  		pm_runtime_put_autosuspend(&gpu->pdev->dev);
  		fallthrough;
  	case 1:
+		/* Drop our vote for inhibiting IFPC: */
+		msm_gpu_disable_ifpc_put(gpu);
+		/* Drop our vote for disabling perfcntr zapping: */
  		refcount_dec(&gpu->sysprof_active);
  		fallthrough;
  	case 0:

drivers/gpu/drm/msm/msm_submitqueue.c int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sy · line 53

Quoted 10 lines
drivers/gpu/drm/msm/msm_submitqueue.c +0 -4
  		break;
  	}
 
-	/* Some gpu families require additional setup for sysprof */
-	if (gpu->funcs->sysprof_setup)
-		gpu->funcs->sysprof_setup(gpu, false);
-
  	ctx->sysprof = sysprof;
 
  	return 0;

Thanks this will help with my hangcheck series.

Best regards,

Signature · 1 line
Anna Maniscalco <[email protected]>
Reviewed-by on v2 2/4
#7 lore raw

Re: [PATCH v2 2/4] drm/msm: Add cleanup help for disabling IFPC

In reply to Rob Clark
Quoted 40 lines · Rob Clark

Simplify error path cleanup for IFPC-disable critical sections.

Note that this moves where we re-enable IFPC until after the wake_up_all(), but that is harmless.

---
  drivers/gpu/drm/msm/msm_gpu.h      | 4 ++++
  drivers/gpu/drm/msm/msm_perfcntr.c | 5 +----
  2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/msm/msm_gpu.h +4 -0
index 0334a0f604ac..e7912d2180a5 100644
@@ -399,6 +399,10 @@ msm_gpu_disable_ifpc_put(struct msm_gpu *gpu)
  		gpu->funcs->ifpc_update(gpu);
  }
 
+DEFINE_GUARD(ifpc_disable, struct msm_gpu *,
+	     msm_gpu_disable_ifpc_get(_T),
+	     msm_gpu_disable_ifpc_put(_T))
+
  /*
   * The number of priority levels provided by drm gpu scheduler.  The
   * DRM_SCHED_PRIORITY_KERNEL priority level is treated specially in some
drivers/gpu/drm/msm/msm_perfcntr.c +1 -1
index 867dd476ed88..995857273aca 100644
@@ -259,7 +259,7 @@ sample_worker(struct kthread_work *work)
  	}
 
  	/* Inhibit IFPC while accessing registers: */
-	msm_gpu_disable_ifpc_get(gpu);
+	guard(ifpc_disable)(gpu);
 
  	if (gpu->funcs->perfcntr_flush)
  		gpu->funcs->perfcntr_flush(gpu);

drivers/gpu/drm/msm/msm_perfcntr.c sample_worker(struct kthread_work *work) · line 300

Quoted 9 lines
drivers/gpu/drm/msm/msm_perfcntr.c +0 -3
@@ -295,9 +295,6 @@ sample_worker(struct kthread_work *work)
  		}
  	}
 
-	/* Re-enable IFPC: */
-	msm_gpu_disable_ifpc_put(gpu);
-
  	smp_store_release(&stream->fifo.head, head);
  	wake_up_all(&stream->poll_wq);
  }

Best regards,

Signature · 1 line
Anna Maniscalco <[email protected]>
Reviewed-by on v2 3/4
#13 lore raw

Re: [PATCH v2 3/4] drm/msm/debugfs: Use cleanup.h for gpu state dump

In reply to Rob Clark
Quoted 59 lines · Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
  drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
  1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +10 -15
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
  	struct drm_device *dev = inode->i_private;
  	struct msm_drm_private *priv = dev->dev_private;
  	struct msm_gpu *gpu = priv->gpu;
-	struct msm_gpu_show_priv *show_priv;
  	int ret;
 
  	if (!gpu || !gpu->funcs->gpu_state_get)
  		return -ENODEV;
 
-	show_priv = kmalloc_obj(*show_priv);
+	struct msm_gpu_show_priv *show_priv __free(kfree)
+		= kmalloc_obj(*show_priv);
  	if (!show_priv)
  		return -ENOMEM;
 
-	ret = mutex_lock_interruptible(&gpu->lock);
-	if (ret)
-		goto free_priv;
-
-	pm_runtime_get_sync(&gpu->pdev->dev);
-	msm_gpu_hw_init(gpu);
-	show_priv->state = gpu->funcs->gpu_state_get(gpu);
-	pm_runtime_put_sync(&gpu->pdev->dev);
-
-	mutex_unlock(&gpu->lock);
+	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+			msm_gpu_hw_init(gpu);
+			show_priv->state = gpu->funcs->gpu_state_get(gpu);
+		}
+	}
 
  	if (IS_ERR(show_priv->state)) {
-		ret = PTR_ERR(show_priv->state);
-		goto free_priv;
+		return PTR_ERR(show_priv->state);
  	}
 
  	show_priv->dev = dev;
 
  	ret = single_open(file, msm_gpu_show, show_priv);
  	if (ret)
-		goto free_priv;
+		return ret;
 

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 105

Quoted 10 lines
drivers/gpu/drm/msm/msm_debugfs.c +2 -4
-	return 0;
+	retain_and_null_ptr(show_priv);
 
-free_priv:
-	kfree(show_priv);
-	return ret;
+	return 0;
  }
 
  static const struct file_operations msm_gpu_fops = {

Best regards,

Signature · 1 line
Anna Maniscalco <[email protected]>
Reviewed-by on v2 4/4
#17 lore raw

Re: [PATCH v2 4/4] drm/msm: Inhibit IFPC for state snapshot

In reply to Rob Clark
Quoted 13 lines · Rob Clark

We need the GPU to stay awake for this.

---
  drivers/gpu/drm/msm/msm_debugfs.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/msm/msm_debugfs.c +0 -0
index aa59b28a3392..29209c23fcfa 100644

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 88

Quoted 8 lines
drivers/gpu/drm/msm/msm_debugfs.c +2 -0
@@ -81,6 +81,8 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
  		return -ENOMEM;
 
  	scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+		guard(ifpc_disable)(gpu);
+
  		scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
  			msm_gpu_hw_init(gpu);
  			show_priv->state = gpu->funcs->gpu_state_get(gpu);

Best regards,

Signature · 1 line
Anna Maniscalco <[email protected]>
#10 lore raw

Re: [PATCH v2 3/4] drm/msm/debugfs: Use cleanup.h for gpu state dump

In reply to Konrad Dybcio
Quoted 42 lines · Konrad Dybcio
Quoted 40 lines · Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
 drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +3 -12
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
      struct drm_device *dev = inode->i_private;
      struct msm_drm_private *priv = dev->dev_private;
      struct msm_gpu *gpu = priv->gpu;
-     struct msm_gpu_show_priv *show_priv;
      int ret;
 
      if (!gpu || !gpu->funcs->gpu_state_get)
              return -ENODEV;
 
-     show_priv = kmalloc_obj(*show_priv);
+     struct msm_gpu_show_priv *show_priv __free(kfree)
+             = kmalloc_obj(*show_priv);
      if (!show_priv)
              return -ENOMEM;
 
-     ret = mutex_lock_interruptible(&gpu->lock);
-     if (ret)
-             goto free_priv;
-
-     pm_runtime_get_sync(&gpu->pdev->dev);
-     msm_gpu_hw_init(gpu);
-     show_priv->state = gpu->funcs->gpu_state_get(gpu);
-     pm_runtime_put_sync(&gpu->pdev->dev);
-
-     mutex_unlock(&gpu->lock);
+     scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
Quoted 8 lines

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 87

Quoted 4 lines
drivers/gpu/drm/msm/msm_debugfs.c +4 -0
+             scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+                     msm_gpu_hw_init(gpu);
+                     show_priv->state = gpu->funcs->gpu_state_get(gpu);
+             }

My clanker tells me we can't grab the PM reference before a mutex, as the recovery mechanism depends on the rpm count being appropriate to let it drop it (and kill gx this way)

Yeah, but we also can't do it in the other order, or more specifically disabling ifpc needs to come before gpu lock or it can deadlock with recovery.. but ifpc disable needs to be after runpm get.

Re-working that ordering was the whole of introducing cleanup.h, to avoid the error path with mutex_lock_interruptible() after taking runpm

I guess if you are hammering on debugfs while crashing the gpu, maybe the first recovery fails, and then a subsequent one eventually succeeds.

BR,
-R

#11 lore raw

Re: [PATCH v2 3/4] drm/msm/debugfs: Use cleanup.h for gpu state dump

Quoted 54 lines · Rob Clark
Quoted 40 lines · Konrad Dybcio
Quoted 39 lines · Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
  drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
  1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +2 -12
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
       struct drm_device *dev = inode->i_private;
       struct msm_drm_private *priv = dev->dev_private;
       struct msm_gpu *gpu = priv->gpu;
-     struct msm_gpu_show_priv *show_priv;
       int ret;
 
       if (!gpu || !gpu->funcs->gpu_state_get)
               return -ENODEV;
 
-     show_priv = kmalloc_obj(*show_priv);
+     struct msm_gpu_show_priv *show_priv __free(kfree)
+             = kmalloc_obj(*show_priv);
       if (!show_priv)
               return -ENOMEM;
 
-     ret = mutex_lock_interruptible(&gpu->lock);
-     if (ret)
-             goto free_priv;
-
-     pm_runtime_get_sync(&gpu->pdev->dev);
-     msm_gpu_hw_init(gpu);
-     show_priv->state = gpu->funcs->gpu_state_get(gpu);
-     pm_runtime_put_sync(&gpu->pdev->dev);
-
-     mutex_unlock(&gpu->lock);
Quoted 8 lines

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 87

Quoted 5 lines
drivers/gpu/drm/msm/msm_debugfs.c +5 -0
+     scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+             scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+                     msm_gpu_hw_init(gpu);
+                     show_priv->state = gpu->funcs->gpu_state_get(gpu);
+             }

My clanker tells me we can't grab the PM reference before a mutex, as the recovery mechanism depends on the rpm count being appropriate to let it drop it (and kill gx this way)

Yeah, but we also can't do it in the other order, or more specifically disabling ifpc needs to come before gpu lock or it can deadlock with recovery.. but ifpc disable needs to be after runpm get.

Quoted 7 lines

Re-working that ordering was the whole of introducing cleanup.h, to avoid the error path with mutex_lock_interruptible() after taking runpm

I guess if you are hammering on debugfs while crashing the gpu, maybe the first recovery fails, and then a subsequent one eventually succeeds.

I guess it leads to a sort of deadlock scenario because the recovery path waits for power to collapse, though it timeouts after one second.

After that hangcheck will have to trigger again so I guess the delay introduced would be significant.

I really wish there was some better way of doing recovery that didn't rely on the counbter having a known value.

Quoted 3 lines

BR,
-R

Best regards,

Signature · 1 line
Anna Maniscalco <[email protected]>
#12 lore raw

Re: [PATCH v2 3/4] drm/msm/debugfs: Use cleanup.h for gpu state dump

Quoted 67 lines · Anna Maniscalco
Quoted 54 lines · Rob Clark
Quoted 40 lines · Konrad Dybcio
Quoted 39 lines · Rob Clark

We need to move the gpu lock around a bit for the next patch. Use scoped_guard/etc so we don't have to make the error path handling more complicated.

---
  drivers/gpu/drm/msm/msm_debugfs.c | 31 ++++++++++++-------------------
  1 file changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/msm/msm_debugfs.c +2 -12
index f12701e286ec..aa59b28a3392 100644
@@ -70,43 +70,36 @@ static int msm_gpu_open(struct inode *inode, struct file *file)
       struct drm_device *dev = inode->i_private;
       struct msm_drm_private *priv = dev->dev_private;
       struct msm_gpu *gpu = priv->gpu;
-     struct msm_gpu_show_priv *show_priv;
       int ret;
 
       if (!gpu || !gpu->funcs->gpu_state_get)
               return -ENODEV;
 
-     show_priv = kmalloc_obj(*show_priv);
+     struct msm_gpu_show_priv *show_priv __free(kfree)
+             = kmalloc_obj(*show_priv);
       if (!show_priv)
               return -ENOMEM;
 
-     ret = mutex_lock_interruptible(&gpu->lock);
-     if (ret)
-             goto free_priv;
-
-     pm_runtime_get_sync(&gpu->pdev->dev);
-     msm_gpu_hw_init(gpu);
-     show_priv->state = gpu->funcs->gpu_state_get(gpu);
-     pm_runtime_put_sync(&gpu->pdev->dev);
-
-     mutex_unlock(&gpu->lock);
Quoted 8 lines

drivers/gpu/drm/msm/msm_debugfs.c static int msm_gpu_open(struct inode *inode, struct file *file) · line 87

Quoted 5 lines
drivers/gpu/drm/msm/msm_debugfs.c +5 -0
+     scoped_guard (pm_runtime_active, &gpu->pdev->dev) {
+             scoped_cond_guard (mutex_intr, return -EINTR, &gpu->lock) {
+                     msm_gpu_hw_init(gpu);
+                     show_priv->state = gpu->funcs->gpu_state_get(gpu);
+             }

My clanker tells me we can't grab the PM reference before a mutex, as the recovery mechanism depends on the rpm count being appropriate to let it drop it (and kill gx this way)

Yeah, but we also can't do it in the other order, or more specifically disabling ifpc needs to come before gpu lock or it can deadlock with recovery.. but ifpc disable needs to be after runpm get.

Quoted 7 lines

Re-working that ordering was the whole of introducing cleanup.h, to avoid the error path with mutex_lock_interruptible() after taking runpm

I guess if you are hammering on debugfs while crashing the gpu, maybe the first recovery fails, and then a subsequent one eventually succeeds.

I guess it leads to a sort of deadlock scenario because the recovery path waits
for power to collapse, though it timeouts after one second.

Quoted 8 lines

After that hangcheck will have to trigger again so I guess the delay introduced
would be significant.

I really wish there was some better way of doing recovery that didn't rely on
the counbter having a known value.

Yeah, I don't love it either.. but also don't see a better option. My best answer is "this is debugfs, it's only best-effort"

BR,
-R