new retransmit part 3
[cor_2_6_31.git] / include / linux / cpumask.h
blobc5ac87ca7bc647d32caef606c9c26ab0bb606090
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 * void 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 * void 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 void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
183 const cpumask_t *src2p, int nbits)
185 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 void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
205 const cpumask_t *src2p, int nbits)
207 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 #define CPU_MASK_ALL_PTR (&CPU_MASK_ALL)
329 #else
331 #define CPU_MASK_ALL \
332 (cpumask_t) { { \
333 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
334 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
337 /* cpu_mask_all is in init/main.c */
338 extern cpumask_t cpu_mask_all;
339 #define CPU_MASK_ALL_PTR (&cpu_mask_all)
341 #endif
343 #define CPU_MASK_NONE \
344 (cpumask_t) { { \
345 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
348 #define CPU_MASK_CPU0 \
349 (cpumask_t) { { \
350 [0] = 1UL \
353 #define cpus_addr(src) ((src).bits)
355 #if NR_CPUS > BITS_PER_LONG
356 #define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
357 #define CPUMASK_FREE(m) kfree(m)
358 #else
359 #define CPUMASK_ALLOC(m) struct m _m, *m = &_m
360 #define CPUMASK_FREE(m)
361 #endif
362 #define CPUMASK_PTR(v, m) cpumask_t *v = &(m->v)
364 #define cpu_remap(oldbit, old, new) \
365 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
366 static inline int __cpu_remap(int oldbit,
367 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
369 return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
372 #define cpus_remap(dst, src, old, new) \
373 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
374 static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
375 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
377 bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
380 #define cpus_onto(dst, orig, relmap) \
381 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
382 static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
383 const cpumask_t *relmapp, int nbits)
385 bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
388 #define cpus_fold(dst, orig, sz) \
389 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
390 static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
391 int sz, int nbits)
393 bitmap_fold(dstp->bits, origp->bits, sz, nbits);
395 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
397 #if NR_CPUS == 1
399 #define nr_cpu_ids 1
400 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
401 #define first_cpu(src) ({ (void)(src); 0; })
402 #define next_cpu(n, src) ({ (void)(src); 1; })
403 #define any_online_cpu(mask) 0
404 #define for_each_cpu_mask(cpu, mask) \
405 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
406 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
407 #else /* NR_CPUS > 1 */
409 extern int nr_cpu_ids;
410 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
411 int __first_cpu(const cpumask_t *srcp);
412 int __next_cpu(int n, const cpumask_t *srcp);
413 int __any_online_cpu(const cpumask_t *mask);
415 #define first_cpu(src) __first_cpu(&(src))
416 #define next_cpu(n, src) __next_cpu((n), &(src))
417 #define any_online_cpu(mask) __any_online_cpu(&(mask))
418 #define for_each_cpu_mask(cpu, mask) \
419 for ((cpu) = -1; \
420 (cpu) = next_cpu((cpu), (mask)), \
421 (cpu) < NR_CPUS; )
422 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
423 #endif
425 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
426 #if NR_CPUS <= 64
428 #define next_cpu_nr(n, src) next_cpu(n, src)
429 #define cpus_weight_nr(cpumask) cpus_weight(cpumask)
430 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
432 #else /* NR_CPUS > 64 */
434 int __next_cpu_nr(int n, const cpumask_t *srcp);
435 #define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
436 #define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
437 #define for_each_cpu_mask_nr(cpu, mask) \
438 for ((cpu) = -1; \
439 (cpu) = next_cpu_nr((cpu), (mask)), \
440 (cpu) < nr_cpu_ids; )
442 #endif /* NR_CPUS > 64 */
443 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
446 * The following particular system cpumasks and operations manage
447 * possible, present, active and online cpus.
449 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
450 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
451 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
452 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
454 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
456 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
457 * that it is possible might ever be plugged in at anytime during the
458 * life of that system boot. The cpu_present_mask is dynamic(*),
459 * representing which CPUs are currently plugged in. And
460 * cpu_online_mask is the dynamic subset of cpu_present_mask,
461 * indicating those CPUs available for scheduling.
463 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
464 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
465 * ACPI reports present at boot.
467 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
468 * depending on what ACPI reports as currently plugged in, otherwise
469 * cpu_present_mask is just a copy of cpu_possible_mask.
471 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
472 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
474 * Subtleties:
475 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
476 * assumption that their single CPU is online. The UP
477 * cpu_{online,possible,present}_masks are placebos. Changing them
478 * will have no useful affect on the following num_*_cpus()
479 * and cpu_*() macros in the UP case. This ugliness is a UP
480 * optimization - don't waste any instructions or memory references
481 * asking if you're online or how many CPUs there are if there is
482 * only one CPU.
485 extern const struct cpumask *const cpu_possible_mask;
486 extern const struct cpumask *const cpu_online_mask;
487 extern const struct cpumask *const cpu_present_mask;
488 extern const struct cpumask *const cpu_active_mask;
490 /* These strip const, as traditionally they weren't const. */
491 #define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
492 #define cpu_online_map (*(cpumask_t *)cpu_online_mask)
493 #define cpu_present_map (*(cpumask_t *)cpu_present_mask)
494 #define cpu_active_map (*(cpumask_t *)cpu_active_mask)
496 #if NR_CPUS > 1
497 #define num_online_cpus() cpumask_weight(cpu_online_mask)
498 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
499 #define num_present_cpus() cpumask_weight(cpu_present_mask)
500 #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
501 #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
502 #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
503 #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
504 #else
505 #define num_online_cpus() 1
506 #define num_possible_cpus() 1
507 #define num_present_cpus() 1
508 #define cpu_online(cpu) ((cpu) == 0)
509 #define cpu_possible(cpu) ((cpu) == 0)
510 #define cpu_present(cpu) ((cpu) == 0)
511 #define cpu_active(cpu) ((cpu) == 0)
512 #endif
514 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
516 /* These are the new versions of the cpumask operators: passed by pointer.
517 * The older versions will be implemented in terms of these, then deleted. */
518 #define cpumask_bits(maskp) ((maskp)->bits)
520 #if NR_CPUS <= BITS_PER_LONG
521 #define CPU_BITS_ALL \
523 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
526 #else /* NR_CPUS > BITS_PER_LONG */
528 #define CPU_BITS_ALL \
530 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
531 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
533 #endif /* NR_CPUS > BITS_PER_LONG */
535 #ifdef CONFIG_CPUMASK_OFFSTACK
536 /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
537 * not all bits may be allocated. */
538 #define nr_cpumask_bits nr_cpu_ids
539 #else
540 #define nr_cpumask_bits NR_CPUS
541 #endif
543 /* verify cpu argument to cpumask_* operators */
544 static inline unsigned int cpumask_check(unsigned int cpu)
546 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
547 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
548 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
549 return cpu;
552 #if NR_CPUS == 1
553 /* Uniprocessor. Assume all masks are "1". */
554 static inline unsigned int cpumask_first(const struct cpumask *srcp)
556 return 0;
559 /* Valid inputs for n are -1 and 0. */
560 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
562 return n+1;
565 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
567 return n+1;
570 static inline unsigned int cpumask_next_and(int n,
571 const struct cpumask *srcp,
572 const struct cpumask *andp)
574 return n+1;
577 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
578 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
579 unsigned int cpu)
581 return 1;
584 #define for_each_cpu(cpu, mask) \
585 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
586 #define for_each_cpu_and(cpu, mask, and) \
587 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
588 #else
590 * cpumask_first - get the first cpu in a cpumask
591 * @srcp: the cpumask pointer
593 * Returns >= nr_cpu_ids if no cpus set.
595 static inline unsigned int cpumask_first(const struct cpumask *srcp)
597 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
601 * cpumask_next - get the next cpu in a cpumask
602 * @n: the cpu prior to the place to search (ie. return will be > @n)
603 * @srcp: the cpumask pointer
605 * Returns >= nr_cpu_ids if no further cpus set.
607 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
609 /* -1 is a legal arg here. */
610 if (n != -1)
611 cpumask_check(n);
612 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
616 * cpumask_next_zero - get the next unset cpu in a cpumask
617 * @n: the cpu prior to the place to search (ie. return will be > @n)
618 * @srcp: the cpumask pointer
620 * Returns >= nr_cpu_ids if no further cpus unset.
622 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
624 /* -1 is a legal arg here. */
625 if (n != -1)
626 cpumask_check(n);
627 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
630 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
631 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
634 * for_each_cpu - iterate over every cpu in a mask
635 * @cpu: the (optionally unsigned) integer iterator
636 * @mask: the cpumask pointer
638 * After the loop, cpu is >= nr_cpu_ids.
640 #define for_each_cpu(cpu, mask) \
641 for ((cpu) = -1; \
642 (cpu) = cpumask_next((cpu), (mask)), \
643 (cpu) < nr_cpu_ids;)
646 * for_each_cpu_and - iterate over every cpu in both masks
647 * @cpu: the (optionally unsigned) integer iterator
648 * @mask: the first cpumask pointer
649 * @and: the second cpumask pointer
651 * This saves a temporary CPU mask in many places. It is equivalent to:
652 * struct cpumask tmp;
653 * cpumask_and(&tmp, &mask, &and);
654 * for_each_cpu(cpu, &tmp)
655 * ...
657 * After the loop, cpu is >= nr_cpu_ids.
659 #define for_each_cpu_and(cpu, mask, and) \
660 for ((cpu) = -1; \
661 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
662 (cpu) < nr_cpu_ids;)
663 #endif /* SMP */
665 #define CPU_BITS_NONE \
667 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
670 #define CPU_BITS_CPU0 \
672 [0] = 1UL \
676 * cpumask_set_cpu - set a cpu in a cpumask
677 * @cpu: cpu number (< nr_cpu_ids)
678 * @dstp: the cpumask pointer
680 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
682 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
686 * cpumask_clear_cpu - clear a cpu in a cpumask
687 * @cpu: cpu number (< nr_cpu_ids)
688 * @dstp: the cpumask pointer
690 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
692 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
696 * cpumask_test_cpu - test for a cpu in a cpumask
697 * @cpu: cpu number (< nr_cpu_ids)
698 * @cpumask: the cpumask pointer
700 * No static inline type checking - see Subtlety (1) above.
702 #define cpumask_test_cpu(cpu, cpumask) \
703 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
706 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
707 * @cpu: cpu number (< nr_cpu_ids)
708 * @cpumask: the cpumask pointer
710 * test_and_set_bit wrapper for cpumasks.
712 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
714 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
718 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
719 * @dstp: the cpumask pointer
721 static inline void cpumask_setall(struct cpumask *dstp)
723 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
727 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
728 * @dstp: the cpumask pointer
730 static inline void cpumask_clear(struct cpumask *dstp)
732 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
736 * cpumask_and - *dstp = *src1p & *src2p
737 * @dstp: the cpumask result
738 * @src1p: the first input
739 * @src2p: the second input
741 static inline void cpumask_and(struct cpumask *dstp,
742 const struct cpumask *src1p,
743 const struct cpumask *src2p)
745 bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
746 cpumask_bits(src2p), nr_cpumask_bits);
750 * cpumask_or - *dstp = *src1p | *src2p
751 * @dstp: the cpumask result
752 * @src1p: the first input
753 * @src2p: the second input
755 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
756 const struct cpumask *src2p)
758 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
759 cpumask_bits(src2p), nr_cpumask_bits);
763 * cpumask_xor - *dstp = *src1p ^ *src2p
764 * @dstp: the cpumask result
765 * @src1p: the first input
766 * @src2p: the second input
768 static inline void cpumask_xor(struct cpumask *dstp,
769 const struct cpumask *src1p,
770 const struct cpumask *src2p)
772 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
773 cpumask_bits(src2p), nr_cpumask_bits);
777 * cpumask_andnot - *dstp = *src1p & ~*src2p
778 * @dstp: the cpumask result
779 * @src1p: the first input
780 * @src2p: the second input
782 static inline void cpumask_andnot(struct cpumask *dstp,
783 const struct cpumask *src1p,
784 const struct cpumask *src2p)
786 bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
787 cpumask_bits(src2p), nr_cpumask_bits);
791 * cpumask_complement - *dstp = ~*srcp
792 * @dstp: the cpumask result
793 * @srcp: the input to invert
795 static inline void cpumask_complement(struct cpumask *dstp,
796 const struct cpumask *srcp)
798 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
799 nr_cpumask_bits);
803 * cpumask_equal - *src1p == *src2p
804 * @src1p: the first input
805 * @src2p: the second input
807 static inline bool cpumask_equal(const struct cpumask *src1p,
808 const struct cpumask *src2p)
810 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
811 nr_cpumask_bits);
815 * cpumask_intersects - (*src1p & *src2p) != 0
816 * @src1p: the first input
817 * @src2p: the second input
819 static inline bool cpumask_intersects(const struct cpumask *src1p,
820 const struct cpumask *src2p)
822 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
823 nr_cpumask_bits);
827 * cpumask_subset - (*src1p & ~*src2p) == 0
828 * @src1p: the first input
829 * @src2p: the second input
831 static inline int cpumask_subset(const struct cpumask *src1p,
832 const struct cpumask *src2p)
834 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
835 nr_cpumask_bits);
839 * cpumask_empty - *srcp == 0
840 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
842 static inline bool cpumask_empty(const struct cpumask *srcp)
844 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
848 * cpumask_full - *srcp == 0xFFFFFFFF...
849 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
851 static inline bool cpumask_full(const struct cpumask *srcp)
853 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
857 * cpumask_weight - Count of bits in *srcp
858 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
860 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
862 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
866 * cpumask_shift_right - *dstp = *srcp >> n
867 * @dstp: the cpumask result
868 * @srcp: the input to shift
869 * @n: the number of bits to shift by
871 static inline void cpumask_shift_right(struct cpumask *dstp,
872 const struct cpumask *srcp, int n)
874 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
875 nr_cpumask_bits);
879 * cpumask_shift_left - *dstp = *srcp << n
880 * @dstp: the cpumask result
881 * @srcp: the input to shift
882 * @n: the number of bits to shift by
884 static inline void cpumask_shift_left(struct cpumask *dstp,
885 const struct cpumask *srcp, int n)
887 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
888 nr_cpumask_bits);
892 * cpumask_copy - *dstp = *srcp
893 * @dstp: the result
894 * @srcp: the input cpumask
896 static inline void cpumask_copy(struct cpumask *dstp,
897 const struct cpumask *srcp)
899 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
903 * cpumask_any - pick a "random" cpu from *srcp
904 * @srcp: the input cpumask
906 * Returns >= nr_cpu_ids if no cpus set.
908 #define cpumask_any(srcp) cpumask_first(srcp)
911 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
912 * @src1p: the first input
913 * @src2p: the second input
915 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
917 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
920 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
921 * @mask1: the first input cpumask
922 * @mask2: the second input cpumask
924 * Returns >= nr_cpu_ids if no cpus set.
926 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
929 * cpumask_of - the cpumask containing just a given cpu
930 * @cpu: the cpu (<= nr_cpu_ids)
932 #define cpumask_of(cpu) (get_cpu_mask(cpu))
935 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
936 * @buf: the buffer to sprintf into
937 * @len: the length of the buffer
938 * @srcp: the cpumask to print
940 * If len is zero, returns zero. Otherwise returns the length of the
941 * (nul-terminated) @buf string.
943 static inline int cpumask_scnprintf(char *buf, int len,
944 const struct cpumask *srcp)
946 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
950 * cpumask_parse_user - extract a cpumask from a user string
951 * @buf: the buffer to extract from
952 * @len: the length of the buffer
953 * @dstp: the cpumask to set.
955 * Returns -errno, or 0 for success.
957 static inline int cpumask_parse_user(const char __user *buf, int len,
958 struct cpumask *dstp)
960 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
964 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
965 * @buf: the buffer to sprintf into
966 * @len: the length of the buffer
967 * @srcp: the cpumask to print
969 * If len is zero, returns zero. Otherwise returns the length of the
970 * (nul-terminated) @buf string.
972 static inline int cpulist_scnprintf(char *buf, int len,
973 const struct cpumask *srcp)
975 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
976 nr_cpumask_bits);
980 * cpulist_parse_user - extract a cpumask from a user string of ranges
981 * @buf: the buffer to extract from
982 * @len: the length of the buffer
983 * @dstp: the cpumask to set.
985 * Returns -errno, or 0 for success.
987 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
989 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
993 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
995 * This will eventually be a runtime variable, depending on nr_cpu_ids.
997 static inline size_t cpumask_size(void)
999 /* FIXME: Once all cpumask assignments are eliminated, this
1000 * can be nr_cpumask_bits */
1001 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
1005 * cpumask_var_t: struct cpumask for stack usage.
1007 * Oh, the wicked games we play! In order to make kernel coding a
1008 * little more difficult, we typedef cpumask_var_t to an array or a
1009 * pointer: doing &mask on an array is a noop, so it still works.
1011 * ie.
1012 * cpumask_var_t tmpmask;
1013 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
1014 * return -ENOMEM;
1016 * ... use 'tmpmask' like a normal struct cpumask * ...
1018 * free_cpumask_var(tmpmask);
1020 #ifdef CONFIG_CPUMASK_OFFSTACK
1021 typedef struct cpumask *cpumask_var_t;
1023 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1024 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1025 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1026 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1027 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1028 void free_cpumask_var(cpumask_var_t mask);
1029 void free_bootmem_cpumask_var(cpumask_var_t mask);
1031 #else
1032 typedef struct cpumask cpumask_var_t[1];
1034 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1036 return true;
1039 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1040 int node)
1042 return true;
1045 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1047 cpumask_clear(*mask);
1048 return true;
1051 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1052 int node)
1054 cpumask_clear(*mask);
1055 return true;
1058 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1062 static inline void free_cpumask_var(cpumask_var_t mask)
1066 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1069 #endif /* CONFIG_CPUMASK_OFFSTACK */
1071 /* It's common to want to use cpu_all_mask in struct member initializers,
1072 * so it has to refer to an address rather than a pointer. */
1073 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1074 #define cpu_all_mask to_cpumask(cpu_all_bits)
1076 /* First bits of cpu_bit_bitmap are in fact unset. */
1077 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1079 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1080 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1081 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1083 /* Wrappers for arch boot code to manipulate normally-constant masks */
1084 void set_cpu_possible(unsigned int cpu, bool possible);
1085 void set_cpu_present(unsigned int cpu, bool present);
1086 void set_cpu_online(unsigned int cpu, bool online);
1087 void set_cpu_active(unsigned int cpu, bool active);
1088 void init_cpu_present(const struct cpumask *src);
1089 void init_cpu_possible(const struct cpumask *src);
1090 void init_cpu_online(const struct cpumask *src);
1091 #endif /* __LINUX_CPUMASK_H */