* [PATCH] drm/vc4: dsi: Allocate the encoder per DRM device
@ 2026-11-17 20:59 Henry Zhang
2026-11-18 10:09 ` Maxime Ripard
0 siblings, 1 reply; 2+ 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] 2+ 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-18 10:09 ` Maxime Ripard
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-18 10:10 UTC | newest]
Thread overview: 2+ 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-18 10:09 ` Maxime Ripard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox