bitmap: Fix return values to be unsigned
Subsystems (2)
PATCH 1/1 bitmap: Fix return values to be unsigned
Both nodemask and bitmap routines had mixed return values that provided potentially signed results that could never happen. This was leading to the compiler getting confusing about the range of possible return values (it was thinking things could be negative where they could not be). Fix all the nodemask and bitmap routines that should be returning unsigned (or bool) values. Silences GCC 12 warnings:
mm/swapfile.c: In function ‘setup_swap_info’:
mm/swapfile.c:2291:47: error: array subscript -1 is below array bounds of ‘struct plist_node[]’ [-Werror=array-bounds]
2291 | p->avail_lists[i].prio = 1;
| ~~~~~~~~~~~~~~^~~
In file included from mm/swapfile.c:16:
./include/linux/swap.h:292:27: note: while referencing ‘avail_lists’
292 | struct plist_node avail_lists[]; /*
| ^~~~~~~~~~~--- include/linux/bitmap.h | 14 +++++++------- include/linux/nodemask.h | 42 ++++++++++++++++++++-------------------- lib/bitmap.c | 28 +++++++++++++-------------- lib/nodemask.c | 6 +++--- 4 files changed, 45 insertions(+), 45 deletions(-)
include/linux/bitmap.h +7 -7
@@ -132,8 +132,8 @@ unsigned long *devm_bitmap_zalloc(struct device *dev,* lib/bitmap.c provides these functions: */ -int __bitmap_equal (const unsigned long *bitmap1, -const unsigned long *bitmap2,unsigned int nbits); +bool __bitmap_equal (const unsigned long *bitmap1, +const unsigned long *bitmap2,unsigned int nbits); bool __pure __bitmap_or_equal(const unsigned long *src1, const unsigned long *src2, const unsigned long *src3,
@@ -157,10 +157,10 @@ int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,void __bitmap_replace (unsigned long *dst,const unsigned long *old,const unsigned long *new,const unsigned long *mask,unsigned int nbits); -int __bitmap_intersects (const unsigned long *bitmap1, -const unsigned long *bitmap2,unsigned int nbits); -int __bitmap_subset (const unsigned long *bitmap1, -const unsigned long *bitmap2,unsigned int nbits); +bool __bitmap_intersects (const unsigned long *bitmap1, +const unsigned long *bitmap2,unsigned int nbits); +bool __bitmap_subset (const unsigned long *bitmap1, +const unsigned long *bitmap2,unsigned int nbits);int __bitmap_weight (const unsigned long *bitmap,unsigned int nbits);void __bitmap_set (unsigned long *map,unsigned int start,int len);void __bitmap_clear (unsigned long *map,unsigned int start,int len);
@@ -331,7 +331,7 @@ static inline void bitmap_complement(unsigned long *dst, const unsigned long *sr#endif #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1) -static inline int bitmap_equal (const unsigned long *src1, +static inline bool bitmap_equal (const unsigned long *src1,const unsigned long *src2,unsigned int nbits) {if (small_const_nbits (nbits))
include/linux/nodemask.h +21 -21
124 lines not on this page · raw message
lib/bitmap.c +14 -14
@@ -45,19 +45,19 @@* for the best explanations of this ordering. */ -int __bitmap_equal (const unsigned long *bitmap1, +bool __bitmap_equal (const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int bits) {unsigned int k, lim = bits/BITS_PER_LONG ;for (k =0 ; k < lim; ++k)if (bitmap1[k] != bitmap2[k]) -return 0 ; +return false;if (bits %BITS_PER_LONG )if ((bitmap1[k] ^ bitmap2[k]) &BITMAP_LAST_WORD_MASK (bits)) -return 0 ; +return false; -return 1 ; +return true; }EXPORT_SYMBOL (__bitmap_equal);
@@ -303,33 +303,33 @@ void __bitmap_replace(unsigned long *dst, }EXPORT_SYMBOL (__bitmap_replace); -int __bitmap_intersects (const unsigned long *bitmap1, -const unsigned long *bitmap2,unsigned int bits) +bool __bitmap_intersects (const unsigned long *bitmap1, +const unsigned long *bitmap2,unsigned int bits) {unsigned int k, lim = bits/BITS_PER_LONG ;for (k =0 ; k < lim; ++k)if (bitmap1[k] & bitmap2[k]) -return 1 ; +return true;if (bits %BITS_PER_LONG )if ((bitmap1[k] & bitmap2[k]) &BITMAP_LAST_WORD_MASK (bits)) -return 1 ; -return 0 ; +return true; +return false; }EXPORT_SYMBOL (__bitmap_intersects); -int __bitmap_subset (const unsigned long *bitmap1, -const unsigned long *bitmap2,unsigned int bits) +bool __bitmap_subset (const unsigned long *bitmap1, +const unsigned long *bitmap2,unsigned int bits) {unsigned int k, lim = bits/BITS_PER_LONG ;for (k =0 ; k < lim; ++k)if (bitmap1[k] & ~bitmap2[k]) -return 0 ; +return false;if (bits %BITS_PER_LONG )if ((bitmap1[k] & ~bitmap2[k]) &BITMAP_LAST_WORD_MASK (bits)) -return 0 ; -return 1 ; +return false; +return true; }EXPORT_SYMBOL (__bitmap_subset);
lib/nodemask.c +3 -3
@@ -3,7 +3,7 @@#include <linux/module.h> #include <linux/random.h> -int __next_node_in (int node,const nodemask_t *srcp) +unsigned int __next_node_in (int node,const nodemask_t *srcp) {int ret =__next_node (node, srcp);
@@ -18,9 +18,9 @@ EXPORT_SYMBOL(__next_node_in);* Return the bit number of a random bit set in the nodemask. * (returns NUMA_NO_NODE if nodemask is empty) */ -int node_random (const nodemask_t *maskp) +unsigned int node_random (const nodemask_t *maskp) { -int w, bit =NUMA_NO_NODE ; +unsigned int w, bit =NUMA_NO_NODE ; w =nodes_weight (*maskp);if (w)
Signature · 1 line
2.32.0
Rasmus Villemoes
reviewerQuoted 6 lines · Kees Cook
Both nodemask and bitmap routines had mixed return values that provided potentially signed results that could never happen. This was leading to the compiler getting confusing about the range of possible return values (it was thinking things could be negative where they could not be). Fix all the nodemask and bitmap routines that should be returning unsigned (or bool) values. Silences GCC 12 warnings:
So, for the bitmap functions themselves, makes sense, and then also for the nodemask functions which are merely wrappers around the bitmap functions (or wrappers around wrappers ...). But see below.
include/linux/nodemask.h static inline void __nodes_shift_left(nodemask_t *dstp, · line 266
Quoted 8 lines
#define
first_node(src)__first_node(&(src))include/linux/nodemask.h +2
-2-static inline int __first_node (const nodemask_t *srcp) +static inline unsigned int __first_node (const nodemask_t *srcp) { -return min_t (int,MAX_NUMNODES ,find_first_bit (srcp->bits,MAX_NUMNODES )); + return min_t(unsigned int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES)); }
Unrelated to the type change, but what's that min() doing there in the first place? Doesn't find_first_bit() already return the nbits argument if no "first bit" exists (i.e., the bitmap is empty)?
include/linux/nodemask.h static inline void __nodes_shift_left(nodemask_t *dstp, · line 272
Quoted 7 lines
#define
next_node(n, src)__next_node((n), &(src))include/linux/nodemask.h +2
-2-static inline int __next_node (int n,const nodemask_t *srcp) +static inline unsigned int __next_node (int n,const nodemask_t *srcp) { -return min_t (int,MAX_NUMNODES ,find_next_bit (srcp->bits,MAX_NUMNODES , n+1 )); + return min_t(unsigned int, MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1)); }
Same here and a few more places.
It seems to go all the way back to pre-git. Hm. Could be cleaned up separately I guess.
include/linux/nodemask.h static inline int num_node_state(enum node_states state) · line 498
Quoted 4 lines
#if
defined(CONFIG_NUMA)&& (MAX_NUMNODES> 1)include/linux/nodemask.h +1
-1-extern int node_random (const nodemask_t *maskp); +extern unsigned int node_random (const nodemask_t *maskp);
So this one I'm not convinced about. It has a documented return value of NUMA_NO_NODE aka -1 if the mask is empty. And since it's not a wrapper around a corresponding bitmap_random() (which would presumably, did it exist, use the "return nbits if empty" convention), there's no compelling reason to make its return type unsigned.
lib/nodemask.c EXPORT_SYMBOL(__next_node_in); · line 26
Quoted 13 lines
lib/nodemask.c +2
-2@@ -18,9 +18,9 @@ EXPORT_SYMBOL(__next_node_in);* Return the bit number of a random bit set in the nodemask. * (returns NUMA_NO_NODE if nodemask is empty) */ -int node_random (const nodemask_t *maskp) +unsigned int node_random (const nodemask_t *maskp) { -int w, bit =NUMA_NO_NODE ; +unsigned int w, bit =NUMA_NO_NODE ; w =nodes_weight (*maskp);if (w)
Rasmus
Quoted 11 lines · Rasmus Villemoes
Quoted 6 lines · Kees Cook
Both nodemask and bitmap routines had mixed return values that provided potentially signed results that could never happen. This was leading to the compiler getting confusing about the range of possible return values (it was thinking things could be negative where they could not be). Fix all the nodemask and bitmap routines that should be returning unsigned (or bool) values. Silences GCC 12 warnings:
So, for the bitmap functions themselves, makes sense, and then also for the nodemask functions which are merely wrappers around the bitmap functions (or wrappers around wrappers ...). But see below.
Cool. I think I should split this into two patches.
Quoted 24 lines
include/linux/nodemask.h static inline void __nodes_shift_left(nodemask_t *dstp, · line 266
Quoted 9 lines
#define
first_node(src)__first_node(&(src))include/linux/nodemask.h +2
-2-static inline int __first_node (const nodemask_t *srcp) +static inline unsigned int __first_node (const nodemask_t *srcp) { -return min_t (int,MAX_NUMNODES ,find_first_bit (srcp->bits,MAX_NUMNODES )); + return min_t(unsigned int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES)); }Unrelated to the type change, but what's that
min()doing there in the first place? Doesn'tfind_first_bit()already return the nbits argument if no "first bit" exists (i.e., the bitmap is empty)?include/linux/nodemask.h static inline void __nodes_shift_left(nodemask_t *dstp, · line 272
Quoted 7 lines
#define
next_node(n, src)__next_node((n), &(src))include/linux/nodemask.h +2
-2-static inline int __next_node (int n,const nodemask_t *srcp) +static inline unsigned int __next_node (int n,const nodemask_t *srcp) { -return min_t (int,MAX_NUMNODES ,find_next_bit (srcp->bits,MAX_NUMNODES , n+1 )); + return min_t(unsigned int, MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1)); }Same here and a few more places.
Quoted 2 lines
It seems to go all the way back to pre-git. Hm. Could be cleaned up separately I guess.
Yeah, all I find as a hint is:
/* FIXME: better would be to fix all architectures to never return
> MAX_NUMNODES, then the silly min_ts could be dropped. */
which also predates git history.
Quoted 11 lines
include/linux/nodemask.h static inline int num_node_state(enum node_states state) · line 498
Quoted 5 lines
#if
defined(CONFIG_NUMA)&& (MAX_NUMNODES> 1)include/linux/nodemask.h +1
-1-extern int node_random (const nodemask_t *maskp); +extern unsigned int node_random (const nodemask_t *maskp);So this one I'm not convinced about. It has a documented return value of
NUMA_NO_NODEaka -1 if the mask is empty. And since it's not a wrapper around a correspondingbitmap_random()(which would presumably, did it exist, use the "return nbits if empty" convention), there's no compelling reason to make its return type unsigned.
Agreed; I'll drop this change.
Thanks!
Signature · 1 line
Kees Cook