dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] drm/flip: drain the flip work on cleanup
@ 2026-11-19  2:36 Yaohan Li
  2026-11-19  2:51 ` sashiko-bot
  2026-11-19  8:31 ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down Yaohan Li
  0 siblings, 2 replies; 6+ messages in thread
From: Yaohan Li @ 2026-11-19  2:36 UTC (permalink / raw)
  To: maarten.lankhorst, mripard, tzimmermann
  Cc: dri-devel, hjc, heiko, andyshrk, linux-kernel

drm_flip_work_cleanup() only warns when the queued or the committed list
is not empty.  Nothing cancels the worker and nothing runs the tasks
that are still queued, so the caller silently leaks whatever the tasks
hold (framebuffer references, vblank references, ...) and, if the
worker is still pending, it can run after the structure embedding the
flip work has been freed.

Cancel the worker and run the remaining tasks from
drm_flip_work_cleanup().  The work item is the only producer of the
committed list, so after cancel_work_sync() the two lists can be
spliced out and iterated without holding the lock.

The helper only drains the work items that exist when it is called, so
the callers have to make sure that no new item can be queued.  Both
in-tree callers already do:

 - Rockchip VOP: rockchip_drm_unbind() runs drm_atomic_helper_shutdown()
   before component_unbind_all(), and vop_unbind() calls
   pm_runtime_disable() before vop_destroy_crtc().  The CRTC is disabled
   and the device cannot be resumed when the cleanup runs, so neither
   vop_plane_atomic_update() nor vop_crtc_atomic_flush() can queue a new
   item, and vop_isr() returns early because
   pm_runtime_get_if_in_use() fails.

 - MSM mdp4/mdp5: msm_drm_uninit() destroys the KMS, which releases the
   interrupt, before drm_dev_put() runs the drmm actions.  The flip
   cleanup action is registered after drmm_crtc_alloc_with_planes(), so
   it runs before drmm_crtc_cleanup() and after the vblank interrupt
   that commits the work is gone.

Rockchip VOP called drm_flip_work_cleanup() after drm_crtc_cleanup(),
which memsets the CRTC.  Its worker calls drm_crtc_vblank_put() on that
CRTC, so the drain has to happen before the CRTC is cleaned up.

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

---
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested only: arm64 with aarch64-linux-gnu-gcc 11.4
and CONFIG_DRM_ROCKCHIP=m, 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.

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/drm_flip_work.c             | 23 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c |  9 +++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_flip_work.c b/drivers/gpu/drm/drm_flip_work.c
index 52c437746d48..b8ce3966fa9a 100644
--- a/drivers/gpu/drm/drm_flip_work.c
+++ b/drivers/gpu/drm/drm_flip_work.c
@@ -149,10 +149,29 @@ EXPORT_SYMBOL(drm_flip_work_init);
  * drm_flip_work_cleanup - cleans up flip-work
  * @work: the flip-work to cleanup
  *
- * Destroy resources allocated for the flip-work
+ * Cancel the worker and run the tasks that are still queued, so that the
+ * resources they hold are released even if the work never made it to the
+ * workqueue.  Must be called before the object embedding @work is freed and
+ * after the driver has stopped the code that calls drm_flip_work_queue()
+ * and drm_flip_work_commit(): only the work items that exist when this
+ * function is called are drained.
  */
 void drm_flip_work_cleanup(struct drm_flip_work *work)
 {
-	WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
+	struct drm_flip_task *task, *tmp;
+	LIST_HEAD(tasks);
+	unsigned long flags;
+
+	cancel_work_sync(&work->worker);
+
+	spin_lock_irqsave(&work->lock, flags);
+	list_splice_tail_init(&work->queued, &tasks);
+	list_splice_tail_init(&work->commited, &tasks);
+	spin_unlock_irqrestore(&work->lock, flags);
+
+	list_for_each_entry_safe(task, tmp, &tasks, node) {
+		work->func(work, task->data);
+		kfree(task);
+	}
 }
 EXPORT_SYMBOL(drm_flip_work_cleanup);
diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_vop.c b/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
index 69e72727da24..38935ef08b70 100644
--- a/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
+++ b/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
@@ -1990,12 +1990,19 @@ static void vop_destroy_crtc(struct vop *vop)
 				 head)
 		drm_plane_cleanup(plane);
 
+	/*
+	 * The flip work drops the vblank reference taken when the framebuffer
+	 * was queued, so it has to be drained while the CRTC is still valid.
+	 * The CRTC has been shut down and runtime PM disabled by the time this
+	 * runs, so no new work item can be queued anymore.
+	 */
+	drm_flip_work_cleanup(&vop->fb_unref_work);
+
 	/*
 	 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
 	 * references the CRTC.
 	 */
 	drm_crtc_cleanup(crtc);
-	drm_flip_work_cleanup(&vop->fb_unref_work);
 }
 
 static int vop_initial(struct vop *vop)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] drm/flip: drain the flip work on cleanup
  2026-11-19  2:36 [RFC PATCH] drm/flip: drain the flip work on cleanup Yaohan Li
@ 2026-11-19  2:51 ` sashiko-bot
  2026-11-19  8:31 ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down Yaohan Li
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-19  2:51 UTC (permalink / raw)
  To: Yaohan Li; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/msm: NULL deref and UAF in unref_cursor_worker() on flip
  cleanup
