Skip to contentSkip to conversation

drm/msm: fix the error handling of the KMS init

PATCH Superseded Dmitry Baryshkov 3 participants 29 messages Oct 23 – Oct 26

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover drm/msm: fix the error handling of the KMS init

When setting up the KMS fails, msm_drm_kms_init() cleans up by calling the ->destroy() callback of the kms driver. That makes ->destroy() responsible for tearing down a KMS in any state, from "not initialised at all" to "fully initialised", and the drivers get it wrong in several ways:

- dpu_kms_hw_init() and mdp5_init() already clean up after their own failures, and ->destroy() then finalises the global state object a second time;

- mdp4_destroy() dereferences mdp4_kms->dev, which isn't set yet when mdp_kms_init() fails;

- msm_kms_init() leaves a NULL workqueue behind when it fails to allocate it, which msm_kms_destroy() passes to destroy_workqueue();

- msm_drm_kms_init() hands a half initialised KMS to the full teardown of msm_drm_kms_uninit(), and the snapshot init ignores the failure to create its worker;

- the IOMMU fault handler and the DPU interrupts can queue a display snapshot on its worker after it has been destroyed, or against a KMS which is still being set up;

- the MDP5 driver appends its hardware blocks to the arrays in mdp5_kms on every bind and overflows them on the next bind;

- the DPU never cleans up its self-refresh helpers, whose delayed work then commits to a KMS which has been torn down.

Most of these were pointed out by Sashiko while reviewing the "drm/msm:
fix SMMU fault dumps" series, which has been carrying the first fixes. Rather than teaching ->destroy() about ever more partial states, this series switches the KMS init to the usual kernel convention: every init function undoes its own steps when it fails, and ->destroy() only ever tears down a KMS which was initialised successfully. This reverses the approach of commit 93c125e4ea98 ("drm/msm: don't tear down KMS twice when KMS init fails"), which moved all of the cleanup into ->destroy().

To keep every step safe and bisectable, the drivers are converted one at a time: a temporary flag tells msm_drm_kms_init() that a driver cleans up its own failures, and both the flag and ->hw_init(), which gets folded into the drivers' kms_init(), are removed once all the drivers have been converted. The last patches fix the teardown of the DPU self-refresh helpers and follow up on the new rules: they unwind the DPU hardware setup step by step, disable the MDP4 vdd regulator on teardown, and drop the runtime PM flags which ->destroy() no longer needs.

Tested on DB820c (MSM8996) with both the MDP5 and the DPU drivers, with failures injected into the msm_kms_init(), the MDP5 kms_init(), the DPU hardware setup and after a successful kms_init(): each of them now fails the bind cleanly, without warnings, and a later bind attempt succeeds. Unbinding and rebinding the driver works as well, except that with MDP5 the HDMI output does not come back after a rebind (its extp clock fails to enable), which is not affected by this series. MDP4 is build-tested only.

---
Changes in v3:
- Rebased on msm/msm-next, fixing the conflict.
- Link to v2: https://patch.msgid.link/20261023-msm-kms-destroy-fixes-v2-0-83727c122de7@oss.qualcomm.com

Changes in v2:
- Added patches which register the KMS fault handler late, reset the
MDP5 hardware block counts and clean up the DPU self-refresh helpers
- The unwind of dpu_kms_hw_init() now clears the drmm-allocated hw block
pointers, dpu_kms outlives them into the next bind (Sashiko)
- kms->vm, kms->wq and the timer workers are cleared on teardown too
- Moved the MDP5 counts fix to the front, merged the error exits of
msm_drm_init(), clarified the fault handler and self-refresh commit
messages
- Link to v1: https://patch.msgid.link/20261003-msm-kms-destroy-fixes-v1-0-e062b7dae77f@oss.qualcomm.com

---
Dmitry Baryshkov (14):
drm/msm: fail the snapshot init when its worker cannot be created drm/msm/mdp5: forget the hardware blocks of a previous bind drm/msm: unwind msm_drm_kms_init() on failure
drm/msm: order the display snapshot sources around its worker drm/msm: clean up after a failed msm_kms_init() drm/msm: let the kms drivers clean up a failed kms_init() drm/msm/mdp5: unwind a failed mdp5_kms_init() drm/msm/mdp4: unwind a failed mdp4_kms_init() drm/msm/dpu: don't tear down a failed hw_init twice drm/msm: tear down only a successfully initialised KMS drm/msm/dpu: clean up the self-refresh helpers on teardown drm/msm/dpu: unwind a failed dpu_kms_hw_init() step by step drm/msm/mdp4: disable the vdd regulator on teardown drm/msm: drop the runtime PM flags from the KMS teardown

 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c      | 73 ++++++++++++++++------------
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h      |  1 -
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c     | 68 ++++++++++++++++----------
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h     |  2 -
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c     | 52 ++++++++++++--------
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h     |  2 -
 drivers/gpu/drm/msm/disp/msm_disp_snapshot.c | 12 ++++-
 drivers/gpu/drm/msm/msm_atomic.c             |  1 +
 drivers/gpu/drm/msm/msm_drv.c                | 29 +++++++----
 drivers/gpu/drm/msm/msm_kms.c                | 55 +++++++++++----------
 drivers/gpu/drm/msm/msm_kms.h                | 19 +++++---
 11 files changed, 190 insertions(+), 124 deletions(-)

---
base-commit: d024361c631b3f020a26c07d683e5103404b0fbf
change-id: 20260930-msm-kms-destroy-fixes-0f04b1d39b83

Best regards,

Signature · 2 lines
With best wishes
Dmitry
#1 lore raw

PATCH v3 1/14 drm/msm: fail the snapshot init when its worker cannot be created

msm_disp_snapshot_init() only logs a failure of kthread_run_worker() and returns success, leaving the error pointer in kms->dump_worker. Both of its users then dereference it: msm_disp_snapshot_state() queues the dump work on it on the first display error, and msm_disp_snapshot_destroy() only checks the pointer for NULL before passing it to kthread_destroy_worker().

Clear the pointer and return the error instead, so that the KMS init fails rather than leaving a snapshot facility which crashes when used.

Fixes 98659487b845 ("drm/msm: add support to take dpu snapshot")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/msm_disp_snapshot.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/disp/msm_disp_snapshot.c +11 -1
index d99771684728..d1b10656e41a 100644
@@ -98,6 +98,7 @@ int msm_disp_snapshot_init(struct drm_device *drm_dev)
 {
 	struct msm_drm_private *priv;
 	struct msm_kms *kms;
+	int ret;
 
 	if (!drm_dev) {
 		DRM_ERROR("invalid params\n");
@@ -110,12 +111,21 @@ int msm_disp_snapshot_init(struct drm_device *drm_dev)
 	mutex_init(&kms->dump_mutex);
 
 	kms->dump_worker = kthread_run_worker(0, "%s", "disp_snapshot");
-	if (IS_ERR(kms->dump_worker))
+	if (IS_ERR(kms->dump_worker)) {
+		ret = PTR_ERR(kms->dump_worker);
 		DRM_ERROR("failed to create disp state task\n");
+		goto err_destroy_mutex;
+	}
 
 	kthread_init_work(&kms->dump_work, _msm_disp_snapshot_work);
 
 	return 0;
+
+err_destroy_mutex:
+	kms->dump_worker = NULL;
+	mutex_destroy(&kms->dump_mutex);
+
+	return ret;
 }
 
 void msm_disp_snapshot_destroy(struct drm_device *drm_dev)
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v3 01/14] drm/msm: fail the snapshot init when its worker cannot be created

Quoted 37 lines · Dmitry Baryshkov

msm_disp_snapshot_init() only logs a failure of kthread_run_worker() and returns success, leaving the error pointer in kms->dump_worker. Both of its users then dereference it: msm_disp_snapshot_state() queues the dump work on it on the first display error, and msm_disp_snapshot_destroy() only checks the pointer for NULL before passing it to kthread_destroy_worker().

Clear the pointer and return the error instead, so that the KMS init fails rather than leaving a snapshot facility which crashes when used.

