1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
3
4 /*
5 * Cpumasks provide a bitmap suitable for representing the
6 * set of CPU's in a system, one bit position per CPU number. In general,
7 * only nr_cpu_ids (<= NR_CPUS) bits are valid.
8 */
9 #include <linux/kernel.h>
10 #include <linux/threads.h>
11 #include <linux/bitmap.h>
12 #include <linux/bug.h>
13
14 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
15
16 /**
17 * cpumask_bits - get the bits in a cpumask
18 * @maskp: the struct cpumask *
19 *
20 * You should only assume nr_cpu_ids bits of this mask are valid. This is
21 * a macro so it's const-correct.
22 */
23 #define cpumask_bits(maskp) ((maskp)->bits)
24
25 /**
26 * cpumask_pr_args - printf args to output a cpumask
27 * @maskp: cpumask to be printed
28 *
29 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
30 */
31 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
32
33 #if NR_CPUS == 1
34 #define nr_cpu_ids 1
35 #else
36 extern int nr_cpu_ids;
37 #endif
38
39 #ifdef CONFIG_CPUMASK_OFFSTACK
40 /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
41 * not all bits may be allocated. */
42 #define nr_cpumask_bits nr_cpu_ids
43 #else
44 #define nr_cpumask_bits NR_CPUS
45 #endif
46
47 /*
48 * The following particular system cpumasks and operations manage
49 * possible, present, active and online cpus.
50 *
51 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
52 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
53 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
54 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
55 *
56 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
57 *
58 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
59 * that it is possible might ever be plugged in at anytime during the
60 * life of that system boot. The cpu_present_mask is dynamic(*),
61 * representing which CPUs are currently plugged in. And
62 * cpu_online_mask is the dynamic subset of cpu_present_mask,
63 * indicating those CPUs available for scheduling.
64 *
65 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
66 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
67 * ACPI reports present at boot.
68 *
69 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
70 * depending on what ACPI reports as currently plugged in, otherwise
71 * cpu_present_mask is just a copy of cpu_possible_mask.
72 *
73 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
74 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
75 *
76 * Subtleties:
77 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
78 * assumption that their single CPU is online. The UP
79 * cpu_{online,possible,present}_masks are placebos. Changing them
80 * will have no useful affect on the following num_*_cpus()
81 * and cpu_*() macros in the UP case. This ugliness is a UP
82 * optimization - don't waste any instructions or memory references
83 * asking if you're online or how many CPUs there are if there is
84 * only one CPU.
85 */
86
87 extern const struct cpumask *const cpu_possible_mask;
88 extern const struct cpumask *const cpu_online_mask;
89 extern const struct cpumask *const cpu_present_mask;
90 extern const struct cpumask *const cpu_active_mask;
91
92 #if NR_CPUS > 1
93 #define num_online_cpus() cpumask_weight(cpu_online_mask)
94 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
95 #define num_present_cpus() cpumask_weight(cpu_present_mask)
96 #define num_active_cpus() cpumask_weight(cpu_active_mask)
97 #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
98 #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
99 #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
100 #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
101 #else
102 #define num_online_cpus() 1U
103 #define num_possible_cpus() 1U
104 #define num_present_cpus() 1U
105 #define num_active_cpus() 1U
106 #define cpu_online(cpu) ((cpu) == 0)
107 #define cpu_possible(cpu) ((cpu) == 0)
108 #define cpu_present(cpu) ((cpu) == 0)
109 #define cpu_active(cpu) ((cpu) == 0)
110 #endif
111
112 /* verify cpu argument to cpumask_* operators */
cpumask_check(unsigned int cpu)113 static inline unsigned int cpumask_check(unsigned int cpu)
114 {
115 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
116 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
117 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
118 return cpu;
119 }
120
121 #if NR_CPUS == 1
122 /* Uniprocessor. Assume all masks are "1". */
cpumask_first(const struct cpumask * srcp)123 static inline unsigned int cpumask_first(const struct cpumask *srcp)
124 {
125 return 0;
126 }
127
128 /* Valid inputs for n are -1 and 0. */
cpumask_next(int n,const struct cpumask * srcp)129 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
130 {
131 return n+1;
132 }
133
cpumask_next_zero(int n,const struct cpumask * srcp)134 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
135 {
136 return n+1;
137 }
138
cpumask_next_and(int n,const struct cpumask * srcp,const struct cpumask * andp)139 static inline unsigned int cpumask_next_and(int n,
140 const struct cpumask *srcp,
141 const struct cpumask *andp)
142 {
143 return n+1;
144 }
145
146 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
cpumask_any_but(const struct cpumask * mask,unsigned int cpu)147 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
148 unsigned int cpu)
149 {
150 return 1;
151 }
152
cpumask_local_spread(unsigned int i,int node)153 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
154 {
155 return 0;
156 }
157
158 #define for_each_cpu(cpu, mask) \
159 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
160 #define for_each_cpu_not(cpu, mask) \
161 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
162 #define for_each_cpu_and(cpu, mask, and) \
163 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
164 #else
165 /**
166 * cpumask_first - get the first cpu in a cpumask
167 * @srcp: the cpumask pointer
168 *
169 * Returns >= nr_cpu_ids if no cpus set.
170 */
cpumask_first(const struct cpumask * srcp)171 static inline unsigned int cpumask_first(const struct cpumask *srcp)
172 {
173 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
174 }
175
176 /**
177 * cpumask_next - get the next cpu in a cpumask
178 * @n: the cpu prior to the place to search (ie. return will be > @n)
179 * @srcp: the cpumask pointer
180 *
181 * Returns >= nr_cpu_ids if no further cpus set.
182 */
cpumask_next(int n,const struct cpumask * srcp)183 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
184 {
185 /* -1 is a legal arg here. */
186 if (n != -1)
187 cpumask_check(n);
188 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
189 }
190
191 /**
192 * cpumask_next_zero - get the next unset cpu in a cpumask
193 * @n: the cpu prior to the place to search (ie. return will be > @n)
194 * @srcp: the cpumask pointer
195 *
196 * Returns >= nr_cpu_ids if no further cpus unset.
197 */
cpumask_next_zero(int n,const struct cpumask * srcp)198 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
199 {
200 /* -1 is a legal arg here. */
201 if (n != -1)
202 cpumask_check(n);
203 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
204 }
205
206 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
207 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
208 unsigned int cpumask_local_spread(unsigned int i, int node);
209
210 /**
211 * for_each_cpu - iterate over every cpu in a mask
212 * @cpu: the (optionally unsigned) integer iterator
213 * @mask: the cpumask pointer
214 *
215 * After the loop, cpu is >= nr_cpu_ids.
216 */
217 #define for_each_cpu(cpu, mask) \
218 for ((cpu) = -1; \
219 (cpu) = cpumask_next((cpu), (mask)), \
220 (cpu) < nr_cpu_ids;)
221
222 /**
223 * for_each_cpu_not - iterate over every cpu in a complemented mask
224 * @cpu: the (optionally unsigned) integer iterator
225 * @mask: the cpumask pointer
226 *
227 * After the loop, cpu is >= nr_cpu_ids.
228 */
229 #define for_each_cpu_not(cpu, mask) \
230 for ((cpu) = -1; \
231 (cpu) = cpumask_next_zero((cpu), (mask)), \
232 (cpu) < nr_cpu_ids;)
233
234 /**
235 * for_each_cpu_and - iterate over every cpu in both masks
236 * @cpu: the (optionally unsigned) integer iterator
237 * @mask: the first cpumask pointer
238 * @and: the second cpumask pointer
239 *
240 * This saves a temporary CPU mask in many places. It is equivalent to:
241 * struct cpumask tmp;
242 * cpumask_and(&tmp, &mask, &and);
243 * for_each_cpu(cpu, &tmp)
244 * ...
245 *
246 * After the loop, cpu is >= nr_cpu_ids.
247 */
248 #define for_each_cpu_and(cpu, mask, and) \
249 for ((cpu) = -1; \
250 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
251 (cpu) < nr_cpu_ids;)
252 #endif /* SMP */
253
254 #define CPU_BITS_NONE \
255 { \
256 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
257 }
258
259 #define CPU_BITS_CPU0 \
260 { \
261 [0] = 1UL \
262 }
263
264 /**
265 * cpumask_set_cpu - set a cpu in a cpumask
266 * @cpu: cpu number (< nr_cpu_ids)
267 * @dstp: the cpumask pointer
268 */
cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)269 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
270 {
271 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
272 }
273
274 /**
275 * cpumask_clear_cpu - clear a cpu in a cpumask
276 * @cpu: cpu number (< nr_cpu_ids)
277 * @dstp: the cpumask pointer
278 */
cpumask_clear_cpu(int cpu,struct cpumask * dstp)279 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
280 {
281 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
282 }
283
284 /**
285 * cpumask_test_cpu - test for a cpu in a cpumask
286 * @cpu: cpu number (< nr_cpu_ids)
287 * @cpumask: the cpumask pointer
288 *
289 * Returns 1 if @cpu is set in @cpumask, else returns 0
290 *
291 * No static inline type checking - see Subtlety (1) above.
292 */
293 #define cpumask_test_cpu(cpu, cpumask) \
294 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
295
296 /**
297 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
298 * @cpu: cpu number (< nr_cpu_ids)
299 * @cpumask: the cpumask pointer
300 *
301 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
302 *
303 * test_and_set_bit wrapper for cpumasks.
304 */
cpumask_test_and_set_cpu(int cpu,struct cpumask * cpumask)305 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
306 {
307 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
308 }
309
310 /**
311 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
312 * @cpu: cpu number (< nr_cpu_ids)
313 * @cpumask: the cpumask pointer
314 *
315 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
316 *
317 * test_and_clear_bit wrapper for cpumasks.
318 */
cpumask_test_and_clear_cpu(int cpu,struct cpumask * cpumask)319 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
320 {
321 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
322 }
323
324 /**
325 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
326 * @dstp: the cpumask pointer
327 */
cpumask_setall(struct cpumask * dstp)328 static inline void cpumask_setall(struct cpumask *dstp)
329 {
330 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
331 }
332
333 /**
334 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
335 * @dstp: the cpumask pointer
336 */
cpumask_clear(struct cpumask * dstp)337 static inline void cpumask_clear(struct cpumask *dstp)
338 {
339 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
340 }
341
342 /**
343 * cpumask_and - *dstp = *src1p & *src2p
344 * @dstp: the cpumask result
345 * @src1p: the first input
346 * @src2p: the second input
347 *
348 * If *@dstp is empty, returns 0, else returns 1
349 */
cpumask_and(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)350 static inline int cpumask_and(struct cpumask *dstp,
351 const struct cpumask *src1p,
352 const struct cpumask *src2p)
353 {
354 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
355 cpumask_bits(src2p), nr_cpumask_bits);
356 }
357
358 /**
359 * cpumask_or - *dstp = *src1p | *src2p
360 * @dstp: the cpumask result
361 * @src1p: the first input
362 * @src2p: the second input
363 */
cpumask_or(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)364 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
365 const struct cpumask *src2p)
366 {
367 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
368 cpumask_bits(src2p), nr_cpumask_bits);
369 }
370
371 /**
372 * cpumask_xor - *dstp = *src1p ^ *src2p
373 * @dstp: the cpumask result
374 * @src1p: the first input
375 * @src2p: the second input
376 */
cpumask_xor(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)377 static inline void cpumask_xor(struct cpumask *dstp,
378 const struct cpumask *src1p,
379 const struct cpumask *src2p)
380 {
381 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
382 cpumask_bits(src2p), nr_cpumask_bits);
383 }
384
385 /**
386 * cpumask_andnot - *dstp = *src1p & ~*src2p
387 * @dstp: the cpumask result
388 * @src1p: the first input
389 * @src2p: the second input
390 *
391 * If *@dstp is empty, returns 0, else returns 1
392 */
cpumask_andnot(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)393 static inline int cpumask_andnot(struct cpumask *dstp,
394 const struct cpumask *src1p,
395 const struct cpumask *src2p)
396 {
397 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
398 cpumask_bits(src2p), nr_cpumask_bits);
399 }
400
401 /**
402 * cpumask_complement - *dstp = ~*srcp
403 * @dstp: the cpumask result
404 * @srcp: the input to invert
405 */
cpumask_complement(struct cpumask * dstp,const struct cpumask * srcp)406 static inline void cpumask_complement(struct cpumask *dstp,
407 const struct cpumask *srcp)
408 {
409 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
410 nr_cpumask_bits);
411 }
412
413 /**
414 * cpumask_equal - *src1p == *src2p
415 * @src1p: the first input
416 * @src2p: the second input
417 */
cpumask_equal(const struct cpumask * src1p,const struct cpumask * src2p)418 static inline bool cpumask_equal(const struct cpumask *src1p,
419 const struct cpumask *src2p)
420 {
421 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
422 nr_cpumask_bits);
423 }
424
425 /**
426 * cpumask_intersects - (*src1p & *src2p) != 0
427 * @src1p: the first input
428 * @src2p: the second input
429 */
cpumask_intersects(const struct cpumask * src1p,const struct cpumask * src2p)430 static inline bool cpumask_intersects(const struct cpumask *src1p,
431 const struct cpumask *src2p)
432 {
433 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
434 nr_cpumask_bits);
435 }
436
437 /**
438 * cpumask_subset - (*src1p & ~*src2p) == 0
439 * @src1p: the first input
440 * @src2p: the second input
441 *
442 * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
443 */
cpumask_subset(const struct cpumask * src1p,const struct cpumask * src2p)444 static inline int cpumask_subset(const struct cpumask *src1p,
445 const struct cpumask *src2p)
446 {
447 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
448 nr_cpumask_bits);
449 }
450
451 /**
452 * cpumask_empty - *srcp == 0
453 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
454 */
cpumask_empty(const struct cpumask * srcp)455 static inline bool cpumask_empty(const struct cpumask *srcp)
456 {
457 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
458 }
459
460 /**
461 * cpumask_full - *srcp == 0xFFFFFFFF...
462 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
463 */
cpumask_full(const struct cpumask * srcp)464 static inline bool cpumask_full(const struct cpumask *srcp)
465 {
466 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
467 }
468
469 /**
470 * cpumask_weight - Count of bits in *srcp
471 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
472 */
cpumask_weight(const struct cpumask * srcp)473 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
474 {
475 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
476 }
477
478 /**
479 * cpumask_shift_right - *dstp = *srcp >> n
480 * @dstp: the cpumask result
481 * @srcp: the input to shift
482 * @n: the number of bits to shift by
483 */
cpumask_shift_right(struct cpumask * dstp,const struct cpumask * srcp,int n)484 static inline void cpumask_shift_right(struct cpumask *dstp,
485 const struct cpumask *srcp, int n)
486 {
487 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
488 nr_cpumask_bits);
489 }
490
491 /**
492 * cpumask_shift_left - *dstp = *srcp << n
493 * @dstp: the cpumask result
494 * @srcp: the input to shift
495 * @n: the number of bits to shift by
496 */
cpumask_shift_left(struct cpumask * dstp,const struct cpumask * srcp,int n)497 static inline void cpumask_shift_left(struct cpumask *dstp,
498 const struct cpumask *srcp, int n)
499 {
500 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
501 nr_cpumask_bits);
502 }
503
504 /**
505 * cpumask_copy - *dstp = *srcp
506 * @dstp: the result
507 * @srcp: the input cpumask
508 */
cpumask_copy(struct cpumask * dstp,const struct cpumask * srcp)509 static inline void cpumask_copy(struct cpumask *dstp,
510 const struct cpumask *srcp)
511 {
512 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
513 }
514
515 /**
516 * cpumask_any - pick a "random" cpu from *srcp
517 * @srcp: the input cpumask
518 *
519 * Returns >= nr_cpu_ids if no cpus set.
520 */
521 #define cpumask_any(srcp) cpumask_first(srcp)
522
523 /**
524 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
525 * @src1p: the first input
526 * @src2p: the second input
527 *
528 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
529 */
530 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
531
532 /**
533 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
534 * @mask1: the first input cpumask
535 * @mask2: the second input cpumask
536 *
537 * Returns >= nr_cpu_ids if no cpus set.
538 */
539 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
540
541 /**
542 * cpumask_of - the cpumask containing just a given cpu
543 * @cpu: the cpu (<= nr_cpu_ids)
544 */
545 #define cpumask_of(cpu) (get_cpu_mask(cpu))
546
547 /**
548 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
549 * @buf: the buffer to sprintf into
550 * @len: the length of the buffer
551 * @srcp: the cpumask to print
552 *
553 * If len is zero, returns zero. Otherwise returns the length of the
554 * (nul-terminated) @buf string.
555 */
cpumask_scnprintf(char * buf,int len,const struct cpumask * srcp)556 static inline int cpumask_scnprintf(char *buf, int len,
557 const struct cpumask *srcp)
558 {
559 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
560 }
561
562 /**
563 * cpumask_parse_user - extract a cpumask from a user string
564 * @buf: the buffer to extract from
565 * @len: the length of the buffer
566 * @dstp: the cpumask to set.
567 *
568 * Returns -errno, or 0 for success.
569 */
cpumask_parse_user(const char __user * buf,int len,struct cpumask * dstp)570 static inline int cpumask_parse_user(const char __user *buf, int len,
571 struct cpumask *dstp)
572 {
573 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
574 }
575
576 /**
577 * cpumask_parselist_user - extract a cpumask from a user string
578 * @buf: the buffer to extract from
579 * @len: the length of the buffer
580 * @dstp: the cpumask to set.
581 *
582 * Returns -errno, or 0 for success.
583 */
cpumask_parselist_user(const char __user * buf,int len,struct cpumask * dstp)584 static inline int cpumask_parselist_user(const char __user *buf, int len,
585 struct cpumask *dstp)
586 {
587 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
588 nr_cpumask_bits);
589 }
590
591 /**
592 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
593 * @buf: the buffer to sprintf into
594 * @len: the length of the buffer
595 * @srcp: the cpumask to print
596 *
597 * If len is zero, returns zero. Otherwise returns the length of the
598 * (nul-terminated) @buf string.
599 */
cpulist_scnprintf(char * buf,int len,const struct cpumask * srcp)600 static inline int cpulist_scnprintf(char *buf, int len,
601 const struct cpumask *srcp)
602 {
603 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
604 nr_cpumask_bits);
605 }
606
607 /**
608 * cpumask_parse - extract a cpumask from from a string
609 * @buf: the buffer to extract from
610 * @dstp: the cpumask to set.
611 *
612 * Returns -errno, or 0 for success.
613 */
cpumask_parse(const char * buf,struct cpumask * dstp)614 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
615 {
616 char *nl = strchr(buf, '\n');
617 unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
618
619 return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
620 }
621
622 /**
623 * cpulist_parse - extract a cpumask from a user string of ranges
624 * @buf: the buffer to extract from
625 * @dstp: the cpumask to set.
626 *
627 * Returns -errno, or 0 for success.
628 */
cpulist_parse(const char * buf,struct cpumask * dstp)629 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
630 {
631 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
632 }
633
634 /**
635 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
636 *
637 * This will eventually be a runtime variable, depending on nr_cpu_ids.
638 */
cpumask_size(void)639 static inline size_t cpumask_size(void)
640 {
641 /* FIXME: Once all cpumask assignments are eliminated, this
642 * can be nr_cpumask_bits */
643 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
644 }
645
646 /*
647 * cpumask_var_t: struct cpumask for stack usage.
648 *
649 * Oh, the wicked games we play! In order to make kernel coding a
650 * little more difficult, we typedef cpumask_var_t to an array or a
651 * pointer: doing &mask on an array is a noop, so it still works.
652 *
653 * ie.
654 * cpumask_var_t tmpmask;
655 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
656 * return -ENOMEM;
657 *
658 * ... use 'tmpmask' like a normal struct cpumask * ...
659 *
660 * free_cpumask_var(tmpmask);
661 *
662 *
663 * However, one notable exception is there. alloc_cpumask_var() allocates
664 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
665 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
666 *
667 * cpumask_var_t tmpmask;
668 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
669 * return -ENOMEM;
670 *
671 * var = *tmpmask;
672 *
673 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
674 * cpumask_copy() provide safe copy functionality.
675 *
676 * Note that there is another evil here: If you define a cpumask_var_t
677 * as a percpu variable then the way to obtain the address of the cpumask
678 * structure differently influences what this_cpu_* operation needs to be
679 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
680 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
681 * other type of cpumask_var_t implementation is configured.
682 */
683 #ifdef CONFIG_CPUMASK_OFFSTACK
684 typedef struct cpumask *cpumask_var_t;
685
686 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
687
688 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
689 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
690 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
691 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
692 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
693 void free_cpumask_var(cpumask_var_t mask);
694 void free_bootmem_cpumask_var(cpumask_var_t mask);
695
696 #else
697 typedef struct cpumask cpumask_var_t[1];
698
699 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
700
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)701 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
702 {
703 return true;
704 }
705
alloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)706 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
707 int node)
708 {
709 return true;
710 }
711
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)712 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
713 {
714 cpumask_clear(*mask);
715 return true;
716 }
717
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)718 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
719 int node)
720 {
721 cpumask_clear(*mask);
722 return true;
723 }
724
alloc_bootmem_cpumask_var(cpumask_var_t * mask)725 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
726 {
727 }
728
free_cpumask_var(cpumask_var_t mask)729 static inline void free_cpumask_var(cpumask_var_t mask)
730 {
731 }
732
free_bootmem_cpumask_var(cpumask_var_t mask)733 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
734 {
735 }
736 #endif /* CONFIG_CPUMASK_OFFSTACK */
737
738 /* It's common to want to use cpu_all_mask in struct member initializers,
739 * so it has to refer to an address rather than a pointer. */
740 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
741 #define cpu_all_mask to_cpumask(cpu_all_bits)
742
743 /* First bits of cpu_bit_bitmap are in fact unset. */
744 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
745
746 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
747 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
748 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
749
750 /* Wrappers for arch boot code to manipulate normally-constant masks */
751 void set_cpu_possible(unsigned int cpu, bool possible);
752 void set_cpu_present(unsigned int cpu, bool present);
753 void set_cpu_online(unsigned int cpu, bool online);
754 void set_cpu_active(unsigned int cpu, bool active);
755 void init_cpu_present(const struct cpumask *src);
756 void init_cpu_possible(const struct cpumask *src);
757 void init_cpu_online(const struct cpumask *src);
758
759 /**
760 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
761 * @bitmap: the bitmap
762 *
763 * There are a few places where cpumask_var_t isn't appropriate and
764 * static cpumasks must be used (eg. very early boot), yet we don't
765 * expose the definition of 'struct cpumask'.
766 *
767 * This does the conversion, and can be used as a constant initializer.
768 */
769 #define to_cpumask(bitmap) \
770 ((struct cpumask *)(1 ? (bitmap) \
771 : (void *)sizeof(__check_is_bitmap(bitmap))))
772
__check_is_bitmap(const unsigned long * bitmap)773 static inline int __check_is_bitmap(const unsigned long *bitmap)
774 {
775 return 1;
776 }
777
778 /*
779 * Special-case data structure for "single bit set only" constant CPU masks.
780 *
781 * We pre-generate all the 64 (or 32) possible bit positions, with enough
782 * padding to the left and the right, and return the constant pointer
783 * appropriately offset.
784 */
785 extern const unsigned long
786 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
787
get_cpu_mask(unsigned int cpu)788 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
789 {
790 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
791 p -= cpu / BITS_PER_LONG;
792 return to_cpumask(p);
793 }
794
795 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
796
797 #if NR_CPUS <= BITS_PER_LONG
798 #define CPU_BITS_ALL \
799 { \
800 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
801 }
802
803 #else /* NR_CPUS > BITS_PER_LONG */
804
805 #define CPU_BITS_ALL \
806 { \
807 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
808 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
809 }
810 #endif /* NR_CPUS > BITS_PER_LONG */
811
812 /*
813 *
814 * From here down, all obsolete. Use cpumask_ variants!
815 *
816 */
817 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
818 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
819
820 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
821
822 #if NR_CPUS <= BITS_PER_LONG
823
824 #define CPU_MASK_ALL \
825 (cpumask_t) { { \
826 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
827 } }
828
829 #else
830
831 #define CPU_MASK_ALL \
832 (cpumask_t) { { \
833 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
834 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
835 } }
836
837 #endif
838
839 #define CPU_MASK_NONE \
840 (cpumask_t) { { \
841 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
842 } }
843
844 #define CPU_MASK_CPU0 \
845 (cpumask_t) { { \
846 [0] = 1UL \
847 } }
848
849 #if NR_CPUS == 1
850 #define first_cpu(src) ({ (void)(src); 0; })
851 #define next_cpu(n, src) ({ (void)(src); 1; })
852 #define any_online_cpu(mask) 0
853 #define for_each_cpu_mask(cpu, mask) \
854 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
855 #else /* NR_CPUS > 1 */
856 int __first_cpu(const cpumask_t *srcp);
857 int __next_cpu(int n, const cpumask_t *srcp);
858
859 #define first_cpu(src) __first_cpu(&(src))
860 #define next_cpu(n, src) __next_cpu((n), &(src))
861 #define any_online_cpu(mask) cpumask_any_and(&mask, cpu_online_mask)
862 #define for_each_cpu_mask(cpu, mask) \
863 for ((cpu) = -1; \
864 (cpu) = next_cpu((cpu), (mask)), \
865 (cpu) < NR_CPUS; )
866 #endif /* SMP */
867
868 #if NR_CPUS <= 64
869
870 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
871
872 #else /* NR_CPUS > 64 */
873
874 int __next_cpu_nr(int n, const cpumask_t *srcp);
875 #define for_each_cpu_mask_nr(cpu, mask) \
876 for ((cpu) = -1; \
877 (cpu) = __next_cpu_nr((cpu), &(mask)), \
878 (cpu) < nr_cpu_ids; )
879
880 #endif /* NR_CPUS > 64 */
881
882 #define cpus_addr(src) ((src).bits)
883
884 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
__cpu_set(int cpu,volatile cpumask_t * dstp)885 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
886 {
887 set_bit(cpu, dstp->bits);
888 }
889
890 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
__cpu_clear(int cpu,volatile cpumask_t * dstp)891 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
892 {
893 clear_bit(cpu, dstp->bits);
894 }
895
896 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
__cpus_setall(cpumask_t * dstp,int nbits)897 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
898 {
899 bitmap_fill(dstp->bits, nbits);
900 }
901
902 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
__cpus_clear(cpumask_t * dstp,int nbits)903 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
904 {
905 bitmap_zero(dstp->bits, nbits);
906 }
907
908 /* No static inline type checking - see Subtlety (1) above. */
909 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
910
911 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
__cpu_test_and_set(int cpu,cpumask_t * addr)912 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
913 {
914 return test_and_set_bit(cpu, addr->bits);
915 }
916
917 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
__cpus_and(cpumask_t * dstp,const cpumask_t * src1p,const cpumask_t * src2p,int nbits)918 static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
919 const cpumask_t *src2p, int nbits)
920 {
921 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
922 }
923
924 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
__cpus_or(cpumask_t * dstp,const cpumask_t * src1p,const cpumask_t * src2p,int nbits)925 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
926 const cpumask_t *src2p, int nbits)
927 {
928 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
929 }
930
931 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
__cpus_xor(cpumask_t * dstp,const cpumask_t * src1p,const cpumask_t * src2p,int nbits)932 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
933 const cpumask_t *src2p, int nbits)
934 {
935 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
936 }
937
938 #define cpus_andnot(dst, src1, src2) \
939 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
__cpus_andnot(cpumask_t * dstp,const cpumask_t * src1p,const cpumask_t * src2p,int nbits)940 static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
941 const cpumask_t *src2p, int nbits)
942 {
943 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
944 }
945
946 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
__cpus_equal(const cpumask_t * src1p,const cpumask_t * src2p,int nbits)947 static inline int __cpus_equal(const cpumask_t *src1p,
948 const cpumask_t *src2p, int nbits)
949 {
950 return bitmap_equal(src1p->bits, src2p->bits, nbits);
951 }
952
953 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
__cpus_intersects(const cpumask_t * src1p,const cpumask_t * src2p,int nbits)954 static inline int __cpus_intersects(const cpumask_t *src1p,
955 const cpumask_t *src2p, int nbits)
956 {
957 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
958 }
959
960 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
__cpus_subset(const cpumask_t * src1p,const cpumask_t * src2p,int nbits)961 static inline int __cpus_subset(const cpumask_t *src1p,
962 const cpumask_t *src2p, int nbits)
963 {
964 return bitmap_subset(src1p->bits, src2p->bits, nbits);
965 }
966
967 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
__cpus_empty(const cpumask_t * srcp,int nbits)968 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
969 {
970 return bitmap_empty(srcp->bits, nbits);
971 }
972
973 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
__cpus_weight(const cpumask_t * srcp,int nbits)974 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
975 {
976 return bitmap_weight(srcp->bits, nbits);
977 }
978
979 #define cpus_shift_left(dst, src, n) \
980 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
__cpus_shift_left(cpumask_t * dstp,const cpumask_t * srcp,int n,int nbits)981 static inline void __cpus_shift_left(cpumask_t *dstp,
982 const cpumask_t *srcp, int n, int nbits)
983 {
984 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
985 }
986 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
987
988 #endif /* __LINUX_CPUMASK_H */
989