linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Syed Nayyar Waris <syednwaris@gmail.com>
To: Arnd Bergmann <arnd@kernel.org>
Cc: William Breathitt Gray <vilhelm.gray@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Michal Simek <michal.simek@xilinx.com>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Linux ARM <linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH v12 4/4] gpio: xilinx: Utilize generic bitmap_get_value and _set_value
Date: Mon, 9 Nov 2020 22:15:29 +0530	[thread overview]
Message-ID: <20201109164529.GA28710@syed.domain.name> (raw)
In-Reply-To: <CAK8P3a2FMkMc0K+hu0pnqC8wEMeapKPkZXaBm+HFYYPTes5NHA@mail.gmail.com>

On Mon, Nov 09, 2020 at 03:41:53PM +0100, Arnd Bergmann wrote:
> On Mon, Nov 9, 2020 at 2:41 PM William Breathitt Gray
> <vilhelm.gray@gmail.com> wrote:
> > On Mon, Nov 09, 2020 at 06:04:11PM +0530, Syed Nayyar Waris wrote:
> >
> > One of my concerns is that we're incurring the latency two additional
> > conditional checks just to suppress a compiler warning about a case that
> > wouldn't occur in the actual use of bitmap_set_value(). I'm hoping
> > there's a way for us to suppress these warnings without adding onto the
> > latency of this function; given that bitmap_set_value() is intended to
> > be used in loops, conditionals here could significantly increase latency
> > in drivers.
> 
> At least for this caller, the size check would be a compile-time
> constant that can be eliminated.
> 
> > I wonder if array_index_nospec() might have the side effect of
> > suppressing these warnings for us. For example, would this work:
> >
> > static inline void bitmap_set_value(unsigned long *map,
> >                                     unsigned long value,
> >                                     unsigned long start, unsigned long nbits)
> > {
> >         const unsigned long offset = start % BITS_PER_LONG;
> >         const unsigned long ceiling = round_up(start + 1, BITS_PER_LONG);
> >         const unsigned long space = ceiling - start;
> >         size_t index = BIT_WORD(start);
> >
> >         value &= GENMASK(nbits - 1, 0);
> >
> >         if (space >= nbits) {
> >                 index = array_index_nospec(index, index + 1);
> >
> >                 map[index] &= ~(GENMASK(nbits - 1, 0) << offset);
> >                 map[index] |= value << offset;
> >         } else {
> >                 index = array_index_nospec(index, index + 2);
> >
> >                 map[index + 0] &= ~BITMAP_FIRST_WORD_MASK(start);
> >                 map[index + 0] |= value << offset;
> >                 map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits);
> >                 map[index + 1] |= value >> space;
> >         }
> > }
> >
> > Or is this going to produce the same warning because we're not using an
> > explicit check against the map array size?
> 
> https://godbolt.org/z/fxnsG9
> 
> It still warns about the 'map[index + 1]' access: from all I can tell,
> gcc mainly complains because it cannot rule out that 'space < nbits',
> and then it knows the size of 'DECLARE_BITMAP(old, 64)' and finds
> that if 'index + 0' is correct, then 'index + 1' overflows that array.
> 
>       Arnd

Hi Arnd,

As suggested by William, sharing another solution to suppress the 
compiler warning. Please let me know your views on the below fix. Thanks.

If its alright, I shall submit a (new) v13 patchset soon. Let me know.

@@ -1,5 +1,5 @@
 static inline void bitmap_set_value(unsigned long *map,
-                                    unsigned long value,
+                                    unsigned long value, const size_t length,
                                     unsigned long start, unsigned long nbits)
 {
         const size_t index = BIT_WORD(start);
@@ -15,6 +15,10 @@ static inline void bitmap_set_value(unsigned long *map,
         } else {
                 map[index + 0] &= ~BITMAP_FIRST_WORD_MASK(start);
                 map[index + 0] |= value << offset;
+
+               if (index + 1 >= length)
+                       __builtin_unreachable();
+
                 map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits);
                 map[index + 1] |= value >> space;
         }



  reply	other threads:[~2020-11-09 16:45 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-18 21:37 [PATCH v12 0/4] Introduce the for_each_set_clump macro Syed Nayyar Waris
2020-10-18 21:40 ` [PATCH v12 3/4] gpio: thunderx: Utilize " Syed Nayyar Waris
2020-11-05  9:10   ` Linus Walleij
2020-10-18 21:41 ` [PATCH v12 4/4] gpio: xilinx: Utilize generic bitmap_get_value and _set_value Syed Nayyar Waris
2020-10-29 22:44   ` Arnd Bergmann
2020-11-01 15:00     ` William Breathitt Gray
2020-11-01 20:08       ` Arnd Bergmann
2020-11-09 12:34         ` Syed Nayyar Waris
2020-11-09 13:13           ` Arnd Bergmann
2020-11-09 13:41           ` William Breathitt Gray
2020-11-09 14:38             ` William Breathitt Gray
2020-11-09 14:48               ` Syed Nayyar Waris
2020-11-09 15:18                 ` William Breathitt Gray
2020-11-09 14:41             ` Arnd Bergmann
2020-11-09 16:45               ` Syed Nayyar Waris [this message]
2020-11-09 17:11                 ` William Breathitt Gray
2020-11-09 17:22                   ` Andy Shevchenko
2020-11-09 17:31                     ` William Breathitt Gray
2020-11-10 10:02                       ` Michal Simek
2020-11-10 12:35                         ` William Breathitt Gray
2020-11-10 17:22                           ` Syed Nayyar Waris
2020-11-10 17:43                             ` William Breathitt Gray
2020-11-10 22:00                               ` Syed Nayyar Waris
2020-11-13 16:52                                 ` Syed Nayyar Waris
2020-11-20 13:26                                   ` Arnd Bergmann
2020-11-20 13:45                                     ` William Breathitt Gray

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201109164529.GA28710@syed.domain.name \
    --to=syednwaris@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@kernel.org \
    --cc=bgolaszewski@baylibre.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.simek@xilinx.com \
    --cc=vilhelm.gray@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).