Skip to content

Commit 005f170

Browse files
keesYuryNorov
authored andcommitted
bitmap: Fix return values to be unsigned
Both nodemask and bitmap routines had mixed return values that provided potentially signed return values 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). In preparation for fixing nodemask, fix all the bitmap routines that should be returning unsigned (or bool) values. Cc: Yury Norov <[email protected]> Cc: Rasmus Villemoes <[email protected]> Cc: Christophe de Dinechin <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Zhen Lei <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Yury Norov <[email protected]>
1 parent d603fd8 commit 005f170

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

include/linux/bitmap.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ unsigned long *devm_bitmap_zalloc(struct device *dev,
134134
* lib/bitmap.c provides these functions:
135135
*/
136136

137-
int __bitmap_equal(const unsigned long *bitmap1,
138-
const unsigned long *bitmap2, unsigned int nbits);
137+
bool __bitmap_equal(const unsigned long *bitmap1,
138+
const unsigned long *bitmap2, unsigned int nbits);
139139
bool __pure __bitmap_or_equal(const unsigned long *src1,
140140
const unsigned long *src2,
141141
const unsigned long *src3,
@@ -159,10 +159,10 @@ int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
159159
void __bitmap_replace(unsigned long *dst,
160160
const unsigned long *old, const unsigned long *new,
161161
const unsigned long *mask, unsigned int nbits);
162-
int __bitmap_intersects(const unsigned long *bitmap1,
163-
const unsigned long *bitmap2, unsigned int nbits);
164-
int __bitmap_subset(const unsigned long *bitmap1,
165-
const unsigned long *bitmap2, unsigned int nbits);
162+
bool __bitmap_intersects(const unsigned long *bitmap1,
163+
const unsigned long *bitmap2, unsigned int nbits);
164+
bool __bitmap_subset(const unsigned long *bitmap1,
165+
const unsigned long *bitmap2, unsigned int nbits);
166166
int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
167167
void __bitmap_set(unsigned long *map, unsigned int start, int len);
168168
void __bitmap_clear(unsigned long *map, unsigned int start, int len);
@@ -353,8 +353,8 @@ static inline void bitmap_complement(unsigned long *dst, const unsigned long *sr
353353
#endif
354354
#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
355355

356-
static inline int bitmap_equal(const unsigned long *src1,
357-
const unsigned long *src2, unsigned int nbits)
356+
static inline bool bitmap_equal(const unsigned long *src1,
357+
const unsigned long *src2, unsigned int nbits)
358358
{
359359
if (small_const_nbits(nbits))
360360
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
@@ -384,17 +384,18 @@ static inline bool bitmap_or_equal(const unsigned long *src1,
384384
return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
385385
}
386386

387-
static inline int bitmap_intersects(const unsigned long *src1,
388-
const unsigned long *src2, unsigned int nbits)
387+
static inline bool bitmap_intersects(const unsigned long *src1,
388+
const unsigned long *src2,
389+
unsigned int nbits)
389390
{
390391
if (small_const_nbits(nbits))
391392
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
392393
else
393394
return __bitmap_intersects(src1, src2, nbits);
394395
}
395396

396-
static inline int bitmap_subset(const unsigned long *src1,
397-
const unsigned long *src2, unsigned int nbits)
397+
static inline bool bitmap_subset(const unsigned long *src1,
398+
const unsigned long *src2, unsigned int nbits)
398399
{
399400
if (small_const_nbits(nbits))
400401
return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));

lib/bitmap.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@
4545
* for the best explanations of this ordering.
4646
*/
4747

48-
int __bitmap_equal(const unsigned long *bitmap1,
49-
const unsigned long *bitmap2, unsigned int bits)
48+
bool __bitmap_equal(const unsigned long *bitmap1,
49+
const unsigned long *bitmap2, unsigned int bits)
5050
{
5151
unsigned int k, lim = bits/BITS_PER_LONG;
5252
for (k = 0; k < lim; ++k)
5353
if (bitmap1[k] != bitmap2[k])
54-
return 0;
54+
return false;
5555

5656
if (bits % BITS_PER_LONG)
5757
if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
58-
return 0;
58+
return false;
5959

60-
return 1;
60+
return true;
6161
}
6262
EXPORT_SYMBOL(__bitmap_equal);
6363

@@ -303,33 +303,33 @@ void __bitmap_replace(unsigned long *dst,
303303
}
304304
EXPORT_SYMBOL(__bitmap_replace);
305305

306-
int __bitmap_intersects(const unsigned long *bitmap1,
307-
const unsigned long *bitmap2, unsigned int bits)
306+
bool __bitmap_intersects(const unsigned long *bitmap1,
307+
const unsigned long *bitmap2, unsigned int bits)
308308
{
309309
unsigned int k, lim = bits/BITS_PER_LONG;
310310
for (k = 0; k < lim; ++k)
311311
if (bitmap1[k] & bitmap2[k])
312-
return 1;
312+
return true;
313313

314314
if (bits % BITS_PER_LONG)
315315
if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
316-
return 1;
317-
return 0;
316+
return true;
317+
return false;
318318
}
319319
EXPORT_SYMBOL(__bitmap_intersects);
320320

321-
int __bitmap_subset(const unsigned long *bitmap1,
322-
const unsigned long *bitmap2, unsigned int bits)
321+
bool __bitmap_subset(const unsigned long *bitmap1,
322+
const unsigned long *bitmap2, unsigned int bits)
323323
{
324324
unsigned int k, lim = bits/BITS_PER_LONG;
325325
for (k = 0; k < lim; ++k)
326326
if (bitmap1[k] & ~bitmap2[k])
327-
return 0;
327+
return false;
328328

329329
if (bits % BITS_PER_LONG)
330330
if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
331-
return 0;
332-
return 1;
331+
return false;
332+
return true;
333333
}
334334
EXPORT_SYMBOL(__bitmap_subset);
335335

tools/include/linux/bitmap.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
1616
const unsigned long *bitmap2, int bits);
1717
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
1818
const unsigned long *bitmap2, unsigned int bits);
19-
int __bitmap_equal(const unsigned long *bitmap1,
20-
const unsigned long *bitmap2, unsigned int bits);
19+
bool __bitmap_equal(const unsigned long *bitmap1,
20+
const unsigned long *bitmap2, unsigned int bits);
2121
void bitmap_clear(unsigned long *map, unsigned int start, int len);
22-
int __bitmap_intersects(const unsigned long *bitmap1,
23-
const unsigned long *bitmap2, unsigned int bits);
22+
bool __bitmap_intersects(const unsigned long *bitmap1,
23+
const unsigned long *bitmap2, unsigned int bits);
2424

2525
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
2626
#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
@@ -162,8 +162,8 @@ static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
162162
#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
163163
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
164164

165-
static inline int bitmap_equal(const unsigned long *src1,
166-
const unsigned long *src2, unsigned int nbits)
165+
static inline bool bitmap_equal(const unsigned long *src1,
166+
const unsigned long *src2, unsigned int nbits)
167167
{
168168
if (small_const_nbits(nbits))
169169
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
@@ -173,8 +173,9 @@ static inline int bitmap_equal(const unsigned long *src1,
173173
return __bitmap_equal(src1, src2, nbits);
174174
}
175175

176-
static inline int bitmap_intersects(const unsigned long *src1,
177-
const unsigned long *src2, unsigned int nbits)
176+
static inline bool bitmap_intersects(const unsigned long *src1,
177+
const unsigned long *src2,
178+
unsigned int nbits)
178179
{
179180
if (small_const_nbits(nbits))
180181
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;

tools/lib/bitmap.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,31 @@ int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
7272
return result != 0;
7373
}
7474

75-
int __bitmap_equal(const unsigned long *bitmap1,
76-
const unsigned long *bitmap2, unsigned int bits)
75+
bool __bitmap_equal(const unsigned long *bitmap1,
76+
const unsigned long *bitmap2, unsigned int bits)
7777
{
7878
unsigned int k, lim = bits/BITS_PER_LONG;
7979
for (k = 0; k < lim; ++k)
8080
if (bitmap1[k] != bitmap2[k])
81-
return 0;
81+
return false;
8282

8383
if (bits % BITS_PER_LONG)
8484
if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
85-
return 0;
85+
return false;
8686

87-
return 1;
87+
return true;
8888
}
8989

90-
int __bitmap_intersects(const unsigned long *bitmap1,
91-
const unsigned long *bitmap2, unsigned int bits)
90+
bool __bitmap_intersects(const unsigned long *bitmap1,
91+
const unsigned long *bitmap2, unsigned int bits)
9292
{
9393
unsigned int k, lim = bits/BITS_PER_LONG;
9494
for (k = 0; k < lim; ++k)
9595
if (bitmap1[k] & bitmap2[k])
96-
return 1;
96+
return true;
9797

9898
if (bits % BITS_PER_LONG)
9999
if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
100-
return 1;
101-
return 0;
100+
return true;
101+
return false;
102102
}

0 commit comments

Comments
 (0)