mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yaohan Li <shunsheng1218@gmail.com>
To: Rob Clark <robin.clark@oss.qualcomm.com>,
	Dmitry Baryshkov <lumag@kernel.org>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>
Cc: "Abhinav Kumar" <abhinav.kumar@linux.dev>,
	"Jessica Zhang" <jesszhan0024@gmail.com>,
	"Sean Paul" <sean@poorly.run>,
	"Marijn Suijten" <marijn.suijten@somainline.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Sandy Huang" <hjc@rock-chips.com>,
	"Heiko Stübner" <heiko@sntech.de>,
	"Andy Yan" <andy.yan@rock-chips.com>,
	linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
	freedreno@lists.freedesktop.org,
	linux-rockchip@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down
Date: Thu, 19 Nov 2026 16:31:32 +0800	[thread overview]
Message-ID: <20261119083133.3437277-1-shunsheng1218@gmail.com> (raw)
In-Reply-To: <20261119023604.1495603-1-shunsheng1218@gmail.com>

mdp4 and mdp5 queue the BO of every cursor update on a drm_flip_work and
release it from unref_cursor_worker() once a vblank interrupt has
committed it.  The work is cleaned up by a DRM-managed action, so it
runs at the final drm_dev_put(), while msm_drm_uninit() destroys the KMS
and its VM in msm_drm_kms_uninit() and only then drops the reference.

A cursor update that was queued but never committed - no vblank
interrupt after the last atomic commit - is still in the flip work at
teardown.  Releasing it needs the VM that unref_cursor_worker() reads
through the KMS, so the cleanup has to run before the KMS goes away and
not after.

Run the cleanup from the KMS destroy path, which is after the interrupts
and the KMS workqueue have been stopped and before drm_gpuvm_put(), and
remove the managed action so that it cannot run a second time.  The
action is only released for a CRTC that registered it: a partially
failed init can leave CRTCs behind that never added one.

Guard the new CRTC traversal when the KMS has not got its drm_device yet:
the teardown can run on a partly initialised KMS.  This does not make
mdp4_destroy() safe for every such state - it dereferences mdp4_kms->dev
before the loop - and the separate KMS-init error handling series is what
reworks that lifecycle.

This prepares for draining the queued tasks in drm_flip_work_cleanup():
running them from the managed action, after the KMS and its VM are gone,
would dereference the freed KMS.

Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>

---
Changes in v2:
  - New patch.  drm_flip_work_cleanup() is about to run the tasks that are
    still queued, and for mdp4/mdp5 those tasks unpin a cursor BO through
    the KMS and its VM, which msm_drm_kms_uninit() destroys long before the
    DRM-managed action would run.  Release the action from the KMS destroy
    path, before drm_gpuvm_put(), instead.  Reported by Sashiko.
  - Only walk the CRTCs when the KMS already has its drm_device; this
    covers the new traversal only.  The existing partial-initialisation
    path in mdp4_destroy() still dereferences mdp4_kms->dev before the loop
    and is left to the in-flight "drm/msm: fix the error handling of the
    KMS init" series, which is not merged in msm-next yet.
  - Checked as a series: both patches apply in order on msm-next
    (d024361c631b) and on drm-misc-next (fac7a5f16230).
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested only: arm64 with aarch64-linux-gnu-gcc 11.4
and CONFIG_DRM_MSM=m.  The affected hardware was not available, so the
teardown path was not exercised and no fault or race injection was run.

Checked against msm-next (d024361c631b): the patch applies there, as part of
the series it also applies on drm-misc-next (fac7a5f16230), and Dmitry
Baryshkov's "drm/msm: fix the error handling of the KMS init" series is not
merged yet, so mdp4_destroy() still dereferences mdp4_kms->dev before the
new loop.  The guard added here covers the new CRTC traversal only; the
existing partial-initialisation path keeps its current behaviour and is left
to that series.

AI assistance: the analysis, the change and its review notes were
prepared with Claude (Anthropic) as a coding assistant.  The author
reviewed the reasoning and takes responsibility for the content.
---
 drivers/gpu/drm/msm/disp/mdp4/mdp4_crtc.c | 26 ++++++++++++++++++++++
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c  | 13 +++++++++++
 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h  |  1 +
 drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c | 27 +++++++++++++++++++++++
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c  | 13 +++++++++++
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h  |  1 +
 6 files changed, 81 insertions(+)