- [Low] drm/flip: missing Fixes: trailers on bug-fix commit
--

commit 73632c7ec76b9cf83dec08873d97bb312392eec3
Author: Yaohan Li <shunsheng1218@gmail.com>

> drm/flip: drain the flip work on cleanup
>
> drm_flip_work_cleanup() only warns when the queued or the committed list
> is not empty.  Nothing cancels the worker and nothing runs the tasks
> that are still queued, so the caller silently leaks whatever the tasks
> hold (framebuffer references, vblank references, ...) and, if the
> worker is still pending, it can run after the structure embedding the
> flip work has been freed.
>
> Cancel the worker and run the remaining tasks from
> drm_flip_work_cleanup().  The work item is the only producer of the
> committed list, so after cancel_work_sync() the two lists can be
> spliced out and iterated without holding the lock.
>
> The helper only drains the work items that exist when it is called, so
> the callers have to make sure that no new item can be queued.  Both
> in-tree callers already do:
>
>  - Rockchip VOP: rockchip_drm_unbind() runs drm_atomic_helper_shutdown()
>    before component_unbind_all(), and vop_unbind() calls
>    pm_runtime_disable() before vop_destroy_crtc().  The CRTC is disabled
>    and the device cannot be resumed when the cleanup runs, so neither
>    vop_plane_atomic_update() nor vop_crtc_atomic_flush() can queue a new
>    item, and vop_isr() returns early because
>    pm_runtime_get_if_in_use() fails.
>
>  - MSM mdp4/mdp5: msm_drm_uninit() destroys the KMS, which releases the
>    interrupt, before drm_dev_put() runs the drmm actions.  The flip
>    cleanup action is registered after drmm_crtc_alloc_with_planes(), so
>    it runs before drmm_crtc_cleanup() and after the vblank interrupt
>    that commits the work is gone.
>
> Rockchip VOP called drm_flip_work_cleanup() after drm_crtc_cleanup(),
> which memsets the CRTC.  Its worker calls drm_crtc_vblank_put() on that
> CRTC, so the drain has to happen before the CRTC is cleaned up.
>
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>

[Severity: Low]
Since this fixes the missing drain in drm_flip_work_cleanup() and the
cleanup ordering in Rockchip VOP, should it include Fixes: tags?

  Fixes: cabaafc78935 ("drm: add flip-work helper")
  Fixes: 47a7eb459777 ("drm/rockchip: Unreference framebuffers from flip work")

> diff --git a/drivers/gpu/drm/drm_flip_work.c b/drivers/gpu/drm/drm_flip_work.c
> index 52c437746d483..b8ce3966fa9ab 100644
> --- a/drivers/gpu/drm/drm_flip_work.c
> +++ b/drivers/gpu/drm/drm_flip_work.c
> @@ -149,10 +149,29 @@ EXPORT_SYMBOL(drm_flip_work_init);
>   * drm_flip_work_cleanup - cleans up flip-work
>   * @work: the flip-work to cleanup
>   *
> - * Destroy resources allocated for the flip-work
> + * Cancel the worker and run the tasks that are still queued, so that the
> + * resources they hold are released even if the work never made it to the
> + * workqueue.  Must be called before the object embedding @work is freed and
> + * after the driver has stopped the code that calls drm_flip_work_queue()
> + * and drm_flip_work_commit(): only the work items that exist when this
> + * function is called are drained.
>   */
>  void drm_flip_work_cleanup(struct drm_flip_work *work)
>  {
> -	WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
> +	struct drm_flip_task *task, *tmp;
> +	LIST_HEAD(tasks);
> +	unsigned long flags;
> +
> +	cancel_work_sync(&work->worker);
> +
> +	spin_lock_irqsave(&work->lock, flags);
> +	list_splice_tail_init(&work->queued, &tasks);
> +	list_splice_tail_init(&work->commited, &tasks);
> +	spin_unlock_irqrestore(&work->lock, flags);
> +
> +	list_for_each_entry_safe(task, tmp, &tasks, node) {
> +		work->func(work, task->data);
> +		kfree(task);
> +	}
>  }

[Severity: High]
Can calling work->func() here cause a NULL pointer dereference and
use-after-free in MSM mdp4 and mdp5 when an uncommitted cursor task
remains in unref_cursor_work.queued at teardown?

In msm_drm_uninit(), KMS is torn down and ddev->dev_private is cleared
before drm_dev_put() runs the drmm-registered mdp4_crtc_flip_cleanup() /
mdp5_crtc_flip_cleanup() actions:

drivers/gpu/drm/msm/msm_drv.c:msm_drm_uninit() {
    ...
	if (priv->kms)
		msm_drm_kms_uninit(dev);
    ...
	ddev->dev_private = NULL;
	drm_dev_put(ddev);
}

msm_drm_kms_uninit() flushes kms->wq (which only runs tasks already
committed by a vblank IRQ) and then calls mdp4_destroy() or
mdp5_kms_destroy(), which drops kms->vm via drm_gpuvm_put(kms->vm).

If a cursor update was queued via mdp4_crtc_cursor_set() or
mdp5_crtc_cursor_set() without a subsequent vblank interrupt to commit it,
the task stays in unref_cursor_work.queued until drm_dev_put() runs
drm_flip_work_cleanup().  That invokes unref_cursor_worker():

drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c:unref_cursor_worker() {
	struct mdp5_crtc *mdp5_crtc =
		container_of(work, struct mdp5_crtc, unref_cursor_work);
	struct mdp5_kms *mdp5_kms = get_kms(&mdp5_crtc->base);
	struct msm_kms *kms = &mdp5_kms->base.base;

	msm_gem_unpin_iova(val, kms->vm);
	drm_gem_object_put(val);
}

