From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 596A5C4741F for ; Sun, 1 Nov 2020 15:04:53 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ED5BC22242 for ; Sun, 1 Nov 2020 15:04:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="FvJgoRby"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="rV6TqHjD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED5BC22242 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type:Cc: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: In-Reply-To:MIME-Version:References:Message-ID:Subject:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=KNHAaQBzNI1J1vA5pwohCX9clCGZsIQCvaH9rkslrEg=; b=FvJgoRbyohyILWmMu/ZFn9mLf zZ+nwAi1rEDSUTJGO+sUYttqzXKafCEaeTQBVRuqp9exiFP1hjwJaw3SFI9/NYa6SXEu1juuaOs2G ShJRUjOemXy3KWidtwm/IVyYZfmwCqHMWS7QrS3D1OhQn0ByJoPJ4FmocMuIWRUELCWkXSbZEdJOc ccONxv8Y+gpDYYAW9NMq6rYnZbQRol5T1OpPvbtjYANpndFgRvxZknlWNkgb4FqqbueLV0CcnIsuX 4/KOh+2YlQimfuHE8aZcnX5P58t1ABfRHQjwPtXlQ4sqNdWG+pBAxR8ciPuET0gxA9VX/TMOVRt0b mstGTL6PA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kZEqo-0001Qx-38; Sun, 01 Nov 2020 15:00:58 +0000 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kZEql-0001Q2-IP for linux-arm-kernel@lists.infradead.org; Sun, 01 Nov 2020 15:00:56 +0000 Received: by mail-qk1-x743.google.com with SMTP id l2so9392166qkf.0 for ; Sun, 01 Nov 2020 07:00:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=b4nU2ptCkmP86QnZPWal726t+pg4pGhqWLMU1nytF/A=; b=rV6TqHjDTjhCkkx80iGKbfmPKYBFQKG09o9c8oK4TQaTKBWTLqMa/rksRh81F6Mb2k T0ezUv0Lx3UJQoyAlxXwQtUJTN6BFagZXMV05ObjclmrcgqluDOqV8W4mvdKlK46WKwy FFHREWP6W21CQjwgjjTpL9GazujB5i0RhLbRQOvNikUctIlo9CcfOds9Huk+TJi/YiF5 O3TCJqpXDdfvKAngbmA9DoDslXCZLGOHLBaXKbeNqlGGZUf3V3M8GWEXcdgt0OOTpFgF Sa/Sqyuen6mKgjJ4k0FiQ72gkXP1G5KEzRSWCYsWmiUZZOt9gMZ/pEdEuQw27AFTjbT7 3urw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=b4nU2ptCkmP86QnZPWal726t+pg4pGhqWLMU1nytF/A=; b=Ic9Q3mM1JAyR88Y0/KlOIy5uNTpLddiLb8I/oOi36GC7/1iQBBWDPSZvDFLm7SGMkL KhT3eOYbXH7/cNQOTI/Yhqt8LFQHyZwuhl1VF4Xbq6LcIs3b6E56LOBmhnvFf1AIceOe j4013G67nZH5d95yt/p6cXc4RnQxOwnTTlQqhh1Mmho5aqFNpbkstuA3UBQ5WvPWSsBB 1uRYZmL4ayTjouMVUUdRQUOgb2RqoRVdbeTvUFwRV0aQ54vJeJ0SU2Bx5ONxDvHudkUf JnXo79bjOLJmWrvbvwfh81ETcMItV3eiPbgcvxSuVnNeYH0jpxssGlFc38CJ4MJi1lK7 i6NA== X-Gm-Message-State: AOAM533ILCwdbSEkyBZDSXbPM5LtNMaaIsTD2gFSq+snIaJFkXzXKYPX ZVNfsYsWwTr1WUZQtWg4IDH2amMn9uNqCw== X-Google-Smtp-Source: ABdhPJzS1QDaqEfdVNwkvlSTnNVg+nyjfQLFsrmz1oDPeduOHNx3+rs+vL2wNUoLuTjdTHgIR1lUOQ== X-Received: by 2002:a37:4ccf:: with SMTP id z198mr11480950qka.348.1604242852347; Sun, 01 Nov 2020 07:00:52 -0800 (PST) Received: from shinobu (072-189-064-225.res.spectrum.com. [72.189.64.225]) by smtp.gmail.com with ESMTPSA id f1sm6029259qto.18.2020.11.01.07.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 07:00:51 -0800 (PST) Date: Sun, 1 Nov 2020 10:00:33 -0500 From: William Breathitt Gray To: Arnd Bergmann Subject: Re: [PATCH v12 4/4] gpio: xilinx: Utilize generic bitmap_get_value and _set_value Message-ID: <20201101150033.GA68138@shinobu> References: <15a044d3ba23f00c31fd09437bdd3e5924bb91cd.1603055402.git.syednwaris@gmail.com> MIME-Version: 1.0 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201101_100055_656280_C0E5BFE1 X-CRM114-Status: GOOD ( 32.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bartosz Golaszewski , Linus Walleij , Michal Simek , "linux-kernel@vger.kernel.org" , "open list:GPIO SUBSYSTEM" , Andrew Morton , Andy Shevchenko , Syed Nayyar Waris , Linux ARM Content-Type: multipart/mixed; boundary="===============0999797791237769424==" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org --===============0999797791237769424== Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="LZvS9be/3tNcYl/X" Content-Disposition: inline --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 29, 2020 at 11:44:47PM +0100, Arnd Bergmann wrote: > On Sun, Oct 18, 2020 at 11:44 PM Syed Nayyar Waris = wrote: > > > > This patch reimplements the xgpio_set_multiple() function in > > drivers/gpio/gpio-xilinx.c to use the new generic functions: > > bitmap_get_value() and bitmap_set_value(). The code is now simpler > > to read and understand. Moreover, instead of looping for each bit > > in xgpio_set_multiple() function, now we can check each channel at > > a time and save cycles. >=20 > This now causes -Wtype-limits warnings in linux-next with gcc-10: Hi Arnd, What version of gcc-10 are you running? I'm having trouble generating these warnings so I suspect I'm using a different version than you. Regardless I can see your concern about the code, and I think I have a solution. >=20 > > + u32 *const state =3D chip->gpio_state; > > + unsigned int *const width =3D chip->gpio_width; > > + > > + DECLARE_BITMAP(old, 64); > > + DECLARE_BITMAP(new, 64); > > + DECLARE_BITMAP(changed, 64); > > + > > + spin_lock_irqsave(&chip->gpio_lock[0], flags); > > + spin_lock(&chip->gpio_lock[1]); > > + > > + bitmap_set_value(old, state[0], 0, width[0]); > > + bitmap_set_value(old, state[1], width[0], width[1]); >=20 > In file included from ../include/linux/cpumask.h:12, > from ../arch/x86/include/asm/cpumask.h:5, > from ../arch/x86/include/asm/msr.h:11, > from ../arch/x86/include/asm/processor.h:22, > from ../arch/x86/include/asm/timex.h:5, > from ../include/linux/timex.h:65, > from ../include/linux/time32.h:13, > from ../include/linux/time.h:73, > from ../include/linux/stat.h:19, > from ../include/linux/module.h:13, > from ../drivers/gpio/gpio-xilinx.c:11: > ../include/linux/bitmap.h:639:18: warning: array subscript [1, > 67108864] is outside array bounds of 'long unsigned int[1]' > [-Warray-bounds] > 639 | map[index + 1] |=3D value >> space; > | ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ > In file included from ../include/linux/kasan-checks.h:5, > from ../include/asm-generic/rwonce.h:26, > from ./arch/x86/include/generated/asm/rwonce.h:1, > from ../include/linux/compiler.h:246, > from ../include/linux/build_bug.h:5, > from ../include/linux/bits.h:22, > from ../include/linux/bitops.h:6, > from ../drivers/gpio/gpio-xilinx.c:8: > ../drivers/gpio/gpio-xilinx.c:144:17: note: while referencing 'old' > 144 | DECLARE_BITMAP(old, 64); > | ^~~ > ../include/linux/types.h:11:16: note: in definition of macro 'DECLARE_BIT= MAP' > 11 | unsigned long name[BITS_TO_LONGS(bits)] > | ^~~~ > In file included from ../include/linux/cpumask.h:12, > from ../arch/x86/include/asm/cpumask.h:5, > from ../arch/x86/include/asm/msr.h:11, > from ../arch/x86/include/asm/processor.h:22, > from ../arch/x86/include/asm/timex.h:5, > from ../include/linux/timex.h:65, > from ../include/linux/time32.h:13, > from ../include/linux/time.h:73, > from ../include/linux/stat.h:19, > from ../include/linux/module.h:13, > from ../drivers/gpio/gpio-xilinx.c:11: >=20 > The compiler clearly tries to do range-checking here and notices > that the index into the fixed-length array on the stack is not correctly > bounded. It seems this would happen whenever width[0] + width[1] > is larger than 64. >=20 > I have just submitted patches for all other -Wtype-limits warnings > and would like to enable this option by default. Can you try to find > a way to make this code safer? I would expect that you need a > variant of bitmap_set_value() that takes an explicit ceiling here, > and checks the stand and nbits values against that. >=20 > Arnd Let me first verify that I understand the problem correctly. The issue is the possibility of a stack smash in bitmap_set_value() when the value of start + nbits is larger than the length of the map bitmap memory region. This is because index (or index + 1) could be outside the range of the bitmap memory region passed in as map. Is my understanding correct here? In xgpio_set_multiple(), the variables width[0] and width[1] serve as possible start and nbits values for the bitmap_set_value() calls. Because width[0] and width[1] are unsigned int variables, GCC considers the possibility that the value of width[0]/width[1] might exceed the length of the bitmap memory region named old and thus result in a stack smash. I don't know if invalid width values are actually possible for the Xilinx gpio device, but let's err on the side of safety and assume this is actually a possibility. We should verify that the combined value of gpio_width[0] + gpio_width[1] does not exceed 64 bits; we can add a check for this in xgpio_probe() when we grab the gpio_width values. However, we're still left with the GCC warnings because GCC is not smart enough to know that we've already checked the boundary and width[0] and width[1] are valid values. I suspect we can avoid this warning is we refactor bitmap_set_value() to increment map seperately and then set it: static inline void bitmap_set_value(unsigned long *map, unsigned long value, unsigned long start, unsigned long nbits) { const unsigned long offset =3D start % BITS_PER_LONG; const unsigned long ceiling =3D round_up(start + 1, BITS_PER_LONG); const unsigned long space =3D ceiling - start; map +=3D BIT_WORD(start); value &=3D GENMASK(nbits - 1, 0); if (space >=3D nbits) { *map &=3D ~(GENMASK(nbits - 1, 0) << offset); *map |=3D value << offset; } else { *map &=3D ~BITMAP_FIRST_WORD_MASK(start); *map |=3D value << offset; map++; *map &=3D ~BITMAP_LAST_WORD_MASK(start + nbits); *map |=3D value >> space; } } This avoids adding a costly conditional check inside bitmap_set_value() when almost all bitmap_set_value() calls will have static arguments with well-defined and obvious boundaries. Do you think this would be an acceptable solution to resolve your GCC warnings? Sincerely, William Breathitt Gray --LZvS9be/3tNcYl/X Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEk5I4PDJ2w1cDf/bghvpINdm7VJIFAl+ezYcACgkQhvpINdm7 VJKmPxAApWxF+K5dr7TywSmNmPJxv2Lk8pyWwosorrzcPhhJA2E2cc4byIUTStu1 57rgsfCWmL3d19tQBJZpiSTeLN4mM2opM2SkIZcKyA0giDgTHogpqA4MGWpLKPNe qroipHFC/gB8qUyb2/RA+N+oycAY/pS2GY/YsvtUQnwvTADPdwV7YJprzdAYISTl DU4kWveqNspIvHIwDGmL+ncqgiop3wxirv8Koqxxj/VeVpgUwaCy2MzhgG971uGl C8qh7oMOXNe1PBMx8+33C/G1GRSwzAZIZHu0v2EEUky0WtUq0A0YZ/rVRUBOpLRe 9HzEEk2LMh1CoC3fmlu9yky+0PgvoHKKWIDI0yX0FW7ttpD/P/05u1/LCxHx5EjE E6qZVyfHAYdMTZMT2I0gT8yJn7dci+XZv2uf923lm4BLvS6pNWAK5l0RrdT9PZCE 08KFLbPPt2RCCpwZViqPo+g2OUxrjEgbgz7wX+mdzoPSJpn/e/Bxcy+zOxTc7YK9 UrDStTDVxCRyE43/gX9TkkUg9NU3cwKgJpX6EaINEh15YMvu4Fk/7515WvtzYqfY rArtnDPkSH0dbbs6Dowm5EFiKFYXQ9J83vHTOhsX82KJT2RLy6QFzUDErl6ux+Z5 7o5oKEOUx8nPAU4NurVit0v/SKFHTWo11H6lYAyHzLwDNYeRNME= =oKZS -----END PGP SIGNATURE----- --LZvS9be/3tNcYl/X-- --===============0999797791237769424== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel --===============0999797791237769424==--