1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_CPUMASK_H 3 #define __LINUX_CPUMASK_H 4 5 /* 6 * Cpumasks provide a bitmap suitable for representing the 7 * set of CPU's in a system, one bit position per CPU number. In general, 8 * only nr_cpu_ids (<= NR_CPUS) bits are valid. 9 */ 10 #include <linux/kernel.h> 11 #include <linux/threads.h> 12 #include <linux/bitmap.h> 13 #include <linux/atomic.h> 14 #include <linux/bug.h> 15 #include <linux/gfp_types.h> 16 #include <linux/numa.h> 17 18 /* Don't assign or return these: may not be this big! */ 19 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; 20 21 /** 22 * cpumask_bits - get the bits in a cpumask 23 * @maskp: the struct cpumask * 24 * 25 * You should only assume nr_cpu_ids bits of this mask are valid. This is 26 * a macro so it's const-correct. 27 */ 28 #define cpumask_bits(maskp) ((maskp)->bits) 29 30 /** 31 * cpumask_pr_args - printf args to output a cpumask 32 * @maskp: cpumask to be printed 33 * 34 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask. 35 */ 36 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp) 37 38 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS) 39 #define nr_cpu_ids ((unsigned int)NR_CPUS) 40 #else 41 extern unsigned int nr_cpu_ids; 42 #endif 43 44 static inline void set_nr_cpu_ids(unsigned int nr) 45 { 46 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS) 47 WARN_ON(nr != nr_cpu_ids); 48 #else 49 nr_cpu_ids = nr; 50 #endif 51 } 52 53 /* 54 * We have several different "preferred sizes" for the cpumask 55 * operations, depending on operation. 56 * 57 * For example, the bitmap scanning and operating operations have 58 * optimized routines that work for the single-word case, but only when 59 * the size is constant. So if NR_CPUS fits in one single word, we are 60 * better off using that small constant, in order to trigger the 61 * optimized bit finding. That is 'small_cpumask_size'. 62 * 63 * The clearing and copying operations will similarly perform better 64 * with a constant size, but we limit that size arbitrarily to four 65 * words. We call this 'large_cpumask_size'. 66 * 67 * Finally, some operations just want the exact limit, either because 68 * they set bits or just don't have any faster fixed-sized versions. We 69 * call this just 'nr_cpumask_bits'. 70 * 71 * Note that these optional constants are always guaranteed to be at 72 * least as big as 'nr_cpu_ids' itself is, and all our cpumask 73 * allocations are at least that size (see cpumask_size()). The 74 * optimization comes from being able to potentially use a compile-time 75 * constant instead of a run-time generated exact number of CPUs. 76 */ 77 #if NR_CPUS <= BITS_PER_LONG 78 #define small_cpumask_bits ((unsigned int)NR_CPUS) 79 #define large_cpumask_bits ((unsigned int)NR_CPUS) 80 #elif NR_CPUS <= 4*BITS_PER_LONG 81 #define small_cpumask_bits nr_cpu_ids 82 #define large_cpumask_bits ((unsigned int)NR_CPUS) 83 #else 84 #define small_cpumask_bits nr_cpu_ids 85 #define large_cpumask_bits nr_cpu_ids 86 #endif 87 #define nr_cpumask_bits nr_cpu_ids 88 89 /* 90 * The following particular system cpumasks and operations manage 91 * possible, present, active and online cpus. 92 * 93 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable 94 * cpu_present_mask - has bit 'cpu' set iff cpu is populated 95 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler 96 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration 97 * 98 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online. 99 * 100 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's 101 * that it is possible might ever be plugged in at anytime during the 102 * life of that system boot. The cpu_present_mask is dynamic(*), 103 * representing which CPUs are currently plugged in. And 104 * cpu_online_mask is the dynamic subset of cpu_present_mask, 105 * indicating those CPUs available for scheduling. 106 * 107 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically, 108 * depending on what ACPI reports as currently plugged in, otherwise 109 * cpu_present_mask is just a copy of cpu_possible_mask. 110 * 111 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not 112 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot. 113 * 114 * Subtleties: 115 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode 116 * assumption that their single CPU is online. The UP 117 * cpu_{online,possible,present}_masks are placebos. Changing them 118 * will have no useful affect on the following num_*_cpus() 119 * and cpu_*() macros in the UP case. This ugliness is a UP 120 * optimization - don't waste any instructions or memory references 121 * asking if you're online or how many CPUs there are if there is 122 * only one CPU. 123 */ 124 125 extern struct cpumask __cpu_possible_mask; 126 extern struct cpumask __cpu_online_mask; 127 extern struct cpumask __cpu_present_mask; 128 extern struct cpumask __cpu_active_mask; 129 extern struct cpumask __cpu_dying_mask; 130 #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask) 131 #define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask) 132 #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask) 133 #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask) 134 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask) 135 136 extern atomic_t __num_online_cpus; 137 138 extern cpumask_t cpus_booted_once_mask; 139 140 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits) 141 { 142 #ifdef CONFIG_DEBUG_PER_CPU_MAPS 143 WARN_ON_ONCE(cpu >= bits); 144 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */ 145 } 146 147 /* verify cpu argument to cpumask_* operators */ 148 static __always_inline unsigned int cpumask_check(unsigned int cpu) 149 { 150 cpu_max_bits_warn(cpu, small_cpumask_bits); 151 return cpu; 152 } 153 154 /** 155 * cpumask_first - get the first cpu in a cpumask 156 * @srcp: the cpumask pointer 157 * 158 * Returns >= nr_cpu_ids if no cpus set. 159 */ 160 static inline unsigned int cpumask_first(const struct cpumask *srcp) 161 { 162 return find_first_bit(cpumask_bits(srcp), small_cpumask_bits); 163 } 164 165 /** 166 * cpumask_first_zero - get the first unset cpu in a cpumask 167 * @srcp: the cpumask pointer 168 * 169 * Returns >= nr_cpu_ids if all cpus are set. 170 */ 171 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp) 172 { 173 return find_first_zero_bit(cpumask_bits(srcp), small_cpumask_bits); 174 } 175 176 /** 177 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2 178 * @src1p: the first input 179 * @src2p: the second input 180 * 181 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and(). 182 */ 183 static inline 184 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2) 185 { 186 return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits); 187 } 188 189 /** 190 * cpumask_last - get the last CPU in a cpumask 191 * @srcp: - the cpumask pointer 192 * 193 * Returns >= nr_cpumask_bits if no CPUs set. 194 */ 195 static inline unsigned int cpumask_last(const struct cpumask *srcp) 196 { 197 return find_last_bit(cpumask_bits(srcp), small_cpumask_bits); 198 } 199 200 /** 201 * cpumask_next - get the next cpu in a cpumask 202 * @n: the cpu prior to the place to search (ie. return will be > @n) 203 * @srcp: the cpumask pointer 204 * 205 * Returns >= nr_cpu_ids if no further cpus set. 206 */ 207 static inline 208 unsigned int cpumask_next(int n, const struct cpumask *srcp) 209 { 210 /* -1 is a legal arg here. */ 211 if (n != -1) 212 cpumask_check(n); 213 return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1); 214 } 215 216 /** 217 * cpumask_next_zero - get the next unset cpu in a cpumask 218 * @n: the cpu prior to the place to search (ie. return will be > @n) 219 * @srcp: the cpumask pointer 220 * 221 * Returns >= nr_cpu_ids if no further cpus unset. 222 */ 223 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp) 224 { 225 /* -1 is a legal arg here. */ 226 if (n != -1) 227 cpumask_check(n); 228 return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1); 229 } 230 231 #if NR_CPUS == 1 232 /* Uniprocessor: there is only one valid CPU */ 233 static inline unsigned int cpumask_local_spread(unsigned int i, int node) 234 { 235 return 0; 236 } 237 238 static inline unsigned int cpumask_any_and_distribute(const struct cpumask *src1p, 239 const struct cpumask *src2p) 240 { 241 return cpumask_first_and(src1p, src2p); 242 } 243 244 static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp) 245 { 246 return cpumask_first(srcp); 247 } 248 #else 249 unsigned int cpumask_local_spread(unsigned int i, int node); 250 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p, 251 const struct cpumask *src2p); 252 unsigned int cpumask_any_distribute(const struct cpumask *srcp); 253 #endif /* NR_CPUS */ 254 255 /** 256 * cpumask_next_and - get the next cpu in *src1p & *src2p 257 * @n: the cpu prior to the place to search (ie. return will be > @n) 258 * @src1p: the first cpumask pointer 259 * @src2p: the second cpumask pointer 260 * 261 * Returns >= nr_cpu_ids if no further cpus set in both. 262 */ 263 static inline 264 unsigned int cpumask_next_and(int n, const struct cpumask *src1p, 265 const struct cpumask *src2p) 266 { 267 /* -1 is a legal arg here. */ 268 if (n != -1) 269 cpumask_check(n); 270 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p), 271 small_cpumask_bits, n + 1); 272 } 273 274 /** 275 * for_each_cpu - iterate over every cpu in a mask 276 * @cpu: the (optionally unsigned) integer iterator 277 * @mask: the cpumask pointer 278 * 279 * After the loop, cpu is >= nr_cpu_ids. 280 */ 281 #define for_each_cpu(cpu, mask) \ 282 for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits) 283 284 #if NR_CPUS == 1 285 static inline 286 unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap) 287 { 288 cpumask_check(start); 289 if (n != -1) 290 cpumask_check(n); 291 292 /* 293 * Return the first available CPU when wrapping, or when starting before cpu0, 294 * since there is only one valid option. 295 */ 296 if (wrap && n >= 0) 297 return nr_cpumask_bits; 298 299 return cpumask_first(mask); 300 } 301 #else 302 unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap); 303 #endif 304 305 /** 306 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location 307 * @cpu: the (optionally unsigned) integer iterator 308 * @mask: the cpumask pointer 309 * @start: the start location 310 * 311 * The implementation does not assume any bit in @mask is set (including @start). 312 * 313 * After the loop, cpu is >= nr_cpu_ids. 314 */ 315 #define for_each_cpu_wrap(cpu, mask, start) \ 316 for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start) 317 318 /** 319 * for_each_cpu_and - iterate over every cpu in both masks 320 * @cpu: the (optionally unsigned) integer iterator 321 * @mask1: the first cpumask pointer 322 * @mask2: the second cpumask pointer 323 * 324 * This saves a temporary CPU mask in many places. It is equivalent to: 325 * struct cpumask tmp; 326 * cpumask_and(&tmp, &mask1, &mask2); 327 * for_each_cpu(cpu, &tmp) 328 * ... 329 * 330 * After the loop, cpu is >= nr_cpu_ids. 331 */ 332 #define for_each_cpu_and(cpu, mask1, mask2) \ 333 for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits) 334 335 /** 336 * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding 337 * those present in another. 338 * @cpu: the (optionally unsigned) integer iterator 339 * @mask1: the first cpumask pointer 340 * @mask2: the second cpumask pointer 341 * 342 * This saves a temporary CPU mask in many places. It is equivalent to: 343 * struct cpumask tmp; 344 * cpumask_andnot(&tmp, &mask1, &mask2); 345 * for_each_cpu(cpu, &tmp) 346 * ... 347 * 348 * After the loop, cpu is >= nr_cpu_ids. 349 */ 350 #define for_each_cpu_andnot(cpu, mask1, mask2) \ 351 for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits) 352 353 /** 354 * cpumask_any_but - return a "random" in a cpumask, but not this one. 355 * @mask: the cpumask to search 356 * @cpu: the cpu to ignore. 357 * 358 * Often used to find any cpu but smp_processor_id() in a mask. 359 * Returns >= nr_cpu_ids if no cpus set. 360 */ 361 static inline 362 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) 363 { 364 unsigned int i; 365 366 cpumask_check(cpu); 367 for_each_cpu(i, mask) 368 if (i != cpu) 369 break; 370 return i; 371 } 372 373 /** 374 * cpumask_nth - get the first cpu in a cpumask 375 * @srcp: the cpumask pointer 376 * @cpu: the N'th cpu to find, starting from 0 377 * 378 * Returns >= nr_cpu_ids if such cpu doesn't exist. 379 */ 380 static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp) 381 { 382 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu)); 383 } 384 385 /** 386 * cpumask_nth_and - get the first cpu in 2 cpumasks 387 * @srcp1: the cpumask pointer 388 * @srcp2: the cpumask pointer 389 * @cpu: the N'th cpu to find, starting from 0 390 * 391 * Returns >= nr_cpu_ids if such cpu doesn't exist. 392 */ 393 static inline 394 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1, 395 const struct cpumask *srcp2) 396 { 397 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), 398 small_cpumask_bits, cpumask_check(cpu)); 399 } 400 401 /** 402 * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd. 403 * @srcp1: the cpumask pointer 404 * @srcp2: the cpumask pointer 405 * @cpu: the N'th cpu to find, starting from 0 406 * 407 * Returns >= nr_cpu_ids if such cpu doesn't exist. 408 */ 409 static inline 410 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1, 411 const struct cpumask *srcp2) 412 { 413 return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), 414 small_cpumask_bits, cpumask_check(cpu)); 415 } 416 417 /** 418 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd. 419 * @srcp1: the cpumask pointer 420 * @srcp2: the cpumask pointer 421 * @srcp3: the cpumask pointer 422 * @cpu: the N'th cpu to find, starting from 0 423 * 424 * Returns >= nr_cpu_ids if such cpu doesn't exist. 425 */ 426 static __always_inline 427 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1, 428 const struct cpumask *srcp2, 429 const struct cpumask *srcp3) 430 { 431 return find_nth_and_andnot_bit(cpumask_bits(srcp1), 432 cpumask_bits(srcp2), 433 cpumask_bits(srcp3), 434 small_cpumask_bits, cpumask_check(cpu)); 435 } 436 437 #define CPU_BITS_NONE \ 438 { \ 439 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ 440 } 441 442 #define CPU_BITS_CPU0 \ 443 { \ 444 [0] = 1UL \ 445 } 446 447 /** 448 * cpumask_set_cpu - set a cpu in a cpumask 449 * @cpu: cpu number (< nr_cpu_ids) 450 * @dstp: the cpumask pointer 451 */ 452 static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) 453 { 454 set_bit(cpumask_check(cpu), cpumask_bits(dstp)); 455 } 456 457 static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) 458 { 459 __set_bit(cpumask_check(cpu), cpumask_bits(dstp)); 460 } 461 462 463 /** 464 * cpumask_clear_cpu - clear a cpu in a cpumask 465 * @cpu: cpu number (< nr_cpu_ids) 466 * @dstp: the cpumask pointer 467 */ 468 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp) 469 { 470 clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); 471 } 472 473 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp) 474 { 475 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); 476 } 477 478 /** 479 * cpumask_test_cpu - test for a cpu in a cpumask 480 * @cpu: cpu number (< nr_cpu_ids) 481 * @cpumask: the cpumask pointer 482 * 483 * Returns true if @cpu is set in @cpumask, else returns false 484 */ 485 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask) 486 { 487 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask))); 488 } 489 490 /** 491 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask 492 * @cpu: cpu number (< nr_cpu_ids) 493 * @cpumask: the cpumask pointer 494 * 495 * Returns true if @cpu is set in old bitmap of @cpumask, else returns false 496 * 497 * test_and_set_bit wrapper for cpumasks. 498 */ 499 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask) 500 { 501 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask)); 502 } 503 504 /** 505 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask 506 * @cpu: cpu number (< nr_cpu_ids) 507 * @cpumask: the cpumask pointer 508 * 509 * Returns true if @cpu is set in old bitmap of @cpumask, else returns false 510 * 511 * test_and_clear_bit wrapper for cpumasks. 512 */ 513 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask) 514 { 515 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask)); 516 } 517 518 /** 519 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask 520 * @dstp: the cpumask pointer 521 */ 522 static inline void cpumask_setall(struct cpumask *dstp) 523 { 524 if (small_const_nbits(small_cpumask_bits)) { 525 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits); 526 return; 527 } 528 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits); 529 } 530 531 /** 532 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask 533 * @dstp: the cpumask pointer 534 */ 535 static inline void cpumask_clear(struct cpumask *dstp) 536 { 537 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits); 538 } 539 540 /** 541 * cpumask_and - *dstp = *src1p & *src2p 542 * @dstp: the cpumask result 543 * @src1p: the first input 544 * @src2p: the second input 545 * 546 * If *@dstp is empty, returns false, else returns true 547 */ 548 static inline bool cpumask_and(struct cpumask *dstp, 549 const struct cpumask *src1p, 550 const struct cpumask *src2p) 551 { 552 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p), 553 cpumask_bits(src2p), small_cpumask_bits); 554 } 555 556 /** 557 * cpumask_or - *dstp = *src1p | *src2p 558 * @dstp: the cpumask result 559 * @src1p: the first input 560 * @src2p: the second input 561 */ 562 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p, 563 const struct cpumask *src2p) 564 { 565 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p), 566 cpumask_bits(src2p), small_cpumask_bits); 567 } 568 569 /** 570 * cpumask_xor - *dstp = *src1p ^ *src2p 571 * @dstp: the cpumask result 572 * @src1p: the first input 573 * @src2p: the second input 574 */ 575 static inline void cpumask_xor(struct cpumask *dstp, 576 const struct cpumask *src1p, 577 const struct cpumask *src2p) 578 { 579 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p), 580 cpumask_bits(src2p), small_cpumask_bits); 581 } 582 583 /** 584 * cpumask_andnot - *dstp = *src1p & ~*src2p 585 * @dstp: the cpumask result 586 * @src1p: the first input 587 * @src2p: the second input 588 * 589 * If *@dstp is empty, returns false, else returns true 590 */ 591 static inline bool cpumask_andnot(struct cpumask *dstp, 592 const struct cpumask *src1p, 593 const struct cpumask *src2p) 594 { 595 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p), 596 cpumask_bits(src2p), small_cpumask_bits); 597 } 598 599 /** 600 * cpumask_equal - *src1p == *src2p 601 * @src1p: the first input 602 * @src2p: the second input 603 */ 604 static inline bool cpumask_equal(const struct cpumask *src1p, 605 const struct cpumask *src2p) 606 { 607 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p), 608 small_cpumask_bits); 609 } 610 611 /** 612 * cpumask_or_equal - *src1p | *src2p == *src3p 613 * @src1p: the first input 614 * @src2p: the second input 615 * @src3p: the third input 616 */ 617 static inline bool cpumask_or_equal(const struct cpumask *src1p, 618 const struct cpumask *src2p, 619 const struct cpumask *src3p) 620 { 621 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p), 622 cpumask_bits(src3p), small_cpumask_bits); 623 } 624 625 /** 626 * cpumask_intersects - (*src1p & *src2p) != 0 627 * @src1p: the first input 628 * @src2p: the second input 629 */ 630 static inline bool cpumask_intersects(const struct cpumask *src1p, 631 const struct cpumask *src2p) 632 { 633 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p), 634 small_cpumask_bits); 635 } 636 637 /** 638 * cpumask_subset - (*src1p & ~*src2p) == 0 639 * @src1p: the first input 640 * @src2p: the second input 641 * 642 * Returns true if *@src1p is a subset of *@src2p, else returns false 643 */ 644 static inline bool cpumask_subset(const struct cpumask *src1p, 645 const struct cpumask *src2p) 646 { 647 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p), 648 small_cpumask_bits); 649 } 650 651 /** 652 * cpumask_empty - *srcp == 0 653 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear. 654 */ 655 static inline bool cpumask_empty(const struct cpumask *srcp) 656 { 657 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits); 658 } 659 660 /** 661 * cpumask_full - *srcp == 0xFFFFFFFF... 662 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set. 663 */ 664 static inline bool cpumask_full(const struct cpumask *srcp) 665 { 666 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits); 667 } 668 669 /** 670 * cpumask_weight - Count of bits in *srcp 671 * @srcp: the cpumask to count bits (< nr_cpu_ids) in. 672 */ 673 static inline unsigned int cpumask_weight(const struct cpumask *srcp) 674 { 675 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits); 676 } 677 678 /** 679 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2) 680 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in. 681 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in. 682 */ 683 static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1, 684 const struct cpumask *srcp2) 685 { 686 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits); 687 } 688 689 /** 690 * cpumask_shift_right - *dstp = *srcp >> n 691 * @dstp: the cpumask result 692 * @srcp: the input to shift 693 * @n: the number of bits to shift by 694 */ 695 static inline void cpumask_shift_right(struct cpumask *dstp, 696 const struct cpumask *srcp, int n) 697 { 698 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n, 699 small_cpumask_bits); 700 } 701 702 /** 703 * cpumask_shift_left - *dstp = *srcp << n 704 * @dstp: the cpumask result 705 * @srcp: the input to shift 706 * @n: the number of bits to shift by 707 */ 708 static inline void cpumask_shift_left(struct cpumask *dstp, 709 const struct cpumask *srcp, int n) 710 { 711 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n, 712 nr_cpumask_bits); 713 } 714 715 /** 716 * cpumask_copy - *dstp = *srcp 717 * @dstp: the result 718 * @srcp: the input cpumask 719 */ 720 static inline void cpumask_copy(struct cpumask *dstp, 721 const struct cpumask *srcp) 722 { 723 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits); 724 } 725 726 /** 727 * cpumask_any - pick a "random" cpu from *srcp 728 * @srcp: the input cpumask 729 * 730 * Returns >= nr_cpu_ids if no cpus set. 731 */ 732 #define cpumask_any(srcp) cpumask_first(srcp) 733 734 /** 735 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2 736 * @mask1: the first input cpumask 737 * @mask2: the second input cpumask 738 * 739 * Returns >= nr_cpu_ids if no cpus set. 740 */ 741 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2)) 742 743 /** 744 * cpumask_of - the cpumask containing just a given cpu 745 * @cpu: the cpu (<= nr_cpu_ids) 746 */ 747 #define cpumask_of(cpu) (get_cpu_mask(cpu)) 748 749 /** 750 * cpumask_parse_user - extract a cpumask from a user string 751 * @buf: the buffer to extract from 752 * @len: the length of the buffer 753 * @dstp: the cpumask to set. 754 * 755 * Returns -errno, or 0 for success. 756 */ 757 static inline int cpumask_parse_user(const char __user *buf, int len, 758 struct cpumask *dstp) 759 { 760 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits); 761 } 762 763 /** 764 * cpumask_parselist_user - extract a cpumask from a user string 765 * @buf: the buffer to extract from 766 * @len: the length of the buffer 767 * @dstp: the cpumask to set. 768 * 769 * Returns -errno, or 0 for success. 770 */ 771 static inline int cpumask_parselist_user(const char __user *buf, int len, 772 struct cpumask *dstp) 773 { 774 return bitmap_parselist_user(buf, len, cpumask_bits(dstp), 775 nr_cpumask_bits); 776 } 777 778 /** 779 * cpumask_parse - extract a cpumask from a string 780 * @buf: the buffer to extract from 781 * @dstp: the cpumask to set. 782 * 783 * Returns -errno, or 0 for success. 784 */ 785 static inline int cpumask_parse(const char *buf, struct cpumask *dstp) 786 { 787 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits); 788 } 789 790 /** 791 * cpulist_parse - extract a cpumask from a user string of ranges 792 * @buf: the buffer to extract from 793 * @dstp: the cpumask to set. 794 * 795 * Returns -errno, or 0 for success. 796 */ 797 static inline int cpulist_parse(const char *buf, struct cpumask *dstp) 798 { 799 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits); 800 } 801 802 /** 803 * cpumask_size - size to allocate for a 'struct cpumask' in bytes 804 */ 805 static inline unsigned int cpumask_size(void) 806 { 807 return BITS_TO_LONGS(large_cpumask_bits) * sizeof(long); 808 } 809 810 /* 811 * cpumask_var_t: struct cpumask for stack usage. 812 * 813 * Oh, the wicked games we play! In order to make kernel coding a 814 * little more difficult, we typedef cpumask_var_t to an array or a 815 * pointer: doing &mask on an array is a noop, so it still works. 816 * 817 * ie. 818 * cpumask_var_t tmpmask; 819 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) 820 * return -ENOMEM; 821 * 822 * ... use 'tmpmask' like a normal struct cpumask * ... 823 * 824 * free_cpumask_var(tmpmask); 825 * 826 * 827 * However, one notable exception is there. alloc_cpumask_var() allocates 828 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has 829 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t. 830 * 831 * cpumask_var_t tmpmask; 832 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) 833 * return -ENOMEM; 834 * 835 * var = *tmpmask; 836 * 837 * This code makes NR_CPUS length memcopy and brings to a memory corruption. 838 * cpumask_copy() provide safe copy functionality. 839 * 840 * Note that there is another evil here: If you define a cpumask_var_t 841 * as a percpu variable then the way to obtain the address of the cpumask 842 * structure differently influences what this_cpu_* operation needs to be 843 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use 844 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the 845 * other type of cpumask_var_t implementation is configured. 846 * 847 * Please also note that __cpumask_var_read_mostly can be used to declare 848 * a cpumask_var_t variable itself (not its content) as read mostly. 849 */ 850 #ifdef CONFIG_CPUMASK_OFFSTACK 851 typedef struct cpumask *cpumask_var_t; 852 853 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x) 854 #define __cpumask_var_read_mostly __read_mostly 855 856 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node); 857 858 static inline 859 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 860 { 861 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node); 862 } 863 864 /** 865 * alloc_cpumask_var - allocate a struct cpumask 866 * @mask: pointer to cpumask_var_t where the cpumask is returned 867 * @flags: GFP_ flags 868 * 869 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 870 * a nop returning a constant 1 (in <linux/cpumask.h>). 871 * 872 * See alloc_cpumask_var_node. 873 */ 874 static inline 875 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 876 { 877 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE); 878 } 879 880 static inline 881 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 882 { 883 return alloc_cpumask_var(mask, flags | __GFP_ZERO); 884 } 885 886 void alloc_bootmem_cpumask_var(cpumask_var_t *mask); 887 void free_cpumask_var(cpumask_var_t mask); 888 void free_bootmem_cpumask_var(cpumask_var_t mask); 889 890 static inline bool cpumask_available(cpumask_var_t mask) 891 { 892 return mask != NULL; 893 } 894 895 #else 896 typedef struct cpumask cpumask_var_t[1]; 897 898 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x) 899 #define __cpumask_var_read_mostly 900 901 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 902 { 903 return true; 904 } 905 906 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, 907 int node) 908 { 909 return true; 910 } 911 912 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 913 { 914 cpumask_clear(*mask); 915 return true; 916 } 917 918 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, 919 int node) 920 { 921 cpumask_clear(*mask); 922 return true; 923 } 924 925 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask) 926 { 927 } 928 929 static inline void free_cpumask_var(cpumask_var_t mask) 930 { 931 } 932 933 static inline void free_bootmem_cpumask_var(cpumask_var_t mask) 934 { 935 } 936 937 static inline bool cpumask_available(cpumask_var_t mask) 938 { 939 return true; 940 } 941 #endif /* CONFIG_CPUMASK_OFFSTACK */ 942 943 /* It's common to want to use cpu_all_mask in struct member initializers, 944 * so it has to refer to an address rather than a pointer. */ 945 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS); 946 #define cpu_all_mask to_cpumask(cpu_all_bits) 947 948 /* First bits of cpu_bit_bitmap are in fact unset. */ 949 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0]) 950 951 #if NR_CPUS == 1 952 /* Uniprocessor: the possible/online/present masks are always "1" */ 953 #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 954 #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 955 #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 956 #else 957 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask) 958 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask) 959 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask) 960 #endif 961 962 /* Wrappers for arch boot code to manipulate normally-constant masks */ 963 void init_cpu_present(const struct cpumask *src); 964 void init_cpu_possible(const struct cpumask *src); 965 void init_cpu_online(const struct cpumask *src); 966 967 static inline void reset_cpu_possible_mask(void) 968 { 969 bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS); 970 } 971 972 static inline void 973 set_cpu_possible(unsigned int cpu, bool possible) 974 { 975 if (possible) 976 cpumask_set_cpu(cpu, &__cpu_possible_mask); 977 else 978 cpumask_clear_cpu(cpu, &__cpu_possible_mask); 979 } 980 981 static inline void 982 set_cpu_present(unsigned int cpu, bool present) 983 { 984 if (present) 985 cpumask_set_cpu(cpu, &__cpu_present_mask); 986 else 987 cpumask_clear_cpu(cpu, &__cpu_present_mask); 988 } 989 990 void set_cpu_online(unsigned int cpu, bool online); 991 992 static inline void 993 set_cpu_active(unsigned int cpu, bool active) 994 { 995 if (active) 996 cpumask_set_cpu(cpu, &__cpu_active_mask); 997 else 998 cpumask_clear_cpu(cpu, &__cpu_active_mask); 999 } 1000 1001 static inline void 1002 set_cpu_dying(unsigned int cpu, bool dying) 1003 { 1004 if (dying) 1005 cpumask_set_cpu(cpu, &__cpu_dying_mask); 1006 else 1007 cpumask_clear_cpu(cpu, &__cpu_dying_mask); 1008 } 1009 1010 /** 1011 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask * 1012 * @bitmap: the bitmap 1013 * 1014 * There are a few places where cpumask_var_t isn't appropriate and 1015 * static cpumasks must be used (eg. very early boot), yet we don't 1016 * expose the definition of 'struct cpumask'. 1017 * 1018 * This does the conversion, and can be used as a constant initializer. 1019 */ 1020 #define to_cpumask(bitmap) \ 1021 ((struct cpumask *)(1 ? (bitmap) \ 1022 : (void *)sizeof(__check_is_bitmap(bitmap)))) 1023 1024 static inline int __check_is_bitmap(const unsigned long *bitmap) 1025 { 1026 return 1; 1027 } 1028 1029 /* 1030 * Special-case data structure for "single bit set only" constant CPU masks. 1031 * 1032 * We pre-generate all the 64 (or 32) possible bit positions, with enough 1033 * padding to the left and the right, and return the constant pointer 1034 * appropriately offset. 1035 */ 1036 extern const unsigned long 1037 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)]; 1038 1039 static inline const struct cpumask *get_cpu_mask(unsigned int cpu) 1040 { 1041 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG]; 1042 p -= cpu / BITS_PER_LONG; 1043 return to_cpumask(p); 1044 } 1045 1046 #if NR_CPUS > 1 1047 /** 1048 * num_online_cpus() - Read the number of online CPUs 1049 * 1050 * Despite the fact that __num_online_cpus is of type atomic_t, this 1051 * interface gives only a momentary snapshot and is not protected against 1052 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held 1053 * region. 1054 */ 1055 static __always_inline unsigned int num_online_cpus(void) 1056 { 1057 return arch_atomic_read(&__num_online_cpus); 1058 } 1059 #define num_possible_cpus() cpumask_weight(cpu_possible_mask) 1060 #define num_present_cpus() cpumask_weight(cpu_present_mask) 1061 #define num_active_cpus() cpumask_weight(cpu_active_mask) 1062 1063 static inline bool cpu_online(unsigned int cpu) 1064 { 1065 return cpumask_test_cpu(cpu, cpu_online_mask); 1066 } 1067 1068 static inline bool cpu_possible(unsigned int cpu) 1069 { 1070 return cpumask_test_cpu(cpu, cpu_possible_mask); 1071 } 1072 1073 static inline bool cpu_present(unsigned int cpu) 1074 { 1075 return cpumask_test_cpu(cpu, cpu_present_mask); 1076 } 1077 1078 static inline bool cpu_active(unsigned int cpu) 1079 { 1080 return cpumask_test_cpu(cpu, cpu_active_mask); 1081 } 1082 1083 static inline bool cpu_dying(unsigned int cpu) 1084 { 1085 return cpumask_test_cpu(cpu, cpu_dying_mask); 1086 } 1087 1088 #else 1089 1090 #define num_online_cpus() 1U 1091 #define num_possible_cpus() 1U 1092 #define num_present_cpus() 1U 1093 #define num_active_cpus() 1U 1094 1095 static inline bool cpu_online(unsigned int cpu) 1096 { 1097 return cpu == 0; 1098 } 1099 1100 static inline bool cpu_possible(unsigned int cpu) 1101 { 1102 return cpu == 0; 1103 } 1104 1105 static inline bool cpu_present(unsigned int cpu) 1106 { 1107 return cpu == 0; 1108 } 1109 1110 static inline bool cpu_active(unsigned int cpu) 1111 { 1112 return cpu == 0; 1113 } 1114 1115 static inline bool cpu_dying(unsigned int cpu) 1116 { 1117 return false; 1118 } 1119 1120 #endif /* NR_CPUS > 1 */ 1121 1122 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu)) 1123 1124 #if NR_CPUS <= BITS_PER_LONG 1125 #define CPU_BITS_ALL \ 1126 { \ 1127 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1128 } 1129 1130 #else /* NR_CPUS > BITS_PER_LONG */ 1131 1132 #define CPU_BITS_ALL \ 1133 { \ 1134 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ 1135 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1136 } 1137 #endif /* NR_CPUS > BITS_PER_LONG */ 1138 1139 /** 1140 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either 1141 * as comma-separated list of cpus or hex values of cpumask 1142 * @list: indicates whether the cpumap must be list 1143 * @mask: the cpumask to copy 1144 * @buf: the buffer to copy into 1145 * 1146 * Returns the length of the (null-terminated) @buf string, zero if 1147 * nothing is copied. 1148 */ 1149 static inline ssize_t 1150 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask) 1151 { 1152 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask), 1153 nr_cpu_ids); 1154 } 1155 1156 /** 1157 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as 1158 * hex values of cpumask 1159 * 1160 * @buf: the buffer to copy into 1161 * @mask: the cpumask to copy 1162 * @off: in the string from which we are copying, we copy to @buf 1163 * @count: the maximum number of bytes to print 1164 * 1165 * The function prints the cpumask into the buffer as hex values of 1166 * cpumask; Typically used by bin_attribute to export cpumask bitmask 1167 * ABI. 1168 * 1169 * Returns the length of how many bytes have been copied, excluding 1170 * terminating '\0'. 1171 */ 1172 static inline ssize_t 1173 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask, 1174 loff_t off, size_t count) 1175 { 1176 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask), 1177 nr_cpu_ids, off, count) - 1; 1178 } 1179 1180 /** 1181 * cpumap_print_list_to_buf - copies the cpumask into the buffer as 1182 * comma-separated list of cpus 1183 * 1184 * Everything is same with the above cpumap_print_bitmask_to_buf() 1185 * except the print format. 1186 */ 1187 static inline ssize_t 1188 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask, 1189 loff_t off, size_t count) 1190 { 1191 return bitmap_print_list_to_buf(buf, cpumask_bits(mask), 1192 nr_cpu_ids, off, count) - 1; 1193 } 1194 1195 #if NR_CPUS <= BITS_PER_LONG 1196 #define CPU_MASK_ALL \ 1197 (cpumask_t) { { \ 1198 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1199 } } 1200 #else 1201 #define CPU_MASK_ALL \ 1202 (cpumask_t) { { \ 1203 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ 1204 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1205 } } 1206 #endif /* NR_CPUS > BITS_PER_LONG */ 1207 1208 #define CPU_MASK_NONE \ 1209 (cpumask_t) { { \ 1210 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ 1211 } } 1212 1213 #define CPU_MASK_CPU0 \ 1214 (cpumask_t) { { \ 1215 [0] = 1UL \ 1216 } } 1217 1218 /* 1219 * Provide a valid theoretical max size for cpumap and cpulist sysfs files 1220 * to avoid breaking userspace which may allocate a buffer based on the size 1221 * reported by e.g. fstat. 1222 * 1223 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length. 1224 * 1225 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up 1226 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to 1227 * cover a worst-case of every other cpu being on one of two nodes for a 1228 * very large NR_CPUS. 1229 * 1230 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding 1231 * unsigned comparison to -1. 1232 */ 1233 #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \ 1234 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE) 1235 #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE) 1236 1237 #endif /* __LINUX_CPUMASK_H */ 1238