gpiolib: Deduplicate cleanup for-loop in gpiochip_add_data_with_key()
PATCH 1/1 gpiolib: Deduplicate cleanup for-loop in gpiochip_add_data_with_key()
There is no need to repeat for-loop twice in the error path in gpiochip_add_data_with_key(). Deduplicate it. While at it, rename loop variable to be more specific and avoid ambguity.
It also properly unwinds the SRCU, i.e. in reversed order of allocating.
--- drivers/gpio/gpiolib.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/gpio/gpiolib.c +11 -15
@@ -861,7 +861,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *request_key) {struct gpio_device *gdev; -unsigned int i, j; +unsigned int desc_index;int base =0 ;int ret =0 ;
@@ -965,8 +965,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } } -for (i =0 ; i < gc->ngpio; i++) - gdev->descs[i].gdev = gdev; +for (desc_index =0 ; desc_index < gc->ngpio; desc_index++) + gdev->descs[desc_index].gdev = gdev;BLOCKING_INIT_NOTIFIER_HEAD (&gdev->line_state_notifier);BLOCKING_INIT_NOTIFIER_HEAD (&gdev->device_notifier);
@@ -992,19 +992,16 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,if (ret) goto err_cleanup_gdev_srcu; -for (i =0 ; i < gc->ngpio; i++) { -struct gpio_desc *desc = &gdev->descs[i]; +for (desc_index =0 ; desc_index < gc->ngpio; desc_index++) { +struct gpio_desc *desc = &gdev->descs[desc_index]; ret =init_srcu_struct (&desc->srcu); -if (ret) { -for (j =0 ; j < i; j++) -cleanup_srcu_struct (&gdev->descs[j].srcu); - goto err_free_gpiochip_mask; - } +if (ret) + goto err_cleanup_desc_srcu; -if (gc->get_direction &&gpiochip_line_is_valid (gc, i)) { +if (gc->get_direction &&gpiochip_line_is_valid (gc, desc_index)) {assign_bit (FLAG_IS_OUT , - &desc->flags, !gc->get_direction (gc, i)); + &desc->flags, !gc->get_direction (gc, desc_index)); }else {assign_bit (FLAG_IS_OUT , &desc->flags, !gc->direction_input);
@@ -1061,9 +1058,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,gpiochip_free_hogs (gc);of_gpiochip_remove (gc); err_cleanup_desc_srcu: -for (i =0 ; i < gdev->ngpio; i++) -cleanup_srcu_struct (&gdev->descs[i].srcu); -err_free_gpiochip_mask: +while (desc_index--) +cleanup_srcu_struct (&gdev->descs[desc_index].srcu);gpiochip_free_valid_mask (gc); err_cleanup_gdev_srcu:cleanup_srcu_struct (&gdev->srcu);
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
Quoted 9 lines · Andy Shevchenko
There is no need to repeat for-loop twice in the error path in
gpiochip_add_data_with_key(). Deduplicate it. While at it, rename loop variable to be more specific and avoid ambguity.It also properly unwinds the SRCU, i.e. in reversed order of allocating.
Signed-off-by Andy Shevchenko <[email protected]>---
This doesn't apply on top of gpio/for-next, I think it depends on one of your earlier patches?
Quoted 54 lines
drivers/gpio/gpiolib.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-)drivers/gpio/gpiolib.c +9
-12@@ -861,7 +861,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *request_key) {struct gpio_device *gdev; -unsigned int i, j; +unsigned int desc_index;int base =0 ;int ret =0 ;@@ -965,8 +965,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } } -for (i =0 ; i < gc->ngpio; i++) - gdev->descs[i].gdev = gdev; +for (desc_index =0 ; desc_index < gc->ngpio; desc_index++) + gdev->descs[desc_index].gdev = gdev;BLOCKING_INIT_NOTIFIER_HEAD (&gdev->line_state_notifier);BLOCKING_INIT_NOTIFIER_HEAD (&gdev->device_notifier);@@ -992,19 +992,16 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,if (ret) goto err_cleanup_gdev_srcu; -for (i =0 ; i < gc->ngpio; i++) { -struct gpio_desc *desc = &gdev->descs[i]; +for (desc_index =0 ; desc_index < gc->ngpio; desc_index++) { +struct gpio_desc *desc = &gdev->descs[desc_index]; ret =init_srcu_struct (&desc->srcu); -if (ret) { -for (j =0 ; j < i; j++) -cleanup_srcu_struct (&gdev->descs[j].srcu); - goto err_free_gpiochip_mask; - } +if (ret) + goto err_cleanup_desc_srcu; -if (gc->get_direction &&gpiochip_line_is_valid (gc, i)) { +if (gc->get_direction &&gpiochip_line_is_valid (gc, desc_index)) {assign_bit (FLAG_IS_OUT , - &desc->flags, !gc->get_direction (gc, i)); + &desc->flags, !gc->get_direction (gc, desc_index)); }else {assign_bit (FLAG_IS_OUT , &desc->flags, !gc->direction_input);
drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 1061
Quoted 7 lines
drivers/gpio/gpiolib.c +1
-3@@ -1061,9 +1058,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,gpiochip_free_hogs (gc);of_gpiochip_remove (gc); err_cleanup_desc_srcu: -for (i =0 ; i < gdev->ngpio; i++) -cleanup_srcu_struct (&gdev->descs[i].srcu); -err_free_gpiochip_mask: +while (desc_index--)
What about gdev->descs[0]?
Quoted 7 lines
drivers/gpio/gpiolib.c +1
-1+cleanup_srcu_struct (&gdev->descs[desc_index].srcu);gpiochip_free_valid_mask (gc); err_cleanup_gdev_srcu:cleanup_srcu_struct (&gdev->srcu); --2.43.0.rc1.1.gbec44491f096
Bart
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
There is no need to repeat for-loop twice in the error path in
gpiochip_add_data_with_key(). Deduplicate it. While at it, rename loop variable to be more specific and avoid ambguity.It also properly unwinds the SRCU, i.e. in reversed order of allocating.
...
Quoted 2 lines
This doesn't apply on top of gpio/for-next, I think it depends on one of your earlier patches?
Yes, on the fix with error path.
...
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)
are all equivalents.
The difference is what the value will i get _after_ the loop.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 26 lines · Andy Shevchenko
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
There is no need to repeat for-loop twice in the error path in
gpiochip_add_data_with_key(). Deduplicate it. While at it, rename loop variable to be more specific and avoid ambguity.It also properly unwinds the SRCU, i.e. in reversed order of allocating.
...
Quoted 2 lines
This doesn't apply on top of gpio/for-next, I think it depends on one of your earlier patches?
Yes, on the fix with error path.
...
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
Quoted 7 lines
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
Bart
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)
Quoted 7 lines
Quoted 3 lines
are all equivalents.
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
I disagree. while (i--) is very well known cleanup pattern. Less letters to parse, easier to understand.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 22 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 12 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
Quoted 9 lines
Quoted 5 lines
Quoted 1 line
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
I disagree.
while (i--)is very well known cleanup pattern. Less letters to parse, easier to understand.
Whatever, I don't have a strong opinion, just rebase it and resend.
Bart
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
The difference is what the value will i get _after_ the loop.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
I disagree.
while (i--)is very well known cleanup pattern. Less letters to parse, easier to understand.Whatever, I don't have a strong opinion, just rebase it and resend.
Sure (just will wait to the fix to be settled down first), thanks for review!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 31 lines · Andy Shevchenko
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
Quoted 7 lines
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I disagree.
while (i--)is very well known cleanup pattern. Less letters to parse, easier to understand.Whatever, I don't have a strong opinion, just rebase it and resend.
Sure (just will wait to the fix to be settled down first), thanks for review!
I realized you haven't resent it after all, do you still want to change this?
Bart
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Bartosz Golaszewski
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
Quoted 7 lines
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I disagree.
while (i--)is very well known cleanup pattern. Less letters to parse, easier to understand.Whatever, I don't have a strong opinion, just rebase it and resend.
Sure (just will wait to the fix to be settled down first), thanks for review!
I realized you haven't resent it after all, do you still want to change this?
Yes. U can prepare a new version later today.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski
On Wed, Feb 21, 2024 at 8:36 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line
+ while (desc_index--)
What about gdev->descs[0]?
What about it? :-)
for (i = i - 1; i >= 0; i--)
while (--i >= 0)
while (i--)are all equivalents.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
The difference is what the value will i get _after_ the loop.
Ugh of course. But the first one is more readable given I got tricked by variant #3 at a quick glance but the for loop says out loud what it does.
I disagree.
while (i--)is very well known cleanup pattern. Less letters to parse, easier to understand.
$ git grep -n 'while (i--)' | wc -l 298 $ git grep -n 'while (--i >= 0)' | wc -l 246 $ git grep -n 'for (--i; i >= 0; i--)' | wc -l 29 $ git grep -n 'for (i = i - 1; i >= 0; i--)' | wc -l 17
Signature · 2 lines
With Best Regards, Andy Shevchenko