at v6.8 37 kB view raw
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/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! */ 19typedef 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 41extern unsigned int nr_cpu_ids; 42#endif 43 44static 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 IDs 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 ARCHes (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 125extern struct cpumask __cpu_possible_mask; 126extern struct cpumask __cpu_online_mask; 127extern struct cpumask __cpu_present_mask; 128extern struct cpumask __cpu_active_mask; 129extern 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 136extern atomic_t __num_online_cpus; 137 138extern cpumask_t cpus_booted_once_mask; 139 140static __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 */ 148static __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 * Return: >= nr_cpu_ids if no cpus set. 159 */ 160static 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 * Return: >= nr_cpu_ids if all cpus are set. 170 */ 171static 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 * @srcp1: the first input 179 * @srcp2: the second input 180 * 181 * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and(). 182 */ 183static inline 184unsigned 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 * Return: >= nr_cpumask_bits if no CPUs set. 194 */ 195static 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 (i.e. return will be > @n) 203 * @srcp: the cpumask pointer 204 * 205 * Return: >= nr_cpu_ids if no further cpus set. 206 */ 207static inline 208unsigned 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 (i.e. return will be > @n) 219 * @srcp: the cpumask pointer 220 * 221 * Return: >= nr_cpu_ids if no further cpus unset. 222 */ 223static 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 */ 233static inline unsigned int cpumask_local_spread(unsigned int i, int node) 234{ 235 return 0; 236} 237 238static 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 244static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp) 245{ 246 return cpumask_first(srcp); 247} 248#else 249unsigned int cpumask_local_spread(unsigned int i, int node); 250unsigned int cpumask_any_and_distribute(const struct cpumask *src1p, 251 const struct cpumask *src2p); 252unsigned 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 (i.e. return will be > @n) 258 * @src1p: the first cpumask pointer 259 * @src2p: the second cpumask pointer 260 * 261 * Return: >= nr_cpu_ids if no further cpus set in both. 262 */ 263static inline 264unsigned 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 285static inline 286unsigned 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 302unsigned 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 * for_each_cpu_or - iterate over every cpu present in either mask 355 * @cpu: the (optionally unsigned) integer iterator 356 * @mask1: the first cpumask pointer 357 * @mask2: the second cpumask pointer 358 * 359 * This saves a temporary CPU mask in many places. It is equivalent to: 360 * struct cpumask tmp; 361 * cpumask_or(&tmp, &mask1, &mask2); 362 * for_each_cpu(cpu, &tmp) 363 * ... 364 * 365 * After the loop, cpu is >= nr_cpu_ids. 366 */ 367#define for_each_cpu_or(cpu, mask1, mask2) \ 368 for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits) 369 370/** 371 * cpumask_any_but - return a "random" in a cpumask, but not this one. 372 * @mask: the cpumask to search 373 * @cpu: the cpu to ignore. 374 * 375 * Often used to find any cpu but smp_processor_id() in a mask. 376 * Return: >= nr_cpu_ids if no cpus set. 377 */ 378static inline 379unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) 380{ 381 unsigned int i; 382 383 cpumask_check(cpu); 384 for_each_cpu(i, mask) 385 if (i != cpu) 386 break; 387 return i; 388} 389 390/** 391 * cpumask_nth - get the Nth cpu in a cpumask 392 * @srcp: the cpumask pointer 393 * @cpu: the Nth cpu to find, starting from 0 394 * 395 * Return: >= nr_cpu_ids if such cpu doesn't exist. 396 */ 397static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp) 398{ 399 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu)); 400} 401 402/** 403 * cpumask_nth_and - get the Nth cpu in 2 cpumasks 404 * @srcp1: the cpumask pointer 405 * @srcp2: the cpumask pointer 406 * @cpu: the Nth cpu to find, starting from 0 407 * 408 * Return: >= nr_cpu_ids if such cpu doesn't exist. 409 */ 410static inline 411unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1, 412 const struct cpumask *srcp2) 413{ 414 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), 415 small_cpumask_bits, cpumask_check(cpu)); 416} 417 418/** 419 * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd. 420 * @srcp1: the cpumask pointer 421 * @srcp2: the cpumask pointer 422 * @cpu: the Nth cpu to find, starting from 0 423 * 424 * Return: >= nr_cpu_ids if such cpu doesn't exist. 425 */ 426static inline 427unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1, 428 const struct cpumask *srcp2) 429{ 430 return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), 431 small_cpumask_bits, cpumask_check(cpu)); 432} 433 434/** 435 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd. 436 * @srcp1: the cpumask pointer 437 * @srcp2: the cpumask pointer 438 * @srcp3: the cpumask pointer 439 * @cpu: the Nth cpu to find, starting from 0 440 * 441 * Return: >= nr_cpu_ids if such cpu doesn't exist. 442 */ 443static __always_inline 444unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1, 445 const struct cpumask *srcp2, 446 const struct cpumask *srcp3) 447{ 448 return find_nth_and_andnot_bit(cpumask_bits(srcp1), 449 cpumask_bits(srcp2), 450 cpumask_bits(srcp3), 451 small_cpumask_bits, cpumask_check(cpu)); 452} 453 454#define CPU_BITS_NONE \ 455{ \ 456 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ 457} 458 459#define CPU_BITS_CPU0 \ 460{ \ 461 [0] = 1UL \ 462} 463 464/** 465 * cpumask_set_cpu - set a cpu in a cpumask 466 * @cpu: cpu number (< nr_cpu_ids) 467 * @dstp: the cpumask pointer 468 */ 469static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) 470{ 471 set_bit(cpumask_check(cpu), cpumask_bits(dstp)); 472} 473 474static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) 475{ 476 __set_bit(cpumask_check(cpu), cpumask_bits(dstp)); 477} 478 479 480/** 481 * cpumask_clear_cpu - clear a cpu in a cpumask 482 * @cpu: cpu number (< nr_cpu_ids) 483 * @dstp: the cpumask pointer 484 */ 485static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp) 486{ 487 clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); 488} 489 490static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp) 491{ 492 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); 493} 494 495/** 496 * cpumask_test_cpu - test for a cpu in a cpumask 497 * @cpu: cpu number (< nr_cpu_ids) 498 * @cpumask: the cpumask pointer 499 * 500 * Return: true if @cpu is set in @cpumask, else returns false 501 */ 502static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask) 503{ 504 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask))); 505} 506 507/** 508 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask 509 * @cpu: cpu number (< nr_cpu_ids) 510 * @cpumask: the cpumask pointer 511 * 512 * test_and_set_bit wrapper for cpumasks. 513 * 514 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false 515 */ 516static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask) 517{ 518 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask)); 519} 520 521/** 522 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask 523 * @cpu: cpu number (< nr_cpu_ids) 524 * @cpumask: the cpumask pointer 525 * 526 * test_and_clear_bit wrapper for cpumasks. 527 * 528 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false 529 */ 530static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask) 531{ 532 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask)); 533} 534 535/** 536 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask 537 * @dstp: the cpumask pointer 538 */ 539static inline void cpumask_setall(struct cpumask *dstp) 540{ 541 if (small_const_nbits(small_cpumask_bits)) { 542 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits); 543 return; 544 } 545 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits); 546} 547 548/** 549 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask 550 * @dstp: the cpumask pointer 551 */ 552static inline void cpumask_clear(struct cpumask *dstp) 553{ 554 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits); 555} 556 557/** 558 * cpumask_and - *dstp = *src1p & *src2p 559 * @dstp: the cpumask result 560 * @src1p: the first input 561 * @src2p: the second input 562 * 563 * Return: false if *@dstp is empty, else returns true 564 */ 565static inline bool cpumask_and(struct cpumask *dstp, 566 const struct cpumask *src1p, 567 const struct cpumask *src2p) 568{ 569 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p), 570 cpumask_bits(src2p), small_cpumask_bits); 571} 572 573/** 574 * cpumask_or - *dstp = *src1p | *src2p 575 * @dstp: the cpumask result 576 * @src1p: the first input 577 * @src2p: the second input 578 */ 579static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p, 580 const struct cpumask *src2p) 581{ 582 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p), 583 cpumask_bits(src2p), small_cpumask_bits); 584} 585 586/** 587 * cpumask_xor - *dstp = *src1p ^ *src2p 588 * @dstp: the cpumask result 589 * @src1p: the first input 590 * @src2p: the second input 591 */ 592static inline void cpumask_xor(struct cpumask *dstp, 593 const struct cpumask *src1p, 594 const struct cpumask *src2p) 595{ 596 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p), 597 cpumask_bits(src2p), small_cpumask_bits); 598} 599 600/** 601 * cpumask_andnot - *dstp = *src1p & ~*src2p 602 * @dstp: the cpumask result 603 * @src1p: the first input 604 * @src2p: the second input 605 * 606 * Return: false if *@dstp is empty, else returns true 607 */ 608static inline bool cpumask_andnot(struct cpumask *dstp, 609 const struct cpumask *src1p, 610 const struct cpumask *src2p) 611{ 612 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p), 613 cpumask_bits(src2p), small_cpumask_bits); 614} 615 616/** 617 * cpumask_equal - *src1p == *src2p 618 * @src1p: the first input 619 * @src2p: the second input 620 * 621 * Return: true if the cpumasks are equal, false if not 622 */ 623static inline bool cpumask_equal(const struct cpumask *src1p, 624 const struct cpumask *src2p) 625{ 626 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p), 627 small_cpumask_bits); 628} 629 630/** 631 * cpumask_or_equal - *src1p | *src2p == *src3p 632 * @src1p: the first input 633 * @src2p: the second input 634 * @src3p: the third input 635 * 636 * Return: true if first cpumask ORed with second cpumask == third cpumask, 637 * otherwise false 638 */ 639static inline bool cpumask_or_equal(const struct cpumask *src1p, 640 const struct cpumask *src2p, 641 const struct cpumask *src3p) 642{ 643 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p), 644 cpumask_bits(src3p), small_cpumask_bits); 645} 646 647/** 648 * cpumask_intersects - (*src1p & *src2p) != 0 649 * @src1p: the first input 650 * @src2p: the second input 651 * 652 * Return: true if first cpumask ANDed with second cpumask is non-empty, 653 * otherwise false 654 */ 655static inline bool cpumask_intersects(const struct cpumask *src1p, 656 const struct cpumask *src2p) 657{ 658 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p), 659 small_cpumask_bits); 660} 661 662/** 663 * cpumask_subset - (*src1p & ~*src2p) == 0 664 * @src1p: the first input 665 * @src2p: the second input 666 * 667 * Return: true if *@src1p is a subset of *@src2p, else returns false 668 */ 669static inline bool cpumask_subset(const struct cpumask *src1p, 670 const struct cpumask *src2p) 671{ 672 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p), 673 small_cpumask_bits); 674} 675 676/** 677 * cpumask_empty - *srcp == 0 678 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear. 679 * 680 * Return: true if srcp is empty (has no bits set), else false 681 */ 682static inline bool cpumask_empty(const struct cpumask *srcp) 683{ 684 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits); 685} 686 687/** 688 * cpumask_full - *srcp == 0xFFFFFFFF... 689 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set. 690 * 691 * Return: true if srcp is full (has all bits set), else false 692 */ 693static inline bool cpumask_full(const struct cpumask *srcp) 694{ 695 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits); 696} 697 698/** 699 * cpumask_weight - Count of bits in *srcp 700 * @srcp: the cpumask to count bits (< nr_cpu_ids) in. 701 * 702 * Return: count of bits set in *srcp 703 */ 704static inline unsigned int cpumask_weight(const struct cpumask *srcp) 705{ 706 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits); 707} 708 709/** 710 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2) 711 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in. 712 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in. 713 * 714 * Return: count of bits set in both *srcp1 and *srcp2 715 */ 716static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1, 717 const struct cpumask *srcp2) 718{ 719 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits); 720} 721 722/** 723 * cpumask_shift_right - *dstp = *srcp >> n 724 * @dstp: the cpumask result 725 * @srcp: the input to shift 726 * @n: the number of bits to shift by 727 */ 728static inline void cpumask_shift_right(struct cpumask *dstp, 729 const struct cpumask *srcp, int n) 730{ 731 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n, 732 small_cpumask_bits); 733} 734 735/** 736 * cpumask_shift_left - *dstp = *srcp << n 737 * @dstp: the cpumask result 738 * @srcp: the input to shift 739 * @n: the number of bits to shift by 740 */ 741static inline void cpumask_shift_left(struct cpumask *dstp, 742 const struct cpumask *srcp, int n) 743{ 744 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n, 745 nr_cpumask_bits); 746} 747 748/** 749 * cpumask_copy - *dstp = *srcp 750 * @dstp: the result 751 * @srcp: the input cpumask 752 */ 753static inline void cpumask_copy(struct cpumask *dstp, 754 const struct cpumask *srcp) 755{ 756 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits); 757} 758 759/** 760 * cpumask_any - pick a "random" cpu from *srcp 761 * @srcp: the input cpumask 762 * 763 * Return: >= nr_cpu_ids if no cpus set. 764 */ 765#define cpumask_any(srcp) cpumask_first(srcp) 766 767/** 768 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2 769 * @mask1: the first input cpumask 770 * @mask2: the second input cpumask 771 * 772 * Return: >= nr_cpu_ids if no cpus set. 773 */ 774#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2)) 775 776/** 777 * cpumask_of - the cpumask containing just a given cpu 778 * @cpu: the cpu (<= nr_cpu_ids) 779 */ 780#define cpumask_of(cpu) (get_cpu_mask(cpu)) 781 782/** 783 * cpumask_parse_user - extract a cpumask from a user string 784 * @buf: the buffer to extract from 785 * @len: the length of the buffer 786 * @dstp: the cpumask to set. 787 * 788 * Return: -errno, or 0 for success. 789 */ 790static inline int cpumask_parse_user(const char __user *buf, int len, 791 struct cpumask *dstp) 792{ 793 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits); 794} 795 796/** 797 * cpumask_parselist_user - extract a cpumask from a user string 798 * @buf: the buffer to extract from 799 * @len: the length of the buffer 800 * @dstp: the cpumask to set. 801 * 802 * Return: -errno, or 0 for success. 803 */ 804static inline int cpumask_parselist_user(const char __user *buf, int len, 805 struct cpumask *dstp) 806{ 807 return bitmap_parselist_user(buf, len, cpumask_bits(dstp), 808 nr_cpumask_bits); 809} 810 811/** 812 * cpumask_parse - extract a cpumask from a string 813 * @buf: the buffer to extract from 814 * @dstp: the cpumask to set. 815 * 816 * Return: -errno, or 0 for success. 817 */ 818static inline int cpumask_parse(const char *buf, struct cpumask *dstp) 819{ 820 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits); 821} 822 823/** 824 * cpulist_parse - extract a cpumask from a user string of ranges 825 * @buf: the buffer to extract from 826 * @dstp: the cpumask to set. 827 * 828 * Return: -errno, or 0 for success. 829 */ 830static inline int cpulist_parse(const char *buf, struct cpumask *dstp) 831{ 832 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits); 833} 834 835/** 836 * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes 837 * 838 * Return: size to allocate for a &struct cpumask in bytes 839 */ 840static inline unsigned int cpumask_size(void) 841{ 842 return BITS_TO_LONGS(large_cpumask_bits) * sizeof(long); 843} 844 845/* 846 * cpumask_var_t: struct cpumask for stack usage. 847 * 848 * Oh, the wicked games we play! In order to make kernel coding a 849 * little more difficult, we typedef cpumask_var_t to an array or a 850 * pointer: doing &mask on an array is a noop, so it still works. 851 * 852 * i.e. 853 * cpumask_var_t tmpmask; 854 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) 855 * return -ENOMEM; 856 * 857 * ... use 'tmpmask' like a normal struct cpumask * ... 858 * 859 * free_cpumask_var(tmpmask); 860 * 861 * 862 * However, one notable exception is there. alloc_cpumask_var() allocates 863 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has 864 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t. 865 * 866 * cpumask_var_t tmpmask; 867 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) 868 * return -ENOMEM; 869 * 870 * var = *tmpmask; 871 * 872 * This code makes NR_CPUS length memcopy and brings to a memory corruption. 873 * cpumask_copy() provide safe copy functionality. 874 * 875 * Note that there is another evil here: If you define a cpumask_var_t 876 * as a percpu variable then the way to obtain the address of the cpumask 877 * structure differently influences what this_cpu_* operation needs to be 878 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use 879 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the 880 * other type of cpumask_var_t implementation is configured. 881 * 882 * Please also note that __cpumask_var_read_mostly can be used to declare 883 * a cpumask_var_t variable itself (not its content) as read mostly. 884 */ 885#ifdef CONFIG_CPUMASK_OFFSTACK 886typedef struct cpumask *cpumask_var_t; 887 888#define this_cpu_cpumask_var_ptr(x) this_cpu_read(x) 889#define __cpumask_var_read_mostly __read_mostly 890 891bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node); 892 893static inline 894bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 895{ 896 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node); 897} 898 899/** 900 * alloc_cpumask_var - allocate a struct cpumask 901 * @mask: pointer to cpumask_var_t where the cpumask is returned 902 * @flags: GFP_ flags 903 * 904 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 905 * a nop returning a constant 1 (in <linux/cpumask.h>). 906 * 907 * See alloc_cpumask_var_node. 908 * 909 * Return: %true if allocation succeeded, %false if not 910 */ 911static inline 912bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 913{ 914 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE); 915} 916 917static inline 918bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 919{ 920 return alloc_cpumask_var(mask, flags | __GFP_ZERO); 921} 922 923void alloc_bootmem_cpumask_var(cpumask_var_t *mask); 924void free_cpumask_var(cpumask_var_t mask); 925void free_bootmem_cpumask_var(cpumask_var_t mask); 926 927static inline bool cpumask_available(cpumask_var_t mask) 928{ 929 return mask != NULL; 930} 931 932#else 933typedef struct cpumask cpumask_var_t[1]; 934 935#define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x) 936#define __cpumask_var_read_mostly 937 938static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 939{ 940 return true; 941} 942 943static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, 944 int node) 945{ 946 return true; 947} 948 949static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 950{ 951 cpumask_clear(*mask); 952 return true; 953} 954 955static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, 956 int node) 957{ 958 cpumask_clear(*mask); 959 return true; 960} 961 962static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask) 963{ 964} 965 966static inline void free_cpumask_var(cpumask_var_t mask) 967{ 968} 969 970static inline void free_bootmem_cpumask_var(cpumask_var_t mask) 971{ 972} 973 974static inline bool cpumask_available(cpumask_var_t mask) 975{ 976 return true; 977} 978#endif /* CONFIG_CPUMASK_OFFSTACK */ 979 980/* It's common to want to use cpu_all_mask in struct member initializers, 981 * so it has to refer to an address rather than a pointer. */ 982extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS); 983#define cpu_all_mask to_cpumask(cpu_all_bits) 984 985/* First bits of cpu_bit_bitmap are in fact unset. */ 986#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0]) 987 988#if NR_CPUS == 1 989/* Uniprocessor: the possible/online/present masks are always "1" */ 990#define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 991#define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 992#define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++) 993#else 994#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask) 995#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask) 996#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask) 997#endif 998 999/* Wrappers for arch boot code to manipulate normally-constant masks */ 1000void init_cpu_present(const struct cpumask *src); 1001void init_cpu_possible(const struct cpumask *src); 1002void init_cpu_online(const struct cpumask *src); 1003 1004static inline void reset_cpu_possible_mask(void) 1005{ 1006 bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS); 1007} 1008 1009static inline void 1010set_cpu_possible(unsigned int cpu, bool possible) 1011{ 1012 if (possible) 1013 cpumask_set_cpu(cpu, &__cpu_possible_mask); 1014 else 1015 cpumask_clear_cpu(cpu, &__cpu_possible_mask); 1016} 1017 1018static inline void 1019set_cpu_present(unsigned int cpu, bool present) 1020{ 1021 if (present) 1022 cpumask_set_cpu(cpu, &__cpu_present_mask); 1023 else 1024 cpumask_clear_cpu(cpu, &__cpu_present_mask); 1025} 1026 1027void set_cpu_online(unsigned int cpu, bool online); 1028 1029static inline void 1030set_cpu_active(unsigned int cpu, bool active) 1031{ 1032 if (active) 1033 cpumask_set_cpu(cpu, &__cpu_active_mask); 1034 else 1035 cpumask_clear_cpu(cpu, &__cpu_active_mask); 1036} 1037 1038static inline void 1039set_cpu_dying(unsigned int cpu, bool dying) 1040{ 1041 if (dying) 1042 cpumask_set_cpu(cpu, &__cpu_dying_mask); 1043 else 1044 cpumask_clear_cpu(cpu, &__cpu_dying_mask); 1045} 1046 1047/** 1048 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask * 1049 * @bitmap: the bitmap 1050 * 1051 * There are a few places where cpumask_var_t isn't appropriate and 1052 * static cpumasks must be used (eg. very early boot), yet we don't 1053 * expose the definition of 'struct cpumask'. 1054 * 1055 * This does the conversion, and can be used as a constant initializer. 1056 */ 1057#define to_cpumask(bitmap) \ 1058 ((struct cpumask *)(1 ? (bitmap) \ 1059 : (void *)sizeof(__check_is_bitmap(bitmap)))) 1060 1061static inline int __check_is_bitmap(const unsigned long *bitmap) 1062{ 1063 return 1; 1064} 1065 1066/* 1067 * Special-case data structure for "single bit set only" constant CPU masks. 1068 * 1069 * We pre-generate all the 64 (or 32) possible bit positions, with enough 1070 * padding to the left and the right, and return the constant pointer 1071 * appropriately offset. 1072 */ 1073extern const unsigned long 1074 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)]; 1075 1076static inline const struct cpumask *get_cpu_mask(unsigned int cpu) 1077{ 1078 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG]; 1079 p -= cpu / BITS_PER_LONG; 1080 return to_cpumask(p); 1081} 1082 1083#if NR_CPUS > 1 1084/** 1085 * num_online_cpus() - Read the number of online CPUs 1086 * 1087 * Despite the fact that __num_online_cpus is of type atomic_t, this 1088 * interface gives only a momentary snapshot and is not protected against 1089 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held 1090 * region. 1091 * 1092 * Return: momentary snapshot of the number of online CPUs 1093 */ 1094static __always_inline unsigned int num_online_cpus(void) 1095{ 1096 return raw_atomic_read(&__num_online_cpus); 1097} 1098#define num_possible_cpus() cpumask_weight(cpu_possible_mask) 1099#define num_present_cpus() cpumask_weight(cpu_present_mask) 1100#define num_active_cpus() cpumask_weight(cpu_active_mask) 1101 1102static inline bool cpu_online(unsigned int cpu) 1103{ 1104 return cpumask_test_cpu(cpu, cpu_online_mask); 1105} 1106 1107static inline bool cpu_possible(unsigned int cpu) 1108{ 1109 return cpumask_test_cpu(cpu, cpu_possible_mask); 1110} 1111 1112static inline bool cpu_present(unsigned int cpu) 1113{ 1114 return cpumask_test_cpu(cpu, cpu_present_mask); 1115} 1116 1117static inline bool cpu_active(unsigned int cpu) 1118{ 1119 return cpumask_test_cpu(cpu, cpu_active_mask); 1120} 1121 1122static inline bool cpu_dying(unsigned int cpu) 1123{ 1124 return cpumask_test_cpu(cpu, cpu_dying_mask); 1125} 1126 1127#else 1128 1129#define num_online_cpus() 1U 1130#define num_possible_cpus() 1U 1131#define num_present_cpus() 1U 1132#define num_active_cpus() 1U 1133 1134static inline bool cpu_online(unsigned int cpu) 1135{ 1136 return cpu == 0; 1137} 1138 1139static inline bool cpu_possible(unsigned int cpu) 1140{ 1141 return cpu == 0; 1142} 1143 1144static inline bool cpu_present(unsigned int cpu) 1145{ 1146 return cpu == 0; 1147} 1148 1149static inline bool cpu_active(unsigned int cpu) 1150{ 1151 return cpu == 0; 1152} 1153 1154static inline bool cpu_dying(unsigned int cpu) 1155{ 1156 return false; 1157} 1158 1159#endif /* NR_CPUS > 1 */ 1160 1161#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu)) 1162 1163#if NR_CPUS <= BITS_PER_LONG 1164#define CPU_BITS_ALL \ 1165{ \ 1166 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1167} 1168 1169#else /* NR_CPUS > BITS_PER_LONG */ 1170 1171#define CPU_BITS_ALL \ 1172{ \ 1173 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ 1174 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1175} 1176#endif /* NR_CPUS > BITS_PER_LONG */ 1177 1178/** 1179 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either 1180 * as comma-separated list of cpus or hex values of cpumask 1181 * @list: indicates whether the cpumap must be list 1182 * @mask: the cpumask to copy 1183 * @buf: the buffer to copy into 1184 * 1185 * Return: the length of the (null-terminated) @buf string, zero if 1186 * nothing is copied. 1187 */ 1188static inline ssize_t 1189cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask) 1190{ 1191 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask), 1192 nr_cpu_ids); 1193} 1194 1195/** 1196 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as 1197 * hex values of cpumask 1198 * 1199 * @buf: the buffer to copy into 1200 * @mask: the cpumask to copy 1201 * @off: in the string from which we are copying, we copy to @buf 1202 * @count: the maximum number of bytes to print 1203 * 1204 * The function prints the cpumask into the buffer as hex values of 1205 * cpumask; Typically used by bin_attribute to export cpumask bitmask 1206 * ABI. 1207 * 1208 * Return: the length of how many bytes have been copied, excluding 1209 * terminating '\0'. 1210 */ 1211static inline ssize_t 1212cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask, 1213 loff_t off, size_t count) 1214{ 1215 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask), 1216 nr_cpu_ids, off, count) - 1; 1217} 1218 1219/** 1220 * cpumap_print_list_to_buf - copies the cpumask into the buffer as 1221 * comma-separated list of cpus 1222 * @buf: the buffer to copy into 1223 * @mask: the cpumask to copy 1224 * @off: in the string from which we are copying, we copy to @buf 1225 * @count: the maximum number of bytes to print 1226 * 1227 * Everything is same with the above cpumap_print_bitmask_to_buf() 1228 * except the print format. 1229 * 1230 * Return: the length of how many bytes have been copied, excluding 1231 * terminating '\0'. 1232 */ 1233static inline ssize_t 1234cpumap_print_list_to_buf(char *buf, const struct cpumask *mask, 1235 loff_t off, size_t count) 1236{ 1237 return bitmap_print_list_to_buf(buf, cpumask_bits(mask), 1238 nr_cpu_ids, off, count) - 1; 1239} 1240 1241#if NR_CPUS <= BITS_PER_LONG 1242#define CPU_MASK_ALL \ 1243(cpumask_t) { { \ 1244 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1245} } 1246#else 1247#define CPU_MASK_ALL \ 1248(cpumask_t) { { \ 1249 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ 1250 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ 1251} } 1252#endif /* NR_CPUS > BITS_PER_LONG */ 1253 1254#define CPU_MASK_NONE \ 1255(cpumask_t) { { \ 1256 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ 1257} } 1258 1259#define CPU_MASK_CPU0 \ 1260(cpumask_t) { { \ 1261 [0] = 1UL \ 1262} } 1263 1264/* 1265 * Provide a valid theoretical max size for cpumap and cpulist sysfs files 1266 * to avoid breaking userspace which may allocate a buffer based on the size 1267 * reported by e.g. fstat. 1268 * 1269 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length. 1270 * 1271 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up 1272 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to 1273 * cover a worst-case of every other cpu being on one of two nodes for a 1274 * very large NR_CPUS. 1275 * 1276 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding 1277 * unsigned comparison to -1. 1278 */ 1279#define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \ 1280 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE) 1281#define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE) 1282 1283#endif /* __LINUX_CPUMASK_H */