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