pandora: defconfig: update
[pandora-kernel.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.  In general,
7  * only nr_cpu_ids (<= NR_CPUS) bits are valid.
8  */
9 #include <linux/kernel.h>
10 #include <linux/threads.h>
11 #include <linux/bitmap.h>
12
13 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
14
15 /**
16  * cpumask_bits - get the bits in a cpumask
17  * @maskp: the struct cpumask *
18  *
19  * You should only assume nr_cpu_ids bits of this mask are valid.  This is
20  * a macro so it's const-correct.
21  */
22 #define cpumask_bits(maskp) ((maskp)->bits)
23
24 #if NR_CPUS == 1
25 #define nr_cpu_ids              1
26 #else
27 extern int nr_cpu_ids;
28 #endif
29
30 #ifdef CONFIG_CPUMASK_OFFSTACK
31 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
32  * not all bits may be allocated. */
33 #define nr_cpumask_bits nr_cpu_ids
34 #else
35 #define nr_cpumask_bits NR_CPUS
36 #endif
37
38 /*
39  * The following particular system cpumasks and operations manage
40  * possible, present, active and online cpus.
41  *
42  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
43  *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
44  *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
45  *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
46  *
47  *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
48  *
49  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
50  *  that it is possible might ever be plugged in at anytime during the
51  *  life of that system boot.  The cpu_present_mask is dynamic(*),
52  *  representing which CPUs are currently plugged in.  And
53  *  cpu_online_mask is the dynamic subset of cpu_present_mask,
54  *  indicating those CPUs available for scheduling.
55  *
56  *  If HOTPLUG is enabled, then cpu_possible_mask is forced to have
57  *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
58  *  ACPI reports present at boot.
59  *
60  *  If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
61  *  depending on what ACPI reports as currently plugged in, otherwise
62  *  cpu_present_mask is just a copy of cpu_possible_mask.
63  *
64  *  (*) Well, cpu_present_mask is dynamic in the hotplug case.  If not
65  *      hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
66  *
67  * Subtleties:
68  * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
69  *    assumption that their single CPU is online.  The UP
70  *    cpu_{online,possible,present}_masks are placebos.  Changing them
71  *    will have no useful affect on the following num_*_cpus()
72  *    and cpu_*() macros in the UP case.  This ugliness is a UP
73  *    optimization - don't waste any instructions or memory references
74  *    asking if you're online or how many CPUs there are if there is
75  *    only one CPU.
76  */
77
78 extern const struct cpumask *const cpu_possible_mask;
79 extern const struct cpumask *const cpu_online_mask;
80 extern const struct cpumask *const cpu_present_mask;
81 extern const struct cpumask *const cpu_active_mask;
82
83 #if NR_CPUS > 1
84 #define num_online_cpus()       cpumask_weight(cpu_online_mask)
85 #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)
86 #define num_present_cpus()      cpumask_weight(cpu_present_mask)
87 #define num_active_cpus()       cpumask_weight(cpu_active_mask)
88 #define cpu_online(cpu)         cpumask_test_cpu((cpu), cpu_online_mask)
89 #define cpu_possible(cpu)       cpumask_test_cpu((cpu), cpu_possible_mask)
90 #define cpu_present(cpu)        cpumask_test_cpu((cpu), cpu_present_mask)
91 #define cpu_active(cpu)         cpumask_test_cpu((cpu), cpu_active_mask)
92 #else
93 #define num_online_cpus()       1U
94 #define num_possible_cpus()     1U
95 #define num_present_cpus()      1U
96 #define num_active_cpus()       1U
97 #define cpu_online(cpu)         ((cpu) == 0)
98 #define cpu_possible(cpu)       ((cpu) == 0)
99 #define cpu_present(cpu)        ((cpu) == 0)
100 #define cpu_active(cpu)         ((cpu) == 0)
101 #endif
102
103 /* verify cpu argument to cpumask_* operators */
104 static inline unsigned int cpumask_check(unsigned int cpu)
105 {
106 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
107         WARN_ON_ONCE(cpu >= nr_cpumask_bits);
108 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
109         return cpu;
110 }
111
112 #if NR_CPUS == 1
113 /* Uniprocessor.  Assume all masks are "1". */
114 static inline unsigned int cpumask_first(const struct cpumask *srcp)
115 {
116         return 0;
117 }
118
119 /* Valid inputs for n are -1 and 0. */
120 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
121 {
122         return n+1;
123 }
124
125 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
126 {
127         return n+1;
128 }
129
130 static inline unsigned int cpumask_next_and(int n,
131                                             const struct cpumask *srcp,
132                                             const struct cpumask *andp)
133 {
134         return n+1;
135 }
136
137 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
138 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
139                                            unsigned int cpu)
140 {
141         return 1;
142 }
143
144 #define for_each_cpu(cpu, mask)                 \
145         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
146 #define for_each_cpu_not(cpu, mask)             \
147         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
148 #define for_each_cpu_and(cpu, mask, and)        \
149         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
150 #else
151 /**
152  * cpumask_first - get the first cpu in a cpumask
153  * @srcp: the cpumask pointer
154  *
155  * Returns >= nr_cpu_ids if no cpus set.
156  */
157 static inline unsigned int cpumask_first(const struct cpumask *srcp)
158 {
159         return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
160 }
161
162 /**
163  * cpumask_next - get the next cpu in a cpumask
164  * @n: the cpu prior to the place to search (ie. return will be > @n)
165  * @srcp: the cpumask pointer
166  *
167  * Returns >= nr_cpu_ids if no further cpus set.
168  */
169 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
170 {
171         /* -1 is a legal arg here. */
172         if (n != -1)
173                 cpumask_check(n);
174         return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
175 }
176
177 /**
178  * cpumask_next_zero - get the next unset cpu in a cpumask
179  * @n: the cpu prior to the place to search (ie. return will be > @n)
180  * @srcp: the cpumask pointer
181  *
182  * Returns >= nr_cpu_ids if no further cpus unset.
183  */
184 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
185 {
186         /* -1 is a legal arg here. */
187         if (n != -1)
188                 cpumask_check(n);
189         return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
190 }
191
192 int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
193 int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
194
195 /**
196  * for_each_cpu - iterate over every cpu in a mask
197  * @cpu: the (optionally unsigned) integer iterator
198  * @mask: the cpumask pointer
199  *
200  * After the loop, cpu is >= nr_cpu_ids.
201  */
202 #define for_each_cpu(cpu, mask)                         \
203         for ((cpu) = -1;                                \
204                 (cpu) = cpumask_next((cpu), (mask)),    \
205                 (cpu) < nr_cpu_ids;)
206
207 /**
208  * for_each_cpu_not - iterate over every cpu in a complemented mask
209  * @cpu: the (optionally unsigned) integer iterator
210  * @mask: the cpumask pointer
211  *
212  * After the loop, cpu is >= nr_cpu_ids.
213  */
214 #define for_each_cpu_not(cpu, mask)                             \
215         for ((cpu) = -1;                                        \
216                 (cpu) = cpumask_next_zero((cpu), (mask)),       \
217                 (cpu) < nr_cpu_ids;)
218
219 extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
220
221 /**
222  * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
223  * @cpu: the (optionally unsigned) integer iterator
224  * @mask: the cpumask poiter
225  * @start: the start location
226  *
227  * The implementation does not assume any bit in @mask is set (including @start).
228  *
229  * After the loop, cpu is >= nr_cpu_ids.
230  */
231 #define for_each_cpu_wrap(cpu, mask, start)                                     \
232         for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false);      \
233              (cpu) < nr_cpumask_bits;                                           \
234              (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
235
236 /**
237  * for_each_cpu_and - iterate over every cpu in both masks
238  * @cpu: the (optionally unsigned) integer iterator
239  * @mask: the first cpumask pointer
240  * @and: the second cpumask pointer
241  *
242  * This saves a temporary CPU mask in many places.  It is equivalent to:
243  *      struct cpumask tmp;
244  *      cpumask_and(&tmp, &mask, &and);
245  *      for_each_cpu(cpu, &tmp)
246  *              ...
247  *
248  * After the loop, cpu is >= nr_cpu_ids.
249  */
250 #define for_each_cpu_and(cpu, mask, and)                                \
251         for ((cpu) = -1;                                                \
252                 (cpu) = cpumask_next_and((cpu), (mask), (and)),         \
253                 (cpu) < nr_cpu_ids;)
254 #endif /* SMP */
255
256 #define CPU_BITS_NONE                                           \
257 {                                                               \
258         [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL                  \
259 }
260
261 #define CPU_BITS_CPU0                                           \
262 {                                                               \
263         [0] =  1UL                                              \
264 }
265
266 /**
267  * cpumask_set_cpu - set a cpu in a cpumask
268  * @cpu: cpu number (< nr_cpu_ids)
269  * @dstp: the cpumask pointer
270  */
271 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
272 {
273         set_bit(cpumask_check(cpu), cpumask_bits(dstp));
274 }
275
276 /**
277  * cpumask_clear_cpu - clear a cpu in a cpumask
278  * @cpu: cpu number (< nr_cpu_ids)
279  * @dstp: the cpumask pointer
280  */
281 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
282 {
283         clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
284 }
285
286 /**
287  * cpumask_test_cpu - test for a cpu in a cpumask
288  * @cpu: cpu number (< nr_cpu_ids)
289  * @cpumask: the cpumask pointer
290  *
291  * No static inline type checking - see Subtlety (1) above.
292  */
293 #define cpumask_test_cpu(cpu, cpumask) \
294         test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
295
296 /**
297  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
298  * @cpu: cpu number (< nr_cpu_ids)
299  * @cpumask: the cpumask pointer
300  *
301  * test_and_set_bit wrapper for cpumasks.
302  */
303 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
304 {
305         return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
306 }
307
308 /**
309  * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
310  * @cpu: cpu number (< nr_cpu_ids)
311  * @cpumask: the cpumask pointer
312  *
313  * test_and_clear_bit wrapper for cpumasks.
314  */
315 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
316 {
317         return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
318 }
319
320 /**
321  * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
322  * @dstp: the cpumask pointer
323  */
324 static inline void cpumask_setall(struct cpumask *dstp)
325 {
326         bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
327 }
328
329 /**
330  * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
331  * @dstp: the cpumask pointer
332  */
333 static inline void cpumask_clear(struct cpumask *dstp)
334 {
335         bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
336 }
337
338 /**
339  * cpumask_and - *dstp = *src1p & *src2p
340  * @dstp: the cpumask result
341  * @src1p: the first input
342  * @src2p: the second input
343  */
344 static inline int cpumask_and(struct cpumask *dstp,
345                                const struct cpumask *src1p,
346                                const struct cpumask *src2p)
347 {
348         return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
349                                        cpumask_bits(src2p), nr_cpumask_bits);
350 }
351
352 /**
353  * cpumask_or - *dstp = *src1p | *src2p
354  * @dstp: the cpumask result
355  * @src1p: the first input
356  * @src2p: the second input
357  */
358 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
359                               const struct cpumask *src2p)
360 {
361         bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
362                                       cpumask_bits(src2p), nr_cpumask_bits);
363 }
364
365 /**
366  * cpumask_xor - *dstp = *src1p ^ *src2p
367  * @dstp: the cpumask result
368  * @src1p: the first input
369  * @src2p: the second input
370  */
371 static inline void cpumask_xor(struct cpumask *dstp,
372                                const struct cpumask *src1p,
373                                const struct cpumask *src2p)
374 {
375         bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
376                                        cpumask_bits(src2p), nr_cpumask_bits);
377 }
378
379 /**
380  * cpumask_andnot - *dstp = *src1p & ~*src2p
381  * @dstp: the cpumask result
382  * @src1p: the first input
383  * @src2p: the second input
384  */
385 static inline int cpumask_andnot(struct cpumask *dstp,
386                                   const struct cpumask *src1p,
387                                   const struct cpumask *src2p)
388 {
389         return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
390                                           cpumask_bits(src2p), nr_cpumask_bits);
391 }
392
393 /**
394  * cpumask_complement - *dstp = ~*srcp
395  * @dstp: the cpumask result
396  * @srcp: the input to invert
397  */
398 static inline void cpumask_complement(struct cpumask *dstp,
399                                       const struct cpumask *srcp)
400 {
401         bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
402                                               nr_cpumask_bits);
403 }
404
405 /**
406  * cpumask_equal - *src1p == *src2p
407  * @src1p: the first input
408  * @src2p: the second input
409  */
410 static inline bool cpumask_equal(const struct cpumask *src1p,
411                                 const struct cpumask *src2p)
412 {
413         return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
414                                                  nr_cpumask_bits);
415 }
416
417 /**
418  * cpumask_intersects - (*src1p & *src2p) != 0
419  * @src1p: the first input
420  * @src2p: the second input
421  */
422 static inline bool cpumask_intersects(const struct cpumask *src1p,
423                                      const struct cpumask *src2p)
424 {
425         return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
426                                                       nr_cpumask_bits);
427 }
428
429 /**
430  * cpumask_subset - (*src1p & ~*src2p) == 0
431  * @src1p: the first input
432  * @src2p: the second input
433  */
434 static inline int cpumask_subset(const struct cpumask *src1p,
435                                  const struct cpumask *src2p)
436 {
437         return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
438                                                   nr_cpumask_bits);
439 }
440
441 /**
442  * cpumask_empty - *srcp == 0
443  * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
444  */
445 static inline bool cpumask_empty(const struct cpumask *srcp)
446 {
447         return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
448 }
449
450 /**
451  * cpumask_full - *srcp == 0xFFFFFFFF...
452  * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
453  */
454 static inline bool cpumask_full(const struct cpumask *srcp)
455 {
456         return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
457 }
458
459 /**
460  * cpumask_weight - Count of bits in *srcp
461  * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
462  */
463 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
464 {
465         return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
466 }
467
468 /**
469  * cpumask_shift_right - *dstp = *srcp >> n
470  * @dstp: the cpumask result
471  * @srcp: the input to shift
472  * @n: the number of bits to shift by
473  */
474 static inline void cpumask_shift_right(struct cpumask *dstp,
475                                        const struct cpumask *srcp, int n)
476 {
477         bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
478                                                nr_cpumask_bits);
479 }
480
481 /**
482  * cpumask_shift_left - *dstp = *srcp << n
483  * @dstp: the cpumask result
484  * @srcp: the input to shift
485  * @n: the number of bits to shift by
486  */
487 static inline void cpumask_shift_left(struct cpumask *dstp,
488                                       const struct cpumask *srcp, int n)
489 {
490         bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
491                                               nr_cpumask_bits);
492 }
493
494 /**
495  * cpumask_copy - *dstp = *srcp
496  * @dstp: the result
497  * @srcp: the input cpumask
498  */
499 static inline void cpumask_copy(struct cpumask *dstp,
500                                 const struct cpumask *srcp)
501 {
502         bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
503 }
504
505 /**
506  * cpumask_any - pick a "random" cpu from *srcp
507  * @srcp: the input cpumask
508  *
509  * Returns >= nr_cpu_ids if no cpus set.
510  */
511 #define cpumask_any(srcp) cpumask_first(srcp)
512
513 /**
514  * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
515  * @src1p: the first input
516  * @src2p: the second input
517  *
518  * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
519  */
520 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
521
522 /**
523  * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
524  * @mask1: the first input cpumask
525  * @mask2: the second input cpumask
526  *
527  * Returns >= nr_cpu_ids if no cpus set.
528  */
529 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
530
531 /**
532  * cpumask_of - the cpumask containing just a given cpu
533  * @cpu: the cpu (<= nr_cpu_ids)
534  */
535 #define cpumask_of(cpu) (get_cpu_mask(cpu))
536
537 /**
538  * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
539  * @buf: the buffer to sprintf into
540  * @len: the length of the buffer
541  * @srcp: the cpumask to print
542  *
543  * If len is zero, returns zero.  Otherwise returns the length of the
544  * (nul-terminated) @buf string.
545  */
546 static inline int cpumask_scnprintf(char *buf, int len,
547                                     const struct cpumask *srcp)
548 {
549         return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
550 }
551
552 /**
553  * cpumask_parse_user - extract a cpumask from a user string
554  * @buf: the buffer to extract from
555  * @len: the length of the buffer
556  * @dstp: the cpumask to set.
557  *
558  * Returns -errno, or 0 for success.
559  */
560 static inline int cpumask_parse_user(const char __user *buf, int len,
561                                      struct cpumask *dstp)
562 {
563         return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
564 }
565
566 /**
567  * cpumask_parselist_user - extract a cpumask from a user string
568  * @buf: the buffer to extract from
569  * @len: the length of the buffer
570  * @dstp: the cpumask to set.
571  *
572  * Returns -errno, or 0 for success.
573  */
574 static inline int cpumask_parselist_user(const char __user *buf, int len,
575                                      struct cpumask *dstp)
576 {
577         return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
578                                                         nr_cpumask_bits);
579 }
580
581 /**
582  * cpulist_scnprintf - print a cpumask into a string as comma-separated list
583  * @buf: the buffer to sprintf into
584  * @len: the length of the buffer
585  * @srcp: the cpumask to print
586  *
587  * If len is zero, returns zero.  Otherwise returns the length of the
588  * (nul-terminated) @buf string.
589  */
590 static inline int cpulist_scnprintf(char *buf, int len,
591                                     const struct cpumask *srcp)
592 {
593         return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
594                                     nr_cpumask_bits);
595 }
596
597 /**
598  * cpulist_parse_user - extract a cpumask from a user string of ranges
599  * @buf: the buffer to extract from
600  * @len: the length of the buffer
601  * @dstp: the cpumask to set.
602  *
603  * Returns -errno, or 0 for success.
604  */
605 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
606 {
607         return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
608 }
609
610 /**
611  * cpumask_size - size to allocate for a 'struct cpumask' in bytes
612  *
613  * This will eventually be a runtime variable, depending on nr_cpu_ids.
614  */
615 static inline size_t cpumask_size(void)
616 {
617         /* FIXME: Once all cpumask assignments are eliminated, this
618          * can be nr_cpumask_bits */
619         return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
620 }
621
622 /*
623  * cpumask_var_t: struct cpumask for stack usage.
624  *
625  * Oh, the wicked games we play!  In order to make kernel coding a
626  * little more difficult, we typedef cpumask_var_t to an array or a
627  * pointer: doing &mask on an array is a noop, so it still works.
628  *
629  * ie.
630  *      cpumask_var_t tmpmask;
631  *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
632  *              return -ENOMEM;
633  *
634  *        ... use 'tmpmask' like a normal struct cpumask * ...
635  *
636  *      free_cpumask_var(tmpmask);
637  *
638  *
639  * However, one notable exception is there. alloc_cpumask_var() allocates
640  * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
641  * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
642  *
643  *      cpumask_var_t tmpmask;
644  *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
645  *              return -ENOMEM;
646  *
647  *      var = *tmpmask;
648  *
649  * This code makes NR_CPUS length memcopy and brings to a memory corruption.
650  * cpumask_copy() provide safe copy functionality.
651  */
652 #ifdef CONFIG_CPUMASK_OFFSTACK
653 typedef struct cpumask *cpumask_var_t;
654
655 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
656 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
657 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
658 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
659 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
660 void free_cpumask_var(cpumask_var_t mask);
661 void free_bootmem_cpumask_var(cpumask_var_t mask);
662
663 #else
664 typedef struct cpumask cpumask_var_t[1];
665
666 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
667 {
668         return true;
669 }
670
671 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
672                                           int node)
673 {
674         return true;
675 }
676
677 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
678 {
679         cpumask_clear(*mask);
680         return true;
681 }
682
683 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
684                                           int node)
685 {
686         cpumask_clear(*mask);
687         return true;
688 }
689
690 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
691 {
692 }
693
694 static inline void free_cpumask_var(cpumask_var_t mask)
695 {
696 }
697
698 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
699 {
700 }
701 #endif /* CONFIG_CPUMASK_OFFSTACK */
702
703 /* It's common to want to use cpu_all_mask in struct member initializers,
704  * so it has to refer to an address rather than a pointer. */
705 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
706 #define cpu_all_mask to_cpumask(cpu_all_bits)
707
708 /* First bits of cpu_bit_bitmap are in fact unset. */
709 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
710
711 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
712 #define for_each_online_cpu(cpu)   for_each_cpu((cpu), cpu_online_mask)
713 #define for_each_present_cpu(cpu)  for_each_cpu((cpu), cpu_present_mask)
714
715 /* Wrappers for arch boot code to manipulate normally-constant masks */
716 void set_cpu_possible(unsigned int cpu, bool possible);
717 void set_cpu_present(unsigned int cpu, bool present);
718 void set_cpu_online(unsigned int cpu, bool online);
719 void set_cpu_active(unsigned int cpu, bool active);
720 void init_cpu_present(const struct cpumask *src);
721 void init_cpu_possible(const struct cpumask *src);
722 void init_cpu_online(const struct cpumask *src);
723
724 /**
725  * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
726  * @bitmap: the bitmap
727  *
728  * There are a few places where cpumask_var_t isn't appropriate and
729  * static cpumasks must be used (eg. very early boot), yet we don't
730  * expose the definition of 'struct cpumask'.
731  *
732  * This does the conversion, and can be used as a constant initializer.
733  */
734 #define to_cpumask(bitmap)                                              \
735         ((struct cpumask *)(1 ? (bitmap)                                \
736                             : (void *)sizeof(__check_is_bitmap(bitmap))))
737
738 static inline int __check_is_bitmap(const unsigned long *bitmap)
739 {
740         return 1;
741 }
742
743 /*
744  * Special-case data structure for "single bit set only" constant CPU masks.
745  *
746  * We pre-generate all the 64 (or 32) possible bit positions, with enough
747  * padding to the left and the right, and return the constant pointer
748  * appropriately offset.
749  */
750 extern const unsigned long
751         cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
752
753 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
754 {
755         const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
756         p -= cpu / BITS_PER_LONG;
757         return to_cpumask(p);
758 }
759
760 #define cpu_is_offline(cpu)     unlikely(!cpu_online(cpu))
761
762 #if NR_CPUS <= BITS_PER_LONG
763 #define CPU_BITS_ALL                                            \
764 {                                                               \
765         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
766 }
767
768 #else /* NR_CPUS > BITS_PER_LONG */
769
770 #define CPU_BITS_ALL                                            \
771 {                                                               \
772         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                \
773         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD         \
774 }
775 #endif /* NR_CPUS > BITS_PER_LONG */
776
777 /*
778  *
779  * From here down, all obsolete.  Use cpumask_ variants!
780  *
781  */
782 #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
783 /* These strip const, as traditionally they weren't const. */
784 #define cpu_possible_map        (*(cpumask_t *)cpu_possible_mask)
785 #define cpu_online_map          (*(cpumask_t *)cpu_online_mask)
786 #define cpu_present_map         (*(cpumask_t *)cpu_present_mask)
787 #define cpu_active_map          (*(cpumask_t *)cpu_active_mask)
788
789 #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
790
791 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
792
793 #if NR_CPUS <= BITS_PER_LONG
794
795 #define CPU_MASK_ALL                                                    \
796 (cpumask_t) { {                                                         \
797         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
798 } }
799
800 #else
801
802 #define CPU_MASK_ALL                                                    \
803 (cpumask_t) { {                                                         \
804         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
805         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
806 } }
807
808 #endif
809
810 #define CPU_MASK_NONE                                                   \
811 (cpumask_t) { {                                                         \
812         [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
813 } }
814
815 #define CPU_MASK_CPU0                                                   \
816 (cpumask_t) { {                                                         \
817         [0] =  1UL                                                      \
818 } }
819
820 #if NR_CPUS == 1
821 #define first_cpu(src)          ({ (void)(src); 0; })
822 #define next_cpu(n, src)        ({ (void)(src); 1; })
823 #define any_online_cpu(mask)    0
824 #define for_each_cpu_mask(cpu, mask)    \
825         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
826 #else /* NR_CPUS > 1 */
827 int __first_cpu(const cpumask_t *srcp);
828 int __next_cpu(int n, const cpumask_t *srcp);
829 int __any_online_cpu(const cpumask_t *mask);
830
831 #define first_cpu(src)          __first_cpu(&(src))
832 #define next_cpu(n, src)        __next_cpu((n), &(src))
833 #define any_online_cpu(mask) __any_online_cpu(&(mask))
834 #define for_each_cpu_mask(cpu, mask)                    \
835         for ((cpu) = -1;                                \
836                 (cpu) = next_cpu((cpu), (mask)),        \
837                 (cpu) < NR_CPUS; )
838 #endif /* SMP */
839
840 #if NR_CPUS <= 64
841
842 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
843
844 #else /* NR_CPUS > 64 */
845
846 int __next_cpu_nr(int n, const cpumask_t *srcp);
847 #define for_each_cpu_mask_nr(cpu, mask)                 \
848         for ((cpu) = -1;                                \
849                 (cpu) = __next_cpu_nr((cpu), &(mask)),  \
850                 (cpu) < nr_cpu_ids; )
851
852 #endif /* NR_CPUS > 64 */
853
854 #define cpus_addr(src) ((src).bits)
855
856 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
857 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
858 {
859         set_bit(cpu, dstp->bits);
860 }
861
862 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
863 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
864 {
865         clear_bit(cpu, dstp->bits);
866 }
867
868 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
869 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
870 {
871         bitmap_fill(dstp->bits, nbits);
872 }
873
874 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
875 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
876 {
877         bitmap_zero(dstp->bits, nbits);
878 }
879
880 /* No static inline type checking - see Subtlety (1) above. */
881 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
882
883 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
884 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
885 {
886         return test_and_set_bit(cpu, addr->bits);
887 }
888
889 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
890 static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
891                                         const cpumask_t *src2p, int nbits)
892 {
893         return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
894 }
895
896 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
897 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
898                                         const cpumask_t *src2p, int nbits)
899 {
900         bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
901 }
902
903 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
904 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
905                                         const cpumask_t *src2p, int nbits)
906 {
907         bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
908 }
909
910 #define cpus_andnot(dst, src1, src2) \
911                                 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
912 static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
913                                         const cpumask_t *src2p, int nbits)
914 {
915         return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
916 }
917
918 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
919 static inline int __cpus_equal(const cpumask_t *src1p,
920                                         const cpumask_t *src2p, int nbits)
921 {
922         return bitmap_equal(src1p->bits, src2p->bits, nbits);
923 }
924
925 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
926 static inline int __cpus_intersects(const cpumask_t *src1p,
927                                         const cpumask_t *src2p, int nbits)
928 {
929         return bitmap_intersects(src1p->bits, src2p->bits, nbits);
930 }
931
932 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
933 static inline int __cpus_subset(const cpumask_t *src1p,
934                                         const cpumask_t *src2p, int nbits)
935 {
936         return bitmap_subset(src1p->bits, src2p->bits, nbits);
937 }
938
939 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
940 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
941 {
942         return bitmap_empty(srcp->bits, nbits);
943 }
944
945 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
946 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
947 {
948         return bitmap_weight(srcp->bits, nbits);
949 }
950
951 #define cpus_shift_left(dst, src, n) \
952                         __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
953 static inline void __cpus_shift_left(cpumask_t *dstp,
954                                         const cpumask_t *srcp, int n, int nbits)
955 {
956         bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
957 }
958 #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
959
960 #endif /* __LINUX_CPUMASK_H */