Re: [PATCHv5] gpio: Remove VLA from gpiolib

From: Phil Reid
Date: Mon Apr 16 2018 - 01:19:15 EST


G'day Laura,

One more comment.
On 16/04/2018 12:41, Phil Reid wrote:
G'day Laura,

On 14/04/2018 05:24, Laura Abbott wrote:
The new challenge is to remove VLAs from the kernel
(see https://lkml.org/lkml/2018/3/7/621) to eventually
turn on -Wvla.

Using a kmalloc array is the easy way to fix this but kmalloc is still
more expensive than stack allocation. Introduce a fast path with a
fixed size stack array to cover most chip with gpios below some fixed
amount. The slow path dynamically allocates an array to cover those
chips with a large number of gpios.

Reviewed-and-tested-by: Lukas Wunner <lukas@xxxxxxxxx>
Signed-off-by: Lukas Wunner <lukas@xxxxxxxxx>
Signed-off-by: Laura Abbott <labbott@xxxxxxxxxx>
---
v5: Dropped some outdated comments and extra whitespace. Switched to
ARCH_NR_GPIOS per suggestion of Linus Walleij.
---
 drivers/gpio/gpiolib.c | 76 +++++++++++++++++++++++++++++++++----------
 drivers/gpio/gpiolib.h | 2 +-
 include/linux/gpio/consumer.h | 10 +++---
 3 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index d66de67ef307..79ec7a29b684 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -61,6 +61,11 @@ static struct bus_type gpio_bus_type = {
ÂÂÂÂÂ .name = "gpio",
 };
+/*
+ * Number of GPIOs to use for the fast path in set array
+ */
+#define FASTPATH_NGPIO ARCH_NR_GPIOS

Also wouldn't this mean that fast path will never be triggered now...


+
 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
ÂÂ * While any GPIO is requested, its gpio_chip is not removable;
ÂÂ * each GPIO's "requested" flag serves as a lock and refcount.
@@ -399,12 +404,11 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd,
ÂÂÂÂÂÂÂÂÂÂÂÂÂ vals[i] = !!ghd.values[i];
ÂÂÂÂÂÂÂÂÂ /* Reuse the array setting function */
-ÂÂÂÂÂÂÂ gpiod_set_array_value_complex(false,
+ÂÂÂÂÂÂÂ return gpiod_set_array_value_complex(false,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ true,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ lh->numdescs,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ lh->descs,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ vals);
-ÂÂÂÂÂÂÂ return 0;
ÂÂÂÂÂ }
ÂÂÂÂÂ return -EINVAL;
 }
@@ -1192,6 +1196,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
ÂÂÂÂÂÂÂÂÂ goto err_free_descs;
ÂÂÂÂÂ }
+ÂÂÂ if (chip->ngpio > FASTPATH_NGPIO)
+ÂÂÂÂÂÂÂ chip_warn(chip, "line cnt %d is greater than fast path cnt %d\n",
+ÂÂÂÂÂÂÂ chip->ngpio, FASTPATH_NGPIO);
+
ÂÂÂÂÂ gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
ÂÂÂÂÂ if (!gdev->label) {
ÂÂÂÂÂÂÂÂÂ status = -ENOMEM;
@@ -2662,16 +2670,28 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂ while (i < array_size) {
ÂÂÂÂÂÂÂÂÂ struct gpio_chip *chip = desc_array[i]->gdev->chip;
-ÂÂÂÂÂÂÂ unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
-ÂÂÂÂÂÂÂ unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
+ÂÂÂÂÂÂÂ unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
+ÂÂÂÂÂÂÂ unsigned long *mask, *bits;
ÂÂÂÂÂÂÂÂÂ int first, j, ret;
+ÂÂÂÂÂÂÂ if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
+ÂÂÂÂÂÂÂÂÂÂÂ memset(fastpath, 0, sizeof(fastpath));
+ÂÂÂÂÂÂÂÂÂÂÂ mask = fastpath;
+ÂÂÂÂÂÂÂÂÂÂÂ bits = fastpath + BITS_TO_LONGS(FASTPATH_NGPIO);
Previously it looks like just mask was zeroed.
So could this just be:
 memset(mask, 0, BITS_TO_LONGS(chip->ngpio));

I'm guessing it's not a huge additional overhead as it is, but it's more in line with what was there.


+ÂÂÂÂÂÂÂ } else {
+ÂÂÂÂÂÂÂÂÂÂÂ mask = kcalloc(2 * BITS_TO_LONGS(chip->ngpio),
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(*mask),
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ can_sleep ? GFP_KERNEL : GFP_ATOMIC);
+ÂÂÂÂÂÂÂÂÂÂÂ if (!mask)
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
+ÂÂÂÂÂÂÂÂÂÂÂ bits = mask + BITS_TO_LONGS(chip->ngpio);
+ÂÂÂÂÂÂÂ }
+
ÂÂÂÂÂÂÂÂÂ if (!can_sleep)
ÂÂÂÂÂÂÂÂÂÂÂÂÂ WARN_ON(chip->can_sleep);
ÂÂÂÂÂÂÂÂÂ /* collect all inputs belonging to the same chip */
ÂÂÂÂÂÂÂÂÂ first = i;
-ÂÂÂÂÂÂÂ memset(mask, 0, sizeof(mask));
ÂÂÂÂÂÂÂÂÂ do {
ÂÂÂÂÂÂÂÂÂÂÂÂÂ const struct gpio_desc *desc = desc_array[i];
ÂÂÂÂÂÂÂÂÂÂÂÂÂ int hwgpio = gpio_chip_hwgpio(desc);
@@ -2682,8 +2702,11 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (desc_array[i]->gdev->chip == chip));
ÂÂÂÂÂÂÂÂÂ ret = gpio_chip_get_multiple(chip, mask, bits);
-ÂÂÂÂÂÂÂ if (ret)
+ÂÂÂÂÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂÂÂÂÂ if (mask != fastpath)
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kfree(mask);
ÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
+ÂÂÂÂÂÂÂ }
ÂÂÂÂÂÂÂÂÂ for (j = first; j < i; j++) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂ const struct gpio_desc *desc = desc_array[j];
@@ -2695,6 +2718,9 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂÂÂÂÂ value_array[j] = value;
ÂÂÂÂÂÂÂÂÂÂÂÂÂ trace_gpio_value(desc_to_gpio(desc), 1, value);
ÂÂÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ if (mask != fastpath)
+ÂÂÂÂÂÂÂÂÂÂÂ kfree(mask);
ÂÂÂÂÂ }
ÂÂÂÂÂ return 0;
 }
@@ -2878,7 +2904,7 @@ static void gpio_chip_set_multiple(struct gpio_chip *chip,
ÂÂÂÂÂ }
 }
-void gpiod_set_array_value_complex(bool raw, bool can_sleep,
+int gpiod_set_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array)
@@ -2887,14 +2913,26 @@ void gpiod_set_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂ while (i < array_size) {
ÂÂÂÂÂÂÂÂÂ struct gpio_chip *chip = desc_array[i]->gdev->chip;
-ÂÂÂÂÂÂÂ unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
-ÂÂÂÂÂÂÂ unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
+ÂÂÂÂÂÂÂ unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
+ÂÂÂÂÂÂÂ unsigned long *mask, *bits;
ÂÂÂÂÂÂÂÂÂ int count = 0;
+ÂÂÂÂÂÂÂ if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
+ÂÂÂÂÂÂÂÂÂÂÂ memset(fastpath, 0, sizeof(fastpath));
+ÂÂÂÂÂÂÂÂÂÂÂ mask = fastpath;
+ÂÂÂÂÂÂÂÂÂÂÂ bits = fastpath + BITS_TO_LONGS(FASTPATH_NGPIO);
+ÂÂÂÂÂÂÂ } else {
+ÂÂÂÂÂÂÂÂÂÂÂ mask = kcalloc(2 * BITS_TO_LONGS(chip->ngpio),
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(*mask),
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ can_sleep ? GFP_KERNEL : GFP_ATOMIC);
+ÂÂÂÂÂÂÂÂÂÂÂ if (!mask)
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
+ÂÂÂÂÂÂÂÂÂÂÂ bits = mask + BITS_TO_LONGS(chip->ngpio);
+ÂÂÂÂÂÂÂ }
+
ÂÂÂÂÂÂÂÂÂ if (!can_sleep)
ÂÂÂÂÂÂÂÂÂÂÂÂÂ WARN_ON(chip->can_sleep);
-ÂÂÂÂÂÂÂ memset(mask, 0, sizeof(mask));
ÂÂÂÂÂÂÂÂÂ do {
ÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc *desc = desc_array[i];
ÂÂÂÂÂÂÂÂÂÂÂÂÂ int hwgpio = gpio_chip_hwgpio(desc);
@@ -2925,7 +2963,11 @@ void gpiod_set_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂ /* push collected bits to outputs */
ÂÂÂÂÂÂÂÂÂ if (count != 0)
ÂÂÂÂÂÂÂÂÂÂÂÂÂ gpio_chip_set_multiple(chip, mask, bits);
+
+ÂÂÂÂÂÂÂ if (mask != fastpath)
+ÂÂÂÂÂÂÂÂÂÂÂ kfree(mask);
ÂÂÂÂÂ }
+ÂÂÂ return 0;
 }
 /**
@@ -3000,13 +3042,13 @@ EXPORT_SYMBOL_GPL(gpiod_set_value);
ÂÂ * This function should be called from contexts where we cannot sleep, and will
ÂÂ * complain if the GPIO chip functions potentially sleep.
ÂÂ */
-void gpiod_set_raw_array_value(unsigned int array_size,
+int gpiod_set_raw_array_value(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array, int *value_array)
 {
ÂÂÂÂÂ if (!desc_array)
-ÂÂÂÂÂÂÂ return;
-ÂÂÂ gpiod_set_array_value_complex(true, false, array_size, desc_array,
-ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ value_array);
+ÂÂÂÂÂÂÂ return -EINVAL;
+ÂÂÂ return gpiod_set_array_value_complex(true, false, array_size,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ desc_array, value_array);
 }
 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
@@ -3326,14 +3368,14 @@ EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
ÂÂ *
ÂÂ * This function is to be called from contexts that can sleep.
ÂÂ */
-void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
+int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array)
 {
ÂÂÂÂÂ might_sleep_if(extra_checks);
ÂÂÂÂÂ if (!desc_array)
-ÂÂÂÂÂÂÂ return;
-ÂÂÂ gpiod_set_array_value_complex(true, true, array_size, desc_array,
+ÂÂÂÂÂÂÂ return -EINVAL;
+ÂÂÂ return gpiod_set_array_value_complex(true, true, array_size, desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ value_array);
 }
 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index b17ec6795c81..b64813e3876e 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -188,7 +188,7 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
-void gpiod_set_array_value_complex(bool raw, bool can_sleep,
+int gpiod_set_array_value_complex(bool raw, bool can_sleep,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index dbd065963296..243112c7fa7d 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -116,7 +116,7 @@ int gpiod_get_raw_array_value(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
-void gpiod_set_raw_array_value(unsigned int array_size,
+int gpiod_set_raw_array_value(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
@@ -134,7 +134,7 @@ int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
-void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
+int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array);
@@ -369,12 +369,13 @@ static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
ÂÂÂÂÂ /* GPIO can never have been requested */
ÂÂÂÂÂ WARN_ON(1);
 }
-static inline void gpiod_set_raw_array_value(unsigned int array_size,
+static inline int gpiod_set_raw_array_value(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array)
 {
ÂÂÂÂÂ /* GPIO can never have been requested */
ÂÂÂÂÂ WARN_ON(1);
+ÂÂÂ return 0;
 }
 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
@@ -423,12 +424,13 @@ static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
ÂÂÂÂÂ /* GPIO can never have been requested */
ÂÂÂÂÂ WARN_ON(1);
 }
-static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
+static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct gpio_desc **desc_array,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int *value_array)
 {
ÂÂÂÂÂ /* GPIO can never have been requested */
ÂÂÂÂÂ WARN_ON(1);
+ÂÂÂ return 0;
 }
 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)





--
Regards
Phil Reid

ElectroMagnetic Imaging Technology Pty Ltd
Development of Geophysical Instrumentation & Software
www.electromag.com.au

3 The Avenue, Midland WA 6056, AUSTRALIA
Ph: +61 8 9250 8100
Fax: +61 8 9250 7100
Email: preid@xxxxxxxxxxxxxxxxx