Compare
fcbc48dc650d2f58bfe19c4486830de0e9cad62e..b7719c575dec69146f6d537fd616b27083c0cf45
gpio: mxc: fix both_edges bit operations
gpio_set_irq_type() can be called concurrently for different pins on
the same port. The IRQ core serializes per irq descriptor, but
different pins have different descriptors, so on SMP systems two
concurrent gpio_set_irq_type() calls for pins on the same port run
without mutual exclusion.
The read-modify-write of port->both_edges is performed outside the
port-wide scoped_guard(gpio_generic_lock_irqsave) section, so
concurrent updates for different pins can overwrite each other,
causing missed interrupts or stuck IRQ lines on dual-edge triggered
pins.
The original uniprocessor MXC SoCs were not affected; the race
became reachable on multi-core i.MX7 and i.MX8 SoCs that reuse this
driver.
Convert both_edges from u32 to unsigned long and use set_bit(),
clear_bit(), and test_bit() to make the modifications atomic.
Fixes: 910862ec092c ("mxc: emulate GPIO interrupt on both-edges")
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Linus Walleij <linusw@kernel.org>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://patch.msgid.link/20261013-gpio-mxc-cleanup-v7-2-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
5 files changed,
+39
−17
- drivers/gpio/gpio-mxc.c
- drivers/gpio/gpiolib-kunit.c
- drivers/gpio/gpiolib-shared.c
- drivers/gpio/gpiolib.c
- tools/gpio/Makefile
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,7 +69,7 @@ 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 | 75 | u32 wakeup_pads; | |
| @@ -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); | |
| @@ -474,8 +475,6 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 474 | 475 | } else | |
| 475 | 476 | port->mx_irq_handler = mx3_gpio_irq_handler; | |
| 476 | 477 | ||
| 477 | - | mxc_update_irq_chained_handler(port, true); | |
| 478 | - | ||
| 479 | 478 | config.dev = &pdev->dev; | |
| 480 | 479 | config.sz = 4; | |
| 481 | 480 | config.dat = port->base + GPIO_PSR; | |
| @@ -525,6 +524,8 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 525 | 524 | ||
| 526 | 525 | list_add_tail(&port->node, &mxc_gpio_ports); | |
| 527 | 526 | ||
| 527 | + | mxc_update_irq_chained_handler(port, true); | |
| 528 | + | ||
| 528 | 529 | platform_set_drvdata(pdev, port); | |
| 529 | 530 | pm_runtime_put_autosuspend(&pdev->dev); | |
| 530 | 531 | ||
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.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 | } | |
modified tools/gpio/Makefile
| @@ -7,7 +7,13 @@ bindir ?= /usr/bin | |||
| 7 | 7 | # isn't set and when invoked from selftests build, where srctree | |
| 8 | 8 | # is set to ".". building_out_of_srctree is undefined for in srctree | |
| 9 | 9 | # builds | |
| 10 | + | ifeq ($(srctree),) | |
| 11 | + | update_srctree := 1 | |
| 12 | + | endif | |
| 10 | 13 | ifndef building_out_of_srctree | |
| 14 | + | update_srctree := 1 | |
| 15 | + | endif | |
| 16 | + | ifeq ($(update_srctree),1) | |
| 11 | 17 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) | |
| 12 | 18 | srctree := $(patsubst %/,%,$(dir $(srctree))) | |
| 13 | 19 | endif | |