cpumask.h 26 KB

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