Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/3] pinctrl: single: follow-up fixes from review
@ 2026-11-19 23:28 A. Sverdlin
  2026-11-19 23:28 ` [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function A. Sverdlin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: A. Sverdlin @ 2026-11-19 23:28 UTC (permalink / raw)
  To: linux-gpio
  Cc: Alexander Sverdlin, Tony Lindgren, Haojian Zhuang, Linus Walleij,
	linux-arm-kernel, linux-omap, linux-kernel, Lee Jones

From: Alexander Sverdlin <alexander.sverdlin@siemens.com>

These are follow-ups to commit ad2093840ca3 ("pinctrl: single: don't leak
group/function tables on consumer re-probe") in pinctrl/for-next, which
this series is based on. All three issues were found in code review
(partly while reviewing Lee's recent pinctrl-single series [1]). None of
them was reproduced, so the series is compile-tested only (W=1, ARM).

1/3 is a gap in ad2093840ca3 itself: the duplicate function is freed, but
its pinconf table isn't. My re-probe testing of ad2093840ca3 (TPM
bind/unbind loop on an AM625 board, watching the slab counters) could not
have shown it. That controller is plain "pinctrl-single" without
PCS_FEAT_PINCONF, so pcs_parse_pinconf() never runs there. kmemleak
doesn't see it either, because the table stays reachable through devres.

2/3 fixes error paths that remove a group/function the core deduplicated,
i.e. one still used by an earlier mapping. Reaching them needs an
allocation failure during a consumer re-probe.

3/3 rejects bit-per-mux without a usable function mask. Such DTs pass
dt-validate but divide by zero at runtime. Checked with dt-validate and
against all in-tree bit-per-mux users, but not booted.

[1] https://lore.kernel.org/all/20261119205013.2563811-10-lee@kernel.org/

Alexander Sverdlin (3):
  pinctrl: single: Also free the pinconf table of a duplicate function
  pinctrl: single: Don't remove deduplicated groups and functions on
    error
  pinctrl: single: Reject bit-per-mux without a usable function mask

 drivers/pinctrl/pinctrl-single.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

-- 
2.56.1


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

* [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function
  2026-11-19 23:28 [PATCH 0/3] pinctrl: single: follow-up fixes from review A. Sverdlin
@ 2026-11-19 23:28 ` A. Sverdlin
  2026-11-19 23:43   ` sashiko-bot
  2026-11-19 23:28 ` [PATCH 2/3] pinctrl: single: Don't remove deduplicated groups and functions on error A. Sverdlin
  2026-11-19 23:28 ` [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask A. Sverdlin
  2 siblings, 1 reply; 6+ messages in thread
From: A. Sverdlin @ 2026-11-19 23:28 UTC (permalink / raw)
  To: linux-gpio
  Cc: Alexander Sverdlin, Tony Lindgren, Haojian Zhuang, Linus Walleij,
	linux-arm-kernel, linux-omap, linux-kernel, Lee Jones, stable

From: Alexander Sverdlin <alexander.sverdlin@siemens.com>

pcs_parse_pinconf() attaches a devm-allocated pcs_conf_vals array to the
function pcs_add_function() has just allocated. When a node is mapped
again, e.g. on consumer re-probe, that function is a duplicate of the one
the core keeps and is freed since commit ad2093840ca3 ("pinctrl: single:
don't leak group/function tables on consumer re-probe"), but its conf
array stays on the controller's devres list. It accumulates with every
re-probe of a consumer whose pin group has pinconf properties, until the
controller is unbound.

Free it together with the function.

Fixes: 9dddb4df90d1 ("pinctrl: single: support generic pinconf")
Cc: stable@vger.kernel.org
Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
---
 drivers/pinctrl/pinctrl-single.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index d72ebb186b894..64472465800c4 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -1107,6 +1107,7 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	if (pinmux_generic_get_function(pcs->pctl, fsel)->data != function) {
 		devm_kfree(pcs->dev, pins);
 		devm_kfree(pcs->dev, vals);
+		devm_kfree(pcs->dev, function->conf);
 		devm_kfree(pcs->dev, function);
 		devm_kfree(pcs->dev, pgnames);
 	}
-- 
2.56.1


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

* [PATCH 2/3] pinctrl: single: Don't remove deduplicated groups and functions on error
  2026-11-19 23:28 [PATCH 0/3] pinctrl: single: follow-up fixes from review A. Sverdlin
  2026-11-19 23:28 ` [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function A. Sverdlin
@ 2026-11-19 23:28 ` A. Sverdlin
  2026-11-19 23:28 ` [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask A. Sverdlin
  2 siblings, 0 replies; 6+ messages in thread
From: A. Sverdlin @ 2026-11-19 23:28 UTC (permalink / raw)
  To: linux-gpio
  Cc: Alexander Sverdlin, Tony Lindgren, Haojian Zhuang, Linus Walleij,
	linux-arm-kernel, linux-omap, linux-kernel, Lee Jones

From: Alexander Sverdlin <alexander.sverdlin@siemens.com>

pinctrl_generic_add_group() and pinmux_generic_add_function() return the
selector of an existing entry with the same name instead of adding a new
one. This is the normal case when a node is mapped again, e.g. on
consumer re-probe. If parsing then fails, the error paths of
pcs_parse_one_pinctrl_entry() and pcs_parse_bits_in_pinctrl_entry()
remove the group and function by that selector anyway, i.e. the entries
an earlier mapping is still using.

Besides breaking that mapping, this corrupts the selector space: the
selectors are allocated from num_groups/num_functions, which the removal
decrements, so the last entry is no longer found by name and the next new
entry collides with it in the radix tree (-EEXIST).

Only remove them if this call actually added them, i.e. if the core's
function refers to our pcs_function. As in the success path, testing the
function is enough: the group has the same name and is always added and
removed together with it.

Fixes: a4ab10860723 ("pinctrl: single: Fix group and function selector use")
Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
---
 drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index 64472465800c4..f5f0b95dc4282 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -1116,10 +1116,16 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	return 0;
 
 free_pingroups:
-	pinctrl_generic_remove_group(pcs->pctl, gsel);
+	/*
+	 * Only remove what we added, not an in-use deduplicated instance. The
+	 * group is deduplicated together with the function.
+	 */
+	if (pinmux_generic_get_function(pcs->pctl, fsel)->data == function)
+		pinctrl_generic_remove_group(pcs->pctl, gsel);
 	*num_maps = 1;
 free_function:
-	pinmux_generic_remove_function(pcs->pctl, fsel);
+	if (pinmux_generic_get_function(pcs->pctl, fsel)->data == function)
+		pinmux_generic_remove_function(pcs->pctl, fsel);
 free_pins:
 	mutex_unlock(&pcs->mutex);
 	devm_kfree(pcs->dev, pins);
@@ -1262,7 +1268,9 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 	return 0;
 
 free_function:
-	pinmux_generic_remove_function(pcs->pctl, fsel);
+	/* See pcs_parse_one_pinctrl_entry() */
+	if (pinmux_generic_get_function(pcs->pctl, fsel)->data == function)
+		pinmux_generic_remove_function(pcs->pctl, fsel);
 free_pins:
 	mutex_unlock(&pcs->mutex);
 	devm_kfree(pcs->dev, pins);
-- 
2.56.1


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

* [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask
  2026-11-19 23:28 [PATCH 0/3] pinctrl: single: follow-up fixes from review A. Sverdlin
  2026-11-19 23:28 ` [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function A. Sverdlin
  2026-11-19 23:28 ` [PATCH 2/3] pinctrl: single: Don't remove deduplicated groups and functions on error A. Sverdlin
@ 2026-11-19 23:28 ` A. Sverdlin
  2026-11-19 23:42   ` sashiko-bot
  2 siblings, 1 reply; 6+ messages in thread
From: A. Sverdlin @ 2026-11-19 23:28 UTC (permalink / raw)
  To: linux-gpio
  Cc: Alexander Sverdlin, Tony Lindgren, Haojian Zhuang, Linus Walleij,
	linux-arm-kernel, linux-omap, linux-kernel, Lee Jones

From: Alexander Sverdlin <alexander.sverdlin@siemens.com>

With "pinctrl-single,bit-per-mux", the number of bits per pin is derived
from "pinctrl-single,function-mask" as fls(fmask). Neither the binding nor
pcs_probe() requires the mask to be present with bit-per-mux, or to fit
the register width:

- If the mask is missing or zero, bits_per_pin stays 0. Probe succeeds,
  but parsing the first pin group divides by zero in
  pcs_parse_bits_in_pinctrl_entry() and pcs_get_pin_by_offset(), and so
  does reading the debugfs "pins" file via pcs_pin_shift_reg_get().

- If the mask is wider than the register, width / bits_per_pin is 0.
  pcs_parse_bits_in_pinctrl_entry() then allocates zero-sized vals and
  pins arrays and writes through the returned ZERO_SIZE_PTR, and
  pcs_pin_shift_reg_get() takes a remainder modulo zero.

Both configurations pass dt-validate against the current binding.
Reject them in pcs_probe().

The patch was inspired by a sibling fix
("pinctrl: single: Reject invalid register width in pcs_probe()").

Link: https://lore.kernel.org/all/20261119205013.2563811-10-lee@kernel.org/
Fixes: 4e7e8017a80e ("pinctrl: pinctrl-single: enhance to configure multiple pins of different modules")
Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
---
 drivers/pinctrl/pinctrl-single.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index f5f0b95dc4282..ba5782b6b63d0 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -1860,6 +1860,14 @@ static int pcs_probe(struct platform_device *pdev)
 
 	pcs->bits_per_mux = of_property_read_bool(np,
 						  "pinctrl-single,bit-per-mux");
+	if (pcs->bits_per_mux &&
+	    (!pcs->fmask || fls(pcs->fmask) > pcs->width)) {
+		dev_err(pcs->dev, "invalid function mask 0x%x for bit-per-mux\n",
+			pcs->fmask);
+
+		return -EINVAL;
+	}
+
 	ret = pcs_quirk_missing_pinctrl_cells(pcs, np,
 					      pcs->bits_per_mux ? 2 : 1);
 	if (ret) {
-- 
2.56.1


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

* Re: [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask
  2026-11-19 23:28 ` [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask A. Sverdlin
@ 2026-11-19 23:42   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-19 23:42 UTC (permalink / raw)
  To: A. Sverdlin, linux-gpio

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] pinctrl: single: __ffs(0) undefined behavior in pcs_probe()
  on zero fmask
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] pinctrl: single: division by zero in pcs_allocate_pin_table()
  [1]
--

commit cc08958bb33456e53a3135b8e53047a689914a44
Author: Alexander Sverdlin <alexander.sverdlin@siemens.com>

pinctrl: single: Reject bit-per-mux without a usable function mask

This commit rejects pinctrl-single,bit-per-mux configurations in
pcs_probe() when pinctrl-single,function-mask is missing, zero, or wider
than the register width.

Link: https://lore.kernel.org/all/20261119205013.2563811-10-lee@kernel.org/

> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index bc6a3a4999156..73425f6c97284 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -1855,6 +1855,14 @@ static int pcs_probe(struct platform_device *pdev)
>  
>  	pcs->bits_per_mux = of_property_read_bool(np,
>  						  "pinctrl-single,bit-per-mux");
> +	if (pcs->bits_per_mux &&
> +	    (!pcs->fmask || fls(pcs->fmask) > pcs->width)) {
> +		dev_err(pcs->dev, "invalid function mask 0x%x for bit-per-mux\n",
> +			pcs->fmask);
> +
> +		return -EINVAL;
> +	}

[Severity: Medium]
Can pcs_probe() invoke __ffs(0) before reaching this !pcs->fmask check?

When pinctrl-single,function-mask is present in the Device Tree with a
value of 0, of_property_read_u32() succeeds earlier in pcs_probe() and
runs:

drivers/pinctrl/pinctrl-single.c:pcs_probe() {
	...
	ret = of_property_read_u32(np, "pinctrl-single,function-mask",
				   &pcs->fmask);
	if (!ret) {
		pcs->fshift = __ffs(pcs->fmask);
		pcs->fmax = pcs->fmask >> pcs->fshift;
	} else {
	...
}

__ffs(0) is undefined (__builtin_ctzl(0)), and shifting the 32-bit
pcs->fmask by pcs->fshift can also trigger an out-of-bounds shift before
 reaching the new check.  Checking if (!ret && pcs->fmask) above would
avoid both.

[Severity: High]
This problem wasn't introduced by this patch, but does pcs_probe() also
fail to reject an invalid pcs->width [1]?

Later in pcs_probe(), the switch on pcs->width ignores anything other
than 8, 16, or 32 in its default case:

drivers/pinctrl/pinctrl-single.c:pcs_probe() {
	...
	switch (pcs->width) {
	case 8:
		pcs->read = pcs_readb;
		pcs->write = pcs_writeb;
		break;
	case 16:
		pcs->read = pcs_readw;
		pcs->write = pcs_writew;
		break;
	case 32:
		pcs->read = pcs_readl;
		pcs->write = pcs_writel;
		break;
	default:
		break;
	}
	...
	ret = pcs_allocate_pin_table(pcs);
	...
}

When pcs->width is less than 8 and bit-per-mux is not set,
pcs_allocate_pin_table() computes mux_bytes = pcs->width / BITS_PER_BYTE
as 0 and divides by zero in pcs->size / mux_bytes.  If pcs->width is
greater than 8 but not 8, 16, or 32, pcs->read and pcs->write remain NULL
and pcs_add_pin() dereferences pcs->read when irq_enable_mask is set.

[1] https://sashiko.dev/#/bug/linux-a0303b6e-d0c6-4292-aeeb-fa8a21861827

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119232806.4093754-1-alexander.sverdlin@siemens.com?part=3

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

* Re: [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function
  2026-11-19 23:28 ` [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function A. Sverdlin
@ 2026-11-19 23:43   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-19 23:43 UTC (permalink / raw)
  To: A. Sverdlin, linux-gpio

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] pinctrl: single: use-after-free in
  pcs_parse_one_pinctrl_entry() [1]
--

commit f9a80b7d4b1cc004ae636f5e91a037b0d6afbc93
Author: Alexander Sverdlin <alexander.sverdlin@siemens.com>

pinctrl: single: Also free the pinconf table of a duplicate function

This commit frees function->conf when pcs_parse_one_pinctrl_entry() drops
a duplicate function on consumer re-probe.

> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index 52734bd7b7f06..95c8bd38459c8 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -1102,6 +1102,7 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
>  	if (pinmux_generic_get_function(pcs->pctl, fsel)->data != function) {
>  		devm_kfree(pcs->dev, pins);
>  		devm_kfree(pcs->dev, vals);
> +		devm_kfree(pcs->dev, function->conf);
>  		devm_kfree(pcs->dev, function);
>  		devm_kfree(pcs->dev, pgnames);
>  	}

[Severity: Critical]
This problem wasn't introduced by this patch, but can the error path in
pcs_parse_one_pinctrl_entry() (and pcs_parse_bits_in_pinctrl_entry())
remove an already registered group and function [1] and cause a
use-after-free?

When a duplicate node is parsed, pcs_add_function() and
pinctrl_generic_add_group() return the existing selectors fsel and gsel.
If pcs_parse_pinconf() then fails, the error path unconditionally removes
both selectors from the core radix trees:

drivers/pinctrl/pinctrl-single.c:pcs_parse_one_pinctrl_entry() {
	...
	if (PCS_HAS_PINCONF && function) {
		res = pcs_parse_pinconf(pcs, np, function, map);
		if (res == 0)
			*num_maps = 2;
		else if (res == -ENOTSUPP)
			*num_maps = 1;
		else
			goto free_pingroups;
	}
	...
free_pingroups:
	pinctrl_generic_remove_group(pcs->pctl, gsel);
	*num_maps = 1;
free_function:
	pinmux_generic_remove_function(pcs->pctl, fsel);
	...
}

This deletes the original group and function descriptors while active
consumers still reference them, and leaks the duplicate function,
pgnames, and function->conf allocations.

[1] https://sashiko.dev/#/bug/linux-146c577c-dec4-4dc4-b28e-826c1cc76a9a

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119232806.4093754-1-alexander.sverdlin@siemens.com?part=1

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

end of thread, other threads:[~2026-11-19 23:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 23:28 [PATCH 0/3] pinctrl: single: follow-up fixes from review A. Sverdlin
2026-11-19 23:28 ` [PATCH 1/3] pinctrl: single: Also free the pinconf table of a duplicate function A. Sverdlin
2026-11-19 23:43   ` sashiko-bot
2026-11-19 23:28 ` [PATCH 2/3] pinctrl: single: Don't remove deduplicated groups and functions on error A. Sverdlin
2026-11-19 23:28 ` [PATCH 3/3] pinctrl: single: Reject bit-per-mux without a usable function mask A. Sverdlin
2026-11-19 23:42   ` sashiko-bot

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