All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device
@ 2026-11-17 20:59 Henry Zhang
  2026-11-17 21:14 ` sashiko-bot
  2026-11-18 10:09 ` Maxime Ripard
  0 siblings, 2 replies; 4+ messages in thread
From: Henry Zhang @ 2026-11-17 20:59 UTC (permalink / raw)
  To: mripard, dave.stevenson, mcanal
  Cc: kernel-list, maarten.lankhorst, tzimmermann, airlied, simona,
	dri-devel, linux-kernel

The DSI encoder is embedded in struct vc4_dsi, which lives as long as
the DSI platform device, but it is initialised with drmm_encoder_init()
and so is only cleaned up when the DRM device is released. The DRM
device can outlive the unbind of the vc4 components: unloading the DSI
panel driver detaches from the DSI host, which deletes the DSI component
and unbinds the whole vc4 DRM device, but a compositor that keeps the
DRM device open keeps it alive.

If the panel driver is loaded again before that, the new DRM device
binds the DSI component and calls drmm_encoder_init() on the encoder
that is still on the old DRM device's encoder list. The old device's
release later cleans it up from under the new one. On a Raspberry Pi 4
with a 6.18-based kernel, closing the old DRM device then gave:

  WARNING: drivers/gpu/drm/drm_encoder.c:214 at drmm_encoder_alloc_release+0x30/0x40 [drm]
  Call trace: drmm_encoder_alloc_release <- drm_dev_put <- drm_gem_dmabuf_release <- __fput
  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008
  pc : drm_mode_config_cleanup+0x5c/0x2f0 [drm]
  Call trace: drm_mode_config_cleanup <- drm_managed_release <- drm_dev_put

Allocate the encoder in bind with drmm_encoder_alloc(), so each DRM
device has its own and releases only its own. The DSI bridge is still
shared: while the old DRM device holds it, drm_bridge_attach() in the
new bind fails with -EBUSY, so binding again before the old DRM device
is released now fails cleanly instead of corrupting both devices.

The analysis, this change and this description were prepared with the
help of an AI coding assistant (Claude Code).

Fixes: 6f0ca506c30e ("drm/vc4: dsi: Switch to DRM-managed encoder initialization")
Signed-off-by: Henry Zhang <henryzhang62@gmail.com>
---
Testing:

- On this base (drm-misc-next 467ac0b56aeb): compile-tested only,
  arm64 defconfig with DRM_VC4=m, W=1, no new warnings.

- A backport to a downstream Raspberry Pi 6.18.34 kernel, on a
  Raspberry Pi 4 with an out-of-tree modular DSI panel driver: with the
  compositor stopped and a process holding /dev/dri/card1 open, unload
  the panel driver and load it again. The bind now fails cleanly:

    vc4-drm gpu: failed to bind fe700000.dsi (ops vc4_dsi_ops [vc4]): -16
    vc4-drm gpu: adev bind failed: -16

  the panel probe fails with -16, and there is no warning or oops,
  also when the holder then closes the old DRM device. Unloading and
  loading the panel driver again after that brings back the display
  and the compositor. Without the change, the same kernel gave the
  warning and oops above. The backport differs from this patch in two
  ways: that kernel still gets the panel bridge with
  drmm_of_get_bridge(), which has a use-after-free of its own in this
  sequence, so the test kernel used devm_drm_of_get_bridge() instead
  (not needed here, where the panel bridge is refcounted); and a
  downstream DSI restart worker that used the embedded encoder now
  follows a pointer to the current one.

The -EBUSY is final: the panel probe is not retried when the old DRM
device goes away, so the panel driver has to be loaded again. Would you
prefer the DSI host to wait for the old DRM device to be released
before adding its component again, or a bridge allocated per bind?

Not addressed: a connector probe that is already running when the
panel is removed can still call into the panel driver.

This applies to drm-misc-next on its own. It also applies on top of my
"drm/vc4: dsi: Serialize register writes through the DMA workaround",
and it does not touch the code changed by my vc4 DSI series
<20261028164539.3471833-1-henryzhang62@gmail.com>.

Tools: Claude Code (an AI coding assistant) analysed the crash logs and
the vc4/DRM lifetimes, located the introducing commit, wrote the change
and this description, built and checkpatch-checked it, and ran the
backport test on the board, at my request.

 drivers/gpu/drm/vc4/vc4_dsi.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/vc4/vc4_dsi.c b/drivers/gpu/drm/vc4/vc4_dsi.c
index ec008f766..5bd8f579b 100644
--- a/drivers/gpu/drm/vc4/vc4_dsi.c
+++ b/drivers/gpu/drm/vc4/vc4_dsi.c
@@ -546,9 +546,18 @@ struct vc4_dsi_variant {
 
 };
 
