[PATCH] bitmap: suppress false-positive -Warray-bounds in bitmap_{read,write}

From: Yury Norov
Date: Tue Nov 07 2023 - 08:05:17 EST


bitmap_{read,write} conditionally accesses map[index + 1], carefully
checking before that it's a safe dereference. But still, gcc-9 emits
-Warray-bounds.

Gcc-12 and gcc-13 are both OK with this code. So fix it for gcc-9 with
OPTIMIZER_HIDE_VAR().

Reported-by: Alexander Lobakin <aleksander.lobakin@xxxxxxxxx>
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Closes: https://lore.kernel.org/oe-kbuild-all/202310170708.fJzLlgDM-lkp@xxxxxxxxx/
Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
include/linux/bitmap.h | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 945a680816cc..0b1a07ff1080 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -592,6 +592,11 @@ static inline unsigned long bitmap_read(const unsigned long *map,
if (unlikely(!nbits || nbits > BITS_PER_LONG))
return 0;

+#if CONFIG_GCC_VERSION < 100000
+ /* Suppress gcc-9 false-positive -Warray-bounds */
+ OPTIMIZER_HIDE_VAR(map);
+#endif
+
if (space >= nbits)
return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);

@@ -634,6 +639,11 @@ static inline void bitmap_write(unsigned long *map, unsigned long value,
fit = space >= nbits;
index = BIT_WORD(start);

+#if CONFIG_GCC_VERSION < 100000
+ /* Suppress gcc-9 false-positive -Warray-bounds */
+ OPTIMIZER_HIDE_VAR(map);
+#endif
+
map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
map[index] |= value << offset;
if (fit)
--
2.39.2