* [PATCH] staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
@ 2024-02-29 18:55 Markus Elfring
2024-03-01 17:39 ` Luca Ceresoli
0 siblings, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2024-02-29 18:55 UTC (permalink / raw)
To: linux-staging, linux-tegra, linux-media, kernel-janitors,
Greg Kroah-Hartman, Jonathan Hunter, Luca Ceresoli,
Mauro Carvalho Chehab, Sowjanya Komatineni, Thierry Reding
Cc: LKML
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 29 Feb 2024 19:44:36 +0100
Add a jump target so that a bit of exception handling can be better reused
at the end of this function implementation.
Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
drivers/staging/media/tegra-video/vi.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/staging/media/tegra-video/vi.c b/drivers/staging/media/tegra-video/vi.c
index af6e3a0d8df4..5a08d9551f8b 100644
--- a/drivers/staging/media/tegra-video/vi.c
+++ b/drivers/staging/media/tegra-video/vi.c
@@ -1730,21 +1730,20 @@ static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
ret = PTR_ERR(tvge);
dev_err(vi->dev,
"failed to add subdev to notifier: %d\n", ret);
- fwnode_handle_put(remote);
- goto cleanup;
+ goto put_fwnode;
}
ret = tegra_vi_graph_parse_one(chan, remote);
- if (ret < 0) {
- fwnode_handle_put(remote);
- goto cleanup;
- }
+ if (ret < 0)
+ goto put_fwnode;
fwnode_handle_put(remote);
}
return 0;
+put_fwnode:
+ fwnode_handle_put(remote);
cleanup:
dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
v4l2_async_nf_cleanup(&chan->notifier);
--
2.44.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
2024-02-29 18:55 [PATCH] staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one() Markus Elfring
@ 2024-03-01 17:39 ` Luca Ceresoli
2024-03-02 9:30 ` Dan Carpenter
0 siblings, 1 reply; 6+ messages in thread
From: Luca Ceresoli @ 2024-03-01 17:39 UTC (permalink / raw)
To: Markus Elfring
Cc: linux-staging, linux-tegra, linux-media, kernel-janitors,
Greg Kroah-Hartman, Jonathan Hunter, Mauro Carvalho Chehab,
Sowjanya Komatineni, Thierry Reding, LKML
Hello Markus,
On Thu, 29 Feb 2024 19:55:46 +0100
Markus Elfring <Markus.Elfring@web.de> wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Thu, 29 Feb 2024 19:44:36 +0100
>
> Add a jump target so that a bit of exception handling can be better reused
> at the end of this function implementation.
>
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
2024-03-01 17:39 ` Luca Ceresoli
@ 2024-03-02 9:30 ` Dan Carpenter
2024-03-02 10:40 ` Markus Elfring
0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2024-03-02 9:30 UTC (permalink / raw)
To: Luca Ceresoli
Cc: Markus Elfring, linux-staging, linux-tegra, linux-media,
kernel-janitors, Greg Kroah-Hartman, Jonathan Hunter,
Mauro Carvalho Chehab, Sowjanya Komatineni, Thierry Reding, LKML
On Fri, Mar 01, 2024 at 06:39:36PM +0100, Luca Ceresoli wrote:
> Hello Markus,
>
> On Thu, 29 Feb 2024 19:55:46 +0100
> Markus Elfring <Markus.Elfring@web.de> wrote:
>
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Thu, 29 Feb 2024 19:44:36 +0100
> >
> > Add a jump target so that a bit of exception handling can be better reused
> > at the end of this function implementation.
> >
> > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
>
> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
These patches make the code worse. If we're in the middle of a loop,
then we should clean up the partial loop before doing the goto.
Otherwise it creates a mess when we add a new allocation function after
the end of the loop.
Someone is going to add a _scoped() loop which uses cleanup.h magic to
call _put automatically. This is a good option.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
2024-03-02 9:30 ` Dan Carpenter
@ 2024-03-02 10:40 ` Markus Elfring
2024-03-05 15:24 ` Luca Ceresoli
0 siblings, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2024-03-02 10:40 UTC (permalink / raw)
To: Dan Carpenter, Luca Ceresoli, linux-staging, linux-tegra,
linux-media, kernel-janitors
Cc: Greg Kroah-Hartman, Jonathan Hunter, Mauro Carvalho Chehab,
Sowjanya Komatineni, Thierry Reding, LKML
>>> Add a jump target so that a bit of exception handling can be better reused
>>> at the end of this function implementation.
…
>> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
>
> These patches make the code worse. If we're in the middle of a loop,
> then we should clean up the partial loop before doing the goto.
> Otherwise it creates a mess when we add a new allocation function after
> the end of the loop.
How does such a feedback fit to another known information source?
Section “7) Centralized exiting of functions”
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.8-rc6#n526
> Someone is going to add a _scoped() loop which uses cleanup.h magic to
> call _put automatically. This is a good option.
I became also curious how scope-based resource management will influence
Linux coding styles further.
Will various collateral evolution become more interesting?
Regards,
Markus
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
2024-03-02 10:40 ` Markus Elfring
@ 2024-03-05 15:24 ` Luca Ceresoli
2024-03-05 16:21 ` Dan Carpenter
0 siblings, 1 reply; 6+ messages in thread
From: Luca Ceresoli @ 2024-03-05 15:24 UTC (permalink / raw)
To: Markus Elfring, Dan Carpenter
Cc: linux-staging, linux-tegra, linux-media, kernel-janitors,
Greg Kroah-Hartman, Jonathan Hunter, Mauro Carvalho Chehab,
Sowjanya Komatineni, Thierry Reding, LKML
Hello Dan, Markus,
On Sat, 2 Mar 2024 11:40:26 +0100
Markus Elfring <Markus.Elfring@web.de> wrote:
> >>> Add a jump target so that a bit of exception handling can be better reused
> >>> at the end of this function implementation.
> …
> >> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
> >
> > These patches make the code worse.
This is of course a legitimate opinion. However Markus' patch
implements what is recommended by the documentation and is in common
use in the kernel code. A quick search found 73 occurrences in v6.8-rc7:
$ expr $(pcregrep -r -M ':\n\tfwnode_handle_put' drivers | wc -l) / 2
73
$
300+ are found for of_node_put().
> > If we're in the middle of a loop,
> > then we should clean up the partial loop before doing the goto.
> > Otherwise it creates a mess when we add a new allocation function after
> > the end of the loop.
>
> How does such a feedback fit to another known information source?
>
> Section “7) Centralized exiting of functions”
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.8-rc6#n526
>
> > Someone is going to add a _scoped() loop which uses cleanup.h magic to
> > call _put automatically. This is a good option.
>
> I became also curious how scope-based resource management will influence
> Linux coding styles further.
> Will various collateral evolution become more interesting?
After some research I think I found what Dan means:
https://lore.kernel.org/all/20240225142714.286440-3-jic23@kernel.org/
After reading the above thread, I agree using *_scoped() macros will
be a good improvement. It is not yet in mainline as of v6.8-rc7, but
it is in linux-next. So I think despite being valid this patch might
still be discarded because a better solution should be available in a
few weeks.
Luca
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one()
2024-03-05 15:24 ` Luca Ceresoli
@ 2024-03-05 16:21 ` Dan Carpenter
0 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2024-03-05 16:21 UTC (permalink / raw)
To: Luca Ceresoli
Cc: Markus Elfring, linux-staging, linux-tegra, linux-media,
kernel-janitors, Greg Kroah-Hartman, Jonathan Hunter,
Mauro Carvalho Chehab, Sowjanya Komatineni, Thierry Reding, LKML
On Tue, Mar 05, 2024 at 04:24:27PM +0100, Luca Ceresoli wrote:
> Hello Dan, Markus,
>
> On Sat, 2 Mar 2024 11:40:26 +0100
> Markus Elfring <Markus.Elfring@web.de> wrote:
>
> > >>> Add a jump target so that a bit of exception handling can be better reused
> > >>> at the end of this function implementation.
> > …
> > >> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
> > >
> > > These patches make the code worse.
>
> This is of course a legitimate opinion. However Markus' patch
> implements what is recommended by the documentation and is in common
> use in the kernel code. A quick search found 73 occurrences in v6.8-rc7:
>
> $ expr $(pcregrep -r -M ':\n\tfwnode_handle_put' drivers | wc -l) / 2
> 73
> $
>
> 300+ are found for of_node_put().
>
Using an unwind ladder is the best way to write error handling, yes.
I've written a long blog about it.
https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/
In my blog, I talk about that "Unwinding from loops is slightly
complicated." Because what you want to do is clean up partial
iterations before the goto.
Now imagine we apply Markus's patch and someone comes along an adds a
new allocation after the loop is over. Then we have to do some kind of
bunny hop:
free_new_thing:
free(thing);
goto cleanup; <-- ugly little goto
put_fwnode:
fwnode_handle_put(remote);
cleanup:
dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
v4l2_async_nf_cleanup(&chan->notifier);
return ret;
Adding the little goto seems like a small thing when you're seeing it
in an email like this. But when you add the new goto years later,
people are used to unwind ladders working in a specific way and they
forget that, "Oh this ladder has a weird rung that we have to skip over."
We see these bugs more with locking.
one = alloc();
if (!one)
return;
lock();
two = alloc();
if (!two)
goto free_one; <-- should have unlocked before the goto
unlock;
three = alloc();
if (!three)
goto free_two;
return 0;
free_two:
free(two);
free_one:
unlock();
free(one);
return -ENOMEM;
regards,
dan carpenter
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-03-05 16:21 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-29 18:55 [PATCH] staging: media: tegra-video: Use common error handling code in tegra_vi_graph_parse_one() Markus Elfring
2024-03-01 17:39 ` Luca Ceresoli
2024-03-02 9:30 ` Dan Carpenter
2024-03-02 10:40 ` Markus Elfring
2024-03-05 15:24 ` Luca Ceresoli
2024-03-05 16:21 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox