cpumask.h 23 KB

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