All of lore.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 v3 08/24] gpio: sysfs: use gpio_device_find() to iterate over existing devices
Date: Thu,  8 Feb 2024 10:59:04 +0100	[thread overview]
Message-ID: <20240208095920.8035-9-brgl@bgdev.pl> (raw)
In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl>

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

With the list of GPIO devices now protected with SRCU we can use
gpio_device_find() to traverse it from sysfs.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/gpio/gpiolib-sysfs.c | 46 ++++++++++++++++++------------------
 drivers/gpio/gpiolib.c       |  2 +-
 drivers/gpio/gpiolib.h       |  1 -
 3 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index 6bf5332136e5..d6ff044ce887 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -790,11 +790,29 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)
 	}
 }
 
+/*
+ * We're not really looking for a device - we just want to iterate over the
+ * list and call this callback for each GPIO device. This is why this function
+ * always returns 0.
+ */
+static int gpiofind_sysfs_register(struct gpio_chip *gc, void *data)
+{
+	struct gpio_device *gdev = gc->gpiodev;
+	int ret;
+
+	if (gdev->mockdev)
+		return 0;
+
+	ret = gpiochip_sysfs_register(gdev);
+	if (ret)
+		chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
+
+	return 0;
+}
+
 static int __init gpiolib_sysfs_init(void)
 {
-	int		status;
-	unsigned long	flags;
-	struct gpio_device *gdev;
+	int status;
 
 	status = class_register(&gpio_class);
 	if (status < 0)
@@ -806,26 +824,8 @@ static int __init gpiolib_sysfs_init(void)
 	 * We run before arch_initcall() so chip->dev nodes can have
 	 * registered, and so arch_initcall() can always gpiod_export().
 	 */
-	spin_lock_irqsave(&gpio_lock, flags);
-	list_for_each_entry(gdev, &gpio_devices, list) {
-		if (gdev->mockdev)
-			continue;
+	(void)gpio_device_find(NULL, gpiofind_sysfs_register);
 
-		/*
-		 * TODO we yield gpio_lock here because
-		 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
-		 * and needs to be fixed.
-		 *
-		 * Also it would be nice to use gpio_device_find() here so we
-		 * can keep gpio_chips local to gpiolib.c, but the yield of
-		 * gpio_lock prevents us from doing this.
-		 */
-		spin_unlock_irqrestore(&gpio_lock, flags);
-		status = gpiochip_sysfs_register(gdev);
-		spin_lock_irqsave(&gpio_lock, flags);
-	}
-	spin_unlock_irqrestore(&gpio_lock, flags);
-
-	return status;
+	return 0;
 }
 postcore_initcall(gpiolib_sysfs_init);
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a87ab8f013c4..68543ab34fb6 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -85,7 +85,7 @@ DEFINE_SPINLOCK(gpio_lock);
 static DEFINE_MUTEX(gpio_lookup_lock);
 static LIST_HEAD(gpio_lookup_list);
 
-LIST_HEAD(gpio_devices);
+static LIST_HEAD(gpio_devices);
 /* Protects the GPIO device list against concurrent modifications. */
 static DEFINE_MUTEX(gpio_devices_lock);
 /* Ensures coherence during read-only accesses to the list of GPIO devices. */
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index d2e73eea9e92..2bf3f9e13ae4 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -136,7 +136,6 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,
 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
 
 extern spinlock_t gpio_lock;
-extern struct list_head gpio_devices;
 
 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
 
-- 
2.40.1


  parent reply	other threads:[~2024-02-08  9:59 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20240213120518eucas1p2d514aac6e6d1e29bbae05f32db6724db@eucas1p2.samsung.com>
2024-02-08  9:58 ` [PATCH v3 00/24] gpio: rework locking and object life-time control Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 01/24] gpio: protect the list of GPIO devices with SRCU Bartosz Golaszewski
2024-02-10 11:00     ` Hillf Danton
2024-02-10 11:07       ` Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 02/24] gpio: of: assign and read the hog pointer atomically Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 03/24] gpio: remove unused logging helpers Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 04/24] gpio: provide and use gpiod_get_label() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 05/24] gpio: don't set label from irq helpers Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 06/24] gpio: add SRCU infrastructure to struct gpio_desc Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 07/24] gpio: protect the descriptor label with SRCU Bartosz Golaszewski
2024-02-12 14:56     ` kernel test robot
2024-02-13 21:16     ` Mark Brown
2024-02-13 22:07       ` Bartosz Golaszewski
2024-02-08  9:59   ` Bartosz Golaszewski [this message]
2024-02-08  9:59   ` [PATCH v3 09/24] gpio: remove gpio_lock Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 10/24] gpio: reinforce desc->flags handling Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 11/24] gpio: remove unneeded code from gpio_device_get_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 12/24] gpio: sysfs: extend the critical section for unregistering sysfs devices Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 13/24] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 14/24] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 15/24] gpio: cdev: don't access gdev->chip if it's not needed Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 16/24] gpio: sysfs: " Bartosz Golaszewski
2024-02-08 12:20     ` Linus Walleij
2024-02-08  9:59   ` [PATCH v3 17/24] gpio: don't dereference gdev->chip in gpiochip_setup_dev() Bartosz Golaszewski
2024-02-08 12:21     ` Linus Walleij
2024-02-08  9:59   ` [PATCH v3 18/24] gpio: reduce the functionality of validate_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 19/24] gpio: remove unnecessary checks from gpiod_to_chip() Bartosz Golaszewski
2024-02-08 17:39     ` Andy Shevchenko
2024-02-08 19:17       ` Bartosz Golaszewski
2024-02-08 19:24         ` Andy Shevchenko
2024-02-08 19:34           ` Bartosz Golaszewski
2024-02-09 13:59             ` Andy Shevchenko
2024-02-08  9:59   ` [PATCH v3 20/24] gpio: add the can_sleep flag to struct gpio_device Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 21/24] gpio: add SRCU infrastructure " Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 22/24] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Bartosz Golaszewski
2024-02-12 15:09     ` kernel test robot
2024-02-12 16:56       ` Bartosz Golaszewski
2024-02-12 21:20       ` Bartosz Golaszewski
2024-02-13  8:10         ` Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 23/24] gpio: remove the RW semaphore from the GPIO device Bartosz Golaszewski
2024-02-10  5:37     ` Kent Gibson
2024-02-12  9:53       ` Bartosz Golaszewski
2024-02-12  9:57         ` Kent Gibson
2024-02-12  9:59           ` Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 24/24] gpio: mark unsafe gpio_chip manipulators as deprecated Bartosz Golaszewski
2024-02-08 17:43   ` [PATCH v3 00/24] gpio: rework locking and object life-time control Andy Shevchenko
2024-02-12 10:07   ` Bartosz Golaszewski
2024-02-13 12:05   ` Marek Szyprowski
2024-02-13 12:08     ` 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=20240208095920.8035-9-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.