Compare
cf73c3fc03c43a8a86fd487c62390b4744eff9ec..170aeff46fb54f41545c85a72aec525aebc79d44
gpio: mxc: fix wakeup_pads bit operations
gpio_set_wake_irq() can be called concurrently for different pins on
the same port. The writer-vs-writer race is the concern: two concurrent
gpio_set_wake_irq() calls for different pins on the same port both do
a non-atomic read-modify-write on the same wakeup_pads word. The old
|= and &= compile to separate load/modify/store instructions that can
interleave, losing one CPU's update.
assign_bit() uses an atomic RMW instruction, so concurrent writers to
different bits in the same word are safe.
The reader side (for_each_set_bit in noirq_suspend, wakeup_pads > 0)
runs in noirq context where no concurrent writer can execute, so
reader atomicity is not needed.
wakeup_pads is a u32, while assign_bit() operates on unsigned long
pointers. On 64-bit platforms, this causes an 8-byte read-modify-write
on a 4-byte field, corrupting the adjacent is_pad_wakeup. So change
wakeup_pads to unsigned long and reorder to avoid wasting words.
And fix the enable/disable path unconditionally sets/clears the
wakeup_pads bit even when enable_irq_wake()/disable_irq_wake() fails.
Only update the bit on success.
Fixes: f60c9eac54af ("gpio: mxc: enable pad wakeup on i.MX8x platforms")
Assisted-by: LLM
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://patch.msgid.link/20261013-gpio-mxc-cleanup-v7-3-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
3 files changed,
+25
−16
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; | |
| @@ -525,6 +526,8 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 525 | 526 | ||
| 526 | 527 | list_add_tail(&port->node, &mxc_gpio_ports); | |
| 527 | 528 | ||
| 529 | + | mxc_update_irq_chained_handler(port, true); | |
| 530 | + | ||
| 528 | 531 | platform_set_drvdata(pdev, port); | |
| 529 | 532 | pm_runtime_put_autosuspend(&pdev->dev); | |
| 530 | 533 | ||
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 | } | |