Merge tag 'gpio-fixes-for-v6.19-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux

Pull gpio fixes from Bartosz Golaszewski:
"There are several ordinary driver fixes and a fix to a race between
the registration of two chips that causes a crash in GPIO core.

The bulk of the changed lines however, concerns the management of
shared GPIOs that landed in v6.19-rc1. Enabling it for ARCH_QCOM
enabled it in defconfig which effectively enabled it for all arm64
platforms and exposed the code to quite a lot of testing (which is
good, right? :)).

As a resukt, I received a number of bug reports, which I progressively
fixed over the course of last weeks. This explains the number of lines
higher than what I normally aim for at this stage.

- balance superio enter/exit calls in error path in gpio-it87

- fix a race where we try to take the SRCU read lock of the GPIO
device before it's been initialized causing a NULL-pointer
dereference

- fix handling of short-pulse interrupts in gpio-pca053x

- fix a reference leak in error path in gpio-mpsse

- mark the GPIO controller as sleeping (it calls sleeping functions)
in gpio-rockchip

- fix several issues in management of shared GPIOs"

* tag 'gpio-fixes-for-v6.19-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux:
gpio: shared: fix a false-positive sharing detection with reset-gpios
gpiolib: fix lookup table matching
gpio: shared: don't allocate the lookup table until we really need it
gpio: shared: fix a race condition
gpio: shared: assign the correct firmware node for reset-gpio use-case
gpio: rockchip: mark the GPIO controller as sleeping
gpio: mpsse: fix reference leak in gpio_mpsse_probe() error paths
gpio: pca953x: handle short interrupt pulses on PCAL devices
gpiolib: fix race condition for gdev->srcu
gpio: shared: allow sharing a reset-gpios pin between reset-gpio and gpiolib
gpio: shared: verify con_id when adding proxy lookup
gpiolib: allow multiple lookup tables per consumer
gpio: it87: balance superio enter/exit calls in error path

