LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Markus Pargmann <mpa@pengutronix.de>
To: Ingo Flaschberger <ingo.flaschberger@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	Markus Pargmann <mpa@pengutronix.de>, <stable@vger.kernel.org>
Subject: [PATCH] w1: gpio: Fix problematic platform_data usage
Date: Mon,  9 Mar 2015 11:18:58 +0100	[thread overview]
Message-ID: <1425896338-31928-1-git-send-email-mpa@pengutronix.de> (raw)
In-Reply-To: <54F8445B.10805@gmail.com>

pdev->dev.platform_data should not be overwritten by a driver. This
patch fixes this issue by not using platform_get_pdata to get pdata. The
DT routine can then simply return a pdata struct with those information.

Without this patch the platform_data may be freed twice, by devres and
the driver framework.

Reported-by: Ingo Flaschberger <ingo.flaschberger@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
---

Hi,

Could you please try this patch? This essentially does not store anything to
platform_data anymore so it should hopefully fix your issue.

Best Regards,

Markus

 drivers/w1/masters/w1-gpio.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/drivers/w1/masters/w1-gpio.c b/drivers/w1/masters/w1-gpio.c
index b99a932ad901..63e7b1d2785f 100644
--- a/drivers/w1/masters/w1-gpio.c
+++ b/drivers/w1/masters/w1-gpio.c
@@ -75,15 +75,18 @@ static struct of_device_id w1_gpio_dt_ids[] = {
 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
 #endif
 
-static int w1_gpio_probe_dt(struct platform_device *pdev)
+static struct w1_gpio_platform_data *w1_gpio_probe_dt(struct platform_device *pdev)
 {
 	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
 	struct device_node *np = pdev->dev.of_node;
 	int gpio;
 
+	if (pdata)
+		return pdata;
+
 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 	if (!pdata)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	if (of_get_property(np, "linux,open-drain", NULL))
 		pdata->is_open_drain = 1;
@@ -95,19 +98,17 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
 					"Failed to parse gpio property for data pin (%d)\n",
 					gpio);
 
-		return gpio;
+		return ERR_PTR(gpio);
 	}
 	pdata->pin = gpio;
 
 	gpio = of_get_gpio(np, 1);
 	if (gpio == -EPROBE_DEFER)
-		return gpio;
+		return ERR_PTR(gpio);
 	/* ignore other errors as the pullup gpio is optional */
 	pdata->ext_pullup_enable_pin = gpio;
 
-	pdev->dev.platform_data = pdata;
-
-	return 0;
+	return pdata;
 }
 
 static int w1_gpio_probe(struct platform_device *pdev)
@@ -116,17 +117,16 @@ static int w1_gpio_probe(struct platform_device *pdev)
 	struct w1_gpio_platform_data *pdata;
 	int err;
 
-	if (of_have_populated_dt()) {
-		err = w1_gpio_probe_dt(pdev);
-		if (err < 0)
-			return err;
-	}
-
-	pdata = dev_get_platdata(&pdev->dev);
+	if (of_have_populated_dt())
+		pdata = w1_gpio_probe_dt(pdev);
+	else
+		pdata = dev_get_platdata(&pdev->dev);
 
-	if (!pdata) {
-		dev_err(&pdev->dev, "No configuration data\n");
-		return -ENXIO;
+	if (IS_ERR_OR_NULL(pdata)) {
+		dev_err(&pdev->dev, "No configuration data or configuration failed\n");
+		if (!pdata)
+			return -ENXIO;
+		return PTR_ERR(pdata);
 	}
 
 	master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
@@ -185,7 +185,7 @@ static int w1_gpio_probe(struct platform_device *pdev)
 static int w1_gpio_remove(struct platform_device *pdev)
 {
 	struct w1_bus_master *master = platform_get_drvdata(pdev);
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(0);
@@ -202,7 +202,8 @@ static int w1_gpio_remove(struct platform_device *pdev)
 
 static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
 {
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct w1_bus_master *master = platform_get_drvdata(pdev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(0);
@@ -212,7 +213,8 @@ static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
 
 static int w1_gpio_resume(struct platform_device *pdev)
 {
-	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct w1_bus_master *master = platform_get_drvdata(pdev);
+	struct w1_gpio_platform_data *pdata = master->data;
 
 	if (pdata->enable_external_pullup)
 		pdata->enable_external_pullup(1);
-- 
2.1.4


  reply	other threads:[~2015-03-09 10:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-04  3:53 unbind/bind w1-gpio with device tree produce a crash Ingo Flaschberger
2015-03-04 16:38 ` Ingo Flaschberger
     [not found]   ` <54F7C5D4.3000604@gmail.com>
2015-03-05  7:35     ` Fwd: " Markus Pargmann
2015-03-05 11:56       ` Ingo Flaschberger
2015-03-09 10:18         ` Markus Pargmann [this message]
2015-04-10 10:22           ` [PATCH] w1: gpio: Fix problematic platform_data usage Markus Pargmann
2016-03-22 11:59             ` Ingo Flaschberger

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=1425896338-31928-1-git-send-email-mpa@pengutronix.de \
    --to=mpa@pengutronix.de \
    --cc=ingo.flaschberger@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --subject='Re: [PATCH] w1: gpio: Fix problematic platform_data usage' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).