Received: by 10.213.65.68 with SMTP id h4csp1311277imn; Mon, 19 Mar 2018 00:02:52 -0700 (PDT) X-Google-Smtp-Source: AG47ELthiSNEUKVsqaj/OXWdAOdSxq5zqQ4KG5uf/ZLSr7W6nbyZjRhnh0cUklM5+4Lsr0h7BsRm X-Received: by 10.99.100.197 with SMTP id y188mr8229988pgb.277.1521442972069; Mon, 19 Mar 2018 00:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521442972; cv=none; d=google.com; s=arc-20160816; b=lD2Wrg/6JKLVNvnBE858SCWLZNmEtrpQxU8gwPl3sz9abjM9zsZ7SDMiIAtlkQEUyn GIsyald49fheyJCQJ/DOBNK09CBdxr4JRHFKQfFgnzMb3czi+FlbkF/XDmVYTBH5r3RL RvUDUHrNm/EcsbWnZo2r+I6w9ykpP76raoDUMf9FndTyU1X99/JWuYtS4ykkWjnFU6v0 V1yuyRqku86DOh2T/oy1Jr//56ggrAKF5VyZB01zqOduJLLrLlosB1vuU2pg68MOjpti KrtDHdOtNZ92+cJcElNvsIFmEZWAUr9jykJC/THXLYgiT+ztMX3s/HY6DoItQYDG5AQE eCGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=FOrMNQs3zFkTFUiolgeNT5U+ikK/8zW123QNGVjbuRY=; b=wv6w8jrc6JMyJomKSO21zngFcQ+SLr6Mblsm6Arf1a50f0VL2aGOhfPZ/ae3Ge1PtI /Veu9tWjplYiVI46WG3UdqCHN0R5swmRjItOJcd9ipOJsGOBHveaWvNdK0t+LF8syMN8 gAfnBAz5Y40HoPM1/x3Eq+ha/PTWL3J7PKDYzTcuISJYYEQARsphLrjBYjQfxX8Eb8Kf zvEQHKeWJSWHUMo78eHwjfNdKWQPX7ksa4QDViRVEfskWLngQiTmfKu4ylWOWn1v3Can m//Mg3kCQ1TZzPR/BQrObOaftB5nSJvixqFxaHZAxckWqHav/+2PSRN5/V5YXr+RCo85 QylQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1-v6si11542074pld.744.2018.03.19.00.02.38; Mon, 19 Mar 2018 00:02:52 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755301AbeCSHAt (ORCPT + 99 others); Mon, 19 Mar 2018 03:00:49 -0400 Received: from bmailout3.hostsharing.net ([176.9.242.62]:45921 "EHLO bmailout3.hostsharing.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755118AbeCSHAp (ORCPT ); Mon, 19 Mar 2018 03:00:45 -0400 Received: from h08.hostsharing.net (h08.hostsharing.net [83.223.95.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.hostsharing.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (not verified)) by bmailout3.hostsharing.net (Postfix) with ESMTPS id 8DC2B100D941E; Mon, 19 Mar 2018 08:00:43 +0100 (CET) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 210B64A9D; Mon, 19 Mar 2018 08:00:43 +0100 (CET) Date: Mon, 19 Mar 2018 08:00:43 +0100 From: Lukas Wunner To: 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 Subject: Re: [PATCH 1/4] gpio: Remove VLA from gpiolib Message-ID: <20180319070043.GA25474@wunner.de> References: <20180310001021.6437-1-labbott@redhat.com> <20180310001021.6437-2-labbott@redhat.com> <20180317082509.GA2579@wunner.de> <20180318142327.GA23761@wunner.de> <0f17eb05-c183-bec9-0076-5ddd00d70f15@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0f17eb05-c183-bec9-0076-5ddd00d70f15@rasmusvillemoes.dk> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 18, 2018 at 09:34:12PM +0100, Rasmus Villemoes wrote: > 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 (?). The number 256 was arbitrarily chosen. I really wouldn't be surprised if gpiochips with more pins exist, but they're probably rare, so using the slowpath seems fine, but dropping support for them completely would be a regression. E.g. many serially attached chips such as MAX3191X are daisy-chainable and the driver deliberately doesn't impose an upper limit on the number of chips because the spec doesn't contain one either. To the OS a daisy-chain of such chips appears as a single gpiochip with many pins. Thanks, Lukas