Commit
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
1 file changed,
+6
−4
modified drivers/gpio/gpio-mxc.c
| @@ -72,8 +72,8 @@ struct mxc_gpio_port { | |||
| 72 | 72 | unsigned long both_edges; | |
| 73 | 73 | struct mxc_gpio_reg_saved gpio_saved_reg; | |
| 74 | 74 | bool power_off; | |
| 75 | - | u32 wakeup_pads; | |
| 76 | 75 | bool is_pad_wakeup; | |
| 76 | + | unsigned long wakeup_pads; | |
| 77 | 77 | u32 pad_type[32]; | |
| 78 | 78 | const struct mxc_gpio_hwdata *hwdata; | |
| 79 | 79 | }; | |
| @@ -331,16 +331,18 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable) | |||
| 331 | 331 | ret = enable_irq_wake(port->irq_high); | |
| 332 | 332 | else | |
| 333 | 333 | ret = enable_irq_wake(port->irq); | |
| 334 | - | port->wakeup_pads |= BIT(gpio_idx); | |
| 335 | 334 | } else { | |
| 336 | 335 | if (port->irq_high && (gpio_idx >= 16)) | |
| 337 | 336 | ret = disable_irq_wake(port->irq_high); | |
| 338 | 337 | else | |
| 339 | 338 | ret = disable_irq_wake(port->irq); | |
| 340 | - | port->wakeup_pads &= ~BIT(gpio_idx); | |
| 341 | 339 | } | |
| 340 | + | if (ret) | |
| 341 | + | return ret; | |
| 342 | 342 | ||
| 343 | - | return ret; | |
| 343 | + | assign_bit(gpio_idx, &port->wakeup_pads, enable); | |
| 344 | + | ||
| 345 | + | return 0; | |
| 344 | 346 | } | |
| 345 | 347 | ||
| 346 | 348 | static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) | |