cpumask.h 25 KB

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