cpumask.h 25 KB

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