Skip to contentSkip to conversation

drm/nouveau: CRTC Runtime PM ref tracking fixes

PATCH Superseded Lyude Paul 3 participants 6 messages Aug 7, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lyude Paul

author maintainer
#0 lore raw

cover drm/nouveau: CRTC Runtime PM ref tracking fixes

Just some runtime PM fixes for some much less noticeable runtime PM ref tracking issues that I got reminded of when fixing some unrelated issues with nouveau.

Lyude Paul (2):
drm/nouveau/dispnv04: Grab/put runtime PM refs on DPMS on/off drm/nouveau/dispnv50: Fix runtime PM ref tracking for non-blocking
modesets

 drivers/gpu/drm/nouveau/dispnv04/crtc.c | 18 +++---------
 drivers/gpu/drm/nouveau/dispnv50/disp.c | 38 +++++++++++--------------
 drivers/gpu/drm/nouveau/nouveau_drv.h   |  3 --
 3 files changed, 21 insertions(+), 38 deletions(-)
Signature · 1 line
2.21.0
author maintainer
#1 lore raw

PATCH 1/2 drm/nouveau/dispnv04: Grab/put runtime PM refs on DPMS on/off

The code claims to grab a runtime PM ref when at least one CRTC is active, but that's not actually the case as we grab a runtime PM ref whenever a CRTC is enabled regardless of it's DPMS state. Meaning that we can end up keeping the GPU awake when there are no screens enabled, something we don't really want to do.

Note that we fixed this same issue for nv50 a while ago in:

commit e5d54f193572 ("drm/nouveau/drm/nouveau: Fix runtime PM leak in nv50_disp_atomic_commit()")

Since we're about to remove nouveau_drm->have_disp_power_ref in the next commit, let's also simplify the RPM code here while we're at it: grab a ref during a modeset, grab additional RPM refs for each CRTC enabled by said modeset, and drop an RPM ref for each CRTC disabled by said modeset. This allows us to keep the GPU awake whenever screens are turned on, without needing to use nouveau_drm->have_disp_power_ref.

---
 drivers/gpu/drm/nouveau/dispnv04/crtc.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/dispnv04/crtc.c +4 -14
index f22f01020625..08ad8e3b9cd2 100644
@@ -183,6 +183,10 @@ nv_crtc_dpms(struct drm_crtc *crtc, int mode)
 		return;
 
 	nv_crtc->last_dpms = mode;
+	if (mode == DRM_MODE_DPMS_ON)
+		pm_runtime_get_noresume(dev->dev);
+	else
+		pm_runtime_put_noidle(dev->dev);
 
 	if (nv_two_heads(dev))
 		NVSetOwner(dev, nv_crtc->index);
@@ -1045,7 +1049,6 @@ nouveau_crtc_set_config(struct drm_mode_set *set,
 
 	dev = set->crtc->dev;
 
-	/* get a pm reference here */
 	ret = pm_runtime_get_sync(dev->dev);
 	if (ret < 0 && ret != -EACCES)
 		return ret;
@@ -1061,19 +1064,6 @@ nouveau_crtc_set_config(struct drm_mode_set *set,
 	}
 
 	pm_runtime_mark_last_busy(dev->dev);
-	/* if we have active crtcs and we don't have a power ref,
-	   take the current one */
-	if (active && !drm->have_disp_power_ref) {
-		drm->have_disp_power_ref = true;
-		return ret;
-	}
-	/* if we have no active crtcs, then drop the power ref
-	   we got before */
-	if (!active && drm->have_disp_power_ref) {
-		pm_runtime_put_autosuspend(dev->dev);
-		drm->have_disp_power_ref = false;
-	}
-	/* drop the power reference we got coming in here */
 	pm_runtime_put_autosuspend(dev->dev);
 	return ret;
 }
Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 1/2] drm/nouveau/dispnv04: Grab/put runtime PM refs on DPMS on/off

Quoted 28 lines · Lyude Paul

The code claims to grab a runtime PM ref when at least one CRTC is active, but that's not actually the case as we grab a runtime PM ref whenever a CRTC is enabled regardless of it's DPMS state. Meaning that we can end up keeping the GPU awake when there are no screens enabled, something we don't really want to do.

Note that we fixed this same issue for nv50 a while ago in:

commit e5d54f193572 ("drm/nouveau/drm/nouveau: Fix runtime PM leak in nv50_disp_atomic_commit()")