Fixes 98659487b845 ("drm/msm: add support to take dpu snapshot")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/msm_disp_snapshot.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/disp/msm_disp_snapshot.c +3 -1
index d99771684728..d1b10656e41a 100644
@@ -98,6 +98,7 @@ int msm_disp_snapshot_init(struct drm_device *drm_dev)
 {
 	struct msm_drm_private *priv;
 	struct msm_kms *kms;
+	int ret;
 
 	if (!drm_dev) {
 		DRM_ERROR("invalid params\n");
@@ -110,12 +111,21 @@ int msm_disp_snapshot_init(struct drm_device *drm_dev)
 	mutex_init(&kms->dump_mutex);
 
 	kms->dump_worker = kthread_run_worker(0, "%s", "disp_snapshot");
-	if (IS_ERR(kms->dump_worker))
+	if (IS_ERR(kms->dump_worker)) {
+		ret = PTR_ERR(kms->dump_worker);
 		DRM_ERROR("failed to create disp state task\n");

drivers/gpu/drm/msm/disp/msm_disp_snapshot.c int msm_disp_snapshot_init(struct drm_device *drm_dev) · line 126

Quoted 10 lines
drivers/gpu/drm/msm/disp/msm_disp_snapshot.c +6 -0
+		goto err_destroy_mutex;
+	}
 
 	kthread_init_work(&kms->dump_work, _msm_disp_snapshot_work);
 
 	return 0;
+
+err_destroy_mutex:
+	kms->dump_worker = NULL;
+	mutex_destroy(&kms->dump_mutex);

LLM:

The error path calls mutex_destroy(&kms->dump_mutex), but msm_disp_snapshot_destroy() runs later on the same failure path and destroys the mutex again. That's harmless in practice (mutex_destroy() on an unlocked mutex is benign, and the debug checks don't fire), but it's a redundant double destroy. A simpler version would drop the mutex handling from the error path and let the destroy function own it

Konrad

#3 lore raw

PATCH v3 2/14 drm/msm/mdp5: forget the hardware blocks of a previous bind

In reply to Dmitry Baryshkov

Binding the MDP5 driver again, after an unbind or after a failed bind, corrupts struct mdp5_kms. On DB820c, binding it after a failed bind oopses in hwmixer_init():

  Unable to handle kernel paging request at virtual address ffff0004d411f9d8
  pc : mdp5_kms_init+0x264/0x7f8

Since mdp5_kms is allocated in mdp5_dev_probe(), it outlives the bind, but hwpipe_init(), hwmixer_init() and interface_init() still run on each bind and append to the hwpipes[], hwmixers[] and intfs[] arrays without resetting their counts. The second bind writes past the end of hwpipes[] into the fields which follow it.

Reset the counts when the hardware blocks are torn down. The blocks themselves are devm-allocated on the MDP5 device on each bind and are only freed when it is unbound; that leak is left for a separate change.

Fixes 1f50db2f3e1e ("drm/msm/mdp5: move resource allocation to the _probe function")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c +4 -0
index 3934cd060b27..085c23820f46 100644
@@ -570,6 +570,10 @@ static void mdp5_destroy(struct mdp5_kms *mdp5_kms)
 		pm_runtime_disable(&mdp5_kms->pdev->dev);
 
 	drm_atomic_private_obj_fini(&mdp5_kms->glob_state);
+
+	mdp5_kms->num_hwpipes = 0;
+	mdp5_kms->num_hwmixers = 0;
+	mdp5_kms->num_intfs = 0;
 }
 
 static int construct_pipes(struct mdp5_kms *mdp5_kms, int cnt,
Signature · 1 line
2.47.3
Reviewed-by on v3 2/14
#4 lore raw

Re: [PATCH v3 02/14] drm/msm/mdp5: forget the hardware blocks of a previous bind

Quoted 13 lines · Dmitry Baryshkov

Binding the MDP5 driver again, after an unbind or after a failed bind, corrupts struct mdp5_kms. On DB820c, binding it after a failed bind oopses in hwmixer_init():

  Unable to handle kernel paging request at virtual address ffff0004d411f9d8
  pc : mdp5_kms_init+0x264/0x7f8

Since mdp5_kms is allocated in mdp5_dev_probe(), it outlives the bind, but hwpipe_init(), hwmixer_init() and interface_init() still run on each bind and append to the hwpipes[], hwmixers[] and intfs[] arrays without resetting their counts. The second bind writes past the end of hwpipes[] into the fields which follow it.

Quoted 8 lines

Reset the counts when the hardware blocks are torn down. The blocks themselves are devm-allocated on the MDP5 device on each bind and are only freed when it is unbound; that leak is left for a separate change.

Fixes 1f50db2f3e1e ("drm/msm/mdp5: move resource allocation to the _probe function")
Assisted-by LLM

---

Konrad

#5 lore raw

PATCH v3 3/14 drm/msm: unwind msm_drm_kms_init() on failure

In reply to Dmitry Baryshkov

msm_drm_kms_init() leaves behind whatever it has already set up when it fails and lets the caller sort it out: msm_drm_init() jumps to msm_drm_uninit(), which runs msm_drm_kms_uninit() for every device which has a kms. That is the teardown of a fully initialised KMS -- it flushes kms->wq, calls ->irq_uninstall() and frees the IRQ, none of which exist yet when the failure happened early.

The kms driver's own init is the first step which can fail, and since commit a409b78fcdf7 ("drm/msm: move wq handling to KMS code") the workqueue is created by msm_kms_init() rather than by msm_drm_init(), so a failure there leaves a NULL kms->wq for flush_workqueue().

Undo the steps which have completed instead, and let msm_drm_init() unwind its own error paths rather than calling the full teardown.

Fixes a409b78fcdf7 ("drm/msm: move wq handling to KMS code")
Assisted-by LLM
---
 drivers/gpu/drm/msm/msm_drv.c | 29 ++++++++++++++++++++---------
 drivers/gpu/drm/msm/msm_kms.c | 36 ++++++++++++++++++++++++------------
 2 files changed, 44 insertions(+), 21 deletions(-)
drivers/gpu/drm/msm/msm_drv.c +20 -9
index 5e8f3ce26b32..1e89fdaf0eea 100644
@@ -230,35 +230,46 @@ static int msm_drm_init(struct device *dev, const struct drm_driver *drv,
 
 	ret = msm_gem_shrinker_init(ddev);
 	if (ret)
-		goto err_msm_uninit;
+		goto err_unbind;
 
 	msm_gem_thp_init(ddev);
 
 	if (priv->kms_init) {
 		ret = msm_drm_kms_init(dev, drv);
 		if (ret)
-			goto err_msm_uninit;
+			goto err_shrinker_cleanup;
 	}
 
 	ret = drm_dev_register(ddev, 0);
 	if (ret)
-		goto err_msm_uninit;
+		goto err_kms_uninit;
 
 	ret = msm_debugfs_late_init(ddev);
 	if (ret)
-		goto err_msm_uninit;
+		goto err_unregister;
 
 	if (priv->kms_init)
 		msm_drm_kms_post_init(dev);
 
 	return 0;
 
-err_msm_uninit:
-	msm_drm_uninit(dev, gpu_ops);
-
-	return ret;
-
+err_unregister:
+	drm_dev_unregister(ddev);
+	if (priv->kms_init)
+		msm_drm_kms_unregister(dev);
+	msm_rd_debugfs_cleanup(priv);
+err_kms_uninit:
+	if (priv->kms_init)
+		msm_drm_kms_uninit(dev);
+err_shrinker_cleanup:
+	msm_gem_shrinker_cleanup(ddev);
+err_unbind:
+	if (gpu_ops)
+		gpu_ops->unbind(dev, dev, NULL);
+	else
+		component_unbind_all(dev, ddev);
 err_put_dev:
+	ddev->dev_private = NULL;
 	drm_dev_put(ddev);
 
 	return ret;
drivers/gpu/drm/msm/msm_kms.c +24 -12
index e5d0ea629448..f3e39c3907a9 100644
@@ -225,13 +225,23 @@ void msm_drm_kms_unregister(struct device *dev)
 	drm_atomic_helper_shutdown(ddev);
 }
 
+static void msm_drm_kms_destroy_event_threads(struct msm_kms *kms)
+{
+	int i;
+
+	for (i = 0; i < MAX_CRTCS; i++) {
+		if (kms->event_thread[i].worker)
+			kthread_destroy_worker(kms->event_thread[i].worker);
+		kms->event_thread[i].worker = NULL;
+	}
+}
+
 void msm_drm_kms_uninit(struct device *dev)
 {
 	struct platform_device *pdev = to_platform_device(dev);
 	struct msm_drm_private *priv = platform_get_drvdata(pdev);
 	struct drm_device *ddev = priv->dev;
 	struct msm_kms *kms = priv->kms;
-	int i;
 
 	BUG_ON(!kms);
 
@@ -242,11 +252,7 @@ void msm_drm_kms_uninit(struct device *dev)
 
 	flush_workqueue(kms->wq);
 
-	/* clean up event worker threads */
-	for (i = 0; i < MAX_CRTCS; i++) {
-		if (kms->event_thread[i].worker)
-			kthread_destroy_worker(kms->event_thread[i].worker);
-	}
+	msm_drm_kms_destroy_event_threads(kms);
 
 	drm_kms_helper_poll_fini(ddev);
 
@@ -282,7 +288,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	ret = priv->kms_init(ddev);
 	if (ret) {
 		DRM_DEV_ERROR(dev, "failed to load kms\n");
-		goto err_msm_uninit;
+		goto err_destroy_kms;
 	}
 
 	/* Enable normalization of plane zpos */
@@ -295,7 +301,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	ret = kms->funcs->hw_init(kms);
 	if (ret) {
 		DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
-		goto err_msm_uninit;
+		goto err_destroy_kms;
 	}
 
 	drm_helper_move_panel_connectors_to_head(ddev);
@@ -311,7 +317,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 			ret = PTR_ERR(ev_thread->worker);
 			DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n");
 			ev_thread->worker = NULL;
-			goto err_msm_uninit;
+			goto err_destroy_event_threads;
 		}
 
 		sched_set_fifo(ev_thread->worker->task);
@@ -320,7 +326,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	ret = drm_vblank_init(ddev, ddev->mode_config.num_crtc);
 	if (ret < 0) {
 		DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
-		goto err_msm_uninit;
+		goto err_destroy_event_threads;
 	}
 
 	pm_runtime_get_sync(dev);
@@ -328,14 +334,20 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	pm_runtime_put_sync(dev);
 	if (ret < 0) {
 		DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
-		goto err_msm_uninit;
+		goto err_destroy_event_threads;
 	}
 
 	drm_mode_config_reset(ddev);
 
 	return 0;
 
-err_msm_uninit:
+err_destroy_event_threads:
+	msm_drm_kms_destroy_event_threads(kms);
+err_destroy_kms:
+	msm_disp_snapshot_destroy(ddev);
+	if (kms->funcs)
+		kms->funcs->destroy(kms);
+
 	return ret;
 }
 
Signature · 1 line
2.47.3
Reviewed-by on v3 3/14
#6 lore raw

