diff mbox series

soc: qcom: pmic_glink_altmode: Use common error handling code in pmic_glink_altmode_probe()

Message ID 29b63eb4-2342-4ca8-a313-5de2a6ec6a83@web.de
State Handled Elsewhere
Headers show
Series soc: qcom: pmic_glink_altmode: Use common error handling code in pmic_glink_altmode_probe() | expand

Commit Message

Markus Elfring Feb. 28, 2024, 6:05 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 28 Feb 2024 18:45:13 +0100

Add a jump target so that a bit of exception handling can be better reused
at the end of this function implementation.

This issue was transformed by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/soc/qcom/pmic_glink_altmode.c | 57 +++++++++++++--------------
 1 file changed, 27 insertions(+), 30 deletions(-)

--
2.43.2

Comments

Konrad Dybcio Feb. 28, 2024, 11:26 p.m. UTC | #1
On 2/28/24 19:05, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 28 Feb 2024 18:45:13 +0100
> 
> Add a jump target so that a bit of exception handling can be better reused
> at the end of this function implementation.
> 
> This issue was transformed by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---

(+CC Peter)

Hmm.. this looks very similar to the problem that __free solves
with <linux/cleanup.h>..

I know no better, but wouldn't the same mechanism, down to the
usage of DEFINE_FREE work fine for _put-like functions?

Konrad
Dan Carpenter Feb. 29, 2024, 7:31 a.m. UTC | #2
On Thu, Feb 29, 2024 at 12:26:49AM +0100, Konrad Dybcio wrote:
> 
> 
> On 2/28/24 19:05, Markus Elfring wrote:
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Wed, 28 Feb 2024 18:45:13 +0100
> > 
> > Add a jump target so that a bit of exception handling can be better reused
> > at the end of this function implementation.
> > 
> > This issue was transformed by using the Coccinelle software.
> > 
> > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> > ---
> 
> (+CC Peter)
> 
> Hmm.. this looks very similar to the problem that __free solves
> with <linux/cleanup.h>..
> 
> I know no better, but wouldn't the same mechanism, down to the
> usage of DEFINE_FREE work fine for _put-like functions?

Jonathan Cameron has created something like this:
https://lore.kernel.org/all/20240225142714.286440-1-jic23@kernel.org/

It hasn't been merged yet and it would need a bit of adjusting for this
use case but it's basically what you want.

regards,
dan carpenter
diff mbox series

Patch

diff --git a/drivers/soc/qcom/pmic_glink_altmode.c b/drivers/soc/qcom/pmic_glink_altmode.c
index b3808fc24c69..c987a45e1703 100644
--- a/drivers/soc/qcom/pmic_glink_altmode.c
+++ b/drivers/soc/qcom/pmic_glink_altmode.c
@@ -434,8 +434,7 @@  static int pmic_glink_altmode_probe(struct auxiliary_device *adev,
 		ret = fwnode_property_read_u32(fwnode, "reg", &port);
 		if (ret < 0) {
 			dev_err(dev, "missing reg property of %pOFn\n", fwnode);
-			fwnode_handle_put(fwnode);
-			return ret;
+			goto put_fwnode;
 		}

 		if (port >= ARRAY_SIZE(altmode->ports)) {
@@ -445,8 +444,8 @@  static int pmic_glink_altmode_probe(struct auxiliary_device *adev,

 		if (altmode->ports[port].altmode) {
 			dev_err(dev, "multiple connector definition for port %u\n", port);
-			fwnode_handle_put(fwnode);
-			return -EINVAL;
+			ret = -EINVAL;
+			goto put_fwnode;
 		}

 		alt_port = &altmode->ports[port];
@@ -456,8 +455,8 @@  static int pmic_glink_altmode_probe(struct auxiliary_device *adev,

 		alt_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode));
 		if (IS_ERR(alt_port->bridge)) {
-			fwnode_handle_put(fwnode);
-			return PTR_ERR(alt_port->bridge);
+			ret = PTR_ERR(alt_port->bridge);
+			goto put_fwnode;
 		}

 		alt_port->dp_alt.svid = USB_TYPEC_DP_SID;
@@ -466,48 +465,42 @@  static int pmic_glink_altmode_probe(struct auxiliary_device *adev,

 		alt_port->typec_mux = fwnode_typec_mux_get(fwnode);
 		if (IS_ERR(alt_port->typec_mux)) {
-			fwnode_handle_put(fwnode);
-			return dev_err_probe(dev, PTR_ERR(alt_port->typec_mux),
-					     "failed to acquire mode-switch for port: %d\n",
-					     port);
+			ret = dev_err_probe(dev, PTR_ERR(alt_port->typec_mux),
+					    "failed to acquire mode-switch for port: %d\n",
+					    port);
+			goto put_fwnode;
 		}

 		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_mux,
 					       alt_port->typec_mux);
-		if (ret) {
-			fwnode_handle_put(fwnode);
-			return ret;
-		}
+		if (ret)
+			goto put_fwnode;

 		alt_port->typec_retimer = fwnode_typec_retimer_get(fwnode);
 		if (IS_ERR(alt_port->typec_retimer)) {
-			fwnode_handle_put(fwnode);
-			return dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer),
-					     "failed to acquire retimer-switch for port: %d\n",
-					     port);
+			ret = dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer),
+					    "failed to acquire retimer-switch for port: %d\n",
+					    port);
+			goto put_fwnode;
 		}

 		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_retimer,
 					       alt_port->typec_retimer);
-		if (ret) {
-			fwnode_handle_put(fwnode);
-			return ret;
-		}
+		if (ret)
+			goto put_fwnode;

 		alt_port->typec_switch = fwnode_typec_switch_get(fwnode);
 		if (IS_ERR(alt_port->typec_switch)) {
-			fwnode_handle_put(fwnode);
-			return dev_err_probe(dev, PTR_ERR(alt_port->typec_switch),
-					     "failed to acquire orientation-switch for port: %d\n",
-					     port);
+			ret = dev_err_probe(dev, PTR_ERR(alt_port->typec_switch),
+					    "failed to acquire orientation-switch for port: %d\n",
+					    port);
+			goto put_fwnode;
 		}

 		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_switch,
 					       alt_port->typec_switch);
-		if (ret) {
-			fwnode_handle_put(fwnode);
-			return ret;
-		}
+		if (ret)
+			goto put_fwnode;
 	}

 	for (port = 0; port < ARRAY_SIZE(altmode->ports); port++) {
@@ -526,6 +519,10 @@  static int pmic_glink_altmode_probe(struct auxiliary_device *adev,
 							  pmic_glink_altmode_pdr_notify,
 							  altmode);
 	return PTR_ERR_OR_ZERO(altmode->client);
+
+put_fwnode:
+	fwnode_handle_put(fwnode);
+	return ret;
 }

 static const struct auxiliary_device_id pmic_glink_altmode_id_table[] = {