Since we're about to remove nouveau_drm->have_disp_power_ref in the next commit, let's also simplify the RPM code here while we're at it: grab a ref during a modeset, grab additional RPM refs for each CRTC enabled by said modeset, and drop an RPM ref for each CRTC disabled by said modeset. This allows us to keep the GPU awake whenever screens are turned on, without needing to use nouveau_drm->have_disp_power_ref.

---
 drivers/gpu/drm/nouveau/dispnv04/crtc.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/dispnv04/crtc.c +0 -0
index f22f01020625..08ad8e3b9cd2 100644

drivers/gpu/drm/nouveau/dispnv04/crtc.c nv_crtc_dpms(struct drm_crtc *crtc, int mode) · line 189

Quoted 7 lines
drivers/gpu/drm/nouveau/dispnv04/crtc.c +4 -0
@@ -183,6 +183,10 @@ nv_crtc_dpms(struct drm_crtc *crtc, int mode)
 		return;
 
 	nv_crtc->last_dpms = mode;
+	if (mode == DRM_MODE_DPMS_ON)
+		pm_runtime_get_noresume(dev->dev);
+	else
+		pm_runtime_put_noidle(dev->dev);

it's after we filter out duplicate operations, so that part looks good. But not all of nouveau's legacy helper crtc callbacks go throuh ->dpms I
think: nv_crtc_disable doesn't, and crtc helpers use that in preference over ->dpms in some cases.

I think the only way to actually hit that path is if you switch an active connector from an active CRTC to an inactive one. This implicitly disables the crtc (well, should, nv_crtc_disable doesn't seem to shut down hw), and I think would leak your runtime PM reference here. At least temporarily.

No idea how to best fix that. Aside from "use atomic" :-)

Cheers, Daniel

Quoted 34 lines
drivers/gpu/drm/nouveau/dispnv04/crtc.c +0 -14
 	if (nv_two_heads(dev))
 		NVSetOwner(dev, nv_crtc->index);
@@ -1045,7 +1049,6 @@ nouveau_crtc_set_config(struct drm_mode_set *set,
 
 	dev = set->crtc->dev;
 
-	/* get a pm reference here */
 	ret = pm_runtime_get_sync(dev->dev);
 	if (ret < 0 && ret != -EACCES)
 		return ret;
@@ -1061,19 +1064,6 @@ nouveau_crtc_set_config(struct drm_mode_set *set,
 	}
 
 	pm_runtime_mark_last_busy(dev->dev);
-	/* if we have active crtcs and we don't have a power ref,
-	   take the current one */
-	if (active && !drm->have_disp_power_ref) {
-		drm->have_disp_power_ref = true;
-		return ret;
-	}
-	/* if we have no active crtcs, then drop the power ref
-	   we got before */
-	if (!active && drm->have_disp_power_ref) {
-		pm_runtime_put_autosuspend(dev->dev);
-		drm->have_disp_power_ref = false;
-	}
-	/* drop the power reference we got coming in here */
 	pm_runtime_put_autosuspend(dev->dev);
 	return ret;
 }
Signature · 1 line
2.21.0
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#3 lore raw

Re: [PATCH 1/2] drm/nouveau/dispnv04: Grab/put runtime PM refs on DPMS on/off

Quoted 43 lines · Daniel Vetter
Quoted 28 lines · Lyude Paul

The code claims to grab a runtime PM ref when at least one CRTC is active, but that's not actually the case as we grab a runtime PM ref whenever a CRTC is enabled regardless of it's DPMS state. Meaning that we can end up keeping the GPU awake when there are no screens enabled, something we don't really want to do.

Note that we fixed this same issue for nv50 a while ago in:

commit e5d54f193572 ("drm/nouveau/drm/nouveau: Fix runtime PM leak in nv50_disp_atomic_commit()")

Since we're about to remove nouveau_drm->have_disp_power_ref in the next commit, let's also simplify the RPM code here while we're at it: grab a ref during a modeset, grab additional RPM refs for each CRTC enabled by said modeset, and drop an RPM ref for each CRTC disabled by said modeset. This allows us to keep the GPU awake whenever screens are turned on, without needing to use nouveau_drm->have_disp_power_ref.

---
 drivers/gpu/drm/nouveau/dispnv04/crtc.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/dispnv04/crtc.c +0 -0
index f22f01020625..08ad8e3b9cd2 100644

drivers/gpu/drm/nouveau/dispnv04/crtc.c nv_crtc_dpms(struct drm_crtc *crtc, int mode) · line 189

Quoted 7 lines
drivers/gpu/drm/nouveau/dispnv04/crtc.c +4 -0
@@ -183,6 +183,10 @@ nv_crtc_dpms(struct drm_crtc *crtc, int mode)
              return;
 
      nv_crtc->last_dpms = mode;
+     if (mode == DRM_MODE_DPMS_ON)
+             pm_runtime_get_noresume(dev->dev);
+     else
+             pm_runtime_put_noidle(dev->dev);

it's after we filter out duplicate operations, so that part looks good. But not all of nouveau's legacy helper crtc callbacks go throuh ->dpms I
think: nv_crtc_disable doesn't, and crtc helpers use that in preference over ->dpms in some cases.

Quoted 6 lines

I think the only way to actually hit that path is if you switch an active connector from an active CRTC to an inactive one. This implicitly disables the crtc (well, should, nv_crtc_disable doesn't seem to shut down hw), and I think would leak your runtime PM reference here. At least temporarily.

No idea how to best fix that. Aside from "use atomic" :-)

Not sure if this is relevant to the discussion at hand, but I'd like to point out that dispnv04 is for pre-nv50 things, which definitely didn't support any kind of ACPI-based runtime suspend.

-ilia

author maintainer
#4 lore raw

Re: [PATCH 1/2] drm/nouveau/dispnv04: Grab/put runtime PM refs on DPMS on/off

Quoted 49 lines · Ilia Mirkin
Quoted 48 lines · Daniel Vetter
Quoted 29 lines · Lyude Paul

The code claims to grab a runtime PM ref when at least one CRTC is active, but that's not actually the case as we grab a runtime PM ref whenever a CRTC is enabled regardless of it's DPMS state. Meaning that we can end up keeping the GPU awake when there are no screens enabled, something we don't really want to do.

Note that we fixed this same issue for nv50 a while ago in:

commit e5d54f193572 ("drm/nouveau/drm/nouveau: Fix runtime PM leak in nv50_disp_atomic_commit()")

Since we're about to remove nouveau_drm->have_disp_power_ref in the next commit, let's also simplify the RPM code here while we're at it: grab a ref during a modeset, grab additional RPM refs for each CRTC enabled by said modeset, and drop an RPM ref for each CRTC disabled by said modeset. This allows us to keep the GPU awake whenever screens are turned on, without needing to use nouveau_drm->have_disp_power_ref.

---
 drivers/gpu/drm/nouveau/dispnv04/crtc.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/dispnv04/crtc.c +0 -0
index f22f01020625..08ad8e3b9cd2 100644

drivers/gpu/drm/nouveau/dispnv04/crtc.c nv_crtc_dpms(struct drm_crtc *crtc, int mode) · line 189

Quoted 7 lines
drivers/gpu/drm/nouveau/dispnv04/crtc.c +4 -0
@@ -183,6 +183,10 @@ nv_crtc_dpms(struct drm_crtc *crtc, int mode)
              return;
 
      nv_crtc->last_dpms = mode;
+     if (mode == DRM_MODE_DPMS_ON)
+             pm_runtime_get_noresume(dev->dev);
+     else
+             pm_runtime_put_noidle(dev->dev);

it's after we filter out duplicate operations, so that part looks good. But not all of nouveau's legacy helper crtc callbacks go throuh ->dpms I
think: nv_crtc_disable doesn't, and crtc helpers use that in preference over ->dpms in some cases.

I think the only way to actually hit that path is if you switch an active connector from an active CRTC to an inactive one. This implicitly disables the crtc (well, should, nv_crtc_disable doesn't seem to shut down hw), and I think would leak your runtime PM reference here. At least temporarily.

Quoted 5 lines
Quoted 1 line

No idea how to best fix that. Aside from "use atomic" :-)

Not sure if this is relevant to the discussion at hand, but I'd like to point out that dispnv04 is for pre-nv50 things, which definitely didn't support any kind of ACPI-based runtime suspend.

I thought it was really suspicious that such an old chipset had any kind of runtime PM, but didn't question the code lol. I guess a more appropriate patch would be to just remove runtime PM support entirely for pre-nv50. Will respin soon and do this.

Quoted 2 lines

-ilia

author maintainer
#5 lore raw

PATCH 2/2 drm/nouveau/dispnv50: Fix runtime PM ref tracking for non-blocking modesets

In reply to Lyude Paul

This is something that got noticed a while ago back when I was fixing a large number of runtime PM related issues in nouveau, but never got fixed:

https://patchwork.freedesktop.org/series/46815/#rev7