Re: [PATCH v3 03/14] drm/msm: unwind msm_drm_kms_init() on failure

Quoted 7 lines · Dmitry Baryshkov

msm_drm_kms_init() leaves behind whatever it has already set up when it fails and lets the caller sort it out: msm_drm_init() jumps to msm_drm_uninit(), which runs msm_drm_kms_uninit() for every device which has a kms. That is the teardown of a fully initialised KMS -- it flushes kms->wq, calls ->irq_uninstall() and frees the IRQ, none of which exist yet when the failure happened early.

Quoted 7 lines

The kms driver's own init is the first step which can fail, and since commit a409b78fcdf7 ("drm/msm: move wq handling to KMS code") the workqueue is created by msm_kms_init() rather than by msm_drm_init(), so a failure there leaves a NULL kms->wq for flush_workqueue().

Undo the steps which have completed instead, and let msm_drm_init() unwind its own error paths rather than calling the full teardown.

I *really* want us to get impl Drop soon...

Konrad

#7 lore raw

PATCH v3 4/14 drm/msm: order the display snapshot sources around its worker

In reply to Dmitry Baryshkov

The display snapshot work is queued on kms->dump_worker by the IOMMU fault handler of the KMS and by the DPU interrupt handlers, but neither of them follows the lifetime of the worker:

- the fault handler is registered by msm_kms_init_vm() in the middle of kms_init() and is never unregistered, so a fault during kms_init(), or in the error path of msm_drm_kms_init(), dumps a KMS which is still being set up or already being torn down, and a fault after msm_drm_kms_uninit() queues the work on a destroyed worker;

- msm_drm_kms_uninit() destroys the worker before it uninstalls the interrupt handlers, so an underrun or a timeout in between queues the work on a destroyed worker as well.

Start the sources only once the KMS is fully set up and stop them before the worker goes away: register the fault handler as the last step of msm_drm_kms_init(), unregister it first thing in msm_drm_kms_uninit(), and destroy the worker after the interrupts have been uninstalled but before the KMS itself, so that the dumps still pending run against an intact KMS.

A fault during kms_init() therefore no longer produces a dump; the KMS is not complete enough to describe one at that point. The handler is cleared with the kms argument left in place, so that a fault handler already running still gets a valid one.

Fixes 2ec5b3dc18ba ("drm/msm: add disp snapshot points across dpu driver")
Fixes eabba31a839a ("drm/msm: register a fault handler for display mmu faults")
Assisted-by LLM
---
 drivers/gpu/drm/msm/msm_kms.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/gpu/drm/msm/msm_kms.c +6 -6
index f3e39c3907a9..a94d2888a21e 100644
@@ -182,8 +182,6 @@ struct drm_gpuvm *msm_kms_init_vm(struct drm_device *dev, struct device *mdss_de
 	struct drm_gpuvm *vm;
 	struct msm_mmu *mmu;
 	struct device *mdp_dev = dev->dev;
-	struct msm_drm_private *priv = dev->dev_private;
-	struct msm_kms *kms = priv->kms;
 	struct device *iommu_dev;
 
 	/*
@@ -211,8 +209,6 @@ struct drm_gpuvm *msm_kms_init_vm(struct drm_device *dev, struct device *mdss_de
 		return vm;
 	}
 
-	msm_mmu_set_fault_handler(to_msm_vm(vm)->mmu, kms, msm_kms_fault_handler);
-
 	return vm;
 }
 
@@ -245,6 +241,8 @@ void msm_drm_kms_uninit(struct device *dev)
 
 	BUG_ON(!kms);
 
+	msm_mmu_set_fault_handler(to_msm_vm(kms->vm)->mmu, kms, NULL);
+
 	/* We must cancel and cleanup any pending vblank enable/disable
 	 * work before msm_irq_uninstall() to avoid work re-enabling an
 	 * irq after uninstall has disabled it.
@@ -256,12 +254,12 @@ void msm_drm_kms_uninit(struct device *dev)
 
 	drm_kms_helper_poll_fini(ddev);
 
-	msm_disp_snapshot_destroy(ddev);
-
 	pm_runtime_get_sync(dev);
 	msm_irq_uninstall(ddev);
 	pm_runtime_put_sync(dev);
 
+	msm_disp_snapshot_destroy(ddev);
+
 	if (kms && kms->funcs)
 		kms->funcs->destroy(kms);
 }
@@ -339,6 +337,8 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 
 	drm_mode_config_reset(ddev);
 
+	msm_mmu_set_fault_handler(to_msm_vm(kms->vm)->mmu, kms, msm_kms_fault_handler);
+
 	return 0;
 
 err_destroy_event_threads:
Signature · 1 line
2.47.3
#8 lore raw

Re: [PATCH v3 04/14] drm/msm: order the display snapshot sources around its worker

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/msm: unsynchronized fault handler clear in msm_drm_kms_uninit() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/msm: unsynchronized fault handler clear in msm_drm_kms_uninit() --

commit a95d27af7e4b3b82df2fddca01a3fd982e834f6d
Author: Dmitry Baryshkov <[email protected]>

drm/msm: order the display snapshot sources around its worker

This commit reorders the registration and teardown of the display IOMMU fault handler and the display snapshot worker in msm_drm_kms_init() and msm_drm_kms_uninit() so that snapshot sources are started after KMS setup and stopped before the snapshot worker is destroyed.

Quoted 4 lines
drivers/gpu/drm/msm/msm_kms.c +0 -0
index f3e39c3907a93..a94d2888a21e5 100644

[ ... ]

Quoted 10 lines
drivers/gpu/drm/msm/msm_kms.c +2 -0
@@ -245,6 +241,8 @@ void msm_drm_kms_uninit(struct device *dev)
 
 	BUG_ON(!kms);
 
+	msm_mmu_set_fault_handler(to_msm_vm(kms->vm)->mmu, kms, NULL);
+
 	/* We must cancel and cleanup any pending vblank enable/disable
 	 * work before msm_irq_uninstall() to avoid work re-enabling an
 	 * irq after uninstall has disabled it.

drivers/gpu/drm/msm/msm_kms.c void msm_drm_kms_uninit(struct device *dev) · line 261

Quoted 10 lines
drivers/gpu/drm/msm/msm_kms.c +1 -2
@@ -256,12 +254,12 @@ void msm_drm_kms_uninit(struct device *dev)
 
 	drm_kms_helper_poll_fini(ddev);
 
-	msm_disp_snapshot_destroy(ddev);
-
 	pm_runtime_get_sync(dev);
 	msm_irq_uninstall(ddev);
 	pm_runtime_put_sync(dev);
 
+	msm_disp_snapshot_destroy(ddev);

[Severity: High]
Can clearing the fault handler in msm_drm_kms_uninit() still race with an in-flight display IOMMU fault and cause a NULL function pointer call or a use-after-free on kms->dump_worker?

In drivers/gpu/drm/msm/msm_mmu.h, msm_mmu_set_fault_handler() performs plain unlocked stores:

static inline void msm_mmu_set_fault_handler(struct msm_mmu *mmu, void *arg,
int (*handler)(void *arg, unsigned long iova, int flags, void *data))

{
	mmu->arg = arg;
	mmu->handler = handler;
}

And in drivers/gpu/drm/msm/msm_iommu.c, msm_disp_fault_handler() checks and calls iommu->base.handler without locking or READ_ONCE():

static int msm_disp_fault_handler(struct iommu_domain *domain, struct device *dev,
unsigned long iova, int flags, void *arg)

{
	struct msm_iommu *iommu = arg;

	if (iommu->base.handler)
		return iommu->base.handler(iommu->base.arg, iova, flags, NULL);

	return -ENOSYS;
}

First, without READ_ONCE() / WRITE_ONCE() or a lock, can the compiler reload iommu->base.handler after the NULL check in msm_disp_fault_handler() while msm_drm_kms_uninit() clears it, calling NULL?

Second, if a display SMMU context fault (for example, via the threaded IRQ handler qcom_smmu_context_fault() -> report_iommu_fault() -> msm_disp_fault_handler() -> msm_kms_fault_handler()) is already running when msm_drm_kms_uninit() sets mmu->handler to NULL, nothing between msm_mmu_set_fault_handler() and msm_disp_snapshot_destroy() waits for that in-flight fault handler to complete (msm_irq_uninstall() only frees kms->irq, while the SMMU domain and its fault IRQ are only torn down later in kms->funcs->destroy(kms)).

If msm_kms_fault_handler() is preempted or running on another CPU, couldn't it call msm_disp_snapshot_state() -> kthread_queue_work(kms->dump_worker, &kms->dump_work) concurrently with or after msm_disp_snapshot_destroy() calls kthread_destroy_worker(kms->dump_worker)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261024-msm-kms-destroy-fixes-v3-0-66a30120a5ea@oss.qualcomm.com?part=4
#9 lore raw

PATCH v3 5/14 drm/msm: clean up after a failed msm_kms_init()

In reply to Dmitry Baryshkov

msm_kms_init() returns early when it fails to allocate its workqueue or to create one of the pending timer workers, leaving behind whatever it has already set up. The kms drivers' ->destroy() callbacks still run in that case and reach msm_kms_destroy(), which passes a workqueue that was never allocated straight to destroy_workqueue().

Tear down the timers and the workqueue created so far when msm_kms_init() fails. Until every kms driver has stopped relying on ->destroy() to clean up a failed init, let msm_kms_destroy() skip what has already been destroyed.

Fixes a409b78fcdf7 ("drm/msm: move wq handling to KMS code")
Assisted-by LLM
---
 drivers/gpu/drm/msm/msm_atomic.c |  1 +
 drivers/gpu/drm/msm/msm_kms.h    | 20 +++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
drivers/gpu/drm/msm/msm_atomic.c +1 -0
index a8babf1dbe0d..c26b0c7acdfa 100644
@@ -134,6 +134,7 @@ void msm_atomic_destroy_pending_timer(struct msm_pending_timer *timer)
 {
 	if (timer->worker)
 		kthread_destroy_worker(timer->worker);
+	timer->worker = NULL;
 }
 
 static bool can_do_async(struct drm_atomic_commit *state,
drivers/gpu/drm/msm/msm_kms.h +15 -5
index f25b31e502d2..ee98393b9855 100644
@@ -175,7 +175,8 @@ struct msm_kms {
 static inline int msm_kms_init(struct msm_kms *kms,
 		const struct msm_kms_funcs *funcs)
 {
-	unsigned i, ret;
+	unsigned int i;
+	int ret;
 
 	for (i = 0; i < ARRAY_SIZE(kms->commit_lock); i++)
 		mutex_init(&kms->commit_lock[i]);
@@ -188,12 +189,19 @@ static inline int msm_kms_init(struct msm_kms *kms,
 
 	for (i = 0; i < ARRAY_SIZE(kms->pending_timers); i++) {
 		ret = msm_atomic_init_pending_timer(&kms->pending_timers[i], kms, i);
-		if (ret) {
-			return ret;
-		}
+		if (ret)
+			goto err_destroy_timers;
 	}
 
 	return 0;
+
+err_destroy_timers:
+	while (i--)
+		msm_atomic_destroy_pending_timer(&kms->pending_timers[i]);
+	destroy_workqueue(kms->wq);
+	kms->wq = NULL;
+
+	return ret;
 }
 
 static inline void msm_kms_destroy(struct msm_kms *kms)
@@ -203,7 +211,9 @@ static inline void msm_kms_destroy(struct msm_kms *kms)
 	for (i = 0; i < ARRAY_SIZE(kms->pending_timers); i++)
 		msm_atomic_destroy_pending_timer(&kms->pending_timers[i]);
 
-	destroy_workqueue(kms->wq);
+	/* the kms drivers' ->destroy() also runs after a failed init */
+	if (kms->wq)
+		destroy_workqueue(kms->wq);
 }
 
 #define for_each_crtc_mask(dev, crtc, crtc_mask) \
