cpumask.h 24 KB

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