Skip to contentSkip to conversation

drm/vc4: Fix firmware reference leak in vc4_hvs_bind()

PATCH In review Wentao Liang 3 participants 6 messages Sep 16 – Nov 9

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/vc4: Fix firmware reference leak in vc4_hvs_bind()

The firmware reference taken via rpi_firmware_get() is leaked when either of the devm_clk_get() calls for the core or display clocks fails, as both error paths return without releasing it.

Put the firmware reference before returning from both clock lookup error paths.

Fixes 2a001ca00ad5 ("drm/vc4: hdmi: Rework hdmi_enable_4kp60 detection code")
---
 drivers/gpu/drm/vc4/vc4_hvs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/vc4/vc4_hvs.c +2 -0
index ee8d0738501b..20dde4150068 100644
@@ -1680,6 +1680,7 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 					     (vc4->gen >= VC4_GEN_6_C) ? "core" : NULL);
 		if (IS_ERR(hvs->core_clk)) {
 			dev_err(&pdev->dev, "Couldn't get core clock\n");
+			rpi_firmware_put(firmware);
 			return PTR_ERR(hvs->core_clk);
 		}
 
@@ -1687,6 +1688,7 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 					     (vc4->gen >= VC4_GEN_6_C) ? "disp" : NULL);
 		if (IS_ERR(hvs->disp_clk)) {
 			dev_err(&pdev->dev, "Couldn't get disp clock\n");
+			rpi_firmware_put(firmware);
 			return PTR_ERR(hvs->disp_clk);
 		}
 
Signature · 1 line
2.34.1
reviewer
#1 lore raw

Hi Wentao,

Quoted 19 lines · Wentao Liang

The firmware reference taken via rpi_firmware_get() is leaked when either of the devm_clk_get() calls for the core or display clocks fails, as both error paths return without releasing it.

Put the firmware reference before returning from both clock lookup error paths.

Fixes 2a001ca00ad5 ("drm/vc4: hdmi: Rework hdmi_enable_4kp60 detection code")
---
  drivers/gpu/drm/vc4/vc4_hvs.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/vc4/vc4_hvs.c +0 -0
index ee8d0738501b..20dde4150068 100644

drivers/gpu/drm/vc4/vc4_hvs.c static int vc4_hvs_bind(struct device *dev, struct device *master, void *data) · line 1683

Quoted 4 lines
drivers/gpu/drm/vc4/vc4_hvs.c +1 -0
@@ -1680,6 +1680,7 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
  					     (vc4->gen >= VC4_GEN_6_C) ? "core" : NULL);
  		if (IS_ERR(hvs->core_clk)) {
  			dev_err(&pdev->dev, "Couldn't get core clock\n");
+			rpi_firmware_put(firmware);

Instead of adding this line twice, move the line firmware = rpi_firmware_get(node) closer to its actual use. Then, you don't need to add these rpi_firmware_put() calls.

The same comment applies to your other PATCH: [PATCH] drm/vc4: Fix firmware reference leak in vc4_drm_bind().

Best regards,
- Maíra

Quoted 11 lines
drivers/gpu/drm/vc4/vc4_hvs.c +1 -0
  			return PTR_ERR(hvs->core_clk);
  		}
 
@@ -1687,6 +1688,7 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
  					     (vc4->gen >= VC4_GEN_6_C) ? "disp" : NULL);
  		if (IS_ERR(hvs->disp_clk)) {
  			dev_err(&pdev->dev, "Couldn't get disp clock\n");
+			rpi_firmware_put(firmware);
  			return PTR_ERR(hvs->disp_clk);
  		}
 
#2 lore raw

Hi Maíra,

Sorry for the very late reply, and thanks for the review.

You are right, and your suggestion is better than what I had. Moving the firmware lookup below the clock lookups keeps the reference close to its use, and the two rpi_firmware_put() calls in the error paths are not needed anymore. The rpi_firmware_find_node() call moves with it, since the device node reference it returns would otherwise be leaked by those same error paths. I am sending v2 with that change.

About the other patch, "drm/vc4: Fix firmware reference leak in vc4_drm_bind()": the same change would work there, since the only thing between the firmware lookup and rpi_firmware_property() is aperture_remove_all_conflicting_devices(), which does not use the reference. That patch was already applied as b816da5d5df1 before this comment, so it is in the tree as posted, with the extra rpi_firmware_put() in the aperture error path.

Thanks,
Wentao

#3 lore raw

PATCH 1/1 drm/vc4: Fix firmware reference leak in vc4_hvs_bind()

In reply to Maíra Canal

The firmware reference taken via rpi_firmware_get() is leaked when either of the devm_clk_get() calls for the core or display clocks fails, as both error paths return without releasing it.

Fix it by moving the firmware lookup below the clock lookups, so that the reference is taken right before the firmware property is read and released immediately afterwards. The rpi_firmware_find_node() call moves with it: the device node reference it returns would otherwise be leaked by those same error paths.

The candidate was found by our static analysis tooling, and this change was prepared with assistance from DeepSeek-V4.1-Flash and GPT-6.1-Sol.

Fixes 2a001ca00ad5 ("drm/vc4: hdmi: Rework hdmi_enable_4kp60 detection code")

