Linux real-time development
 help / color / mirror / Atom feed
* [PATCH 0/2] gpio: mvebu: resume and PREEMPT_RT irq mask fixes
@ 2026-12-03 22:58 Rosen Penev
  2026-12-03 22:58 ` [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock Rosen Penev
  2026-12-03 22:58 ` [PATCH 2/2] gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too Rosen Penev
  0 siblings, 2 replies; 4+ messages in thread
From: Rosen Penev @ 2026-12-03 22:58 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Sebastian Andrzej Siewior,
	Clark Williams, Steven Rostedt, Rosen Penev, Gregory CLEMENT
  Cc: Bartosz Golaszewski, linux-gpio, linux-rt-devel

Two fixes for the irq mask registers.

Patch 1 closes a race in resume. The mask registers were rebuilt from a
snapshot of the irqchip mask cache taken under gc->lock, but written
after the lock was dropped. A mask or unmask in between was lost, and
could leave a level line unmasked behind genirq's back.

Patch 2 extends commit 42269cdb3d3b ("gpio: mvebu: fix sleeping-while-
atomic in mvebu_gpio_level_irq_mask()") to Armada 7K/8K. There the
driver used the parent's syscon regmap, which has an ordinary spinlock,
so the mask/unmask callbacks still slept under the raw gc->lock on
PREEMPT_RT. The driver now maps the parent's range and uses its own
raw-locked regmap.

The series is based on gpio/for-current, which has commit d8e27d042747
("gpio: mvebu: fix Armada XP per-CPU mask register save/restore") that
patch 1 builds on.

Each patch builds with W=1 for arm64 (defconfig + PREEMPT_RT +
PROVE_LOCKING). Neither has been tested on hardware yet.

Rosen Penev (2):
  gpio: mvebu: restore suspend-time irq masks under gc->lock
  gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too

 drivers/gpio/gpio-mvebu.c | 114 +++++++++++++++++++++++---------------
 1 file changed, 70 insertions(+), 44 deletions(-)


base-commit: f4379936003e4a233ed8d7b8ab299630bae85fec
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock
  2026-12-03 22:58 [PATCH 0/2] gpio: mvebu: resume and PREEMPT_RT irq mask fixes Rosen Penev
@ 2026-12-03 22:58 ` Rosen Penev
  2026-12-03 23:14   ` sashiko-bot
  2026-12-03 22:58 ` [PATCH 2/2] gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too Rosen Penev
  1 sibling, 1 reply; 4+ messages in thread
From: Rosen Penev @ 2026-12-03 22:58 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Sebastian Andrzej Siewior,
	Clark Williams, Steven Rostedt, Rosen Penev, Gregory CLEMENT
  Cc: Bartosz Golaszewski, linux-gpio, linux-rt-devel

mvebu_gpio_resume() snapshots the irqchip's mask_cache_priv under
gc->lock, drops the lock, and only then writes the saved edge/level
masks ANDed with that snapshot back to hardware.  A mask or unmask
handler running in that window updates the cache and the register, and
the restore then overwrites the register with the stale value.  A line
masked in the window ends up unmasked behind genirq's back, so an
asserted level line has nobody to ack it and the chained handler
storms; a line unmasked in the window stays masked in hardware and
loses its interrupts.

Hold gc->lock for the whole restore, as the mask/unmask handlers
already do for their cache update and register write, so the two
cannot interleave.  Move the per-variant register writes into
mvebu_gpio_restore_irq_masks() to keep the locked section readable,
and replace the BUG() on an unknown SoC variant with WARN_ON_ONCE().

Controllers without an irq domain no longer have their mask registers
written on resume.  Nothing unmasks lines on such a controller, so the
registers it saved at suspend time are not ones the driver set.

Fixes: 02af7eac17bc ("gpio: mvebu: keep resume masks within the irqchip cache")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/gpio/gpio-mvebu.c | 88 ++++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 42 deletions(-)

diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
index 6c2bcc5d749c..c202b946f595 100644
--- a/drivers/gpio/gpio-mvebu.c
+++ b/drivers/gpio/gpio-mvebu.c
@@ -996,6 +996,45 @@ static const struct of_device_id mvebu_gpio_of_match[] = {
 	},
 };
 
+static void
+mvebu_gpio_restore_irq_masks(struct mvebu_gpio_chip *mvchip, u32 level_cache,
+			     u32 edge_cache)
+{
+	int i;
+
+	switch (mvchip->soc_variant) {
+	case MVEBU_GPIO_SOC_VARIANT_ORION:
+	case MVEBU_GPIO_SOC_VARIANT_A8K:
+		regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
+			     mvchip->edge_mask_regs[0] & edge_cache);
+		regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
+			     mvchip->level_mask_regs[0] & level_cache);
+		break;
+	case MVEBU_GPIO_SOC_VARIANT_MV78200:
+		for (i = 0; i < 2; i++) {
+			regmap_write(mvchip->regs,
+				     GPIO_EDGE_MASK_MV78200_OFF(i),
+				     mvchip->edge_mask_regs[i] & edge_cache);
+			regmap_write(mvchip->regs,
+				     GPIO_LEVEL_MASK_MV78200_OFF(i),
+				     mvchip->level_mask_regs[i] & level_cache);
+		}
+		break;
+	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
+		for (i = 0; i < 4; i++) {
+			regmap_write(mvchip->percpu_regs,
+				     GPIO_EDGE_MASK_ARMADAXP_OFF(i),
+				     mvchip->edge_mask_regs[i] & edge_cache);
+			regmap_write(mvchip->percpu_regs,
+				     GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
+				     mvchip->level_mask_regs[i] & level_cache);
+		}
+		break;
+	default:
+		WARN_ON_ONCE(1);
+	}
+}
+
 static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
 {
 	struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
@@ -1051,9 +1090,6 @@ static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
 static int mvebu_gpio_resume(struct platform_device *pdev)
 {
 	struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
-	u32 edge_cache = ~0U, level_cache = ~0U;
-	unsigned long flags;
-	int i;
 
 	regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
 		     mvchip->out_reg);
@@ -1069,50 +1105,18 @@ static int mvebu_gpio_resume(struct platform_device *pdev)
 	 * unmasking one behind its back leaves an asserted level line that
 	 * nobody masks. Restore only bits the irqchip cache still has set.
 	 *
-	 * Snapshot the caches under the raw spinlock, but release it before
-	 * the regmap writes below: regmap_write() takes a sleepable lock on
-	 * PREEMPT_RT.
+	 * Hold gc->lock across the writes so the restore cannot race the
+	 * mask/unmask handlers, which update the cache and write the mask
+	 * registers under the same lock.
 	 */
 	if (mvchip->domain) {
 		struct irq_chip_generic *gc;
 
 		gc = irq_get_domain_generic_chip(mvchip->domain, 0);
-		raw_spin_lock_irqsave(&gc->lock, flags);
-		level_cache = gc->chip_types[0].mask_cache_priv;
-		edge_cache = gc->chip_types[1].mask_cache_priv;
-		raw_spin_unlock_irqrestore(&gc->lock, flags);
-	}
-
-	switch (mvchip->soc_variant) {
-	case MVEBU_GPIO_SOC_VARIANT_ORION:
-	case MVEBU_GPIO_SOC_VARIANT_A8K:
-		regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
-			     mvchip->edge_mask_regs[0] & edge_cache);
-		regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
-			     mvchip->level_mask_regs[0] & level_cache);
-		break;
-	case MVEBU_GPIO_SOC_VARIANT_MV78200:
-		for (i = 0; i < 2; i++) {
-			regmap_write(mvchip->regs,
-				     GPIO_EDGE_MASK_MV78200_OFF(i),
-				     mvchip->edge_mask_regs[i] & edge_cache);
-			regmap_write(mvchip->regs,
-				     GPIO_LEVEL_MASK_MV78200_OFF(i),
-				     mvchip->level_mask_regs[i] & level_cache);
-		}
-		break;
-	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
-		for (i = 0; i < 4; i++) {
-			regmap_write(mvchip->percpu_regs,
-				     GPIO_EDGE_MASK_ARMADAXP_OFF(i),
-				     mvchip->edge_mask_regs[i] & edge_cache);
-			regmap_write(mvchip->percpu_regs,
-				     GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
-				     mvchip->level_mask_regs[i] & level_cache);
-		}
-		break;
-	default:
-		BUG();
+		guard(raw_spinlock_irqsave)(&gc->lock);
+		mvebu_gpio_restore_irq_masks(mvchip,
+					     gc->chip_types[0].mask_cache_priv,
+					     gc->chip_types[1].mask_cache_priv);
 	}
 
 	if (IS_REACHABLE(CONFIG_PWM) && mvchip->mvpwm)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too
  2026-12-03 22:58 [PATCH 0/2] gpio: mvebu: resume and PREEMPT_RT irq mask fixes Rosen Penev
  2026-12-03 22:58 ` [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock Rosen Penev
@ 2026-12-03 22:58 ` Rosen Penev
  1 sibling, 0 replies; 4+ messages in thread
From: Rosen Penev @ 2026-12-03 22:58 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Sebastian Andrzej Siewior,
	Clark Williams, Steven Rostedt, Rosen Penev, Gregory CLEMENT
  Cc: Bartosz Golaszewski, linux-gpio, linux-rt-devel

Commit 42269cdb3d3b ("gpio: mvebu: fix sleeping-while-atomic in
mvebu_gpio_level_irq_mask()") made the driver's MMIO regmaps use raw
spinlocks, because the irq_chip mask/unmask callbacks write the mask
registers while holding the generic chip's raw gc->lock.  That only
covers the regmaps the driver creates itself.  On Armada 7K/8K
("marvell,armada-8k-gpio") the GPIO block sits inside a system
controller and the driver uses the parent's syscon regmap, which syscon
creates with an ordinary spinlock.  On PREEMPT_RT that lock sleeps, so
masking or unmasking a GPIO interrupt there still sleeps in atomic
context.

The syscon regmap's lock cannot be changed from here, and nothing else
accesses the GPIO bank's registers at its offset, so the shared lock is
not protecting anything this driver needs.  Map the parent's register
range again and create a regmap of our own with the raw-locked
mvebu_gpio_regmap_config, as the non-syscon path already does.  The
mapping is not requested, as syscon does not request it either.

Fixes: b6730b20837e ("gpio: mvebu: Add support for the Armada 7K/8K SoCs")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/gpio/gpio-mvebu.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
index c202b946f595..a2974515b408 100644
--- a/drivers/gpio/gpio-mvebu.c
+++ b/drivers/gpio/gpio-mvebu.c
@@ -42,8 +42,8 @@
 #include <linux/irq.h>
 #include <linux/irqchip/chained_irq.h>
 #include <linux/irqdomain.h>
-#include <linux/mfd/syscon.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
@@ -1167,7 +1167,29 @@ static int mvebu_gpio_probe_raw(struct platform_device *pdev,
 static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
 				   struct mvebu_gpio_chip *mvchip)
 {
-	mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
+	struct device_node *np = pdev->dev.parent->of_node;
+	void __iomem *base;
+	struct resource res;
+	int ret;
+
+	/*
+	 * The GPIO registers live inside the parent system controller, but
+	 * don't use its syscon regmap: that one is created with a normal
+	 * spinlock, which sleeps on PREEMPT_RT, and the irq_chip callbacks
+	 * write the mask registers under the raw gc->lock. This driver is
+	 * the only user of the registers at its offset, so map the parent's
+	 * range again and access it through a raw-locked regmap of our own.
+	 */
+	ret = of_address_to_resource(np, 0, &res);
+	if (ret)
+		return ret;
+
+	base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
+	if (!base)
+		return -ENOMEM;
+
+	mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
+					     &mvebu_gpio_regmap_config);
 	if (IS_ERR(mvchip->regs))
 		return PTR_ERR(mvchip->regs);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock
  2026-12-03 22:58 ` [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock Rosen Penev
@ 2026-12-03 23:14   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 23:14 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-gpio, linux-rt-devel, linux-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] gpio: mvebu: mvebu_gpio_resume() nests spinlock_t under raw
  gc->lock
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] gpio: mvebu: dropped unmasked IRQs in mvebu_gpio_resume() [1]
--

commit 2b335edf3e9a01fe8cd24e622c3bca3a7c7c7760
Author: Rosen Penev <rosenp@gmail.com>

gpio: mvebu: restore suspend-time irq masks under gc->lock

This commit moves the suspend-time IRQ mask register restoration in
mvebu_gpio_resume() into a helper called under gc->lock so it cannot race
with concurrent mask or unmask handlers.

> diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c
> index 6c2bcc5d749cc..c202b946f595e 100644
> --- a/drivers/gpio/gpio-mvebu.c
> +++ b/drivers/gpio/gpio-mvebu.c
> @@ -996,6 +996,45 @@ static const struct of_device_id mvebu_gpio_of_match[] = {
>  	},
>  };
>  
> +static void
> +mvebu_gpio_restore_irq_masks(struct mvebu_gpio_chip *mvchip, u32 level_cache,
> +			     u32 edge_cache)
> +{
> +	int i;
> +
> +	switch (mvchip->soc_variant) {
> +	case MVEBU_GPIO_SOC_VARIANT_ORION:
> +	case MVEBU_GPIO_SOC_VARIANT_A8K:
> +		regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
> +			     mvchip->edge_mask_regs[0] & edge_cache);
> +		regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
> +			     mvchip->level_mask_regs[0] & level_cache);
> +		break;

