cpumask.h 26 KB

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