Signature · 1 line
2.47.3
Reviewed-by on v3 5/14
#10 lore raw

Re: [PATCH v3 05/14] drm/msm: clean up after a failed msm_kms_init()

Quoted 11 lines · Dmitry Baryshkov

msm_kms_init() returns early when it fails to allocate its workqueue or to create one of the pending timer workers, leaving behind whatever it has already set up. The kms drivers' ->destroy() callbacks still run in that case and reach msm_kms_destroy(), which passes a workqueue that was never allocated straight to destroy_workqueue().

Tear down the timers and the workqueue created so far when msm_kms_init() fails. Until every kms driver has stopped relying on ->destroy() to clean up a failed init, let msm_kms_destroy() skip what has already been destroyed.

Quoted 4 lines
Fixes a409b78fcdf7 ("drm/msm: move wq handling to KMS code")
Assisted-by LLM

---

Konrad

#11 lore raw

PATCH v3 6/14 drm/msm: let the kms drivers clean up a failed kms_init()

In reply to Dmitry Baryshkov

A failed priv->kms_init() or ->hw_init() is cleaned up by the ->destroy() callback, which makes ->destroy() responsible for tearing down a KMS in any state between "not initialised at all" and "fully initialised". The kernel convention is the opposite: a function which fails undoes its own steps, and the teardown only ever sees a fully set up object.

Let the kms drivers switch to that convention one at a time: skip ->destroy() after a failed kms_init() of a driver which sets init_unwinds, and let the drivers fold their hardware setup into kms_init() and drop ->hw_init(). Set up the mode config before calling kms_init(), so that it is already in place for the hardware setup.

While at it, tear the snapshot worker down after the KMS, in the reverse order of its setup. Nothing queues a snapshot until msm_drm_kms_init() has succeeded, so no dump can be pending when the KMS goes away.

The flag and the optional ->hw_init() go away once all the drivers have been converted.

Assisted-by LLM
---
 drivers/gpu/drm/msm/msm_kms.c | 23 ++++++++++++++---------
 drivers/gpu/drm/msm/msm_kms.h |  3 +++
 2 files changed, 17 insertions(+), 9 deletions(-)
drivers/gpu/drm/msm/msm_kms.c +14 -9
index a94d2888a21e..e4d61935a6df 100644
@@ -283,12 +283,6 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 		return ret;
 	}
 
-	ret = priv->kms_init(ddev);
-	if (ret) {
-		DRM_DEV_ERROR(dev, "failed to load kms\n");
-		goto err_destroy_kms;
-	}
-
 	/* Enable normalization of plane zpos */
 	ddev->mode_config.normalize_zpos = true;
 
@@ -296,12 +290,22 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	ddev->mode_config.helper_private = &mode_config_helper_funcs;
 
 	kms->dev = ddev;
-	ret = kms->funcs->hw_init(kms);
+	ret = priv->kms_init(ddev);
 	if (ret) {
-		DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
+		DRM_DEV_ERROR(dev, "failed to load kms\n");
+		if (kms->init_unwinds)
+			goto err_destroy_snapshot;
 		goto err_destroy_kms;
 	}
 
+	if (kms->funcs->hw_init) {
+		ret = kms->funcs->hw_init(kms);
+		if (ret) {
+			DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
+			goto err_destroy_kms;
+		}
+	}
+
 	drm_helper_move_panel_connectors_to_head(ddev);
 
 	drm_for_each_crtc(crtc, ddev) {
@@ -344,9 +348,10 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 err_destroy_event_threads:
 	msm_drm_kms_destroy_event_threads(kms);
 err_destroy_kms:
-	msm_disp_snapshot_destroy(ddev);
 	if (kms->funcs)
 		kms->funcs->destroy(kms);
+err_destroy_snapshot:
+	msm_disp_snapshot_destroy(ddev);
 
 	return ret;
 }
drivers/gpu/drm/msm/msm_kms.h +3 -0
index ee98393b9855..2f097e23e2e9 100644
@@ -149,6 +149,9 @@ struct msm_kms {
 	int irq;
 	bool irq_requested;
 
+	/* set by the kms drivers whose kms_init() undoes its own failures */
+	bool init_unwinds;
+
 	/* rate limit the snapshot capture to once per attach */
 	atomic_t fault_snapshot_capture;
 
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH v3 06/14] drm/msm: let the kms drivers clean up a failed kms_init()

Quoted 12 lines · Dmitry Baryshkov

A failed priv->kms_init() or ->hw_init() is cleaned up by the ->destroy() callback, which makes ->destroy() responsible for tearing down a KMS in any state between "not initialised at all" and "fully initialised". The kernel convention is the opposite: a function which fails undoes its own steps, and the teardown only ever sees a fully set up object.

Let the kms drivers switch to that convention one at a time: skip ->destroy() after a failed kms_init() of a driver which sets init_unwinds, and let the drivers fold their hardware setup into kms_init() and drop ->hw_init(). Set up the mode config before calling kms_init(), so that it is already in place for the hardware setup.

Quoted 3 lines

While at it, tear the snapshot worker down after the KMS, in the reverse order of its setup. Nothing queues a snapshot until msm_drm_kms_init() has succeeded, so no dump can be pending when the KMS goes away.

LLM:

msm_kms_init_vm() arms the IOMMU fault handler inside kms_init, and that handler queues dump_work, so a fault during init can queue a dump.

The patch then tears down the KMS before the snapshot worker. kthread_destroy_worker() flushes queued work, so the pending dump would run against a destroyed KMS. The original order, snapshot first, avoids that. Unbind (msm_drm_kms_uninit()) keeps the original order, so the two paths now disagree.

Konrad:

I don't think it's entirely unreasonable to get a iommu error halfway through init

Konrad

#13 lore raw

PATCH v3 7/14 drm/msm/mdp5: unwind a failed mdp5_kms_init()

In reply to Dmitry Baryshkov

mdp5_kms_init() returns straight away when one of its later steps fails, leaving behind the kms, the runtime PM reference taken around the address space setup and the address space itself, and lets the ->destroy() callback, which the caller runs afterwards, clean up. When mdp5_init() fails, however, it has already run mdp5_destroy() itself, and ->destroy() runs it a second time: the global state object is finalised twice.

Undo the steps which have completed, in the reverse order, drop the runtime PM reference on the error path as well, and tell the caller not to run ->destroy() after a failed init. Fold the hardware setup, which can't fail, into mdp5_kms_init().

Fixes 8d58ef346f30 ("drm/msm/mdp5: Add global state as a private atomic object")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c | 45 ++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c +28 -17
index 085c23820f46..30af8ef51e4c 100644
@@ -19,9 +19,8 @@
 #include "msm_mmu.h"
 #include "mdp5_kms.h"
 
-static int mdp5_hw_init(struct msm_kms *kms)
+static void mdp5_hw_init(struct mdp5_kms *mdp5_kms)
 {
-	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
 	struct device *dev = &mdp5_kms->pdev->dev;
 	unsigned long flags;
 
@@ -58,8 +57,6 @@ static int mdp5_hw_init(struct msm_kms *kms)
 	mdp5_ctlm_hw_reset(mdp5_kms->ctlm);
 
 	pm_runtime_put_sync(dev);
-
-	return 0;
 }
 
 /* Global/shared object state funcs */
