Skip to content

Commit 1470afe

Browse files
Dave ChinnerDarrick J. Wong
authored andcommitted
cpumask: introduce for_each_cpu_or
Equivalent of for_each_cpu_and, except it ORs the two masks together so it iterates all the CPUs present in either mask. Signed-off-by: Dave Chinner <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]>
1 parent 3cfb929 commit 1470afe

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

include/linux/cpumask.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@ unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int sta
350350
#define for_each_cpu_andnot(cpu, mask1, mask2) \
351351
for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
352352

353+
/**
354+
* for_each_cpu_or - iterate over every cpu present in either mask
355+
* @cpu: the (optionally unsigned) integer iterator
356+
* @mask1: the first cpumask pointer
357+
* @mask2: the second cpumask pointer
358+
*
359+
* This saves a temporary CPU mask in many places. It is equivalent to:
360+
* struct cpumask tmp;
361+
* cpumask_or(&tmp, &mask1, &mask2);
362+
* for_each_cpu(cpu, &tmp)
363+
* ...
364+
*
365+
* After the loop, cpu is >= nr_cpu_ids.
366+
*/
367+
#define for_each_cpu_or(cpu, mask1, mask2) \
368+
for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
369+
353370
/**
354371
* cpumask_any_but - return a "random" in a cpumask, but not this one.
355372
* @mask: the cpumask to search

include/linux/find.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long
1414
unsigned long nbits, unsigned long start);
1515
unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
1616
unsigned long nbits, unsigned long start);
17+
unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2,
18+
unsigned long nbits, unsigned long start);
1719
unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
1820
unsigned long start);
1921
extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
@@ -127,6 +129,36 @@ unsigned long find_next_andnot_bit(const unsigned long *addr1,
127129
}
128130
#endif
129131

132+
#ifndef find_next_or_bit
133+
/**
134+
* find_next_or_bit - find the next set bit in either memory regions
135+
* @addr1: The first address to base the search on
136+
* @addr2: The second address to base the search on
137+
* @size: The bitmap size in bits
138+
* @offset: The bitnumber to start searching at
139+
*
140+
* Returns the bit number for the next set bit
141+
* If no bits are set, returns @size.
142+
*/
143+
static inline
144+
unsigned long find_next_or_bit(const unsigned long *addr1,
145+
const unsigned long *addr2, unsigned long size,
146+
unsigned long offset)
147+
{
148+
if (small_const_nbits(size)) {
149+
unsigned long val;
150+
151+
if (unlikely(offset >= size))
152+
return size;
153+
154+
val = (*addr1 | *addr2) & GENMASK(size - 1, offset);
155+
return val ? __ffs(val) : size;
156+
}
157+
158+
return _find_next_or_bit(addr1, addr2, size, offset);
159+
}
160+
#endif
161+
130162
#ifndef find_next_zero_bit
131163
/**
132164
* find_next_zero_bit - find the next cleared bit in a memory region
@@ -536,6 +568,11 @@ unsigned long find_next_bit_le(const void *addr, unsigned
536568
(bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
537569
(bit)++)
538570

571+
#define for_each_or_bit(bit, addr1, addr2, size) \
572+
for ((bit) = 0; \
573+
(bit) = find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
574+
(bit)++)
575+
539576
/* same as for_each_set_bit() but use bit as value to start with */
540577
#define for_each_set_bit_from(bit, addr, size) \
541578
for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)

lib/find_bit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned l
182182
EXPORT_SYMBOL(_find_next_andnot_bit);
183183
#endif
184184

185+
#ifndef find_next_or_bit
186+
unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2,
187+
unsigned long nbits, unsigned long start)
188+
{
189+
return FIND_NEXT_BIT(addr1[idx] | addr2[idx], /* nop */, nbits, start);
190+
}
191+
EXPORT_SYMBOL(_find_next_or_bit);
192+
#endif
193+
185194
#ifndef find_next_zero_bit
186195
unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
187196
unsigned long start)

0 commit comments

Comments
 (0)