All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] gpiolib: rewrite gpiodev_add_to_list
@ 2016-02-26 14:37 Bamvor Jian Zhang
  2016-03-07  5:04 ` Linus Walleij
  0 siblings, 1 reply; 2+ messages in thread
From: Bamvor Jian Zhang @ 2016-02-26 14:37 UTC (permalink / raw)
  To: linux-gpio
  Cc: linus.walleij, broonie, julien.grossholtz, arnd, Bamvor Jian Zhang

From: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>

The original code of gpiodev_add_to_list is not very clear which
lead to bugs or compiling warning, reference the following patches:
Bugs:
1.  Commit ef7c7553039b ("gpiolib: improve overlap check of range of
    gpio").
2.  Commit 96098df125c0 ("gpiolib: fix chip order in gpio list")

Warning:
1.  Commit e28ecca6eac4 ("gpio: fix warning about iterator").
of gpio").

There is a off-list discussion about how to improve it consequently.
This commit try to follow this by rewriting the whole functions.

Tested pass with my gpio mockup driver and test scripts[1].

[1] http://www.spinics.net/lists/linux-gpio/msg09598.html

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
---
 drivers/gpio/gpiolib.c | 65 +++++++++++++++++++-------------------------------
 1 file changed, 25 insertions(+), 40 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index bc788b9..1c10bd8 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -206,58 +206,43 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiodev_add_to_list(struct gpio_device *gdev)
 {
-	struct gpio_device *iterator;
-	struct gpio_device *previous = NULL;
-
-	if (!gdev->chip)
-		return -EINVAL;
+	struct gpio_device *prev, *next;
 
 	if (list_empty(&gpio_devices)) {
+		/* initial entry in list */
 		list_add_tail(&gdev->list, &gpio_devices);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_devices, list) {
-		if (iterator->base >= gdev->base + gdev->ngpio) {
-			/*
-			 * Iterator is the first GPIO chip so there is no
-			 * previous one
-			 */
-			if (!previous) {
-				goto found;
-			} else {
-				/*
-				 * We found a valid range(means
-				 * [base, base + ngpio - 1]) between previous
-				 * and iterator chip.
-				 */
-				if (previous->base + previous->ngpio
-				    <= gdev->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_devices.next, struct gpio_device, list);
+	if (gdev->base + gdev->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&gdev->list, &gpio_devices);
+		return 0;
 	}
 
-	/*
-	 * We are beyond the last chip in the list and iterator now
-	 * points to the head.
-	 * Let iterator point to the last chip in the list.
-	 */
-
-	iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
-	if (iterator->base + iterator->ngpio <= gdev->base) {
-		list_add(&gdev->list, &iterator->list);
+	prev = list_entry(gpio_devices.prev, struct gpio_device, list);
+	if (prev->base + prev->ngpio <= gdev->base) {
+		/* add behind last entry */
+		list_add_tail(&gdev->list, &gpio_devices);
 		return 0;
 	}
 
-	dev_err(&gdev->dev,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+	list_for_each_entry_safe(prev, next, &gpio_devices, list) {
+		/* at the end of the list */
+		if (&next->list == &gpio_devices)
+			break;
 
-found:
-	list_add_tail(&gdev->list, &iterator->list);
-	return 0;
+		/* add between prev and next */
+		if (prev->base + prev->ngpio <= gdev->base
+				&& gdev->base + gdev->ngpio <= next->base) {
+			list_add(&gdev->list, &prev->list);
+			return 0;
+		}
+	}
+
+	dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
 /**
-- 
2.6.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] gpiolib: rewrite gpiodev_add_to_list
  2016-02-26 14:37 [PATCH v2] gpiolib: rewrite gpiodev_add_to_list Bamvor Jian Zhang
@ 2016-03-07  5:04 ` Linus Walleij
  0 siblings, 0 replies; 2+ messages in thread
From: Linus Walleij @ 2016-03-07  5:04 UTC (permalink / raw)
  To: Bamvor Jian Zhang
  Cc: linux-gpio, Mark Brown, Julien Grossholtz, Arnd Bergmann,
	Bamvor Jian Zhang

On Fri, Feb 26, 2016 at 9:37 PM, Bamvor Jian Zhang <bamv2005@gmail.com> wrote:

> From: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
>
> The original code of gpiodev_add_to_list is not very clear which
> lead to bugs or compiling warning, reference the following patches:
> Bugs:
> 1.  Commit ef7c7553039b ("gpiolib: improve overlap check of range of
>     gpio").
> 2.  Commit 96098df125c0 ("gpiolib: fix chip order in gpio list")
>
> Warning:
> 1.  Commit e28ecca6eac4 ("gpio: fix warning about iterator").
> of gpio").
>
> There is a off-list discussion about how to improve it consequently.
> This commit try to follow this by rewriting the whole functions.
>
> Tested pass with my gpio mockup driver and test scripts[1].
>
> [1] http://www.spinics.net/lists/linux-gpio/msg09598.html
>
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>

Patch applied. It surely looks much better like this!

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-03-07  5:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-26 14:37 [PATCH v2] gpiolib: rewrite gpiodev_add_to_list Bamvor Jian Zhang
2016-03-07  5:04 ` Linus Walleij

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.