[Severity: High]
This problem wasn't introduced by this patch, but can bitwise-ANDing with
the suspend-time snapshot in [1] silently re-mask an IRQ that was
enabled during early resume?

If a driver disables its IRQ before suspend, the saved mask register
records 0.  If it then calls enable_irq() during resume_noirq, the line is
unmasked and set to 1 in the cache, but mvebu_gpio_resume() later writes
0 & 1 to hardware and silently drops subsequent interrupts.

[ ... ]

> @@ -1069,50 +1105,18 @@ static int mvebu_gpio_resume(struct platform_device *pdev)
>  	 * unmasking one behind its back leaves an asserted level line that
>  	 * nobody masks. Restore only bits the irqchip cache still has set.
>  	 *
> -	 * Snapshot the caches under the raw spinlock, but release it before
> -	 * the regmap writes below: regmap_write() takes a sleepable lock on
> -	 * PREEMPT_RT.
> +	 * Hold gc->lock across the writes so the restore cannot race the
> +	 * mask/unmask handlers, which update the cache and write the mask
> +	 * registers under the same lock.
>  	 */
>  	if (mvchip->domain) {
>  		struct irq_chip_generic *gc;
>  
>  		gc = irq_get_domain_generic_chip(mvchip->domain, 0);
[ ... ]
> +		guard(raw_spinlock_irqsave)(&gc->lock);
> +		mvebu_gpio_restore_irq_masks(mvchip,
> +					     gc->chip_types[0].mask_cache_priv,
> +					     gc->chip_types[1].mask_cache_priv);
>  	}

