Skip to content

Commit ec288a2

Browse files
committed
bitmap: unify find_bit operations
bitmap_for_each_{set,clear}_region() are similar to for_each_bit() macros in include/linux/find.h, but interface and implementation of them are different. This patch adds for_each_bitrange() macros and drops unused bitmap_*_region() API in sake of unification. Signed-off-by: Yury Norov <yury.norov@gmail.com> Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Acked-by: Dennis Zhou <dennis@kernel.org> Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # For MMC
1 parent 801a573 commit ec288a2

File tree

4 files changed

+65
-46
lines changed

4 files changed

+65
-46
lines changed

drivers/mmc/host/renesas_sdhi_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ static int renesas_sdhi_select_tuning(struct tmio_mmc_host *host)
628628
* is at least SH_MOBILE_SDHI_MIN_TAP_ROW probes long then use the
629629
* center index as the tap, otherwise bail out.
630630
*/
631-
bitmap_for_each_set_region(bitmap, rs, re, 0, taps_size) {
631+
for_each_set_bitrange(rs, re, bitmap, taps_size) {
632632
if (re - rs > tap_cnt) {
633633
tap_end = re;
634634
tap_start = rs;

include/linux/bitmap.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ struct device;
5555
* bitmap_clear(dst, pos, nbits) Clear specified bit area
5656
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
5757
* bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
58-
* bitmap_next_clear_region(map, &start, &end, nbits) Find next clear region
59-
* bitmap_next_set_region(map, &start, &end, nbits) Find next set region
60-
* bitmap_for_each_clear_region(map, rs, re, start, end)
61-
* Iterate over all clear regions
62-
* bitmap_for_each_set_region(map, rs, re, start, end)
63-
* Iterate over all set regions
6458
* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
6559
* bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
6660
* bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
@@ -467,14 +461,6 @@ static inline void bitmap_replace(unsigned long *dst,
467461
__bitmap_replace(dst, old, new, mask, nbits);
468462
}
469463

470-
static inline void bitmap_next_clear_region(unsigned long *bitmap,
471-
unsigned int *rs, unsigned int *re,
472-
unsigned int end)
473-
{
474-
*rs = find_next_zero_bit(bitmap, end, *rs);
475-
*re = find_next_bit(bitmap, end, *rs + 1);
476-
}
477-
478464
static inline void bitmap_next_set_region(unsigned long *bitmap,
479465
unsigned int *rs, unsigned int *re,
480466
unsigned int end)
@@ -483,25 +469,6 @@ static inline void bitmap_next_set_region(unsigned long *bitmap,
483469
*re = find_next_zero_bit(bitmap, end, *rs + 1);
484470
}
485471

486-
/*
487-
* Bitmap region iterators. Iterates over the bitmap between [@start, @end).
488-
* @rs and @re should be integer variables and will be set to start and end
489-
* index of the current clear or set region.
490-
*/
491-
#define bitmap_for_each_clear_region(bitmap, rs, re, start, end) \
492-
for ((rs) = (start), \
493-
bitmap_next_clear_region((bitmap), &(rs), &(re), (end)); \
494-
(rs) < (re); \
495-
(rs) = (re) + 1, \
496-
bitmap_next_clear_region((bitmap), &(rs), &(re), (end)))
497-
498-
#define bitmap_for_each_set_region(bitmap, rs, re, start, end) \
499-
for ((rs) = (start), \
500-
bitmap_next_set_region((bitmap), &(rs), &(re), (end)); \
501-
(rs) < (re); \
502-
(rs) = (re) + 1, \
503-
bitmap_next_set_region((bitmap), &(rs), &(re), (end)))
504-
505472
/**
506473
* BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
507474
* @n: u64 value

include/linux/find.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,62 @@ unsigned long find_next_bit_le(const void *addr, unsigned
301301
(bit) < (size); \
302302
(bit) = find_next_zero_bit((addr), (size), (bit) + 1))
303303

304+
/**
305+
* for_each_set_bitrange - iterate over all set bit ranges [b; e)
306+
* @b: bit offset of start of current bitrange (first set bit)
307+
* @e: bit offset of end of current bitrange (first unset bit)
308+
* @addr: bitmap address to base the search on
309+
* @size: bitmap size in number of bits
310+
*/
311+
#define for_each_set_bitrange(b, e, addr, size) \
312+
for ((b) = find_next_bit((addr), (size), 0), \
313+
(e) = find_next_zero_bit((addr), (size), (b) + 1); \
314+
(b) < (size); \
315+
(b) = find_next_bit((addr), (size), (e) + 1), \
316+
(e) = find_next_zero_bit((addr), (size), (b) + 1))
317+
318+
/**
319+
* for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
320+
* @b: bit offset of start of current bitrange (first set bit); must be initialized
321+
* @e: bit offset of end of current bitrange (first unset bit)
322+
* @addr: bitmap address to base the search on
323+
* @size: bitmap size in number of bits
324+
*/
325+
#define for_each_set_bitrange_from(b, e, addr, size) \
326+
for ((b) = find_next_bit((addr), (size), (b)), \
327+
(e) = find_next_zero_bit((addr), (size), (b) + 1); \
328+
(b) < (size); \
329+
(b) = find_next_bit((addr), (size), (e) + 1), \
330+
(e) = find_next_zero_bit((addr), (size), (b) + 1))
331+
332+
/**
333+
* for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
334+
* @b: bit offset of start of current bitrange (first unset bit)
335+
* @e: bit offset of end of current bitrange (first set bit)
336+
* @addr: bitmap address to base the search on
337+
* @size: bitmap size in number of bits
338+
*/
339+
#define for_each_clear_bitrange(b, e, addr, size) \
340+
for ((b) = find_next_zero_bit((addr), (size), 0), \
341+
(e) = find_next_bit((addr), (size), (b) + 1); \
342+
(b) < (size); \
343+
(b) = find_next_zero_bit((addr), (size), (e) + 1), \
344+
(e) = find_next_bit((addr), (size), (b) + 1))
345+
346+
/**
347+
* for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
348+
* @b: bit offset of start of current bitrange (first set bit); must be initialized
349+
* @e: bit offset of end of current bitrange (first unset bit)
350+
* @addr: bitmap address to base the search on
351+
* @size: bitmap size in number of bits
352+
*/
353+
#define for_each_clear_bitrange_from(b, e, addr, size) \
354+
for ((b) = find_next_zero_bit((addr), (size), (b)), \
355+
(e) = find_next_bit((addr), (size), (b) + 1); \
356+
(b) < (size); \
357+
(b) = find_next_zero_bit((addr), (size), (e) + 1), \
358+
(e) = find_next_bit((addr), (size), (b) + 1))
359+
304360
/**
305361
* for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
306362
* @start: bit offset to start search and to store the current iteration offset

mm/percpu.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index)
779779
{
780780
struct pcpu_block_md *block = chunk->md_blocks + index;
781781
unsigned long *alloc_map = pcpu_index_alloc_map(chunk, index);
782-
unsigned int rs, re, start; /* region start, region end */
782+
unsigned int start, end; /* region start, region end */
783783

784784
/* promote scan_hint to contig_hint */
785785
if (block->scan_hint) {
@@ -795,9 +795,8 @@ static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index)
795795
block->right_free = 0;
796796

797797
/* iterate over free areas and update the contig hints */
798-
bitmap_for_each_clear_region(alloc_map, rs, re, start,
799-
PCPU_BITMAP_BLOCK_BITS)
800-
pcpu_block_update(block, rs, re);
798+
for_each_clear_bitrange_from(start, end, alloc_map, PCPU_BITMAP_BLOCK_BITS)
799+
pcpu_block_update(block, start, end);
801800
}
802801

803802
/**
@@ -1852,13 +1851,12 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
18521851

18531852
/* populate if not all pages are already there */
18541853
if (!is_atomic) {
1855-
unsigned int page_start, page_end, rs, re;
1854+
unsigned int page_end, rs, re;
18561855

1857-
page_start = PFN_DOWN(off);
1856+
rs = PFN_DOWN(off);
18581857
page_end = PFN_UP(off + size);
18591858

1860-
bitmap_for_each_clear_region(chunk->populated, rs, re,
1861-
page_start, page_end) {
1859+
for_each_clear_bitrange_from(rs, re, chunk->populated, page_end) {
18621860
WARN_ON(chunk->immutable);
18631861

18641862
ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp);
@@ -2014,8 +2012,7 @@ static void pcpu_balance_free(bool empty_only)
20142012
list_for_each_entry_safe(chunk, next, &to_free, list) {
20152013
unsigned int rs, re;
20162014

2017-
bitmap_for_each_set_region(chunk->populated, rs, re, 0,
2018-
chunk->nr_pages) {
2015+
for_each_set_bitrange(rs, re, chunk->populated, chunk->nr_pages) {
20192016
pcpu_depopulate_chunk(chunk, rs, re);
20202017
spin_lock_irq(&pcpu_lock);
20212018
pcpu_chunk_depopulated(chunk, rs, re);
@@ -2085,8 +2082,7 @@ static void pcpu_balance_populated(void)
20852082
continue;
20862083

20872084
/* @chunk can't go away while pcpu_alloc_mutex is held */
2088-
bitmap_for_each_clear_region(chunk->populated, rs, re, 0,
2089-
chunk->nr_pages) {
2085+
for_each_clear_bitrange(rs, re, chunk->populated, chunk->nr_pages) {
20902086
int nr = min_t(int, re - rs, nr_to_pop);
20912087

20922088
spin_unlock_irq(&pcpu_lock);

0 commit comments

Comments
 (0)