Skip to content

Commit da91309

Browse files
amirvdavem330
authored andcommitted
cpumask: Utility function to set n'th cpu - local cpu first
This function sets the n'th cpu - local cpu's first. For example: in a 16 cores server with even cpu's local, will get the following values: cpumask_set_cpu_local_first(0, numa, cpumask) => cpu 0 is set cpumask_set_cpu_local_first(1, numa, cpumask) => cpu 2 is set ... cpumask_set_cpu_local_first(7, numa, cpumask) => cpu 14 is set cpumask_set_cpu_local_first(8, numa, cpumask) => cpu 1 is set cpumask_set_cpu_local_first(9, numa, cpumask) => cpu 3 is set ... cpumask_set_cpu_local_first(15, numa, cpumask) => cpu 15 is set Curently this function will be used by multi queue networking devices to calculate the irq affinity mask, such that as many local cpu's as possible will be utilized to handle the mq device irq's. Signed-off-by: Amir Vadai <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d4f3862 commit da91309

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

include/linux/cpumask.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ static inline unsigned int cpumask_any_but(const struct cpumask *mask,
142142
return 1;
143143
}
144144

145+
static inline int cpumask_set_cpu_local_first(int i, int numa_node, cpumask_t *dstp)
146+
{
147+
set_bit(0, cpumask_bits(dstp));
148+
149+
return 0;
150+
}
151+
145152
#define for_each_cpu(cpu, mask) \
146153
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
147154
#define for_each_cpu_not(cpu, mask) \
@@ -192,6 +199,7 @@ static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
192199

193200
int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
194201
int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
202+
int cpumask_set_cpu_local_first(int i, int numa_node, cpumask_t *dstp);
195203

196204
/**
197205
* for_each_cpu - iterate over every cpu in a mask

lib/cpumask.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,66 @@ void __init free_bootmem_cpumask_var(cpumask_var_t mask)
164164
memblock_free_early(__pa(mask), cpumask_size());
165165
}
166166
#endif
167+
168+
/**
169+
* cpumask_set_cpu_local_first - set i'th cpu with local numa cpu's first
170+
*
171+
* @i: index number
172+
* @numa_node: local numa_node
173+
* @dstp: cpumask with the relevant cpu bit set according to the policy
174+
*
175+
* This function sets the cpumask according to a numa aware policy.
176+
* cpumask could be used as an affinity hint for the IRQ related to a
177+
* queue. When the policy is to spread queues across cores - local cores
178+
* first.
179+
*
180+
* Returns 0 on success, -ENOMEM for no memory, and -EAGAIN when failed to set
181+
* the cpu bit and need to re-call the function.
182+
*/
183+
int cpumask_set_cpu_local_first(int i, int numa_node, cpumask_t *dstp)
184+
{
185+
cpumask_var_t mask;
186+
int cpu;
187+
int ret = 0;
188+
189+
if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
190+
return -ENOMEM;
191+
192+
i %= num_online_cpus();
193+
194+
if (!cpumask_of_node(numa_node)) {
195+
/* Use all online cpu's for non numa aware system */
196+
cpumask_copy(mask, cpu_online_mask);
197+
} else {
198+
int n;
199+
200+
cpumask_and(mask,
201+
cpumask_of_node(numa_node), cpu_online_mask);
202+
203+
n = cpumask_weight(mask);
204+
if (i >= n) {
205+
i -= n;
206+
207+
/* If index > number of local cpu's, mask out local
208+
* cpu's
209+
*/
210+
cpumask_andnot(mask, cpu_online_mask, mask);
211+
}
212+
}
213+
214+
for_each_cpu(cpu, mask) {
215+
if (--i < 0)
216+
goto out;
217+
}
218+
219+
ret = -EAGAIN;
220+
221+
out:
222+
free_cpumask_var(mask);
223+
224+
if (!ret)
225+
cpumask_set_cpu(cpu, dstp);
226+
227+
return ret;
228+
}
229+
EXPORT_SYMBOL(cpumask_set_cpu_local_first);

0 commit comments

Comments
 (0)