Skip to contentSkip to conversation

pmdomain: imx8m-blk-ctrl: imx8mp-blk-ctrl: Error out if domains are missing in DT

PATCH In review Marek Vasut 4 participants 5 messages Jan 19, 2024 – Jan 22, 2024

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

PATCH v2 1/1 pmdomain: imx8m-blk-ctrl: imx8mp-blk-ctrl: Error out if domains are missing in DT

This driver assumes that domain->power_dev is non-NULL in its suspend/resume path. The assumption is valid, since all the devices that are being looked up here should be described in DT. In case they are not described in DT, beause the DT is faulty, suspend/resume attempt would trigger NULL pointer dereference. To avoid this failure, check whether the power_dev assignment is not NULL right away in probe callback and fail early if it is.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Jindong Yue <[email protected]>
Cc: Lucas Stach <[email protected]>
Cc: Marco Felsch <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add extra check for domain being NULL (thanks Peng)

---
 drivers/pmdomain/imx/imx8m-blk-ctrl.c  | 9 ++++++---
 drivers/pmdomain/imx/imx8mp-blk-ctrl.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/pmdomain/imx/imx8m-blk-ctrl.c +6 -3
index 1341a707f61bc..ca942d7929c2b 100644
@@ -258,11 +258,14 @@ static int imx8m_blk_ctrl_probe(struct platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain \"%s\"\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
 			goto cleanup_pds;
 		}
 
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +6 -3
index e3203eb6a0229..e488cf79b8007 100644
@@ -687,11 +687,14 @@ static int imx8mp_blk_ctrl_probe(struct platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain %s\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
 			goto cleanup_pds;
 		}
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines

Subject: [PATCH v2] pmdomain: imx8m-blk-ctrl: imx8mp-blk-ctrl: Error out if domains are missing in DT

This driver assumes that domain->power_dev is non-NULL in its suspend/resume path. The assumption is valid, since all the devices that are being looked up here should be described in DT. In case they are not described in DT, beause the DT is faulty, suspend/resume attempt would trigger NULL pointer dereference.
To avoid this failure, check whether the power_dev assignment is not NULL right away in probe callback and fail early if it is.

Quoted 1 line
Quoted 74 lines

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Jindong Yue <[email protected]>
Cc: Lucas Stach <[email protected]>
Cc: Marco Felsch <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add extra check for domain being NULL (thanks Peng)

---
 drivers/pmdomain/imx/imx8m-blk-ctrl.c  | 9 ++++++---
drivers/pmdomain/imx/imx8mp-blk-ctrl.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/pmdomain/imx/imx8m-blk-ctrl.c +6 -3
index 1341a707f61bc..ca942d7929c2b 100644
@@ -258,11 +258,14 @@ static int imx8m_blk_ctrl_probe(struct
 platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data-
 >gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain
 \"%s\"\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
drivers/pmdomain/imx/imx8m-blk-ctrl.c +0 -0
 			goto cleanup_pds;
 		}
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +6 -3
index e3203eb6a0229..e488cf79b8007 100644
@@ -687,11 +687,14 @@ static int imx8mp_blk_ctrl_probe(struct
 platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data-
 >gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain %s\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
 			goto cleanup_pds;

}

--
2.43.0

#2 lore raw
In reply to Marek Vasut
Quoted 60 lines · Marek Vasut

This driver assumes that domain->power_dev is non-NULL in its suspend/resume path. The assumption is valid, since all the devices that are being looked up here should be described in DT. In case they are not described in DT, beause the DT is faulty, suspend/resume attempt would trigger NULL pointer dereference. To avoid this failure, check whether the power_dev assignment is not NULL right away in probe callback and fail early if it is.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Jindong Yue <[email protected]>
Cc: Lucas Stach <[email protected]>
Cc: Marco Felsch <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add extra check for domain being NULL (thanks Peng)

---
 drivers/pmdomain/imx/imx8m-blk-ctrl.c  | 9 ++++++---
 drivers/pmdomain/imx/imx8mp-blk-ctrl.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/pmdomain/imx/imx8m-blk-ctrl.c +6 -3
index 1341a707f61bc..ca942d7929c2b 100644
@@ -258,11 +258,14 @@ static int imx8m_blk_ctrl_probe(struct platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain \"%s\"\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
 			goto cleanup_pds;
 		}
 
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +0 -1
index e3203eb6a0229..e488cf79b8007 100644
@@ -687,11 +687,14 @@ static int imx8mp_blk_ctrl_probe(struct platform_device *pdev)
 
 		domain->power_dev =
 			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {

drivers/pmdomain/imx/imx8mp-blk-ctrl.c static int imx8mp_blk_ctrl_probe(struct platform_device *pdev) · line 694

Quoted 10 lines
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +6 -2
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
 				      "failed to attach power domain %s\n",
 				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);

