cpumask.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #ifndef __LINUX_CPUMASK_H
  2. #define __LINUX_CPUMASK_H
  3. /*
  4. * Cpumasks provide a bitmap suitable for representing the
  5. * set of CPU's in a system, one bit position per CPU number.
  6. *
  7. * See detailed comments in the file linux/bitmap.h describing the
  8. * data type on which these cpumasks are based.
  9. *
  10. * For details of cpumask_scnprintf() and cpumask_parse_user(),
  11. * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
  12. * For details of cpulist_scnprintf() and cpulist_parse(), see
  13. * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
  14. * For details of cpu_remap(), see bitmap_bitremap in lib/bitmap.c
  15. * For details of cpus_remap(), see bitmap_remap in lib/bitmap.c.
  16. * For details of cpus_onto(), see bitmap_onto in lib/bitmap.c.
  17. * For details of cpus_fold(), see bitmap_fold in lib/bitmap.c.
  18. *
  19. * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  20. * Note: The alternate operations with the suffix "_nr" are used
  21. * to limit the range of the loop to nr_cpu_ids instead of
  22. * NR_CPUS when NR_CPUS > 64 for performance reasons.
  23. * If NR_CPUS is <= 64 then most assembler bitmask
  24. * operators execute faster with a constant range, so
  25. * the operator will continue to use NR_CPUS.
  26. *
  27. * Another consideration is that nr_cpu_ids is initialized
  28. * to NR_CPUS and isn't lowered until the possible cpus are
  29. * discovered (including any disabled cpus). So early uses
  30. * will span the entire range of NR_CPUS.
  31. * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  32. *
  33. * The available cpumask operations are:
  34. *
  35. * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
  36. * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
  37. * void cpus_setall(mask) set all bits
  38. * void cpus_clear(mask) clear all bits
  39. * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
  40. * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
  41. *
  42. * void cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
  43. * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
  44. * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
  45. * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
  46. * void cpus_complement(dst, src) dst = ~src
  47. *
  48. * int cpus_equal(mask1, mask2) Does mask1 == mask2?
  49. * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
  50. * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
  51. * int cpus_empty(mask) Is mask empty (no bits sets)?
  52. * int cpus_full(mask) Is mask full (all bits sets)?
  53. * int cpus_weight(mask) Hamming weigh - number of set bits
  54. * int cpus_weight_nr(mask) Same using nr_cpu_ids instead of NR_CPUS
  55. *
  56. * void cpus_shift_right(dst, src, n) Shift right
  57. * void cpus_shift_left(dst, src, n) Shift left
  58. *
  59. * int first_cpu(mask) Number lowest set bit, or NR_CPUS
  60. * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
  61. * int next_cpu_nr(cpu, mask) Next cpu past 'cpu', or nr_cpu_ids
  62. *
  63. * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
  64. * (can be used as an lvalue)
  65. * CPU_MASK_ALL Initializer - all bits set
  66. * CPU_MASK_NONE Initializer - no bits set
  67. * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
  68. *
  69. * CPUMASK_ALLOC kmalloc's a structure that is a composite of many cpumask_t
  70. * variables, and CPUMASK_PTR provides pointers to each field.
  71. *
  72. * The structure should be defined something like this:
  73. * struct my_cpumasks {
  74. * cpumask_t mask1;
  75. * cpumask_t mask2;
  76. * };
  77. *
  78. * Usage is then:
  79. * CPUMASK_ALLOC(my_cpumasks);
  80. * CPUMASK_PTR(mask1, my_cpumasks);
  81. * CPUMASK_PTR(mask2, my_cpumasks);
  82. *
  83. * --- DO NOT reference cpumask_t pointers until this check ---
  84. * if (my_cpumasks == NULL)
  85. * "kmalloc failed"...
  86. *
  87. * References are now pointers to the cpumask_t variables (*mask1, ...)
  88. *
  89. *if NR_CPUS > BITS_PER_LONG
  90. * CPUMASK_ALLOC(m) Declares and allocates struct m *m =
  91. * kmalloc(sizeof(*m), GFP_KERNEL)
  92. * CPUMASK_FREE(m) Macro for kfree(m)
  93. *else
  94. * CPUMASK_ALLOC(m) Declares struct m _m, *m = &_m
  95. * CPUMASK_FREE(m) Nop
  96. *endif
  97. * CPUMASK_PTR(v, m) Declares cpumask_t *v = &(m->v)
  98. * ------------------------------------------------------------------------
  99. *
  100. * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
  101. * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
  102. * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
  103. * int cpulist_parse(buf, map) Parse ascii string as cpulist
  104. * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
  105. * void cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
  106. * void cpus_onto(dst, orig, relmap) *dst = orig relative to relmap
  107. * void cpus_fold(dst, orig, sz) dst bits = orig bits mod sz
  108. *
  109. * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
  110. * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
  111. *
  112. * int num_online_cpus() Number of online CPUs
  113. * int num_possible_cpus() Number of all possible CPUs
  114. * int num_present_cpus() Number of present CPUs
  115. *
  116. * int cpu_online(cpu) Is some cpu online?
  117. * int cpu_possible(cpu) Is some cpu possible?
  118. * int cpu_present(cpu) Is some cpu present (can schedule)?
  119. *
  120. * int any_online_cpu(mask) First online cpu in mask
  121. *
  122. * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
  123. * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
  124. * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
  125. *
  126. * Subtlety:
  127. * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
  128. * to generate slightly worse code. Note for example the additional
  129. * 40 lines of assembly code compiling the "for each possible cpu"
  130. * loops buried in the disk_stat_read() macros calls when compiling
  131. * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
  132. * one-line #define for cpu_isset(), instead of wrapping an inline
  133. * inside a macro, the way we do the other calls.
  134. */
  135. #include <linux/kernel.h>
  136. #include <linux/threads.h>
  137. #include <linux/bitmap.h>
  138. typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  139. extern cpumask_t _unused_cpumask_arg_;
  140. #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
  141. static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
  142. {
  143. set_bit(cpu, dstp->bits);
  144. }
  145. #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
  146. static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
  147. {
  148. clear_bit(cpu, dstp->bits);
  149. }
  150. #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
  151. static inline void __cpus_setall(cpumask_t *dstp, int nbits)
  152. {
  153. bitmap_fill(dstp->bits, nbits);
  154. }
  155. #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
  156. static inline void __cpus_clear(cpumask_t *dstp, int nbits)
  157. {
  158. bitmap_zero(dstp->bits, nbits);
  159. }
  160. /* No static inline type checking - see Subtlety (1) above. */
  161. #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
  162. #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
  163. static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
  164. {
  165. return test_and_set_bit(cpu, addr->bits);
  166. }
  167. #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
  168. static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
  169. const cpumask_t *src2p, int nbits)
  170. {
  171. bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  172. }
  173. #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
  174. static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
  175. const cpumask_t *src2p, int nbits)
  176. {
  177. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  178. }
  179. #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
  180. static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
  181. const cpumask_t *src2p, int nbits)
  182. {
  183. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  184. }
  185. #define cpus_andnot(dst, src1, src2) \
  186. __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
  187. static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
  188. const cpumask_t *src2p, int nbits)
  189. {
  190. bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  191. }
  192. #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
  193. static inline void __cpus_complement(cpumask_t *dstp,
  194. const cpumask_t *srcp, int nbits)
  195. {
  196. bitmap_complement(dstp->bits, srcp->bits, nbits);
  197. }
  198. #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
  199. static inline int __cpus_equal(const cpumask_t *src1p,
  200. const cpumask_t *src2p, int nbits)
  201. {
  202. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  203. }
  204. #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
  205. static inline int __cpus_intersects(const cpumask_t *src1p,
  206. const cpumask_t *src2p, int nbits)
  207. {
  208. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  209. }
  210. #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
  211. static inline int __cpus_subset(const cpumask_t *src1p,
  212. const cpumask_t *src2p, int nbits)
  213. {
  214. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  215. }
  216. #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
  217. static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
  218. {
  219. return bitmap_empty(srcp->bits, nbits);
  220. }
  221. #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
  222. static inline int __cpus_full(const cpumask_t *srcp, int nbits)
  223. {
  224. return bitmap_full(srcp->bits, nbits);
  225. }
  226. #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
  227. static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
  228. {
  229. return bitmap_weight(srcp->bits, nbits);
  230. }
  231. #define cpus_shift_right(dst, src, n) \
  232. __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
  233. static inline void __cpus_shift_right(cpumask_t *dstp,
  234. const cpumask_t *srcp, int n, int nbits)
  235. {
  236. bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
  237. }
  238. #define cpus_shift_left(dst, src, n) \
  239. __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
  240. static inline void __cpus_shift_left(cpumask_t *dstp,
  241. const cpumask_t *srcp, int n, int nbits)
  242. {
  243. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  244. }
  245. /*
  246. * Special-case data structure for "single bit set only" constant CPU masks.
  247. *
  248. * We pre-generate all the 64 (or 32) possible bit positions, with enough
  249. * padding to the left and the right, and return the constant pointer
  250. * appropriately offset.
  251. */
  252. extern const unsigned long
  253. cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
  254. static inline const cpumask_t *get_cpu_mask(unsigned int cpu)
  255. {
  256. const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
  257. p -= cpu / BITS_PER_LONG;
  258. return (const cpumask_t *)p;
  259. }
  260. /*
  261. * In cases where we take the address of the cpumask immediately,
  262. * gcc optimizes it out (it's a constant) and there's no huge stack
  263. * variable created:
  264. */
  265. #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
  266. #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
  267. #if NR_CPUS <= BITS_PER_LONG
  268. #define CPU_MASK_ALL \
  269. (cpumask_t) { { \
  270. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  271. } }
  272. #define CPU_MASK_ALL_PTR (&CPU_MASK_ALL)
  273. #else
  274. #define CPU_MASK_ALL \
  275. (cpumask_t) { { \
  276. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  277. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  278. } }
  279. /* cpu_mask_all is in init/main.c */
  280. extern cpumask_t cpu_mask_all;
  281. #define CPU_MASK_ALL_PTR (&cpu_mask_all)
  282. #endif
  283. #define CPU_MASK_NONE \
  284. (cpumask_t) { { \
  285. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  286. } }
  287. #define CPU_MASK_CPU0 \
  288. (cpumask_t) { { \
  289. [0] = 1UL \
  290. } }
  291. #define cpus_addr(src) ((src).bits)
  292. #if NR_CPUS > BITS_PER_LONG
  293. #define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
  294. #define CPUMASK_FREE(m) kfree(m)
  295. #else
  296. #define CPUMASK_ALLOC(m) struct m _m, *m = &_m
  297. #define CPUMASK_FREE(m)
  298. #endif
  299. #define CPUMASK_PTR(v, m) cpumask_t *v = &(m->v)
  300. #define cpumask_scnprintf(buf, len, src) \
  301. __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
  302. static inline int __cpumask_scnprintf(char *buf, int len,
  303. const cpumask_t *srcp, int nbits)
  304. {
  305. return bitmap_scnprintf(buf, len, srcp->bits, nbits);
  306. }
  307. #define cpumask_parse_user(ubuf, ulen, dst) \
  308. __cpumask_parse_user((ubuf), (ulen), &(dst), NR_CPUS)
  309. static inline int __cpumask_parse_user(const char __user *buf, int len,
  310. cpumask_t *dstp, int nbits)
  311. {
  312. return bitmap_parse_user(buf, len, dstp->bits, nbits);
  313. }
  314. #define cpulist_scnprintf(buf, len, src) \
  315. __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
  316. static inline int __cpulist_scnprintf(char *buf, int len,
  317. const cpumask_t *srcp, int nbits)
  318. {
  319. return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
  320. }
  321. #define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
  322. static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
  323. {
  324. return bitmap_parselist(buf, dstp->bits, nbits);
  325. }
  326. #define cpu_remap(oldbit, old, new) \
  327. __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
  328. static inline int __cpu_remap(int oldbit,
  329. const cpumask_t *oldp, const cpumask_t *newp, int nbits)
  330. {
  331. return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
  332. }
  333. #define cpus_remap(dst, src, old, new) \
  334. __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
  335. static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
  336. const cpumask_t *oldp, const cpumask_t *newp, int nbits)
  337. {
  338. bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
  339. }
  340. #define cpus_onto(dst, orig, relmap) \
  341. __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
  342. static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
  343. const cpumask_t *relmapp, int nbits)
  344. {
  345. bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
  346. }
  347. #define cpus_fold(dst, orig, sz) \
  348. __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
  349. static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
  350. int sz, int nbits)
  351. {
  352. bitmap_fold(dstp->bits, origp->bits, sz, nbits);
  353. }
  354. #if NR_CPUS == 1
  355. #define nr_cpu_ids 1
  356. #define first_cpu(src) ({ (void)(src); 0; })
  357. #define next_cpu(n, src) ({ (void)(src); 1; })
  358. #define any_online_cpu(mask) 0
  359. #define for_each_cpu_mask(cpu, mask) \
  360. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
  361. #else /* NR_CPUS > 1 */
  362. extern int nr_cpu_ids;
  363. int __first_cpu(const cpumask_t *srcp);
  364. int __next_cpu(int n, const cpumask_t *srcp);
  365. int __any_online_cpu(const cpumask_t *mask);
  366. #define first_cpu(src) __first_cpu(&(src))
  367. #define next_cpu(n, src) __next_cpu((n), &(src))
  368. #define any_online_cpu(mask) __any_online_cpu(&(mask))
  369. #define for_each_cpu_mask(cpu, mask) \
  370. for ((cpu) = -1; \
  371. (cpu) = next_cpu((cpu), (mask)), \
  372. (cpu) < NR_CPUS; )
  373. #endif
  374. #if NR_CPUS <= 64
  375. #define next_cpu_nr(n, src) next_cpu(n, src)
  376. #define cpus_weight_nr(cpumask) cpus_weight(cpumask)
  377. #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
  378. #else /* NR_CPUS > 64 */
  379. int __next_cpu_nr(int n, const cpumask_t *srcp);
  380. #define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
  381. #define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
  382. #define for_each_cpu_mask_nr(cpu, mask) \
  383. for ((cpu) = -1; \
  384. (cpu) = next_cpu_nr((cpu), (mask)), \
  385. (cpu) < nr_cpu_ids; )
  386. #endif /* NR_CPUS > 64 */
  387. /*
  388. * The following particular system cpumasks and operations manage
  389. * possible, present, active and online cpus. Each of them is a fixed size
  390. * bitmap of size NR_CPUS.
  391. *
  392. * #ifdef CONFIG_HOTPLUG_CPU
  393. * cpu_possible_map - has bit 'cpu' set iff cpu is populatable
  394. * cpu_present_map - has bit 'cpu' set iff cpu is populated
  395. * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
  396. * cpu_active_map - has bit 'cpu' set iff cpu available to migration
  397. * #else
  398. * cpu_possible_map - has bit 'cpu' set iff cpu is populated
  399. * cpu_present_map - copy of cpu_possible_map
  400. * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
  401. * #endif
  402. *
  403. * In either case, NR_CPUS is fixed at compile time, as the static
  404. * size of these bitmaps. The cpu_possible_map is fixed at boot
  405. * time, as the set of CPU id's that it is possible might ever
  406. * be plugged in at anytime during the life of that system boot.
  407. * The cpu_present_map is dynamic(*), representing which CPUs
  408. * are currently plugged in. And cpu_online_map is the dynamic
  409. * subset of cpu_present_map, indicating those CPUs available
  410. * for scheduling.
  411. *
  412. * If HOTPLUG is enabled, then cpu_possible_map is forced to have
  413. * all NR_CPUS bits set, otherwise it is just the set of CPUs that
  414. * ACPI reports present at boot.
  415. *
  416. * If HOTPLUG is enabled, then cpu_present_map varies dynamically,
  417. * depending on what ACPI reports as currently plugged in, otherwise
  418. * cpu_present_map is just a copy of cpu_possible_map.
  419. *
  420. * (*) Well, cpu_present_map is dynamic in the hotplug case. If not
  421. * hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
  422. *
  423. * Subtleties:
  424. * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
  425. * assumption that their single CPU is online. The UP
  426. * cpu_{online,possible,present}_maps are placebos. Changing them
  427. * will have no useful affect on the following num_*_cpus()
  428. * and cpu_*() macros in the UP case. This ugliness is a UP
  429. * optimization - don't waste any instructions or memory references
  430. * asking if you're online or how many CPUs there are if there is
  431. * only one CPU.
  432. * 2) Most SMP arch's #define some of these maps to be some
  433. * other map specific to that arch. Therefore, the following
  434. * must be #define macros, not inlines. To see why, examine
  435. * the assembly code produced by the following. Note that
  436. * set1() writes phys_x_map, but set2() writes x_map:
  437. * int x_map, phys_x_map;
  438. * #define set1(a) x_map = a
  439. * inline void set2(int a) { x_map = a; }
  440. * #define x_map phys_x_map
  441. * main(){ set1(3); set2(5); }
  442. */
  443. extern cpumask_t cpu_possible_map;
  444. extern cpumask_t cpu_online_map;
  445. extern cpumask_t cpu_present_map;
  446. extern cpumask_t cpu_active_map;
  447. #if NR_CPUS > 1
  448. #define num_online_cpus() cpus_weight_nr(cpu_online_map)
  449. #define num_possible_cpus() cpus_weight_nr(cpu_possible_map)
  450. #define num_present_cpus() cpus_weight_nr(cpu_present_map)
  451. #define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
  452. #define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
  453. #define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
  454. #define cpu_active(cpu) cpu_isset((cpu), cpu_active_map)
  455. #else
  456. #define num_online_cpus() 1
  457. #define num_possible_cpus() 1
  458. #define num_present_cpus() 1
  459. #define cpu_online(cpu) ((cpu) == 0)
  460. #define cpu_possible(cpu) ((cpu) == 0)
  461. #define cpu_present(cpu) ((cpu) == 0)
  462. #define cpu_active(cpu) ((cpu) == 0)
  463. #endif
  464. #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
  465. #define for_each_possible_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_possible_map)
  466. #define for_each_online_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_online_map)
  467. #define for_each_present_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_present_map)
  468. #endif /* __LINUX_CPUMASK_H */