Received: by 10.213.65.68 with SMTP id h4csp1075123imn; Sun, 18 Mar 2018 13:35:23 -0700 (PDT) X-Google-Smtp-Source: AG47ELuYeKAFvvzJRKXUbp8w3rNiyohXWH8fOsY78GWAx+OXFQn9ZjaEPzeL4t3eYLjBdzDNdh8b X-Received: by 2002:a17:902:102c:: with SMTP id b41-v6mr5533186pla.39.1521405323485; Sun, 18 Mar 2018 13:35:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521405323; cv=none; d=google.com; s=arc-20160816; b=fL0rU0nECEN5mpOsq2rdL7lbJUNQYk/VEYeojapLWG75YrRqUBe54xw+WnvODSh6mY u/KmSCt8kDtCRTIJfadmWIHXtICSockSDyq6fokwwr0BnKhDBS1KSr3EfQc/WxZ//LKd rxVBTyWJewJbhodZmMTzyunF+6o0SSLLLnikGLTDyuBwSRDzDzu1Adte+PBBW/gg4/us cfa6DHTmDH3eer4j1s1bNvi/+O+uHyqQ6SnozG0lHkxDenB3eQNTPfDh7RWGsvW3K0DW Od5sefsA2HW/vLuxEDbUT75qymW0YZaFrZgbCceNwM+heHTiJgamRPiTSBTxwUCAXufc 0ePQ== 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=Rf5gOgm+K3dV8RV6FvL1tqgOMzNFRLRtcGqSFGzGTOY=; b=tl1SzpsQGZpHdC8fE49b/WMHCMCpeLSsxU6C7gfCv+wsLSvKJH2iUxWY10TO0UyTSY lCDHcqUszAd3aiWCCdd5EeduLxhTapM3fSTXnb1GOQark0Ld2/0Se8d7wylySTzci+t/ DaRkb8bu20Y02odTwlpPnEwBQy1BSR9k7sMIeGv7BYHtLsCTMyvHhUEKwl5ZaFXKQQDP jO3cnwWZsBGOQVnjpQljSYb7+xCNkEYeEMCkKlmzXm5y2nuTeUTHbSZmQD/VQEOAbYk7 q9eoBk6rMAFKizRSjyd1MBNKXgXUMEBB2jEE91B4szqsa5aY5ipfC3R7MOYnpvn1ohaO InHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=e1/g4/K6; 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 34-v6si10637539plm.543.2018.03.18.13.35.09; Sun, 18 Mar 2018 13:35:23 -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=e1/g4/K6; 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 S1754535AbeCRUeR (ORCPT + 99 others); Sun, 18 Mar 2018 16:34:17 -0400 Received: from mail-wm0-f49.google.com ([74.125.82.49]:32800 "EHLO mail-wm0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754239AbeCRUeP (ORCPT ); Sun, 18 Mar 2018 16:34:15 -0400 Received: by mail-wm0-f49.google.com with SMTP id s206so9907578wme.0 for ; Sun, 18 Mar 2018 13:34:14 -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=Rf5gOgm+K3dV8RV6FvL1tqgOMzNFRLRtcGqSFGzGTOY=; b=e1/g4/K6bQ89B6MW8UWlbxonMY3RoXB1cmP5QaAThrQeFpq+zaQQRV4N3DOqCjSww+ 0EI4rioHydbxuGPRdjS9aYoET2wZZ9mzjBw8uouDIJcer+24ZSX7dgaIqAfD47ppCxBV kRhqEArWMQZ/izMb0fZ0JRt7w6v4Xo5YlOj+4= 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=Rf5gOgm+K3dV8RV6FvL1tqgOMzNFRLRtcGqSFGzGTOY=; b=IGhMLi6kaIz1x94gaU7DBSK8SFnjw+w4SCLchtoPqws8rZ+ZjY6IAn9/LscCSlUdf9 0aTsVIQ+dH6720KvyKZi0lPAsfDRNPblYHID92koBKwdbYqrckkT822E57tm+Ui/iO/l UyrGJ2O3vJO1KOVwjXx4urxdZltMwxogGl514vVzEBKUbWj4ZBD1nV89WTjsYqf+MTjK VrHrt6S7LLZT0GSAAwI/UPluCeaYt4JA7VQZxHUUqlLlTSAeA87hoOHSbx2EXlk3n1xL OPGNBaZnxzRS5pP3UIjVN18Gb1sje29D3M4JKA0bEbZvO5VDWADch3E49qpYtfMXAkMT Yc1g== X-Gm-Message-State: AElRT7GZo4LXmM+Bqr+gKiPQEn5Q4WgnkY9CYWbp2arQ+kXEKyT/g0m3 744aCbaZgPg+20YuLvrFaj9O1g== X-Received: by 10.80.214.141 with SMTP id r13mr10705440edi.288.1521405254268; Sun, 18 Mar 2018 13:34:14 -0700 (PDT) Received: from [192.168.0.189] (dhcp-5-186-126-104.cgn.ip.fibianet.dk. [5.186.126.104]) by smtp.gmail.com with ESMTPSA id y17sm7037909edl.67.2018.03.18.13.34.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Mar 2018 13:34:13 -0700 (PDT) Subject: Re: [PATCH 1/4] gpio: Remove VLA from gpiolib To: Lukas Wunner , Rasmus Villemoes Cc: Laura Abbott , Linus Walleij , Kees Cook , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Mathias Duckeck , Nandor Han , Semi Malinen , Patrice Chotard References: <20180310001021.6437-1-labbott@redhat.com> <20180310001021.6437-2-labbott@redhat.com> <20180317082509.GA2579@wunner.de> <20180318142327.GA23761@wunner.de> From: Rasmus Villemoes Message-ID: <0f17eb05-c183-bec9-0076-5ddd00d70f15@rasmusvillemoes.dk> Date: Sun, 18 Mar 2018 21:34:12 +0100 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: <20180318142327.GA23761@wunner.de> 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-18 15:23, Lukas Wunner wrote: >>> >>> Other random thoughts: maybe two allocations for each loop iteration is >>> a bit much. Maybe do a first pass over the array and collect the maximal >>> chip->ngpio, do the memory allocation and freeing outside the loop (then >>> you'd of course need to preserve the memset() with appropriate length >>> computed). And maybe even just do one allocation, making bits point at >>> the second half. >> >> I think those are great ideas because the function is kind of a hotpath >> and usage of VLAs was motivated by the desire to make it fast. >> >> I'd go one step further and store the maximum ngpio of all registered >> chips in a global variable (and update it in gpiochip_add_data_with_key()), >> then allocate 2 * max_ngpio once before entering the loop (as you've >> suggested). That would avoid the first pass to determine the maximum >> chip->ngpio. In most systems max_ngpio will be < 64, so one or two >> unsigned longs depending on the arch's bitness. > > Actually, scratch that. If ngpio is usually smallish, we can just > allocate reasonably sized space for mask and bits on the stack, Yes. > and fall back to the kcalloc slowpath only if chip->ngpio exceeds > that limit. Well, I'd suggest not adding that fallback code now, but simply add a check in gpiochip_add_data_with_key to ensure ngpio is sane (and refuse to register the chip otherwise), at least if we know that every currently supported/known chip is covered by the 256 (?). That keeps the code simple and fast, and then if somebody has a chip with 40000 gpio lines, we can add a fallback path. Or we could consider alternative solutions, to avoid a 10000 byte GFP_ATOMIC allocation (maybe hang a pre-allocation off the gpio_chip; that's only two more bits per descriptor, and there's already a whole gpio_desc for each - but not sure about the locking in that case). Rasmus