drm/msm/dp: hold one runtime PM reference per plugged state
PATCH v2 1/1 drm/msm/dp: hold one runtime PM reference per plugged state
The plug handler takes a runtime PM reference every time it runs and sets dp->plugged; the unplug handler drops a single reference when dp->plugged is set. msm_dp_bridge_detect() also takes a reference and marks the sink plugged, and keeps that reference as well -- on every detect while the sink stays connected. On top of that, in the current tree the plug handler runs more than once per plug:drm_bridge_connector_detect() calls .detect() and then .hpd_notify(), and pmic_glink altmode reports an IRQ_HPD as "connected" too. Every extra reference is leaked on unplug.
On a Lenovo TB323FU (SM8850, DP over the USB-C port via pmic_glink altmode) the DP controller's usage count reached 28-34 after a few plugs (rpm_usage tracepoint), so the controller never runtime-suspended after the display was unplugged. It in turn kept the MDSS core GDSC on while all displays were off.
Make the plugged state own exactly one reference: the plug handler only takes it when the sink was not plugged yet, and detect() returns its own reference when the plugged state already holds one, or drops both when it finds the sink gone.
With this the count stays at 1-3 while a display is connected, the controller suspends 3-4 s after unplug, MDSS suspends and its GDSC powers off on DPMS off, and replugging (quickly or after 20 s) still brings the picture back. Only tested on this device.
---
Changes in v2:
- Drop the plugged state's reference once, after the if/else, instead of in both branches [Val]
- Drop the comments
- Link to v1: https://lore.kernel.org/all/[email protected]/
drivers/gpu/drm/msm/dp/dp_display.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpu/drm/msm/dp/dp_display.c +11 -4
@@ -412,10 +412,12 @@ static int msm_dp_hpd_plug_handle(struct msm_dp_display_private *dp)guard (mutex)(&dp->plugged_lock); - ret =pm_runtime_resume_and_get (&pdev->dev); -if (ret) { -DRM_ERROR ("failed to pm_runtime_resume\n" ); -return ret; +if (!dp->plugged) { + ret =pm_runtime_resume_and_get (&pdev->dev); +if (ret) { +DRM_ERROR ("failed to pm_runtime_resume\n" ); +return ret; + } }msm_dp_aux_enable_xfers (dp->aux, true);
@@ -897,6 +899,7 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,u8 dpcd[DP_RECEIVER_CAP_SIZE ];struct drm_dp_desc desc;bool phy_deinit; +bool was_plugged;int ret; dp =to_dp_bridge (bridge)->msm_dp_display;
@@ -910,6 +913,7 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,return status; } + was_plugged = priv->plugged; phy_deinit =msm_dp_display_host_phy_init (priv);msm_dp_aux_enable_xfers (priv->aux, true);
@@ -962,6 +966,9 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,pm_runtime_put_sync (&dp->pdev->dev); } +if (was_plugged) +pm_runtime_put_sync (&dp->pdev->dev); +return status; }
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/msm/dp: unplug teardown bypassed when detect() clears priv->plugged --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/msm/dp: unplug teardown bypassed when detect() clears priv->plugged --
commit 6677860d1d5222553e73900478155ec25c55e88b
Author: Joonhoe Kim <[email protected]>
drm/msm/dp: hold one runtime PM reference per plugged state
This commit adjusts PM runtime references in the DisplayPort driver to hold exactly one reference per plugged state. It takes a reference on plug and drops it when detect() notices the sink is gone or when the unplug handler runs.
Quoted 4 lines
drivers/gpu/drm/msm/dp/dp_display.c +0
-0
[ ... ]
Quoted 8 lines
drivers/gpu/drm/msm/dp/dp_display.c +1
-0@@ -910,6 +913,7 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,return status; } + was_plugged = priv->plugged; phy_deinit =msm_dp_display_host_phy_init (priv);msm_dp_aux_enable_xfers (priv->aux, true);
[ ... ]
drivers/gpu/drm/msm/dp/dp_display.c enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge, · line 973
Quoted 9 lines
drivers/gpu/drm/msm/dp/dp_display.c +3
-0@@ -962,6 +966,9 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,pm_runtime_put_sync (&dp->pdev->dev); } +if (was_plugged) +pm_runtime_put_sync (&dp->pdev->dev); +return status; }
[Severity: High]
Does this cement a hardware teardown bypass on unplug?
When a physical unplug occurs, msm_dp_bridge_detect() fails to read DPCD caps and sets priv->plugged = false. By dropping the PM runtime reference here using the newly added was_plugged flag, it completes the state transition early.
The DRM core subsequently calls .hpd_notify() which invokes msm_dp_hpd_unplug_handle(). Because the plugged flag was already cleared in detect(), the unplug handler immediately early-returns:
msm_dp_hpd_unplug_handle() {
guard(mutex)(&dp->plugged_lock);
if (!dp->plugged)
return 0;
// Teardown code is skipped
...
}This entirely bypasses drm_edid_connector_update(..., NULL), drm_dp_set_subconnector_property(), and msm_dp_display_handle_plugged_change(..., false). As a result, the EDID is left stale, and the audio subsystem is never notified of the unplug (unless an active audio stream was running).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 2 lines · On Wed, 07 Oct 2026 16:12:16 +0000, [email protected] wrote:
When a physical unplug occurs,
msm_dp_bridge_detect()fails to read DPCD caps and setspriv->plugged= false.
[...]
Quoted 2 lines
Because the plugged flag was already cleared in
detect(), the unplug handler immediately early-returns:
Both of these are in the tree without this patch: detect() clears plugged on a failed DPCD read, and the unplug handler returns early when plugged is clear. The patch does not change either; it only drops the reference that detect() used to leak in that case.
The controller can now runtime-suspend in that case, but not under an active display or audio stream: the enable path holds its own reference until atomic_post_disable(), which first notifies audio and clears audio_enabled and power_on, the flags the audio callbacks check.
Whether detect() should leave the unplug teardown to the unplug handler is a separate question, and I would rather not change it in this fix.
Thanks,
Joonhoe Kim
Quoted 50 lines · Joonhoe Kim
The plug handler takes a runtime PM reference every time it runs and sets
dp->plugged; the unplug handler drops a single reference whendp->pluggedis set.msm_dp_bridge_detect()also takes a reference and marks the sink plugged, and keeps that reference as well -- on every detect while the sink stays connected. On top of that, in the current tree the plug handler runs more than once per plug:drm_bridge_connector_detect()calls .detect() and then .hpd_notify(), andpmic_glinkaltmode reports anIRQ_HPDas "connected" too. Every extra reference is leaked on unplug.On a Lenovo TB323FU (SM8850, DP over the USB-C port via
pmic_glinkaltmode) the DP controller's usage count reached 28-34 after a few plugs (rpm_usagetracepoint), so the controller never runtime-suspended after the display was unplugged. It in turn kept the MDSS core GDSC on while all displays were off.Make the plugged state own exactly one reference: the plug handler only takes it when the sink was not plugged yet, and
detect()returns its own reference when the plugged state already holds one, or drops both when it finds the sink gone.With this the count stays at 1-3 while a display is connected, the controller suspends 3-4 s after unplug, MDSS suspends and its GDSC powers off on DPMS off, and replugging (quickly or after 20 s) still brings the picture back. Only tested on this device.
Fixes 3ea2d1c3d154 ("drm/msm/dp: turn link_ready into plugged")Assisted-by LLMSigned-off-by Joonhoe Kim <[email protected]>---
Changes in v2:
- Drop the plugged state's reference once, after the if/else, instead of in both branches [Val]
- Drop the comments
- Link to v1: https://lore.kernel.org/all/[email protected]/drivers/gpu/drm/msm/dp/dp_display.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/gpu/drm/msm/dp/dp_display.c +0
-3@@ -412,10 +412,12 @@ static int msm_dp_hpd_plug_handle(struct msm_dp_display_private *dp)guard (mutex)(&dp->plugged_lock); - ret =pm_runtime_resume_and_get (&pdev->dev); -if (ret) { -DRM_ERROR ("failed to pm_runtime_resume\n" );
drivers/gpu/drm/msm/dp/dp_display.c static int msm_dp_hpd_plug_handle(struct msm_dp_display_private *dp) · line 423
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_display.c +6
-1-return ret; +if (!dp->plugged) { + ret =pm_runtime_resume_and_get (&pdev->dev); +if (ret) { +DRM_ERROR ("failed to pm_runtime_resume\n" ); +return ret; + } }msm_dp_aux_enable_xfers (dp->aux, true);
This part (msm_dp_hpd_plug_handle) is now also handled by the IRQ_HPD series:
Quoted 17 lines
drivers/gpu/drm/msm/dp/dp_display.c +2
-0@@ -897,6 +899,7 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,u8 dpcd[DP_RECEIVER_CAP_SIZE ];struct drm_dp_desc desc;bool phy_deinit; +bool was_plugged;int ret; dp =to_dp_bridge (bridge)->msm_dp_display;@@ -910,6 +913,7 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,return status; } + was_plugged = priv->plugged; phy_deinit =msm_dp_display_host_phy_init (priv);msm_dp_aux_enable_xfers (priv->aux, true);
drivers/gpu/drm/msm/dp/dp_display.c enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge, · line 970
Quoted 5 lines
drivers/gpu/drm/msm/dp/dp_display.c +2
-0@@ -962,6 +966,9 @@ enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,pm_runtime_put_sync (&dp->pdev->dev); } +if (was_plugged) +pm_runtime_put_sync (&dp->pdev->dev);
Thinking about this again, this flow of the reference(s) seems confusing.
If I understand it correctly now,
* there's a long-term reference associated with priv->plugged;
* and this function needs the device to not be suspended because it's doing transfers to check if there's something plugged into the port.
With the current flow (fixed by this v2 patch to not leak, or at least leak less):
* we always take +1 reference;
* if we "find a plug" (!was_plugged && priv->plugged): no drop, our reference "becomes" the long-term one;
* if we "find an unplug" (was_plugged && !priv->plugged): we drop our own reference, as well as the long-term one;
* if we "find no change while plugged" (was_plugged && priv->plugged): we only drop our own reference;
* if we "find no change while unplugged" (!was_plugged && !priv->plugged): no drop, is this intended or a leak still? Not clear who needs the reference to persist in this case?
**If** that last case is actually **not** intended to keep the reference, I think it would be clearer to, instead:
* use a guard (probably PM_RUNTIME_ACQUIRE_IF_ENABLED to keep the error check) for this function's own reference;
* and use a plain get an extra time when setting priv->plugged = true..?
drivers/gpu/drm/msm/dp/dp_display.c enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge, · line 974
Quoted 4 lines
drivers/gpu/drm/msm/dp/dp_display.c +1
-0+return status; }
Thanks,
~val
Quoted 2 lines · Val Packett
This part (
msm_dp_hpd_plug_handle) is now also handled by theIRQ_HPDseries:
Yes. v3 is on top of that series and keeps only the detect() part.
Quoted 2 lines
* if we "find no change while unplugged" (!was_plugged && !priv->plugged): no drop, is this intended or a leak still?
It does not leak: the existing put in the "if (!priv->plugged)" block drops the function's own reference in that case, and only the second put depends on was_plugged. Agreed that this is hard to see from the code, though.
Quoted 3 lines
* use a guard (probably
PM_RUNTIME_ACQUIRE_IF_ENABLEDto keep the error check) for this function's own reference;
* and use a plain get an extra time when settingpriv->plugged= true..?
Done in v3: the plugged state takes its reference with pm_runtime_get_noresume() when detect() sets priv->plugged and drops it when detect() clears it:
https://lore.kernel.org/all/[email protected]/
Thanks,
Joonhoe Kim