Skip to content

Commit 944c417

Browse files
committed
cpumask: add cpumask_nth_{,and,andnot}
Add cpumask_nth_{,and,andnot} as wrappers around corresponding find functions, and use it in cpumask_local_spread(). Signed-off-by: Yury Norov <[email protected]>
1 parent 97848c1 commit 944c417

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

include/linux/cpumask.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,50 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
337337
return i;
338338
}
339339

340+
/**
341+
* cpumask_nth - get the first cpu in a cpumask
342+
* @srcp: the cpumask pointer
343+
* @cpu: the N'th cpu to find, starting from 0
344+
*
345+
* Returns >= nr_cpu_ids if such cpu doesn't exist.
346+
*/
347+
static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
348+
{
349+
return find_nth_bit(cpumask_bits(srcp), nr_cpumask_bits, cpumask_check(cpu));
350+
}
351+
352+
/**
353+
* cpumask_nth_and - get the first cpu in 2 cpumasks
354+
* @srcp1: the cpumask pointer
355+
* @srcp2: the cpumask pointer
356+
* @cpu: the N'th cpu to find, starting from 0
357+
*
358+
* Returns >= nr_cpu_ids if such cpu doesn't exist.
359+
*/
360+
static inline
361+
unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
362+
const struct cpumask *srcp2)
363+
{
364+
return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
365+
nr_cpumask_bits, cpumask_check(cpu));
366+
}
367+
368+
/**
369+
* cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd.
370+
* @srcp1: the cpumask pointer
371+
* @srcp2: the cpumask pointer
372+
* @cpu: the N'th cpu to find, starting from 0
373+
*
374+
* Returns >= nr_cpu_ids if such cpu doesn't exist.
375+
*/
376+
static inline
377+
unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
378+
const struct cpumask *srcp2)
379+
{
380+
return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
381+
nr_cpumask_bits, cpumask_check(cpu));
382+
}
383+
340384
#define CPU_BITS_NONE \
341385
{ \
342386
[0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \

lib/cpumask.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,21 @@ unsigned int cpumask_local_spread(unsigned int i, int node)
128128
i %= num_online_cpus();
129129

130130
if (node == NUMA_NO_NODE) {
131-
for_each_cpu(cpu, cpu_online_mask)
132-
if (i-- == 0)
133-
return cpu;
131+
cpu = cpumask_nth(i, cpu_online_mask);
132+
if (cpu < nr_cpu_ids)
133+
return cpu;
134134
} else {
135135
/* NUMA first. */
136-
for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
137-
if (i-- == 0)
138-
return cpu;
139-
140-
for_each_cpu(cpu, cpu_online_mask) {
141-
/* Skip NUMA nodes, done above. */
142-
if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
143-
continue;
144-
145-
if (i-- == 0)
146-
return cpu;
147-
}
136+
cpu = cpumask_nth_and(i, cpu_online_mask, cpumask_of_node(node));
137+
if (cpu < nr_cpu_ids)
138+
return cpu;
139+
140+
i -= cpumask_weight_and(cpu_online_mask, cpumask_of_node(node));
141+
142+
/* Skip NUMA nodes, done above. */
143+
cpu = cpumask_nth_andnot(i, cpu_online_mask, cpumask_of_node(node));
144+
if (cpu < nr_cpu_ids)
145+
return cpu;
148146
}
149147
BUG();
150148
}

0 commit comments

Comments
 (0)