cpumask.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 nr_cpu_ids
  38. #else
  39. #define nr_cpumask_bits 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_wrap(cpu, mask, start) \
  151. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
  152. #define for_each_cpu_and(cpu, mask, and) \
  153. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
  154. #else
  155. /**
  156. * cpumask_first - get the first cpu in a cpumask
  157. * @srcp: the cpumask pointer
  158. *
  159. * Returns >= nr_cpu_ids if no cpus set.
  160. */
  161. static inline unsigned int cpumask_first(const struct cpumask *srcp)
  162. {
  163. return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
  164. }
  165. /**
  166. * cpumask_next - get the next cpu in a cpumask
  167. * @n: the cpu prior to the place to search (ie. return will be > @n)
  168. * @srcp: the cpumask pointer
  169. *
  170. * Returns >= nr_cpu_ids if no further cpus set.
  171. */
  172. static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
  173. {
  174. /* -1 is a legal arg here. */
  175. if (n != -1)
  176. cpumask_check(n);
  177. return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
  178. }
  179. /**
  180. * cpumask_next_zero - get the next unset cpu in a cpumask
  181. * @n: the cpu prior to the place to search (ie. return will be > @n)
  182. * @srcp: the cpumask pointer
  183. *
  184. * Returns >= nr_cpu_ids if no further cpus unset.
  185. */
  186. static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
  187. {
  188. /* -1 is a legal arg here. */
  189. if (n != -1)
  190. cpumask_check(n);
  191. return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
  192. }
  193. int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
  194. int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
  195. unsigned int cpumask_local_spread(unsigned int i, int node);
  196. /**
  197. * for_each_cpu - iterate over every cpu in a mask
  198. * @cpu: the (optionally unsigned) integer iterator
  199. * @mask: the cpumask pointer
  200. *
  201. * After the loop, cpu is >= nr_cpu_ids.
  202. */
  203. #define for_each_cpu(cpu, mask) \
  204. for ((cpu) = -1; \
  205. (cpu) = cpumask_next((cpu), (mask)), \
  206. (cpu) < nr_cpu_ids;)
  207. /**
  208. * for_each_cpu_not - iterate over every cpu in a complemented mask
  209. * @cpu: the (optionally unsigned) integer iterator
  210. * @mask: the cpumask pointer
  211. *
  212. * After the loop, cpu is >= nr_cpu_ids.
  213. */
  214. #define for_each_cpu_not(cpu, mask) \
  215. for ((cpu) = -1; \
  216. (cpu) = cpumask_next_zero((cpu), (mask)), \
  217. (cpu) < nr_cpu_ids;)
  218. extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
  219. /**
  220. * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
  221. * @cpu: the (optionally unsigned) integer iterator
  222. * @mask: the cpumask poiter
  223. * @start: the start location
  224. *
  225. * The implementation does not assume any bit in @mask is set (including @start).
  226. *
  227. * After the loop, cpu is >= nr_cpu_ids.
  228. */
  229. #define for_each_cpu_wrap(cpu, mask, start) \
  230. for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false); \
  231. (cpu) < nr_cpumask_bits; \
  232. (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
  233. /**
  234. * for_each_cpu_and - iterate over every cpu in both masks
  235. * @cpu: the (optionally unsigned) integer iterator
  236. * @mask: the first cpumask pointer
  237. * @and: the second cpumask pointer
  238. *
  239. * This saves a temporary CPU mask in many places. It is equivalent to:
  240. * struct cpumask tmp;
  241. * cpumask_and(&tmp, &mask, &and);
  242. * for_each_cpu(cpu, &tmp)
  243. * ...
  244. *
  245. * After the loop, cpu is >= nr_cpu_ids.
  246. */
  247. #define for_each_cpu_and(cpu, mask, and) \
  248. for ((cpu) = -1; \
  249. (cpu) = cpumask_next_and((cpu), (mask), (and)), \
  250. (cpu) < nr_cpu_ids;)
  251. #endif /* SMP */
  252. #define CPU_BITS_NONE \
  253. { \
  254. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  255. }
  256. #define CPU_BITS_CPU0 \
  257. { \
  258. [0] = 1UL \
  259. }
  260. /**
  261. * cpumask_set_cpu - set a cpu in a cpumask
  262. * @cpu: cpu number (< nr_cpu_ids)
  263. * @dstp: the cpumask pointer
  264. */
  265. static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
  266. {
  267. set_bit(cpumask_check(cpu), cpumask_bits(dstp));
  268. }
  269. /**
  270. * cpumask_clear_cpu - clear a cpu in a cpumask
  271. * @cpu: cpu number (< nr_cpu_ids)
  272. * @dstp: the cpumask pointer
  273. */
  274. static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
  275. {
  276. clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
  277. }
  278. /**
  279. * cpumask_test_cpu - test for a cpu in a cpumask
  280. * @cpu: cpu number (< nr_cpu_ids)
  281. * @cpumask: the cpumask pointer
  282. *
  283. * Returns 1 if @cpu is set in @cpumask, else returns 0
  284. */
  285. static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
  286. {
  287. return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
  288. }
  289. /**
  290. * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
  291. * @cpu: cpu number (< nr_cpu_ids)
  292. * @cpumask: the cpumask pointer
  293. *
  294. * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
  295. *
  296. * test_and_set_bit wrapper for cpumasks.
  297. */
  298. static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
  299. {
  300. return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
  301. }
  302. /**
  303. * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
  304. * @cpu: cpu number (< nr_cpu_ids)
  305. * @cpumask: the cpumask pointer
  306. *
  307. * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
  308. *
  309. * test_and_clear_bit wrapper for cpumasks.
  310. */
  311. static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
  312. {
  313. return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
  314. }
  315. /**
  316. * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
  317. * @dstp: the cpumask pointer
  318. */
  319. static inline void cpumask_setall(struct cpumask *dstp)
  320. {
  321. bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
  322. }
  323. /**
  324. * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
  325. * @dstp: the cpumask pointer
  326. */
  327. static inline void cpumask_clear(struct cpumask *dstp)
  328. {
  329. bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
  330. }
  331. /**
  332. * cpumask_and - *dstp = *src1p & *src2p
  333. * @dstp: the cpumask result
  334. * @src1p: the first input
  335. * @src2p: the second input
  336. *
  337. * If *@dstp is empty, returns 0, else returns 1
  338. */
  339. static inline int cpumask_and(struct cpumask *dstp,
  340. const struct cpumask *src1p,
  341. const struct cpumask *src2p)
  342. {
  343. return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
  344. cpumask_bits(src2p), nr_cpumask_bits);
  345. }
  346. /**
  347. * cpumask_or - *dstp = *src1p | *src2p
  348. * @dstp: the cpumask result
  349. * @src1p: the first input
  350. * @src2p: the second input
  351. */
  352. static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
  353. const struct cpumask *src2p)
  354. {
  355. bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
  356. cpumask_bits(src2p), nr_cpumask_bits);
  357. }
  358. /**
  359. * cpumask_xor - *dstp = *src1p ^ *src2p
  360. * @dstp: the cpumask result
  361. * @src1p: the first input
  362. * @src2p: the second input
  363. */
  364. static inline void cpumask_xor(struct cpumask *dstp,
  365. const struct cpumask *src1p,
  366. const struct cpumask *src2p)
  367. {
  368. bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
  369. cpumask_bits(src2p), nr_cpumask_bits);
  370. }
  371. /**
  372. * cpumask_andnot - *dstp = *src1p & ~*src2p
  373. * @dstp: the cpumask result
  374. * @src1p: the first input
  375. * @src2p: the second input
  376. *
  377. * If *@dstp is empty, returns 0, else returns 1
  378. */
  379. static inline int cpumask_andnot(struct cpumask *dstp,
  380. const struct cpumask *src1p,
  381. const struct cpumask *src2p)
  382. {
  383. return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
  384. cpumask_bits(src2p), nr_cpumask_bits);
  385. }
  386. /**
  387. * cpumask_complement - *dstp = ~*srcp
  388. * @dstp: the cpumask result
  389. * @srcp: the input to invert
  390. */
  391. static inline void cpumask_complement(struct cpumask *dstp,
  392. const struct cpumask *srcp)
  393. {
  394. bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
  395. nr_cpumask_bits);
  396. }
  397. /**
  398. * cpumask_equal - *src1p == *src2p
  399. * @src1p: the first input
  400. * @src2p: the second input
  401. */
  402. static inline bool cpumask_equal(const struct cpumask *src1p,
  403. const struct cpumask *src2p)
  404. {
  405. return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
  406. nr_cpumask_bits);
  407. }
  408. /**
  409. * cpumask_intersects - (*src1p & *src2p) != 0
  410. * @src1p: the first input
  411. * @src2p: the second input
  412. */
  413. static inline bool cpumask_intersects(const struct cpumask *src1p,
  414. const struct cpumask *src2p)
  415. {
  416. return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
  417. nr_cpumask_bits);
  418. }
  419. /**
  420. * cpumask_subset - (*src1p & ~*src2p) == 0
  421. * @src1p: the first input
  422. * @src2p: the second input
  423. *
  424. * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
  425. */
  426. static inline int cpumask_subset(const struct cpumask *src1p,
  427. const struct cpumask *src2p)
  428. {
  429. return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
  430. nr_cpumask_bits);
  431. }
  432. /**
  433. * cpumask_empty - *srcp == 0
  434. * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
  435. */
  436. static inline bool cpumask_empty(const struct cpumask *srcp)
  437. {
  438. return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
  439. }
  440. /**
  441. * cpumask_full - *srcp == 0xFFFFFFFF...
  442. * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
  443. */
  444. static inline bool cpumask_full(const struct cpumask *srcp)
  445. {
  446. return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
  447. }
  448. /**
  449. * cpumask_weight - Count of bits in *srcp
  450. * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
  451. */
  452. static inline unsigned int cpumask_weight(const struct cpumask *srcp)
  453. {
  454. return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
  455. }
  456. /**
  457. * cpumask_shift_right - *dstp = *srcp >> n
  458. * @dstp: the cpumask result
  459. * @srcp: the input to shift
  460. * @n: the number of bits to shift by
  461. */
  462. static inline void cpumask_shift_right(struct cpumask *dstp,
  463. const struct cpumask *srcp, int n)
  464. {
  465. bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
  466. nr_cpumask_bits);
  467. }
  468. /**
  469. * cpumask_shift_left - *dstp = *srcp << n
  470. * @dstp: the cpumask result
  471. * @srcp: the input to shift
  472. * @n: the number of bits to shift by
  473. */
  474. static inline void cpumask_shift_left(struct cpumask *dstp,
  475. const struct cpumask *srcp, int n)
  476. {
  477. bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
  478. nr_cpumask_bits);
  479. }
  480. /**
  481. * cpumask_copy - *dstp = *srcp
  482. * @dstp: the result
  483. * @srcp: the input cpumask
  484. */
  485. static inline void cpumask_copy(struct cpumask *dstp,
  486. const struct cpumask *srcp)
  487. {
  488. bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
  489. }
  490. /**
  491. * cpumask_any - pick a "random" cpu from *srcp
  492. * @srcp: the input cpumask
  493. *
  494. * Returns >= nr_cpu_ids if no cpus set.
  495. */
  496. #define cpumask_any(srcp) cpumask_first(srcp)
  497. /**
  498. * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
  499. * @src1p: the first input
  500. * @src2p: the second input
  501. *
  502. * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
  503. */
  504. #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
  505. /**
  506. * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
  507. * @mask1: the first input cpumask
  508. * @mask2: the second input cpumask
  509. *
  510. * Returns >= nr_cpu_ids if no cpus set.
  511. */
  512. #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
  513. /**
  514. * cpumask_of - the cpumask containing just a given cpu
  515. * @cpu: the cpu (<= nr_cpu_ids)
  516. */
  517. #define cpumask_of(cpu) (get_cpu_mask(cpu))
  518. /**
  519. * cpumask_parse_user - extract a cpumask from a user string
  520. * @buf: the buffer to extract from
  521. * @len: the length of the buffer
  522. * @dstp: the cpumask to set.
  523. *
  524. * Returns -errno, or 0 for success.
  525. */
  526. static inline int cpumask_parse_user(const char __user *buf, int len,
  527. struct cpumask *dstp)
  528. {
  529. return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
  530. }
  531. /**
  532. * cpumask_parselist_user - extract a cpumask from a user string
  533. * @buf: the buffer to extract from
  534. * @len: the length of the buffer
  535. * @dstp: the cpumask to set.
  536. *
  537. * Returns -errno, or 0 for success.
  538. */
  539. static inline int cpumask_parselist_user(const char __user *buf, int len,
  540. struct cpumask *dstp)
  541. {
  542. return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
  543. nr_cpumask_bits);
  544. }
  545. /**
  546. * cpumask_parse - extract a cpumask from a string
  547. * @buf: the buffer to extract from
  548. * @dstp: the cpumask to set.
  549. *
  550. * Returns -errno, or 0 for success.
  551. */
  552. static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
  553. {
  554. char *nl = strchr(buf, '\n');
  555. unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
  556. return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
  557. }
  558. /**
  559. * cpulist_parse - extract a cpumask from a user string of ranges
  560. * @buf: the buffer to extract from
  561. * @dstp: the cpumask to set.
  562. *
  563. * Returns -errno, or 0 for success.
  564. */
  565. static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
  566. {
  567. return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
  568. }
  569. /**
  570. * cpumask_size - size to allocate for a 'struct cpumask' in bytes
  571. */
  572. static inline size_t cpumask_size(void)
  573. {
  574. return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
  575. }
  576. /*
  577. * cpumask_var_t: struct cpumask for stack usage.
  578. *
  579. * Oh, the wicked games we play! In order to make kernel coding a
  580. * little more difficult, we typedef cpumask_var_t to an array or a
  581. * pointer: doing &mask on an array is a noop, so it still works.
  582. *
  583. * ie.
  584. * cpumask_var_t tmpmask;
  585. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  586. * return -ENOMEM;
  587. *
  588. * ... use 'tmpmask' like a normal struct cpumask * ...
  589. *
  590. * free_cpumask_var(tmpmask);
  591. *
  592. *
  593. * However, one notable exception is there. alloc_cpumask_var() allocates
  594. * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
  595. * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
  596. *
  597. * cpumask_var_t tmpmask;
  598. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  599. * return -ENOMEM;
  600. *
  601. * var = *tmpmask;
  602. *
  603. * This code makes NR_CPUS length memcopy and brings to a memory corruption.
  604. * cpumask_copy() provide safe copy functionality.
  605. *
  606. * Note that there is another evil here: If you define a cpumask_var_t
  607. * as a percpu variable then the way to obtain the address of the cpumask
  608. * structure differently influences what this_cpu_* operation needs to be
  609. * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
  610. * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
  611. * other type of cpumask_var_t implementation is configured.
  612. */
  613. #ifdef CONFIG_CPUMASK_OFFSTACK
  614. typedef struct cpumask *cpumask_var_t;
  615. #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
  616. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  617. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  618. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  619. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  620. void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
  621. void free_cpumask_var(cpumask_var_t mask);
  622. void free_bootmem_cpumask_var(cpumask_var_t mask);
  623. static inline bool cpumask_available(cpumask_var_t mask)
  624. {
  625. return mask != NULL;
  626. }
  627. #else
  628. typedef struct cpumask cpumask_var_t[1];
  629. #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
  630. static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  631. {
  632. return true;
  633. }
  634. static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  635. int node)
  636. {
  637. return true;
  638. }
  639. static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  640. {
  641. cpumask_clear(*mask);
  642. return true;
  643. }
  644. static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  645. int node)
  646. {
  647. cpumask_clear(*mask);
  648. return true;
  649. }
  650. static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  651. {
  652. }
  653. static inline void free_cpumask_var(cpumask_var_t mask)
  654. {
  655. }
  656. static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
  657. {
  658. }
  659. static inline bool cpumask_available(cpumask_var_t mask)
  660. {
  661. return true;
  662. }
  663. #endif /* CONFIG_CPUMASK_OFFSTACK */
  664. /* It's common to want to use cpu_all_mask in struct member initializers,
  665. * so it has to refer to an address rather than a pointer. */
  666. extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
  667. #define cpu_all_mask to_cpumask(cpu_all_bits)
  668. /* First bits of cpu_bit_bitmap are in fact unset. */
  669. #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
  670. #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
  671. #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
  672. #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
  673. /* Wrappers for arch boot code to manipulate normally-constant masks */
  674. void init_cpu_present(const struct cpumask *src);
  675. void init_cpu_possible(const struct cpumask *src);
  676. void init_cpu_online(const struct cpumask *src);
  677. static inline void
  678. set_cpu_possible(unsigned int cpu, bool possible)
  679. {
  680. if (possible)
  681. cpumask_set_cpu(cpu, &__cpu_possible_mask);
  682. else
  683. cpumask_clear_cpu(cpu, &__cpu_possible_mask);
  684. }
  685. static inline void
  686. set_cpu_present(unsigned int cpu, bool present)
  687. {
  688. if (present)
  689. cpumask_set_cpu(cpu, &__cpu_present_mask);
  690. else
  691. cpumask_clear_cpu(cpu, &__cpu_present_mask);
  692. }
  693. static inline void
  694. set_cpu_online(unsigned int cpu, bool online)
  695. {
  696. if (online)
  697. cpumask_set_cpu(cpu, &__cpu_online_mask);
  698. else
  699. cpumask_clear_cpu(cpu, &__cpu_online_mask);
  700. }
  701. static inline void
  702. set_cpu_active(unsigned int cpu, bool active)
  703. {
  704. if (active)
  705. cpumask_set_cpu(cpu, &__cpu_active_mask);
  706. else
  707. cpumask_clear_cpu(cpu, &__cpu_active_mask);
  708. }
  709. /**
  710. * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
  711. * @bitmap: the bitmap
  712. *
  713. * There are a few places where cpumask_var_t isn't appropriate and
  714. * static cpumasks must be used (eg. very early boot), yet we don't
  715. * expose the definition of 'struct cpumask'.
  716. *
  717. * This does the conversion, and can be used as a constant initializer.
  718. */
  719. #define to_cpumask(bitmap) \
  720. ((struct cpumask *)(1 ? (bitmap) \
  721. : (void *)sizeof(__check_is_bitmap(bitmap))))
  722. static inline int __check_is_bitmap(const unsigned long *bitmap)
  723. {
  724. return 1;
  725. }
  726. /*
  727. * Special-case data structure for "single bit set only" constant CPU masks.
  728. *
  729. * We pre-generate all the 64 (or 32) possible bit positions, with enough
  730. * padding to the left and the right, and return the constant pointer
  731. * appropriately offset.
  732. */
  733. extern const unsigned long
  734. cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
  735. static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
  736. {
  737. const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
  738. p -= cpu / BITS_PER_LONG;
  739. return to_cpumask(p);
  740. }
  741. #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
  742. #if NR_CPUS <= BITS_PER_LONG
  743. #define CPU_BITS_ALL \
  744. { \
  745. [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
  746. }
  747. #else /* NR_CPUS > BITS_PER_LONG */
  748. #define CPU_BITS_ALL \
  749. { \
  750. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  751. [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
  752. }
  753. #endif /* NR_CPUS > BITS_PER_LONG */
  754. /**
  755. * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
  756. * as comma-separated list of cpus or hex values of cpumask
  757. * @list: indicates whether the cpumap must be list
  758. * @mask: the cpumask to copy
  759. * @buf: the buffer to copy into
  760. *
  761. * Returns the length of the (null-terminated) @buf string, zero if
  762. * nothing is copied.
  763. */
  764. static inline ssize_t
  765. cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
  766. {
  767. return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
  768. nr_cpu_ids);
  769. }
  770. #if NR_CPUS <= BITS_PER_LONG
  771. #define CPU_MASK_ALL \
  772. (cpumask_t) { { \
  773. [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
  774. } }
  775. #else
  776. #define CPU_MASK_ALL \
  777. (cpumask_t) { { \
  778. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  779. [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
  780. } }
  781. #endif /* NR_CPUS > BITS_PER_LONG */
  782. #define CPU_MASK_NONE \
  783. (cpumask_t) { { \
  784. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  785. } }
  786. #define CPU_MASK_CPU0 \
  787. (cpumask_t) { { \
  788. [0] = 1UL \
  789. } }
  790. #endif /* __LINUX_CPUMASK_H */