[PATCH 2/8] regulator: max8907: Duplicate of_regulator_match table per device

From: Lee Jones

Date: Fri Nov 27 2026 - 13:36:53 EST


max8907_regulator_parse_dt() passes the static global max8907_matches
array directly to of_regulator_match(), which populates init_data and
of_node in place and registers a devres callback holding a pointer to
that array. With multiple MAX8907 instances, a second probe overwrites
the first instance's entries and subsequent unbinds invoke of_node_put()
twice on the same device nodes, causing reference count underflow and
use-after-free.

Mark max8907_matches const and duplicate it per device with
devm_kmemdup() before calling of_regulator_match().

Fixes: ffee19091a4e ("regulator: add MAX8907 driver")
Closes: https://sashiko.dev/#/bug/linux-295d0600-9849-4e47-802c-7e7a883167aa
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/regulator/max8907-regulator.c | 57 +++++++++++----------------
1 file changed, 24 insertions(+), 33 deletions(-)

diff --git a/drivers/regulator/max8907-regulator.c b/drivers/regulator/max8907-regulator.c
index fd227fd13612..7781901ed115 100644
--- a/drivers/regulator/max8907-regulator.c
+++ b/drivers/regulator/max8907-regulator.c
@@ -186,7 +186,7 @@ static const struct regulator_desc max8907_regulators[] = {
.driver_data = (void *)&max8907_regulators[MAX8907_##_id], \
}

-static struct of_regulator_match max8907_matches[] = {
+static const struct of_regulator_match max8907_matches[] = {
MATCH(mbatt, MBATT),
MATCH(sd1, SD1),
MATCH(sd2, SD2),
@@ -218,54 +218,44 @@ static struct of_regulator_match max8907_matches[] = {
MATCH(vrtc, VRTC),
};

-static int max8907_regulator_parse_dt(struct platform_device *pdev)
+static struct of_regulator_match *
+max8907_regulator_parse_dt(struct platform_device *pdev)
{
+ struct of_regulator_match *matches;
struct device_node *np, *regulators;
int ret;

np = pdev->dev.parent->of_node;
if (!np)
- return 0;
+ return NULL;

regulators = of_get_child_by_name(np, "regulators");
if (!regulators) {
dev_err(&pdev->dev, "regulators node not found\n");
- return -EINVAL;
+ return ERR_PTR(-EINVAL);
}

- ret = of_regulator_match(&pdev->dev, regulators, max8907_matches,
+ matches = devm_kmemdup(&pdev->dev, max8907_matches,
+ sizeof(max8907_matches), GFP_KERNEL);
+ if (!matches) {
+ of_node_put(regulators);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ ret = of_regulator_match(&pdev->dev, regulators, matches,
ARRAY_SIZE(max8907_matches));
of_node_put(regulators);
if (ret < 0) {
dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
ret);
- return ret;
+ return ERR_PTR(ret);
}

- return 0;
-}
-
-static inline struct regulator_init_data *match_init_data(int index)
-{
- return max8907_matches[index].init_data;
-}
-
-static inline struct device_node *match_of_node(int index)
-{
- return max8907_matches[index].of_node;
+ return matches;
}
#else
-static int max8907_regulator_parse_dt(struct platform_device *pdev)
-{
- return 0;
-}
-
-static inline struct regulator_init_data *match_init_data(int index)
-{
- return NULL;
-}
-
-static inline struct device_node *match_of_node(int index)
+static struct of_regulator_match *
+max8907_regulator_parse_dt(struct platform_device *pdev)
{
return NULL;
}
@@ -280,12 +270,13 @@ static int max8907_regulator_probe(struct platform_device *pdev)
unsigned int val;
int i;
struct regulator_config config = {};
+ struct of_regulator_match *matches;
struct regulator_init_data *idata;
const char *mbatt_rail_name = NULL;

- ret = max8907_regulator_parse_dt(pdev);
- if (ret)
- return ret;
+ matches = max8907_regulator_parse_dt(pdev);
+ if (IS_ERR(matches))
+ return PTR_ERR(matches);

pmic = devm_kzalloc_obj(&pdev->dev, *pmic);
if (!pmic)
@@ -315,11 +306,11 @@ static int max8907_regulator_probe(struct platform_device *pdev)
if (pdata)
idata = pdata->init_data[i];
else
- idata = match_init_data(i);
+ idata = matches ? matches[i].init_data : NULL;
config.init_data = idata;
config.driver_data = pmic;
config.regmap = max8907->regmap_gen;
- config.of_node = match_of_node(i);
+ config.of_node = matches ? matches[i].of_node : NULL;

switch (pmic->desc[i].id) {
case MAX8907_MBATT:
--
2.56.1.839.g22a8290db9-goog