mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Gyungoh Yoo <jack.yoo@maxim-ic.com>,
	Graeme Gregory <gg@slimlogic.co.uk>,
	Eduardo Candelaria <jedu@slimlogic.co.uk>,
	Stephen Warren <swarren@nvidia.com>,
	Laxman Dewangan <ldewangan@nvidia.com>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 2/8] regulator: max8907: Duplicate of_regulator_match table per device
Date: Fri, 27 Nov 2026 18:35:42 +0000	[thread overview]
Message-ID: <20261127183624.3341010-3-lee@kernel.org> (raw)
In-Reply-To: <20261127183624.3341010-1-lee@kernel.org>

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@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 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


  parent reply	other threads:[~2026-11-27 18:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 18:35 [PATCH 0/8] regulator: Fix locking, OOB read, lifetime, and global state bugs (batch 2) Lee Jones
2026-11-27 18:35 ` [PATCH 1/8] regulator: core: Defer supply cleanup and unregister event forwarding on error Lee Jones
2026-11-27 18:35 ` Lee Jones [this message]
2026-11-27 18:35 ` [PATCH 3/8] regulator: tps6594-regulator: Use dedicated handler for external regulator IRQs Lee Jones
2026-11-27 18:35 ` [PATCH 4/8] regulator: tps65910: Fix OOB read and bit shift in external sleep config Lee Jones
2026-11-27 18:35 ` [PATCH 5/8] regulator: tps6594-regulator: Bind IRQ resources to child platform device Lee Jones
2026-11-27 18:35 ` [PATCH 6/8] regulator: core: Respect is_locked when disabling supply on enable failure Lee Jones
2026-11-27 18:35 ` [PATCH 7/8] regulator: of: Guard against NULL device_node in of_regulator_bulk_get_all() Lee Jones
2026-11-27 18:35 ` [PATCH 8/8] regulator: aat2870: Duplicate regulator data per device Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127183624.3341010-3-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=broonie@kernel.org \
    --cc=gg@slimlogic.co.uk \
    --cc=jack.yoo@maxim-ic.com \
    --cc=jedu@slimlogic.co.uk \
    --cc=ldewangan@nvidia.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=swarren@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®