All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpiolib: rewrite gpiochip_add_to_list
@ 2016-01-08  6:37 Bamvor Jian Zhang
  2016-01-08  6:37 ` Bamvor Jian Zhang
  0 siblings, 1 reply; 7+ messages in thread
From: Bamvor Jian Zhang @ 2016-01-08  6:37 UTC (permalink / raw)
  To: linux-gpio
  Cc: linus.walleij, broonie, julien.grossholtz, arnd, Bamvor Jian Zhang

The original code of gpiochip_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.  "[PATCH] gpiolib: fix chip order in gpio list" [1]

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[2].

[1] http://www.spinics.net/lists/kernel/msg2156917.html
[2] 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, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..b9cc01f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list)
+		if (chip->base > prev->base)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+	if (&prev->list != &gpio_chips &&
+	    &next->list != &gpio_chips &&
+	    prev->base + prev->ngpio <= chip->base &&
+	    chip->base + chip->ngpio <= next->base) {
+		/* add between prev and next */
+		list_add(&chip->list, &prev->list);
+		return 0;
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */
-- 
2.1.4


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

* [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-08  6:37 [PATCH] gpiolib: rewrite gpiochip_add_to_list Bamvor Jian Zhang
@ 2016-01-08  6:37 ` Bamvor Jian Zhang
  2016-01-08 21:47   ` Julien Grossholtz
  0 siblings, 1 reply; 7+ messages in thread
From: Bamvor Jian Zhang @ 2016-01-08  6:37 UTC (permalink / raw)
  To: linux-gpio
  Cc: linus.walleij, broonie, julien.grossholtz, arnd, Bamvor Jian Zhang

The original code of gpiochip_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.  "[PATCH] gpiolib: fix chip order in gpio list" [1]

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[2].

[1] http://www.spinics.net/lists/kernel/msg2156917.html
[2] 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, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..b9cc01f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list)
+		if (chip->base > prev->base)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+	if (&prev->list != &gpio_chips &&
+	    &next->list != &gpio_chips &&
+	    prev->base + prev->ngpio <= chip->base &&
+	    chip->base + chip->ngpio <= next->base) {
+		/* add between prev and next */
+		list_add(&chip->list, &prev->list);
+		return 0;
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */
-- 
2.1.4


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

* Re: [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-08  6:37 ` Bamvor Jian Zhang
@ 2016-01-08 21:47   ` Julien Grossholtz
  2016-01-08 22:56     ` [Kernel] " Julien Grossholtz
  0 siblings, 1 reply; 7+ messages in thread
From: Julien Grossholtz @ 2016-01-08 21:47 UTC (permalink / raw)
  To: Bamvor Jian Zhang; +Cc: linux-gpio, linus walleij, broonie, arnd, kernel

Hello,

I tested this patch. It is not working in the following situation:
A first driver request manually base number from 0 to 31, 32 to 63 etc... for some chips (gpio-mxc.c in my case).
After this, an other driver set base to -1. It works for the first chip, but fails for all others.

It looks like there is still an issue in the gpio chips list order. I will put some debug and see if I can give you feedback.

Julien

----- Original Message -----
From: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
To: linux-gpio@vger.kernel.org
Cc: "linus walleij" <linus.walleij@linaro.org>, broonie@kernel.org, "julien grossholtz" <julien.grossholtz@savoirfairelinux.com>, arnd@arndb.de, "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
Sent: Friday, January 8, 2016 1:37:15 AM
Subject: [PATCH] gpiolib: rewrite gpiochip_add_to_list

The original code of gpiochip_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.  "[PATCH] gpiolib: fix chip order in gpio list" [1]

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[2].

