Skip to contentSkip to conversation

media: rcar-csi2: Use common error handling code in rcsi2_parse_dt()

PATCH NAK Markus Elfring 6 participants 11 messages Mar 1, 2024 – Mar 16, 2024

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

PATCH 1/1 media: rcar-csi2: Use common error handling code in rcsi2_parse_dt()

From: Markus Elfring <[email protected]>
Date: Fri, 1 Mar 2024 13:02:18 +0100

Add a label so that a bit of exception handling can be better reused in an if branch of this function implementation.

---
 drivers/media/platform/renesas/rcar-csi2.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/renesas/rcar-csi2.c +3 -2
index 582d5e35db0e..621c92c31965 100644
@@ -1388,12 +1388,13 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 	ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
 	if (ret) {
 		dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-		fwnode_handle_put(ep);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto put_fwnode_ep;
 	}
 
 	ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
 	if (ret) {
+put_fwnode_ep:
 		fwnode_handle_put(ep);
 		return ret;
 	}

--
2.44.0

#1 lore raw

Hi Markus,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 1 Mar 2024 13:02:18 +0100

Add a label so that a bit of exception handling can be better reused in an if branch of this function implementation.

Thanks for your patch!

Quoted 8 lines
drivers/media/platform/renesas/rcar-csi2.c +0 -2
@@ -1388,12 +1388,13 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
        ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
        if (ret) {
                dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-               fwnode_handle_put(ep);
-               return -EINVAL;

drivers/media/platform/renesas/rcar-csi2.c static int rcsi2_parse_dt(struct rcar_csi2 *priv) · line 1400

Quoted 10 lines
drivers/media/platform/renesas/rcar-csi2.c +3 -0
+               ret = -EINVAL;
+               goto put_fwnode_ep;
        }
 
        ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
        if (ret) {
+put_fwnode_ep:
                fwnode_handle_put(ep);
                return ret;
        }

Please do not use goto to jump to random positions buried deep inside a function,as this makes the code harder to read.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

