Skip to content

Commit 5da703e

Browse files
YuryNorovbp3tk0v
authored andcommitted
cpumask: Add cpumask_{first,next}_andnot() API
With the lack of the functions, client code has to abuse less efficient cpumask_nth(). Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com> Signed-off-by: James Morse <james.morse@arm.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: James Morse <james.morse@arm.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reviewed-by: Fenghua Yu <fenghuay@nvidia.com> Tested-by: Fenghua Yu <fenghuay@nvidia.com> Tested-by: James Morse <james.morse@arm.com> Tested-by: Tony Luck <tony.luck@intel.com> Link: https://lore.kernel.org/20250515165855.31452-4-james.morse@arm.com
1 parent 13f0a02 commit 5da703e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

include/linux/cpumask.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask
178178
return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
179179
}
180180

181+
/**
182+
* cpumask_first_andnot - return the first cpu from *srcp1 & ~*srcp2
183+
* @srcp1: the first input
184+
* @srcp2: the second input
185+
*
186+
* Return: >= nr_cpu_ids if no such cpu found.
187+
*/
188+
static __always_inline
189+
unsigned int cpumask_first_andnot(const struct cpumask *srcp1, const struct cpumask *srcp2)
190+
{
191+
return find_first_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
192+
}
193+
181194
/**
182195
* cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
183196
* @srcp1: the first input
@@ -284,6 +297,25 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
284297
small_cpumask_bits, n + 1);
285298
}
286299

300+
/**
301+
* cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
302+
* @n: the cpu prior to the place to search (i.e. return will be > @n)
303+
* @src1p: the first cpumask pointer
304+
* @src2p: the second cpumask pointer
305+
*
306+
* Return: >= nr_cpu_ids if no further cpus set in both.
307+
*/
308+
static __always_inline
309+
unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
310+
const struct cpumask *src2p)
311+
{
312+
/* -1 is a legal arg here. */
313+
if (n != -1)
314+
cpumask_check(n);
315+
return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
316+
small_cpumask_bits, n + 1);
317+
}
318+
287319
/**
288320
* cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
289321
* @n+1. If nothing found, wrap around and start from
@@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
458490
return cpumask_next_and(cpu, mask1, mask2);
459491
}
460492

493+
/**
494+
* cpumask_any_andnot_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
495+
* @mask1: the first input cpumask
496+
* @mask2: the second input cpumask
497+
* @cpu: the cpu to ignore
498+
*
499+
* If @cpu == -1, the function returns the first matching cpu.
500+
* Returns >= nr_cpu_ids if no cpus set.
501+
*/
502+
static __always_inline
503+
unsigned int cpumask_any_andnot_but(const struct cpumask *mask1,
504+
const struct cpumask *mask2,
505+
int cpu)
506+
{
507+
unsigned int i;
508+
509+
/* -1 is a legal arg here. */
510+
if (cpu != -1)
511+
cpumask_check(cpu);
512+
513+
i = cpumask_first_andnot(mask1, mask2);
514+
if (i != cpu)
515+
return i;
516+
517+
return cpumask_next_andnot(cpu, mask1, mask2);
518+
}
519+
461520
/**
462521
* cpumask_nth - get the Nth cpu in a cpumask
463522
* @srcp: the cpumask pointer

0 commit comments

Comments
 (0)