cpumask.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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() 1U
  86. #define num_possible_cpus() 1U
  87. #define num_present_cpus() 1U
  88. #define num_active_cpus() 1U
  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. * cpumask_parselist_user - extract a cpumask from a user string
  500. * @buf: the buffer to extract from
  501. * @len: the length of the buffer
  502. * @dstp: the cpumask to set.
  503. *
  504. * Returns -errno, or 0 for success.
  505. */
  506. static inline int cpumask_parselist_user(const char __user *buf, int len,
  507. struct cpumask *dstp)
  508. {
  509. return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
  510. nr_cpumask_bits);
  511. }
  512. /**
  513. * cpulist_scnprintf - print a cpumask into a string as comma-separated list
  514. * @buf: the buffer to sprintf into
  515. * @len: the length of the buffer
  516. * @srcp: the cpumask to print
  517. *
  518. * If len is zero, returns zero. Otherwise returns the length of the
  519. * (nul-terminated) @buf string.
  520. */
  521. static inline int cpulist_scnprintf(char *buf, int len,
  522. const struct cpumask *srcp)
  523. {
  524. return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
  525. nr_cpumask_bits);
  526. }
  527. /**
  528. * cpulist_parse_user - extract a cpumask from a user string of ranges
  529. * @buf: the buffer to extract from
  530. * @len: the length of the buffer
  531. * @dstp: the cpumask to set.
  532. *
  533. * Returns -errno, or 0 for success.
  534. */
  535. static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
  536. {
  537. return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
  538. }
  539. /**
  540. * cpumask_size - size to allocate for a 'struct cpumask' in bytes
  541. *
  542. * This will eventually be a runtime variable, depending on nr_cpu_ids.
  543. */
  544. static inline size_t cpumask_size(void)
  545. {
  546. /* FIXME: Once all cpumask assignments are eliminated, this
  547. * can be nr_cpumask_bits */
  548. return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
  549. }
  550. /*
  551. * cpumask_var_t: struct cpumask for stack usage.
  552. *
  553. * Oh, the wicked games we play! In order to make kernel coding a
  554. * little more difficult, we typedef cpumask_var_t to an array or a
  555. * pointer: doing &mask on an array is a noop, so it still works.
  556. *
  557. * ie.
  558. * cpumask_var_t tmpmask;
  559. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  560. * return -ENOMEM;
  561. *
  562. * ... use 'tmpmask' like a normal struct cpumask * ...
  563. *
  564. * free_cpumask_var(tmpmask);
  565. *
  566. *
  567. * However, one notable exception is there. alloc_cpumask_var() allocates
  568. * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
  569. * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
  570. *
  571. * cpumask_var_t tmpmask;
  572. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  573. * return -ENOMEM;
  574. *
  575. * var = *tmpmask;
  576. *
  577. * This code makes NR_CPUS length memcopy and brings to a memory corruption.
  578. * cpumask_copy() provide safe copy functionality.
  579. */
  580. #ifdef CONFIG_CPUMASK_OFFSTACK
  581. typedef struct cpumask *cpumask_var_t;
  582. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  583. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  584. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  585. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  586. void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
  587. void free_cpumask_var(cpumask_var_t mask);
  588. void free_bootmem_cpumask_var(cpumask_var_t mask);
  589. #else
  590. typedef struct cpumask cpumask_var_t[1];
  591. static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  592. {
  593. return true;
  594. }
  595. static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  596. int node)
  597. {
  598. return true;
  599. }
  600. static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  601. {
  602. cpumask_clear(*mask);
  603. return true;
  604. }
  605. static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  606. int node)
  607. {
  608. cpumask_clear(*mask);
  609. return true;
  610. }
  611. static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  612. {
  613. }
  614. static inline void free_cpumask_var(cpumask_var_t mask)
  615. {
  616. }
  617. static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
  618. {
  619. }
  620. #endif /* CONFIG_CPUMASK_OFFSTACK */
  621. /* It's common to want to use cpu_all_mask in struct member initializers,
  622. * so it has to refer to an address rather than a pointer. */
  623. extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
  624. #define cpu_all_mask to_cpumask(cpu_all_bits)
  625. /* First bits of cpu_bit_bitmap are in fact unset. */
  626. #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
  627. #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
  628. #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
  629. #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
  630. /* Wrappers for arch boot code to manipulate normally-constant masks */
  631. void set_cpu_possible(unsigned int cpu, bool possible);
  632. void set_cpu_present(unsigned int cpu, bool present);
  633. void set_cpu_online(unsigned int cpu, bool online);
  634. void set_cpu_active(unsigned int cpu, bool active);
  635. void init_cpu_present(const struct cpumask *src);
  636. void init_cpu_possible(const struct cpumask *src);
  637. void init_cpu_online(const struct cpumask *src);
  638. /**
  639. * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
  640. * @bitmap: the bitmap
  641. *
  642. * There are a few places where cpumask_var_t isn't appropriate and
  643. * static cpumasks must be used (eg. very early boot), yet we don't
  644. * expose the definition of 'struct cpumask'.
  645. *
  646. * This does the conversion, and can be used as a constant initializer.
  647. */
  648. #define to_cpumask(bitmap) \
  649. ((struct cpumask *)(1 ? (bitmap) \
  650. : (void *)sizeof(__check_is_bitmap(bitmap))))
  651. static inline int __check_is_bitmap(const unsigned long *bitmap)
  652. {
  653. return 1;
  654. }
  655. /*
  656. * Special-case data structure for "single bit set only" constant CPU masks.
  657. *
  658. * We pre-generate all the 64 (or 32) possible bit positions, with enough
  659. * padding to the left and the right, and return the constant pointer
  660. * appropriately offset.
  661. */
  662. extern const unsigned long
  663. cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
  664. static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
  665. {
  666. const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
  667. p -= cpu / BITS_PER_LONG;
  668. return to_cpumask(p);
  669. }
  670. #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
  671. #if NR_CPUS <= BITS_PER_LONG
  672. #define CPU_BITS_ALL \
  673. { \
  674. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  675. }
  676. #else /* NR_CPUS > BITS_PER_LONG */
  677. #define CPU_BITS_ALL \
  678. { \
  679. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  680. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  681. }
  682. #endif /* NR_CPUS > BITS_PER_LONG */
  683. /*
  684. *
  685. * From here down, all obsolete. Use cpumask_ variants!
  686. *
  687. */
  688. #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
  689. /* These strip const, as traditionally they weren't const. */
  690. #define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
  691. #define cpu_online_map (*(cpumask_t *)cpu_online_mask)
  692. #define cpu_present_map (*(cpumask_t *)cpu_present_mask)
  693. #define cpu_active_map (*(cpumask_t *)cpu_active_mask)
  694. #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
  695. #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
  696. #if NR_CPUS <= BITS_PER_LONG
  697. #define CPU_MASK_ALL \
  698. (cpumask_t) { { \
  699. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  700. } }
  701. #else
  702. #define CPU_MASK_ALL \
  703. (cpumask_t) { { \
  704. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  705. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  706. } }
  707. #endif
  708. #define CPU_MASK_NONE \
  709. (cpumask_t) { { \
  710. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  711. } }
  712. #define CPU_MASK_CPU0 \
  713. (cpumask_t) { { \
  714. [0] = 1UL \
  715. } }
  716. #if NR_CPUS == 1
  717. #define first_cpu(src) ({ (void)(src); 0; })
  718. #define next_cpu(n, src) ({ (void)(src); 1; })
  719. #define any_online_cpu(mask) 0
  720. #define for_each_cpu_mask(cpu, mask) \
  721. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
  722. #else /* NR_CPUS > 1 */
  723. int __first_cpu(const cpumask_t *srcp);
  724. int __next_cpu(int n, const cpumask_t *srcp);
  725. int __any_online_cpu(const cpumask_t *mask);
  726. #define first_cpu(src) __first_cpu(&(src))
  727. #define next_cpu(n, src) __next_cpu((n), &(src))
  728. #define any_online_cpu(mask) __any_online_cpu(&(mask))
  729. #define for_each_cpu_mask(cpu, mask) \
  730. for ((cpu) = -1; \
  731. (cpu) = next_cpu((cpu), (mask)), \
  732. (cpu) < NR_CPUS; )
  733. #endif /* SMP */
  734. #if NR_CPUS <= 64
  735. #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
  736. #else /* NR_CPUS > 64 */
  737. int __next_cpu_nr(int n, const cpumask_t *srcp);
  738. #define for_each_cpu_mask_nr(cpu, mask) \
  739. for ((cpu) = -1; \
  740. (cpu) = __next_cpu_nr((cpu), &(mask)), \
  741. (cpu) < nr_cpu_ids; )
  742. #endif /* NR_CPUS > 64 */
  743. #define cpus_addr(src) ((src).bits)
  744. #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
  745. static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
  746. {
  747. set_bit(cpu, dstp->bits);
  748. }
  749. #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
  750. static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
  751. {
  752. clear_bit(cpu, dstp->bits);
  753. }
  754. #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
  755. static inline void __cpus_setall(cpumask_t *dstp, int nbits)
  756. {
  757. bitmap_fill(dstp->bits, nbits);
  758. }
  759. #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
  760. static inline void __cpus_clear(cpumask_t *dstp, int nbits)
  761. {
  762. bitmap_zero(dstp->bits, nbits);
  763. }
  764. /* No static inline type checking - see Subtlety (1) above. */
  765. #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
  766. #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
  767. static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
  768. {
  769. return test_and_set_bit(cpu, addr->bits);
  770. }
  771. #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
  772. static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
  773. const cpumask_t *src2p, int nbits)
  774. {
  775. return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  776. }
  777. #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
  778. static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
  779. const cpumask_t *src2p, int nbits)
  780. {
  781. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  782. }
  783. #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
  784. static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
  785. const cpumask_t *src2p, int nbits)
  786. {
  787. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  788. }
  789. #define cpus_andnot(dst, src1, src2) \
  790. __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
  791. static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
  792. const cpumask_t *src2p, int nbits)
  793. {
  794. return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  795. }
  796. #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
  797. static inline int __cpus_equal(const cpumask_t *src1p,
  798. const cpumask_t *src2p, int nbits)
  799. {
  800. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  801. }
  802. #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
  803. static inline int __cpus_intersects(const cpumask_t *src1p,
  804. const cpumask_t *src2p, int nbits)
  805. {
  806. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  807. }
  808. #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
  809. static inline int __cpus_subset(const cpumask_t *src1p,
  810. const cpumask_t *src2p, int nbits)
  811. {
  812. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  813. }
  814. #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
  815. static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
  816. {
  817. return bitmap_empty(srcp->bits, nbits);
  818. }
  819. #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
  820. static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
  821. {
  822. return bitmap_weight(srcp->bits, nbits);
  823. }
  824. #define cpus_shift_left(dst, src, n) \
  825. __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
  826. static inline void __cpus_shift_left(cpumask_t *dstp,
  827. const cpumask_t *srcp, int n, int nbits)
  828. {
  829. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  830. }
  831. #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
  832. #endif /* __LINUX_CPUMASK_H */