cpumask.h 15 KB

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