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