Skip to contentSkip to conversation

drm/mipi-dsi: Use put_device() on device_add() failure

PATCH New Yuho Choi 2 participants 2 messages Jun 15

Subsystems (2)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 drm/mipi-dsi: Use put_device() on device_add() failure

mipi_dsi_device_alloc() initializes the embedded struct device before mipi_dsi_device_register_full() calls mipi_dsi_device_add(). If device_add() fails, the code frees the DSI object directly with kfree(), bypassing the device release callback.

Drop the initialized device reference with put_device() instead, so mipi_dsi_dev_release() runs and releases resources owned by the device, including the OF node reference.

Fixes 068a00233969 ("drm: Add MIPI DSI bus support")
---
 drivers/gpu/drm/drm_mipi_dsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_mipi_dsi.c +1 -1
index 0390e14d3157..862d56eaf934 100644
@@ -233,7 +233,7 @@ mipi_dsi_device_register_full(struct mipi_dsi_host *host,
 	ret = mipi_dsi_device_add(dsi);
 	if (ret) {
 		dev_err(host->dev, "failed to add DSI device %d\n", ret);
-		kfree(dsi);
+		put_device(&dsi->dev);
 		return ERR_PTR(ret);
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

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

New issues:
- [High] The patch introduces an inconsistent API contract for ownership of the info->node reference in mipi_dsi_device_register_full(). By using put_device() on device_add() failure, it now consumes the reference (via mipi_dsi_dev_release()) on that specific error path. However, on earlier error paths (like invalid channel or mipi_dsi_device_alloc() failure), it returns an error pointer without dropping the reference. Because the function returns an opaque ERR_PTR on all failures, the caller cannot know whether it still owns the reference, making it structurally impossible for callers to safely handle errors without risking a double-put or memory leak.

Pre-existing issues:
- [High] Double-put of OF node in rm69380_probe() due to the API contract of mipi_dsi_device_register_full(). --

commit d11ddeaad1a55c59a2b2250f973cf5d648093cee
Author: Yuho Choi <[email protected]>

drm/mipi-dsi: Use put_device() on device_add() failure

mipi_dsi_device_alloc() initializes the embedded struct device before mipi_dsi_device_register_full() calls mipi_dsi_device_add(). If device_add() fails, the code frees the DSI object directly with kfree(), bypassing the device release callback.

Drop the initialized device reference with put_device() instead, so mipi_dsi_dev_release() runs and releases resources owned by the device, including the OF node reference.

Quoted 4 lines
drivers/gpu/drm/drm_mipi_dsi.c +0 -0
index 3ac1dd5ad640a..1b4c0acbd30d2 100644

[ ... ]

drivers/gpu/drm/drm_mipi_dsi.c mipi_dsi_device_register_full(struct mipi_dsi_host *host, · line 236

Quoted 6 lines
drivers/gpu/drm/drm_mipi_dsi.c +1 -1
@@ -233,7 +233,7 @@ mipi_dsi_device_register_full(struct mipi_dsi_host *host,
 	ret = mipi_dsi_device_add(dsi);
 	if (ret) {
 		dev_err(host->dev, "failed to add DSI device %d\n", ret);
-		kfree(dsi);
+		put_device(&dsi->dev);

[Severity: High]
Does this introduce an inconsistent API contract for ownership of the info->node reference?

Earlier in mipi_dsi_device_register_full(), if mipi_dsi_device_alloc() fails, the function returns ERR_PTR without dropping the reference:

    dsi = mipi_dsi_device_alloc(host);
    if (IS_ERR(dsi)) {
        dev_err(host->dev, "failed to allocate DSI device %ld\n",
                PTR_ERR(dsi));
        return dsi;
    }

By using put_device() here, this specific error path now consumes the reference via mipi_dsi_dev_release().

Since the caller receives an opaque ERR_PTR in both failure cases, they have no way to distinguish whether the reference was consumed or not, making it structurally impossible to safely handle errors without risking a double-put or a memory leak.

[Severity: High]
This isn't a bug introduced by this patch, but does this API contract lead to a double-put of the OF node in callers like rm69380_probe()?

In drivers/gpu/drm/panel/panel-raydium-rm69380.c, rm69380_probe() calls of_node_put() explicitly before passing the node to the register function:

    of_node_put(dsi_sec);
    if (!dsi_sec_host)
        return dev_err_probe(dev, -EPROBE_DEFER,
                             "Cannot get secondary DSI host\n");

    ctx->dsi[1] =
        devm_mipi_dsi_device_register_full(dev, dsi_sec_host, &info);

Because mipi_dsi_device_register_full() assigns the node without acquiring its own reference, and mipi_dsi_dev_release() calls of_node_put(dev->of_node) when the device is released (or when device_add() fails via this patch), this results in a use-after-free and double-put of the OF node.

Quoted 3 lines
drivers/gpu/drm/drm_mipi_dsi.c +0 -0
 		return ERR_PTR(ret);
 	}
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1