File: /Users/paulross/dev/linux/linux-3.13/include/linux/cpumask.h

Green shading in the line number column means the source is part of the translation unit, red means it is conditionally excluded. Highlighted line numbers link to the translation unit page. Highlighted macros link to the macro page.

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