[PATCH v2 2/2] pinctrl: starfive: jh7100: Reject malformed pin properties

From: Zhiqi Zhang

Date: Fri Dec 04 2026 - 07:47:03 EST


The JH7100 binding allows each pin group to specify either "pinmux"
or "pins", but not both. starfive_dt_node_to_map() used
of_property_count_u32_elems() to decide which property is present, so a
negative parser error was handled the same way as an absent property.

A group that contains a malformed "pinmux" property and a valid "pins"
property is therefore accepted as a "pins" group. Similarly, a malformed
"pins" property can be ignored when a valid "pinmux" property is present.

Use a helper to check property presence separately from array length.
Reject groups that specify both or neither property, and require the
selected u32 array to contain at least one element before sizing and
filling the maps.

I found this issue. OpenAI Codex helped draft the fix, and I reviewed
the result.

Fixes: ec648f6b7686 ("pinctrl: starfive: Add pinctrl driver for StarFive SoCs")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
.../starfive/pinctrl-starfive-jh7100.c | 61 +++++++++++++------
1 file changed, 43 insertions(+), 18 deletions(-)

diff --git a/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c b/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
index d89f6a4722..eea9e8cad9 100644
--- a/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
+++ b/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
@@ -472,6 +472,34 @@ static void starfive_pin_dbg_show(struct pinctrl_dev *pctldev,
#define starfive_pin_dbg_show NULL
#endif

+static int starfive_dt_count_pins(struct device *dev, struct device_node *np,
+ struct device_node *child, bool *is_pinmux)
+{
+ bool has_pinmux = of_property_present(child, "pinmux");
+ bool has_pins = of_property_present(child, "pins");
+ const char *propname;
+ int npins;
+
+ if (has_pinmux && has_pins) {
+ dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: both pinmux and pins set\n",
+ np, child);
+ return -EINVAL;
+ }
+ if (!has_pinmux && !has_pins) {
+ dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: neither pinmux nor pins set\n",
+ np, child);
+ return -EINVAL;
+ }
+
+ *is_pinmux = has_pinmux;
+ propname = has_pinmux ? "pinmux" : "pins";
+ npins = of_property_count_u32_elems(child, propname);
+ if (npins < 1)
+ return npins ?: -EINVAL;
+
+ return npins;
+}
+
static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
struct device_node *np,
struct pinctrl_map **maps,
@@ -491,21 +519,14 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
nmaps = 0;
ngroups = 0;
for_each_available_child_of_node_scoped(np, child) {
- int npinmux = of_property_count_u32_elems(child, "pinmux");
- int npins = of_property_count_u32_elems(child, "pins");
+ bool is_pinmux;
+ int npins;

- if (npinmux > 0 && npins > 0) {
- dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: both pinmux and pins set\n",
- np, child);
- return -EINVAL;
- }
- if (npinmux == 0 && npins == 0) {
- dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: neither pinmux nor pins set\n",
- np, child);
- return -EINVAL;
- }
+ npins = starfive_dt_count_pins(dev, np, child, &is_pinmux);
+ if (npins < 0)
+ return npins;

- if (npinmux > 0)
+ if (is_pinmux)
nmaps += 2;
else
nmaps += 1;
@@ -524,9 +545,16 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
ngroups = 0;
mutex_lock(&sfp->mutex);
for_each_available_child_of_node_scoped(np, child) {
+ bool is_pinmux;
int npins;
int i;

+ npins = starfive_dt_count_pins(dev, np, child, &is_pinmux);
+ if (npins < 0) {
+ ret = npins;
+ goto free_map;
+ }
+
grpname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", np, child);
if (!grpname) {
ret = -ENOMEM;
@@ -535,7 +563,7 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,

pgnames[ngroups++] = grpname;

- if ((npins = of_property_count_u32_elems(child, "pinmux")) > 0) {
+ if (is_pinmux) {
pins = devm_kzalloc_objs(dev, *pins, npins);
if (!pins) {
ret = -ENOMEM;
@@ -562,7 +590,7 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
map[nmaps].data.mux.function = np->name;
map[nmaps].data.mux.group = grpname;
nmaps += 1;
- } else if ((npins = of_property_count_u32_elems(child, "pins")) > 0) {
+ } else {
pins = devm_kzalloc_objs(dev, *pins, npins);
if (!pins) {
ret = -ENOMEM;
@@ -579,9 +607,6 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
goto free_map;
pins[i] = v;
}
- } else {
- ret = -EINVAL;
- goto free_map;
}

ret = pinctrl_generic_add_group(pctldev, grpname, pins, npins, pinmux);
--
2.54.0 (Apple Git-157)