[Severity: Medium]
Can calling regmap_write() under gc->lock sleep on CONFIG_PREEMPT_RT or
trigger a lockdep invalid wait context splat on Armada 7K/8K?

At this commit, mvebu_gpio_probe_syscon() still uses the parent syscon
regmap, whose syscon_regmap_config does not set use_raw_spinlock.
mvebu_gpio_resume() -> mvebu_gpio_restore_irq_masks() -> regmap_write()
therefore acquires a regular spinlock_t inside raw_spinlock_t gc->lock.

Because commit "gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too"
later in the series switches Armada 7K/8K to a raw-locked MMIO regmap,
ordering that commit before this one would avoid the intermediate
bisection regression.

[1] https://sashiko.dev/#/bug/linux-21cc7884-41c0-4b66-ad75-b626faddba53

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203225820.83277-1-rosenp@gmail.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 23:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 22:58 [PATCH 0/2] gpio: mvebu: resume and PREEMPT_RT irq mask fixes Rosen Penev
2026-12-03 22:58 ` [PATCH 1/2] gpio: mvebu: restore suspend-time irq masks under gc->lock Rosen Penev
2026-12-03 23:14   ` sashiko-bot
2026-12-03 22:58 ` [PATCH 2/2] gpio: mvebu: use a raw-locked regmap on Armada 7K/8K too Rosen Penev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox