cpumask.h 25 KB

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