@@ -198,24 +195,26 @@ static void mdp5_complete_commit(struct msm_kms *kms, unsigned crtc_mask)
 
 static void mdp5_destroy(struct mdp5_kms *mdp5_kms);
 
-static void mdp5_kms_destroy(struct msm_kms *kms)
+static void mdp5_kms_destroy_vm(struct msm_kms *kms)
 {
-	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
+	struct msm_mmu *mmu = to_msm_vm(kms->vm)->mmu;
 
-	if (kms->vm) {
-		struct msm_mmu *mmu = to_msm_vm(kms->vm)->mmu;
+	mmu->funcs->detach(mmu);
+	drm_gpuvm_put(kms->vm);
+	kms->vm = NULL;
+}
 
-		mmu->funcs->detach(mmu);
-		drm_gpuvm_put(kms->vm);
-	}
+static void mdp5_kms_destroy(struct msm_kms *kms)
+{
+	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
 
+	mdp5_kms_destroy_vm(kms);
 	mdp_kms_destroy(&mdp5_kms->base);
 	mdp5_destroy(mdp5_kms);
 }
 
 static const struct mdp_kms_funcs kms_funcs = {
 	.base = {
-		.hw_init         = mdp5_hw_init,
 		.irq_preinstall  = mdp5_irq_preinstall,
 		.irq_postinstall = mdp5_irq_postinstall,
 		.irq_uninstall   = mdp5_irq_uninstall,
@@ -506,6 +505,8 @@ static int mdp5_kms_init(struct drm_device *dev)
 	struct drm_gpuvm *vm;
 	int i, ret;
 
+	kms->init_unwinds = true;
+
 	ret = mdp5_init(to_platform_device(dev->dev), dev);
 	if (ret)
 		return ret;
@@ -517,7 +518,7 @@ static int mdp5_kms_init(struct drm_device *dev)
 	ret = mdp_kms_init(&mdp5_kms->base, &kms_funcs);
 	if (ret) {
 		DRM_DEV_ERROR(&pdev->dev, "failed to init kms\n");
-		return ret;
+		goto err_mdp5_destroy;
 	}
 
 	config = mdp5_cfg_get_config(mdp5_kms->cfg);
@@ -538,19 +539,18 @@ static int mdp5_kms_init(struct drm_device *dev)
 	mdelay(16);
 
 	vm = msm_kms_init_vm(mdp5_kms->dev, pdev->dev.parent);
+	pm_runtime_put_sync(&pdev->dev);
 	if (IS_ERR(vm)) {
 		ret = PTR_ERR(vm);
-		return ret;
+		goto err_kms_destroy;
 	}
 
 	kms->vm = vm;
 
-	pm_runtime_put_sync(&pdev->dev);
-
 	ret = modeset_init(mdp5_kms);
 	if (ret) {
 		DRM_DEV_ERROR(&pdev->dev, "modeset_init failed: %d\n", ret);
-		return ret;
+		goto err_destroy_vm;
 	}
 
 	dev->mode_config.min_width = 0;
@@ -561,7 +561,18 @@ static int mdp5_kms_init(struct drm_device *dev)
 	dev->max_vblank_count = 0; /* max_vblank_count is set on each CRTC */
 	dev->vblank_disable_immediate = true;
 
+	mdp5_hw_init(mdp5_kms);
+
 	return 0;
+
+err_destroy_vm:
+	mdp5_kms_destroy_vm(kms);
+err_kms_destroy:
+	mdp_kms_destroy(&mdp5_kms->base);
+err_mdp5_destroy:
+	mdp5_destroy(mdp5_kms);
+
+	return ret;
 }
 
 static void mdp5_destroy(struct mdp5_kms *mdp5_kms)
Signature · 1 line
2.47.3
Acked-by on v3 7/14
#14 lore raw

Re: [PATCH v3 07/14] drm/msm/mdp5: unwind a failed mdp5_kms_init()

Quoted 12 lines · Dmitry Baryshkov

mdp5_kms_init() returns straight away when one of its later steps fails, leaving behind the kms, the runtime PM reference taken around the address space setup and the address space itself, and lets the ->destroy() callback, which the caller runs afterwards, clean up. When mdp5_init() fails, however, it has already run mdp5_destroy() itself, and ->destroy() runs it a second time: the global state object is finalised twice.

Undo the steps which have completed, in the reverse order, drop the runtime PM reference on the error path as well, and tell the caller not to run ->destroy() after a failed init. Fold the hardware setup, which can't fail, into mdp5_kms_init().

Quoted 4 lines
Fixes 8d58ef346f30 ("drm/msm/mdp5: Add global state as a private atomic object")
Assisted-by LLM

---

Konrad

#15 lore raw

PATCH v3 8/14 drm/msm/mdp4: unwind a failed mdp4_kms_init()

In reply to Dmitry Baryshkov

mdp4_kms_init() returns straight away when one of its steps fails and lets the ->destroy() callback, which the caller runs afterwards, clean up. mdp4_destroy() however dereferences mdp4_kms->dev, which is only set after mdp_kms_init() has succeeded, so a failure there turns into a NULL pointer dereference. It also leaves the vdd regulator enabled.

Set mdp4_kms->dev before anything can fail, undo the steps which have completed, in the reverse order, and tell the caller not to run ->destroy() after a failed init. Fold the hardware setup, which can't fail, into mdp4_kms_init().

Fixes 93c125e4ea98 ("drm/msm: don't tear down KMS twice when KMS init fails")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c | 61 ++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 23 deletions(-)
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +38 -23
index c299fef1a400..c2172e5f9e85 100644
@@ -29,9 +29,8 @@ int msm_count_mdp4(void)
 	return count;
 }
 
-static int mdp4_hw_init(struct msm_kms *kms)
+static void mdp4_hw_init(struct mdp4_kms *mdp4_kms)
 {
-	struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
 	struct drm_device *dev = mdp4_kms->dev;
 	u32 dmap_cfg, vg_cfg;
 	unsigned long clk;
@@ -84,8 +83,6 @@ static int mdp4_hw_init(struct msm_kms *kms)
 		mdp4_write(mdp4_kms, REG_MDP4_RESET_STATUS, 1);
 
 	pm_runtime_put_sync(dev->dev);
-
-	return 0;
 }
 
 static void mdp4_enable_commit(struct msm_kms *kms)
@@ -132,6 +129,15 @@ static long mdp4_round_pixclk(struct msm_kms *kms, unsigned long rate,
 	}
 }
 
+static void mdp4_destroy_vm(struct msm_kms *kms)
+{
+	struct msm_mmu *mmu = to_msm_vm(kms->vm)->mmu;
+
+	mmu->funcs->detach(mmu);
+	drm_gpuvm_put(kms->vm);
+	kms->vm = NULL;
+}
+
 static void mdp4_destroy(struct msm_kms *kms)
 {
 	struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
@@ -141,12 +147,7 @@ static void mdp4_destroy(struct msm_kms *kms)
 		msm_gem_unpin_iova(mdp4_kms->blank_cursor_bo, kms->vm);
 	drm_gem_object_put(mdp4_kms->blank_cursor_bo);
 
-	if (kms->vm) {
-		struct msm_mmu *mmu = to_msm_vm(kms->vm)->mmu;
-
-		mmu->funcs->detach(mmu);
-		drm_gpuvm_put(kms->vm);
-	}
+	mdp4_destroy_vm(kms);
 
 	if (mdp4_kms->rpm_enabled)
 		pm_runtime_disable(dev);
@@ -156,7 +157,6 @@ static void mdp4_destroy(struct msm_kms *kms)
 
 static const struct mdp_kms_funcs kms_funcs = {
 	.base = {
-		.hw_init         = mdp4_hw_init,
 		.irq_preinstall  = mdp4_irq_preinstall,
 		.irq_postinstall = mdp4_irq_postinstall,
 		.irq_uninstall   = mdp4_irq_uninstall,
@@ -400,7 +400,7 @@ static int mdp4_kms_init(struct drm_device *dev)
 {
 	struct msm_drm_private *priv = dev->dev_private;
 	struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(priv->kms));
-	struct msm_kms *kms = NULL;
+	struct msm_kms *kms = priv->kms;
 	struct drm_gpuvm *vm;
 	int ret;
 	u32 major, minor;
@@ -409,21 +409,20 @@ static int mdp4_kms_init(struct drm_device *dev)
 	/* TODO: Chips that aren't apq8064 have a 200 Mhz max_clk */
 	max_clk = 266667000;
 
+	kms->init_unwinds = true;
+	mdp4_kms->dev = dev;
+
 	ret = mdp_kms_init(&mdp4_kms->base, &kms_funcs);
 	if (ret) {
 		DRM_DEV_ERROR(dev->dev, "failed to init kms\n");
 		return ret;
 	}
 
-	kms = priv->kms;
-
-	mdp4_kms->dev = dev;
-
 	if (mdp4_kms->vdd) {
 		ret = regulator_enable(mdp4_kms->vdd);
 		if (ret) {
 			DRM_DEV_ERROR(dev->dev, "failed to enable regulator vdd: %d\n", ret);
-			return ret;
+			goto err_kms_destroy;
 		}
 	}
 
@@ -435,7 +434,7 @@ static int mdp4_kms_init(struct drm_device *dev)
 		DRM_DEV_ERROR(dev->dev, "unexpected MDP version: v%d.%d\n",
 			      major, minor);
 		ret = -ENXIO;
-		return ret;
+		goto err_disable_vdd;
 	}
 
 	mdp4_kms->rev = minor;
@@ -444,7 +443,7 @@ static int mdp4_kms_init(struct drm_device *dev)
 		if (!mdp4_kms->lut_clk) {
 			DRM_DEV_ERROR(dev->dev, "failed to get lut_clk\n");
 			ret = -ENODEV;
-			return ret;
+			goto err_disable_vdd;
 		}
 		clk_set_rate(mdp4_kms->lut_clk, max_clk);
 	}
@@ -466,7 +465,7 @@ static int mdp4_kms_init(struct drm_device *dev)
 	vm = msm_kms_init_vm(mdp4_kms->dev, NULL);
 	if (IS_ERR(vm)) {
 		ret = PTR_ERR(vm);
-		return ret;
+		goto err_disable_rpm;
 	}
 
 	kms->vm = vm;
@@ -474,7 +473,7 @@ static int mdp4_kms_init(struct drm_device *dev)
 	ret = modeset_init(mdp4_kms);
 	if (ret) {
 		DRM_DEV_ERROR(dev->dev, "modeset_init failed: %d\n", ret);
-		return ret;
+		goto err_destroy_vm;
 	}
 
 	mdp4_kms->blank_cursor_bo = msm_gem_new(dev, SZ_16K, MSM_BO_WC | MSM_BO_SCANOUT, NULL);
@@ -482,14 +481,14 @@ static int mdp4_kms_init(struct drm_device *dev)
 		ret = PTR_ERR(mdp4_kms->blank_cursor_bo);
 		DRM_DEV_ERROR(dev->dev, "could not allocate blank-cursor bo: %d\n", ret);
 		mdp4_kms->blank_cursor_bo = NULL;
-		return ret;
+		goto err_destroy_vm;
 	}
 
 	ret = msm_gem_get_and_pin_iova(mdp4_kms->blank_cursor_bo, kms->vm,
 			&mdp4_kms->blank_cursor_iova);
 	if (ret) {
 		DRM_DEV_ERROR(dev->dev, "could not pin blank-cursor bo: %d\n", ret);
-		return ret;
+		goto err_put_cursor;
 	}
 
 	dev->mode_config.min_width = 0;
@@ -497,7 +496,23 @@ static int mdp4_kms_init(struct drm_device *dev)
 	dev->mode_config.max_width = 2048;
 	dev->mode_config.max_height = 2048;
 
+	mdp4_hw_init(mdp4_kms);
+
 	return 0;
+
+err_put_cursor:
+	drm_gem_object_put(mdp4_kms->blank_cursor_bo);
+err_destroy_vm:
+	mdp4_destroy_vm(kms);
+err_disable_rpm:
+	pm_runtime_disable(dev->dev);
+err_disable_vdd:
+	if (mdp4_kms->vdd)
+		regulator_disable(mdp4_kms->vdd);
+err_kms_destroy:
+	mdp_kms_destroy(&mdp4_kms->base);
+
+	return ret;
 }
 
 static const struct dev_pm_ops mdp4_pm_ops = {
Signature · 1 line
2.47.3
Acked-by on v3 8/14
#16 lore raw

Re: [PATCH v3 08/14] drm/msm/mdp4: unwind a failed mdp4_kms_init()

Quoted 11 lines · Dmitry Baryshkov

mdp4_kms_init() returns straight away when one of its steps fails and lets the ->destroy() callback, which the caller runs afterwards, clean up. mdp4_destroy() however dereferences mdp4_kms->dev, which is only set after mdp_kms_init() has succeeded, so a failure there turns into a NULL pointer dereference. It also leaves the vdd regulator enabled.

Set mdp4_kms->dev before anything can fail, undo the steps which have completed, in the reverse order, and tell the caller not to run ->destroy() after a failed init. Fold the hardware setup, which can't fail, into mdp4_kms_init().

Quoted 4 lines
Fixes 93c125e4ea98 ("drm/msm: don't tear down KMS twice when KMS init fails")
Assisted-by LLM

---

Konrad

#17 lore raw

PATCH v3 9/14 drm/msm/dpu: don't tear down a failed hw_init twice

In reply to Dmitry Baryshkov

When dpu_kms_hw_init() fails it calls _dpu_kms_hw_destroy() itself, and the caller then runs the ->destroy() callback, which calls _dpu_kms_hw_destroy() a second time. Since the global state object is finalised there, the second call deletes its list entry again and frees its state twice.

Fold the hardware setup into dpu_kms_init(), undo the steps of dpu_kms_init() when it fails, and tell the caller not to run ->destroy() after a failed init.

Fixes 49e27d3c9cd6 ("drm/msm/dpu: finalise global state object")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +15 -14
index da3556eb6ecc..255beb83fc94 100644
@@ -55,7 +55,6 @@
 bool dpu_use_virtual_planes = true;
 module_param(dpu_use_virtual_planes, bool, 0);
 
-static int dpu_kms_hw_init(struct msm_kms *kms);
 static void _dpu_kms_mmu_destroy(struct dpu_kms *dpu_kms);
 
 #ifdef CONFIG_DEBUG_FS
@@ -1067,7 +1066,6 @@ static void dpu_kms_mdp_snapshot(struct msm_disp_state *disp_state, struct msm_k
 }
 
 static const struct msm_kms_funcs kms_funcs = {
-	.hw_init         = dpu_kms_hw_init,
 	.irq_preinstall  = dpu_core_irq_preinstall,
 	.irq_postinstall = dpu_irq_postinstall,
 	.irq_uninstall   = dpu_core_irq_uninstall,
@@ -1135,21 +1133,12 @@ unsigned long dpu_kms_get_clk_rate(struct dpu_kms *dpu_kms, char *clock_name)
 
 #define	DPU_PERF_DEFAULT_MAX_CORE_CLK_RATE	412500000
 
-static int dpu_kms_hw_init(struct msm_kms *kms)
+static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 {
-	struct dpu_kms *dpu_kms;
-	struct drm_device *dev;
-	int rc = -EINVAL;
+	struct drm_device *dev = dpu_kms->dev;
 	unsigned long max_core_clk_rate;
 	u32 core_rev;
-
-	if (!kms) {
-		DPU_ERROR("invalid kms\n");
-		return rc;
-	}
-
-	dpu_kms = to_dpu_kms(kms);
-	dev = dpu_kms->dev;
+	int rc;
 
 	dev->mode_config.cursor_width = 512;
 	dev->mode_config.cursor_height = 512;
@@ -1301,6 +1290,8 @@ static int dpu_kms_init(struct drm_device *ddev)
 	int ret = 0;
 	unsigned long max_freq = ULONG_MAX;
 
+	dpu_kms->base.init_unwinds = true;
+
 	opp = dev_pm_opp_find_freq_floor(dev, &max_freq);
 	if (!IS_ERR(opp))
 		dev_pm_opp_put(opp);
@@ -1317,7 +1308,17 @@ static int dpu_kms_init(struct drm_device *ddev)
 	pm_runtime_enable(&pdev->dev);
 	dpu_kms->rpm_enabled = true;
 
+	ret = dpu_kms_hw_init(dpu_kms);
+	if (ret)
+		goto err_disable_rpm;
+
 	return 0;
+
+err_disable_rpm:
+	pm_runtime_disable(&pdev->dev);
+	msm_kms_destroy(&dpu_kms->base);
+
+	return ret;
 }
 
 static int dpu_kms_mmap_mdp5(struct dpu_kms *dpu_kms)
Signature · 1 line
2.47.3
Acked-by on v3 9/14
#18 lore raw

Re: [PATCH v3 09/14] drm/msm/dpu: don't tear down a failed hw_init twice

Quoted 6 lines · Dmitry Baryshkov

When dpu_kms_hw_init() fails it calls _dpu_kms_hw_destroy() itself, and the caller then runs the ->destroy() callback, which calls _dpu_kms_hw_destroy() a second time. Since the global state object is finalised there, the second call deletes its list entry again and frees its state twice.

Quoted 8 lines

Fold the hardware setup into dpu_kms_init(), undo the steps of dpu_kms_init() when it fails, and tell the caller not to run ->destroy() after a failed init.

Fixes 49e27d3c9cd6 ("drm/msm/dpu: finalise global state object")
Assisted-by LLM

---

Konrad

#19 lore raw

PATCH v3 10/14 drm/msm: tear down only a successfully initialised KMS

In reply to Dmitry Baryshkov

All the kms drivers now undo their own failures in kms_init() and have folded their hardware setup into it.

Drop ->hw_init(), call ->destroy() only for a KMS which was initialised successfully, and remove the flag which let the drivers opt in to that one at a time, as well as the handling of a half torn down KMS in msm_kms_destroy().

Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c  |  2 --
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c |  1 -
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c |  2 --
 drivers/gpu/drm/msm/msm_kms.c            | 16 ++--------------
 drivers/gpu/drm/msm/msm_kms.h            | 10 ++--------
 5 files changed, 4 insertions(+), 27 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +0 -2
index 255beb83fc94..65ba8fa697e9 100644
@@ -1290,8 +1290,6 @@ static int dpu_kms_init(struct drm_device *ddev)
 	int ret = 0;
 	unsigned long max_freq = ULONG_MAX;
 
-	dpu_kms->base.init_unwinds = true;
-
 	opp = dev_pm_opp_find_freq_floor(dev, &max_freq);
 	if (!IS_ERR(opp))
 		dev_pm_opp_put(opp);
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +0 -1
index c2172e5f9e85..c1f61ecf800f 100644
@@ -409,7 +409,6 @@ static int mdp4_kms_init(struct drm_device *dev)
 	/* TODO: Chips that aren't apq8064 have a 200 Mhz max_clk */
 	max_clk = 266667000;
 
-	kms->init_unwinds = true;
 	mdp4_kms->dev = dev;
 
 	ret = mdp_kms_init(&mdp4_kms->base, &kms_funcs);
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c +0 -2
index 30af8ef51e4c..c349eedd55a6 100644
@@ -505,8 +505,6 @@ static int mdp5_kms_init(struct drm_device *dev)
 	struct drm_gpuvm *vm;
 	int i, ret;
 
-	kms->init_unwinds = true;
-
 	ret = mdp5_init(to_platform_device(dev->dev), dev);
 	if (ret)
 		return ret;
drivers/gpu/drm/msm/msm_kms.c +2 -14
index e4d61935a6df..9a207c1e8583 100644
@@ -293,17 +293,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 	ret = priv->kms_init(ddev);
 	if (ret) {
 		DRM_DEV_ERROR(dev, "failed to load kms\n");
-		if (kms->init_unwinds)
-			goto err_destroy_snapshot;
-		goto err_destroy_kms;
-	}
-
-	if (kms->funcs->hw_init) {
-		ret = kms->funcs->hw_init(kms);
-		if (ret) {
-			DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
-			goto err_destroy_kms;
-		}
+		goto err_destroy_snapshot;
 	}
 
 	drm_helper_move_panel_connectors_to_head(ddev);
@@ -347,9 +337,7 @@ int msm_drm_kms_init(struct device *dev, const struct drm_driver *drv)
 
 err_destroy_event_threads:
 	msm_drm_kms_destroy_event_threads(kms);
-err_destroy_kms:
-	if (kms->funcs)
-		kms->funcs->destroy(kms);
+	kms->funcs->destroy(kms);
 err_destroy_snapshot:
 	msm_disp_snapshot_destroy(ddev);
 
drivers/gpu/drm/msm/msm_kms.h +2 -8
index 2f097e23e2e9..b5e1cd38cd8a 100644
@@ -23,8 +23,6 @@
  * for constructing the appropriate planes/crtcs/encoders/connectors.
  */
 struct msm_kms_funcs {
-	/* hw initialization: */
-	int (*hw_init)(struct msm_kms *kms);
 	/* irq handling: */
 	void (*irq_preinstall)(struct msm_kms *kms);
 	int (*irq_postinstall)(struct msm_kms *kms);
@@ -149,9 +147,6 @@ struct msm_kms {
 	int irq;
 	bool irq_requested;
 
-	/* set by the kms drivers whose kms_init() undoes its own failures */
-	bool init_unwinds;
-
 	/* rate limit the snapshot capture to once per attach */
 	atomic_t fault_snapshot_capture;
 
@@ -214,9 +209,8 @@ static inline void msm_kms_destroy(struct msm_kms *kms)
 	for (i = 0; i < ARRAY_SIZE(kms->pending_timers); i++)
 		msm_atomic_destroy_pending_timer(&kms->pending_timers[i]);
 
-	/* the kms drivers' ->destroy() also runs after a failed init */
-	if (kms->wq)
-		destroy_workqueue(kms->wq);
+	destroy_workqueue(kms->wq);
+	kms->wq = NULL;
 }
 
 #define for_each_crtc_mask(dev, crtc, crtc_mask) \
Signature · 1 line
2.47.3
Acked-by on v3 10/14
#20 lore raw

Re: [PATCH v3 10/14] drm/msm: tear down only a successfully initialised KMS

Quoted 11 lines · Dmitry Baryshkov

All the kms drivers now undo their own failures in kms_init() and have folded their hardware setup into it.

Drop ->hw_init(), call ->destroy() only for a KMS which was initialised successfully, and remove the flag which let the drivers opt in to that one at a time, as well as the handling of a half torn down KMS in msm_kms_destroy().

Assisted-by LLM

---

Konrad

#21 lore raw

PATCH v3 11/14 drm/msm/dpu: clean up the self-refresh helpers on teardown

In reply to Dmitry Baryshkov

Unbinding the display driver on MSM8996 with DPU oopses about a second later:

  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000870
  pc : drm_atomic_commit_alloc+0x10/0x9c
  lr : drm_self_refresh_helper_entry_work+0x5c/0x1dc

dpu_crtc_init() sets up the self-refresh helper for each CRTC, but nothing ever calls drm_self_refresh_helper_cleanup(). Every modeset commit, including the one which shuts the display down on unbind, queues the delayed entry work of the helper, which then runs after the CRTC and the DRM device have been freed, and its data is leaked.

Clean the helpers up when tearing down the DPU hardware, which cancels the pending work while the KMS is still intact. Doing it from a managed action instead would not be enough: the DRM device, and its managed resources with it, outlives the unbind as long as userspace holds it open.

Fixes 1122697810e5 ("drm/msm/disp/dpu: add PSR support for eDP interface in dpu driver")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +11 -0
index 65ba8fa697e9..934ba47b2dae 100644
@@ -17,6 +17,7 @@
 #include <drm/drm_crtc.h>
 #include <drm/drm_file.h>
 #include <drm/drm_framebuffer.h>
+#include <drm/drm_self_refresh_helper.h>
 #include <drm/drm_vblank.h>
 #include <drm/drm_writeback.h>
 
@@ -885,8 +886,18 @@ static int _dpu_kms_drm_obj_init(struct dpu_kms *dpu_kms)
 	return 0;
 }
 
+static void dpu_kms_self_refresh_cleanup(struct dpu_kms *dpu_kms)
+{
+	struct drm_crtc *crtc;
+
+	drm_for_each_crtc(crtc, dpu_kms->dev)
+		drm_self_refresh_helper_cleanup(crtc);
+}
+
 static void _dpu_kms_hw_destroy(struct dpu_kms *dpu_kms)
 {
+	dpu_kms_self_refresh_cleanup(dpu_kms);
+
 	dpu_kms->hw_intr = NULL;
 
 	/* safe to call these more than once during shutdown */
Signature · 1 line
2.47.3
Reviewed-by on v3 11/14
#22 lore raw

Re: [PATCH v3 11/14] drm/msm/dpu: clean up the self-refresh helpers on teardown

Quoted 19 lines · Dmitry Baryshkov

Unbinding the display driver on MSM8996 with DPU oopses about a second later:

  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000870
  pc : drm_atomic_commit_alloc+0x10/0x9c
  lr : drm_self_refresh_helper_entry_work+0x5c/0x1dc

dpu_crtc_init() sets up the self-refresh helper for each CRTC, but nothing ever calls drm_self_refresh_helper_cleanup(). Every modeset commit, including the one which shuts the display down on unbind, queues the delayed entry work of the helper, which then runs after the CRTC and the DRM device have been freed, and its data is leaked.

Clean the helpers up when tearing down the DPU hardware, which cancels the pending work while the KMS is still intact. Doing it from a managed action instead would not be enough: the DRM device, and its managed resources with it, outlives the unbind as long as userspace holds it open.

Quoted 4 lines
Fixes 1122697810e5 ("drm/msm/disp/dpu: add PSR support for eDP interface in dpu driver")
Assisted-by LLM

---

Konrad

#23 lore raw

PATCH v3 12/14 drm/msm/dpu: unwind a failed dpu_kms_hw_init() step by step

In reply to Dmitry Baryshkov

When dpu_kms_hw_init() fails it calls _dpu_kms_hw_destroy(), the teardown of a fully initialised KMS, whatever step it failed at.

Undo only the steps which have completed, in the reverse order, as the kernel convention wants.

The hw_mdp, hw_vbif and hw_intr blocks are allocated with drmm and go away with the DRM device, while dpu_kms stays around for the next bind attempt, so clear the pointers to them as _dpu_kms_hw_destroy() did. Otherwise dpu_runtime_resume() of the next bind attempt passes the stale hw_vbif to dpu_vbif_init_memtypes() before dpu_kms_hw_init() sets it.

Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +18 -13
index 934ba47b2dae..3546efacba3c 100644
@@ -1161,7 +1161,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 
 	rc = pm_runtime_resume_and_get(&dpu_kms->pdev->dev);
 	if (rc < 0)
-		goto error;
+		goto err_global_obj_fini;
 
 	core_rev = readl_relaxed(dpu_kms->mmio + 0x0);
 
@@ -1188,19 +1188,19 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	if (IS_ERR(dpu_kms->mdss)) {
 		rc = PTR_ERR(dpu_kms->mdss);
 		DPU_ERROR("failed to get UBWC config data: %d\n", rc);
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	if (!dpu_kms->mdss) {
 		rc = -EINVAL;
 		DPU_ERROR("NULL MDSS data\n");
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	rc = dpu_rm_init(dev, &dpu_kms->rm, dpu_kms->catalog, dpu_kms->mdss, dpu_kms->mmio);
 	if (rc) {
 		DPU_ERROR("rm init failed: %d\n", rc);
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	dpu_kms->hw_mdp = dpu_hw_mdptop_init(dev,
@@ -1210,8 +1210,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	if (IS_ERR(dpu_kms->hw_mdp)) {
 		rc = PTR_ERR(dpu_kms->hw_mdp);
 		DPU_ERROR("failed to get hw_mdp: %d\n", rc);
-		dpu_kms->hw_mdp = NULL;
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	struct dpu_hw_vbif *hw;
@@ -1221,7 +1220,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	if (IS_ERR(hw)) {
 		rc = PTR_ERR(hw);
 		DPU_ERROR("failed to init vbif: %d\n", rc);
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	dpu_kms->hw_vbif = hw;
@@ -1236,7 +1235,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	rc = dpu_core_perf_init(&dpu_kms->perf, dpu_kms->catalog->perf, max_core_clk_rate);
 	if (rc) {
 		DPU_ERROR("failed to init perf %d\n", rc);
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	/*
@@ -1253,8 +1252,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	if (IS_ERR(dpu_kms->hw_intr)) {
 		rc = PTR_ERR(dpu_kms->hw_intr);
 		DPU_ERROR("hw_intr init failed: %d\n", rc);
-		dpu_kms->hw_intr = NULL;
-		goto err_pm_put;
+		goto err_mmu_destroy;
 	}
 
 	dev->mode_config.min_width = 0;
@@ -1274,7 +1272,7 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 	rc = _dpu_kms_drm_obj_init(dpu_kms);
 	if (rc) {
 		DPU_ERROR("modeset init failed: %d\n", rc);
-		goto err_pm_put;
+		goto err_self_refresh_cleanup;
 	}
 
 	dpu_vbif_init_memtypes(dpu_kms);
@@ -1283,10 +1281,17 @@ static int dpu_kms_hw_init(struct dpu_kms *dpu_kms)
 
 	return 0;
 
+err_self_refresh_cleanup:
+	dpu_kms_self_refresh_cleanup(dpu_kms);
+err_mmu_destroy:
+	dpu_kms->hw_intr = NULL;
+	dpu_kms->hw_vbif = NULL;
+	dpu_kms->hw_mdp = NULL;
+	_dpu_kms_mmu_destroy(dpu_kms);
 err_pm_put:
 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
-error:
-	_dpu_kms_hw_destroy(dpu_kms);
+err_global_obj_fini:
+	dpu_kms_global_obj_fini(dpu_kms);
 
 	return rc;
 }
Signature · 1 line
2.47.3
Acked-by on v3 12/14
#24 lore raw

Re: [PATCH v3 12/14] drm/msm/dpu: unwind a failed dpu_kms_hw_init() step by step

Quoted 12 lines · Dmitry Baryshkov

When dpu_kms_hw_init() fails it calls _dpu_kms_hw_destroy(), the teardown of a fully initialised KMS, whatever step it failed at.

Undo only the steps which have completed, in the reverse order, as the kernel convention wants.

The hw_mdp, hw_vbif and hw_intr blocks are allocated with drmm and go away with the DRM device, while dpu_kms stays around for the next bind attempt, so clear the pointers to them as _dpu_kms_hw_destroy() did. Otherwise dpu_runtime_resume() of the next bind attempt passes the stale hw_vbif to dpu_vbif_init_memtypes() before dpu_kms_hw_init() sets it.

Quoted 3 lines
Assisted-by LLM

---

LLM is slightly curious about not clearing dpu_kms->catalog but I think it's fine

Konrad

#25 lore raw

PATCH v3 13/14 drm/msm/mdp4: disable the vdd regulator on teardown

In reply to Dmitry Baryshkov

mdp4_kms_init() enables the optional vdd regulator, but only its error path disables it again: unbinding the display leaves it on, and the regulator core warns when the devm-managed exclusive handle is released while enabled.

Disable it again in mdp4_destroy().

Fixes c8afe684c95c ("drm/msm: basic KMS driver for snapdragon")
Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +3 -0
index c1f61ecf800f..cb95a492e070 100644
@@ -152,6 +152,9 @@ static void mdp4_destroy(struct msm_kms *kms)
 	if (mdp4_kms->rpm_enabled)
 		pm_runtime_disable(dev);
 
+	if (mdp4_kms->vdd)
+		regulator_disable(mdp4_kms->vdd);
+
 	mdp_kms_destroy(&mdp4_kms->base);
 }
 
Signature · 1 line
2.47.3
Reviewed-by on v3 13/14
#26 lore raw

Re: [PATCH v3 13/14] drm/msm/mdp4: disable the vdd regulator on teardown

Quoted 11 lines · Dmitry Baryshkov

mdp4_kms_init() enables the optional vdd regulator, but only its error path disables it again: unbinding the display leaves it on, and the regulator core warns when the devm-managed exclusive handle is released while enabled.

Disable it again in mdp4_destroy().

Fixes c8afe684c95c ("drm/msm: basic KMS driver for snapdragon")
Assisted-by LLM

---

Konrad

#27 lore raw

PATCH v3 14/14 drm/msm: drop the runtime PM flags from the KMS teardown

In reply to Dmitry Baryshkov

The DPU, MDP4 and MDP5 drivers track whether they have enabled runtime PM, and MDP4 whether it has pinned its blank cursor, so that ->destroy() can tear down a KMS whose init failed half way. ->destroy() now only runs for a KMS which was initialised successfully, where both are always true.

Drop the flags and the checks.

Assisted-by LLM
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c  | 4 +---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h  | 1 -
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c | 7 ++-----
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h | 2 --
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c | 5 +----
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h | 2 --
 6 files changed, 4 insertions(+), 17 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +1 -3
index 3546efacba3c..e994fd267c5b 100644
@@ -927,8 +927,7 @@ static void dpu_kms_destroy(struct msm_kms *kms)
 
 	msm_kms_destroy(&dpu_kms->base);
 
-	if (dpu_kms->rpm_enabled)
-		pm_runtime_disable(&dpu_kms->pdev->dev);
+	pm_runtime_disable(&dpu_kms->pdev->dev);
 }
 
 static int dpu_irq_postinstall(struct msm_kms *kms)
@@ -1320,7 +1319,6 @@ static int dpu_kms_init(struct drm_device *ddev)
 	dpu_kms->dev = ddev;
 
 	pm_runtime_enable(&pdev->dev);
-	dpu_kms->rpm_enabled = true;
 
 	ret = dpu_kms_hw_init(dpu_kms);
 	if (ret)
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h +0 -1
index e39831a397b1..769b8ce6332f 100644
@@ -87,7 +87,6 @@ struct dpu_kms {
 	bool has_danger_ctrl;
 
 	struct platform_device *pdev;
-	bool rpm_enabled;
 
 	struct clk_bulk_data *clocks;
 	size_t num_clocks;
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +2 -5
index cb95a492e070..8b07881c3f8d 100644
@@ -143,14 +143,12 @@ static void mdp4_destroy(struct msm_kms *kms)
 	struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
 	struct device *dev = mdp4_kms->dev->dev;
 
-	if (mdp4_kms->blank_cursor_iova)
-		msm_gem_unpin_iova(mdp4_kms->blank_cursor_bo, kms->vm);
+	msm_gem_unpin_iova(mdp4_kms->blank_cursor_bo, kms->vm);
 	drm_gem_object_put(mdp4_kms->blank_cursor_bo);
 
 	mdp4_destroy_vm(kms);
 
-	if (mdp4_kms->rpm_enabled)
-		pm_runtime_disable(dev);
+	pm_runtime_disable(dev);
 
 	if (mdp4_kms->vdd)
 		regulator_disable(mdp4_kms->vdd);
@@ -451,7 +449,6 @@ static int mdp4_kms_init(struct drm_device *dev)
 	}
 
 	pm_runtime_enable(dev->dev);
-	mdp4_kms->rpm_enabled = true;
 
 	/* make sure things are off before attaching iommu (bootloader could
 	 * have left things on, in which case we'll start getting faults if
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h +0 -2
index 06458d4ee48c..9dd829ec9401 100644
@@ -34,8 +34,6 @@ struct mdp4_kms {
 
 	struct mdp_irq error_handler;
 
-	bool rpm_enabled;
-
 	/* empty/blank cursor bo to use when cursor is "disabled" */
 	struct drm_gem_object *blank_cursor_bo;
 	uint64_t blank_cursor_iova;
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c +1 -4
index c349eedd55a6..bbf459bff35d 100644
@@ -575,9 +575,7 @@ static int mdp5_kms_init(struct drm_device *dev)
 
 static void mdp5_destroy(struct mdp5_kms *mdp5_kms)
 {
-	if (mdp5_kms->rpm_enabled)
-		pm_runtime_disable(&mdp5_kms->pdev->dev);
-
+	pm_runtime_disable(&mdp5_kms->pdev->dev);
 	drm_atomic_private_obj_fini(&mdp5_kms->glob_state);
 
 	mdp5_kms->num_hwpipes = 0;
@@ -734,7 +732,6 @@ static int mdp5_init(struct platform_device *pdev, struct drm_device *dev)
 	clk_set_rate(mdp5_kms->core_clk, 200000000);
 
 	pm_runtime_enable(&pdev->dev);
-	mdp5_kms->rpm_enabled = true;
 
 	read_mdp_hw_revision(mdp5_kms, &major, &minor);
 
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h +0 -2
index 7bf2172fce0b..c0b163c4baa3 100644
@@ -62,8 +62,6 @@ struct mdp5_kms {
 	 */
 	spinlock_t resource_lock;
 
-	bool rpm_enabled;
-
 	struct mdp_irq error_handler;
 
 	int enable_count;
Signature · 1 line
2.47.3
Acked-by on v3 14/14
#28 lore raw

Re: [PATCH v3 14/14] drm/msm: drop the runtime PM flags from the KMS teardown

Quoted 10 lines · Dmitry Baryshkov

The DPU, MDP4 and MDP5 drivers track whether they have enabled runtime PM, and MDP4 whether it has pinned its blank cursor, so that ->destroy() can tear down a KMS whose init failed half way. ->destroy() now only runs for a KMS which was initialised successfully, where both are always true.

Drop the flags and the checks.

Assisted-by LLM

---

Konrad