[1] http://www.spinics.net/lists/kernel/msg2156917.html
[2] 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, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..b9cc01f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list)
+		if (chip->base > prev->base)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+	if (&prev->list != &gpio_chips &&
+	    &next->list != &gpio_chips &&
+	    prev->base + prev->ngpio <= chip->base &&
+	    chip->base + chip->ngpio <= next->base) {
+		/* add between prev and next */
+		list_add(&chip->list, &prev->list);
+		return 0;
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */
-- 
2.1.4

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

* Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-08 21:47   ` Julien Grossholtz
@ 2016-01-08 22:56     ` Julien Grossholtz
  2016-01-09 15:16       ` Bamvor Zhang Jian
  0 siblings, 1 reply; 7+ messages in thread
From: Julien Grossholtz @ 2016-01-08 22:56 UTC (permalink / raw)
  To: Bamvor Jian Zhang; +Cc: linux-gpio, linus walleij, broonie, kernel, arnd

Hi,

The issue is in the break condition of the list_for_each_entry_safe loop.
If there is a chip at the beginning it breaks even if there is another
one after. You should check the next chip:
if (chip->base > prev->base && chip->base + chip->ngpio <= next->base)

You can also call list_add directly into this for_each.

Julien 

----- Original Message -----
From: "Julien Grossholtz" <julien.grossholtz@savoirfairelinux.com>
To: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
Cc: linux-gpio@vger.kernel.org, "linus walleij" <linus.walleij@linaro.org>, "broonie" <broonie@kernel.org>, "kernel" <kernel@savoirfairelinux.com>, arnd@arndb.de
Sent: Friday, January 8, 2016 4:47:03 PM
Subject: Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list

Hello,

I tested this patch. It is not working in the following situation:
A first driver request manually base number from 0 to 31, 32 to 63 etc... for some chips (gpio-mxc.c in my case).
After this, an other driver set base to -1. It works for the first chip, but fails for all others.

It looks like there is still an issue in the gpio chips list order. I will put some debug and see if I can give you feedback.

Julien

----- Original Message -----
From: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
To: linux-gpio@vger.kernel.org
Cc: "linus walleij" <linus.walleij@linaro.org>, broonie@kernel.org, "julien grossholtz" <julien.grossholtz@savoirfairelinux.com>, arnd@arndb.de, "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
Sent: Friday, January 8, 2016 1:37:15 AM
Subject: [PATCH] gpiolib: rewrite gpiochip_add_to_list

The original code of gpiochip_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.  "[PATCH] gpiolib: fix chip order in gpio list" [1]

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[2].

[1] http://www.spinics.net/lists/kernel/msg2156917.html
[2] 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, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..b9cc01f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list)
+		if (chip->base > prev->base)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+	if (&prev->list != &gpio_chips &&
+	    &next->list != &gpio_chips &&
+	    prev->base + prev->ngpio <= chip->base &&
+	    chip->base + chip->ngpio <= next->base) {
+		/* add between prev and next */
+		list_add(&chip->list, &prev->list);
+		return 0;
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */
-- 
2.1.4
_______________________________________________
kernel mailing list
kernel@lists.savoirfairelinux.net
https://lists.savoirfairelinux.net/mailman/listinfo/kernel

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

* Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-08 22:56     ` [Kernel] " Julien Grossholtz
@ 2016-01-09 15:16       ` Bamvor Zhang Jian
  2016-01-11 14:35         ` Bamvor Jian Zhang
  0 siblings, 1 reply; 7+ messages in thread
From: Bamvor Zhang Jian @ 2016-01-09 15:16 UTC (permalink / raw)
  To: Julien Grossholtz
  Cc: linux-gpio, linus walleij, broonie, kernel, Arnd Bergmann,
	Bamvor Zhang Jian

Hi, Julien

On 01/09/2016 06:56 AM, Julien Grossholtz wrote:
> Hi,
>
> The issue is in the break condition of the list_for_each_entry_safe loop.
> If there is a chip at the beginning it breaks even if there is another
> one after. You should check the next chip:
> if (chip->base > prev->base && chip->base + chip->ngpio <= next->base)
Thanks you suggestion. It indeed fix the issue you mentioned.
I will send the new version after I make sure there is no other corner case.

Regards

Bamvor
>
> You can also call list_add directly into this for_each.
>
> Julien
>
> ----- Original Message -----
> From: "Julien Grossholtz" <julien.grossholtz@savoirfairelinux.com>
> To: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
> Cc: linux-gpio@vger.kernel.org, "linus walleij" <linus.walleij@linaro.org>, "broonie" <broonie@kernel.org>, "kernel" <kernel@savoirfairelinux.com>, arnd@arndb.de
> Sent: Friday, January 8, 2016 4:47:03 PM
> Subject: Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list
>
> Hello,
>
> I tested this patch. It is not working in the following situation:
> A first driver request manually base number from 0 to 31, 32 to 63 etc... for some chips (gpio-mxc.c in my case).
> After this, an other driver set base to -1. It works for the first chip, but fails for all others.
>
> It looks like there is still an issue in the gpio chips list order. I will put some debug and see if I can give you feedback.
>
> Julien
>
> ----- Original Message -----
> From: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
> To: linux-gpio@vger.kernel.org
> Cc: "linus walleij" <linus.walleij@linaro.org>, broonie@kernel.org, "julien grossholtz" <julien.grossholtz@savoirfairelinux.com>, arnd@arndb.de, "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
> Sent: Friday, January 8, 2016 1:37:15 AM
> Subject: [PATCH] gpiolib: rewrite gpiochip_add_to_list
>
> The original code of gpiochip_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.  "[PATCH] gpiolib: fix chip order in gpio list" [1]
>
> 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[2].
>
> [1] http://www.spinics.net/lists/kernel/msg2156917.html
> [2] 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, 28 insertions(+), 37 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 3db34e7..b9cc01f 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
>   */
>  static int gpiochip_add_to_list(struct gpio_chip *chip)
>  {
> - struct gpio_chip *iterator;
> - struct gpio_chip *previous = NULL;
> + struct gpio_chip *prev, *next;
>
>   if (list_empty(&gpio_chips)) {
> - list_add_tail(&chip->list, &gpio_chips);
> + /* initial entry in list */
> + list_add(&chip->list, &gpio_chips);
>   return 0;
>   }
>
> - list_for_each_entry(iterator, &gpio_chips, list) {
> - if (iterator->base >= chip->base + chip->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
> - <= chip->base)
> - goto found;
> - }
> - }
> - previous = iterator;
> + next = list_entry(gpio_chips.next, struct gpio_chip, list);
> + if (chip->base + chip->ngpio <= next->base) {
> + /* add before first entry */
> + list_add(&chip->list, &gpio_chips);
> + 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.
> - */
> + prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
> + if (prev->base + prev->ngpio <= chip->base) {
> + /* add behind last entry */
> + list_add_tail(&chip->list, &gpio_chips);
> + return 0;
> + }
>
> - iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
> - if (iterator->base + iterator->ngpio <= chip->base)
> - goto found;
> + list_for_each_entry_safe(prev, next, &gpio_chips, list)
> + if (chip->base > prev->base)
> + break;
>
> - dev_err(chip->parent,
> -       "GPIO integer space overlap, cannot add chip\n");
> - return -EBUSY;
> + if (&prev->list != &gpio_chips &&
> +    &next->list != &gpio_chips &&
> +    prev->base + prev->ngpio <= chip->base &&
> +    chip->base + chip->ngpio <= next->base) {
> + /* add between prev and next */
> + list_add(&chip->list, &prev->list);
> + return 0;
> + }
>
> -found:
> - list_add_tail(&chip->list, &iterator->list);
> - return 0;
> + dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
> + return -EBUSY;
>  }
>
> +
>  /**
>   * Convert a GPIO name to its descriptor
>   */
>

-- 
-----------------------------
   blog: http://aarch64.me
-----------------------------

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

* Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-09 15:16       ` Bamvor Zhang Jian
@ 2016-01-11 14:35         ` Bamvor Jian Zhang
  2016-01-11 19:56           ` Julien Grossholtz
  0 siblings, 1 reply; 7+ messages in thread
From: Bamvor Jian Zhang @ 2016-01-11 14:35 UTC (permalink / raw)
  To: julien.grossholtz
  Cc: linux-gpio, linus.walleij, broonie, kernel, arnd, bamvor.zhangjian

Hi, Julien

On 01/09/2016 11:16 PM, Bamvor Zhang Jian wrote:
> Hi, Julien
>
> On 01/09/2016 06:56 AM, Julien Grossholtz wrote:
>> Hi,
>>
>> The issue is in the break condition of the list_for_each_entry_safe loop.
>> If there is a chip at the beginning it breaks even if there is another
>> one after. You should check the next chip:
>> if (chip->base > prev->base && chip->base + chip->ngpio <= next->base)
> Thanks you suggestion. It indeed fix the issue you mentioned.
> I will send the new version after I make sure there is no other corner case.
Could you do me favor to test the following patches?
It works for the following ranges: [0,31], [32,64], [-1,32], [-1, 32] and the
other test cases I wrote.

Regards

Bamvor

---
 drivers/gpio/gpiolib.c | 65 ++++++++++++++++++++++----------------------------
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..9460f94 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list) {
+		/* at the end of the list */
+		if (&(next->list) == &gpio_chips)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+		/* add between prev and next */
+		if ( prev->base + prev->ngpio <= chip->base
+				&& chip->base + chip->ngpio <= next->base) {
+			list_add(&chip->list, &prev->list);
+			return 0;
+		}
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */
-- 
2.1.4


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

* Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list
  2016-01-11 14:35         ` Bamvor Jian Zhang
@ 2016-01-11 19:56           ` Julien Grossholtz
  0 siblings, 0 replies; 7+ messages in thread
From: Julien Grossholtz @ 2016-01-11 19:56 UTC (permalink / raw)
  To: Bamvor Jian Zhang; +Cc: linux-gpio, linus walleij, broonie, kernel, arnd

Hi Bamvor,

I checked, your updated patch works well for me.

To improve readability I also suggest to change:
if (&(next->list) == &gpio_chips)
To something similar to:
if(prev == list_last_entry(&gpio_chips ,struct gpio_chip ,list)



----- Original Message -----
From: "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
To: "julien grossholtz" <julien.grossholtz@savoirfairelinux.com>
Cc: linux-gpio@vger.kernel.org, "linus walleij" <linus.walleij@linaro.org>, "broonie" <broonie@kernel.org>, kernel@savoirfairelinux.com, arnd@arndb.de, "Bamvor Jian Zhang" <bamvor.zhangjian@linaro.org>
Sent: Monday, January 11, 2016 9:35:13 AM
Subject: Re: [Kernel] [PATCH] gpiolib: rewrite gpiochip_add_to_list

Hi, Julien

On 01/09/2016 11:16 PM, Bamvor Zhang Jian wrote:
> Hi, Julien
>
> On 01/09/2016 06:56 AM, Julien Grossholtz wrote:
>> Hi,
>>
>> The issue is in the break condition of the list_for_each_entry_safe loop.
>> If there is a chip at the beginning it breaks even if there is another
>> one after. You should check the next chip:
>> if (chip->base > prev->base && chip->base + chip->ngpio <= next->base)
> Thanks you suggestion. It indeed fix the issue you mentioned.
> I will send the new version after I make sure there is no other corner case.
Could you do me favor to test the following patches?
It works for the following ranges: [0,31], [32,64], [-1,32], [-1, 32] and the
other test cases I wrote.

Regards

Bamvor

---
 drivers/gpio/gpiolib.c | 65 ++++++++++++++++++++++----------------------------
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 3db34e7..9460f94 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -189,55 +189,46 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction);
  */
 static int gpiochip_add_to_list(struct gpio_chip *chip)
 {
-	struct gpio_chip *iterator;
-	struct gpio_chip *previous = NULL;
+	struct gpio_chip *prev, *next;
 
 	if (list_empty(&gpio_chips)) {
-		list_add_tail(&chip->list, &gpio_chips);
+		/* initial entry in list */
+		list_add(&chip->list, &gpio_chips);
 		return 0;
 	}
 
-	list_for_each_entry(iterator, &gpio_chips, list) {
-		if (iterator->base >= chip->base + chip->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
-						<= chip->base)
-					goto found;
-			}
-		}
-		previous = iterator;
+	next = list_entry(gpio_chips.next, struct gpio_chip, list);
+	if (chip->base + chip->ngpio <= next->base) {
+		/* add before first entry */
+		list_add(&chip->list, &gpio_chips);
+		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.
-	 */
+	prev = list_entry(gpio_chips.prev, struct gpio_chip, list);
+	if (prev->base + prev->ngpio <= chip->base) {
+		/* add behind last entry */
+		list_add_tail(&chip->list, &gpio_chips);
+		return 0;
+	}
 
-	iterator = list_last_entry(&gpio_chips, struct gpio_chip, list);
-	if (iterator->base + iterator->ngpio <= chip->base)
-		goto found;
+	list_for_each_entry_safe(prev, next, &gpio_chips, list) {
+		/* at the end of the list */
+		if (&(next->list) == &gpio_chips)
+			break;
 
-	dev_err(chip->parent,
-	       "GPIO integer space overlap, cannot add chip\n");
-	return -EBUSY;
+		/* add between prev and next */
+		if ( prev->base + prev->ngpio <= chip->base
+				&& chip->base + chip->ngpio <= next->base) {
+			list_add(&chip->list, &prev->list);
+			return 0;
+		}
+	}
 
-found:
-	list_add_tail(&chip->list, &iterator->list);
-	return 0;
+	dev_err(chip->parent, "GPIO integer space overlap, cannot add chip\n");
+	return -EBUSY;
 }
 
+
 /**
  * Convert a GPIO name to its descriptor
  */

tested-by: Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>
-- 
2.1.4

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

end of thread, other threads:[~2016-01-11 19:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-08  6:37 [PATCH] gpiolib: rewrite gpiochip_add_to_list Bamvor Jian Zhang
2016-01-08  6:37 ` Bamvor Jian Zhang
2016-01-08 21:47   ` Julien Grossholtz
2016-01-08 22:56     ` [Kernel] " Julien Grossholtz
2016-01-09 15:16       ` Bamvor Zhang Jian
2016-01-11 14:35         ` Bamvor Jian Zhang
2016-01-11 19:56           ` Julien Grossholtz

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.