+3 -8
drivers/gpio/gpio-it87.c
··· 12 12 13 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 14 15 + #include <linux/cleanup.h> 15 16 #include <linux/init.h> 16 17 #include <linux/kernel.h> 17 18 #include <linux/module.h> ··· 242 241 mask = 1 << (gpio_num % 8); 243 242 group = (gpio_num / 8); 244 243 245 - spin_lock(&it87_gpio->lock); 244 + guard(spinlock)(&it87_gpio->lock); 246 245 247 246 rc = superio_enter(); 248 247 if (rc) 249 - goto exit; 248 + return rc; 250 249 251 250 /* set the output enable bit */ 252 251 superio_set_mask(mask, group + it87_gpio->output_base); 253 252 254 253 rc = it87_gpio_set(chip, gpio_num, val); 255 - if (rc) 256 - goto exit; 257 - 258 254 superio_exit(); 259 - 260 - exit: 261 - spin_unlock(&it87_gpio->lock); 262 255 return rc; 263 256 } 264 257
+11 -1
drivers/gpio/gpio-mpsse.c
··· 548 548 ida_free(&gpio_mpsse_ida, priv->id); 549 549 } 550 550 551 + static void gpio_mpsse_usb_put_dev(void *data) 552 + { 553 + struct mpsse_priv *priv = data; 554 + 555 + usb_put_dev(priv->udev); 556 + } 557 + 551 558 static int mpsse_init_valid_mask(struct gpio_chip *chip, 552 559 unsigned long *valid_mask, 553 560 unsigned int ngpios) ··· 599 592 INIT_LIST_HEAD(&priv->workers); 600 593 601 594 priv->udev = usb_get_dev(interface_to_usbdev(interface)); 595 + err = devm_add_action_or_reset(dev, gpio_mpsse_usb_put_dev, priv); 596 + if (err) 597 + return err; 598 + 602 599 priv->intf = interface; 603 600 priv->intf_id = interface->cur_altsetting->desc.bInterfaceNumber; 604 601 ··· 724 713 725 714 priv->intf = NULL; 726 715 usb_set_intfdata(intf, NULL); 727 - usb_put_dev(priv->udev); 728 716 } 729 717 730 718 static struct usb_driver gpio_mpsse_driver = {
+24 -1
drivers/gpio/gpio-pca953x.c
··· 943 943 DECLARE_BITMAP(old_stat, MAX_LINE); 944 944 DECLARE_BITMAP(cur_stat, MAX_LINE); 945 945 DECLARE_BITMAP(new_stat, MAX_LINE); 946 + DECLARE_BITMAP(int_stat, MAX_LINE); 946 947 DECLARE_BITMAP(trigger, MAX_LINE); 947 948 DECLARE_BITMAP(edges, MAX_LINE); 948 949 int ret; 949 950 951 + if (chip->driver_data & PCA_PCAL) { 952 + /* Read INT_STAT before it is cleared by the input-port read. */ 953 + ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, int_stat); 954 + if (ret) 955 + return false; 956 + } 957 + 950 958 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat); 951 959 if (ret) 952 960 return false; 961 + 962 + if (chip->driver_data & PCA_PCAL) { 963 + /* Detect short pulses via INT_STAT. */ 964 + bitmap_and(trigger, int_stat, chip->irq_mask, gc->ngpio); 965 + 966 + /* Apply filter for rising/falling edge selection. */ 967 + bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, 968 + cur_stat, gc->ngpio); 969 + 970 + bitmap_and(int_stat, new_stat, trigger, gc->ngpio); 971 + } else { 972 + bitmap_zero(int_stat, gc->ngpio); 973 + } 953 974 954 975 /* Remove output pins from the equation */ 955 976 pca953x_read_regs(chip, chip->regs->direction, reg_direction); ··· 985 964 986 965 if (bitmap_empty(chip->irq_trig_level_high, gc->ngpio) && 987 966 bitmap_empty(chip->irq_trig_level_low, gc->ngpio)) { 988 - if (bitmap_empty(trigger, gc->ngpio)) 967 + if (bitmap_empty(trigger, gc->ngpio) && 968 + bitmap_empty(int_stat, gc->ngpio)) 989 969 return false; 990 970 } 991 971 ··· 994 972 bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio); 995 973 bitmap_or(edges, old_stat, cur_stat, gc->ngpio); 996 974 bitmap_and(pending, edges, trigger, gc->ngpio); 975 + bitmap_or(pending, pending, int_stat, gc->ngpio); 997 976 998 977 bitmap_and(cur_stat, new_stat, chip->irq_trig_level_high, gc->ngpio); 999 978 bitmap_and(cur_stat, cur_stat, chip->irq_mask, gc->ngpio);
+1
drivers/gpio/gpio-rockchip.c
··· 593 593 gc->ngpio = bank->nr_pins; 594 594 gc->label = bank->name; 595 595 gc->parent = bank->dev; 596 + gc->can_sleep = true; 596 597 597 598 ret = gpiochip_add_data(gc, bank); 598 599 if (ret) {
+180 -71
drivers/gpio/gpiolib-shared.c
··· 38 38 int dev_id; 39 39 /* Protects the auxiliary device struct and the lookup table. */ 40 40 struct mutex lock; 41 + struct lock_class_key lock_key; 41 42 struct auxiliary_device adev; 42 43 struct gpiod_lookup_table *lookup; 44 + bool is_reset_gpio; 43 45 }; 44 46 45 47 /* Represents a single GPIO pin. */ ··· 78 76 return NULL; 79 77 } 80 78 79 + static struct gpio_shared_ref *gpio_shared_make_ref(struct fwnode_handle *fwnode, 80 + const char *con_id, 81 + enum gpiod_flags flags) 82 + { 83 + char *con_id_cpy __free(kfree) = NULL; 84 + 85 + struct gpio_shared_ref *ref __free(kfree) = kzalloc(sizeof(*ref), GFP_KERNEL); 86 + if (!ref) 87 + return NULL; 88 + 89 + if (con_id) { 90 + con_id_cpy = kstrdup(con_id, GFP_KERNEL); 91 + if (!con_id_cpy) 92 + return NULL; 93 + } 94 + 95 + ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL); 96 + if (ref->dev_id < 0) 97 + return NULL; 98 + 99 + ref->flags = flags; 100 + ref->con_id = no_free_ptr(con_id_cpy); 101 + ref->fwnode = fwnode; 102 + lockdep_register_key(&ref->lock_key); 103 + mutex_init_with_key(&ref->lock, &ref->lock_key); 104 + 105 + return no_free_ptr(ref); 106 + } 107 + 108 + static int gpio_shared_setup_reset_proxy(struct gpio_shared_entry *entry, 109 + enum gpiod_flags flags) 110 + { 111 + struct gpio_shared_ref *ref; 112 + 113 + list_for_each_entry(ref, &entry->refs, list) { 114 + if (ref->is_reset_gpio) 115 + /* Already set-up. */ 116 + return 0; 117 + } 118 + 119 + ref = gpio_shared_make_ref(NULL, "reset", flags); 120 + if (!ref) 121 + return -ENOMEM; 122 + 123 + ref->is_reset_gpio = true; 124 + 125 + list_add_tail(&ref->list, &entry->refs); 126 + 127 + pr_debug("Created a secondary shared GPIO reference for potential reset-gpio device for GPIO %u at %s\n", 128 + entry->offset, fwnode_get_name(entry->fwnode)); 129 + 130 + return 0; 131 + } 132 + 81 133 /* Handle all special nodes that we should ignore. */ 82 134 static bool gpio_shared_of_node_ignore(struct device_node *node) 83 135 { ··· 162 106 size_t con_id_len, suffix_len; 163 107 struct fwnode_handle *fwnode; 164 108 struct of_phandle_args args; 109 + struct gpio_shared_ref *ref; 165 110 struct property *prop; 166 111 unsigned int offset; 167 112 const char *suffix; ··· 195 138 196 139 for (i = 0; i < count; i++) { 197 140 struct device_node *np __free(device_node) = NULL; 141 + char *con_id __free(kfree) = NULL; 198 142 199 143 ret = of_parse_phandle_with_args(curr, prop->name, 200 144 "#gpio-cells", i, ··· 240 182 list_add_tail(&entry->list, &gpio_shared_list); 241 183 } 242 184 243 - struct gpio_shared_ref *ref __free(kfree) = 244 - kzalloc(sizeof(*ref), GFP_KERNEL); 245 - if (!ref) 246 - return -ENOMEM; 247 - 248 - ref->fwnode = fwnode_handle_get(of_fwnode_handle(curr)); 249 - ref->flags = args.args[1]; 250 - mutex_init(&ref->lock); 251 - 252 185 if (strends(prop->name, "gpios")) 253 186 suffix = "-gpios"; 254 187 else if (strends(prop->name, "gpio")) ··· 251 202 252 203 /* We only set con_id if there's actually one. */ 253 204 if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) { 254 - ref->con_id = kstrdup(prop->name, GFP_KERNEL); 255 - if (!ref->con_id) 205 + con_id = kstrdup(prop->name, GFP_KERNEL); 206 + if (!con_id) 256 207 return -ENOMEM; 257 208 258 - con_id_len = strlen(ref->con_id); 209 + con_id_len = strlen(con_id); 259 210 suffix_len = strlen(suffix); 260 211 261 - ref->con_id[con_id_len - suffix_len] = '\0'; 212 + con_id[con_id_len - suffix_len] = '\0'; 262 213 } 263 214 264 - ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL); 265 - if (ref->dev_id < 0) { 266 - kfree(ref->con_id); 215 + ref = gpio_shared_make_ref(fwnode_handle_get(of_fwnode_handle(curr)), 216 + con_id, args.args[1]); 217 + if (!ref) 267 218 return -ENOMEM; 268 - } 269 219 270 220 if (!list_empty(&entry->refs)) 271 221 pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n", 272 222 entry->offset, fwnode_get_name(entry->fwnode)); 273 223 274 - list_add_tail(&no_free_ptr(ref)->list, &entry->refs); 224 + list_add_tail(&ref->list, &entry->refs); 225 + 226 + if (strcmp(prop->name, "reset-gpios") == 0) { 227 + ret = gpio_shared_setup_reset_proxy(entry, args.args[1]); 228 + if (ret) 229 + return ret; 230 + } 275 231 } 276 232 } 277 233 ··· 360 306 struct fwnode_handle *reset_fwnode = dev_fwnode(consumer); 361 307 struct fwnode_reference_args ref_args, aux_args; 362 308 struct device *parent = consumer->parent; 309 + struct gpio_shared_ref *real_ref; 363 310 bool match; 364 311 int ret; 365 312 313 + lockdep_assert_held(&ref->lock); 314 + 366 315 /* The reset-gpio device must have a parent AND a firmware node. */ 367 316 if (!parent || !reset_fwnode) 368 - return false; 369 - 370 - /* 371 - * FIXME: use device_is_compatible() once the reset-gpio drivers gains 372 - * a compatible string which it currently does not have. 373 - */ 374 - if (!strstarts(dev_name(consumer), "reset.gpio.")) 375 317 return false; 376 318 377 319 /* ··· 378 328 return false; 379 329 380 330 /* 381 - * The device associated with the shared reference's firmware node is 382 - * the consumer of the reset control exposed by the reset-gpio device. 383 - * It must have a "reset-gpios" property that's referencing the entry's 384 - * firmware node. 385 - * 386 - * The reference args must agree between the real consumer and the 387 - * auxiliary reset-gpio device. 331 + * Now we need to find the actual pin we want to assign to this 332 + * reset-gpio device. To that end: iterate over the list of references 333 + * of this entry and see if there's one, whose reset-gpios property's 334 + * arguments match the ones from this consumer's node. 388 335 */ 389 - ret = fwnode_property_get_reference_args(ref->fwnode, "reset-gpios", 390 - NULL, 2, 0, &ref_args); 391 - if (ret) 392 - return false; 336 + list_for_each_entry(real_ref, &entry->refs, list) { 337 + if (real_ref == ref) 338 + continue; 393 339 394 - ret = fwnode_property_get_reference_args(reset_fwnode, "reset-gpios", 395 - NULL, 2, 0, &aux_args); 396 - if (ret) { 340 + guard(mutex)(&real_ref->lock); 341 + 342 + if (!real_ref->fwnode) 343 + continue; 344 + 345 + /* 346 + * The device associated with the shared reference's firmware 347 + * node is the consumer of the reset control exposed by the 348 + * reset-gpio device. It must have a "reset-gpios" property 349 + * that's referencing the entry's firmware node. 350 + * 351 + * The reference args must agree between the real consumer and 352 + * the auxiliary reset-gpio device. 353 + */ 354 + ret = fwnode_property_get_reference_args(real_ref->fwnode, 355 + "reset-gpios", 356 + NULL, 2, 0, &ref_args); 357 + if (ret) 358 + continue; 359 + 360 + ret = fwnode_property_get_reference_args(reset_fwnode, "reset-gpios", 361 + NULL, 2, 0, &aux_args); 362 + if (ret) { 363 + fwnode_handle_put(ref_args.fwnode); 364 + continue; 365 + } 366 + 367 + match = ((ref_args.fwnode == entry->fwnode) && 368 + (aux_args.fwnode == entry->fwnode) && 369 + (ref_args.args[0] == aux_args.args[0])); 370 + 397 371 fwnode_handle_put(ref_args.fwnode); 398 - return false; 372 + fwnode_handle_put(aux_args.fwnode); 373 + 374 + if (!match) 375 + continue; 376 + 377 + /* 378 + * Reuse the fwnode of the real device, next time we'll use it 379 + * in the normal path. 380 + */ 381 + ref->fwnode = fwnode_handle_get(reset_fwnode); 382 + return true; 399 383 } 400 384 401 - match = ((ref_args.fwnode == entry->fwnode) && 402 - (aux_args.fwnode == entry->fwnode) && 403 - (ref_args.args[0] == aux_args.args[0])); 404 - 405 - fwnode_handle_put(ref_args.fwnode); 406 - fwnode_handle_put(aux_args.fwnode); 407 - return match; 385 + return false; 408 386 } 409 387 #else 410 388 static bool gpio_shared_dev_is_reset_gpio(struct device *consumer, ··· 443 365 } 444 366 #endif /* CONFIG_RESET_GPIO */ 445 367 446 - int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags) 368 + int gpio_shared_add_proxy_lookup(struct device *consumer, const char *con_id, 369 + unsigned long lflags) 447 370 { 448 371 const char *dev_id = dev_name(consumer); 372 + struct gpiod_lookup_table *lookup; 449 373 struct gpio_shared_entry *entry; 450 374 struct gpio_shared_ref *ref; 451 375 452 - struct gpiod_lookup_table *lookup __free(kfree) = 453 - kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); 454 - if (!lookup) 455 - return -ENOMEM; 456 - 457 376 list_for_each_entry(entry, &gpio_shared_list, list) { 458 377 list_for_each_entry(ref, &entry->refs, list) { 459 - if (!device_match_fwnode(consumer, ref->fwnode) && 460 - !gpio_shared_dev_is_reset_gpio(consumer, entry, ref)) 461 - continue; 462 - 463 378 guard(mutex)(&ref->lock); 379 + 380 + /* 381 + * FIXME: use device_is_compatible() once the reset-gpio 382 + * drivers gains a compatible string which it currently 383 + * does not have. 384 + */ 385 + if (!ref->fwnode && strstarts(dev_name(consumer), "reset.gpio.")) { 386 + if (!gpio_shared_dev_is_reset_gpio(consumer, entry, ref)) 387 + continue; 388 + } else if (!device_match_fwnode(consumer, ref->fwnode)) { 389 + continue; 390 + } 391 + 392 + if ((!con_id && ref->con_id) || (con_id && !ref->con_id) || 393 + (con_id && ref->con_id && strcmp(con_id, ref->con_id) != 0)) 394 + continue; 464 395 465 396 /* We've already done that on a previous request. */ 466 397 if (ref->lookup) ··· 482 395 if (!key) 483 396 return -ENOMEM; 484 397 398 + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); 399 + if (!lookup) 400 + return -ENOMEM; 401 + 485 402 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n", 486 403 dev_id, key, ref->con_id ?: "none"); 487 404 ··· 493 402 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0, 494 403 ref->con_id, lflags); 495 404 496 - ref->lookup = no_free_ptr(lookup); 405 + ref->lookup = lookup; 497 406 gpiod_add_lookup_table(ref->lookup); 498 407 499 408 return 0; ··· 557 466 entry->offset, gpio_device_get_label(gdev)); 558 467 559 468 list_for_each_entry(ref, &entry->refs, list) { 560 - pr_debug("Setting up a shared GPIO entry for %s\n", 561 - fwnode_get_name(ref->fwnode)); 469 + pr_debug("Setting up a shared GPIO entry for %s (con_id: '%s')\n", 470 + fwnode_get_name(ref->fwnode) ?: "(no fwnode)", 471 + ref->con_id ?: "(none)"); 562 472 563 473 ret = gpio_shared_make_adev(gdev, entry, ref); 564 474 if (ret) ··· 579 487 if (!device_match_fwnode(&gdev->dev, entry->fwnode)) 580 488 continue; 581 489 582 - /* 583 - * For some reason if we call synchronize_srcu() in GPIO core, 584 - * descent here and take this mutex and then recursively call 585 - * synchronize_srcu() again from gpiochip_remove() (which is 586 - * totally fine) called after gpio_shared_remove_adev(), 587 - * lockdep prints a false positive deadlock splat. Disable 588 - * lockdep here. 589 - */ 590 - lockdep_off(); 591 490 list_for_each_entry(ref, &entry->refs, list) { 592 491 guard(mutex)(&ref->lock); 593 492 ··· 591 508 592 509 gpio_shared_remove_adev(&ref->adev); 593 510 } 594 - lockdep_on(); 595 511 } 596 512 } 597 513 ··· 686 604 { 687 605 list_del(&ref->list); 688 606 mutex_destroy(&ref->lock); 607 + lockdep_unregister_key(&ref->lock_key); 689 608 kfree(ref->con_id); 690 609 ida_free(&gpio_shared_ida, ref->dev_id); 691 610 fwnode_handle_put(ref->fwnode); ··· 718 635 } 719 636 } 720 637 638 + static bool gpio_shared_entry_is_really_shared(struct gpio_shared_entry *entry) 639 + { 640 + size_t num_nodes = list_count_nodes(&entry->refs); 641 + struct gpio_shared_ref *ref; 642 + 643 + if (num_nodes <= 1) 644 + return false; 645 + 646 + if (num_nodes > 2) 647 + return true; 648 + 649 + /* Exactly two references: */ 650 + list_for_each_entry(ref, &entry->refs, list) { 651 + /* 652 + * Corner-case: the second reference comes from the potential 653 + * reset-gpio instance. However, this pin is not really shared 654 + * as it would have three references in this case. Avoid 655 + * creating unnecessary proxies. 656 + */ 657 + if (ref->is_reset_gpio) 658 + return false; 659 + } 660 + 661 + return true; 662 + } 663 + 721 664 static void gpio_shared_free_exclusive(void) 722 665 { 723 666 struct gpio_shared_entry *entry, *epos; 724 667 725 668 list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) { 726 - if (list_count_nodes(&entry->refs) > 1) 669 + if (gpio_shared_entry_is_really_shared(entry)) 727 670 continue; 728 671 729 672 gpio_shared_drop_ref(list_first_entry(&entry->refs,
+3 -1
drivers/gpio/gpiolib-shared.h
··· 16 16 17 17 int gpio_device_setup_shared(struct gpio_device *gdev); 18 18 void gpio_device_teardown_shared(struct gpio_device *gdev); 19 - int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags); 19 + int gpio_shared_add_proxy_lookup(struct device *consumer, const char *con_id, 20 + unsigned long lflags); 20 21 21 22 #else 22 23 ··· 29 28 static inline void gpio_device_teardown_shared(struct gpio_device *gdev) { } 30 29 31 30 static inline int gpio_shared_add_proxy_lookup(struct device *consumer, 31 + const char *con_id, 32 32 unsigned long lflags) 33 33 { 34 34 return 0;
+79 -57
drivers/gpio/gpiolib.c
··· 1105 1105 gdev->ngpio = gc->ngpio; 1106 1106 gdev->can_sleep = gc->can_sleep; 1107 1107 1108 + rwlock_init(&gdev->line_state_lock); 1109 + RAW_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); 1110 + BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); 1111 + 1112 + ret = init_srcu_struct(&gdev->srcu); 1113 + if (ret) 1114 + goto err_free_label; 1115 + 1116 + ret = init_srcu_struct(&gdev->desc_srcu); 1117 + if (ret) 1118 + goto err_cleanup_gdev_srcu; 1119 + 1108 1120 scoped_guard(mutex, &gpio_devices_lock) { 1109 1121 /* 1110 1122 * TODO: this allocates a Linux GPIO number base in the global ··· 1131 1119 if (base < 0) { 1132 1120 ret = base; 1133 1121 base = 0; 1134 - goto err_free_label; 1122 + goto err_cleanup_desc_srcu; 1135 1123 } 1136 1124 1137 1125 /* ··· 1151 1139 ret = gpiodev_add_to_list_unlocked(gdev); 1152 1140 if (ret) { 1153 1141 gpiochip_err(gc, "GPIO integer space overlap, cannot add chip\n"); 1154 - goto err_free_label; 1142 + goto err_cleanup_desc_srcu; 1155 1143 } 1156 1144 } 1157 - 1158 - rwlock_init(&gdev->line_state_lock); 1159 - RAW_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); 1160 - BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); 1161 - 1162 - ret = init_srcu_struct(&gdev->srcu); 1163 - if (ret) 1164 - goto err_remove_from_list; 1165 - 1166 - ret = init_srcu_struct(&gdev->desc_srcu); 1167 - if (ret) 1168 - goto err_cleanup_gdev_srcu; 1169 1145 1170 1146 #ifdef CONFIG_PINCTRL 1171 1147 INIT_LIST_HEAD(&gdev->pin_ranges); ··· 1164 1164 1165 1165 ret = gpiochip_set_names(gc); 1166 1166 if (ret) 1167 - goto err_cleanup_desc_srcu; 1167 + goto err_remove_from_list; 1168 1168 1169 1169 ret = gpiochip_init_valid_mask(gc); 1170 1170 if (ret) 1171 - goto err_cleanup_desc_srcu; 1171 + goto err_remove_from_list; 1172 1172 1173 1173 for (desc_index = 0; desc_index < gc->ngpio; desc_index++) { 1174 1174 struct gpio_desc *desc = &gdev->descs[desc_index]; ··· 1248 1248 of_gpiochip_remove(gc); 1249 1249 err_free_valid_mask: 1250 1250 gpiochip_free_valid_mask(gc); 1251 - err_cleanup_desc_srcu: 1252 - cleanup_srcu_struct(&gdev->desc_srcu); 1253 - err_cleanup_gdev_srcu: 1254 - cleanup_srcu_struct(&gdev->srcu); 1255 1251 err_remove_from_list: 1256 1252 scoped_guard(mutex, &gpio_devices_lock) 1257 1253 list_del_rcu(&gdev->list); ··· 1257 1261 gpio_device_put(gdev); 1258 1262 goto err_print_message; 1259 1263 } 1264 + err_cleanup_desc_srcu: 1265 + cleanup_srcu_struct(&gdev->desc_srcu); 1266 + err_cleanup_gdev_srcu: 1267 + cleanup_srcu_struct(&gdev->srcu); 1260 1268 err_free_label: 1261 1269 kfree_const(gdev->label); 1262 1270 err_free_descs: ··· 4508 4508 } 4509 4509 EXPORT_SYMBOL_GPL(gpiod_remove_hogs); 4510 4510 4511 - static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) 4511 + static bool gpiod_match_lookup_table(struct device *dev, 4512 + const struct gpiod_lookup_table *table) 4512 4513 { 4513 4514 const char *dev_id = dev ? dev_name(dev) : NULL; 4514 - struct gpiod_lookup_table *table; 4515 4515 4516 - list_for_each_entry(table, &gpio_lookup_list, list) { 4517 - if (table->dev_id && dev_id) { 4518 - /* 4519 - * Valid strings on both ends, must be identical to have 4520 - * a match 4521 - */ 4522 - if (!strcmp(table->dev_id, dev_id)) 4523 - return table; 4524 - } else { 4525 - /* 4526 - * One of the pointers is NULL, so both must be to have 4527 - * a match 4528 - */ 4529 - if (dev_id == table->dev_id) 4530 - return table; 4531 - } 4516 + lockdep_assert_held(&gpio_lookup_lock); 4517 + 4518 + if (table->dev_id && dev_id) { 4519 + /* 4520 + * Valid strings on both ends, must be identical to have 4521 + * a match 4522 + */ 4523 + if (!strcmp(table->dev_id, dev_id)) 4524 + return true; 4525 + } else { 4526 + /* 4527 + * One of the pointers is NULL, so both must be to have 4528 + * a match 4529 + */ 4530 + if (dev_id == table->dev_id) 4531 + return true; 4532 4532 } 4533 4533 4534 - return NULL; 4534 + return false; 4535 4535 } 4536 4536 4537 - static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 4538 - unsigned int idx, unsigned long *flags) 4537 + static struct gpio_desc *gpio_desc_table_match(struct device *dev, const char *con_id, 4538 + unsigned int idx, unsigned long *flags, 4539 + struct gpiod_lookup_table *table) 4539 4540 { 4540 - struct gpio_desc *desc = ERR_PTR(-ENOENT); 4541 - struct gpiod_lookup_table *table; 4541 + struct gpio_desc *desc; 4542 4542 struct gpiod_lookup *p; 4543 4543 struct gpio_chip *gc; 4544 4544 4545 - guard(mutex)(&gpio_lookup_lock); 4546 - 4547 - table = gpiod_find_lookup_table(dev); 4548 - if (!table) 4549 - return desc; 4545 + lockdep_assert_held(&gpio_lookup_lock); 4550 4546 4551 4547 for (p = &table->table[0]; p->key; p++) { 4552 4548 /* idx must always match exactly */ ··· 4596 4600 return desc; 4597 4601 } 4598 4602 4599 - return desc; 4603 + return NULL; 4604 + } 4605 + 4606 + static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 4607 + unsigned int idx, unsigned long *flags) 4608 + { 4609 + struct gpiod_lookup_table *table; 4610 + struct gpio_desc *desc; 4611 + 4612 + guard(mutex)(&gpio_lookup_lock); 4613 + 4614 + list_for_each_entry(table, &gpio_lookup_list, list) { 4615 + if (!gpiod_match_lookup_table(dev, table)) 4616 + continue; 4617 + 4618 + desc = gpio_desc_table_match(dev, con_id, idx, flags, table); 4619 + if (!desc) 4620 + continue; 4621 + 4622 + /* On IS_ERR() or match. */ 4623 + return desc; 4624 + } 4625 + 4626 + return ERR_PTR(-ENOENT); 4600 4627 } 4601 4628 4602 4629 static int platform_gpio_count(struct device *dev, const char *con_id) ··· 4629 4610 unsigned int count = 0; 4630 4611 4631 4612 scoped_guard(mutex, &gpio_lookup_lock) { 4632 - table = gpiod_find_lookup_table(dev); 4633 - if (!table) 4634 - return -ENOENT; 4613 + list_for_each_entry(table, &gpio_lookup_list, list) { 4614 + if (!gpiod_match_lookup_table(dev, table)) 4615 + continue; 4635 4616 4636 - for (p = &table->table[0]; p->key; p++) { 4637 - if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || 4638 - (!con_id && !p->con_id)) 4639 - count++; 4617 + for (p = &table->table[0]; p->key; p++) { 4618 + if ((con_id && p->con_id && 4619 + !strcmp(con_id, p->con_id)) || 4620 + (!con_id && !p->con_id)) 4621 + count++; 4622 + } 4640 4623 } 4641 4624 } 4642 4625 ··· 4717 4696 * lookup table for the proxy device as previously 4718 4697 * we only knew the consumer's fwnode. 4719 4698 */ 4720 - ret = gpio_shared_add_proxy_lookup(consumer, lookupflags); 4699 + ret = gpio_shared_add_proxy_lookup(consumer, con_id, 4700 + lookupflags); 4721 4701 if (ret) 4722 4702 return ERR_PTR(ret); 4723 4703