Skip to contentSkip to conversation

drm/arm/malidp: fix ignored clk_prepare_enable() in runtime PM resume

PATCH In review Gustavo Kenji Mendonça Kaneko 2 participants 5 messages May 31 – Jun 5

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

PATCH 1/2 drm/arm/malidp: fix ignored clk_prepare_enable() in runtime PM resume

malidp_runtime_pm_resume() calls clk_prepare_enable() three times without checking the return value. If any clock fails to enable, the driver silently proceeds with unclocked hardware, leading to undefined behavior.

Use clk_bulk_prepare_enable() which atomically enables all clocks and automatically rolls back successfully enabled clocks on failure. This is consistent with how the suspend path already uses clk_bulk_disable_unprepare() in malidp_runtime_pm_suspend().

This issue was found by code review without access to Mali DP hardware.

---
 drivers/gpu/drm/arm/malidp_drv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/arm/malidp_drv.c +10 -3
index b765f6c9eea4..1e4336c3a2fa 100644
@@ -689,10 +689,17 @@ static int malidp_runtime_pm_resume(struct device *dev)
 	struct drm_device *drm = dev_get_drvdata(dev);
 	struct malidp_drm *malidp = drm_to_malidp(drm);
 	struct malidp_hw_device *hwdev = malidp->dev;
+	struct clk_bulk_data clks[] = {
+		{ .clk = hwdev->pclk },
+		{ .clk = hwdev->aclk },
+		{ .clk = hwdev->mclk },
+	};
+	int err;
+
+	err = clk_bulk_prepare_enable(ARRAY_SIZE(clks), clks);
+	if (err)
+		return err;
 
-	clk_prepare_enable(hwdev->pclk);
-	clk_prepare_enable(hwdev->aclk);
-	clk_prepare_enable(hwdev->mclk);
 	hwdev->pm_suspended = false;
 	malidp_de_irq_hw_init(hwdev);
 	malidp_se_irq_hw_init(hwdev);
Signature · 1 line
2.54.0
#1 lore raw

PATCH 2/2 drm/arm/komeda: fix ignored clk_prepare_enable() return value in resume

komeda_dev_resume() calls clk_prepare_enable() without checking the return value. If the clock fails to enable, the function returns 0 (success) while IRQs are enabled and IOMMU is connected on potentially unclocked hardware, causing undefined behavior on resume.

Propagate the error from clk_prepare_enable() and return early on failure to prevent hardware access without a valid clock.

This issue was found by code review without access to Komeda hardware.

---
 drivers/gpu/drm/arm/display/komeda/komeda_dev.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/arm/display/komeda/komeda_dev.c +5 -1