drivers/media/platform/renesas/rcar-csi2.c +3 -8
index 582d5e35db0e..c569df6057b7 100644
@@ -1372,8 +1372,8 @@ static int rcsi2_parse_v4l2(struct rcar_csi2 *priv,
 static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 {
 	struct v4l2_async_connection *asc;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *ep;
+	struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;
+	struct fwnode_handle *ep __free(fwnode_handle);
 	struct v4l2_fwnode_endpoint v4l2_ep = {
 		.bus_type = V4L2_MBUS_UNKNOWN,
 	};
@@ -1388,18 +1388,14 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 	ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
 	if (ret) {
 		dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-		fwnode_handle_put(ep);
 		return -EINVAL;
 	}
 
 	ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
-	if (ret) {
-		fwnode_handle_put(ep);
+	if (ret)
 		return ret;
-	}
 
 	fwnode = fwnode_graph_get_remote_endpoint(ep);
-	fwnode_handle_put(ep);
 
 	dev_dbg(priv->dev, "Found '%pOF'\n", to_of_node(fwnode));
 
@@ -1408,7 +1404,6 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 
 	asc = v4l2_async_nf_add_fwnode(&priv->notifier, fwnode,
 				       struct v4l2_async_connection);
-	fwnode_handle_put(fwnode);
 	if (IS_ERR(asc))
 		return PTR_ERR(asc);
 
#3 lore raw
Quoted 1 line

Sakari Ailus pointed out in another thread that we could use __free() instead.

See also:
Contributions by Jonathan Cameron from 2024-02-17

* device property: Move fwnode_handle_put() into property.h
https://lore.kernel.org/r/[email protected]

* device property: Add cleanup.h based fwnode_handle_put() scope based cleanup.
https://lore.kernel.org/r/[email protected]

Quoted 7 lines

Something like this:

drivers/media/platform/renesas/rcar-csi2.c +0 -0
index 582d5e35db0e..c569df6057b7 100644

drivers/media/platform/renesas/rcar-csi2.c static int rcsi2_parse_v4l2(struct rcar_csi2 *priv, · line 1379

Quoted 10 lines
drivers/media/platform/renesas/rcar-csi2.c +2 -2
@@ -1372,8 +1372,8 @@ static int rcsi2_parse_v4l2(struct rcar_csi2 *priv,
 static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 {
 	struct v4l2_async_connection *asc;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *ep;
+	struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;
+	struct fwnode_handle *ep __free(fwnode_handle);
 	struct v4l2_fwnode_endpoint v4l2_ep = {
 		.bus_type = V4L2_MBUS_UNKNOWN,
 	};

I suggest to reconsider the position for the adjusted variable declarations a bit more.

Quoted 21 lines
drivers/media/platform/renesas/rcar-csi2.c +1 -5
@@ -1388,18 +1388,14 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 	ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
 	if (ret) {
 		dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-		fwnode_handle_put(ep);
 		return -EINVAL;
 	}
 
 	ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
-	if (ret) {
-		fwnode_handle_put(ep);
+	if (ret)
 		return ret;
-	}
 
 	fwnode = fwnode_graph_get_remote_endpoint(ep);
-	fwnode_handle_put(ep);
 
 	dev_dbg(priv->dev, "Found '%pOF'\n", to_of_node(fwnode));
 

drivers/media/platform/renesas/rcar-csi2.c static int rcsi2_parse_dt(struct rcar_csi2 *priv) · line 1409

Quoted 7 lines
drivers/media/platform/renesas/rcar-csi2.c +0 -1
@@ -1408,7 +1404,6 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 
 	asc = v4l2_async_nf_add_fwnode(&priv->notifier, fwnode,
 				       struct v4l2_async_connection);
-	fwnode_handle_put(fwnode);
 	if (IS_ERR(asc))
 		return PTR_ERR(asc);
 

I find that two function calls marked the end of scopes here
which obviously are not at the end of the discussed function implementation. Thus I imagine that the known source code transformation “Reduce scope for variables” will become relevant.
https://refactoring.com/catalog/reduceScopeOfVariable.html

Regards,
Markus

#4 lore raw
In reply to Dan Carpenter

Hi Dan,

Quoted 3 lines · Dan Carpenter

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

Looks good to me.

We could merge this with your SoB (pending Niklas's review). :-) The driver has been since moved under drivers/media/platform/renesas/rcar-vin/ .

Quoted 44 lines
drivers/media/platform/renesas/rcar-csi2.c +3 -8
index 582d5e35db0e..c569df6057b7 100644
@@ -1372,8 +1372,8 @@ static int rcsi2_parse_v4l2(struct rcar_csi2 *priv,
 static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 {
 	struct v4l2_async_connection *asc;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *ep;
+	struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;
+	struct fwnode_handle *ep __free(fwnode_handle);
 	struct v4l2_fwnode_endpoint v4l2_ep = {
 		.bus_type = V4L2_MBUS_UNKNOWN,
 	};
@@ -1388,18 +1388,14 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 	ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
 	if (ret) {
 		dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-		fwnode_handle_put(ep);
 		return -EINVAL;
 	}
 
 	ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
-	if (ret) {
-		fwnode_handle_put(ep);
+	if (ret)
 		return ret;
-	}
 
 	fwnode = fwnode_graph_get_remote_endpoint(ep);
-	fwnode_handle_put(ep);
 
 	dev_dbg(priv->dev, "Found '%pOF'\n", to_of_node(fwnode));
 
@@ -1408,7 +1404,6 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 
 	asc = v4l2_async_nf_add_fwnode(&priv->notifier, fwnode,
 				       struct v4l2_async_connection);
-	fwnode_handle_put(fwnode);
 	if (IS_ERR(asc))
 		return PTR_ERR(asc);
 
Signature · 3 lines
Regards,

Sakari Ailus
#5 lore raw
Quoted 8 lines · Sakari Ailus

Hi Dan,

Quoted 3 lines · Dan Carpenter

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

Looks good to me.

Thanks for checking! I've never used these before.

Quoted 3 lines

We could merge this with your SoB (pending Niklas's review). :-) The driver has been since moved under drivers/media/platform/renesas/rcar-vin/ .

Alright. I can resend this as a proper patch.

regards,
dan carpenter

#6 lore raw

Hi Dan,

Quoted 10 lines · Dan Carpenter
Quoted 8 lines · Sakari Ailus

Hi Dan,

Quoted 3 lines · Dan Carpenter

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

Looks good to me.

Quoted 7 lines

Thanks for checking! I've never used these before.

Quoted 3 lines

We could merge this with your SoB (pending Niklas's review). :-) The driver has been since moved under drivers/media/platform/renesas/rcar-vin/ .

Alright. I can resend this as a proper patch.

Please do.

I do find the idea of scoped operations and the syntax

    struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;

a bit foreign in a C context. But I think the intention is clear and it allows us to avoid having the remember to free the fwnode in error paths which is a nice thing.

Quoted 4 lines

regards,
dan carpenter

Signature · 2 lines
Kind Regards,
Niklas Söderlund
#7 lore raw
Quoted 23 lines · Niklas Söderlund

Hi Dan,

Quoted 10 lines · Dan Carpenter
Quoted 8 lines · Sakari Ailus

Hi Dan,

Quoted 3 lines · Dan Carpenter

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

Looks good to me.

Quoted 7 lines

Thanks for checking! I've never used these before.

Quoted 3 lines

We could merge this with your SoB (pending Niklas's review). :-) The driver has been since moved under drivers/media/platform/renesas/rcar-vin/ .

Alright. I can resend this as a proper patch.

Please do.

Quoted 8 lines

I do find the idea of scoped operations and the syntax

    struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;

a bit foreign in a C context. But I think the intention is clear and it allows us to avoid having the remember to free the fwnode in error paths which is a nice thing.

I said I would send a couple of these but then Markus went ahead and sent the patches that I was going to write... And then it was like, "Oh, these have some questionable style issues" so it wasn't clear what was happening and I lost track.

regards,
dan carpenter

#8 lore raw
Quoted 33 lines · Dan Carpenter
Quoted 23 lines · Niklas Söderlund

Hi Dan,

Quoted 10 lines · Dan Carpenter
Quoted 8 lines · Sakari Ailus

Hi Dan,

Quoted 3 lines · Dan Carpenter

Sakari Ailus pointed out in another thread that we could use __free() instead. Something like this:

Looks good to me.

Quoted 7 lines

Thanks for checking! I've never used these before.

Quoted 3 lines

We could merge this with your SoB (pending Niklas's review). :-) The driver has been since moved under drivers/media/platform/renesas/rcar-vin/ .

Alright. I can resend this as a proper patch.

Please do.

Quoted 8 lines

I do find the idea of scoped operations and the syntax

    struct fwnode_handle *fwnode __free(fwnode_handle) = NULL;

a bit foreign in a C context. But I think the intention is clear and it allows us to avoid having the remember to free the fwnode in error paths which is a nice thing.

Quoted 4 lines

I said I would send a couple of these but then Markus went ahead and sent the patches that I was going to write... And then it was like, "Oh, these have some questionable style issues" so it wasn't clear what was happening and I lost track.

I have not been CCed on any other work in this area for this driver then what's in this thread at least. So if you know of no other work in another thread I think you can go a head and send a proper patch for this driver at least, if you want.

Signature · 2 lines
Kind Regards,
Niklas Söderlund
#9 lore raw
In reply to Dan Carpenter
Quoted 2 lines

I said I would send a couple of these but then Markus went ahead and sent the patches that I was going to write...

I dared also to touch some software components.

Quoted 2 lines

And then it was like,
"Oh, these have some questionable style issues"

The patch review is still evolving, isn't it?

Quoted 1 line

so it wasn't clear what was happening and I lost track.

I find such information surprising.

There are various source code places left over which could be adjusted somehow.

Some contributors would appreciate further clarifications according to desirable collateral evolution.

See also:
question about kernel cocci and cleanup.h
2024-03-07
https://lore.kernel.org/cocci/CO1PR11MB49149F1167679926A2917E0997202@CO1PR11MB4914.namprd11.prod.outlook.com/

Regards,
Markus

#10 lore raw
In reply to Markus Elfring
Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 1 Mar 2024 13:02:18 +0100

Add a label so that a bit of exception handling can be better reused in an if branch of this function implementation.

Just in case someone may be tempted to apply this:

NAK

Markus, don't bother replying to this e-mail, I will delete your reply without reading it.

Quoted 24 lines
---
 drivers/media/platform/renesas/rcar-csi2.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/platform/renesas/rcar-csi2.c +3 -2
index 582d5e35db0e..621c92c31965 100644
@@ -1388,12 +1388,13 @@ static int rcsi2_parse_dt(struct rcar_csi2 *priv)
 	ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
 	if (ret) {
 		dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
-		fwnode_handle_put(ep);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto put_fwnode_ep;
 	}
 
 	ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
 	if (ret) {
+put_fwnode_ep:
 		fwnode_handle_put(ep);
 		return ret;
 	}
Signature · 3 lines
Regards,

Laurent Pinchart