From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Yury Norov <ynorov@nvidia.com>
Cc: linux-kernel@vger.kernel.org, Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Jonathan Cameron <jic23@kernel.org>,
Chris Li <sparse@chrisli.org>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>,
linux-sparse@vger.kernel.org
Subject: Re: [PATCH v1 1/1] bitmap: Refactor bitmap_read() and bitmap_write() to avoid too big shift
Date: Thu, 19 Nov 2026 23:03:53 +0200 [thread overview]
Message-ID: <av9kOeZtIjOtH2yp@ashevche-desk.local> (raw)
In-Reply-To: <av9eAde6AKlAKWaH@yury>
On Thu, Nov 19, 2026 at 03:37:33PM -0500, Yury Norov wrote:
> On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:
Thanks for review, my answers below.
> > While compiling ad4691.c I have noticed the following warnings:
> >
> > drivers/iio/adc/ad4691.c: note: in included file:
> > include/linux/bitmap.h:854:55: warning: shift too big (64) for type unsigned long
> > include/linux/bitmap.h:854:55: warning: shift too big (64) for type unsigned long
> >
> > Since the bitmap_read() is called with statically define 'start',
> > the 'space' local variable becomes equal to BITS_PER_LONG, or 64 on
> > 64-bit machines. The 'nbits' parameter is coming from the run-time
> > defined value. Static analyser doesn't see that the right shift
> > by 64-bit can't happen — the space >= nbits check guards that.
> > While Jonathan put it as false positive [1], we still can give a hint
> > to it without growing in the code (`bloat-o-meter` for above mentioned
> > driver shows no difference in size). So this is what this change does.
>
> Jonathan put it right.
>
> If the analyzer (assuming sparse?) throws false positives, we don't
> need to change the kernel. This is simply a wrong motivation. We need
> to improve sparse instead.
>
> Otherwise, the kernel will become a collection of hacks for various
> poorly written tools. Let's do it the other way, please.
I agree in general. However, we can sometimes give hints without breaking the code.
Now, thinking of this, the mentioned check can be done against offset leaving code
generation and sparse happy and have no unneeded bulk.
> > For completeness, bitmap_write() is also refactored in the similar manner.
> > I have also checked a few users of it and `bloat-o-meter` showed no change.
...
> > - if (space >= nbits)
> > + if (space >= nbits || offset == 0)
> > return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
>
> So, doubling the number of conditions, and the new one duplicates the
> existing check. This version suppresses sparse false positive:
>
> if (offset <= BITS_PER_LONG - nbits)
Yes, you beat me up to it. I just was thinking about similar check in the above
comment.
> And even with that I'm not sure we need to touch the kernel to make
> sparse or whatever else tool happy...
It might take ages for sparse to fix even more annoying issues (if you remember
something related to cleanup or alike change in the kernel and sparse was
lagging for weeks).
...
> > size_t index;
> > - unsigned long offset;
> > - unsigned long space;
> > - unsigned long mask;
> > - bool fit;
> > + unsigned long offset = start % BITS_PER_LONG;
> > + unsigned long space = BITS_PER_LONG - offset;
> >
> > if (unlikely(!nbits || nbits > BITS_PER_LONG))
> > return;
>
> Initialization of local variables before the sanity check
> may become useless waste of cycles if this sanity fails.
How? they are not used and function is marked as always inline. I believe
optimisation in the compiler does the right thing.
> This is also not necessarily needed for sparse happiness,
This is to make both look similar at a glance.
> I guess... Let's keep the patch minimal?
>
> > - mask = BITMAP_LAST_WORD_MASK(nbits);
> > - value &= mask;
> > - offset = start % BITS_PER_LONG;
> > - space = BITS_PER_LONG - offset;
> > - fit = space >= nbits;
>
> I don't like this 'fit' thing, and would like to drop it. But
> let's find a better motivation to do that?
Why? The fit keeps the check I want to refactor, since it makes it twice
the code with that ternary looks ugly and unreadable. This is exactly the
chance to kill two birds with one stone.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-11-19 21:03 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 19:15 Andy Shevchenko
2026-11-19 20:37 ` Yury Norov
2026-11-19 21:03 ` Andy Shevchenko [this message]
2026-11-19 21:22 ` Yury Norov
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=av9kOeZtIjOtH2yp@ashevche-desk.local \
--to=andriy.shevchenko@linux.intel.com \
--cc=jic23@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=luc.vanoostenryck@gmail.com \
--cc=sparse@chrisli.org \
--cc=ynorov@nvidia.com \
--cc=yury.norov@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
all inboxes | Powered by JetHome®