index 5ba62e637a61..9aad1d1d28ec 100644
@@ -313,7 +313,11 @@ void komeda_dev_destroy(struct komeda_dev *mdev)
 
 int komeda_dev_resume(struct komeda_dev *mdev)
 {
-	clk_prepare_enable(mdev->aclk);
+	int err;
+
+	err = clk_prepare_enable(mdev->aclk);
+	if (err)
+		return err;
 
 	mdev->funcs->enable_irq(mdev);
 
Signature · 1 line
2.54.0
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/arm/komeda: fix ignored clk_prepare_enable() return value in resume

Quoted 9 lines · Gustavo Kenji Mendonça Kaneko

komeda_dev_resume() calls clk_prepare_enable() without checking the return value. If the clock fails to enable, the function returns 0 (success) while IRQs are enabled and IOMMU is connected on potentially unclocked hardware, causing undefined behavior on resume.

Propagate the error from clk_prepare_enable() and return early on failure to prevent hardware access without a valid clock.

This issue was found by code review without access to Komeda hardware.

Hello,

There is nothing wrong with the content of this patch, but I would like if anyone wants to fix these kind of problems to at least be thorough.

komeda_dev_suspend() is also ignoring the errors coming from clk_disable_unprepare() and both fuctions before this patch always return 0, so in komeda_drv.c we can sometimes ignore the return value. If we add an extended version of this patch then we should also do proper error handling in komeda_drv.c

Best regards,
Liviu

Quoted 27 lines
---
 drivers/gpu/drm/arm/display/komeda/komeda_dev.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/arm/display/komeda/komeda_dev.c +5 -1
index 5ba62e637a61..9aad1d1d28ec 100644
@@ -313,7 +313,11 @@ void komeda_dev_destroy(struct komeda_dev *mdev)
 
 int komeda_dev_resume(struct komeda_dev *mdev)
 {
-	clk_prepare_enable(mdev->aclk);
+	int err;
+
+	err = clk_prepare_enable(mdev->aclk);
+	if (err)
+		return err;
 
 	mdev->funcs->enable_irq(mdev);
 
Signature · 1 line
2.54.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer Reviewed-by on v1 1/2
#3 lore raw
In reply to Gustavo Kenji Mendonça Kaneko
Quoted 5 lines · Gustavo Kenji Mendonça Kaneko

malidp_runtime_pm_resume() calls clk_prepare_enable() three times without checking the return value. If any clock fails to enable, the driver silently proceeds with unclocked hardware, leading to undefined behavior.

Quoted 8 lines

Use clk_bulk_prepare_enable() which atomically enables all clocks and automatically rolls back successfully enabled clocks on failure. This is consistent with how the suspend path already uses clk_bulk_disable_unprepare() in malidp_runtime_pm_suspend().

This issue was found by code review without access to Mali DP hardware.

Best regards,
Liviu

Quoted 33 lines
---
 drivers/gpu/drm/arm/malidp_drv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/arm/malidp_drv.c +10 -3
index b765f6c9eea4..1e4336c3a2fa 100644
@@ -689,10 +689,17 @@ static int malidp_runtime_pm_resume(struct device *dev)
 	struct drm_device *drm = dev_get_drvdata(dev);
 	struct malidp_drm *malidp = drm_to_malidp(drm);
 	struct malidp_hw_device *hwdev = malidp->dev;
+	struct clk_bulk_data clks[] = {
+		{ .clk = hwdev->pclk },
+		{ .clk = hwdev->aclk },
+		{ .clk = hwdev->mclk },
+	};
+	int err;
+
+	err = clk_bulk_prepare_enable(ARRAY_SIZE(clks), clks);
+	if (err)
+		return err;
 
-	clk_prepare_enable(hwdev->pclk);
-	clk_prepare_enable(hwdev->aclk);
-	clk_prepare_enable(hwdev->mclk);
 	hwdev->pm_suspended = false;
 	malidp_de_irq_hw_init(hwdev);
 	malidp_se_irq_hw_init(hwdev);
Signature · 1 line
2.54.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer
#4 lore raw
In reply to Gustavo Kenji Mendonça Kaneko
Quoted 9 lines · Gustavo Kenji Mendonça Kaneko

malidp_runtime_pm_resume() calls clk_prepare_enable() three times without checking the return value. If any clock fails to enable, the driver silently proceeds with unclocked hardware, leading to undefined behavior.

Use clk_bulk_prepare_enable() which atomically enables all clocks and automatically rolls back successfully enabled clocks on failure. This is consistent with how the suspend path already uses clk_bulk_disable_unprepare() in malidp_runtime_pm_suspend().

I can't see where you can find other calls to clk_buld_xxxx() in the malidp code. What sort of code review have you done here?

Best regards,
Liviu

Quoted 37 lines

This issue was found by code review without access to Mali DP hardware.

---
 drivers/gpu/drm/arm/malidp_drv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/arm/malidp_drv.c +10 -3
index b765f6c9eea4..1e4336c3a2fa 100644
@@ -689,10 +689,17 @@ static int malidp_runtime_pm_resume(struct device *dev)
 	struct drm_device *drm = dev_get_drvdata(dev);
 	struct malidp_drm *malidp = drm_to_malidp(drm);
 	struct malidp_hw_device *hwdev = malidp->dev;
+	struct clk_bulk_data clks[] = {
+		{ .clk = hwdev->pclk },
+		{ .clk = hwdev->aclk },
+		{ .clk = hwdev->mclk },
+	};
+	int err;
+
+	err = clk_bulk_prepare_enable(ARRAY_SIZE(clks), clks);
+	if (err)
+		return err;
 
-	clk_prepare_enable(hwdev->pclk);
-	clk_prepare_enable(hwdev->aclk);
-	clk_prepare_enable(hwdev->mclk);
 	hwdev->pm_suspended = false;
 	malidp_de_irq_hw_init(hwdev);
 	malidp_se_irq_hw_init(hwdev);
Signature · 1 line
2.54.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