[PATCH 10/12] pinctrl: sunxi: Serialise per-bank regulator request and free

From: Lee Jones

Date: Wed Dec 02 2026 - 04:33:04 EST


The pinmux core invokes pmxops->request() and pmxops->free() without
holding a controller-wide lock. When two pins in the same 32-pin bank
are requested or freed concurrently, sunxi_pmx_request() and
sunxi_pmx_free() race on s_reg->regulator and s_reg->refcount, which can
result in duplicate regulator_get()/regulator_enable() calls or
addition-on-zero refcount warnings.

Serialise per-bank regulator acquisition and release in
sunxi_pmx_request() and sunxi_pmx_free() with a mutex.

Fixes: 9a2a566adb00 ("pinctrl: sunxi: Deal with per-bank regulators")
Closes: https://sashiko.dev/#/bug/linux-8e59d9d1-607f-4eba-a596-fa524b707d0e
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/pinctrl/sunxi/pinctrl-sunxi.c | 17 ++++++++++-------
drivers/pinctrl/sunxi/pinctrl-sunxi.h | 3 +++
2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
index bf4ec4665e2b..d20415b885a9 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
@@ -10,6 +10,7 @@
* warranty of any kind, whether express or implied.
*/

+#include <linux/cleanup.h>
#include <linux/clk.h>
#include <linux/export.h>
#include <linux/gpio/driver.h>
@@ -913,13 +914,16 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
unsigned short bank_offset = bank - pctl->desc->pin_base /
PINS_PER_BANK;
struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
- struct regulator *reg = s_reg->regulator;
+ struct regulator *reg;
char supply[16];
int ret;

if (WARN_ON_ONCE(bank_offset >= ARRAY_SIZE(pctl->regulators)))
return -EINVAL;

+ guard(mutex)(&pctl->reg_lock);
+
+ reg = s_reg->regulator;
if (reg) {
refcount_inc(&s_reg->refcount);
return 0;
@@ -936,7 +940,8 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
if (ret) {
dev_err(pctl->dev,
"Couldn't enable bank P%c regulator\n", 'A' + bank);
- goto out;
+ regulator_put(reg);
+ return ret;
}

sunxi_pinctrl_set_io_bias_cfg(pctl, offset, reg);
@@ -945,11 +950,6 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
refcount_set(&s_reg->refcount, 1);

return 0;
-
-out:
- regulator_put(reg);
-
- return ret;
}

static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
@@ -960,6 +960,8 @@ static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
PINS_PER_BANK;
struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];

+ guard(mutex)(&pctl->reg_lock);
+
if (!refcount_dec_and_test(&s_reg->refcount))
return 0;

@@ -1609,6 +1611,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
platform_set_drvdata(pdev, pctl);

raw_spin_lock_init(&pctl->lock);
+ mutex_init(&pctl->reg_lock);

pctl->membase = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(pctl->membase))
diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.h b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
index 7c2f29e75519..1d8547e9492e 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi.h
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
@@ -14,6 +14,7 @@
#define __PINCTRL_SUNXI_H

#include <linux/kernel.h>
+#include <linux/mutex.h>
#include <linux/spinlock.h>

#define PA_BASE 0
@@ -187,6 +188,8 @@ struct sunxi_pinctrl {
int *irq;
unsigned *irq_array;
raw_spinlock_t lock;
+ /* Protects @regulators */
+ struct mutex reg_lock;
/*
* Output latch shadow, one word per bank. Seeded lockless at
* probe before the pinctrl device registers, protected by @lock
--
2.56.1.911.g883d8bcb20-goog