get_kms() reads crtc->dev->dev_private (now NULL) and dereferences
priv->kms, and kms->vm has already been destroyed (along with the
devm-allocated KMS structures if an open DRM fd delayed the final
drm_dev_put() past device unbind).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119023604.1495603-1-shunsheng1218@gmail.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down
  2026-11-19  2:36 [RFC PATCH] drm/flip: drain the flip work on cleanup Yaohan Li
  2026-11-19  2:51 ` sashiko-bot
@ 2026-11-19  8:31 ` Yaohan Li
  2026-11-19  8:31   ` [RFC PATCH v2 2/2] drm/flip: drain the flip work on cleanup Yaohan Li
  2026-11-19  8:48   ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down sashiko-bot
  1 sibling, 2 replies; 6+ messages in thread
From: Yaohan Li @ 2026-11-19  8:31 UTC (permalink / raw)
  To: Rob Clark, Dmitry Baryshkov, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann
  Cc: Abhinav Kumar, Jessica Zhang, Sean Paul, Marijn Suijten,
	David Airlie, Simona Vetter, Sandy Huang, Heiko Stübner,
	Andy Yan, linux-arm-msm, dri-devel, freedreno, linux-rockchip,
	linux-arm-kernel, linux-kernel

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RFC PATCH v2 2/2] drm/flip: drain the flip work on cleanup
  2026-11-19  8:31 ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down Yaohan Li
@ 2026-11-19  8:31   ` Yaohan Li
  2026-11-19  8:48   ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down sashiko-bot
  1 sibling, 0 replies; 6+ messages in thread
From: Yaohan Li @ 2026-11-19  8:31 UTC (permalink / raw)
  To: Rob Clark, Dmitry Baryshkov, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann
  Cc: Abhinav Kumar, Jessica Zhang, Sean Paul, Marijn Suijten,
	David Airlie, Simona Vetter, Sandy Huang, Heiko Stübner,
	Andy Yan, linux-arm-msm, dri-devel, freedreno, linux-rockchip,
	linux-arm-kernel, linux-kernel

drm_flip_work_cleanup() only warns when the queued or the committed list
is not empty.  Nothing cancels the worker and nothing runs the tasks
that are still queued, so the caller silently leaks whatever the tasks
hold (framebuffer references, vblank references, ...) and, if the
worker is still pending, it can run after the structure embedding the
flip work has been freed.

Cancel the worker and run the remaining tasks from
drm_flip_work_cleanup().  The work item is the only producer of the
committed list, so after cancel_work_sync() the two lists can be
spliced out and iterated without holding the lock.

The helper only drains the work items that exist when it is called, so
the callers have to make sure that no new item can be queued and that
the objects the tasks still use are alive:

 - Rockchip VOP: vop_unbind() unregisters the shared interrupt with
   devm_free_irq() before the runtime PM is disabled and before the CRTC
   and its flip work are cleaned up.  Disabling runtime PM is not a
   barrier: vop_isr() bails out when pm_runtime_get_if_in_use() fails, but
   that helper returns -EINVAL once runtime PM is disabled rather than a
   "not in use" value, and the line is shared, so another user can still
   get the handler invoked.  Removing it from the descriptor waits for the
   invocation that is running and keeps new ones from starting, which is
   what makes the drain that follows safe.  The bind error path that fails
   after the interrupt was requested frees it the same way, so every
   request is released exactly once.

 - MSM mdp4/mdp5: mdp4_destroy() and mdp5_kms_destroy() release the flip
   work from the KMS destroy path, after the interrupt and the KMS
   workqueue have been stopped and before drm_gpuvm_put() releases the
   VM the worker needs.  The DRM-managed action is removed there, so it
   cannot run later against a KMS that no longer exists.

Rockchip VOP called drm_flip_work_cleanup() after drm_crtc_cleanup(),
which memsets the CRTC.  Its worker calls drm_crtc_vblank_put() on that
CRTC, so the drain has to happen before the CRTC is cleaned up.

Fixes: cabaafc78935 ("drm: add flip-work helper")
Fixes: 47a7eb459777 ("drm/rockchip: Unreference framebuffers from flip work")
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>

---
Changes in v2:
  - Split the MSM preparation out into patch 1; the generic helper keeps
    draining the queued tasks here.
  - Add the Fixes: tags for the flip-work helper and for the Rockchip
    ordering.
  - Describe the MSM callers by the new KMS-destroy call site instead of
    the managed action, which now runs earlier.
  - Rockchip: drop the pm_runtime_get_if_in_use() change.  Unregister the
    shared interrupt before the runtime PM is disabled and before the CRTC
    is destroyed, and release it on the probe path that fails after the
    handler was registered.
  - 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_ROCKCHIP=m, 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 as a series against the target trees: both patches apply in order on
msm-next (d024361c631b) and on drm-misc-next (fac7a5f16230).

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/drm_flip_work.c             | 23 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c | 21 +++++++++++++++++--
 2 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/drm_flip_work.c b/drivers/gpu/drm/drm_flip_work.c
index 52c437746d483..b8ce3966fa9ab 100644
--- a/drivers/gpu/drm/drm_flip_work.c
+++ b/drivers/gpu/drm/drm_flip_work.c
@@ -149,10 +149,29 @@ EXPORT_SYMBOL(drm_flip_work_init);
  * drm_flip_work_cleanup - cleans up flip-work
  * @work: the flip-work to cleanup
  *
- * Destroy resources allocated for the flip-work
+ * Cancel the worker and run the tasks that are still queued, so that the
+ * resources they hold are released even if the work never made it to the
+ * workqueue.  Must be called before the object embedding @work is freed and
+ * after the driver has stopped the code that calls drm_flip_work_queue()
+ * and drm_flip_work_commit(): only the work items that exist when this
+ * function is called are drained.
  */
 void drm_flip_work_cleanup(struct drm_flip_work *work)
 {
-	WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
+	struct drm_flip_task *task, *tmp;
+	LIST_HEAD(tasks);
+	unsigned long flags;
+
+	cancel_work_sync(&work->worker);
+
+	spin_lock_irqsave(&work->lock, flags);
+	list_splice_tail_init(&work->queued, &tasks);
+	list_splice_tail_init(&work->commited, &tasks);
+	spin_unlock_irqrestore(&work->lock, flags);
+
+	list_for_each_entry_safe(task, tmp, &tasks, node) {
+		work->func(work, task->data);
+		kfree(task);
+	}
 }
 EXPORT_SYMBOL(drm_flip_work_cleanup);
diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_vop.c b/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
index 69e72727da24f..c10ad16147a54 100644
--- a/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
+++ b/drivers/gpu/drm/rockchip/rockchip_drm_vop.c
@@ -1990,12 +1990,19 @@ static void vop_destroy_crtc(struct vop *vop)
 				 head)
 		drm_plane_cleanup(plane);
 
+	/*
+	 * The flip work drops the vblank reference taken when the framebuffer
+	 * was queued, so it has to be drained while the CRTC is still valid.
+	 * The CRTC has been shut down and runtime PM disabled by the time this
+	 * runs, so no new work item can be queued anymore.
+	 */
+	drm_flip_work_cleanup(&vop->fb_unref_work);
+
 	/*
 	 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
 	 * references the CRTC.
 	 */
 	drm_crtc_cleanup(crtc);
-	drm_flip_work_cleanup(&vop->fb_unref_work);
 }
 
 static int vop_initial(struct vop *vop)
@@ -2259,7 +2266,7 @@ static int vop_bind(struct device *dev, struct device *master, void *data)
 		vop->rgb = rockchip_rgb_init(dev, &vop->crtc, vop->drm_dev, 0);
 		if (IS_ERR(vop->rgb)) {
 			ret = PTR_ERR(vop->rgb);
-			goto err_disable_pm_runtime;
+			goto err_free_irq;
 		}
 	}
 
@@ -2267,6 +2274,8 @@ static int vop_bind(struct device *dev, struct device *master, void *data)
 
 	return 0;
 
+err_free_irq:
+	devm_free_irq(dev, vop->irq, vop);
 err_disable_pm_runtime:
 	pm_runtime_disable(&pdev->dev);
 	vop_destroy_crtc(vop);
