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