Skip to content

Commit e5af323

Browse files
Matthew Wilcoxtorvalds
authored andcommitted
bitmap: optimise bitmap_set and bitmap_clear of a single bit
We have eight users calling bitmap_clear for a single bit and seventeen calling bitmap_set for a single bit. Rather than fix all of them to call __clear_bit or __set_bit, turn bitmap_clear and bitmap_set into inline functions and make this special case efficient. Link: http://lkml.kernel.org/r/20170628153221.11322-3-willy@infradead.org Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com> Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Matthew Wilcox <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3cc7812 commit e5af323

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎include/linux/bitmap.h‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ extern int __bitmap_intersects(const unsigned long *bitmap1,
112112
extern int __bitmap_subset(const unsigned long *bitmap1,
113113
const unsigned long *bitmap2, unsigned int nbits);
114114
extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
115-
116-
extern void bitmap_set(unsigned long *map, unsigned int start, int len);
117-
extern void bitmap_clear(unsigned long *map, unsigned int start, int len);
115+
extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
116+
extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
118117

119118
extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
120119
unsigned long size,
@@ -315,6 +314,24 @@ static __always_inline int bitmap_weight(const unsigned long *src, unsigned int
315314
return __bitmap_weight(src, nbits);
316315
}
317316

317+
static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
318+
unsigned int nbits)
319+
{
320+
if (__builtin_constant_p(nbits) && nbits == 1)
321+
__set_bit(start, map);
322+
else
323+
__bitmap_set(map, start, nbits);
324+
}
325+
326+
static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
327+
unsigned int nbits)
328+
{
329+
if (__builtin_constant_p(nbits) && nbits == 1)
330+
__clear_bit(start, map);
331+
else
332+
__bitmap_clear(map, start, nbits);
333+
}
334+
318335
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
319336
unsigned int shift, int nbits)
320337
{

‎lib/bitmap.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
251251
}
252252
EXPORT_SYMBOL(__bitmap_weight);
253253

254-
void bitmap_set(unsigned long *map, unsigned int start, int len)
254+
void __bitmap_set(unsigned long *map, unsigned int start, int len)
255255
{
256256
unsigned long *p = map + BIT_WORD(start);
257257
const unsigned int size = start + len;
@@ -270,9 +270,9 @@ void bitmap_set(unsigned long *map, unsigned int start, int len)
270270
*p |= mask_to_set;
271271
}
272272
}
273-
EXPORT_SYMBOL(bitmap_set);
273+
EXPORT_SYMBOL(__bitmap_set);
274274

275-
void bitmap_clear(unsigned long *map, unsigned int start, int len)
275+
void __bitmap_clear(unsigned long *map, unsigned int start, int len)
276276
{
277277
unsigned long *p = map + BIT_WORD(start);
278278
const unsigned int size = start + len;
@@ -291,7 +291,7 @@ void bitmap_clear(unsigned long *map, unsigned int start, int len)
291291
*p &= ~mask_to_clear;
292292
}
293293
}
294-
EXPORT_SYMBOL(bitmap_clear);
294+
EXPORT_SYMBOL(__bitmap_clear);
295295

296296
/**
297297
* bitmap_find_next_zero_area_off - find a contiguous aligned zero area

‎lib/test_bitmap.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,6 @@ static void __init test_bitmap_u32_array_conversions(void)
333333
}
334334
}
335335

336-
#define __bitmap_set(a, b, c) bitmap_set(a, b, c)
337-
#define __bitmap_clear(a, b, c) bitmap_clear(a, b, c)
338-
339336
static void noinline __init test_mem_optimisations(void)
340337
{
341338
DECLARE_BITMAP(bmap1, 1024);

0 commit comments

Comments
 (0)