This could be made a bit more compact using:

	domain->power_dev =
		dev_pm_domain_attach_by_name(dev, data->gpc_name) ||
		ERR_PTR(-ENODEV);
	if (IS_ERR(domain->power_dev)) {
		ret = PTR_ERR(domain->power_dev);
		...

I'm unsure though if this is human friendly enough?!

Having said that I wonder about dev_pm_domain_attach_by_name(). IMHO if NULL is an error case it and other errors are signaled by error pointers, there is something to fix there.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#3 lore raw
Quoted 74 lines · Uwe Kleine-König
Quoted 60 lines · Marek Vasut

This driver assumes that domain->power_dev is non-NULL in its suspend/resume path. The assumption is valid, since all the devices that are being looked up here should be described in DT. In case they are not described in DT, beause the DT is faulty, suspend/resume attempt would trigger NULL pointer dereference. To avoid this failure, check whether the power_dev assignment is not NULL right away in probe callback and fail early if it is.

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Jindong Yue <[email protected]>
Cc: Lucas Stach <[email protected]>
Cc: Marco Felsch <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add extra check for domain being NULL (thanks Peng)

---
  drivers/pmdomain/imx/imx8m-blk-ctrl.c  | 9 ++++++---
  drivers/pmdomain/imx/imx8mp-blk-ctrl.c | 9 ++++++---
  2 files changed, 12 insertions(+), 6 deletions(-)
drivers/pmdomain/imx/imx8m-blk-ctrl.c +6 -3
index 1341a707f61bc..ca942d7929c2b 100644
@@ -258,11 +258,14 @@ static int imx8m_blk_ctrl_probe(struct platform_device *pdev)
 
  		domain->power_dev =
  			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
  				      "failed to attach power domain \"%s\"\n",
  				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);
  			goto cleanup_pds;
  		}
 
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +0 -1
index e3203eb6a0229..e488cf79b8007 100644
@@ -687,11 +687,14 @@ static int imx8mp_blk_ctrl_probe(struct platform_device *pdev)
 
  		domain->power_dev =
  			dev_pm_domain_attach_by_name(dev, data->gpc_name);
-		if (IS_ERR(domain->power_dev)) {

drivers/pmdomain/imx/imx8mp-blk-ctrl.c static int imx8mp_blk_ctrl_probe(struct platform_device *pdev) · line 694

Quoted 10 lines
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +6 -2
-			dev_err_probe(dev, PTR_ERR(domain->power_dev),
+		if (IS_ERR_OR_NULL(domain->power_dev)) {
+			if (!domain->power_dev)
+				ret = -ENODEV;
+			else
+				ret = PTR_ERR(domain->power_dev);
+			dev_err_probe(dev, ret,
  				      "failed to attach power domain %s\n",
  				      data->gpc_name);
-			ret = PTR_ERR(domain->power_dev);

This could be made a bit more compact using:

Quoted 8 lines
	domain->power_dev =
		dev_pm_domain_attach_by_name(dev, data->gpc_name) ||
		ERR_PTR(-ENODEV);
	if (IS_ERR(domain->power_dev)) {
		ret = PTR_ERR(domain->power_dev);
		...

I'm unsure though if this is human friendly enough?!

I think it is only more cryptic and doesn't improve readability.

Quoted 3 lines

Having said that I wonder about dev_pm_domain_attach_by_name(). IMHO if NULL is an error case it and other errors are signaled by error pointers, there is something to fix there.

I don't think dev_pm_domain_attach_by_name() returning NULL is an error -- the domain may be missing from DT and that is legitimate use case I think.

But not here, where all the domains should be described in DT because the driver makes assumptions about their presence in the suspend/resume part, and because the DT should fully describe the domains of this hardware anyway, so we better catch such DT issues.

#4 lore raw
In reply to Marek Vasut
Quoted 9 lines · Marek Vasut

This driver assumes that domain->power_dev is non-NULL in its suspend/resume path. The assumption is valid, since all the devices that are being looked up here should be described in DT. In case they are not described in DT, beause the DT is faulty, suspend/resume attempt would trigger NULL pointer dereference. To avoid this failure, check whether the power_dev assignment is not NULL right away in probe callback and fail early if it is.

Applied for next, thanks!

Kind regards
Uffe

Quoted 68 lines

---
Cc: "Uwe Kleine-König" <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Jindong Yue <[email protected]>
Cc: Lucas Stach <[email protected]>
Cc: Marco Felsch <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Peng Fan <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add extra check for domain being NULL (thanks Peng)

---
 drivers/pmdomain/imx/imx8m-blk-ctrl.c  | 9 ++++++---
 drivers/pmdomain/imx/imx8mp-blk-ctrl.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/pmdomain/imx/imx8m-blk-ctrl.c +6 -3
index 1341a707f61bc..ca942d7929c2b 100644
@@ -258,11 +258,14 @@ static int imx8m_blk_ctrl_probe(struct platform_device *pdev)
 
                domain->power_dev =
                        dev_pm_domain_attach_by_name(dev, data->gpc_name);
-               if (IS_ERR(domain->power_dev)) {
-                       dev_err_probe(dev, PTR_ERR(domain->power_dev),
+               if (IS_ERR_OR_NULL(domain->power_dev)) {
+                       if (!domain->power_dev)
+                               ret = -ENODEV;
+                       else
+                               ret = PTR_ERR(domain->power_dev);
+                       dev_err_probe(dev, ret,
                                      "failed to attach power domain \"%s\"\n",
                                      data->gpc_name);
-                       ret = PTR_ERR(domain->power_dev);
                        goto cleanup_pds;
                }
 
drivers/pmdomain/imx/imx8mp-blk-ctrl.c +6 -4
index e3203eb6a0229..e488cf79b8007 100644
@@ -687,11 +687,14 @@ static int imx8mp_blk_ctrl_probe(struct platform_device *pdev)
 
                domain->power_dev =
                        dev_pm_domain_attach_by_name(dev, data->gpc_name);
-               if (IS_ERR(domain->power_dev)) {
-                       dev_err_probe(dev, PTR_ERR(domain->power_dev),
+               if (IS_ERR_OR_NULL(domain->power_dev)) {
+                       if (!domain->power_dev)
+                               ret = -ENODEV;
+                       else
+                               ret = PTR_ERR(domain->power_dev);
+                       dev_err_probe(dev, ret,
                                      "failed to attach power domain %s\n",
                                      data->gpc_name);
-                       ret = PTR_ERR(domain->power_dev);
                        goto cleanup_pds;
                }
 
--

2.43.0