linux/include/linux/cpumask.h
<<
>>
Prefs
   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/bug.h>
  14
  15/* Don't assign or return these: may not be this big! */
  16typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  17
  18/**
  19 * cpumask_bits - get the bits in a cpumask
  20 * @maskp: the struct cpumask *
  21 *
  22 * You should only assume nr_cpu_ids bits of this mask are valid.  This is
  23 * a macro so it's const-correct.
  24 */
  25#define cpumask_bits(maskp) ((maskp)->bits)
  26
  27/**
  28 * cpumask_pr_args - printf args to output a cpumask
  29 * @maskp: cpumask to be printed
  30 *
  31 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
  32 */
  33#define cpumask_pr_args(maskp)          nr_cpu_ids, cpumask_bits(maskp)
  34
  35#if NR_CPUS == 1
  36#define nr_cpu_ids              1U
  37#else
  38extern unsigned int nr_cpu_ids;
  39#endif
  40
  41#ifdef CONFIG_CPUMASK_OFFSTACK
  42/* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  43 * not all bits may be allocated. */
  44#define nr_cpumask_bits nr_cpu_ids
  45#else
  46#define nr_cpumask_bits ((unsigned int)NR_CPUS)
  47#endif
  48
  49/*
  50 * The following particular system cpumasks and operations manage
  51 * possible, present, active and online cpus.
  52 *
  53 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
  54 *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
  55 *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
  56 *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
  57 *
  58 *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
  59 *
  60 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  61 *  that it is possible might ever be plugged in at anytime during the
  62 *  life of that system boot.  The cpu_present_mask is dynamic(*),
  63 *  representing which CPUs are currently plugged in.  And
  64 *  cpu_online_mask is the dynamic subset of cpu_present_mask,
  65 *  indicating those CPUs available for scheduling.
  66 *
  67 *  If HOTPLUG is enabled, then cpu_possible_mask is forced to have
  68 *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
  69 *  ACPI reports present at boot.
  70 *
  71 *  If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
  72 *  depending on what ACPI reports as currently plugged in, otherwise
  73 *  cpu_present_mask is just a copy of cpu_possible_mask.
  74 *
  75 *  (*) Well, cpu_present_mask is dynamic in the hotplug case.  If not
  76 *      hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
  77 *
  78 * Subtleties:
  79 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
  80 *    assumption that their single CPU is online.  The UP
  81 *    cpu_{online,possible,present}_masks are placebos.  Changing them
  82 *    will have no useful affect on the following num_*_cpus()
  83 *    and cpu_*() macros in the UP case.  This ugliness is a UP
  84 *    optimization - don't waste any instructions or memory references
  85 *    asking if you're online or how many CPUs there are if there is
  86 *    only one CPU.
  87 */
  88
  89extern struct cpumask __cpu_possible_mask;
  90extern struct cpumask __cpu_online_mask;
  91extern struct cpumask __cpu_present_mask;
  92extern struct cpumask __cpu_active_mask;
  93#define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
  94#define cpu_online_mask   ((const struct cpumask *)&__cpu_online_mask)
  95#define cpu_present_mask  ((const struct cpumask *)&__cpu_present_mask)
  96#define cpu_active_mask   ((const struct cpumask *)&__cpu_active_mask)
  97
  98#if NR_CPUS > 1
  99#define num_online_cpus()       cpumask_weight(cpu_online_mask)
 100#define num_possible_cpus()     cpumask_weight(cpu_possible_mask)
 101#define num_present_cpus()      cpumask_weight(cpu_present_mask)
 102#define num_active_cpus()       cpumask_weight(cpu_active_mask)
 103#define cpu_online(cpu)         cpumask_test_cpu((cpu), cpu_online_mask)
 104#define cpu_possible(cpu)       cpumask_test_cpu((cpu), cpu_possible_mask)
 105#define cpu_present(cpu)        cpumask_test_cpu((cpu), cpu_present_mask)
 106#define cpu_active(cpu)         cpumask_test_cpu((cpu), cpu_active_mask)
 107#else
 108#define num_online_cpus()       1U
 109#define num_possible_cpus()     1U
 110#define num_present_cpus()      1U
 111#define num_active_cpus()       1U
 112#define cpu_online(cpu)         ((cpu) == 0)
 113#define cpu_possible(cpu)       ((cpu) == 0)
 114#define cpu_present(cpu)        ((cpu) == 0)
 115#define cpu_active(cpu)         ((cpu) == 0)
 116#endif
 117
 118static inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
 119{
 120#ifdef CONFIG_DEBUG_PER_CPU_MAPS
 121        WARN_ON_ONCE(cpu >= bits);
 122#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
 123}
 124
 125/* verify cpu argument to cpumask_* operators */
 126static inline unsigned int cpumask_check(unsigned int cpu)
 127{
 128        cpu_max_bits_warn(cpu, nr_cpumask_bits);
 129        return cpu;
 130}
 131
 132#if NR_CPUS == 1
 133/* Uniprocessor.  Assume all masks are "1". */
 134static inline unsigned int cpumask_first(const struct cpumask *srcp)
 135{
 136        return 0;
 137}
 138
 139static inline unsigned int cpumask_last(const struct cpumask *srcp)
 140{
 141        return 0;
 142}
 143
 144/* Valid inputs for n are -1 and 0. */
 145static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
 146{
 147        return n+1;
 148}
 149
 150static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
 151{
 152        return n+1;
 153}
 154
 155static inline unsigned int cpumask_next_and(int n,
 156                                            const struct cpumask *srcp,
 157                                            const struct cpumask *andp)
 158{
 159        return n+1;
 160}
 161
 162/* cpu must be a valid cpu, ie 0, so there's no other choice. */
 163static inline unsigned int cpumask_any_but(const struct cpumask *mask,
 164                                           unsigned int cpu)
 165{
 166        return 1;
 167}
 168
 169static inline unsigned int cpumask_local_spread(unsigned int i, int node)
 170{
 171        return 0;
 172}
 173
 174#define for_each_cpu(cpu, mask)                 \
 175        for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
 176#define for_each_cpu_not(cpu, mask)             \
 177        for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
 178#define for_each_cpu_wrap(cpu, mask, start)     \
 179        for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
 180#define for_each_cpu_and(cpu, mask, and)        \
 181        for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
 182#else
 183/**
 184 * cpumask_first - get the first cpu in a cpumask
 185 * @srcp: the cpumask pointer
 186 *
 187 * Returns >= nr_cpu_ids if no cpus set.
 188 */
 189static inline unsigned int cpumask_first(const struct cpumask *srcp)
 190{
 191        return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
 192}
 193
 194/**
 195 * cpumask_last - get the last CPU in a cpumask
 196 * @srcp:       - the cpumask pointer
 197 *
 198 * Returns      >= nr_cpumask_bits if no CPUs set.
 199 */
 200static inline unsigned int cpumask_last(const struct cpumask *srcp)
 201{
 202        return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
 203}
 204
 205unsigned int cpumask_next(int n, const struct cpumask *srcp);
 206
 207/**
 208 * cpumask_next_zero - get the next unset cpu in a cpumask
 209 * @n: the cpu prior to the place to search (ie. return will be > @n)
 210 * @srcp: the cpumask pointer
 211 *
 212 * Returns >= nr_cpu_ids if no further cpus unset.
 213 */
 214static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
 215{
 216        /* -1 is a legal arg here. */
 217        if (n != -1)
 218                cpumask_check(n);
 219        return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
 220}
 221
 222int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
 223int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
 224unsigned int cpumask_local_spread(unsigned int i, int node);
 225
 226/**
 227 * for_each_cpu - iterate over every cpu in a mask
 228 * @cpu: the (optionally unsigned) integer iterator
 229 * @mask: the cpumask pointer
 230 *
 231 * After the loop, cpu is >= nr_cpu_ids.
 232 */
 233#define for_each_cpu(cpu, mask)                         \
 234        for ((cpu) = -1;                                \
 235                (cpu) = cpumask_next((cpu), (mask)),    \
 236                (cpu) < nr_cpu_ids;)
 237
 238/**
 239 * for_each_cpu_not - iterate over every cpu in a complemented mask
 240 * @cpu: the (optionally unsigned) integer iterator
 241 * @mask: the cpumask pointer
 242 *
 243 * After the loop, cpu is >= nr_cpu_ids.
 244 */
 245#define for_each_cpu_not(cpu, mask)                             \
 246        for ((cpu) = -1;                                        \
 247                (cpu) = cpumask_next_zero((cpu), (mask)),       \
 248                (cpu) < nr_cpu_ids;)
 249
 250extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
 251
 252/**
 253 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
 254 * @cpu: the (optionally unsigned) integer iterator
 255 * @mask: the cpumask poiter
 256 * @start: the start location
 257 *
 258 * The implementation does not assume any bit in @mask is set (including @start).
 259 *
 260 * After the loop, cpu is >= nr_cpu_ids.
 261 */
 262#define for_each_cpu_wrap(cpu, mask, start)                                     \
 263        for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false);      \
 264             (cpu) < nr_cpumask_bits;                                           \
 265             (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
 266
 267/**
 268 * for_each_cpu_and - iterate over every cpu in both masks
 269 * @cpu: the (optionally unsigned) integer iterator
 270 * @mask: the first cpumask pointer
 271 * @and: the second cpumask pointer
 272 *
 273 * This saves a temporary CPU mask in many places.  It is equivalent to:
 274 *      struct cpumask tmp;
 275 *      cpumask_and(&tmp, &mask, &and);
 276 *      for_each_cpu(cpu, &tmp)
 277 *              ...
 278 *
 279 * After the loop, cpu is >= nr_cpu_ids.
 280 */
 281#define for_each_cpu_and(cpu, mask, and)                                \
 282        for ((cpu) = -1;                                                \
 283                (cpu) = cpumask_next_and((cpu), (mask), (and)),         \
 284                (cpu) < nr_cpu_ids;)
 285#endif /* SMP */
 286
 287#define CPU_BITS_NONE                                           \
 288{                                                               \
 289        [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL                  \
 290}
 291
 292#define CPU_BITS_CPU0                                           \
 293{                                                               \
 294        [0] =  1UL                                              \
 295}
 296
 297/**
 298 * cpumask_set_cpu - set a cpu in a cpumask
 299 * @cpu: cpu number (< nr_cpu_ids)
 300 * @dstp: the cpumask pointer
 301 */
 302static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
 303{
 304        set_bit(cpumask_check(cpu), cpumask_bits(dstp));
 305}
 306
 307static inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
 308{
 309        __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
 310}
 311
 312
 313/**
 314 * cpumask_clear_cpu - clear a cpu in a cpumask
 315 * @cpu: cpu number (< nr_cpu_ids)
 316 * @dstp: the cpumask pointer
 317 */
 318static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
 319{
 320        clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
 321}
 322
 323static inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
 324{
 325        __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
 326}
 327
 328/**
 329 * cpumask_test_cpu - test for a cpu in a cpumask
 330 * @cpu: cpu number (< nr_cpu_ids)
 331 * @cpumask: the cpumask pointer
 332 *
 333 * Returns 1 if @cpu is set in @cpumask, else returns 0
 334 */
 335static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
 336{
 337        return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
 338}
 339
 340/**
 341 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
 342 * @cpu: cpu number (< nr_cpu_ids)
 343 * @cpumask: the cpumask pointer
 344 *
 345 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
 346 *
 347 * test_and_set_bit wrapper for cpumasks.
 348 */
 349static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
 350{
 351        return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
 352}
 353
 354/**
 355 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
 356 * @cpu: cpu number (< nr_cpu_ids)
 357 * @cpumask: the cpumask pointer
 358 *
 359 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
 360 *
 361 * test_and_clear_bit wrapper for cpumasks.
 362 */
 363static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
 364{
 365        return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
 366}
 367
 368/**
 369 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
 370 * @dstp: the cpumask pointer
 371 */
 372static inline void cpumask_setall(struct cpumask *dstp)
 373{
 374        bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
 375}
 376
 377/**
 378 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
 379 * @dstp: the cpumask pointer
 380 */
 381static inline void cpumask_clear(struct cpumask *dstp)
 382{
 383        bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
 384}
 385
 386/**
 387 * cpumask_and - *dstp = *src1p & *src2p
 388 * @dstp: the cpumask result
 389 * @src1p: the first input
 390 * @src2p: the second input
 391 *
 392 * If *@dstp is empty, returns 0, else returns 1
 393 */
 394static inline int cpumask_and(struct cpumask *dstp,
 395                               const struct cpumask *src1p,
 396                               const struct cpumask *src2p)
 397{
 398        return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
 399                                       cpumask_bits(src2p), nr_cpumask_bits);
 400}
 401
 402/**
 403 * cpumask_or - *dstp = *src1p | *src2p
 404 * @dstp: the cpumask result
 405 * @src1p: the first input
 406 * @src2p: the second input
 407 */
 408static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
 409                              const struct cpumask *src2p)
 410{
 411        bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
 412                                      cpumask_bits(src2p), nr_cpumask_bits);
 413}
 414
 415/**
 416 * cpumask_xor - *dstp = *src1p ^ *src2p
 417 * @dstp: the cpumask result
 418 * @src1p: the first input
 419 * @src2p: the second input
 420 */
 421static inline void cpumask_xor(struct cpumask *dstp,
 422                               const struct cpumask *src1p,
 423                               const struct cpumask *src2p)
 424{
 425        bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
 426                                       cpumask_bits(src2p), nr_cpumask_bits);
 427}
 428
 429/**
 430 * cpumask_andnot - *dstp = *src1p & ~*src2p
 431 * @dstp: the cpumask result
 432 * @src1p: the first input
 433 * @src2p: the second input
 434 *
 435 * If *@dstp is empty, returns 0, else returns 1
 436 */
 437static inline int cpumask_andnot(struct cpumask *dstp,
 438                                  const struct cpumask *src1p,
 439                                  const struct cpumask *src2p)
 440{
 441        return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
 442                                          cpumask_bits(src2p), nr_cpumask_bits);
 443}
 444
 445/**
 446 * cpumask_complement - *dstp = ~*srcp
 447 * @dstp: the cpumask result
 448 * @srcp: the input to invert
 449 */
 450static inline void cpumask_complement(struct cpumask *dstp,
 451                                      const struct cpumask *srcp)
 452{
 453        bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
 454                                              nr_cpumask_bits);
 455}
 456
 457/**
 458 * cpumask_equal - *src1p == *src2p
 459 * @src1p: the first input
 460 * @src2p: the second input
 461 */
 462static inline bool cpumask_equal(const struct cpumask *src1p,
 463                                const struct cpumask *src2p)
 464{
 465        return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
 466                                                 nr_cpumask_bits);
 467}
 468
 469/**
 470 * cpumask_intersects - (*src1p & *src2p) != 0
 471 * @src1p: the first input
 472 * @src2p: the second input
 473 */
 474static inline bool cpumask_intersects(const struct cpumask *src1p,
 475                                     const struct cpumask *src2p)
 476{
 477        return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
 478                                                      nr_cpumask_bits);
 479}
 480
 481/**
 482 * cpumask_subset - (*src1p & ~*src2p) == 0
 483 * @src1p: the first input
 484 * @src2p: the second input
 485 *
 486 * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
 487 */
 488static inline int cpumask_subset(const struct cpumask *src1p,
 489                                 const struct cpumask *src2p)
 490{
 491        return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
 492                                                  nr_cpumask_bits);
 493}
 494
 495/**
 496 * cpumask_empty - *srcp == 0
 497 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
 498 */
 499static inline bool cpumask_empty(const struct cpumask *srcp)
 500{
 501        return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
 502}
 503
 504/**
 505 * cpumask_full - *srcp == 0xFFFFFFFF...
 506 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
 507 */
 508static inline bool cpumask_full(const struct cpumask *srcp)
 509{
 510        return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
 511}
 512
 513/**
 514 * cpumask_weight - Count of bits in *srcp
 515 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
 516 */
 517static inline unsigned int cpumask_weight(const struct cpumask *srcp)
 518{
 519        return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
 520}
 521
 522/**
 523 * cpumask_shift_right - *dstp = *srcp >> n
 524 * @dstp: the cpumask result
 525 * @srcp: the input to shift
 526 * @n: the number of bits to shift by
 527 */
 528static inline void cpumask_shift_right(struct cpumask *dstp,
 529                                       const struct cpumask *srcp, int n)
 530{
 531        bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
 532                                               nr_cpumask_bits);
 533}
 534
 535/**
 536 * cpumask_shift_left - *dstp = *srcp << n
 537 * @dstp: the cpumask result
 538 * @srcp: the input to shift
 539 * @n: the number of bits to shift by
 540 */
 541static inline void cpumask_shift_left(struct cpumask *dstp,
 542                                      const struct cpumask *srcp, int n)
 543{
 544        bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
 545                                              nr_cpumask_bits);
 546}
 547
 548/**
 549 * cpumask_copy - *dstp = *srcp
 550 * @dstp: the result
 551 * @srcp: the input cpumask
 552 */
 553static inline void cpumask_copy(struct cpumask *dstp,
 554                                const struct cpumask *srcp)
 555{
 556        bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
 557}
 558
 559/**
 560 * cpumask_any - pick a "random" cpu from *srcp
 561 * @srcp: the input cpumask
 562 *
 563 * Returns >= nr_cpu_ids if no cpus set.
 564 */
 565#define cpumask_any(srcp) cpumask_first(srcp)
 566
 567/**
 568 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
 569 * @src1p: the first input
 570 * @src2p: the second input
 571 *
 572 * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
 573 */
 574#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
 575
 576/**
 577 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
 578 * @mask1: the first input cpumask
 579 * @mask2: the second input cpumask
 580 *
 581 * Returns >= nr_cpu_ids if no cpus set.
 582 */
 583#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
 584
 585/**
 586 * cpumask_of - the cpumask containing just a given cpu
 587 * @cpu: the cpu (<= nr_cpu_ids)
 588 */
 589#define cpumask_of(cpu) (get_cpu_mask(cpu))
 590
 591/**
 592 * cpumask_parse_user - extract a cpumask from a user string
 593 * @buf: the buffer to extract from
 594 * @len: the length of the buffer
 595 * @dstp: the cpumask to set.
 596 *
 597 * Returns -errno, or 0 for success.
 598 */
 599static inline int cpumask_parse_user(const char __user *buf, int len,
 600                                     struct cpumask *dstp)
 601{
 602        return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
 603}
 604
 605/**
 606 * cpumask_parselist_user - extract a cpumask from a user string
 607 * @buf: the buffer to extract from
 608 * @len: the length of the buffer
 609 * @dstp: the cpumask to set.
 610 *
 611 * Returns -errno, or 0 for success.
 612 */
 613static inline int cpumask_parselist_user(const char __user *buf, int len,
 614                                     struct cpumask *dstp)
 615{
 616        return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
 617                                     nr_cpumask_bits);
 618}
 619
 620/**
 621 * cpumask_parse - extract a cpumask from a string
 622 * @buf: the buffer to extract from
 623 * @dstp: the cpumask to set.
 624 *
 625 * Returns -errno, or 0 for success.
 626 */
 627static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
 628{
 629        char *nl = strchr(buf, '\n');
 630        unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
 631
 632        return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
 633}
 634
 635/**
 636 * cpulist_parse - extract a cpumask from a user string of ranges
 637 * @buf: the buffer to extract from
 638 * @dstp: the cpumask to set.
 639 *
 640 * Returns -errno, or 0 for success.
 641 */
 642static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
 643{
 644        return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
 645}
 646
 647/**
 648 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
 649 */
 650static inline unsigned int cpumask_size(void)
 651{
 652        return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
 653}
 654
 655/*
 656 * cpumask_var_t: struct cpumask for stack usage.
 657 *
 658 * Oh, the wicked games we play!  In order to make kernel coding a
 659 * little more difficult, we typedef cpumask_var_t to an array or a
 660 * pointer: doing &mask on an array is a noop, so it still works.
 661 *
 662 * ie.
 663 *      cpumask_var_t tmpmask;
 664 *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
 665 *              return -ENOMEM;
 666 *
 667 *        ... use 'tmpmask' like a normal struct cpumask * ...
 668 *
 669 *      free_cpumask_var(tmpmask);
 670 *
 671 *
 672 * However, one notable exception is there. alloc_cpumask_var() allocates
 673 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
 674 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
 675 *
 676 *      cpumask_var_t tmpmask;
 677 *      if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
 678 *              return -ENOMEM;
 679 *
 680 *      var = *tmpmask;
 681 *
 682 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
 683 * cpumask_copy() provide safe copy functionality.
 684 *
 685 * Note that there is another evil here: If you define a cpumask_var_t
 686 * as a percpu variable then the way to obtain the address of the cpumask
 687 * structure differently influences what this_cpu_* operation needs to be
 688 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
 689 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
 690 * other type of cpumask_var_t implementation is configured.
 691 *
 692 * Please also note that __cpumask_var_read_mostly can be used to declare
 693 * a cpumask_var_t variable itself (not its content) as read mostly.
 694 */
 695#ifdef CONFIG_CPUMASK_OFFSTACK
 696typedef struct cpumask *cpumask_var_t;
 697
 698#define this_cpu_cpumask_var_ptr(x)     this_cpu_read(x)
 699#define __cpumask_var_read_mostly       __read_mostly
 700
 701bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
 702bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
 703bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
 704bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
 705void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
 706void free_cpumask_var(cpumask_var_t mask);
 707void free_bootmem_cpumask_var(cpumask_var_t mask);
 708
 709static inline bool cpumask_available(cpumask_var_t mask)
 710{
 711        return mask != NULL;
 712}
 713
 714#else
 715typedef struct cpumask cpumask_var_t[1];
 716
 717#define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
 718#define __cpumask_var_read_mostly
 719
 720static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
 721{
 722        return true;
 723}
 724
 725static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
 726                                          int node)
 727{
 728        return true;
 729}
 730
 731static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
 732{
 733        cpumask_clear(*mask);
 734        return true;
 735}
 736
 737static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
 738                                          int node)
 739{
 740        cpumask_clear(*mask);
 741        return true;
 742}
 743
 744static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
 745{
 746}
 747
 748static inline void free_cpumask_var(cpumask_var_t mask)
 749{
 750}
 751
 752static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
 753{
 754}
 755
 756static inline bool cpumask_available(cpumask_var_t mask)
 757{
 758        return true;
 759}
 760#endif /* CONFIG_CPUMASK_OFFSTACK */
 761
 762/* It's common to want to use cpu_all_mask in struct member initializers,
 763 * so it has to refer to an address rather than a pointer. */
 764extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
 765#define cpu_all_mask to_cpumask(cpu_all_bits)
 766
 767/* First bits of cpu_bit_bitmap are in fact unset. */
 768#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
 769
 770#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
 771#define for_each_online_cpu(cpu)   for_each_cpu((cpu), cpu_online_mask)
 772#define for_each_present_cpu(cpu)  for_each_cpu((cpu), cpu_present_mask)
 773
 774/* Wrappers for arch boot code to manipulate normally-constant masks */
 775void init_cpu_present(const struct cpumask *src);
 776void init_cpu_possible(const struct cpumask *src);
 777void init_cpu_online(const struct cpumask *src);
 778
 779static inline void reset_cpu_possible_mask(void)
 780{
 781        bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
 782}
 783
 784static inline void
 785set_cpu_possible(unsigned int cpu, bool possible)
 786{
 787        if (possible)
 788                cpumask_set_cpu(cpu, &__cpu_possible_mask);
 789        else
 790                cpumask_clear_cpu(cpu, &__cpu_possible_mask);
 791}
 792
 793static inline void
 794set_cpu_present(unsigned int cpu, bool present)
 795{
 796        if (present)
 797                cpumask_set_cpu(cpu, &__cpu_present_mask);
 798        else
 799                cpumask_clear_cpu(cpu, &__cpu_present_mask);
 800}
 801
 802static inline void
 803set_cpu_online(unsigned int cpu, bool online)
 804{
 805        if (online)
 806                cpumask_set_cpu(cpu, &__cpu_online_mask);
 807        else
 808                cpumask_clear_cpu(cpu, &__cpu_online_mask);
 809}
 810
 811static inline void
 812set_cpu_active(unsigned int cpu, bool active)
 813{
 814        if (active)
 815                cpumask_set_cpu(cpu, &__cpu_active_mask);
 816        else
 817                cpumask_clear_cpu(cpu, &__cpu_active_mask);
 818}
 819
 820
 821/**
 822 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
 823 * @bitmap: the bitmap
 824 *
 825 * There are a few places where cpumask_var_t isn't appropriate and
 826 * static cpumasks must be used (eg. very early boot), yet we don't
 827 * expose the definition of 'struct cpumask'.
 828 *
 829 * This does the conversion, and can be used as a constant initializer.
 830 */
 831#define to_cpumask(bitmap)                                              \
 832        ((struct cpumask *)(1 ? (bitmap)                                \
 833                            : (void *)sizeof(__check_is_bitmap(bitmap))))
 834
 835static inline int __check_is_bitmap(const unsigned long *bitmap)
 836{
 837        return 1;
 838}
 839
 840/*
 841 * Special-case data structure for "single bit set only" constant CPU masks.
 842 *
 843 * We pre-generate all the 64 (or 32) possible bit positions, with enough
 844 * padding to the left and the right, and return the constant pointer
 845 * appropriately offset.
 846 */
 847extern const unsigned long
 848        cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
 849
 850static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
 851{
 852        const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
 853        p -= cpu / BITS_PER_LONG;
 854        return to_cpumask(p);
 855}
 856
 857#define cpu_is_offline(cpu)     unlikely(!cpu_online(cpu))
 858
 859#if NR_CPUS <= BITS_PER_LONG
 860#define CPU_BITS_ALL                                            \
 861{                                                               \
 862        [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)     \
 863}
 864
 865#else /* NR_CPUS > BITS_PER_LONG */
 866
 867#define CPU_BITS_ALL                                            \
 868{                                                               \
 869        [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                \
 870        [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)     \
 871}
 872#endif /* NR_CPUS > BITS_PER_LONG */
 873
 874/**
 875 * cpumap_print_to_pagebuf  - copies the cpumask into the buffer either
 876 *      as comma-separated list of cpus or hex values of cpumask
 877 * @list: indicates whether the cpumap must be list
 878 * @mask: the cpumask to copy
 879 * @buf: the buffer to copy into
 880 *
 881 * Returns the length of the (null-terminated) @buf string, zero if
 882 * nothing is copied.
 883 */
 884static inline ssize_t
 885cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
 886{
 887        return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
 888                                      nr_cpu_ids);
 889}
 890
 891#if NR_CPUS <= BITS_PER_LONG
 892#define CPU_MASK_ALL                                                    \
 893(cpumask_t) { {                                                         \
 894        [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)     \
 895} }
 896#else
 897#define CPU_MASK_ALL                                                    \
 898(cpumask_t) { {                                                         \
 899        [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
 900        [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)     \
 901} }
 902#endif /* NR_CPUS > BITS_PER_LONG */
 903
 904#define CPU_MASK_NONE                                                   \
 905(cpumask_t) { {                                                         \
 906        [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
 907} }
 908
 909#define CPU_MASK_CPU0                                                   \
 910(cpumask_t) { {                                                         \
 911        [0] =  1UL                                                      \
 912} }
 913
 914#endif /* __LINUX_CPUMASK_H */
 915