Skip to content

Commit 8ae438f

Browse files
andy-shevBartosz Golaszewski
authored andcommitted
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. Signed-off-by: Andy Shevchenko <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent e9c717b commit 8ae438f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

drivers/gpio/gpiolib.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
861861
struct lock_class_key *request_key)
862862
{
863863
struct gpio_device *gdev;
864-
unsigned int i, j;
864+
unsigned int desc_index;
865865
int base = 0;
866866
int ret = 0;
867867

@@ -965,8 +965,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
965965
}
966966
}
967967

968-
for (i = 0; i < gc->ngpio; i++)
969-
gdev->descs[i].gdev = gdev;
968+
for (desc_index = 0; desc_index < gc->ngpio; desc_index++)
969+
gdev->descs[desc_index].gdev = gdev;
970970

971971
BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier);
972972
BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier);
@@ -992,19 +992,16 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
992992
if (ret)
993993
goto err_cleanup_gdev_srcu;
994994

995-
for (i = 0; i < gc->ngpio; i++) {
996-
struct gpio_desc *desc = &gdev->descs[i];
995+
for (desc_index = 0; desc_index < gc->ngpio; desc_index++) {
996+
struct gpio_desc *desc = &gdev->descs[desc_index];
997997

998998
ret = init_srcu_struct(&desc->srcu);
999-
if (ret) {
1000-
for (j = 0; j < i; j++)
1001-
cleanup_srcu_struct(&gdev->descs[j].srcu);
1002-
goto err_free_gpiochip_mask;
1003-
}
999+
if (ret)
1000+
goto err_cleanup_desc_srcu;
10041001

1005-
if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
1002+
if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
10061003
assign_bit(FLAG_IS_OUT,
1007-
&desc->flags, !gc->get_direction(gc, i));
1004+
&desc->flags, !gc->get_direction(gc, desc_index));
10081005
} else {
10091006
assign_bit(FLAG_IS_OUT,
10101007
&desc->flags, !gc->direction_input);
@@ -1061,9 +1058,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
10611058
err_remove_of_chip:
10621059
of_gpiochip_remove(gc);
10631060
err_cleanup_desc_srcu:
1064-
for (i = 0; i < gdev->ngpio; i++)
1065-
cleanup_srcu_struct(&gdev->descs[i].srcu);
1066-
err_free_gpiochip_mask:
1061+
while (desc_index--)
1062+
cleanup_srcu_struct(&gdev->descs[desc_index].srcu);
10671063
gpiochip_free_valid_mask(gc);
10681064
err_cleanup_gdev_srcu:
10691065
cleanup_srcu_struct(&gdev->srcu);

0 commit comments

Comments
 (0)