public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Linus Walleij <linus.walleij@linaro.org>,
	Kent Gibson <warthog618@gmail.com>, Alex Elder <elder@linaro.org>,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Wolfram Sang <wsa@the-dreams.de>
Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [PATCH v2 10/23] gpio: reinforce desc->flags handling
Date: Mon,  5 Feb 2024 10:34:05 +0100	[thread overview]
Message-ID: <20240205093418.39755-11-brgl@bgdev.pl> (raw)
In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl>

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

We now removed the gpio_lock spinlock and modified the places
previously protected by it to handle desc->flags access in a consistent
way. Let's improve other places that were previously unprotected by
reading the flags field of gpio_desc once and using the stored value for
logic consistency. If we need to modify the field, let's also write it
back once with a consistent value resulting from the function's logic.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/gpio/gpiolib.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5ed5b71590ab..d49f84d692ce 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -336,18 +336,20 @@ static int gpiochip_find_base_unlocked(int ngpio)
 int gpiod_get_direction(struct gpio_desc *desc)
 {
 	struct gpio_chip *gc;
+	unsigned long flags;
 	unsigned int offset;
 	int ret;
 
 	gc = gpiod_to_chip(desc);
 	offset = gpio_chip_hwgpio(desc);
+	flags = READ_ONCE(desc->flags);
 
 	/*
 	 * Open drain emulation using input mode may incorrectly report
 	 * input here, fix that up.
 	 */
-	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
-	    test_bit(FLAG_IS_OUT, &desc->flags))
+	if (test_bit(FLAG_OPEN_DRAIN, &flags) &&
+	    test_bit(FLAG_IS_OUT, &flags))
 		return 0;
 
 	if (!gc->get_direction)
@@ -361,7 +363,8 @@ int gpiod_get_direction(struct gpio_desc *desc)
 	if (ret > 0)
 		ret = 1;
 
-	assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
+	assign_bit(FLAG_IS_OUT, &flags, !ret);
+	WRITE_ONCE(desc->flags, flags);
 
 	return ret;
 }
@@ -747,9 +750,6 @@ static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
 		return;
 	}
 
-	if (test_bit(FLAG_IS_HOGGED, &desc->flags))
-		return;
-
 	rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
 	if (rv)
 		gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
@@ -2522,13 +2522,16 @@ static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
 static int gpio_set_bias(struct gpio_desc *desc)
 {
 	enum pin_config_param bias;
+	unsigned long flags;
 	unsigned int arg;
 
-	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
+	flags = READ_ONCE(desc->flags);
+
+	if (test_bit(FLAG_BIAS_DISABLE, &flags))
 		bias = PIN_CONFIG_BIAS_DISABLE;
-	else if (test_bit(FLAG_PULL_UP, &desc->flags))
+	else if (test_bit(FLAG_PULL_UP, &flags))
 		bias = PIN_CONFIG_BIAS_PULL_UP;
-	else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
+	else if (test_bit(FLAG_PULL_DOWN, &flags))
 		bias = PIN_CONFIG_BIAS_PULL_DOWN;
 	else
 		return 0;
@@ -2694,24 +2697,28 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  */
 int gpiod_direction_output(struct gpio_desc *desc, int value)
 {
+	unsigned long flags;
 	int ret;
 
 	VALIDATE_DESC(desc);
-	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
+
+	flags = READ_ONCE(desc->flags);
+
+	if (test_bit(FLAG_ACTIVE_LOW, &flags))
 		value = !value;
 	else
 		value = !!value;
 
 	/* GPIOs used for enabled IRQs shall not be set as output */
-	if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
-	    test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
+	if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
+	    test_bit(FLAG_IRQ_IS_ENABLED, &flags)) {
 		gpiod_err(desc,
 			  "%s: tried to set a GPIO tied to an IRQ as output\n",
 			  __func__);
 		return -EIO;
 	}
 
-	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
+	if (test_bit(FLAG_OPEN_DRAIN, &flags)) {
 		/* First see if we can enable open drain in hardware */
 		ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
 		if (!ret)
@@ -2721,7 +2728,7 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 			ret = gpiod_direction_input(desc);
 			goto set_output_flag;
 		}
-	} else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
+	} else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
 		ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
 		if (!ret)
 			goto set_output_value;
@@ -4418,21 +4425,22 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
 	int hwnum;
 	int ret;
 
