Received: by 10.192.165.156 with SMTP id m28csp138274imm; Tue, 10 Apr 2018 18:16:53 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+6bo8di2Ge01bqVQWYj8MCkn1eMfB+JSCsTFSDQ9kZWy+bhhp+B1YMvHutZfGJt05rzc7B X-Received: by 2002:a17:902:a514:: with SMTP id s20-v6mr2700136plq.272.1523409413857; Tue, 10 Apr 2018 18:16:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523409413; cv=none; d=google.com; s=arc-20160816; b=fPS/6t7iA2dtfM2A3/lGhKU6gvNozdpkZ7P6M2cMP5bws2a3aKN9qJclB5a9HF/SM4 WudIwOZ0/4WyrdJwE8Q9HVAf6euAdrKdr5sGKGPwyuA2ePGnGbcWb/o/tGs63gpirpm/ eCoGXl9kMY24xHc2+V2FUBLQ02bUnc2NHA86Gy48iF+PVY8nA4g1Z7wFJGRFJHf3wz7w ffhRpSaYckh2xwkYIHK0VTHS7fEcvYCOCfuKWn9xdpnCGXRAVMtuYj9ikHZd5mQuQhEn X0Zmd7YFYWUqF8TkFZnh5PDvFvuB7BIyKWQbygG9fSgbFz7S7TxlvgWwfh12HQeJB5lr GaoQ== 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=qZtCb8WTK8SD0ZYyNTlFNJ8k3oiY7Jz5Pa30z2IYpAE=; b=np3tu/F0NpijEqXO8NCLc5KLPeQAYq6XplABQ3tZe78HGfjmWwE+7U2+4zAzomqxvw XbJrJ+LRsEZ+6IXKT2e3JvjnXUvaYcm+CF2yp6tGRLN7NjFI3zZxkOH2zp23N1A0nol0 4Y6mCZh85PNYqpmOxCgnXBawQ7WzXAfrdLqOhIuYmfwTyQtaQkmfXa9ktPMM6AWM5ePB ee4pGrFYKhm3BMCtHO2Kn0HvOsdvkSYpO4q2e3Fqe1pLeui1DL+yActF8h5KvOrsJOMu OhmMC/0VzzaipfteM0m4Bl9nemkk8Htz5uKureGKH8aArsrZKvdo1az9fQatIsdnhsLh zccA== 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 m7si1873505pgs.727.2018.04.10.18.16.16; Tue, 10 Apr 2018 18:16:53 -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 S1753073AbeDKBEI (ORCPT + 99 others); Tue, 10 Apr 2018 21:04:08 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:37885 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752836AbeDKBEF (ORCPT ); Tue, 10 Apr 2018 21:04:05 -0400 Received: by mail-pf0-f196.google.com with SMTP id p6so137922pfn.4 for ; Tue, 10 Apr 2018 18:04:05 -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=qZtCb8WTK8SD0ZYyNTlFNJ8k3oiY7Jz5Pa30z2IYpAE=; b=rmWvJrFqckdQ85x12yhe/Ve5KIRqIaUJ7HBZbivIOFGgXK7OyRUAjLEDD0HUDfEqML W3OFlFJhCW1ojJo8Rw5fvLrZkbCW5Y6NMLz8kRzt7ccp19FFNqSgHJdajy24mt+KDlvN fAU242Je8/OyNGMVubOonBk9sJfT4wQQcpqugOMDlS53sPebje5X+tbZe166vPDDo/FD palIPJZuDGyMYuSSvZQrr0+iIkktX3dSMY+5f2KnyCZREGugJd9a+hl7KWxqc8gnWRTJ J/70CuUqs8z+ZAFKVyIzbSshJH32/cRNvEWZw0WUl8qP+RoKClxumq/T48WbZuiqp981 oCEA== X-Gm-Message-State: ALQs6tBBIaD4MT0eqKuWGFNFvcmv0zfrgYCpoJv7PbtpPF1CaHFuxY6V ymV7oer2mcCSMYBWZqHt5UBEjQ== X-Received: by 10.98.157.219 with SMTP id a88mr2158588pfk.75.1523408644724; Tue, 10 Apr 2018 18:04:04 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc:4eb2:6dae:ab32:e5b0]) by smtp.gmail.com with ESMTPSA id k24sm7474951pfj.32.2018.04.10.18.04.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Apr 2018 18:04:03 -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 Subject: [PATCHv4] gpio: Remove VLA from gpiolib Date: Tue, 10 Apr 2018 18:03:52 -0700 Message-Id: <20180411010352.17929-1-labbott@redhat.com> X-Mailer: git-send-email 2.14.3 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 --- v4: Changed some local variables to avoid coccinelle warnings. Added a warning if the number of GPIOs exceeds the current fast path define. Lukas, I kept your Tested-by because the changes were pretty minimal. Let me know if you want to run the tests again. --- drivers/gpio/gpiolib.c | 89 ++++++++++++++++++++++++++++++++++--------- drivers/gpio/gpiolib.h | 2 +- include/linux/gpio/consumer.h | 10 +++-- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d66de67ef307..b6605255662e 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 256 + /* 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; @@ -2653,6 +2661,7 @@ static int gpio_chip_get_multiple(struct gpio_chip *chip, return -EIO; } + int gpiod_get_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array, @@ -2662,16 +2671,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 +2703,15 @@ 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) { + /* + * Go against style and intentionally do the check for + * performance reasons + */ + if (mask != fastpath) + kfree(mask); return ret; + } for (j = first; j < i; j++) { const struct gpio_desc *desc = desc_array[j]; @@ -2695,6 +2723,13 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep, value_array[j] = value; trace_gpio_value(desc_to_gpio(desc), 1, value); } + + /* + * Go against style and intentionally do the check for + * performance reasons + */ + if (mask != fastpath) + kfree(mask); } return 0; } @@ -2878,7 +2913,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 +2922,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 +2972,15 @@ 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); + + /* + * Go against style and intentionally do the check for + * performance reasons + */ + if (mask != fastpath) + kfree(mask); } + return 0; } /** @@ -3000,13 +3055,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 +3381,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.14.3