Received: by 10.213.65.68 with SMTP id h4csp110987imn; Thu, 15 Mar 2018 11:05:11 -0700 (PDT) X-Google-Smtp-Source: AG47ELuH0qkymztOK6e/kWYcUkup++qrzlbJTENx3wgRNjwU4Ps5cZNAlS1bK7QBCeWmDISHQI+X X-Received: by 2002:a17:902:aa97:: with SMTP id d23-v6mr3596092plr.320.1521137111063; Thu, 15 Mar 2018 11:05:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521137111; cv=none; d=google.com; s=arc-20160816; b=vxFPnL4+1mvEq2EaHcUS+q5UbCjVi5alWbC1PpBQna0mKL5Bbh3vy1Z3GPXeXPe01N kOM00muLQmfsWaNOROaKsbTNP0DfdyoPbRfN+36nEMfRq6SwX+VxUdRC6A+cM96rpjAC O6WFrs7CHOynGeiKgeGHagAewULDzKhNgcY5C5ha3u+tBEr4L22QR+WfidauG0ruJGHg jBYNOjUWkbdcXL9oitmAsBE4fF1haKt8r9kTl3JcwnzMH/pL6BkqgRBUqVZt8wovg7Pd v//ES466H8OfnrIvEQ3Dx83GqjHIQ35+CUYJV8VWhtAc8nD2Ot5wKyf/xD+xJ2s1sMqP 7JFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=j7YMmp47d5awK1SCCHOQ+4Xw92TK6fpAagNfJHET6MU=; b=C+IvMmF4jFSNdj+TFRSgSLZm0FiljfAKGANOL3RYtpYuuBr2vf6MUsRof3Ec6Ob5+h cnQwuqxyENL+sRakISJw3VB76U/mmTVGjBrwVTt5Z22J+0VKeiFHYDSRCRjcGfueEX6U rc+gXIDHPMdHjjDqg1vSruAPAnFotkxAawzIFWPfb9h9QpMii2z5hpV1lOcGN14ly3Qh bdeZ9HuXqkXRT/UFZU8VUXlpMdI6HP4riXRMOH0Mxz+thCEltggtRUonkcIl+TptMM+F 2CcVGntPL/6jisb3QFm5n9VvgC0lI2BoDlio0jn/qK2l1r8sRRicpm5Qe8rTer7kQmI0 Iuug== 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 j10si3752406pgs.597.2018.03.15.11.04.56; Thu, 15 Mar 2018 11:05:11 -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 S932349AbeCOSAn (ORCPT + 99 others); Thu, 15 Mar 2018 14:00:43 -0400 Received: from mail-oi0-f68.google.com ([209.85.218.68]:42522 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751714AbeCOSAl (ORCPT ); Thu, 15 Mar 2018 14:00:41 -0400 Received: by mail-oi0-f68.google.com with SMTP id c18so6451692oiy.9 for ; Thu, 15 Mar 2018 11:00:41 -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:in-reply-to :references; bh=j7YMmp47d5awK1SCCHOQ+4Xw92TK6fpAagNfJHET6MU=; b=en2kHVE4mb6wbiYL31dsNMq0lF1RgKOxdPOjSrUMixRJsYbBNe46HokY3rsuJgfSdD 73VIzz/7uNEmCxlgG7FcoPxzH0ca4EZlguCjEVt7UkAUiI1SFcdqLoK4p9DzZaAGhj6k Bdr5m5B21ImlWwowqz+7q1eoBE59D2tY1p4V06FWYEaeidcJmMjCqqu3oVHB6XOf7K5V M8tYDfFJLGNlSjVW20wtBFFFZ/QX/3g93e4WO8sRgDX8sLQEGVb5elK1OcM8Ny76jULu j74+Fv0MgvqMk/N4W+h+IE3jzkePr1gfHWJxIQ88XLkbVFGmrvm1XbFUTo442NYQYH8t sEHg== X-Gm-Message-State: AElRT7FK3IbUE30snDTTQV+0eqtYhRav2ZGFGWOgFbs5vyjkHVK/LrHA sQAFOILP8IXtnCrV4bEF6AJ+KA== X-Received: by 10.202.228.141 with SMTP id b135mr6052824oih.137.1521136840501; Thu, 15 Mar 2018 11:00:40 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::b761]) by smtp.gmail.com with ESMTPSA id 68sm1372098otj.34.2018.03.15.11.00.37 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 15 Mar 2018 11:00:38 -0700 (PDT) From: Laura Abbott To: Linus Walleij , Kees Cook Cc: Laura Abbott , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Lukas Wunner , Mathias Duckeck , Nandor Han , Patrice Chotard Subject: [PATCHv2 1/4] gpio: Remove VLA from gpiolib Date: Thu, 15 Mar 2018 11:00:27 -0700 Message-Id: <20180315180030.20001-2-labbott@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180315180030.20001-1-labbott@redhat.com> References: <20180315180030.20001-1-labbott@redhat.com> 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. This patch replaces several VLAs with an appropriate call to kmalloc_array. Signed-off-by: Laura Abbott --- v2: Switched to kcalloc. Adjusted return types to propegate error code. --- drivers/gpio/gpiolib.c | 76 ++++++++++++++++++++++++++++++++++--------- drivers/gpio/gpiolib.h | 2 +- include/linux/gpio/consumer.h | 8 +++-- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d66de67ef307..c81e5d0aea42 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -390,6 +390,7 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd, return 0; } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { + int ret; /* TODO: check if descriptors are really output */ if (copy_from_user(&ghd, ip, sizeof(ghd))) return -EFAULT; @@ -399,11 +400,14 @@ 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, + ret = gpiod_set_array_value_complex(false, true, lh->numdescs, lh->descs, vals); + if (ret) + return ret; + return 0; } return -EINVAL; @@ -2662,16 +2666,32 @@ 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 *mask; + unsigned long *bits; int first, j, ret; + mask = kcalloc(BITS_TO_LONGS(chip->ngpio), + sizeof(*mask), + can_sleep ? GFP_KERNEL : GFP_ATOMIC); + + if (!mask) + return -ENOMEM; + + bits = kcalloc(BITS_TO_LONGS(chip->ngpio), + sizeof(*bits), + can_sleep ? GFP_KERNEL : GFP_ATOMIC); + + if (!bits) { + kfree(mask); + return -ENOMEM; + } + + 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) { + kfree(bits); + kfree(mask); return ret; + } for (j = first; j < i; j++) { const struct gpio_desc *desc = desc_array[j]; @@ -2695,6 +2718,8 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep, value_array[j] = value; trace_gpio_value(desc_to_gpio(desc), 1, value); } + kfree(bits); + kfree(mask); } return 0; } @@ -2878,7 +2903,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 +2912,29 @@ 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 *mask; + unsigned long *bits; int count = 0; + mask = kcalloc(BITS_TO_LONGS(chip->ngpio), + sizeof(*mask), + can_sleep ? GFP_KERNEL : GFP_ATOMIC); + + if (!mask) + return -ENOMEM; + + bits = kcalloc(BITS_TO_LONGS(chip->ngpio), + sizeof(*bits), + can_sleep ? GFP_KERNEL : GFP_ATOMIC); + + if (!bits) { + kfree(mask); + return -ENOMEM; + } + 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 +2965,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); + + kfree(mask); + kfree(bits); } + return 0; } /** @@ -3000,13 +3044,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 +3370,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..da52610cc3bd 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) @@ -429,6 +430,7 @@ static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, { /* 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