@@ -2280,6 +2289,14 @@ static void vop_unbind(struct device *dev, struct device *master, void *data)
 	if (vop->rgb)
 		rockchip_rgb_fini(vop->rgb);
 
+	/*
+	 * Unregister the shared handler before the runtime PM is disabled and
+	 * before the CRTC and its flip work are cleaned up: disabling runtime
+	 * PM does not keep vop_isr() from running, and the flip work the
+	 * handler commits has to be drained afterwards.
+	 */
+	devm_free_irq(dev, vop->irq, vop);
+
 	pm_runtime_disable(dev);
 	vop_destroy_crtc(vop);
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down
  2026-11-19  8:31 ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down Yaohan Li
  2026-11-19  8:31   ` [RFC PATCH v2 2/2] drm/flip: drain the flip work on cleanup Yaohan Li
@ 2026-11-19  8:48   ` sashiko-bot
  2026-11-19 11:18     ` Yaohan Li
  1 sibling, 1 reply; 6+ messages in thread
From: sashiko-bot @ 2026-11-19  8:48 UTC (permalink / raw)
  To: Yaohan Li; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/msm/mdp4: mdp4_kms->dev dereferenced before NULL check in
  mdp4_destroy()
--

commit 26c33f96a5c574b9e64b260a7b6a74a4cfcc0b65
Author: Yaohan Li <shunsheng1218@gmail.com>

drm/msm: release the cursor flip work before the KMS is torn down

This commit releases the DRM-managed cursor flip work cleanup action from
mdp4_destroy() and mdp5_kms_destroy() before the KMS VM is destroyed.

> 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);

[Severity: Medium]
Can the new NULL check on mdp4_kms->dev ever protect against a NULL
mdp4_kms->dev when mdp4_kms->dev->dev is dereferenced right at the top of
mdp4_destroy()?

In mdp4_kms_init(), mdp_kms_init() -> msm_kms_init() sets kms->funcs before
mdp4_kms->dev is assigned, so if msm_kms_init() fails afterwards,
msm_drm_kms_uninit() calls mdp4_destroy() with mdp4_kms->dev == NULL and
triggers a NULL pointer dereference before reaching the new guard (while
dev is only used later under if (mdp4_kms->rpm_enabled)).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119083133.3437277-1-shunsheng1218@gmail.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down
  2026-11-19  8:48   ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down sashiko-bot
@ 2026-11-19 11:18     ` Yaohan Li
  0 siblings, 0 replies; 6+ messages in thread
From: Yaohan Li @ 2026-11-19 11:18 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Rob Clark, Dmitry Baryshkov, dri-devel, linux-arm-msm,
	linux-kernel

Hi,

thanks for the review.  You are right, and the check is redundant in the
current mdp4 code: mdp4_destroy() dereferences mdp4_kms->dev at the top,
before the new guard.

The guard only protects the CRTC traversal this patch adds, and the v2
commit message says that it does not make mdp4_destroy() safe for every
partial-initialization state.  The existing problem belongs to the separate
KMS initialization error-handling series ("drm/msm: fix the error handling
of the KMS init"), which was not merged in the msm-next baseline used for
this RFC.

I would prefer to keep this change focused on the cursor flip-work
lifetime; if the check is more noise than it is worth, I can drop it in a
follow-up revision.

Thanks,
Yaohan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-19 11:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  2:36 [RFC PATCH] drm/flip: drain the flip work on cleanup Yaohan Li
2026-11-19  2:51 ` sashiko-bot
2026-11-19  8:31 ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down Yaohan Li
2026-11-19  8:31   ` [RFC PATCH v2 2/2] drm/flip: drain the flip work on cleanup Yaohan Li
2026-11-19  8:48   ` [RFC PATCH v2 1/2] drm/msm: release the cursor flip work before the KMS is torn down sashiko-bot
2026-11-19 11:18     ` Yaohan Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox