Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2645116imm; Wed, 16 May 2018 17:01:49 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr25yuYCW1yBmBuKc5fDws2Z6tqnhzN/pkWSqL3sRlv1HN3U0G8AB3yDaayVP/gLSMf5V7x X-Received: by 2002:a63:69c3:: with SMTP id e186-v6mr2325416pgc.353.1526515309211; Wed, 16 May 2018 17:01:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526515309; cv=none; d=google.com; s=arc-20160816; b=F+xMlVKmoyAonr3AlcqEPwB1FOKvFNs20J3yWzFk5geyPSJ93G15ShALkHaNYXJYk8 noCxJtPsS71H6Zo48t0V1OR4n/IQR6yzgORbs1uOQr8a9zUZVUS+Mj5lDe/adnWVMaN+ R2X6AG6COELf4k6hIjtT3y+8Ye5I5J+AFPgV7JAQ2iVmXvFBwN/3tcW4R29/RcaOPa9/ DB8O4WBpsaQS8OYgiJHNulVAWifrQYxd/CX2pQFbMUwSLX0/rftyw2QW5UiNoGtTDDCQ cI6zvcqp4CKzrnHETc4MH5xSul7MEmpTrXGV/EWkqbWNQPBFi0zn5Wjf5IzmE1c+52yy vhaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=+YB6Hp7uzdGtnSV1PgaBslPkH0r9+QXILr877ZMZ9hQ=; b=JH4pBDnHbTcOIu2xvWk7MBwws4bAcDQ4TXuFm9rbn9FmCJ0fBFgnUyajG3m5qvq+0l +jYed0r/a35p3k/SHqJLiqUumjIB/s0I+enEElYQXXtai11mX1PktRTFIctGUog93F3a NSInqvSQ3wdC8Z2O6m1yx8oYncusJ3zV9q8mqCSOeB2G5jSfVe3lQGYDU+53Yo6vczP6 qGo7Vo5a/icMTf7ow2HdZzmAv8N+rj2g0d+N/IrSpWfxGENma9CHnYxbKQYBqTQgSxbI R88H7jaIsG9AJBa6GCb4rCEfaRSzbrBbq8cIoxDhwxlhY29dw4CzpQ00u+svpo6IsVPr Kedw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u74-v6si2745727pgc.186.2018.05.16.17.01.34; Wed, 16 May 2018 17:01:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752500AbeEQAAj (ORCPT + 99 others); Wed, 16 May 2018 20:00:39 -0400 Received: from mail-oi0-f68.google.com ([209.85.218.68]:41488 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752472AbeEQAAh (ORCPT ); Wed, 16 May 2018 20:00:37 -0400 Received: by mail-oi0-f68.google.com with SMTP id 11-v6so2378258ois.8 for ; Wed, 16 May 2018 17:00:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+YB6Hp7uzdGtnSV1PgaBslPkH0r9+QXILr877ZMZ9hQ=; b=P0Mxt/eJSoK1vXRWKtC7QVLSmOMF4MIUBcSpAdRn1DGxsjTxyhqasoIJJLsAlC/3/3 X3CWPNg02DNHxMD2iTkcYE/KwjvwXqLm19ekLnXoiKWLSJNXYAuvt2LoruWRDU7Y4xrv vS0+4HhVqjetLC+GsMV/A2RtZZlMQgGVtNvTVisoTAsgMxkD1eJZRn/tt644pCA83SHU jX0zq7SzTUu2lHlnQ7sAxp5QhyYxY+iZJwOXqP58JC6P2EQuT76c7KXtC6Ri6pKG+GxW oL59TNjuqJ1Pz9zMj5mjqHB55AXzXR6oSeDTDRl9XZYlIPYcTKfESnA8hHbredeVpol9 1+aA== X-Gm-Message-State: ALKqPwdHeRaxnSSlDSTk4PT7pVRhIYKNGbPYsUXwbzXsh8+PUHSbYkjY WasGZoKCxrPlI+ZFXJIOU8LfPQ== X-Received: by 2002:aca:cc89:: with SMTP id c131-v6mr2065292oig.75.1526515236768; Wed, 16 May 2018 17:00:36 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::d2dd]) by smtp.gmail.com with ESMTPSA id u13-v6sm1749516oia.37.2018.05.16.17.00.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 16 May 2018 17:00:35 -0700 (PDT) From: Laura Abbott To: Linus Walleij , Kees Cook , Lukas Wunner , Rasmus Villemoes Cc: Laura Abbott , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Phil Reid , Geert Uytterhoeven Subject: [PATCHv6] gpio: Remove VLA from gpiolib Date: Wed, 16 May 2018 17:00:27 -0700 Message-Id: <20180517000027.26639-1-labbott@redhat.com> X-Mailer: git-send-email 2.17.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Lukas Wunner Signed-off-by: Laura Abbott --- v6: Introduce a config option for setting the fast path GPIOs because there are too many combinations to make the arch default workable. I went with a default of 512 in the Kconfig. Also to other points: I don't think the warning should be triggerable from userspace, it should only happen on probe. I also think only memsetting half the array is more likely to be error prone. We can change it if there is significant overhead. --- drivers/gpio/Kconfig | 10 +++++ drivers/gpio/gpiolib.c | 76 +++++++++++++++++++++++++++-------- drivers/gpio/gpiolib.h | 2 +- include/linux/gpio/consumer.h | 10 +++-- 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 68d812b38be7..2855b5c5c8ca 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -22,6 +22,16 @@ menuconfig GPIOLIB if GPIOLIB +config GPIOLIB_FASTPATH_LIMIT + int "Maximum number of GPIOs for fast path" + default 512 + help + This adjusts the point at which certain APIs will switch from + using a statically allocated fixed size buffer to a dynamically + allocated buffer. This is a trade-off in stackspace vs. speed. + You shouldn't need to change this unless you really need to + optimize one of those two. + config OF_GPIO def_bool y depends on OF diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d66de67ef307..f7ce546796e0 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 CONFIG_GPIOLIB_FASTPATH_LIMIT + /* 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); + } 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) -- 2.17.0