+	if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags))
+		return 0;
+
 	gc = gpiod_to_chip(desc);
 	hwnum = gpio_chip_hwgpio(desc);
 
 	local_desc = gpiochip_request_own_desc(gc, hwnum, name,
 					       lflags, dflags);
 	if (IS_ERR(local_desc)) {
+		clear_bit(FLAG_IS_HOGGED, &desc->flags);
 		ret = PTR_ERR(local_desc);
 		pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
 		       name, gc->label, hwnum, ret);
 		return ret;
 	}
 
-	/* Mark GPIO as hogged so it can be identified and removed later */
-	set_bit(FLAG_IS_HOGGED, &desc->flags);
-
 	gpiod_dbg(desc, "hogged as %s%s\n",
 		(dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
 		(dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
-- 
2.40.1


  parent reply	other threads:[~2024-02-05  9:34 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-05  9:33 [PATCH v2 00/23] gpio: rework locking and object life-time control Bartosz Golaszewski
2024-02-05  9:33 ` [PATCH v2 01/23] gpio: protect the list of GPIO devices with SRCU Bartosz Golaszewski
2024-02-05  9:33 ` [PATCH v2 02/23] gpio: of: assign and read the hog pointer atomically Bartosz Golaszewski
2024-02-05  9:33 ` [PATCH v2 03/23] gpio: remove unused logging helpers Bartosz Golaszewski
2024-02-05  9:33 ` [PATCH v2 04/23] gpio: provide and use gpiod_get_label() Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 05/23] gpio: don't set label from irq helpers Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 06/23] gpio: add SRCU infrastructure to struct gpio_desc Bartosz Golaszewski
     [not found]   ` <ZcDRuRCT9xE48cYi@smile.fi.intel.com>
2024-02-05 13:54     ` Bartosz Golaszewski
2024-02-05 13:57       ` Andy Shevchenko
2024-02-05 14:04         ` Bartosz Golaszewski
2024-02-05 14:06           ` Andy Shevchenko
2024-02-05 14:07             ` Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 07/23] gpio: protect the descriptor label with SRCU Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 08/23] gpio: sysfs: use gpio_device_find() to iterate over existing devices Bartosz Golaszewski
2024-02-05 12:18   ` Andy Shevchenko
2024-02-05 13:19     ` Bartosz Golaszewski
2024-02-05 13:38       ` Andy Shevchenko
2024-02-05 13:39         ` Bartosz Golaszewski
2024-02-05 13:47           ` Andy Shevchenko
2024-02-05 13:50             ` Bartosz Golaszewski
2024-02-05 13:58               ` Andy Shevchenko
2024-02-05 14:04                 ` Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 09/23] gpio: remove gpio_lock Bartosz Golaszewski
2024-02-05  9:34 ` Bartosz Golaszewski [this message]
2024-02-05  9:34 ` [PATCH v2 11/23] gpio: remove unneeded code from gpio_device_get_desc() Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 12/23] gpio: sysfs: extend the critical section for unregistering sysfs devices Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 13/23] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 14/23] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 15/23] gpio: cdev: don't access gdev->chip if it's not needed Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 16/23] gpio: don't dereference gdev->chip in gpiochip_setup_dev() Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 17/23] gpio: reduce the functionality of validate_desc() Bartosz Golaszewski
     [not found]   ` <ZcDS60dB39y-B6WR@smile.fi.intel.com>
2024-02-05 19:22     ` Bartosz Golaszewski
2024-02-06 12:30       ` Andy Shevchenko
2024-02-05  9:34 ` [PATCH v2 18/23] gpio: remove unnecessary checks from gpiod_to_chip() Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 19/23] gpio: add the can_sleep flag to struct gpio_device Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 20/23] gpio: add SRCU infrastructure " Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 21/23] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Bartosz Golaszewski
2024-02-05 12:31   ` Andy Shevchenko
2024-02-05 13:30     ` Bartosz Golaszewski
2024-02-05 19:32     ` Bartosz Golaszewski
2024-02-05 19:36     ` Bartosz Golaszewski
2024-02-06 12:24       ` Andy Shevchenko
2024-02-06 12:57         ` Bartosz Golaszewski
2024-02-06 13:13           ` Andy Shevchenko
2024-02-06 13:23             ` Bartosz Golaszewski
2024-02-06 13:43               ` Andy Shevchenko
2024-02-05  9:34 ` [PATCH v2 22/23] gpio: remove the RW semaphore from the GPIO device Bartosz Golaszewski
2024-02-05  9:34 ` [PATCH v2 23/23] gpio: mark unsafe gpio_chip manipulators as deprecated Bartosz Golaszewski

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=20240205093418.39755-11-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=elder@linaro.org \
    --cc=geert+renesas@glider.be \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    --cc=warthog618@gmail.com \
    --cc=wsa@the-dreams.de \
    /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