Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

authored by

Andy Shevchenko and committed by
Bartosz Golaszewski
8ae438f5 e9c717be

+11 -15
+11 -15
drivers/gpio/gpiolib.c
··· 861 861 struct lock_class_key *request_key) 862 862 { 863 863 struct gpio_device *gdev; 864 - unsigned int i, j; 864 + unsigned int desc_index; 865 865 int base = 0; 866 866 int ret = 0; 867 867 ··· 965 965 } 966 966 } 967 967 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; 970 970 971 971 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); 972 972 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); ··· 992 992 if (ret) 993 993 goto err_cleanup_gdev_srcu; 994 994 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]; 997 997 998 998 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; 1004 1001 1005 - if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { 1002 + if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) { 1006 1003 assign_bit(FLAG_IS_OUT, 1007 - &desc->flags, !gc->get_direction(gc, i)); 1004 + &desc->flags, !gc->get_direction(gc, desc_index)); 1008 1005 } else { 1009 1006 assign_bit(FLAG_IS_OUT, 1010 1007 &desc->flags, !gc->direction_input); ··· 1058 1061 err_remove_of_chip: 1059 1062 of_gpiochip_remove(gc); 1060 1063 err_cleanup_desc_srcu: 1061 - for (i = 0; i < gdev->ngpio; i++) 1062 - cleanup_srcu_struct(&gdev->descs[i].srcu); 1063 - err_free_gpiochip_mask: 1064 + while (desc_index--) 1065 + cleanup_srcu_struct(&gdev->descs[desc_index].srcu); 1064 1066 gpiochip_free_valid_mask(gc); 1065 1067 err_cleanup_gdev_srcu: 1066 1068 cleanup_srcu_struct(&gdev->srcu);