diff --git a/drivers/gpu/drm/msm/disp/mdp4/mdp4_crtc.c b/drivers/gpu/drm/msm/disp/mdp4/mdp4_crtc.c
index 93050b3c3548c..9767d9aef43d6 100644
--- a/drivers/gpu/drm/msm/disp/mdp4/mdp4_crtc.c
+++ b/drivers/gpu/drm/msm/disp/mdp4/mdp4_crtc.c
@@ -54,6 +54,9 @@ struct mdp4_crtc {
 	/* for unref'ing cursor bo's after scanout completes: */
 	struct drm_flip_work unref_cursor_work;
 
+	/* the flip cleanup action is registered on the DRM device */
+	bool flip_cleanup_registered;
+
 	struct mdp_irq vblank;
 	struct mdp_irq err;
 };
@@ -622,6 +625,27 @@ static void mdp4_crtc_flip_cleanup(struct drm_device *dev, void *ptr)
 	drm_flip_work_cleanup(&mdp4_crtc->unref_cursor_work);
 }
 
+/*
+ * Run the cursor flip cleanup now instead of at the final drm_dev_put().
+ * msm_drm_kms_uninit() destroys the KMS and its VM long before that, and
+ * unref_cursor_worker() needs both to release the BOs of the cursor updates
+ * that were queued but never committed by a vblank interrupt.
+ */
+void mdp4_crtc_release_flip_work(struct drm_crtc *crtc)
+{
+	struct mdp4_crtc *mdp4_crtc;
+
+	if (crtc->funcs != &mdp4_crtc_funcs)
+		return;
+
+	mdp4_crtc = to_mdp4_crtc(crtc);
+	if (!mdp4_crtc->flip_cleanup_registered)
+		return;
+
+	mdp4_crtc->flip_cleanup_registered = false;
+	drmm_release_action(crtc->dev, mdp4_crtc_flip_cleanup, mdp4_crtc);
+}
+
 /* initialize crtc */
 struct drm_crtc *mdp4_crtc_init(struct drm_device *dev,
 		struct drm_plane *plane, int ovlp_id,
@@ -659,6 +683,8 @@ struct drm_crtc *mdp4_crtc_init(struct drm_device *dev,
 	if (ret)
 		return ERR_PTR(ret);
 
+	mdp4_crtc->flip_cleanup_registered = true;
+
 	drm_crtc_helper_add(crtc, &mdp4_crtc_helper_funcs);
 
 	return crtc;
diff --git a/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c b/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c
index ab7b62381685f..3fd528eeff0e3 100644
--- a/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c
+++ b/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c
@@ -136,6 +136,19 @@ 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;
+	struct drm_crtc *crtc;
+
+	/*
+	 * Release the cursor flip work while the VM is still alive.  The
+	 * cleanup is registered as a DRM-managed action, so it would only run
+	 * at the final drm_dev_put(), long after drm_gpuvm_put() below has
+	 * destroyed the VM that unref_cursor_worker() needs.  The interrupts
+	 * and the KMS workqueue have been stopped at this point, so no new
+	 * cursor update can be queued any more.
+	 */
+	if (mdp4_kms->dev)
+		drm_for_each_crtc(crtc, mdp4_kms->dev)
+			mdp4_crtc_release_flip_work(crtc);
 
 	if (mdp4_kms->blank_cursor_iova)
 		msm_gem_unpin_iova(mdp4_kms->blank_cursor_bo, kms->vm);
diff --git a/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h b/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h
index 06458d4ee48c9..48cb2b9d896b8 100644
--- a/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h
+++ b/drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.h
@@ -184,6 +184,7 @@ uint32_t mdp4_crtc_vblank(struct drm_crtc *crtc);
 void mdp4_crtc_set_config(struct drm_crtc *crtc, uint32_t config);
 void mdp4_crtc_set_intf(struct drm_crtc *crtc, enum mdp4_intf intf, int mixer);
 void mdp4_crtc_wait_for_commit_done(struct drm_crtc *crtc);
+void mdp4_crtc_release_flip_work(struct drm_crtc *crtc);
 struct drm_crtc *mdp4_crtc_init(struct drm_device *dev,
 		struct drm_plane *plane, int ovlp_id,
 		enum mdp4_dma dma_id);
diff --git a/drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c b/drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c
index 1e1fff37380c7..2d457311a3ef5 100644
--- a/drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c
+++ b/drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c
@@ -53,6 +53,9 @@ struct mdp5_crtc {
 
 	bool lm_cursor_enabled;
 
+	/* the flip cleanup action is registered on the DRM device */
+	bool flip_cleanup_registered;
+
 	struct {
 		/* protect REG_MDP5_LM_CURSOR* registers and cursor scanout_bo*/
 		spinlock_t lock;
@@ -1316,6 +1319,28 @@ void mdp5_crtc_wait_for_commit_done(struct drm_crtc *crtc)
 }
 
 /* initialize crtc */
+/*
+ * Run the cursor flip cleanup now instead of at the final drm_dev_put().
+ * msm_drm_kms_uninit() destroys the KMS and its VM long before that, and
+ * unref_cursor_worker() needs both to release the BOs of the cursor updates
+ * that were queued but never committed by a vblank interrupt.
+ */
+void mdp5_crtc_release_flip_work(struct drm_crtc *crtc)
+{
+	struct mdp5_crtc *mdp5_crtc;
+
+	if (crtc->funcs != &mdp5_crtc_funcs &&
+	    crtc->funcs != &mdp5_crtc_no_lm_cursor_funcs)
+		return;
+
+	mdp5_crtc = to_mdp5_crtc(crtc);
+	if (!mdp5_crtc->flip_cleanup_registered)
+		return;
+
+	mdp5_crtc->flip_cleanup_registered = false;
+	drmm_release_action(crtc->dev, mdp5_crtc_flip_cleanup, mdp5_crtc);
+}
+
 struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
 				struct drm_plane *plane,
 				struct drm_plane *cursor_plane, int id)
@@ -1353,6 +1378,8 @@ struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
 	if (ret)
 		return ERR_PTR(ret);
 
+	mdp5_crtc->flip_cleanup_registered = true;
+
 	drm_crtc_helper_add(crtc, &mdp5_crtc_helper_funcs);
 
 	return crtc;
diff --git a/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c b/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c
index ff6751b2ae626..834faf9c886ad 100644
--- a/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c
+++ b/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c
@@ -201,6 +201,19 @@ static void mdp5_destroy(struct mdp5_kms *mdp5_kms);
 static void mdp5_kms_destroy(struct msm_kms *kms)
 {
 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
+	struct drm_crtc *crtc;
+
+	/*
+	 * Release the cursor flip work while the VM is still alive.  The
+	 * cleanup is registered as a DRM-managed action, so it would only run
+	 * at the final drm_dev_put(), long after drm_gpuvm_put() below has
+	 * destroyed the VM that unref_cursor_worker() needs.  The interrupts
+	 * and the KMS workqueue have been stopped at this point, so no new
+	 * cursor update can be queued any more.
+	 */
+	if (mdp5_kms->dev)
+		drm_for_each_crtc(crtc, mdp5_kms->dev)
+			mdp5_crtc_release_flip_work(crtc);
 
 	if (kms->vm) {
 		struct msm_mmu *mmu = to_msm_vm(kms->vm)->mmu;
diff --git a/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h b/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h
index 7bf2172fce0bd..b8b8c23b58500 100644
--- a/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h
+++ b/drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.h
@@ -284,6 +284,7 @@ struct mdp5_hw_mixer *mdp5_crtc_get_mixer(struct drm_crtc *crtc);
 struct mdp5_pipeline *mdp5_crtc_get_pipeline(struct drm_crtc *crtc);
 void mdp5_crtc_set_pipeline(struct drm_crtc *crtc);
 void mdp5_crtc_wait_for_commit_done(struct drm_crtc *crtc);
+void mdp5_crtc_release_flip_work(struct drm_crtc *crtc);
 struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
 				struct drm_plane *plane,
 				struct drm_plane *cursor_plane, int id);
-- 
2.34.1


  reply	other threads:[~2026-11-19  8:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  2:36 [RFC PATCH] drm/flip: drain the flip work on cleanup Yaohan Li
2026-11-19  8:31 ` Yaohan Li [this message]
2026-11-19  8:31   ` [RFC PATCH v2 2/2] " Yaohan Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119083133.3437277-1-shunsheng1218@gmail.com \
    --to=shunsheng1218@gmail.com \
    --cc=abhinav.kumar@linux.dev \
    --cc=airlied@gmail.com \
    --cc=andy.yan@rock-chips.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=freedreno@lists.freedesktop.org \
    --cc=heiko@sntech.de \
    --cc=hjc@rock-chips.com \
    --cc=jesszhan0024@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=lumag@kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=marijn.suijten@somainline.org \
    --cc=mripard@kernel.org \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=sean@poorly.run \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®