Skip to contentSkip to conversation

drm/rockchip: rk3066_hdmi: convert to devm_drm_bridge_alloc() API

PATCH Superseded Johan Jonker 3 participants 5 messages Aug 24 – Sep 3

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/rockchip: rk3066_hdmi: convert to devm_drm_bridge_alloc() API

From: Hüseyin BIYIK <[email protected]>

The function devm_drm_bridge_alloc() is the new API for allocating DRM bridges. This conversion was missed during the initial conversion of all bridges to the new API.

---
 drivers/gpu/drm/rockchip/rk3066_hdmi.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/rockchip/rk3066_hdmi.c +4 -4
index f28d6a883e09..39c65ee07caa 100644
@@ -703,7 +703,6 @@ rk3066_hdmi_register(struct drm_device *drm, struct rk3066_hdmi *hdmi)
 			 DRM_MODE_ENCODER_TMDS, NULL);
 
 	hdmi->bridge.driver_private = hdmi;
-	hdmi->bridge.funcs = &rk3066_hdmi_bridge_funcs;
 	hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT |
 			   DRM_BRIDGE_OP_EDID |
 			   DRM_BRIDGE_OP_HDMI |
@@ -747,9 +746,10 @@ static int rk3066_hdmi_bind(struct device *dev, struct device *master,
 	int irq;
 	int ret;
 
-	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
-	if (!hdmi)
-		return -ENOMEM;
+	hdmi = devm_drm_bridge_alloc(dev, struct rk3066_hdmi, bridge,
+				     &rk3066_hdmi_bridge_funcs);
+	if (IS_ERR(hdmi))
+		return PTR_ERR(hdmi);
 
 	hdmi->dev = dev;
 	hdmi->drm_dev = drm;

---
base-commit: 2709dd5ae32f0828f386327c76bba9f39f63a1c6
change-id: 20260824-drm-e4689f65ab8c

Best regards,

Signature · 1 line
Johan Jonker <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but there is a Use-After-Free vulnerability on the error path of rk3066_hdmi_bind(). If rk3066_hdmi_register() fails, the code jumps to err_disable_hclk, skipping the encoder cleanup label err_cleanup_hdmi. Since the DRM encoder was added to the DRM device's list during drm_encoder_init() but is not removed, and the hdmi struct containing the encoder is freed by devres upon bind failure, the DRM core retains a dangling pointer. This will cause a Use-After-Free when the DRM core later iterates over the encoder list or during teardown. --

--- Patch [1]: [PATCH v2] drm/rockchip: rk3066_hdmi: convert to devm_drm_bridge_alloc() API --- commit bdad67e77f8f693946bd674b1a40ce7af3ce54c3
Author: Hüseyin BIYIK <[email protected]>

drm/rockchip: rk3066_hdmi: convert to devm_drm_bridge_alloc() API

This commit converts the rk3066_hdmi driver to use devm_drm_bridge_alloc() for allocating DRM bridges. This conversion was missed during the initial conversion of all bridges to the new API.

Quoted 4 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
index f28d6a883e099..39c65ee07caac 100644

[ ... ]

drivers/gpu/drm/rockchip/rk3066_hdmi.c static int rk3066_hdmi_bind(struct device *dev, struct device *master, · line 752

Quoted 11 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +4 -3
@@ -747,9 +746,10 @@ static int rk3066_hdmi_bind(struct device *dev, struct device *master,
 	int irq;
 	int ret;
 
-	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
-	if (!hdmi)
-		return -ENOMEM;
+	hdmi = devm_drm_bridge_alloc(dev, struct rk3066_hdmi, bridge,
+				     &rk3066_hdmi_bridge_funcs);
+	if (IS_ERR(hdmi))
+		return PTR_ERR(hdmi);

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the allocation of the hdmi struct in rk3066_hdmi_bind() inside drivers/gpu/drm/rockchip/rk3066_hdmi.c, I noticed a potential use-after-free on the error path.

If rk3066_hdmi_register() fails after drm_encoder_init() has been called, the error path in rk3066_hdmi_bind() jumps to err_disable_hclk:

	ret = rk3066_hdmi_register(drm, hdmi);
	if (ret)
		goto err_disable_hclk;

This bypasses the err_cleanup_hdmi label which destroys the encoder:

err_cleanup_hdmi:

	hdmi->encoder.encoder.funcs->destroy(&hdmi->encoder.encoder);
err_disable_hclk:
	clk_disable_unprepare(hdmi->hclk);

Since the encoder was added to the DRM device's internal lists during drm_encoder_init(), and the devm-allocated hdmi struct will be freed when rk3066_hdmi_bind() returns an error, does this leave a dangling pointer in the DRM core that will cause a use-after-free when the list is iterated or torn down?

Quoted 3 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
 	hdmi->dev = dev;
 	hdmi->drm_dev = drm;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Johan Jonker

Am Montag, 24. August 2026, 12:21:19 Mitteleuropäische Sommerzeit schrieb Johan Jonker via B4 Relay:

Quoted 25 lines

From: Hüseyin BIYIK <[email protected]>

The function devm_drm_bridge_alloc() is the new API for allocating DRM bridges. This conversion was missed during the initial conversion of all bridges to the new API.

---
 drivers/gpu/drm/rockchip/rk3066_hdmi.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -1
index f28d6a883e09..39c65ee07caa 100644
@@ -703,7 +703,6 @@ rk3066_hdmi_register(struct drm_device *drm, struct rk3066_hdmi *hdmi)
 			 DRM_MODE_ENCODER_TMDS, NULL);
 
 	hdmi->bridge.driver_private = hdmi;
-	hdmi->bridge.funcs = &rk3066_hdmi_bridge_funcs;
 	hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT |
 			   DRM_BRIDGE_OP_EDID |
 			   DRM_BRIDGE_OP_HDMI |

drivers/gpu/drm/rockchip/rk3066_hdmi.c static int rk3066_hdmi_bind(struct device *dev, struct device *master, · line 752

Quoted 10 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +4 -3
@@ -747,9 +746,10 @@ static int rk3066_hdmi_bind(struct device *dev, struct device *master,
 	int irq;
 	int ret;
 
-	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
-	if (!hdmi)
-		return -ENOMEM;
+	hdmi = devm_drm_bridge_alloc(dev, struct rk3066_hdmi, bridge,
+				     &rk3066_hdmi_bridge_funcs);
+	if (IS_ERR(hdmi))
+		return PTR_ERR(hdmi);

If I'm reading things correctly, the parts in rk3066_hdmi_register setting hdmi->bridge.driver_private (should use the container field of struct drm_bridge instead) and hdmi->bridge.funcs are redundant by this change?

Heiko

Quoted 10 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -1
 	hdmi->dev = dev;
 	hdmi->drm_dev = drm;
 
---

base-commit: 2709dd5ae32f0828f386327c76bba9f39f63a1c6
change-id: 20260824-drm-e4689f65ab8c

Best regards,

#3 lore raw

Hi,

Quoted 22 lines · Heiko Stübner

Am Montag, 24. August 2026, 12:21:19 Mitteleuropäische Sommerzeit schrieb Johan Jonker via B4 Relay:

Quoted 21 lines

From: Hüseyin BIYIK <[email protected]>

The function devm_drm_bridge_alloc() is the new API for allocating DRM bridges. This conversion was missed during the initial conversion of all bridges to the new API.

---
 drivers/gpu/drm/rockchip/rk3066_hdmi.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
index f28d6a883e09..39c65ee07caa 100644
@@ -703,7 +703,6 @@ rk3066_hdmi_register(struct drm_device *drm, struct rk3066_hdmi *hdmi)
 			 DRM_MODE_ENCODER_TMDS, NULL);
 
 
 	hdmi->bridge.driver_private = hdmi;

Can this be removed?
See link/comment below.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -1
-	hdmi->bridge.funcs = &rk3066_hdmi_bridge_funcs;
 	hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT |
 			   DRM_BRIDGE_OP_EDID |
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
 			   DRM_BRIDGE_OP_HDMI |
@@ -747,9 +746,10 @@ static int rk3066_hdmi_bind(struct device *dev, struct device *master,
 	int irq;
 	int ret;
 
Quoted 12 lines

drivers/gpu/drm/rockchip/rk3066_hdmi.c static int rk3066_hdmi_bind(struct device *dev, struct device *master, · line 752

Quoted 7 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +4 -3
-	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
-	if (!hdmi)
-		return -ENOMEM;
+	hdmi = devm_drm_bridge_alloc(dev, struct rk3066_hdmi, bridge,
+				     &rk3066_hdmi_bridge_funcs);
+	if (IS_ERR(hdmi))
+		return PTR_ERR(hdmi);

If I'm reading things correctly, the parts in rk3066_hdmi_register setting hdmi->bridge.driver_private (should use the container field of struct drm_bridge instead)

The driver_private line was added with this patch:

[PATCH] drm/rockchip: rk3066_hdmi: switch to drm bridge
https://lore.kernel.org/all/[email protected]/

and hdmi->bridge.funcs are redundant by this change?

Not redundant, just funcs are attached in a common function __devm_drm_bridge_alloc()

https://elixir.bootlin.com/linux/v7.2.2/source/drivers/gpu/drm/drm_bridge.c#L383

void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
const struct drm_bridge_funcs *funcs)

{
	void *container;
	struct drm_bridge *bridge;
	int err;

	if (!funcs) {
		dev_warn(dev, "Missing funcs pointer\n");
		return ERR_PTR(-EINVAL);
	}

	container = kzalloc(size, GFP_KERNEL);
	if (!container)
		return ERR_PTR(-ENOMEM);

	bridge = container + offset;
	INIT_LIST_HEAD(&bridge->list);
	bridge->container = container;
	bridge->funcs = funcs;
	kref_init(&bridge->refcount);

	err = devm_add_action_or_reset(dev, drm_bridge_put_void, bridge);
	if (err)
		return ERR_PTR(err);

	return container;
}

EXPORT_SYMBOL(__devm_drm_bridge_alloc);

===========

Other example from sti:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

It looks like you don't set bridge->driver_private anymore. Is it on purpose?

This looks correct to me. In current code, driver_private is used to hold a pointer to the driver private struct (struct analogix_dp_device). With devm_drm_bridge_alloc() container_of() is now enough, no pointer is needed. With the patch applied, driver_private becomes unused.

Then we should remove it from the structure if it's unused.

drm/sti: hdmi: convert to devm_drm_bridge_alloc() API
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac4531424d907f3983e919a7bda2b90ea0cede4f

============

This serie was missing a few bridges too many.

drm: convert all bridges to devm_drm_bridge_alloc()
https://patchwork.freedesktop.org/series/148229/

====

Please advise what changes are needed?
RK3066_hdmi is broken since somewhere 2025-05.

Johan

Quoted 20 lines

Heiko

Quoted 10 lines
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -1
 	hdmi->dev = dev;
 	hdmi->drm_dev = drm;
 
---

base-commit: 2709dd5ae32f0828f386327c76bba9f39f63a1c6
change-id: 20260824-drm-e4689f65ab8c

Best regards,

maintainer
#4 lore raw

Hi Johan,

Am Donnerstag, 3. September 2026, 22:44:57 Mitteleuropäische Sommerzeit schrieb Johan Jonker:

Quoted 22 lines · Heiko Stübner
Quoted 20 lines

Am Montag, 24. August 2026, 12:21:19 Mitteleuropäische Sommerzeit schrieb Johan Jonker via B4 Relay:

Quoted 19 lines

From: Hüseyin BIYIK <[email protected]>

The function devm_drm_bridge_alloc() is the new API for allocating DRM bridges. This conversion was missed during the initial conversion of all bridges to the new API.

---
 drivers/gpu/drm/rockchip/rk3066_hdmi.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
index f28d6a883e09..39c65ee07caa 100644
@@ -703,7 +703,6 @@ rk3066_hdmi_register(struct drm_device *drm, struct rk3066_hdmi *hdmi)
 			 DRM_MODE_ENCODER_TMDS, NULL);
 
Quoted 4 lines
Quoted 1 line

drivers/gpu/drm/rockchip/rk3066_hdmi.c rk3066_hdmi_register(struct drm_device *drm, struct rk3066_hdmi *hdmi) · line 705

Quoted 1 line
drivers/gpu/drm/rockchip/rk3066_hdmi.c +0 -0
 	hdmi->bridge.driver_private = hdmi;

Can this be removed?
See link/comment below.

that is what I meant. bridge.container holds the same information and bridge.private_data is not used in the rk3066_hdmi driver at all.

So if some future function needs to access the struct rk3066_hdmi it can do so via the container field.

Quoted 3 lines

and hdmi->bridge.funcs are redundant by this change?

Not redundant, just funcs are attached in a common function __devm_drm_bridge_alloc()

that's exactly wat I meant. As they are attached already in the common function, there is no need to do it _again_ in rk3066_hdmi_register()

Quoted 3 lines

Please advise what changes are needed?
RK3066_hdmi is broken since somewhere 2025-05.

So both should be dropped.

Heiko