Compare

bd22f7e63509fa758cf500d9af3e88a3d3df96be..205cccd06e545694754e1bf8d626ea96e742cd8a

gpio: mxc: fix race between gpiochip registration and IRQ domain setup

devm_gpiochip_add_data() makes the GPIO chip visible to consumers
immediately. Since the chip sets .to_irq = mxc_gpio_to_irq, which calls
irq_find_mapping(port->domain, offset), any to_irq() call between
gpiochip registration and IRQ domain creation will operate on a NULL
domain.

Fix by moving devm_gpiochip_add_data() after the IRQ domain and generic
irq chip are fully initialized, ensuring port->domain and the irq chip
handlers are ready before the chip becomes visible to consumers.

Fixes: d37a65bb4663 ("gpio/mxc: Move Freescale MXC gpio driver to drivers/gpio")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Linus Walleij <linusw@kernel.org>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://patch.msgid.link/20261013-gpio-mxc-cleanup-v7-4-c4d769587d3e@nxp.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

Peng Fan <peng.fan@nxp.com> · 2026-10-13 06:01 UTC · committed by Bartosz Golaszewski

Browse files
4 files changed, +40 −21
modified drivers/gpio/gpio-mxc.c
@@ -7,6 +7,7 @@
7 7 // Authors: Daniel Mack, Juergen Beisert.
8 8 // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9 9
10 + #include <linux/bitops.h>
10 11 #include <linux/cleanup.h>
11 12 #include <linux/clk.h>
12 13 #include <linux/err.h>
@@ -68,11 +69,11 @@ struct mxc_gpio_port {
68 69 struct irq_domain *domain;
69 70 struct gpio_generic_chip gen_gc;
70 71 struct device *dev;
71 - u32 both_edges;
72 + unsigned long both_edges;
72 73 struct mxc_gpio_reg_saved gpio_saved_reg;
73 74 bool power_off;
74 - u32 wakeup_pads;
75 75 bool is_pad_wakeup;
76 + unsigned long wakeup_pads;
76 77 u32 pad_type[32];
77 78 const struct mxc_gpio_hwdata *hwdata;
78 79 };
@@ -168,7 +169,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
168 169 int edge;
169 170 void __iomem *reg = port->base;
170 171
171 - port->both_edges &= ~(1 << gpio_idx);
172 + clear_bit(gpio_idx, &port->both_edges);
172 173 switch (type) {
173 174 case IRQ_TYPE_EDGE_RISING:
174 175 edge = GPIO_INT_RISE_EDGE;
@@ -188,7 +189,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
188 189 edge = GPIO_INT_HIGH_LEV;
189 190 pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
190 191 }
191 - port->both_edges |= 1 << gpio_idx;
192 + set_bit(gpio_idx, &port->both_edges);
192 193 }
193 194 break;
194 195 case IRQ_TYPE_LEVEL_LOW:
@@ -259,7 +260,7 @@ static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
259 260 while (irq_stat != 0) {
260 261 int irqoffset = fls(irq_stat) - 1;
261 262
262 - if (port->both_edges & (1 << irqoffset))
263 + if (test_bit(irqoffset, &port->both_edges))
263 264 mxc_flip_edge(port, irqoffset);
264 265
265 266 generic_handle_domain_irq(port->domain, irqoffset);
@@ -330,16 +331,18 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
330 331 ret = enable_irq_wake(port->irq_high);
331 332 else
332 333 ret = enable_irq_wake(port->irq);
333 - port->wakeup_pads |= BIT(gpio_idx);
334 334 } else {
335 335 if (port->irq_high && (gpio_idx >= 16))
336 336 ret = disable_irq_wake(port->irq_high);
337 337 else
338 338 ret = disable_irq_wake(port->irq);
339 - port->wakeup_pads &= ~BIT(gpio_idx);
340 339 }
340 + if (ret)
341 + return ret;
341 342
342 - return ret;
343 + assign_bit(gpio_idx, &port->wakeup_pads, enable);
344 +
345 + return 0;
343 346 }
344 347
345 348 static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
@@ -474,8 +477,6 @@ static int mxc_gpio_probe(struct platform_device *pdev)
474 477 } else
475 478 port->mx_irq_handler = mx3_gpio_irq_handler;
476 479
477 - mxc_update_irq_chained_handler(port, true);
478 -
479 480 config.dev = &pdev->dev;
480 481 config.sz = 4;
481 482 config.dat = port->base + GPIO_PSR;
@@ -499,10 +500,6 @@ static int mxc_gpio_probe(struct platform_device *pdev)
499 500 else /* silence boot time warning */
500 501 port->gen_gc.gc.base = -1;
501 502
502 - err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port);
503 - if (err)
504 - goto out_bgio;
505 -
506 503 irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
507 504 if (irq_base < 0) {
508 505 err = irq_base;
@@ -523,8 +520,19 @@ static int mxc_gpio_probe(struct platform_device *pdev)
523 520 if (err < 0)
524 521 goto out_irqdomain_remove;
525 522
523 + /*
524 + * Register the GPIO chip last: devm_gpiochip_add_data() makes the
525 + * chip visible to consumers immediately, so the IRQ domain and
526 + * generic irq chip must be ready before any to_irq() call.
527 + */
528 + err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port);
529 + if (err)
530 + goto out_irqdomain_remove;
531 +
526 532 list_add_tail(&port->node, &mxc_gpio_ports);
527 533
534 + mxc_update_irq_chained_handler(port, true);
535 +
528 536 platform_set_drvdata(pdev, port);
529 537 pm_runtime_put_autosuspend(&pdev->dev);
530 538
modified drivers/gpio/gpiolib-kunit.c
@@ -28,6 +28,8 @@
28 28
29 29 #define GPIO_TEST_PROVIDER_NGPIO 4
30 30
31 + KUNIT_DEFINE_ACTION_WRAPPER(gpio_device_put_wrapper, gpio_device_put, struct gpio_device *);
32 +
31 33 /*
32 34 * The test provider tracks per-line direction and value so that lines can be
33 35 * driven as both inputs and outputs - this is needed to exercise input as well
@@ -770,6 +772,7 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
770 772 struct property_entry properties[2];
771 773 struct platform_device *pdev;
772 774 struct fwnode_handle *fwnode;
775 + struct gpio_device *gdev;
773 776 struct gpio_desc *desc;
774 777 bool bound = true;
775 778 int ret;
@@ -789,10 +792,12 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
789 792 wait_for_device_probe();
790 793
791 794 /* The hog must have configured the line with the expected direction. */
792 - struct gpio_device *gdev __free(gpio_device_put) =
793 - gpio_device_find_by_label(GPIO_CONSUMER_NAME);
795 + gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME);
794 796 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev);
795 797
798 + ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev);
799 + KUNIT_ASSERT_EQ(test, ret, 0);
800 +
796 801 desc = gpio_device_get_desc(gdev, offset);
797 802 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
798 803
modified drivers/gpio/gpiolib-shared.c
@@ -449,9 +449,14 @@ static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
449 449 int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *fwnode,
450 450 const char *con_id, unsigned long lflags)
451 451 {
452 - const char *dev_id = dev_name(consumer);
453 452 struct gpio_shared_entry *entry;
454 453 struct gpio_shared_ref *ref;
454 + const char *dev_id;
455 +
456 + if (!consumer)
457 + return -EOPNOTSUPP;
458 +
459 + dev_id = dev_name(consumer);
455 460
456 461 list_for_each_entry(entry, &gpio_shared_list, list) {
457 462 list_for_each_entry(ref, &entry->refs, list) {
modified drivers/gpio/gpiolib.c
@@ -3954,16 +3954,17 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3954 3954 } while ((i < array_size) &&
3955 3955 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3956 3956 /* push collected bits to outputs */
3957 - if (count != 0) {
3957 + ret = 0;
3958 + if (count != 0)
3958 3959 ret = gpiochip_set_multiple(guard.gc, mask, bits);
3959 - if (ret)
3960 - return ret;
3961 - }
3962 3960
3963 3961 if (mask != fastpath_mask)
3964 3962 bitmap_free(mask);
3965 3963 if (bits != fastpath_bits)
3966 3964 bitmap_free(bits);
3965 +
3966 + if (ret)
3967 + return ret;
3967 3968 }
3968 3969 return 0;
3969 3970 }