cpumask.h 26 KB

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