All of lore.kernel.org
 help / color / mirror / Atom feed
From: William Breathitt Gray <vilhelm.gray@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Syed Nayyar Waris <syednwaris@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Linux-Arch <linux-arch@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v10 1/4] bitops: Introduce the for_each_set_clump macro
Date: Sat, 3 Oct 2020 08:56:26 -0400	[thread overview]
Message-ID: <20201003125626.GA3732@shinobu> (raw)
In-Reply-To: <CAHp75VdC+eH0ScksdAVp==HnDMTMY3vVUZM5NZy6mfVSR0YoLA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3897 bytes --]

On Sat, Oct 03, 2020 at 03:45:04PM +0300, Andy Shevchenko wrote:
> On Sat, Oct 3, 2020 at 2:37 PM Syed Nayyar Waris <syednwaris@gmail.com> wrote:
> > On Sat, Oct 3, 2020 at 2:14 PM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:
> > > On Sat, Oct 3, 2020 at 2:51 AM Syed Nayyar Waris <syednwaris@gmail.com> wrote:
> 
> ...
> 
> > > > +/**
> > > > + * bitmap_get_value - get a value of n-bits from the memory region
> > > > + * @map: address to the bitmap memory region
> > > > + * @start: bit offset of the n-bit value
> > > > + * @nbits: size of value in bits
> > > > + *
> > > > + * Returns value of nbits located at the @start bit offset within the @map
> > > > + * memory region.
> > > > + */
> 
> ...
> 
> > > > +               return (map[index] >> offset) & GENMASK(nbits - 1, 0);
> > >
> > > This is UB in GENMASK() when nbits == 0.
> >
> > 'nbits' actually specifies the width of clump value. Basically 'nbits'
> > denotes how-many-bits wide the clump value is.
> > 'nbits' having a value of '0' means zero-width-sized clump, meaning
> > nothing. 'nbits' can take valid values from '1' to BITS_PER_LONG.
> > The minimum value the 'nbits' can have is 1 because the smallest sized
> > clump can be 1-bit-wide. It can't be smaller than that.
> >
> > Let me know if I have misunderstood something?
> 
> It's still possible to call with an nbits parameter be equal to 0.
> If code is optimized to allow it, it should be documented that 0
> parameter is not valid and behaviour is undefined.

Documenting that 0 is not valid would be preferred because an additional
conditional check in the code could add a significant latency in a loop.
So perhaps change the documentation line to:

    @nbits: size of value in bits (must be between 1 and BITS_PER_LONG)

> 
> ...
> 
> > > > +/**
> > > > + * bitmap_set_value - set n-bit value within a memory region
> > > > + * @map: address to the bitmap memory region
> > > > + * @value: value of nbits
> > > > + * @start: bit offset of the n-bit value
> > > > + * @nbits: size of value in bits
> > > > + */
> 
> ...
> 
> > > > +       value &= GENMASK(nbits - 1, 0);
> > >
> > > This is UB when nbits == 0.
> >
> > Same as above.
> > 'nbits' actually specifies the width of clump value. Basically 'nbits'
> > denotes how-many-bits wide the clump value is.
> > 'nbits' having a value of '0' means zero-width-sized clump, meaning
> > nothing. 'nbits' can take valid values from '1' to BITS_PER_LONG.
> > The minimum value the 'nbits' can have is 1 because the smallest sized
> > clump can be 1-bit-wide. It can't be smaller than that.
> 
> Same as above.
> 
> ...
> 
> > > > +               map[index] &= ~BITMAP_FIRST_WORD_MASK(start);
> > > > +               map[index] |= value << offset;
> 
> Side note: I would prefer + 0 here and there, but it's up to you.
> 
> > > > +               map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits);
> > > > +               map[index + 1] |= (value >> space);
> 
> By the way, what about this in the case of start=0, nbits > 64?
> space == 64 -> UB.
> 
> (And btw parentheses are redundant here)

I think this is the same situation as before: we should document that
nbits must be between 1 and BITS_PER_LONG.

William Breathitt Gray

> 
> > > And another LKP finding was among these lines, but I don't remember the details.
> >
> > Yes you are right. There was sparse warning reported for this.
> > sparse: shift too big (64) for type unsigned long
> > The warning was reported in patch [4/4] referring to this patch [1/4].
> >
> > Later it was clarified by the sparse-check maintainer that this
> > warning is to be ignored and no code fix is required.
> >
> > https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2202377.html
> 
> Ah, okay!
> --
> With Best Regards,
> Andy Shevchenko

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2020-10-03 12:56 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-02 23:47 [PATCH v10 0/4] Introduce the for_each_set_clump macro Syed Nayyar Waris
2020-10-02 23:47 ` Syed Nayyar Waris
2020-10-02 23:48 ` [PATCH v10 1/4] bitops: " Syed Nayyar Waris
2020-10-03  8:44   ` Andy Shevchenko
2020-10-03 11:36     ` Syed Nayyar Waris
2020-10-03 12:45       ` Andy Shevchenko
2020-10-03 12:56         ` William Breathitt Gray [this message]
2020-10-03 13:02           ` Andy Shevchenko
2020-10-03 15:08             ` Syed Nayyar Waris
2020-10-05  9:35               ` Andy Shevchenko
2020-10-02 23:49 ` [PATCH v10 2/4] lib/test_bitmap.c: Add for_each_set_clump test cases Syed Nayyar Waris
2020-10-02 23:51 ` [PATCH v10 3/4] gpio: thunderx: Utilize for_each_set_clump macro Syed Nayyar Waris
2020-10-02 23:52 ` [PATCH v10 4/4] gpio: xilinx: Utilize generic bitmap_get_value and _set_value Syed Nayyar Waris
2020-10-02 23:52   ` Syed Nayyar Waris
2020-10-09 13:36   ` kernel test robot

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=20201003125626.GA3732@shinobu \
    --to=vilhelm.gray@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=arnd@arndb.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syednwaris@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.