---
Changes in v2:
- Sorry for the very late reply, and thanks for the review.
- Move the firmware and device node lookups below the clock lookups instead of adding rpi_firmware_put() to the two error paths, as suggested by Maíra.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/vc4/vc4_hvs.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/vc4/vc4_hvs.c +9 -9
index b1fb096858cc..537aaa992129 100644
@@ -1677,15 +1677,6 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 		struct device_node *node;
 		unsigned int max_rate;
 
-		node = rpi_firmware_find_node();
-		if (!node)
-			return -EINVAL;
-
-		firmware = rpi_firmware_get(node);
-		of_node_put(node);
-		if (!firmware)
-			return -EPROBE_DEFER;
-
 		hvs->core_clk = devm_clk_get(&pdev->dev,
 					     (vc4->gen >= VC4_GEN_6_C) ? "core" : NULL);
 		if (IS_ERR(hvs->core_clk)) {
@@ -1700,6 +1691,15 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 			return PTR_ERR(hvs->disp_clk);
 		}
 
+		node = rpi_firmware_find_node();
+		if (!node)
+			return -EINVAL;
+
+		firmware = rpi_firmware_get(node);
+		of_node_put(node);
+		if (!firmware)
+			return -EPROBE_DEFER;
+
 		max_rate = rpi_firmware_clk_get_max_rate(firmware,
 							 RPI_FIRMWARE_CORE_CLK_ID);
 		rpi_firmware_put(firmware);
Signature · 1 line
2.34.1
#4 lore raw
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: - [High] drm/vc4: resource leak and NULL pointer…

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:
- [High] drm/vc4: resource leak and NULL pointer dereference in vc4_hvs_bind() [1]
--

commit 17330184776a3fb8ffab5b0b09aa15bba8f8bd33
Author: Wentao Liang <[email protected]>

drm/vc4: Fix firmware reference leak in vc4_hvs_bind()

This commit moves the rpi_firmware_find_node() and rpi_firmware_get() calls in vc4_hvs_bind() below the devm_clk_get() lookups so the firmware reference is not leaked when clock acquisition fails.

Quoted 26 lines
drivers/gpu/drm/vc4/vc4_hvs.c +2 -9
index b1fb096858ccc..537aaa9921293 100644
@@ -1677,15 +1677,6 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 		struct device_node *node;
 		unsigned int max_rate;
 
-		node = rpi_firmware_find_node();
-		if (!node)
-			return -EINVAL;
-
-		firmware = rpi_firmware_get(node);
-		of_node_put(node);
-		if (!firmware)
-			return -EPROBE_DEFER;
-
 		hvs->core_clk = devm_clk_get(&pdev->dev,
 					     (vc4->gen >= VC4_GEN_6_C) ? "core" : NULL);
 		if (IS_ERR(hvs->core_clk)) {
@@ -1700,6 +1691,15 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 			return PTR_ERR(hvs->disp_clk);
 		}
 
+		node = rpi_firmware_find_node();
+		if (!node)

drivers/gpu/drm/vc4/vc4_hvs.c static int vc4_hvs_bind(struct device *dev, struct device *master, void *data) · line 1705

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_hvs.c +7 -0
+			return -EINVAL;
+
+		firmware = rpi_firmware_get(node);
+		of_node_put(node);
+		if (!firmware)
+			return -EPROBE_DEFER;
+
 		max_rate = rpi_firmware_clk_get_max_rate(firmware,
 							 RPI_FIRMWARE_CORE_CLK_ID);
 		rpi_firmware_put(firmware);

[Severity: High]
This problem wasn't introduced by this patch, but can vc4_hvs_irq_handler() dereference a NULL vc4->hvs pointer after component unbind [1]?

vc4_hvs_bind() allocates hvs and registers vc4_hvs_irq_handler() with devm_kzalloc() and devm_request_irq() tied to the component platform device rather than the aggregate DRM lifetime. When the component unbinds (for example on aggregate probe deferral), vc4_hvs_unbind() sets vc4->hvs to NULL while the devres-managed IRQ handler remains registered:

drivers/gpu/drm/vc4/vc4_hvs.c:vc4_hvs_irq_handler() {
	...
	struct vc4_dev *vc4 = to_vc4_dev(dev);
	...
	status = HVS_READ(SCALER_DISPSTAT);
	...
}

If an interrupt fires after unbind, HVS_READ() dereferences vc4->hvs->regs, causing a NULL pointer dereference, and a subsequent re-bind leaks devres allocations and fails in devm_request_irq() with -EBUSY.

[1] https://sashiko.dev/#/bug/linux-4ad12be9-d1a5-441d-9845-195d42c3f880

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Applied
#5 lore raw
In reply to Wentao Liang
Quoted 11 lines · Wentao Liang

The firmware reference taken via rpi_firmware_get() is leaked when either of the devm_clk_get() calls for the core or display clocks fails, as both error paths return without releasing it.

Fix it by moving the firmware lookup below the clock lookups, so that the reference is taken right before the firmware property is read and released immediately afterwards. The rpi_firmware_find_node() call moves with it: the device node reference it returns would otherwise be leaked by those same error paths.

[...]

Applied, thanks!

[1/1] drm/vc4: Fix firmware reference leak in vc4_hvs_bind()

      commit: af200e91c7b996177aa90981b2f0e794b085d43a

Best regards,
- Maíra