All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Yury Norov <ynorov@nvidia.com>, linux-kernel@vger.kernel.org
Cc: Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Jonathan Cameron <jic23@kernel.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: [PATCH v1 1/1] bitmap: Refactor bitmap_read() and bitmap_write() to avoid too big shift
Date: Thu, 19 Nov 2026 20:15:30 +0100	[thread overview]
Message-ID: <20261119191530.1716850-1-andriy.shevchenko@linux.intel.com> (raw)

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.

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.

Link: https://lore.kernel.org/r/20260531111528.32697d1f@jic23-huawei [1]
Fixes: 63c15822b8dd ("lib/bitmap: add bitmap_{read,write}()")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/bitmap.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index adafbcf2016b..f331cf06130d 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -846,7 +846,7 @@ unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return 0;
 
-	if (space >= nbits)
+	if (space >= nbits || offset == 0)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
 
 	value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
@@ -874,25 +874,23 @@ void bitmap_write(unsigned long *map, unsigned long value,
 		  unsigned long start, unsigned long nbits)
 {
 	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;
 
-	mask = BITMAP_LAST_WORD_MASK(nbits);
-	value &= mask;
-	offset = start % BITS_PER_LONG;
-	space = BITS_PER_LONG - offset;
-	fit = space >= nbits;
+	value &= BITMAP_LAST_WORD_MASK(nbits);
 	index = BIT_WORD(start);
 
-	map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
-	map[index] |= value << offset;
-	if (fit)
+	if (space >= nbits || offset == 0) {
+		map[index] &= ~(BITMAP_LAST_WORD_MASK(nbits) << offset);
+		map[index] |= value << offset;
 		return;
+	}
+
+	map[index] &= ~BITMAP_FIRST_WORD_MASK(start);
+	map[index] |= value << offset;
 
 	map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
 	map[index + 1] |= (value >> space);
-- 
2.50.1


             reply	other threads:[~2026-11-19 19:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:15 Andy Shevchenko [this message]
2026-11-19 20:37 ` [PATCH v1 1/1] bitmap: Refactor bitmap_read() and bitmap_write() to avoid too big shift Yury Norov
2026-11-19 21:03   ` Andy Shevchenko
2026-11-19 21:22     ` Yury Norov
2026-11-20  8:42       ` Andy Shevchenko
2026-11-20  8:51         ` Andy Shevchenko
2026-11-20 14:42         ` 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=20261119191530.1716850-1-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=jic23@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --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 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.