Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/bitmap: drop optimization of bitmap_{from,to}_arr64

bitmap_{from,to}_arr64() optimization is overly optimistic on 32-bit LE
architectures when it's wired to bitmap_copy_clear_tail().

bitmap_copy_clear_tail() takes care of unused bits in the bitmap up to
the next word boundary. But on 32-bit machines when copying bits from
bitmap to array of 64-bit words, it's expected that the unused part of
a recipient array must be cleared up to 64-bit boundary, so the last 4
bytes may stay untouched when nbits % 64 <= 32.

While the copying part of the optimization works correct, that clear-tail
trick makes corresponding tests reasonably fail:

test_bitmap: bitmap_to_arr64(nbits == 1): tail is not safely cleared: 0xa5a5a5a500000001 (must be 0x0000000000000001)

Fix it by removing bitmap_{from,to}_arr64() optimization for 32-bit LE
arches.

Reported-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/lkml/20230225184702.GA3587246@roeck-us.net/
Fixes: 0a97953fd221 ("lib: add bitmap_{from,to}_arr64")
Signed-off-by: Yury Norov <yury.norov@gmail.com>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Alexander Lobakin <aleksander.lobakin@intel.com>

+4 -6
+3 -5
include/linux/bitmap.h
··· 302 302 #endif 303 303 304 304 /* 305 - * On 64-bit systems bitmaps are represented as u64 arrays internally. On LE32 306 - * machines the order of hi and lo parts of numbers match the bitmap structure. 307 - * In both cases conversion is not needed when copying data from/to arrays of 308 - * u64. 305 + * On 64-bit systems bitmaps are represented as u64 arrays internally. So, 306 + * the conversion is not needed when copying data from/to arrays of u64. 309 307 */ 310 - #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) 308 + #if BITS_PER_LONG == 32 311 309 void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits); 312 310 void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits); 313 311 #else
+1 -1
lib/bitmap.c
··· 1495 1495 EXPORT_SYMBOL(bitmap_to_arr32); 1496 1496 #endif 1497 1497 1498 - #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) 1498 + #if BITS_PER_LONG == 32 1499 1499 /** 1500 1500 * bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap 1501 1501 * @bitmap: array of unsigned longs, the destination bitmap