Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Charles Keepax <ckeepax@opensource.cirrus.com>
To: <linus.walleij@linaro.org>, <brgl@bgdev.pl>
Cc: <linux-gpio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<patches@opensource.cirrus.com>
Subject: [PATCH] gpio: swnode: Don't use __free() on result of swnode_get_gpio_device()
Date: Mon, 4 Mar 2024 16:03:20 +0000	[thread overview]
Message-ID: <20240304160320.1054811-1-ckeepax@opensource.cirrus.com> (raw)

swnode_get_gpio_device() can return an error pointer, however
gpio_device_put() is not able to accept error values. Thus using
__free() will result in dereferencing an invalid pointer.

As there is only a single exit point anyway, simply call
gpio_device_put() manually. Whilst modifying the code move
the variable declaration to the top of the function, and move
fwnode_handle_put() until after the error check. Technically
fwnode_handle_put() will handle being passed an error value, but
no need to call it when the code knows it doesn't need to.

Fixes: b7b56e64a345 ("gpio: swnode: replace gpiochip_find() with gpio_device_find_by_label()")
Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
---
 drivers/gpio/gpiolib-swnode.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c
index fa52bdb1a29a3..de43e0b06a4b1 100644
--- a/drivers/gpio/gpiolib-swnode.c
+++ b/drivers/gpio/gpiolib-swnode.c
@@ -50,6 +50,7 @@ struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
 {
 	const struct software_node *swnode;
 	struct fwnode_reference_args args;
+	struct gpio_device *gdev;
 	struct gpio_desc *desc;
 	char propname[32]; /* 32 is max size of property name */
 	int error;
@@ -71,12 +72,12 @@ struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
 		return ERR_PTR(error);
 	}
 
-	struct gpio_device *gdev __free(gpio_device_put) =
-					swnode_get_gpio_device(args.fwnode);
-	fwnode_handle_put(args.fwnode);
+	gdev = swnode_get_gpio_device(args.fwnode);
 	if (IS_ERR(gdev))
 		return ERR_CAST(gdev);
 
+	fwnode_handle_put(args.fwnode);
+
 	/*
 	 * FIXME: The GPIO device reference is put at return but the descriptor
 	 * is passed on. Find a proper solution.
@@ -87,6 +88,8 @@ struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
 	pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
 		 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
 
+	gpio_device_put(gdev);
+
 	return desc;
 }
 
-- 
2.39.2


             reply	other threads:[~2024-03-04 16:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-04 16:03 Charles Keepax [this message]
2024-03-04 16:34 ` [PATCH] gpio: swnode: Don't use __free() on result of swnode_get_gpio_device() Bartosz Golaszewski
2024-03-04 17:08   ` Charles Keepax
2024-03-05 14:14 ` Dan Carpenter

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=20240304160320.1054811-1-ckeepax@opensource.cirrus.com \
    --to=ckeepax@opensource.cirrus.com \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patches@opensource.cirrus.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