It's not safe to iterate the entire list of CRTCs in nv50_disp_atomic_commit(), as we could be doing a non-blocking modeset on one CRTC in parallel with one or more other CRTCs. Likewise, this means it's also not safe to do so in order to track runtime PM state. While this code is certainly wrong, so far the only issues I've seen this cause in the wild is the occasional PM ref unbalance after an atomic check failure + module reloading (since the PCI device will outlive nouveau in such scenarios).

So, do this far more elegantly: grab a runtime PM ref across the modeset and commit tail, then grab/put references for each CRTC enable/disable. This also ends up being much simpler then the previous broken solution we had.

Finally, since we've removed all it's users: get rid of nouveau_drm->have_disp_power_ref.

---
 drivers/gpu/drm/nouveau/dispnv50/disp.c | 38 +++++++++++--------------
 drivers/gpu/drm/nouveau/nouveau_drv.h   |  3 --
 2 files changed, 17 insertions(+), 24 deletions(-)
drivers/gpu/drm/nouveau/dispnv50/disp.c +17 -21
index 126703816794..659e6fa645cb 100644
@@ -1826,8 +1826,11 @@ nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
 
 		NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
 			  asyh->clr.mask, asyh->set.mask);
-		if (old_crtc_state->active && !new_crtc_state->active)
+
+		if (old_crtc_state->active && !new_crtc_state->active) {
+			pm_runtime_put_noidle(dev->dev);
 			drm_crtc_vblank_off(crtc);
+		}
 
 		if (asyh->clr.mask) {
 			nv50_head_flush_clr(head, asyh, atom->flush_disable);
@@ -1913,8 +1916,10 @@ nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
 		}
 
 		if (new_crtc_state->active) {
-			if (!old_crtc_state->active)
+			if (!old_crtc_state->active) {
 				drm_crtc_vblank_on(crtc);
+				pm_runtime_get_noresume(dev->dev);
+			}
 			if (new_crtc_state->event)
 				drm_crtc_vblank_get(crtc);
 		}
@@ -1979,6 +1984,10 @@ nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
 	drm_atomic_helper_cleanup_planes(dev, state);
 	drm_atomic_helper_commit_cleanup_done(state);
 	drm_atomic_state_put(state);
+
+	/* Drop the RPM ref we got from nv50_disp_atomic_commit() */
+	pm_runtime_mark_last_busy(dev->dev);
+	pm_runtime_put_autosuspend(dev->dev);
 }
 
 static void
@@ -1993,11 +2002,8 @@ static int
 nv50_disp_atomic_commit(struct drm_device *dev,
 			struct drm_atomic_state *state, bool nonblock)
 {
-	struct nouveau_drm *drm = nouveau_drm(dev);
 	struct drm_plane_state *new_plane_state;
 	struct drm_plane *plane;
-	struct drm_crtc *crtc;
-	bool active = false;
 	int ret, i;
 
 	ret = pm_runtime_get_sync(dev->dev);
@@ -2034,27 +2040,17 @@ nv50_disp_atomic_commit(struct drm_device *dev,
 
 	drm_atomic_state_get(state);
 
+	/*
+	 * Grab another RPM ref for the commit tail, which will release the
+	 * ref when it's finished
+	 */
+	pm_runtime_get_noresume(dev->dev);
+
 	if (nonblock)
 		queue_work(system_unbound_wq, &state->commit_work);
 	else
 		nv50_disp_atomic_commit_tail(state);
 
-	drm_for_each_crtc(crtc, dev) {
-		if (crtc->state->active) {
-			if (!drm->have_disp_power_ref) {
-				drm->have_disp_power_ref = true;
-				return 0;
-			}
-			active = true;
-			break;
-		}
-	}
-
-	if (!active && drm->have_disp_power_ref) {
-		pm_runtime_put_autosuspend(dev->dev);
-		drm->have_disp_power_ref = false;
-	}
-
 err_cleanup:
 	if (ret)
 		drm_atomic_helper_cleanup_planes(dev, state);
drivers/gpu/drm/nouveau/nouveau_drv.h +0 -3
index aae035816383..411352dd5390 100644
@@ -204,9 +204,6 @@ struct nouveau_drm {
 	/* led management */
 	struct nouveau_led *led;
 
-	/* display power reference */
-	bool have_disp_power_ref;
-
 	struct dev_pm_domain vga_pm_domain;
 
 	struct nouveau_svm *svm;
Signature · 1 line
2.21.0