Received: by 10.213.65.68 with SMTP id h4csp1633005imn; Thu, 5 Apr 2018 00:42:31 -0700 (PDT) X-Google-Smtp-Source: AIpwx498jW/SaxdDGzt8lq2+bCbCGBPpYei810dHnkqz14RQ+HwAHvQRaexOh6J8wepp7oFJ3xp2 X-Received: by 10.101.90.203 with SMTP id d11mr13935874pgt.20.1522914151611; Thu, 05 Apr 2018 00:42:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522914151; cv=none; d=google.com; s=arc-20160816; b=yufohgpjrzaZR5Rs72ooWrUKGHrk26ZjkRW5ZMg9vH6cuH44s6BEqYxhmXNSeUAskc rWIF8L85bC3MZDy4NuiE80bzYwjRBjjpZnghKYJV/6J/e8TUMgiGZVvylx93VM/BmI7h XYJHRRXmk2fvUZ4L/5BpSRp+j+KhVc1w1em766thgI8GvRXtRg+oSX8ZIAj1x8IxThWD 73HTHaA4nmb6jaQlr+UcBlf06mpC0PN44fVDz8eASq7ltatQX7pDd5QfnlwP83hf9htP +A6qUAOeMrkBN69KyOzpc28u6yV+nlmmjnGbRZpMXQnQfOkx7rAZlHsxItKMdswaloSB e7Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=lpCS20MwfKcij7ZoAryIg5YpLPwVlYXVGHTA6qNl7dU=; b=hePnXBS6Iqe3EehV6H9jRIg8fO43bmKfBllPCaSMiBdHcFMWjxVmydwgJQFNVk8Pgv Z5zo+q3EfmQVxlhfrMVEb7cOERrInX7ppqEpZnjIN7yGl6U/uzze8VQmdfkVEFT2APFW /nipJgb4FeJ1eA9z6rLQVJvLdRJDBJ17uUXmhMdQMOVEQtvyJlblnIkx23WDzbbwvjmX b9DDi+LM+gUyrAOOdu6KPksDBivG4ej0qjsi/14EDQzR6lqI9Xt2HTZSBeY3cRWtrSC4 w3p3FwyTYS7iDM4A8OCfkTdAXr8F8HQuni9ICJ04DgyoVYQLVS+Tv2BM3SWq0uTrbgi9 utIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=aZ9q0Kvz; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x5-v6si3893635plv.293.2018.04.05.00.42.17; Thu, 05 Apr 2018 00:42:31 -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; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=aZ9q0Kvz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751315AbeDEHlH (ORCPT + 99 others); Thu, 5 Apr 2018 03:41:07 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36521 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751216AbeDEHlG (ORCPT ); Thu, 5 Apr 2018 03:41:06 -0400 Received: by mail-wm0-f65.google.com with SMTP id x82so4134088wmg.1 for ; Thu, 05 Apr 2018 00:41:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=lpCS20MwfKcij7ZoAryIg5YpLPwVlYXVGHTA6qNl7dU=; b=aZ9q0KvzjTqtyHqGJFjFcMTikR2bEqPPHVZH96BlThNx7HFHoATG37ZxQkJr+OAfbK ZY1CiTLkliEBXbxhwkREwqhOU+os/WF441fd/EchmbSWVh13OXZMfPMMyzWNuHb+/Jwj XScSrlbvEFnG7lT8aUn/RhKdx7wEbA+UFonfw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lpCS20MwfKcij7ZoAryIg5YpLPwVlYXVGHTA6qNl7dU=; b=AGnDKrx/sO6u7BzChOKSR6UhT3fJfxe9pI/ga9l8ugYmPBPFrJKlv/i/zovMCr5Dsl fVt3mg4NleY/UuoH+EdMJlLl6LNCWdYmTYWqqfyXm75aZ0alH5CUkWmmhSWn22VD6VPr pyK8lzXpFstrEqRjMbXLexghH9hHTjgee6ad8GJOeTKckeY3wMT5T2og6ZB8TIOgZ9Uc tGDYLVvrI39C6Qt+38HCqO+/z+VyD2YtF+G02PrhUknUxi45ry+cRGyO0KZtubEiR1ys kjjHxCBS2ha8Ckb3aE8Q2rQERnCK5jkNUbFcapwDSB8N21dMEDoVOPmcDYmtJZIpUdbI YaUQ== X-Gm-Message-State: ALQs6tDLyJIBAqTjPy7XXqM7l56dbHpVxN1qk16SrdvbYjM5vYfnXxCS YieRBhMC0doDFyuI2ndsjWDhog== X-Received: by 10.46.47.7 with SMTP id v7mr6092516ljv.56.1522914064872; Thu, 05 Apr 2018 00:41:04 -0700 (PDT) Received: from [172.16.11.29] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id j64sm1185223ljb.61.2018.04.05.00.41.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Apr 2018 00:41:04 -0700 (PDT) Subject: Re: [PATCHv3] gpio: Remove VLA from gpiolib To: Laura Abbott , Linus Walleij , Kees Cook , Lukas Wunner Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com References: <20180328181809.24505-1-labbott@redhat.com> From: Rasmus Villemoes Message-ID: Date: Thu, 5 Apr 2018 09:41:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180328181809.24505-1-labbott@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-03-28 20:18, 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. > > Signed-off-by: Lukas Wunner > Signed-off-by: Laura Abbott > --- > v3: Split out from the series since patches have been picked up > independently. Fold in patch from Lukas Wunner to introduce slow/fast > paths. I took his suggestions to go with 384 as the maximum number of > gpios. Also fixed one 0-day bot issue where I forgot to change the > return type. > > 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 *slowpath = NULL, *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 { > + slowpath = kcalloc(2 * BITS_TO_LONGS(chip->ngpio), > + sizeof(*slowpath), > + can_sleep ? GFP_KERNEL : GFP_ATOMIC); > + if (!slowpath) > + return -ENOMEM; > + mask = slowpath; > + bits = slowpath + BITS_TO_LONGS(chip->ngpio); > + } > + To avoid the static analysis complaints about the "if (slowpath) kfree(slowpath)" pattern, I'd suggest getting rid of the slowpath variable and simply assign the kcalloc directly to mask. Then the condition becomes "if (mask != fastpath) kfree(mask)". A comment won't silence subsequenct coccicheck runs, and unfortunately probably won't prevent certain individuals from sending auto-generated patches. Maybe also pull out the "bits = mask + BITS_TO_LONGS(chip->ngpio)" to the common path. Another thing: Maybe a pr_warn or at least a pr_info would be in order when a gpio chip with > FASTPATH_NGPIO lines is registered? And if that triggers for a lot of different SoCs, one could consider changing it to a CONFIG_ thing, or just bumping it to 512 if that would seem to cover all the reports. Rasmus