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