]> bbs.cooldavid.org Git - net-next-2.6.git/blob - include/linux/cpumask.h
cpumask: remove unused deprecated functions, avoid accusations of insanity
[net-next-2.6.git] / include / linux / cpumask.h
1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
3
4 /*
5  * Cpumasks provide a bitmap suitable for representing the
6  * set of CPU's in a system, one bit position per CPU number.
7  *
8  * The new cpumask_ ops take a "struct cpumask *"; the old ones
9  * use cpumask_t.
10  *
11  * See detailed comments in the file linux/bitmap.h describing the
12  * data type on which these cpumasks are based.
13  *
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  *
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.
26  *
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  * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32  *
33  * The obsolescent cpumask operations are:
34  *
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
41  *
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
46  *
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
52  *
53  * void cpus_shift_left(dst, src, n)    Shift left
54  *
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
57  *
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
63  *
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
68  *
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
71  *
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
75  *
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)?
79  *
80  * int any_online_cpu(mask)             First online cpu in mask
81  *
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
85  *
86  * Subtlety:
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.
94  */
95
96 #include <linux/kernel.h>
97 #include <linux/threads.h>
98 #include <linux/bitmap.h>
99
100 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
101
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)
105 {
106         set_bit(cpu, dstp->bits);
107 }
108
109 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
110 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
111 {
112         clear_bit(cpu, dstp->bits);
113 }
114
115 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
116 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
117 {
118         bitmap_fill(dstp->bits, nbits);
119 }
120
121 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
122 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
123 {
124         bitmap_zero(dstp->bits, nbits);
125 }
126
127 /* No static inline type checking - see Subtlety (1) above. */
128 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
129
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)
132 {
133         return test_and_set_bit(cpu, addr->bits);
134 }
135
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)
139 {
140         return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
141 }
142
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)
146 {
147         bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
148 }
149
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)
153 {
154         bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
155 }
156
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)
161 {
162         return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
163 }
164
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)
168 {
169         return bitmap_equal(src1p->bits, src2p->bits, nbits);
170 }
171
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)
175 {
176         return bitmap_intersects(src1p->bits, src2p->bits, nbits);
177 }
178
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)
182 {
183         return bitmap_subset(src1p->bits, src2p->bits, nbits);
184 }
185
186 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
187 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
188 {
189         return bitmap_empty(srcp->bits, nbits);
190 }
191
192 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
193 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
194 {
195         return bitmap_weight(srcp->bits, nbits);
196 }
197
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)
202 {
203         bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
204 }
205 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
206
207 /**
208  * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
209  * @bitmap: the bitmap
210  *
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'.
214  *
215  * This does the conversion, and can be used as a constant initializer.
216  */
217 #define to_cpumask(bitmap)                                              \
218         ((struct cpumask *)(1 ? (bitmap)                                \
219                             : (void *)sizeof(__check_is_bitmap(bitmap))))
220
221 static inline int __check_is_bitmap(const unsigned long *bitmap)
222 {
223         return 1;
224 }
225
226 /*
227  * Special-case data structure for "single bit set only" constant CPU masks.
228  *
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.
232  */
233 extern const unsigned long
234         cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
235
236 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
237 {
238         const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
239         p -= cpu / BITS_PER_LONG;
240         return to_cpumask(p);
241 }
242
243 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
244 /*
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
247  * variable created:
248  */
249 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
250
251
252 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
253
254 #if NR_CPUS <= BITS_PER_LONG
255
256 #define CPU_MASK_ALL                                                    \
257 (cpumask_t) { {                                                         \
258         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
259 } }
260
261 #else
262
263 #define CPU_MASK_ALL                                                    \
264 (cpumask_t) { {                                                         \
265         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
266         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
267 } }
268
269 #endif
270
271 #define CPU_MASK_NONE                                                   \
272 (cpumask_t) { {                                                         \
273         [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
274 } }
275
276 #define CPU_MASK_CPU0                                                   \
277 (cpumask_t) { {                                                         \
278         [0] =  1UL                                                      \
279 } }
280
281 #define cpus_addr(src) ((src).bits)
282
283 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
284
285 #if NR_CPUS == 1
286
287 #define nr_cpu_ids              1
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 */
296
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);
302
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)                    \
307         for ((cpu) = -1;                                \
308                 (cpu) = next_cpu((cpu), (mask)),        \
309                 (cpu) < NR_CPUS; )
310 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
311 #endif
312
313 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
314 #if NR_CPUS <= 64
315
316 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
317
318 #else /* NR_CPUS > 64 */
319
320 int __next_cpu_nr(int n, const cpumask_t *srcp);
321 #define for_each_cpu_mask_nr(cpu, mask)                 \
322         for ((cpu) = -1;                                \
323                 (cpu) = __next_cpu_nr((cpu), &(mask)),  \
324                 (cpu) < nr_cpu_ids; )
325
326 #endif /* NR_CPUS > 64 */
327 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
328
329 /*
330  * The following particular system cpumasks and operations manage
331  * possible, present, active and online cpus.
332  *
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
337  *
338  *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
339  *
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.
346  *
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.
350  *
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.
354  *
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.
357  *
358  * Subtleties:
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
366  *    only one CPU.
367  */
368
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;
373
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)
379
380 #if NR_CPUS > 1
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)
388 #else
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)
396 #endif
397
398 #define cpu_is_offline(cpu)     unlikely(!cpu_online(cpu))
399
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)
403
404 #if NR_CPUS <= BITS_PER_LONG
405 #define CPU_BITS_ALL                                            \
406 {                                                               \
407         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
408 }
409
410 #else /* NR_CPUS > BITS_PER_LONG */
411
412 #define CPU_BITS_ALL                                            \
413 {                                                               \
414         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                \
415         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD         \
416 }
417 #endif /* NR_CPUS > BITS_PER_LONG */
418
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
423 #else
424 #define nr_cpumask_bits NR_CPUS
425 #endif
426
427 /* verify cpu argument to cpumask_* operators */
428 static inline unsigned int cpumask_check(unsigned int cpu)
429 {
430 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
431         WARN_ON_ONCE(cpu >= nr_cpumask_bits);
432 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
433         return cpu;
434 }
435
436 #if NR_CPUS == 1
437 /* Uniprocessor.  Assume all masks are "1". */
438 static inline unsigned int cpumask_first(const struct cpumask *srcp)
439 {
440         return 0;
441 }
442
443 /* Valid inputs for n are -1 and 0. */
444 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
445 {
446         return n+1;
447 }
448
449 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
450 {
451         return n+1;
452 }
453
454 static inline unsigned int cpumask_next_and(int n,
455                                             const struct cpumask *srcp,
456                                             const struct cpumask *andp)
457 {
458         return n+1;
459 }
460
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,
463                                            unsigned int cpu)
464 {
465         return 1;
466 }
467
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)
472 #else
473 /**
474  * cpumask_first - get the first cpu in a cpumask
475  * @srcp: the cpumask pointer
476  *
477  * Returns >= nr_cpu_ids if no cpus set.
478  */
479 static inline unsigned int cpumask_first(const struct cpumask *srcp)
480 {
481         return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
482 }
483
484 /**
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
488  *
489  * Returns >= nr_cpu_ids if no further cpus set.
490  */
491 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
492 {
493         /* -1 is a legal arg here. */
494         if (n != -1)
495                 cpumask_check(n);
496         return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
497 }
498
499 /**
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
503  *
504  * Returns >= nr_cpu_ids if no further cpus unset.
505  */
506 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
507 {
508         /* -1 is a legal arg here. */
509         if (n != -1)
510                 cpumask_check(n);
511         return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
512 }
513
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);
516
517 /**
518  * for_each_cpu - iterate over every cpu in a mask
519  * @cpu: the (optionally unsigned) integer iterator
520  * @mask: the cpumask pointer
521  *
522  * After the loop, cpu is >= nr_cpu_ids.
523  */
524 #define for_each_cpu(cpu, mask)                         \
525         for ((cpu) = -1;                                \
526                 (cpu) = cpumask_next((cpu), (mask)),    \
527                 (cpu) < nr_cpu_ids;)
528
529 /**
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
534  *
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)
539  *              ...
540  *
541  * After the loop, cpu is >= nr_cpu_ids.
542  */
543 #define for_each_cpu_and(cpu, mask, and)                                \
544         for ((cpu) = -1;                                                \
545                 (cpu) = cpumask_next_and((cpu), (mask), (and)),         \
546                 (cpu) < nr_cpu_ids;)
547 #endif /* SMP */
548
549 #define CPU_BITS_NONE                                           \
550 {                                                               \
551         [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL                  \
552 }
553
554 #define CPU_BITS_CPU0                                           \
555 {                                                               \
556         [0] =  1UL                                              \
557 }
558
559 /**
560  * cpumask_set_cpu - set a cpu in a cpumask
561  * @cpu: cpu number (< nr_cpu_ids)
562  * @dstp: the cpumask pointer
563  */
564 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
565 {
566         set_bit(cpumask_check(cpu), cpumask_bits(dstp));
567 }
568
569 /**
570  * cpumask_clear_cpu - clear a cpu in a cpumask
571  * @cpu: cpu number (< nr_cpu_ids)
572  * @dstp: the cpumask pointer
573  */
574 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
575 {
576         clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
577 }
578
579 /**
580  * cpumask_test_cpu - test for a cpu in a cpumask
581  * @cpu: cpu number (< nr_cpu_ids)
582  * @cpumask: the cpumask pointer
583  *
584  * No static inline type checking - see Subtlety (1) above.
585  */
586 #define cpumask_test_cpu(cpu, cpumask) \
587         test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
588
589 /**
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
593  *
594  * test_and_set_bit wrapper for cpumasks.
595  */
596 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
597 {
598         return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
599 }
600
601 /**
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
605  *
606  * test_and_clear_bit wrapper for cpumasks.
607  */
608 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
609 {
610         return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
611 }
612
613 /**
614  * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
615  * @dstp: the cpumask pointer
616  */
617 static inline void cpumask_setall(struct cpumask *dstp)
618 {
619         bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
620 }
621
622 /**
623  * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
624  * @dstp: the cpumask pointer
625  */
626 static inline void cpumask_clear(struct cpumask *dstp)
627 {
628         bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
629 }
630
631 /**
632  * cpumask_and - *dstp = *src1p & *src2p
633  * @dstp: the cpumask result
634  * @src1p: the first input
635  * @src2p: the second input
636  */
637 static inline int cpumask_and(struct cpumask *dstp,
638                                const struct cpumask *src1p,
639                                const struct cpumask *src2p)
640 {
641         return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
642                                        cpumask_bits(src2p), nr_cpumask_bits);
643 }
644
645 /**
646  * cpumask_or - *dstp = *src1p | *src2p
647  * @dstp: the cpumask result
648  * @src1p: the first input
649  * @src2p: the second input
650  */
651 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
652                               const struct cpumask *src2p)
653 {
654         bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
655                                       cpumask_bits(src2p), nr_cpumask_bits);
656 }
657
658 /**
659  * cpumask_xor - *dstp = *src1p ^ *src2p
660  * @dstp: the cpumask result
661  * @src1p: the first input
662  * @src2p: the second input
663  */
664 static inline void cpumask_xor(struct cpumask *dstp,
665                                const struct cpumask *src1p,
666                                const struct cpumask *src2p)
667 {
668         bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
669                                        cpumask_bits(src2p), nr_cpumask_bits);
670 }
671
672 /**
673  * cpumask_andnot - *dstp = *src1p & ~*src2p
674  * @dstp: the cpumask result
675  * @src1p: the first input
676  * @src2p: the second input
677  */
678 static inline int cpumask_andnot(struct cpumask *dstp,
679                                   const struct cpumask *src1p,
680                                   const struct cpumask *src2p)
681 {
682         return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
683                                           cpumask_bits(src2p), nr_cpumask_bits);
684 }
685
686 /**
687  * cpumask_complement - *dstp = ~*srcp
688  * @dstp: the cpumask result
689  * @srcp: the input to invert
690  */
691 static inline void cpumask_complement(struct cpumask *dstp,
692                                       const struct cpumask *srcp)
693 {
694         bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
695                                               nr_cpumask_bits);
696 }
697
698 /**
699  * cpumask_equal - *src1p == *src2p
700  * @src1p: the first input
701  * @src2p: the second input
702  */
703 static inline bool cpumask_equal(const struct cpumask *src1p,
704                                 const struct cpumask *src2p)
705 {
706         return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
707                                                  nr_cpumask_bits);
708 }
709
710 /**
711  * cpumask_intersects - (*src1p & *src2p) != 0
712  * @src1p: the first input
713  * @src2p: the second input
714  */
715 static inline bool cpumask_intersects(const struct cpumask *src1p,
716                                      const struct cpumask *src2p)
717 {
718         return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
719                                                       nr_cpumask_bits);
720 }
721
722 /**
723  * cpumask_subset - (*src1p & ~*src2p) == 0
724  * @src1p: the first input
725  * @src2p: the second input
726  */
727 static inline int cpumask_subset(const struct cpumask *src1p,
728                                  const struct cpumask *src2p)
729 {
730         return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
731                                                   nr_cpumask_bits);
732 }
733
734 /**
735  * cpumask_empty - *srcp == 0
736  * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
737  */
738 static inline bool cpumask_empty(const struct cpumask *srcp)
739 {
740         return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
741 }
742
743 /**
744  * cpumask_full - *srcp == 0xFFFFFFFF...
745  * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
746  */
747 static inline bool cpumask_full(const struct cpumask *srcp)
748 {
749         return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
750 }
751
752 /**
753  * cpumask_weight - Count of bits in *srcp
754  * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
755  */
756 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
757 {
758         return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
759 }
760
761 /**
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
766  */
767 static inline void cpumask_shift_right(struct cpumask *dstp,
768                                        const struct cpumask *srcp, int n)
769 {
770         bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
771                                                nr_cpumask_bits);
772 }
773
774 /**
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
779  */
780 static inline void cpumask_shift_left(struct cpumask *dstp,
781                                       const struct cpumask *srcp, int n)
782 {
783         bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
784                                               nr_cpumask_bits);
785 }
786
787 /**
788  * cpumask_copy - *dstp = *srcp
789  * @dstp: the result
790  * @srcp: the input cpumask
791  */
792 static inline void cpumask_copy(struct cpumask *dstp,
793                                 const struct cpumask *srcp)
794 {
795         bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
796 }
797
798 /**
799  * cpumask_any - pick a "random" cpu from *srcp
800  * @srcp: the input cpumask
801  *
802  * Returns >= nr_cpu_ids if no cpus set.
803  */
804 #define cpumask_any(srcp) cpumask_first(srcp)
805
806 /**
807  * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
808  * @src1p: the first input
809  * @src2p: the second input
810  *
811  * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
812  */
813 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
814
815 /**
816  * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
817  * @mask1: the first input cpumask
818  * @mask2: the second input cpumask
819  *
820  * Returns >= nr_cpu_ids if no cpus set.
821  */
822 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
823
824 /**
825  * cpumask_of - the cpumask containing just a given cpu
826  * @cpu: the cpu (<= nr_cpu_ids)
827  */
828 #define cpumask_of(cpu) (get_cpu_mask(cpu))
829
830 /**
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
835  *
836  * If len is zero, returns zero.  Otherwise returns the length of the
837  * (nul-terminated) @buf string.
838  */
839 static inline int cpumask_scnprintf(char *buf, int len,
840                                     const struct cpumask *srcp)
841 {
842         return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
843 }
844
845 /**
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.
850  *
851  * Returns -errno, or 0 for success.
852  */
853 static inline int cpumask_parse_user(const char __user *buf, int len,
854                                      struct cpumask *dstp)
855 {
856         return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
857 }
858
859 /**
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
864  *
865  * If len is zero, returns zero.  Otherwise returns the length of the
866  * (nul-terminated) @buf string.
867  */
868 static inline int cpulist_scnprintf(char *buf, int len,
869                                     const struct cpumask *srcp)
870 {
871         return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
872                                     nr_cpumask_bits);
873 }
874
875 /**
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.
880  *
881  * Returns -errno, or 0 for success.
882  */
883 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
884 {
885         return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
886 }
887
888 /**
889  * cpumask_size - size to allocate for a 'struct cpumask' in bytes
890  *
891  * This will eventually be a runtime variable, depending on nr_cpu_ids.
892  */
893 static inline size_t cpumask_size(void)
894 {
895         /* FIXME: Once all cpumask assignments are eliminated, this
896          * can be nr_cpumask_bits */
897         return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
898 }
899
900 /*
901  * cpumask_var_t: struct cpumask for stack usage.
902  *
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.
906  *
907  * ie.
908  *      cpumask_var_t tmpmask;
909  *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
910  *              return -ENOMEM;
911  *
912  *        ... use 'tmpmask' like a normal struct cpumask * ...
913  *
914  *      free_cpumask_var(tmpmask);
915  */
916 #ifdef CONFIG_CPUMASK_OFFSTACK
917 typedef struct cpumask *cpumask_var_t;
918
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);
926
927 #else
928 typedef struct cpumask cpumask_var_t[1];
929
930 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
931 {
932         return true;
933 }
934
935 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
936                                           int node)
937 {
938         return true;
939 }
940
941 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
942 {
943         cpumask_clear(*mask);
944         return true;
945 }
946
947 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
948                                           int node)
949 {
950         cpumask_clear(*mask);
951         return true;
952 }
953
954 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
955 {
956 }
957
958 static inline void free_cpumask_var(cpumask_var_t mask)
959 {
960 }
961
962 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
963 {
964 }
965 #endif /* CONFIG_CPUMASK_OFFSTACK */
966
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)
971
972 /* First bits of cpu_bit_bitmap are in fact unset. */
973 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
974
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)
978
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 */