public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Axel Lin <axel.lin@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Linus Walleij <linus.walleij@linaro.org>, Greg KH <gregkh@suse.de>
Subject: [RFC][PATCH] pinmux: fix pinctrl_register error handling
Date: Fri, 30 Sep 2011 21:08:34 +0800	[thread overview]
Message-ID: <1317388114.3935.2.camel@phoenix> (raw)

If device_register fails, current code kfree pctldev multiple times.
current code calls "put_device(&pctldev->dev);kfree(pctldev);" before
and after goto out_err.

The correct fix should be:
If device_register fails, we just need to call put_device(&pctldev->dev);
Since we have "release" callback in pinctrl_type, we don't need to call
kfree(pctldev) after put_device().

If pinctrl_register_pins fails, we need to delete the successfully registerd
device from system by calling device_del().

In pinctrl_unregister(), calling device_unregister(&pctldev->dev) will
free pctldev ( by the release callback ).
We should not call kfree(pctldev) after device_unregister().

Signed-off-by: Axel Lin <axel.lin@gmail.com>
---
Hi Linus,
The fix is base on my understanding of the driver-model code,
and it is untested ( only compile test), thus I add [RFC] on subject line.
Also CC Greg KH who is the maintainer of drivers/base/*.

Regards,
Axel

 drivers/pinctrl/core.c |   14 ++++++--------
 1 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
index f9263b2..28bdb74 100644
--- a/drivers/pinctrl/core.c
+++ b/drivers/pinctrl/core.c
@@ -559,9 +559,7 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
 	ret = device_register(&pctldev->dev);
 	if (ret != 0) {
 		pr_err("error in device registration\n");
-		put_device(&pctldev->dev);
-		kfree(pctldev);
-		goto out_err;
+		goto out_reg_dev_err;
 	}
 	dev_set_drvdata(&pctldev->dev, pctldev);
 
@@ -573,7 +571,7 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
 		pr_err("error during pin registration\n");
 		pinctrl_free_pindescs(pctldev, pctldesc->pins,
 				      pctldesc->npins);
-		goto out_err;
+		goto out_reg_pins_err;
 	}
 
 	pinctrl_init_device_debugfs(pctldev);
@@ -583,9 +581,10 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
 	pinmux_hog_maps(pctldev);
 	return pctldev;
 
-out_err:
+out_reg_pins_err:
+	device_del(&pctldev->dev);
+out_reg_dev_err:
 	put_device(&pctldev->dev);
-	kfree(pctldev);
 	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(pinctrl_register);
@@ -606,11 +605,10 @@ void pinctrl_unregister(struct pinctrl_dev *pctldev)
 	mutex_lock(&pinctrldev_list_mutex);
 	list_del(&pctldev->node);
 	mutex_unlock(&pinctrldev_list_mutex);
-	device_unregister(&pctldev->dev);
 	/* Destroy descriptor tree */
 	pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
 			      pctldev->desc->npins);
-	kfree(pctldev);
+	device_unregister(&pctldev->dev);
 }
 EXPORT_SYMBOL_GPL(pinctrl_unregister);
 
-- 
1.7.4.1




             reply	other threads:[~2011-09-30 13:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-30 13:08 Axel Lin [this message]
2011-10-03  8:34 ` [RFC][PATCH] pinmux: fix pinctrl_register error handling Linus Walleij
2011-10-03  8:44   ` Axel Lin
2011-10-05 13:25     ` Linus Walleij

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=1317388114.3935.2.camel@phoenix \
    --to=axel.lin@gmail.com \
    --cc=gregkh@suse.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    /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