1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
5 * Cpumasks provide a bitmap suitable for representing the
6 * set of CPU's in a system, one bit position per CPU number.
8 * The new cpumask_ ops take a "struct cpumask *"; the old ones
11 * See detailed comments in the file linux/bitmap.h describing the
12 * data type on which these cpumasks are based.
14 * For details of cpumask_scnprintf() and cpumask_parse_user(),
15 * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
16 * For details of cpulist_scnprintf() and cpulist_parse(), see
17 * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
19 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20 * Note: The alternate operations with the suffix "_nr" are used
21 * to limit the range of the loop to nr_cpu_ids instead of
22 * NR_CPUS when NR_CPUS > 64 for performance reasons.
23 * If NR_CPUS is <= 64 then most assembler bitmask
24 * operators execute faster with a constant range, so
25 * the operator will continue to use NR_CPUS.
27 * Another consideration is that nr_cpu_ids is initialized
28 * to NR_CPUS and isn't lowered until the possible cpus are
29 * discovered (including any disabled cpus). So early uses
30 * will span the entire range of NR_CPUS.
31 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33 * The obsolescent cpumask operations are:
35 * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
36 * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
37 * void cpus_setall(mask) set all bits
38 * void cpus_clear(mask) clear all bits
39 * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
40 * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
42 * int cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
43 * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
44 * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
45 * int cpus_andnot(dst, src1, src2) dst = src1 & ~src2
47 * int cpus_equal(mask1, mask2) Does mask1 == mask2?
48 * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
49 * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
50 * int cpus_empty(mask) Is mask empty (no bits sets)?
51 * int cpus_weight(mask) Hamming weigh - number of set bits
53 * void cpus_shift_left(dst, src, n) Shift left
55 * int first_cpu(mask) Number lowest set bit, or NR_CPUS
56 * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
58 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
59 * (can be used as an lvalue)
60 * CPU_MASK_ALL Initializer - all bits set
61 * CPU_MASK_NONE Initializer - no bits set
62 * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
64 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
65 * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
66 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
67 * int cpulist_parse(buf, map) Parse ascii string as cpulist
69 * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
70 * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
72 * int num_online_cpus() Number of online CPUs
73 * int num_possible_cpus() Number of all possible CPUs
74 * int num_present_cpus() Number of present CPUs
76 * int cpu_online(cpu) Is some cpu online?
77 * int cpu_possible(cpu) Is some cpu possible?
78 * int cpu_present(cpu) Is some cpu present (can schedule)?
80 * int any_online_cpu(mask) First online cpu in mask
82 * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
83 * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
84 * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
87 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
88 * to generate slightly worse code. Note for example the additional
89 * 40 lines of assembly code compiling the "for each possible cpu"
90 * loops buried in the disk_stat_read() macros calls when compiling
91 * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
92 * one-line #define for cpu_isset(), instead of wrapping an inline
93 * inside a macro, the way we do the other calls.
96 #include <linux/kernel.h>
97 #include <linux/threads.h>
98 #include <linux/bitmap.h>
100 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
102 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
103 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
104 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
106 set_bit(cpu, dstp->bits);
109 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
110 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
112 clear_bit(cpu, dstp->bits);
115 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
116 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
118 bitmap_fill(dstp->bits, nbits);
121 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
122 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
124 bitmap_zero(dstp->bits, nbits);
127 /* No static inline type checking - see Subtlety (1) above. */
128 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
130 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
131 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
133 return test_and_set_bit(cpu, addr->bits);
136 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
137 static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
138 const cpumask_t *src2p, int nbits)
140 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
143 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
144 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
145 const cpumask_t *src2p, int nbits)
147 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
150 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
151 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
152 const cpumask_t *src2p, int nbits)
154 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
157 #define cpus_andnot(dst, src1, src2) \
158 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
159 static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
160 const cpumask_t *src2p, int nbits)
162 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
165 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
166 static inline int __cpus_equal(const cpumask_t *src1p,
167 const cpumask_t *src2p, int nbits)
169 return bitmap_equal(src1p->bits, src2p->bits, nbits);
172 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
173 static inline int __cpus_intersects(const cpumask_t *src1p,
174 const cpumask_t *src2p, int nbits)
176 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
179 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
180 static inline int __cpus_subset(const cpumask_t *src1p,
181 const cpumask_t *src2p, int nbits)
183 return bitmap_subset(src1p->bits, src2p->bits, nbits);
186 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
187 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
189 return bitmap_empty(srcp->bits, nbits);
192 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
193 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
195 return bitmap_weight(srcp->bits, nbits);
198 #define cpus_shift_left(dst, src, n) \
199 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
200 static inline void __cpus_shift_left(cpumask_t *dstp,
201 const cpumask_t *srcp, int n, int nbits)
203 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
205 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
208 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
209 * @bitmap: the bitmap
211 * There are a few places where cpumask_var_t isn't appropriate and
212 * static cpumasks must be used (eg. very early boot), yet we don't
213 * expose the definition of 'struct cpumask'.
215 * This does the conversion, and can be used as a constant initializer.
217 #define to_cpumask(bitmap) \
218 ((struct cpumask *)(1 ? (bitmap) \
219 : (void *)sizeof(__check_is_bitmap(bitmap))))
221 static inline int __check_is_bitmap(const unsigned long *bitmap)
227 * Special-case data structure for "single bit set only" constant CPU masks.
229 * We pre-generate all the 64 (or 32) possible bit positions, with enough
230 * padding to the left and the right, and return the constant pointer
231 * appropriately offset.
233 extern const unsigned long
234 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
236 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
238 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
239 p -= cpu / BITS_PER_LONG;
240 return to_cpumask(p);
243 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
245 * In cases where we take the address of the cpumask immediately,
246 * gcc optimizes it out (it's a constant) and there's no huge stack
249 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
252 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
254 #if NR_CPUS <= BITS_PER_LONG
256 #define CPU_MASK_ALL \
258 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
263 #define CPU_MASK_ALL \
265 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
266 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
271 #define CPU_MASK_NONE \
273 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
276 #define CPU_MASK_CPU0 \
281 #define cpus_addr(src) ((src).bits)
283 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
288 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
289 #define first_cpu(src) ({ (void)(src); 0; })
290 #define next_cpu(n, src) ({ (void)(src); 1; })
291 #define any_online_cpu(mask) 0
292 #define for_each_cpu_mask(cpu, mask) \
293 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
294 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
295 #else /* NR_CPUS > 1 */
297 extern int nr_cpu_ids;
298 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
299 int __first_cpu(const cpumask_t *srcp);
300 int __next_cpu(int n, const cpumask_t *srcp);
301 int __any_online_cpu(const cpumask_t *mask);
303 #define first_cpu(src) __first_cpu(&(src))
304 #define next_cpu(n, src) __next_cpu((n), &(src))
305 #define any_online_cpu(mask) __any_online_cpu(&(mask))
306 #define for_each_cpu_mask(cpu, mask) \
308 (cpu) = next_cpu((cpu), (mask)), \
310 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
313 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
316 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
318 #else /* NR_CPUS > 64 */
320 int __next_cpu_nr(int n, const cpumask_t *srcp);
321 #define for_each_cpu_mask_nr(cpu, mask) \
323 (cpu) = __next_cpu_nr((cpu), &(mask)), \
324 (cpu) < nr_cpu_ids; )
326 #endif /* NR_CPUS > 64 */
327 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
330 * The following particular system cpumasks and operations manage
331 * possible, present, active and online cpus.
333 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
334 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
335 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
336 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
338 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
340 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
341 * that it is possible might ever be plugged in at anytime during the
342 * life of that system boot. The cpu_present_mask is dynamic(*),
343 * representing which CPUs are currently plugged in. And
344 * cpu_online_mask is the dynamic subset of cpu_present_mask,
345 * indicating those CPUs available for scheduling.
347 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
348 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
349 * ACPI reports present at boot.
351 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
352 * depending on what ACPI reports as currently plugged in, otherwise
353 * cpu_present_mask is just a copy of cpu_possible_mask.
355 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
356 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
359 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
360 * assumption that their single CPU is online. The UP
361 * cpu_{online,possible,present}_masks are placebos. Changing them
362 * will have no useful affect on the following num_*_cpus()
363 * and cpu_*() macros in the UP case. This ugliness is a UP
364 * optimization - don't waste any instructions or memory references
365 * asking if you're online or how many CPUs there are if there is
369 extern const struct cpumask *const cpu_possible_mask;
370 extern const struct cpumask *const cpu_online_mask;
371 extern const struct cpumask *const cpu_present_mask;
372 extern const struct cpumask *const cpu_active_mask;
374 /* These strip const, as traditionally they weren't const. */
375 #define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
376 #define cpu_online_map (*(cpumask_t *)cpu_online_mask)
377 #define cpu_present_map (*(cpumask_t *)cpu_present_mask)
378 #define cpu_active_map (*(cpumask_t *)cpu_active_mask)
381 #define num_online_cpus() cpumask_weight(cpu_online_mask)
382 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
383 #define num_present_cpus() cpumask_weight(cpu_present_mask)
384 #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
385 #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
386 #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
387 #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
389 #define num_online_cpus() 1
390 #define num_possible_cpus() 1
391 #define num_present_cpus() 1
392 #define cpu_online(cpu) ((cpu) == 0)
393 #define cpu_possible(cpu) ((cpu) == 0)
394 #define cpu_present(cpu) ((cpu) == 0)
395 #define cpu_active(cpu) ((cpu) == 0)
398 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
400 /* These are the new versions of the cpumask operators: passed by pointer.
401 * The older versions will be implemented in terms of these, then deleted. */
402 #define cpumask_bits(maskp) ((maskp)->bits)
404 #if NR_CPUS <= BITS_PER_LONG
405 #define CPU_BITS_ALL \
407 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
410 #else /* NR_CPUS > BITS_PER_LONG */
412 #define CPU_BITS_ALL \
414 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
415 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
417 #endif /* NR_CPUS > BITS_PER_LONG */
419 #ifdef CONFIG_CPUMASK_OFFSTACK
420 /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
421 * not all bits may be allocated. */
422 #define nr_cpumask_bits nr_cpu_ids
424 #define nr_cpumask_bits NR_CPUS
427 /* verify cpu argument to cpumask_* operators */
428 static inline unsigned int cpumask_check(unsigned int cpu)
430 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
431 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
432 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
437 /* Uniprocessor. Assume all masks are "1". */
438 static inline unsigned int cpumask_first(const struct cpumask *srcp)
443 /* Valid inputs for n are -1 and 0. */
444 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
449 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
454 static inline unsigned int cpumask_next_and(int n,
455 const struct cpumask *srcp,
456 const struct cpumask *andp)
461 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
462 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
468 #define for_each_cpu(cpu, mask) \
469 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
470 #define for_each_cpu_and(cpu, mask, and) \
471 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
474 * cpumask_first - get the first cpu in a cpumask
475 * @srcp: the cpumask pointer
477 * Returns >= nr_cpu_ids if no cpus set.
479 static inline unsigned int cpumask_first(const struct cpumask *srcp)
481 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
485 * cpumask_next - get the next cpu in a cpumask
486 * @n: the cpu prior to the place to search (ie. return will be > @n)
487 * @srcp: the cpumask pointer
489 * Returns >= nr_cpu_ids if no further cpus set.
491 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
493 /* -1 is a legal arg here. */
496 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
500 * cpumask_next_zero - get the next unset cpu in a cpumask
501 * @n: the cpu prior to the place to search (ie. return will be > @n)
502 * @srcp: the cpumask pointer
504 * Returns >= nr_cpu_ids if no further cpus unset.
506 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
508 /* -1 is a legal arg here. */
511 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
514 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
515 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
518 * for_each_cpu - iterate over every cpu in a mask
519 * @cpu: the (optionally unsigned) integer iterator
520 * @mask: the cpumask pointer
522 * After the loop, cpu is >= nr_cpu_ids.
524 #define for_each_cpu(cpu, mask) \
526 (cpu) = cpumask_next((cpu), (mask)), \
530 * for_each_cpu_and - iterate over every cpu in both masks
531 * @cpu: the (optionally unsigned) integer iterator
532 * @mask: the first cpumask pointer
533 * @and: the second cpumask pointer
535 * This saves a temporary CPU mask in many places. It is equivalent to:
536 * struct cpumask tmp;
537 * cpumask_and(&tmp, &mask, &and);
538 * for_each_cpu(cpu, &tmp)
541 * After the loop, cpu is >= nr_cpu_ids.
543 #define for_each_cpu_and(cpu, mask, and) \
545 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
549 #define CPU_BITS_NONE \
551 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
554 #define CPU_BITS_CPU0 \
560 * cpumask_set_cpu - set a cpu in a cpumask
561 * @cpu: cpu number (< nr_cpu_ids)
562 * @dstp: the cpumask pointer
564 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
566 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
570 * cpumask_clear_cpu - clear a cpu in a cpumask
571 * @cpu: cpu number (< nr_cpu_ids)
572 * @dstp: the cpumask pointer
574 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
576 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
580 * cpumask_test_cpu - test for a cpu in a cpumask
581 * @cpu: cpu number (< nr_cpu_ids)
582 * @cpumask: the cpumask pointer
584 * No static inline type checking - see Subtlety (1) above.
586 #define cpumask_test_cpu(cpu, cpumask) \
587 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
590 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
591 * @cpu: cpu number (< nr_cpu_ids)
592 * @cpumask: the cpumask pointer
594 * test_and_set_bit wrapper for cpumasks.
596 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
598 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
602 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
603 * @cpu: cpu number (< nr_cpu_ids)
604 * @cpumask: the cpumask pointer
606 * test_and_clear_bit wrapper for cpumasks.
608 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
610 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
614 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
615 * @dstp: the cpumask pointer
617 static inline void cpumask_setall(struct cpumask *dstp)
619 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
623 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
624 * @dstp: the cpumask pointer
626 static inline void cpumask_clear(struct cpumask *dstp)
628 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
632 * cpumask_and - *dstp = *src1p & *src2p
633 * @dstp: the cpumask result
634 * @src1p: the first input
635 * @src2p: the second input
637 static inline int cpumask_and(struct cpumask *dstp,
638 const struct cpumask *src1p,
639 const struct cpumask *src2p)
641 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
642 cpumask_bits(src2p), nr_cpumask_bits);
646 * cpumask_or - *dstp = *src1p | *src2p
647 * @dstp: the cpumask result
648 * @src1p: the first input
649 * @src2p: the second input
651 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
652 const struct cpumask *src2p)
654 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
655 cpumask_bits(src2p), nr_cpumask_bits);
659 * cpumask_xor - *dstp = *src1p ^ *src2p
660 * @dstp: the cpumask result
661 * @src1p: the first input
662 * @src2p: the second input
664 static inline void cpumask_xor(struct cpumask *dstp,
665 const struct cpumask *src1p,
666 const struct cpumask *src2p)
668 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
669 cpumask_bits(src2p), nr_cpumask_bits);
673 * cpumask_andnot - *dstp = *src1p & ~*src2p
674 * @dstp: the cpumask result
675 * @src1p: the first input
676 * @src2p: the second input
678 static inline int cpumask_andnot(struct cpumask *dstp,
679 const struct cpumask *src1p,
680 const struct cpumask *src2p)
682 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
683 cpumask_bits(src2p), nr_cpumask_bits);
687 * cpumask_complement - *dstp = ~*srcp
688 * @dstp: the cpumask result
689 * @srcp: the input to invert
691 static inline void cpumask_complement(struct cpumask *dstp,
692 const struct cpumask *srcp)
694 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
699 * cpumask_equal - *src1p == *src2p
700 * @src1p: the first input
701 * @src2p: the second input
703 static inline bool cpumask_equal(const struct cpumask *src1p,
704 const struct cpumask *src2p)
706 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
711 * cpumask_intersects - (*src1p & *src2p) != 0
712 * @src1p: the first input
713 * @src2p: the second input
715 static inline bool cpumask_intersects(const struct cpumask *src1p,
716 const struct cpumask *src2p)
718 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
723 * cpumask_subset - (*src1p & ~*src2p) == 0
724 * @src1p: the first input
725 * @src2p: the second input
727 static inline int cpumask_subset(const struct cpumask *src1p,
728 const struct cpumask *src2p)
730 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
735 * cpumask_empty - *srcp == 0
736 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
738 static inline bool cpumask_empty(const struct cpumask *srcp)
740 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
744 * cpumask_full - *srcp == 0xFFFFFFFF...
745 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
747 static inline bool cpumask_full(const struct cpumask *srcp)
749 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
753 * cpumask_weight - Count of bits in *srcp
754 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
756 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
758 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
762 * cpumask_shift_right - *dstp = *srcp >> n
763 * @dstp: the cpumask result
764 * @srcp: the input to shift
765 * @n: the number of bits to shift by
767 static inline void cpumask_shift_right(struct cpumask *dstp,
768 const struct cpumask *srcp, int n)
770 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
775 * cpumask_shift_left - *dstp = *srcp << n
776 * @dstp: the cpumask result
777 * @srcp: the input to shift
778 * @n: the number of bits to shift by
780 static inline void cpumask_shift_left(struct cpumask *dstp,
781 const struct cpumask *srcp, int n)
783 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
788 * cpumask_copy - *dstp = *srcp
790 * @srcp: the input cpumask
792 static inline void cpumask_copy(struct cpumask *dstp,
793 const struct cpumask *srcp)
795 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
799 * cpumask_any - pick a "random" cpu from *srcp
800 * @srcp: the input cpumask
802 * Returns >= nr_cpu_ids if no cpus set.
804 #define cpumask_any(srcp) cpumask_first(srcp)
807 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
808 * @src1p: the first input
809 * @src2p: the second input
811 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
813 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
816 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
817 * @mask1: the first input cpumask
818 * @mask2: the second input cpumask
820 * Returns >= nr_cpu_ids if no cpus set.
822 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
825 * cpumask_of - the cpumask containing just a given cpu
826 * @cpu: the cpu (<= nr_cpu_ids)
828 #define cpumask_of(cpu) (get_cpu_mask(cpu))
831 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
832 * @buf: the buffer to sprintf into
833 * @len: the length of the buffer
834 * @srcp: the cpumask to print
836 * If len is zero, returns zero. Otherwise returns the length of the
837 * (nul-terminated) @buf string.
839 static inline int cpumask_scnprintf(char *buf, int len,
840 const struct cpumask *srcp)
842 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
846 * cpumask_parse_user - extract a cpumask from a user string
847 * @buf: the buffer to extract from
848 * @len: the length of the buffer
849 * @dstp: the cpumask to set.
851 * Returns -errno, or 0 for success.
853 static inline int cpumask_parse_user(const char __user *buf, int len,
854 struct cpumask *dstp)
856 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
860 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
861 * @buf: the buffer to sprintf into
862 * @len: the length of the buffer
863 * @srcp: the cpumask to print
865 * If len is zero, returns zero. Otherwise returns the length of the
866 * (nul-terminated) @buf string.
868 static inline int cpulist_scnprintf(char *buf, int len,
869 const struct cpumask *srcp)
871 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
876 * cpulist_parse_user - extract a cpumask from a user string of ranges
877 * @buf: the buffer to extract from
878 * @len: the length of the buffer
879 * @dstp: the cpumask to set.
881 * Returns -errno, or 0 for success.
883 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
885 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
889 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
891 * This will eventually be a runtime variable, depending on nr_cpu_ids.
893 static inline size_t cpumask_size(void)
895 /* FIXME: Once all cpumask assignments are eliminated, this
896 * can be nr_cpumask_bits */
897 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
901 * cpumask_var_t: struct cpumask for stack usage.
903 * Oh, the wicked games we play! In order to make kernel coding a
904 * little more difficult, we typedef cpumask_var_t to an array or a
905 * pointer: doing &mask on an array is a noop, so it still works.
908 * cpumask_var_t tmpmask;
909 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
912 * ... use 'tmpmask' like a normal struct cpumask * ...
914 * free_cpumask_var(tmpmask);
916 #ifdef CONFIG_CPUMASK_OFFSTACK
917 typedef struct cpumask *cpumask_var_t;
919 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
920 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
921 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
922 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
923 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
924 void free_cpumask_var(cpumask_var_t mask);
925 void free_bootmem_cpumask_var(cpumask_var_t mask);
928 typedef struct cpumask cpumask_var_t[1];
930 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
935 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
941 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
943 cpumask_clear(*mask);
947 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
950 cpumask_clear(*mask);
954 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
958 static inline void free_cpumask_var(cpumask_var_t mask)
962 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
965 #endif /* CONFIG_CPUMASK_OFFSTACK */
967 /* It's common to want to use cpu_all_mask in struct member initializers,
968 * so it has to refer to an address rather than a pointer. */
969 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
970 #define cpu_all_mask to_cpumask(cpu_all_bits)
972 /* First bits of cpu_bit_bitmap are in fact unset. */
973 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
975 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
976 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
977 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
979 /* Wrappers for arch boot code to manipulate normally-constant masks */
980 void set_cpu_possible(unsigned int cpu, bool possible);
981 void set_cpu_present(unsigned int cpu, bool present);
982 void set_cpu_online(unsigned int cpu, bool online);
983 void set_cpu_active(unsigned int cpu, bool active);
984 void init_cpu_present(const struct cpumask *src);
985 void init_cpu_possible(const struct cpumask *src);
986 void init_cpu_online(const struct cpumask *src);
987 #endif /* __LINUX_CPUMASK_H */