Skip to content

Commit cdc6655

Browse files
Dawei LiKAGA-KOKO
authored andcommitted
cpumask: Introduce cpumask_first_and_and()
Introduce cpumask_first_and_and() to get intersection between 3 cpumasks, free of any intermediate cpumask variable. Instead, cpumask_first_and_and() works in-place with all inputs and produces desired output directly. Signed-off-by: Dawei Li <dawei.li@shingroup.cn> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Yury Norov <yury.norov@gmail.com> Link: https://lore.kernel.org/r/20240416085454.3547175-2-dawei.li@shingroup.cn
1 parent c7cad38 commit cdc6655

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

include/linux/cpumask.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask
187187
return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
188188
}
189189

190+
/**
191+
* cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
192+
* @srcp1: the first input
193+
* @srcp2: the second input
194+
* @srcp3: the third input
195+
*
196+
* Return: >= nr_cpu_ids if no cpus set in all.
197+
*/
198+
static inline
199+
unsigned int cpumask_first_and_and(const struct cpumask *srcp1,
200+
const struct cpumask *srcp2,
201+
const struct cpumask *srcp3)
202+
{
203+
return find_first_and_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
204+
cpumask_bits(srcp3), small_cpumask_bits);
205+
}
206+
190207
/**
191208
* cpumask_last - get the last CPU in a cpumask
192209
* @srcp: - the cpumask pointer

include/linux/find.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsign
2929
unsigned long n);
3030
extern unsigned long _find_first_and_bit(const unsigned long *addr1,
3131
const unsigned long *addr2, unsigned long size);
32+
unsigned long _find_first_and_and_bit(const unsigned long *addr1, const unsigned long *addr2,
33+
const unsigned long *addr3, unsigned long size);
3234
extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
3335
extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
3436

@@ -345,6 +347,31 @@ unsigned long find_first_and_bit(const unsigned long *addr1,
345347
}
346348
#endif
347349

350+
/**
351+
* find_first_and_and_bit - find the first set bit in 3 memory regions
352+
* @addr1: The first address to base the search on
353+
* @addr2: The second address to base the search on
354+
* @addr3: The third address to base the search on
355+
* @size: The bitmap size in bits
356+
*
357+
* Returns the bit number for the first set bit
358+
* If no bits are set, returns @size.
359+
*/
360+
static inline
361+
unsigned long find_first_and_and_bit(const unsigned long *addr1,
362+
const unsigned long *addr2,
363+
const unsigned long *addr3,
364+
unsigned long size)
365+
{
366+
if (small_const_nbits(size)) {
367+
unsigned long val = *addr1 & *addr2 & *addr3 & GENMASK(size - 1, 0);
368+
369+
return val ? __ffs(val) : size;
370+
}
371+
372+
return _find_first_and_and_bit(addr1, addr2, addr3, size);
373+
}
374+
348375
#ifndef find_first_zero_bit
349376
/**
350377
* find_first_zero_bit - find the first cleared bit in a memory region

lib/find_bit.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ unsigned long _find_first_and_bit(const unsigned long *addr1,
116116
EXPORT_SYMBOL(_find_first_and_bit);
117117
#endif
118118

119+
/*
120+
* Find the first set bit in three memory regions.
121+
*/
122+
unsigned long _find_first_and_and_bit(const unsigned long *addr1,
123+
const unsigned long *addr2,
124+
const unsigned long *addr3,
125+
unsigned long size)
126+
{
127+
return FIND_FIRST_BIT(addr1[idx] & addr2[idx] & addr3[idx], /* nop */, size);
128+
}
129+
EXPORT_SYMBOL(_find_first_and_and_bit);
130+
119131
#ifndef find_first_zero_bit
120132
/*
121133
* Find the first cleared bit in a memory region.

0 commit comments

Comments
 (0)