+/*
+ * The encoder of one DRM device. The DRM device can outlive the unbind of
+ * this component, so a later bind must not reuse the encoder of an earlier
+ * one: allocate one per bind, managed by the DRM device.
+ */
+struct vc4_dsi_encoder {
+	struct vc4_encoder base;
+	struct vc4_dsi *dsi;
+};
+
 /* General DSI hardware state. */
 struct vc4_dsi {
-	struct vc4_encoder encoder;
 	struct mipi_dsi_host dsi_host;
 
 	struct platform_device *pdev;
@@ -601,7 +610,7 @@ struct vc4_dsi {
 	container_of_const(host, struct vc4_dsi, dsi_host)
 
 #define to_vc4_dsi(_encoder)					\
-	container_of_const(_encoder, struct vc4_dsi, encoder.base)
+	(container_of_const(_encoder, struct vc4_dsi_encoder, base.base)->dsi)
 
 #define bridge_to_vc4_dsi(_bridge)				\
 	container_of_const(_bridge, struct vc4_dsi, bridge)
@@ -1626,7 +1635,8 @@ static int vc4_dsi_bind(struct device *dev, struct device *master, void *data)
 	struct platform_device *pdev = to_platform_device(dev);
 	struct drm_device *drm = dev_get_drvdata(master);
 	struct vc4_dsi *dsi = dev_get_drvdata(dev);
-	struct drm_encoder *encoder = &dsi->encoder.base;
+	struct vc4_dsi_encoder *dsi_encoder;
+	struct drm_encoder *encoder;
 	int ret;
 
 	drm_bridge_get(&dsi->bridge);
@@ -1637,9 +1647,6 @@ static int vc4_dsi_bind(struct device *dev, struct device *master, void *data)
 
 	dsi->variant = of_device_get_match_data(dev);
 
-	dsi->encoder.type = dsi->variant->port ?
-		VC4_ENCODER_TYPE_DSI1 : VC4_ENCODER_TYPE_DSI0;
-
 	dsi->regs = vc4_ioremap_regs(pdev, 0);
 	if (IS_ERR(dsi->regs))
 		return PTR_ERR(dsi->regs);
@@ -1758,12 +1765,16 @@ static int vc4_dsi_bind(struct device *dev, struct device *master, void *data)
 	if (ret)
 		return ret;
 
-	ret = drmm_encoder_init(drm, encoder,
-				&vc4_dsi_encoder_funcs,
-				DRM_MODE_ENCODER_DSI,
-				NULL);
-	if (ret)
-		return ret;
+	dsi_encoder = drmm_encoder_alloc(drm, struct vc4_dsi_encoder, base.base,
+					 &vc4_dsi_encoder_funcs,
+					 DRM_MODE_ENCODER_DSI, NULL);
+	if (IS_ERR(dsi_encoder))
+		return PTR_ERR(dsi_encoder);
+
+	dsi_encoder->dsi = dsi;
+	dsi_encoder->base.type = dsi->variant->port ?
+		VC4_ENCODER_TYPE_DSI1 : VC4_ENCODER_TYPE_DSI0;
+	encoder = &dsi_encoder->base.base;
 
 	ret = devm_pm_runtime_enable(dev);
 	if (ret)

base-commit: 467ac0b56aebc804360f276feffbc9032c265a6e
-- 
2.47.3


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

* Re: [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device
  2026-11-17 20:59 [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device Henry Zhang
@ 2026-11-17 21:14 ` sashiko-bot
  2026-11-17 23:51   ` Henry Zhang
  2026-11-18 10:09 ` Maxime Ripard
  1 sibling, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-17 21:14 UTC (permalink / raw)
  To: Henry Zhang; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] drm/vc4: dsi: unmanaged OF clock provider in
  vc4_dsi_init_phy_clocks() [1]
--

commit 809765ced535c89f7b7aa0f22cdfc9aa61cc56b3
Author: Henry Zhang <henryzhang62@gmail.com>

drm/vc4: dsi: Allocate the encoder per DRM device

This commit moves the VC4 DSI encoder out of struct vc4_dsi into a
DRM-managed struct vc4_dsi_encoder allocated in vc4_dsi_bind() so that
re-binding the DSI component while an older DRM device is still open does
not reuse or corrupt the old encoder.

> diff --git a/drivers/gpu/drm/vc4/vc4_dsi.c b/drivers/gpu/drm/vc4/vc4_dsi.c
> index ec008f766dd5b..5bd8f579b515a 100644
> --- a/drivers/gpu/drm/vc4/vc4_dsi.c
> +++ b/drivers/gpu/drm/vc4/vc4_dsi.c

[ ... ]

> @@ -1758,12 +1765,16 @@ static int vc4_dsi_bind(struct device *dev, struct device *master, void *data)
>  	if (ret)
>  		return ret;
>  
> -	ret = drmm_encoder_init(drm, encoder,
> -				&vc4_dsi_encoder_funcs,
> -				DRM_MODE_ENCODER_DSI,
> -				NULL);
> -	if (ret)
> -		return ret;
> +	dsi_encoder = drmm_encoder_alloc(drm, struct vc4_dsi_encoder, base.base,
> +					 &vc4_dsi_encoder_funcs,
> +					 DRM_MODE_ENCODER_DSI, NULL);
> +	if (IS_ERR(dsi_encoder))
> +		return PTR_ERR(dsi_encoder);
> +
> +	dsi_encoder->dsi = dsi;
> +	dsi_encoder->base.type = dsi->variant->port ?
> +		VC4_ENCODER_TYPE_DSI1 : VC4_ENCODER_TYPE_DSI0;
> +	encoder = &dsi_encoder->base.base;
>  
>  	ret = devm_pm_runtime_enable(dev);
>  	if (ret)

[Severity: Critical]
This problem wasn't introduced by this patch, but does
vc4_dsi_init_phy_clocks() just above this hunk leave a dangling OF clock
provider behind [1] when vc4_dsi_bind() later fails or the component
is unbound?

drivers/gpu/drm/vc4/vc4_dsi.c:vc4_dsi_init_phy_clocks() {
	dsi->clk_onecell = devm_kzalloc(dev, ...);
	...
	return of_clk_add_hw_provider(dev->of_node,
				      of_clk_hw_onecell_get,
				      dsi->clk_onecell);
}

Because vc4_dsi_init_phy_clocks() registers the provider with the unmanaged
of_clk_add_hw_provider() instead of devm_of_clk_add_hw_provider(),
unbinding the component (or failing later in vc4_dsi_bind(), such as when
drm_bridge_attach() returns -EBUSY) frees the devres-managed
dsi->clk_onecell while the provider stays on of_clk_providers, causing a
use-after-free in of_clk_hw_onecell_get() on subsequent clock lookups.

[1] https://sashiko.dev/#/bug/linux-4c5f4ec7-e4cd-4631-aa06-37e8551cebec

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

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

* Re: [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device
  2026-11-17 21:14 ` sashiko-bot
@ 2026-11-17 23:51   ` Henry Zhang
  0 siblings, 0 replies; 4+ messages in thread
From: Henry Zhang @ 2026-11-17 23:51 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel

On Tue, Nov 17, 2026 at 1:14 PM <sashiko-bot@kernel.org> wrote:
> [Severity: Critical]
> This problem wasn't introduced by this patch, but does
> vc4_dsi_init_phy_clocks() just above this hunk leave a dangling OF clock
> provider behind [1] when vc4_dsi_bind() later fails or the component
> is unbound?
Thanks. This is pre-existing and independent of this patch; I sent a
  separate fix:

    [PATCH] drm/vc4: dsi: Register the PHY clock provider with devm
    https://lore.kernel.org/all/20261117234107.1298575-1-henryzhang62@gmail.com/

  It confirms the stale provider after unbind and after a failed bind at
  runtime; the use-after-free lookup itself was not reproduced on my
  Pi 4, since no clock lookup on the DSI node happened in the tests.

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

* Re: [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device
  2026-11-17 20:59 [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device Henry Zhang
  2026-11-17 21:14 ` sashiko-bot
@ 2026-11-18 10:09 ` Maxime Ripard
  1 sibling, 0 replies; 4+ messages in thread
From: Maxime Ripard @ 2026-11-18 10:09 UTC (permalink / raw)
  To: Henry Zhang
  Cc: dave.stevenson, mcanal, kernel-list, maarten.lankhorst,
	tzimmermann, airlied, simona, dri-devel, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2060 bytes --]

On Tue, Nov 17, 2026 at 12:59:02PM -0800, Henry Zhang wrote:
> The DSI encoder is embedded in struct vc4_dsi, which lives as long as
> the DSI platform device

That's not true.

It's allocated with devm_drm_bridge_alloc() that will *not* free it in
remove but give away its reference there.

We have another reference taken in vc4_dsi_bind, that is only given up
through a drm-managed action and thus will only be freed when the DRM
device is destroyed.

> but it is initialised with drmm_encoder_init() and so is only cleaned
> up when the DRM device is released. The DRM device can outlive the
> unbind of the vc4 components: unloading the DSI panel driver detaches
> from the DSI host, which deletes the DSI component and unbinds the
> whole vc4 DRM device, but a compositor that keeps the DRM device open
> keeps it alive.

All vc4 components should be tied to the DRM device lifetime so that's
not generally correct either. Something might have snuck back in though.

Also, generally speaking, unloading panel drivers is unsupported at this
point. It's a work in progress, but requires more changes to be safe.

> If the panel driver is loaded again before that, the new DRM device
> binds the DSI component and calls drmm_encoder_init() on the encoder
> that is still on the old DRM device's encoder list. The old device's
> release later cleans it up from under the new one. On a Raspberry Pi 4
> with a 6.18-based kernel, closing the old DRM device then gave:
> 
>   WARNING: drivers/gpu/drm/drm_encoder.c:214 at drmm_encoder_alloc_release+0x30/0x40 [drm]
>   Call trace: drmm_encoder_alloc_release <- drm_dev_put <- drm_gem_dmabuf_release <- __fput
>   Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008
>   pc : drm_mode_config_cleanup+0x5c/0x2f0 [drm]
>   Call trace: drm_mode_config_cleanup <- drm_managed_release <- drm_dev_put

This is surprising though. I'd like to see a detailed analysis of what's
actually going on on mainline because it doesn't look right.

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 20:59 [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device Henry Zhang
2026-11-17 21:14 ` sashiko-bot
2026-11-17 23:51   ` Henry Zhang
2026-11-18 10:09 ` Maxime Ripard

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.