cpumask.h 25 KB

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