cpumask: remove dangerous CPU_MASK_ALL_PTR
[pohmelfs.git] / include / linux / cpumask.h
blobc0ab3588129d28ebf6399278919a88d9e6769f65
1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
4 /*
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
9 * use cpumask_t.
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.
18 * For details of cpu_remap(), see bitmap_bitremap in lib/bitmap.c
19 * For details of cpus_remap(), see bitmap_remap in lib/bitmap.c.
20 * For details of cpus_onto(), see bitmap_onto in lib/bitmap.c.
21 * For details of cpus_fold(), see bitmap_fold in lib/bitmap.c.
23 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24 * Note: The alternate operations with the suffix "_nr" are used
25 * to limit the range of the loop to nr_cpu_ids instead of
26 * NR_CPUS when NR_CPUS > 64 for performance reasons.
27 * If NR_CPUS is <= 64 then most assembler bitmask
28 * operators execute faster with a constant range, so
29 * the operator will continue to use NR_CPUS.
31 * Another consideration is that nr_cpu_ids is initialized
32 * to NR_CPUS and isn't lowered until the possible cpus are
33 * discovered (including any disabled cpus). So early uses
34 * will span the entire range of NR_CPUS.
35 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37 * The obsolescent cpumask operations are:
39 * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
40 * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
41 * void cpus_setall(mask) set all bits
42 * void cpus_clear(mask) clear all bits
43 * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
44 * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
46 * int cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
47 * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
48 * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
49 * int cpus_andnot(dst, src1, src2) dst = src1 & ~src2
50 * void cpus_complement(dst, src) dst = ~src
52 * int cpus_equal(mask1, mask2) Does mask1 == mask2?
53 * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
54 * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
55 * int cpus_empty(mask) Is mask empty (no bits sets)?
56 * int cpus_full(mask) Is mask full (all bits sets)?
57 * int cpus_weight(mask) Hamming weigh - number of set bits
58 * int cpus_weight_nr(mask) Same using nr_cpu_ids instead of NR_CPUS
60 * void cpus_shift_right(dst, src, n) Shift right
61 * void cpus_shift_left(dst, src, n) Shift left
63 * int first_cpu(mask) Number lowest set bit, or NR_CPUS
64 * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
65 * int next_cpu_nr(cpu, mask) Next cpu past 'cpu', or nr_cpu_ids
67 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
68 * (can be used as an lvalue)
69 * CPU_MASK_ALL Initializer - all bits set
70 * CPU_MASK_NONE Initializer - no bits set
71 * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
73 * CPUMASK_ALLOC kmalloc's a structure that is a composite of many cpumask_t
74 * variables, and CPUMASK_PTR provides pointers to each field.
76 * The structure should be defined something like this:
77 * struct my_cpumasks {
78 * cpumask_t mask1;
79 * cpumask_t mask2;
80 * };
82 * Usage is then:
83 * CPUMASK_ALLOC(my_cpumasks);
84 * CPUMASK_PTR(mask1, my_cpumasks);
85 * CPUMASK_PTR(mask2, my_cpumasks);
87 * --- DO NOT reference cpumask_t pointers until this check ---
88 * if (my_cpumasks == NULL)
89 * "kmalloc failed"...
91 * References are now pointers to the cpumask_t variables (*mask1, ...)
93 *if NR_CPUS > BITS_PER_LONG
94 * CPUMASK_ALLOC(m) Declares and allocates struct m *m =
95 * kmalloc(sizeof(*m), GFP_KERNEL)
96 * CPUMASK_FREE(m) Macro for kfree(m)
97 *else
98 * CPUMASK_ALLOC(m) Declares struct m _m, *m = &_m
99 * CPUMASK_FREE(m) Nop
100 *endif
101 * CPUMASK_PTR(v, m) Declares cpumask_t *v = &(m->v)
102 * ------------------------------------------------------------------------
104 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
105 * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
106 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
107 * int cpulist_parse(buf, map) Parse ascii string as cpulist
108 * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
109 * void cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
110 * void cpus_onto(dst, orig, relmap) *dst = orig relative to relmap
111 * void cpus_fold(dst, orig, sz) dst bits = orig bits mod sz
113 * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
114 * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
116 * int num_online_cpus() Number of online CPUs
117 * int num_possible_cpus() Number of all possible CPUs
118 * int num_present_cpus() Number of present CPUs
120 * int cpu_online(cpu) Is some cpu online?
121 * int cpu_possible(cpu) Is some cpu possible?
122 * int cpu_present(cpu) Is some cpu present (can schedule)?
124 * int any_online_cpu(mask) First online cpu in mask
126 * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
127 * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
128 * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
130 * Subtlety:
131 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
132 * to generate slightly worse code. Note for example the additional
133 * 40 lines of assembly code compiling the "for each possible cpu"
134 * loops buried in the disk_stat_read() macros calls when compiling
135 * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
136 * one-line #define for cpu_isset(), instead of wrapping an inline
137 * inside a macro, the way we do the other calls.
140 #include <linux/kernel.h>
141 #include <linux/threads.h>
142 #include <linux/bitmap.h>
144 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
145 extern cpumask_t _unused_cpumask_arg_;
147 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
148 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
149 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
151 set_bit(cpu, dstp->bits);
154 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
155 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
157 clear_bit(cpu, dstp->bits);
160 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
161 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
163 bitmap_fill(dstp->bits, nbits);
166 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
167 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
169 bitmap_zero(dstp->bits, nbits);
172 /* No static inline type checking - see Subtlety (1) above. */
173 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
175 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
176 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
178 return test_and_set_bit(cpu, addr->bits);
181 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
182 static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
183 const cpumask_t *src2p, int nbits)
185 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
188 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
189 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
190 const cpumask_t *src2p, int nbits)
192 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
195 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
196 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
197 const cpumask_t *src2p, int nbits)
199 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
202 #define cpus_andnot(dst, src1, src2) \
203 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
204 static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
205 const cpumask_t *src2p, int nbits)
207 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
210 #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
211 static inline void __cpus_complement(cpumask_t *dstp,
212 const cpumask_t *srcp, int nbits)
214 bitmap_complement(dstp->bits, srcp->bits, nbits);
217 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
218 static inline int __cpus_equal(const cpumask_t *src1p,
219 const cpumask_t *src2p, int nbits)
221 return bitmap_equal(src1p->bits, src2p->bits, nbits);
224 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
225 static inline int __cpus_intersects(const cpumask_t *src1p,
226 const cpumask_t *src2p, int nbits)
228 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
231 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
232 static inline int __cpus_subset(const cpumask_t *src1p,
233 const cpumask_t *src2p, int nbits)
235 return bitmap_subset(src1p->bits, src2p->bits, nbits);
238 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
239 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
241 return bitmap_empty(srcp->bits, nbits);
244 #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
245 static inline int __cpus_full(const cpumask_t *srcp, int nbits)
247 return bitmap_full(srcp->bits, nbits);
250 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
251 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
253 return bitmap_weight(srcp->bits, nbits);
256 #define cpus_shift_right(dst, src, n) \
257 __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
258 static inline void __cpus_shift_right(cpumask_t *dstp,
259 const cpumask_t *srcp, int n, int nbits)
261 bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
264 #define cpus_shift_left(dst, src, n) \
265 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
266 static inline void __cpus_shift_left(cpumask_t *dstp,
267 const cpumask_t *srcp, int n, int nbits)
269 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
271 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
274 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
275 * @bitmap: the bitmap
277 * There are a few places where cpumask_var_t isn't appropriate and
278 * static cpumasks must be used (eg. very early boot), yet we don't
279 * expose the definition of 'struct cpumask'.
281 * This does the conversion, and can be used as a constant initializer.
283 #define to_cpumask(bitmap) \
284 ((struct cpumask *)(1 ? (bitmap) \
285 : (void *)sizeof(__check_is_bitmap(bitmap))))
287 static inline int __check_is_bitmap(const unsigned long *bitmap)
289 return 1;
293 * Special-case data structure for "single bit set only" constant CPU masks.
295 * We pre-generate all the 64 (or 32) possible bit positions, with enough
296 * padding to the left and the right, and return the constant pointer
297 * appropriately offset.
299 extern const unsigned long
300 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
302 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
304 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
305 p -= cpu / BITS_PER_LONG;
306 return to_cpumask(p);
309 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
311 * In cases where we take the address of the cpumask immediately,
312 * gcc optimizes it out (it's a constant) and there's no huge stack
313 * variable created:
315 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
318 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
320 #if NR_CPUS <= BITS_PER_LONG
322 #define CPU_MASK_ALL \
323 (cpumask_t) { { \
324 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
327 #else
329 #define CPU_MASK_ALL \
330 (cpumask_t) { { \
331 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
332 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
335 /* cpu_mask_all is in init/main.c */
336 extern cpumask_t cpu_mask_all;
338 #endif
340 #define CPU_MASK_NONE \
341 (cpumask_t) { { \
342 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
345 #define CPU_MASK_CPU0 \
346 (cpumask_t) { { \
347 [0] = 1UL \
350 #define cpus_addr(src) ((src).bits)
352 #if NR_CPUS > BITS_PER_LONG
353 #define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
354 #define CPUMASK_FREE(m) kfree(m)
355 #else
356 #define CPUMASK_ALLOC(m) struct m _m, *m = &_m
357 #define CPUMASK_FREE(m)
358 #endif
359 #define CPUMASK_PTR(v, m) cpumask_t *v = &(m->v)
361 #define cpu_remap(oldbit, old, new) \
362 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
363 static inline int __cpu_remap(int oldbit,
364 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
366 return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
369 #define cpus_remap(dst, src, old, new) \
370 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
371 static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
372 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
374 bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
377 #define cpus_onto(dst, orig, relmap) \
378 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
379 static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
380 const cpumask_t *relmapp, int nbits)
382 bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
385 #define cpus_fold(dst, orig, sz) \
386 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
387 static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
388 int sz, int nbits)
390 bitmap_fold(dstp->bits, origp->bits, sz, nbits);
392 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
394 #if NR_CPUS == 1
396 #define nr_cpu_ids 1
397 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
398 #define first_cpu(src) ({ (void)(src); 0; })
399 #define next_cpu(n, src) ({ (void)(src); 1; })
400 #define any_online_cpu(mask) 0
401 #define for_each_cpu_mask(cpu, mask) \
402 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
403 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
404 #else /* NR_CPUS > 1 */
406 extern int nr_cpu_ids;
407 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
408 int __first_cpu(const cpumask_t *srcp);
409 int __next_cpu(int n, const cpumask_t *srcp);
410 int __any_online_cpu(const cpumask_t *mask);
412 #define first_cpu(src) __first_cpu(&(src))
413 #define next_cpu(n, src) __next_cpu((n), &(src))
414 #define any_online_cpu(mask) __any_online_cpu(&(mask))
415 #define for_each_cpu_mask(cpu, mask) \
416 for ((cpu) = -1; \
417 (cpu) = next_cpu((cpu), (mask)), \
418 (cpu) < NR_CPUS; )
419 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
420 #endif
422 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
423 #if NR_CPUS <= 64
425 #define next_cpu_nr(n, src) next_cpu(n, src)
426 #define cpus_weight_nr(cpumask) cpus_weight(cpumask)
427 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
429 #else /* NR_CPUS > 64 */
431 int __next_cpu_nr(int n, const cpumask_t *srcp);
432 #define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
433 #define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
434 #define for_each_cpu_mask_nr(cpu, mask) \
435 for ((cpu) = -1; \
436 (cpu) = next_cpu_nr((cpu), (mask)), \
437 (cpu) < nr_cpu_ids; )
439 #endif /* NR_CPUS > 64 */
440 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
443 * The following particular system cpumasks and operations manage
444 * possible, present, active and online cpus.
446 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
447 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
448 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
449 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
451 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
453 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
454 * that it is possible might ever be plugged in at anytime during the
455 * life of that system boot. The cpu_present_mask is dynamic(*),
456 * representing which CPUs are currently plugged in. And
457 * cpu_online_mask is the dynamic subset of cpu_present_mask,
458 * indicating those CPUs available for scheduling.
460 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
461 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
462 * ACPI reports present at boot.
464 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
465 * depending on what ACPI reports as currently plugged in, otherwise
466 * cpu_present_mask is just a copy of cpu_possible_mask.
468 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
469 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
471 * Subtleties:
472 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
473 * assumption that their single CPU is online. The UP
474 * cpu_{online,possible,present}_masks are placebos. Changing them
475 * will have no useful affect on the following num_*_cpus()
476 * and cpu_*() macros in the UP case. This ugliness is a UP
477 * optimization - don't waste any instructions or memory references
478 * asking if you're online or how many CPUs there are if there is
479 * only one CPU.
482 extern const struct cpumask *const cpu_possible_mask;
483 extern const struct cpumask *const cpu_online_mask;
484 extern const struct cpumask *const cpu_present_mask;
485 extern const struct cpumask *const cpu_active_mask;
487 /* These strip const, as traditionally they weren't const. */
488 #define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
489 #define cpu_online_map (*(cpumask_t *)cpu_online_mask)
490 #define cpu_present_map (*(cpumask_t *)cpu_present_mask)
491 #define cpu_active_map (*(cpumask_t *)cpu_active_mask)
493 #if NR_CPUS > 1
494 #define num_online_cpus() cpumask_weight(cpu_online_mask)
495 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
496 #define num_present_cpus() cpumask_weight(cpu_present_mask)
497 #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
498 #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
499 #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
500 #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
501 #else
502 #define num_online_cpus() 1
503 #define num_possible_cpus() 1
504 #define num_present_cpus() 1
505 #define cpu_online(cpu) ((cpu) == 0)
506 #define cpu_possible(cpu) ((cpu) == 0)
507 #define cpu_present(cpu) ((cpu) == 0)
508 #define cpu_active(cpu) ((cpu) == 0)
509 #endif
511 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
513 /* These are the new versions of the cpumask operators: passed by pointer.
514 * The older versions will be implemented in terms of these, then deleted. */
515 #define cpumask_bits(maskp) ((maskp)->bits)
517 #if NR_CPUS <= BITS_PER_LONG
518 #define CPU_BITS_ALL \
520 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
523 #else /* NR_CPUS > BITS_PER_LONG */
525 #define CPU_BITS_ALL \
527 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
528 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
530 #endif /* NR_CPUS > BITS_PER_LONG */
532 #ifdef CONFIG_CPUMASK_OFFSTACK
533 /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
534 * not all bits may be allocated. */
535 #define nr_cpumask_bits nr_cpu_ids
536 #else
537 #define nr_cpumask_bits NR_CPUS
538 #endif
540 /* verify cpu argument to cpumask_* operators */
541 static inline unsigned int cpumask_check(unsigned int cpu)
543 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
544 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
545 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
546 return cpu;
549 #if NR_CPUS == 1
550 /* Uniprocessor. Assume all masks are "1". */
551 static inline unsigned int cpumask_first(const struct cpumask *srcp)
553 return 0;
556 /* Valid inputs for n are -1 and 0. */
557 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
559 return n+1;
562 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
564 return n+1;
567 static inline unsigned int cpumask_next_and(int n,
568 const struct cpumask *srcp,
569 const struct cpumask *andp)
571 return n+1;
574 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
575 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
576 unsigned int cpu)
578 return 1;
581 #define for_each_cpu(cpu, mask) \
582 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
583 #define for_each_cpu_and(cpu, mask, and) \
584 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
585 #else
587 * cpumask_first - get the first cpu in a cpumask
588 * @srcp: the cpumask pointer
590 * Returns >= nr_cpu_ids if no cpus set.
592 static inline unsigned int cpumask_first(const struct cpumask *srcp)
594 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
598 * cpumask_next - get the next cpu in a cpumask
599 * @n: the cpu prior to the place to search (ie. return will be > @n)
600 * @srcp: the cpumask pointer
602 * Returns >= nr_cpu_ids if no further cpus set.
604 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
606 /* -1 is a legal arg here. */
607 if (n != -1)
608 cpumask_check(n);
609 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
613 * cpumask_next_zero - get the next unset cpu in a cpumask
614 * @n: the cpu prior to the place to search (ie. return will be > @n)
615 * @srcp: the cpumask pointer
617 * Returns >= nr_cpu_ids if no further cpus unset.
619 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
621 /* -1 is a legal arg here. */
622 if (n != -1)
623 cpumask_check(n);
624 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
627 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
628 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
631 * for_each_cpu - iterate over every cpu in a mask
632 * @cpu: the (optionally unsigned) integer iterator
633 * @mask: the cpumask pointer
635 * After the loop, cpu is >= nr_cpu_ids.
637 #define for_each_cpu(cpu, mask) \
638 for ((cpu) = -1; \
639 (cpu) = cpumask_next((cpu), (mask)), \
640 (cpu) < nr_cpu_ids;)
643 * for_each_cpu_and - iterate over every cpu in both masks
644 * @cpu: the (optionally unsigned) integer iterator
645 * @mask: the first cpumask pointer
646 * @and: the second cpumask pointer
648 * This saves a temporary CPU mask in many places. It is equivalent to:
649 * struct cpumask tmp;
650 * cpumask_and(&tmp, &mask, &and);
651 * for_each_cpu(cpu, &tmp)
652 * ...
654 * After the loop, cpu is >= nr_cpu_ids.
656 #define for_each_cpu_and(cpu, mask, and) \
657 for ((cpu) = -1; \
658 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
659 (cpu) < nr_cpu_ids;)
660 #endif /* SMP */
662 #define CPU_BITS_NONE \
664 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
667 #define CPU_BITS_CPU0 \
669 [0] = 1UL \
673 * cpumask_set_cpu - set a cpu in a cpumask
674 * @cpu: cpu number (< nr_cpu_ids)
675 * @dstp: the cpumask pointer
677 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
679 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
683 * cpumask_clear_cpu - clear a cpu in a cpumask
684 * @cpu: cpu number (< nr_cpu_ids)
685 * @dstp: the cpumask pointer
687 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
689 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
693 * cpumask_test_cpu - test for a cpu in a cpumask
694 * @cpu: cpu number (< nr_cpu_ids)
695 * @cpumask: the cpumask pointer
697 * No static inline type checking - see Subtlety (1) above.
699 #define cpumask_test_cpu(cpu, cpumask) \
700 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
703 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
704 * @cpu: cpu number (< nr_cpu_ids)
705 * @cpumask: the cpumask pointer
707 * test_and_set_bit wrapper for cpumasks.
709 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
711 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
715 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
716 * @cpu: cpu number (< nr_cpu_ids)
717 * @cpumask: the cpumask pointer
719 * test_and_clear_bit wrapper for cpumasks.
721 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
723 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
727 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
728 * @dstp: the cpumask pointer
730 static inline void cpumask_setall(struct cpumask *dstp)
732 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
736 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
737 * @dstp: the cpumask pointer
739 static inline void cpumask_clear(struct cpumask *dstp)
741 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
745 * cpumask_and - *dstp = *src1p & *src2p
746 * @dstp: the cpumask result
747 * @src1p: the first input
748 * @src2p: the second input
750 static inline int cpumask_and(struct cpumask *dstp,
751 const struct cpumask *src1p,
752 const struct cpumask *src2p)
754 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
755 cpumask_bits(src2p), nr_cpumask_bits);
759 * cpumask_or - *dstp = *src1p | *src2p
760 * @dstp: the cpumask result
761 * @src1p: the first input
762 * @src2p: the second input
764 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
765 const struct cpumask *src2p)
767 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
768 cpumask_bits(src2p), nr_cpumask_bits);
772 * cpumask_xor - *dstp = *src1p ^ *src2p
773 * @dstp: the cpumask result
774 * @src1p: the first input
775 * @src2p: the second input
777 static inline void cpumask_xor(struct cpumask *dstp,
778 const struct cpumask *src1p,
779 const struct cpumask *src2p)
781 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
782 cpumask_bits(src2p), nr_cpumask_bits);
786 * cpumask_andnot - *dstp = *src1p & ~*src2p
787 * @dstp: the cpumask result
788 * @src1p: the first input
789 * @src2p: the second input
791 static inline int cpumask_andnot(struct cpumask *dstp,
792 const struct cpumask *src1p,
793 const struct cpumask *src2p)
795 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
796 cpumask_bits(src2p), nr_cpumask_bits);
800 * cpumask_complement - *dstp = ~*srcp
801 * @dstp: the cpumask result
802 * @srcp: the input to invert
804 static inline void cpumask_complement(struct cpumask *dstp,
805 const struct cpumask *srcp)
807 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
808 nr_cpumask_bits);
812 * cpumask_equal - *src1p == *src2p
813 * @src1p: the first input
814 * @src2p: the second input
816 static inline bool cpumask_equal(const struct cpumask *src1p,
817 const struct cpumask *src2p)
819 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
820 nr_cpumask_bits);
824 * cpumask_intersects - (*src1p & *src2p) != 0
825 * @src1p: the first input
826 * @src2p: the second input
828 static inline bool cpumask_intersects(const struct cpumask *src1p,
829 const struct cpumask *src2p)
831 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
832 nr_cpumask_bits);
836 * cpumask_subset - (*src1p & ~*src2p) == 0
837 * @src1p: the first input
838 * @src2p: the second input
840 static inline int cpumask_subset(const struct cpumask *src1p,
841 const struct cpumask *src2p)
843 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
844 nr_cpumask_bits);
848 * cpumask_empty - *srcp == 0
849 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
851 static inline bool cpumask_empty(const struct cpumask *srcp)
853 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
857 * cpumask_full - *srcp == 0xFFFFFFFF...
858 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
860 static inline bool cpumask_full(const struct cpumask *srcp)
862 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
866 * cpumask_weight - Count of bits in *srcp
867 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
869 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
871 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
875 * cpumask_shift_right - *dstp = *srcp >> n
876 * @dstp: the cpumask result
877 * @srcp: the input to shift
878 * @n: the number of bits to shift by
880 static inline void cpumask_shift_right(struct cpumask *dstp,
881 const struct cpumask *srcp, int n)
883 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
884 nr_cpumask_bits);
888 * cpumask_shift_left - *dstp = *srcp << n
889 * @dstp: the cpumask result
890 * @srcp: the input to shift
891 * @n: the number of bits to shift by
893 static inline void cpumask_shift_left(struct cpumask *dstp,
894 const struct cpumask *srcp, int n)
896 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
897 nr_cpumask_bits);
901 * cpumask_copy - *dstp = *srcp
902 * @dstp: the result
903 * @srcp: the input cpumask
905 static inline void cpumask_copy(struct cpumask *dstp,
906 const struct cpumask *srcp)
908 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
912 * cpumask_any - pick a "random" cpu from *srcp
913 * @srcp: the input cpumask
915 * Returns >= nr_cpu_ids if no cpus set.
917 #define cpumask_any(srcp) cpumask_first(srcp)
920 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
921 * @src1p: the first input
922 * @src2p: the second input
924 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
926 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
929 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
930 * @mask1: the first input cpumask
931 * @mask2: the second input cpumask
933 * Returns >= nr_cpu_ids if no cpus set.
935 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
938 * cpumask_of - the cpumask containing just a given cpu
939 * @cpu: the cpu (<= nr_cpu_ids)
941 #define cpumask_of(cpu) (get_cpu_mask(cpu))
944 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
945 * @buf: the buffer to sprintf into
946 * @len: the length of the buffer
947 * @srcp: the cpumask to print
949 * If len is zero, returns zero. Otherwise returns the length of the
950 * (nul-terminated) @buf string.
952 static inline int cpumask_scnprintf(char *buf, int len,
953 const struct cpumask *srcp)
955 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
959 * cpumask_parse_user - extract a cpumask from a user string
960 * @buf: the buffer to extract from
961 * @len: the length of the buffer
962 * @dstp: the cpumask to set.
964 * Returns -errno, or 0 for success.
966 static inline int cpumask_parse_user(const char __user *buf, int len,
967 struct cpumask *dstp)
969 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
973 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
974 * @buf: the buffer to sprintf into
975 * @len: the length of the buffer
976 * @srcp: the cpumask to print
978 * If len is zero, returns zero. Otherwise returns the length of the
979 * (nul-terminated) @buf string.
981 static inline int cpulist_scnprintf(char *buf, int len,
982 const struct cpumask *srcp)
984 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
985 nr_cpumask_bits);
989 * cpulist_parse_user - extract a cpumask from a user string of ranges
990 * @buf: the buffer to extract from
991 * @len: the length of the buffer
992 * @dstp: the cpumask to set.
994 * Returns -errno, or 0 for success.
996 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
998 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
1002 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
1004 * This will eventually be a runtime variable, depending on nr_cpu_ids.
1006 static inline size_t cpumask_size(void)
1008 /* FIXME: Once all cpumask assignments are eliminated, this
1009 * can be nr_cpumask_bits */
1010 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
1014 * cpumask_var_t: struct cpumask for stack usage.
1016 * Oh, the wicked games we play! In order to make kernel coding a
1017 * little more difficult, we typedef cpumask_var_t to an array or a
1018 * pointer: doing &mask on an array is a noop, so it still works.
1020 * ie.
1021 * cpumask_var_t tmpmask;
1022 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
1023 * return -ENOMEM;
1025 * ... use 'tmpmask' like a normal struct cpumask * ...
1027 * free_cpumask_var(tmpmask);
1029 #ifdef CONFIG_CPUMASK_OFFSTACK
1030 typedef struct cpumask *cpumask_var_t;
1032 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1033 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1034 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1035 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1036 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1037 void free_cpumask_var(cpumask_var_t mask);
1038 void free_bootmem_cpumask_var(cpumask_var_t mask);
1040 #else
1041 typedef struct cpumask cpumask_var_t[1];
1043 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1045 return true;
1048 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1049 int node)
1051 return true;
1054 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1056 cpumask_clear(*mask);
1057 return true;
1060 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1061 int node)
1063 cpumask_clear(*mask);
1064 return true;
1067 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1071 static inline void free_cpumask_var(cpumask_var_t mask)
1075 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1078 #endif /* CONFIG_CPUMASK_OFFSTACK */
1080 /* It's common to want to use cpu_all_mask in struct member initializers,
1081 * so it has to refer to an address rather than a pointer. */
1082 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1083 #define cpu_all_mask to_cpumask(cpu_all_bits)
1085 /* First bits of cpu_bit_bitmap are in fact unset. */
1086 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1088 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1089 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1090 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1092 /* Wrappers for arch boot code to manipulate normally-constant masks */
1093 void set_cpu_possible(unsigned int cpu, bool possible);
1094 void set_cpu_present(unsigned int cpu, bool present);
1095 void set_cpu_online(unsigned int cpu, bool online);
1096 void set_cpu_active(unsigned int cpu, bool active);
1097 void init_cpu_present(const struct cpumask *src);
1098 void init_cpu_possible(const struct cpumask *src);
1099 void init_cpu_online(const struct cpumask *src);
1100 #endif /* __LINUX_CPUMASK_H */