cpumask: switch over to cpu_online/possible/active/present_mask: core
[linux-2.6] / 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  * 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.
22  *
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.
30  *
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  * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36  *
37  * The obsolescent cpumask operations are:
38  *
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
45  *
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
51  *
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
59  *
60  * void cpus_shift_right(dst, src, n)   Shift right
61  * void cpus_shift_left(dst, src, n)    Shift left
62  *
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
66  *
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
72  *
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.
75  *
76  * The structure should be defined something like this:
77  * struct my_cpumasks {
78  *      cpumask_t mask1;
79  *      cpumask_t mask2;
80  * };
81  *
82  * Usage is then:
83  *      CPUMASK_ALLOC(my_cpumasks);
84  *      CPUMASK_PTR(mask1, my_cpumasks);
85  *      CPUMASK_PTR(mask2, my_cpumasks);
86  *
87  *      --- DO NOT reference cpumask_t pointers until this check ---
88  *      if (my_cpumasks == NULL)
89  *              "kmalloc failed"...
90  *
91  * References are now pointers to the cpumask_t variables (*mask1, ...)
92  *
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  * ------------------------------------------------------------------------
103  *
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
112  *
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
115  *
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
119  *
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)?
123  *
124  * int any_online_cpu(mask)             First online cpu in mask
125  *
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
129  *
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.
138  */
139
140 #include <linux/kernel.h>
141 #include <linux/threads.h>
142 #include <linux/bitmap.h>
143
144 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
145 extern cpumask_t _unused_cpumask_arg_;
146
147 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
148 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
149 {
150         set_bit(cpu, dstp->bits);
151 }
152
153 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
154 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
155 {
156         clear_bit(cpu, dstp->bits);
157 }
158
159 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
160 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
161 {
162         bitmap_fill(dstp->bits, nbits);
163 }
164
165 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
166 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
167 {
168         bitmap_zero(dstp->bits, nbits);
169 }
170
171 /* No static inline type checking - see Subtlety (1) above. */
172 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
173
174 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
175 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
176 {
177         return test_and_set_bit(cpu, addr->bits);
178 }
179
180 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
181 static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
182                                         const cpumask_t *src2p, int nbits)
183 {
184         bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
185 }
186
187 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
188 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
189                                         const cpumask_t *src2p, int nbits)
190 {
191         bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
192 }
193
194 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
195 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
196                                         const cpumask_t *src2p, int nbits)
197 {
198         bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
199 }
200
201 #define cpus_andnot(dst, src1, src2) \
202                                 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
203 static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
204                                         const cpumask_t *src2p, int nbits)
205 {
206         bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
207 }
208
209 #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
210 static inline void __cpus_complement(cpumask_t *dstp,
211                                         const cpumask_t *srcp, int nbits)
212 {
213         bitmap_complement(dstp->bits, srcp->bits, nbits);
214 }
215
216 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
217 static inline int __cpus_equal(const cpumask_t *src1p,
218                                         const cpumask_t *src2p, int nbits)
219 {
220         return bitmap_equal(src1p->bits, src2p->bits, nbits);
221 }
222
223 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
224 static inline int __cpus_intersects(const cpumask_t *src1p,
225                                         const cpumask_t *src2p, int nbits)
226 {
227         return bitmap_intersects(src1p->bits, src2p->bits, nbits);
228 }
229
230 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
231 static inline int __cpus_subset(const cpumask_t *src1p,
232                                         const cpumask_t *src2p, int nbits)
233 {
234         return bitmap_subset(src1p->bits, src2p->bits, nbits);
235 }
236
237 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
238 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
239 {
240         return bitmap_empty(srcp->bits, nbits);
241 }
242
243 #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
244 static inline int __cpus_full(const cpumask_t *srcp, int nbits)
245 {
246         return bitmap_full(srcp->bits, nbits);
247 }
248
249 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
250 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
251 {
252         return bitmap_weight(srcp->bits, nbits);
253 }
254
255 #define cpus_shift_right(dst, src, n) \
256                         __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
257 static inline void __cpus_shift_right(cpumask_t *dstp,
258                                         const cpumask_t *srcp, int n, int nbits)
259 {
260         bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
261 }
262
263 #define cpus_shift_left(dst, src, n) \
264                         __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
265 static inline void __cpus_shift_left(cpumask_t *dstp,
266                                         const cpumask_t *srcp, int n, int nbits)
267 {
268         bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
269 }
270
271 /*
272  * Special-case data structure for "single bit set only" constant CPU masks.
273  *
274  * We pre-generate all the 64 (or 32) possible bit positions, with enough
275  * padding to the left and the right, and return the constant pointer
276  * appropriately offset.
277  */
278 extern const unsigned long
279         cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
280
281 static inline const cpumask_t *get_cpu_mask(unsigned int cpu)
282 {
283         const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
284         p -= cpu / BITS_PER_LONG;
285         return (const cpumask_t *)p;
286 }
287
288 /*
289  * In cases where we take the address of the cpumask immediately,
290  * gcc optimizes it out (it's a constant) and there's no huge stack
291  * variable created:
292  */
293 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
294
295
296 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
297
298 #if NR_CPUS <= BITS_PER_LONG
299
300 #define CPU_MASK_ALL                                                    \
301 (cpumask_t) { {                                                         \
302         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
303 } }
304
305 #define CPU_MASK_ALL_PTR        (&CPU_MASK_ALL)
306
307 #else
308
309 #define CPU_MASK_ALL                                                    \
310 (cpumask_t) { {                                                         \
311         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
312         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
313 } }
314
315 /* cpu_mask_all is in init/main.c */
316 extern cpumask_t cpu_mask_all;
317 #define CPU_MASK_ALL_PTR        (&cpu_mask_all)
318
319 #endif
320
321 #define CPU_MASK_NONE                                                   \
322 (cpumask_t) { {                                                         \
323         [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
324 } }
325
326 #define CPU_MASK_CPU0                                                   \
327 (cpumask_t) { {                                                         \
328         [0] =  1UL                                                      \
329 } }
330
331 #define cpus_addr(src) ((src).bits)
332
333 #if NR_CPUS > BITS_PER_LONG
334 #define CPUMASK_ALLOC(m)        struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
335 #define CPUMASK_FREE(m)         kfree(m)
336 #else
337 #define CPUMASK_ALLOC(m)        struct m _m, *m = &_m
338 #define CPUMASK_FREE(m)
339 #endif
340 #define CPUMASK_PTR(v, m)       cpumask_t *v = &(m->v)
341
342 #define cpu_remap(oldbit, old, new) \
343                 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
344 static inline int __cpu_remap(int oldbit,
345                 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
346 {
347         return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
348 }
349
350 #define cpus_remap(dst, src, old, new) \
351                 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
352 static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
353                 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
354 {
355         bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
356 }
357
358 #define cpus_onto(dst, orig, relmap) \
359                 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
360 static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
361                 const cpumask_t *relmapp, int nbits)
362 {
363         bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
364 }
365
366 #define cpus_fold(dst, orig, sz) \
367                 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
368 static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
369                 int sz, int nbits)
370 {
371         bitmap_fold(dstp->bits, origp->bits, sz, nbits);
372 }
373
374 #if NR_CPUS == 1
375
376 #define nr_cpu_ids              1
377 #define first_cpu(src)          ({ (void)(src); 0; })
378 #define next_cpu(n, src)        ({ (void)(src); 1; })
379 #define any_online_cpu(mask)    0
380 #define for_each_cpu_mask(cpu, mask)    \
381         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
382
383 #else /* NR_CPUS > 1 */
384
385 extern int nr_cpu_ids;
386 int __first_cpu(const cpumask_t *srcp);
387 int __next_cpu(int n, const cpumask_t *srcp);
388 int __any_online_cpu(const cpumask_t *mask);
389
390 #define first_cpu(src)          __first_cpu(&(src))
391 #define next_cpu(n, src)        __next_cpu((n), &(src))
392 #define any_online_cpu(mask) __any_online_cpu(&(mask))
393 #define for_each_cpu_mask(cpu, mask)                    \
394         for ((cpu) = -1;                                \
395                 (cpu) = next_cpu((cpu), (mask)),        \
396                 (cpu) < NR_CPUS; )
397 #endif
398
399 #if NR_CPUS <= 64
400
401 #define next_cpu_nr(n, src)             next_cpu(n, src)
402 #define cpus_weight_nr(cpumask)         cpus_weight(cpumask)
403 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
404
405 #else /* NR_CPUS > 64 */
406
407 int __next_cpu_nr(int n, const cpumask_t *srcp);
408 #define next_cpu_nr(n, src)     __next_cpu_nr((n), &(src))
409 #define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
410 #define for_each_cpu_mask_nr(cpu, mask)                 \
411         for ((cpu) = -1;                                \
412                 (cpu) = next_cpu_nr((cpu), (mask)),     \
413                 (cpu) < nr_cpu_ids; )
414
415 #endif /* NR_CPUS > 64 */
416
417 /*
418  * The following particular system cpumasks and operations manage
419  * possible, present, active and online cpus.
420  *
421  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
422  *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
423  *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
424  *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
425  *
426  *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
427  *
428  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
429  *  that it is possible might ever be plugged in at anytime during the
430  *  life of that system boot.  The cpu_present_mask is dynamic(*),
431  *  representing which CPUs are currently plugged in.  And
432  *  cpu_online_mask is the dynamic subset of cpu_present_mask,
433  *  indicating those CPUs available for scheduling.
434  *
435  *  If HOTPLUG is enabled, then cpu_possible_mask is forced to have
436  *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
437  *  ACPI reports present at boot.
438  *
439  *  If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
440  *  depending on what ACPI reports as currently plugged in, otherwise
441  *  cpu_present_mask is just a copy of cpu_possible_mask.
442  *
443  *  (*) Well, cpu_present_mask is dynamic in the hotplug case.  If not
444  *      hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
445  *
446  * Subtleties:
447  * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
448  *    assumption that their single CPU is online.  The UP
449  *    cpu_{online,possible,present}_masks are placebos.  Changing them
450  *    will have no useful affect on the following num_*_cpus()
451  *    and cpu_*() macros in the UP case.  This ugliness is a UP
452  *    optimization - don't waste any instructions or memory references
453  *    asking if you're online or how many CPUs there are if there is
454  *    only one CPU.
455  */
456
457 extern const struct cpumask *const cpu_possible_mask;
458 extern const struct cpumask *const cpu_online_mask;
459 extern const struct cpumask *const cpu_present_mask;
460 extern const struct cpumask *const cpu_active_mask;
461
462 /* These strip const, as traditionally they weren't const. */
463 #define cpu_possible_map        (*(cpumask_t *)cpu_possible_mask)
464 #define cpu_online_map          (*(cpumask_t *)cpu_online_mask)
465 #define cpu_present_map         (*(cpumask_t *)cpu_present_mask)
466 #define cpu_active_map          (*(cpumask_t *)cpu_active_mask)
467
468 #if NR_CPUS > 1
469 #define num_online_cpus()       cpus_weight_nr(cpu_online_map)
470 #define num_possible_cpus()     cpus_weight_nr(cpu_possible_map)
471 #define num_present_cpus()      cpus_weight_nr(cpu_present_map)
472 #define cpu_online(cpu)         cpu_isset((cpu), cpu_online_map)
473 #define cpu_possible(cpu)       cpu_isset((cpu), cpu_possible_map)
474 #define cpu_present(cpu)        cpu_isset((cpu), cpu_present_map)
475 #define cpu_active(cpu)         cpu_isset((cpu), cpu_active_map)
476 #else
477 #define num_online_cpus()       1
478 #define num_possible_cpus()     1
479 #define num_present_cpus()      1
480 #define cpu_online(cpu)         ((cpu) == 0)
481 #define cpu_possible(cpu)       ((cpu) == 0)
482 #define cpu_present(cpu)        ((cpu) == 0)
483 #define cpu_active(cpu)         ((cpu) == 0)
484 #endif
485
486 #define cpu_is_offline(cpu)     unlikely(!cpu_online(cpu))
487
488 #define for_each_possible_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_possible_map)
489 #define for_each_online_cpu(cpu)   for_each_cpu_mask_nr((cpu), cpu_online_map)
490 #define for_each_present_cpu(cpu)  for_each_cpu_mask_nr((cpu), cpu_present_map)
491
492 /* These are the new versions of the cpumask operators: passed by pointer.
493  * The older versions will be implemented in terms of these, then deleted. */
494 #define cpumask_bits(maskp) ((maskp)->bits)
495
496 #if NR_CPUS <= BITS_PER_LONG
497 #define CPU_BITS_ALL                                            \
498 {                                                               \
499         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
500 }
501
502 #else /* NR_CPUS > BITS_PER_LONG */
503
504 #define CPU_BITS_ALL                                            \
505 {                                                               \
506         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                \
507         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD         \
508 }
509 #endif /* NR_CPUS > BITS_PER_LONG */
510
511 #ifdef CONFIG_CPUMASK_OFFSTACK
512 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
513  * not all bits may be allocated. */
514 #define nr_cpumask_bits nr_cpu_ids
515 #else
516 #define nr_cpumask_bits NR_CPUS
517 #endif
518
519 /* verify cpu argument to cpumask_* operators */
520 static inline unsigned int cpumask_check(unsigned int cpu)
521 {
522 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
523         WARN_ON_ONCE(cpu >= nr_cpumask_bits);
524 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
525         return cpu;
526 }
527
528 #if NR_CPUS == 1
529 /* Uniprocessor.  Assume all masks are "1". */
530 static inline unsigned int cpumask_first(const struct cpumask *srcp)
531 {
532         return 0;
533 }
534
535 /* Valid inputs for n are -1 and 0. */
536 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
537 {
538         return n+1;
539 }
540
541 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
542 {
543         return n+1;
544 }
545
546 static inline unsigned int cpumask_next_and(int n,
547                                             const struct cpumask *srcp,
548                                             const struct cpumask *andp)
549 {
550         return n+1;
551 }
552
553 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
554 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
555                                            unsigned int cpu)
556 {
557         return 1;
558 }
559
560 #define for_each_cpu(cpu, mask)                 \
561         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
562 #define for_each_cpu_and(cpu, mask, and)        \
563         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
564 #else
565 /**
566  * cpumask_first - get the first cpu in a cpumask
567  * @srcp: the cpumask pointer
568  *
569  * Returns >= nr_cpu_ids if no cpus set.
570  */
571 static inline unsigned int cpumask_first(const struct cpumask *srcp)
572 {
573         return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
574 }
575
576 /**
577  * cpumask_next - get the next cpu in a cpumask
578  * @n: the cpu prior to the place to search (ie. return will be > @n)
579  * @srcp: the cpumask pointer
580  *
581  * Returns >= nr_cpu_ids if no further cpus set.
582  */
583 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
584 {
585         /* -1 is a legal arg here. */
586         if (n != -1)
587                 cpumask_check(n);
588         return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
589 }
590
591 /**
592  * cpumask_next_zero - get the next unset cpu in a cpumask
593  * @n: the cpu prior to the place to search (ie. return will be > @n)
594  * @srcp: the cpumask pointer
595  *
596  * Returns >= nr_cpu_ids if no further cpus unset.
597  */
598 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
599 {
600         /* -1 is a legal arg here. */
601         if (n != -1)
602                 cpumask_check(n);
603         return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
604 }
605
606 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
607 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
608
609 /**
610  * for_each_cpu - iterate over every cpu in a mask
611  * @cpu: the (optionally unsigned) integer iterator
612  * @mask: the cpumask pointer
613  *
614  * After the loop, cpu is >= nr_cpu_ids.
615  */
616 #define for_each_cpu(cpu, mask)                         \
617         for ((cpu) = -1;                                \
618                 (cpu) = cpumask_next((cpu), (mask)),    \
619                 (cpu) < nr_cpu_ids;)
620
621 /**
622  * for_each_cpu_and - iterate over every cpu in both masks
623  * @cpu: the (optionally unsigned) integer iterator
624  * @mask: the first cpumask pointer
625  * @and: the second cpumask pointer
626  *
627  * This saves a temporary CPU mask in many places.  It is equivalent to:
628  *      struct cpumask tmp;
629  *      cpumask_and(&tmp, &mask, &and);
630  *      for_each_cpu(cpu, &tmp)
631  *              ...
632  *
633  * After the loop, cpu is >= nr_cpu_ids.
634  */
635 #define for_each_cpu_and(cpu, mask, and)                                \
636         for ((cpu) = -1;                                                \
637                 (cpu) = cpumask_next_and((cpu), (mask), (and)),         \
638                 (cpu) < nr_cpu_ids;)
639 #endif /* SMP */
640
641 #define CPU_BITS_NONE                                           \
642 {                                                               \
643         [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL                  \
644 }
645
646 #define CPU_BITS_CPU0                                           \
647 {                                                               \
648         [0] =  1UL                                              \
649 }
650
651 /**
652  * cpumask_set_cpu - set a cpu in a cpumask
653  * @cpu: cpu number (< nr_cpu_ids)
654  * @dstp: the cpumask pointer
655  */
656 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
657 {
658         set_bit(cpumask_check(cpu), cpumask_bits(dstp));
659 }
660
661 /**
662  * cpumask_clear_cpu - clear a cpu in a cpumask
663  * @cpu: cpu number (< nr_cpu_ids)
664  * @dstp: the cpumask pointer
665  */
666 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
667 {
668         clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
669 }
670
671 /**
672  * cpumask_test_cpu - test for a cpu in a cpumask
673  * @cpu: cpu number (< nr_cpu_ids)
674  * @cpumask: the cpumask pointer
675  *
676  * No static inline type checking - see Subtlety (1) above.
677  */
678 #define cpumask_test_cpu(cpu, cpumask) \
679         test_bit(cpumask_check(cpu), (cpumask)->bits)
680
681 /**
682  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
683  * @cpu: cpu number (< nr_cpu_ids)
684  * @cpumask: the cpumask pointer
685  *
686  * test_and_set_bit wrapper for cpumasks.
687  */
688 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
689 {
690         return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
691 }
692
693 /**
694  * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
695  * @dstp: the cpumask pointer
696  */
697 static inline void cpumask_setall(struct cpumask *dstp)
698 {
699         bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
700 }
701
702 /**
703  * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
704  * @dstp: the cpumask pointer
705  */
706 static inline void cpumask_clear(struct cpumask *dstp)
707 {
708         bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
709 }
710
711 /**
712  * cpumask_and - *dstp = *src1p & *src2p
713  * @dstp: the cpumask result
714  * @src1p: the first input
715  * @src2p: the second input
716  */
717 static inline void cpumask_and(struct cpumask *dstp,
718                                const struct cpumask *src1p,
719                                const struct cpumask *src2p)
720 {
721         bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
722                                        cpumask_bits(src2p), nr_cpumask_bits);
723 }
724
725 /**
726  * cpumask_or - *dstp = *src1p | *src2p
727  * @dstp: the cpumask result
728  * @src1p: the first input
729  * @src2p: the second input
730  */
731 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
732                               const struct cpumask *src2p)
733 {
734         bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
735                                       cpumask_bits(src2p), nr_cpumask_bits);
736 }
737
738 /**
739  * cpumask_xor - *dstp = *src1p ^ *src2p
740  * @dstp: the cpumask result
741  * @src1p: the first input
742  * @src2p: the second input
743  */
744 static inline void cpumask_xor(struct cpumask *dstp,
745                                const struct cpumask *src1p,
746                                const struct cpumask *src2p)
747 {
748         bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
749                                        cpumask_bits(src2p), nr_cpumask_bits);
750 }
751
752 /**
753  * cpumask_andnot - *dstp = *src1p & ~*src2p
754  * @dstp: the cpumask result
755  * @src1p: the first input
756  * @src2p: the second input
757  */
758 static inline void cpumask_andnot(struct cpumask *dstp,
759                                   const struct cpumask *src1p,
760                                   const struct cpumask *src2p)
761 {
762         bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
763                                           cpumask_bits(src2p), nr_cpumask_bits);
764 }
765
766 /**
767  * cpumask_complement - *dstp = ~*srcp
768  * @dstp: the cpumask result
769  * @srcp: the input to invert
770  */
771 static inline void cpumask_complement(struct cpumask *dstp,
772                                       const struct cpumask *srcp)
773 {
774         bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
775                                               nr_cpumask_bits);
776 }
777
778 /**
779  * cpumask_equal - *src1p == *src2p
780  * @src1p: the first input
781  * @src2p: the second input
782  */
783 static inline bool cpumask_equal(const struct cpumask *src1p,
784                                 const struct cpumask *src2p)
785 {
786         return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
787                                                  nr_cpumask_bits);
788 }
789
790 /**
791  * cpumask_intersects - (*src1p & *src2p) != 0
792  * @src1p: the first input
793  * @src2p: the second input
794  */
795 static inline bool cpumask_intersects(const struct cpumask *src1p,
796                                      const struct cpumask *src2p)
797 {
798         return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
799                                                       nr_cpumask_bits);
800 }
801
802 /**
803  * cpumask_subset - (*src1p & ~*src2p) == 0
804  * @src1p: the first input
805  * @src2p: the second input
806  */
807 static inline int cpumask_subset(const struct cpumask *src1p,
808                                  const struct cpumask *src2p)
809 {
810         return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
811                                                   nr_cpumask_bits);
812 }
813
814 /**
815  * cpumask_empty - *srcp == 0
816  * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
817  */
818 static inline bool cpumask_empty(const struct cpumask *srcp)
819 {
820         return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
821 }
822
823 /**
824  * cpumask_full - *srcp == 0xFFFFFFFF...
825  * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
826  */
827 static inline bool cpumask_full(const struct cpumask *srcp)
828 {
829         return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
830 }
831
832 /**
833  * cpumask_weight - Count of bits in *srcp
834  * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
835  */
836 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
837 {
838         return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
839 }
840
841 /**
842  * cpumask_shift_right - *dstp = *srcp >> n
843  * @dstp: the cpumask result
844  * @srcp: the input to shift
845  * @n: the number of bits to shift by
846  */
847 static inline void cpumask_shift_right(struct cpumask *dstp,
848                                        const struct cpumask *srcp, int n)
849 {
850         bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
851                                                nr_cpumask_bits);
852 }
853
854 /**
855  * cpumask_shift_left - *dstp = *srcp << n
856  * @dstp: the cpumask result
857  * @srcp: the input to shift
858  * @n: the number of bits to shift by
859  */
860 static inline void cpumask_shift_left(struct cpumask *dstp,
861                                       const struct cpumask *srcp, int n)
862 {
863         bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
864                                               nr_cpumask_bits);
865 }
866
867 /**
868  * cpumask_copy - *dstp = *srcp
869  * @dstp: the result
870  * @srcp: the input cpumask
871  */
872 static inline void cpumask_copy(struct cpumask *dstp,
873                                 const struct cpumask *srcp)
874 {
875         bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
876 }
877
878 /**
879  * cpumask_any - pick a "random" cpu from *srcp
880  * @srcp: the input cpumask
881  *
882  * Returns >= nr_cpu_ids if no cpus set.
883  */
884 #define cpumask_any(srcp) cpumask_first(srcp)
885
886 /**
887  * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
888  * @src1p: the first input
889  * @src2p: the second input
890  *
891  * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
892  */
893 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
894
895 /**
896  * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
897  * @mask1: the first input cpumask
898  * @mask2: the second input cpumask
899  *
900  * Returns >= nr_cpu_ids if no cpus set.
901  */
902 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
903
904 /**
905  * cpumask_of - the cpumask containing just a given cpu
906  * @cpu: the cpu (<= nr_cpu_ids)
907  */
908 #define cpumask_of(cpu) (get_cpu_mask(cpu))
909
910 /**
911  * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
912  * @buf: the buffer to sprintf into
913  * @len: the length of the buffer
914  * @srcp: the cpumask to print
915  *
916  * If len is zero, returns zero.  Otherwise returns the length of the
917  * (nul-terminated) @buf string.
918  */
919 static inline int cpumask_scnprintf(char *buf, int len,
920                                     const struct cpumask *srcp)
921 {
922         return bitmap_scnprintf(buf, len, srcp->bits, nr_cpumask_bits);
923 }
924
925 /**
926  * cpumask_parse_user - extract a cpumask from a user string
927  * @buf: the buffer to extract from
928  * @len: the length of the buffer
929  * @dstp: the cpumask to set.
930  *
931  * Returns -errno, or 0 for success.
932  */
933 static inline int cpumask_parse_user(const char __user *buf, int len,
934                                      struct cpumask *dstp)
935 {
936         return bitmap_parse_user(buf, len, dstp->bits, nr_cpumask_bits);
937 }
938
939 /**
940  * cpulist_scnprintf - print a cpumask into a string as comma-separated list
941  * @buf: the buffer to sprintf into
942  * @len: the length of the buffer
943  * @srcp: the cpumask to print
944  *
945  * If len is zero, returns zero.  Otherwise returns the length of the
946  * (nul-terminated) @buf string.
947  */
948 static inline int cpulist_scnprintf(char *buf, int len,
949                                     const struct cpumask *srcp)
950 {
951         return bitmap_scnlistprintf(buf, len, srcp->bits, nr_cpumask_bits);
952 }
953
954 /**
955  * cpulist_parse_user - extract a cpumask from a user string of ranges
956  * @buf: the buffer to extract from
957  * @len: the length of the buffer
958  * @dstp: the cpumask to set.
959  *
960  * Returns -errno, or 0 for success.
961  */
962 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
963 {
964         return bitmap_parselist(buf, dstp->bits, nr_cpumask_bits);
965 }
966
967 /**
968  * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
969  * @bitmap: the bitmap
970  *
971  * There are a few places where cpumask_var_t isn't appropriate and
972  * static cpumasks must be used (eg. very early boot), yet we don't
973  * expose the definition of 'struct cpumask'.
974  *
975  * This does the conversion, and can be used as a constant initializer.
976  */
977 #define to_cpumask(bitmap)                                              \
978         ((struct cpumask *)(1 ? (bitmap)                                \
979                             : (void *)sizeof(__check_is_bitmap(bitmap))))
980
981 static inline int __check_is_bitmap(const unsigned long *bitmap)
982 {
983         return 1;
984 }
985
986 /**
987  * cpumask_size - size to allocate for a 'struct cpumask' in bytes
988  *
989  * This will eventually be a runtime variable, depending on nr_cpu_ids.
990  */
991 static inline size_t cpumask_size(void)
992 {
993         /* FIXME: Once all cpumask assignments are eliminated, this
994          * can be nr_cpumask_bits */
995         return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
996 }
997
998 /*
999  * cpumask_var_t: struct cpumask for stack usage.
1000  *
1001  * Oh, the wicked games we play!  In order to make kernel coding a
1002  * little more difficult, we typedef cpumask_var_t to an array or a
1003  * pointer: doing &mask on an array is a noop, so it still works.
1004  *
1005  * ie.
1006  *      cpumask_var_t tmpmask;
1007  *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
1008  *              return -ENOMEM;
1009  *
1010  *        ... use 'tmpmask' like a normal struct cpumask * ...
1011  *
1012  *      free_cpumask_var(tmpmask);
1013  */
1014 #ifdef CONFIG_CPUMASK_OFFSTACK
1015 typedef struct cpumask *cpumask_var_t;
1016
1017 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1018 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1019 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1020 void free_cpumask_var(cpumask_var_t mask);
1021 void free_bootmem_cpumask_var(cpumask_var_t mask);
1022
1023 #else
1024 typedef struct cpumask cpumask_var_t[1];
1025
1026 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1027 {
1028         return true;
1029 }
1030
1031 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1032                                           int node)
1033 {
1034         return true;
1035 }
1036
1037 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1038 {
1039 }
1040
1041 static inline void free_cpumask_var(cpumask_var_t mask)
1042 {
1043 }
1044
1045 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1046 {
1047 }
1048 #endif /* CONFIG_CPUMASK_OFFSTACK */
1049
1050 /* It's common to want to use cpu_all_mask in struct member initializers,
1051  * so it has to refer to an address rather than a pointer. */
1052 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1053 #define cpu_all_mask to_cpumask(cpu_all_bits)
1054
1055 /* First bits of cpu_bit_bitmap are in fact unset. */
1056 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1057
1058 /* Wrappers for arch boot code to manipulate normally-constant masks */
1059 static inline void set_cpu_possible(unsigned int cpu, bool possible)
1060 {
1061         if (possible)
1062                 cpumask_set_cpu(cpu, &cpu_possible_map);
1063         else
1064                 cpumask_clear_cpu(cpu, &cpu_possible_map);
1065 }
1066
1067 static inline void set_cpu_present(unsigned int cpu, bool present)
1068 {
1069         if (present)
1070                 cpumask_set_cpu(cpu, &cpu_present_map);
1071         else
1072                 cpumask_clear_cpu(cpu, &cpu_present_map);
1073 }
1074
1075 static inline void set_cpu_online(unsigned int cpu, bool online)
1076 {
1077         if (online)
1078                 cpumask_set_cpu(cpu, &cpu_online_map);
1079         else
1080                 cpumask_clear_cpu(cpu, &cpu_online_map);
1081 }
1082
1083 static inline void set_cpu_active(unsigned int cpu, bool active)
1084 {
1085         if (active)
1086                 cpumask_set_cpu(cpu, &cpu_active_map);
1087         else
1088                 cpumask_clear_cpu(cpu, &cpu_active_map);
1089 }
1090
1091 static inline void init_cpu_present(const struct cpumask *src)
1092 {
1093         cpumask_copy(&cpu_present_map, src);
1094 }
1095
1096 static inline void init_cpu_possible(const struct cpumask *src)
1097 {
1098         cpumask_copy(&cpu_possible_map, src);
1099 }
1100
1101 static inline void init_cpu_online(const struct cpumask *src)
1102 {
1103         cpumask_copy(&cpu_online_map, src);
1104 }
1105 #endif /* __LINUX_CPUMASK_H */