cpumask.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. * Returns 1 if @cpu is set in @cpumask, else returns 0
  250. *
  251. * No static inline type checking - see Subtlety (1) above.
  252. */
  253. #define cpumask_test_cpu(cpu, cpumask) \
  254. test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
  255. /**
  256. * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
  257. * @cpu: cpu number (< nr_cpu_ids)
  258. * @cpumask: the cpumask pointer
  259. *
  260. * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
  261. *
  262. * test_and_set_bit wrapper for cpumasks.
  263. */
  264. static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
  265. {
  266. return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
  267. }
  268. /**
  269. * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
  270. * @cpu: cpu number (< nr_cpu_ids)
  271. * @cpumask: the cpumask pointer
  272. *
  273. * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
  274. *
  275. * test_and_clear_bit wrapper for cpumasks.
  276. */
  277. static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
  278. {
  279. return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
  280. }
  281. /**
  282. * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
  283. * @dstp: the cpumask pointer
  284. */
  285. static inline void cpumask_setall(struct cpumask *dstp)
  286. {
  287. bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
  288. }
  289. /**
  290. * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
  291. * @dstp: the cpumask pointer
  292. */
  293. static inline void cpumask_clear(struct cpumask *dstp)
  294. {
  295. bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
  296. }
  297. /**
  298. * cpumask_and - *dstp = *src1p & *src2p
  299. * @dstp: the cpumask result
  300. * @src1p: the first input
  301. * @src2p: the second input
  302. *
  303. * If *@dstp is empty, returns 0, else returns 1
  304. */
  305. static inline int cpumask_and(struct cpumask *dstp,
  306. const struct cpumask *src1p,
  307. const struct cpumask *src2p)
  308. {
  309. return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
  310. cpumask_bits(src2p), nr_cpumask_bits);
  311. }
  312. /**
  313. * cpumask_or - *dstp = *src1p | *src2p
  314. * @dstp: the cpumask result
  315. * @src1p: the first input
  316. * @src2p: the second input
  317. */
  318. static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
  319. const struct cpumask *src2p)
  320. {
  321. bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
  322. cpumask_bits(src2p), nr_cpumask_bits);
  323. }
  324. /**
  325. * cpumask_xor - *dstp = *src1p ^ *src2p
  326. * @dstp: the cpumask result
  327. * @src1p: the first input
  328. * @src2p: the second input
  329. */
  330. static inline void cpumask_xor(struct cpumask *dstp,
  331. const struct cpumask *src1p,
  332. const struct cpumask *src2p)
  333. {
  334. bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
  335. cpumask_bits(src2p), nr_cpumask_bits);
  336. }
  337. /**
  338. * cpumask_andnot - *dstp = *src1p & ~*src2p
  339. * @dstp: the cpumask result
  340. * @src1p: the first input
  341. * @src2p: the second input
  342. *
  343. * If *@dstp is empty, returns 0, else returns 1
  344. */
  345. static inline int cpumask_andnot(struct cpumask *dstp,
  346. const struct cpumask *src1p,
  347. const struct cpumask *src2p)
  348. {
  349. return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
  350. cpumask_bits(src2p), nr_cpumask_bits);
  351. }
  352. /**
  353. * cpumask_complement - *dstp = ~*srcp
  354. * @dstp: the cpumask result
  355. * @srcp: the input to invert
  356. */
  357. static inline void cpumask_complement(struct cpumask *dstp,
  358. const struct cpumask *srcp)
  359. {
  360. bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
  361. nr_cpumask_bits);
  362. }
  363. /**
  364. * cpumask_equal - *src1p == *src2p
  365. * @src1p: the first input
  366. * @src2p: the second input
  367. */
  368. static inline bool cpumask_equal(const struct cpumask *src1p,
  369. const struct cpumask *src2p)
  370. {
  371. return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
  372. nr_cpumask_bits);
  373. }
  374. /**
  375. * cpumask_intersects - (*src1p & *src2p) != 0
  376. * @src1p: the first input
  377. * @src2p: the second input
  378. */
  379. static inline bool cpumask_intersects(const struct cpumask *src1p,
  380. const struct cpumask *src2p)
  381. {
  382. return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
  383. nr_cpumask_bits);
  384. }
  385. /**
  386. * cpumask_subset - (*src1p & ~*src2p) == 0
  387. * @src1p: the first input
  388. * @src2p: the second input
  389. *
  390. * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
  391. */
  392. static inline int cpumask_subset(const struct cpumask *src1p,
  393. const struct cpumask *src2p)
  394. {
  395. return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
  396. nr_cpumask_bits);
  397. }
  398. /**
  399. * cpumask_empty - *srcp == 0
  400. * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
  401. */
  402. static inline bool cpumask_empty(const struct cpumask *srcp)
  403. {
  404. return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
  405. }
  406. /**
  407. * cpumask_full - *srcp == 0xFFFFFFFF...
  408. * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
  409. */
  410. static inline bool cpumask_full(const struct cpumask *srcp)
  411. {
  412. return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
  413. }
  414. /**
  415. * cpumask_weight - Count of bits in *srcp
  416. * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
  417. */
  418. static inline unsigned int cpumask_weight(const struct cpumask *srcp)
  419. {
  420. return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
  421. }
  422. /**
  423. * cpumask_shift_right - *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_right(struct cpumask *dstp,
  429. const struct cpumask *srcp, int n)
  430. {
  431. bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
  432. nr_cpumask_bits);
  433. }
  434. /**
  435. * cpumask_shift_left - *dstp = *srcp << n
  436. * @dstp: the cpumask result
  437. * @srcp: the input to shift
  438. * @n: the number of bits to shift by
  439. */
  440. static inline void cpumask_shift_left(struct cpumask *dstp,
  441. const struct cpumask *srcp, int n)
  442. {
  443. bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
  444. nr_cpumask_bits);
  445. }
  446. /**
  447. * cpumask_copy - *dstp = *srcp
  448. * @dstp: the result
  449. * @srcp: the input cpumask
  450. */
  451. static inline void cpumask_copy(struct cpumask *dstp,
  452. const struct cpumask *srcp)
  453. {
  454. bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
  455. }
  456. /**
  457. * cpumask_any - pick a "random" cpu from *srcp
  458. * @srcp: the input cpumask
  459. *
  460. * Returns >= nr_cpu_ids if no cpus set.
  461. */
  462. #define cpumask_any(srcp) cpumask_first(srcp)
  463. /**
  464. * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
  465. * @src1p: the first input
  466. * @src2p: the second input
  467. *
  468. * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
  469. */
  470. #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
  471. /**
  472. * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
  473. * @mask1: the first input cpumask
  474. * @mask2: the second input cpumask
  475. *
  476. * Returns >= nr_cpu_ids if no cpus set.
  477. */
  478. #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
  479. /**
  480. * cpumask_of - the cpumask containing just a given cpu
  481. * @cpu: the cpu (<= nr_cpu_ids)
  482. */
  483. #define cpumask_of(cpu) (get_cpu_mask(cpu))
  484. /**
  485. * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
  486. * @buf: the buffer to sprintf into
  487. * @len: the length of the buffer
  488. * @srcp: the cpumask to print
  489. *
  490. * If len is zero, returns zero. Otherwise returns the length of the
  491. * (nul-terminated) @buf string.
  492. */
  493. static inline int cpumask_scnprintf(char *buf, int len,
  494. const struct cpumask *srcp)
  495. {
  496. return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
  497. }
  498. /**
  499. * cpumask_parse_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_parse_user(const char __user *buf, int len,
  507. struct cpumask *dstp)
  508. {
  509. return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
  510. }
  511. /**
  512. * cpumask_parselist_user - extract a cpumask from a user string
  513. * @buf: the buffer to extract from
  514. * @len: the length of the buffer
  515. * @dstp: the cpumask to set.
  516. *
  517. * Returns -errno, or 0 for success.
  518. */
  519. static inline int cpumask_parselist_user(const char __user *buf, int len,
  520. struct cpumask *dstp)
  521. {
  522. return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
  523. nr_cpumask_bits);
  524. }
  525. /**
  526. * cpulist_scnprintf - print a cpumask into a string as comma-separated list
  527. * @buf: the buffer to sprintf into
  528. * @len: the length of the buffer
  529. * @srcp: the cpumask to print
  530. *
  531. * If len is zero, returns zero. Otherwise returns the length of the
  532. * (nul-terminated) @buf string.
  533. */
  534. static inline int cpulist_scnprintf(char *buf, int len,
  535. const struct cpumask *srcp)
  536. {
  537. return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
  538. nr_cpumask_bits);
  539. }
  540. /**
  541. * cpulist_parse - extract a cpumask from a user string of ranges
  542. * @buf: the buffer to extract from
  543. * @dstp: the cpumask to set.
  544. *
  545. * Returns -errno, or 0 for success.
  546. */
  547. static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
  548. {
  549. return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
  550. }
  551. /**
  552. * cpumask_size - size to allocate for a 'struct cpumask' in bytes
  553. *
  554. * This will eventually be a runtime variable, depending on nr_cpu_ids.
  555. */
  556. static inline size_t cpumask_size(void)
  557. {
  558. /* FIXME: Once all cpumask assignments are eliminated, this
  559. * can be nr_cpumask_bits */
  560. return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
  561. }
  562. /*
  563. * cpumask_var_t: struct cpumask for stack usage.
  564. *
  565. * Oh, the wicked games we play! In order to make kernel coding a
  566. * little more difficult, we typedef cpumask_var_t to an array or a
  567. * pointer: doing &mask on an array is a noop, so it still works.
  568. *
  569. * ie.
  570. * cpumask_var_t tmpmask;
  571. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  572. * return -ENOMEM;
  573. *
  574. * ... use 'tmpmask' like a normal struct cpumask * ...
  575. *
  576. * free_cpumask_var(tmpmask);
  577. *
  578. *
  579. * However, one notable exception is there. alloc_cpumask_var() allocates
  580. * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
  581. * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
  582. *
  583. * cpumask_var_t tmpmask;
  584. * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  585. * return -ENOMEM;
  586. *
  587. * var = *tmpmask;
  588. *
  589. * This code makes NR_CPUS length memcopy and brings to a memory corruption.
  590. * cpumask_copy() provide safe copy functionality.
  591. */
  592. #ifdef CONFIG_CPUMASK_OFFSTACK
  593. typedef struct cpumask *cpumask_var_t;
  594. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  595. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  596. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
  597. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
  598. void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
  599. void free_cpumask_var(cpumask_var_t mask);
  600. void free_bootmem_cpumask_var(cpumask_var_t mask);
  601. #else
  602. typedef struct cpumask cpumask_var_t[1];
  603. static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  604. {
  605. return true;
  606. }
  607. static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  608. int node)
  609. {
  610. return true;
  611. }
  612. static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  613. {
  614. cpumask_clear(*mask);
  615. return true;
  616. }
  617. static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
  618. int node)
  619. {
  620. cpumask_clear(*mask);
  621. return true;
  622. }
  623. static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  624. {
  625. }
  626. static inline void free_cpumask_var(cpumask_var_t mask)
  627. {
  628. }
  629. static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
  630. {
  631. }
  632. #endif /* CONFIG_CPUMASK_OFFSTACK */
  633. /* It's common to want to use cpu_all_mask in struct member initializers,
  634. * so it has to refer to an address rather than a pointer. */
  635. extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
  636. #define cpu_all_mask to_cpumask(cpu_all_bits)
  637. /* First bits of cpu_bit_bitmap are in fact unset. */
  638. #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
  639. #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
  640. #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
  641. #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
  642. /* Wrappers for arch boot code to manipulate normally-constant masks */
  643. void set_cpu_possible(unsigned int cpu, bool possible);
  644. void set_cpu_present(unsigned int cpu, bool present);
  645. void set_cpu_online(unsigned int cpu, bool online);
  646. void set_cpu_active(unsigned int cpu, bool active);
  647. void init_cpu_present(const struct cpumask *src);
  648. void init_cpu_possible(const struct cpumask *src);
  649. void init_cpu_online(const struct cpumask *src);
  650. /**
  651. * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
  652. * @bitmap: the bitmap
  653. *
  654. * There are a few places where cpumask_var_t isn't appropriate and
  655. * static cpumasks must be used (eg. very early boot), yet we don't
  656. * expose the definition of 'struct cpumask'.
  657. *
  658. * This does the conversion, and can be used as a constant initializer.
  659. */
  660. #define to_cpumask(bitmap) \
  661. ((struct cpumask *)(1 ? (bitmap) \
  662. : (void *)sizeof(__check_is_bitmap(bitmap))))
  663. static inline int __check_is_bitmap(const unsigned long *bitmap)
  664. {
  665. return 1;
  666. }
  667. /*
  668. * Special-case data structure for "single bit set only" constant CPU masks.
  669. *
  670. * We pre-generate all the 64 (or 32) possible bit positions, with enough
  671. * padding to the left and the right, and return the constant pointer
  672. * appropriately offset.
  673. */
  674. extern const unsigned long
  675. cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
  676. static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
  677. {
  678. const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
  679. p -= cpu / BITS_PER_LONG;
  680. return to_cpumask(p);
  681. }
  682. #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
  683. #if NR_CPUS <= BITS_PER_LONG
  684. #define CPU_BITS_ALL \
  685. { \
  686. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  687. }
  688. #else /* NR_CPUS > BITS_PER_LONG */
  689. #define CPU_BITS_ALL \
  690. { \
  691. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  692. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  693. }
  694. #endif /* NR_CPUS > BITS_PER_LONG */
  695. /*
  696. *
  697. * From here down, all obsolete. Use cpumask_ variants!
  698. *
  699. */
  700. #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
  701. #define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
  702. #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
  703. #if NR_CPUS <= BITS_PER_LONG
  704. #define CPU_MASK_ALL \
  705. (cpumask_t) { { \
  706. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  707. } }
  708. #else
  709. #define CPU_MASK_ALL \
  710. (cpumask_t) { { \
  711. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  712. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  713. } }
  714. #endif
  715. #define CPU_MASK_NONE \
  716. (cpumask_t) { { \
  717. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  718. } }
  719. #define CPU_MASK_CPU0 \
  720. (cpumask_t) { { \
  721. [0] = 1UL \
  722. } }
  723. #if NR_CPUS == 1
  724. #define first_cpu(src) ({ (void)(src); 0; })
  725. #define next_cpu(n, src) ({ (void)(src); 1; })
  726. #define any_online_cpu(mask) 0
  727. #define for_each_cpu_mask(cpu, mask) \
  728. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
  729. #else /* NR_CPUS > 1 */
  730. int __first_cpu(const cpumask_t *srcp);
  731. int __next_cpu(int n, const cpumask_t *srcp);
  732. #define first_cpu(src) __first_cpu(&(src))
  733. #define next_cpu(n, src) __next_cpu((n), &(src))
  734. #define any_online_cpu(mask) cpumask_any_and(&mask, cpu_online_mask)
  735. #define for_each_cpu_mask(cpu, mask) \
  736. for ((cpu) = -1; \
  737. (cpu) = next_cpu((cpu), (mask)), \
  738. (cpu) < NR_CPUS; )
  739. #endif /* SMP */
  740. #if NR_CPUS <= 64
  741. #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
  742. #else /* NR_CPUS > 64 */
  743. int __next_cpu_nr(int n, const cpumask_t *srcp);
  744. #define for_each_cpu_mask_nr(cpu, mask) \
  745. for ((cpu) = -1; \
  746. (cpu) = __next_cpu_nr((cpu), &(mask)), \
  747. (cpu) < nr_cpu_ids; )
  748. #endif /* NR_CPUS > 64 */
  749. #define cpus_addr(src) ((src).bits)
  750. #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
  751. static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
  752. {
  753. set_bit(cpu, dstp->bits);
  754. }
  755. #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
  756. static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
  757. {
  758. clear_bit(cpu, dstp->bits);
  759. }
  760. #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
  761. static inline void __cpus_setall(cpumask_t *dstp, int nbits)
  762. {
  763. bitmap_fill(dstp->bits, nbits);
  764. }
  765. #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
  766. static inline void __cpus_clear(cpumask_t *dstp, int nbits)
  767. {
  768. bitmap_zero(dstp->bits, nbits);
  769. }
  770. /* No static inline type checking - see Subtlety (1) above. */
  771. #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
  772. #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
  773. static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
  774. {
  775. return test_and_set_bit(cpu, addr->bits);
  776. }
  777. #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
  778. static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
  779. const cpumask_t *src2p, int nbits)
  780. {
  781. return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  782. }
  783. #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
  784. static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
  785. const cpumask_t *src2p, int nbits)
  786. {
  787. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  788. }
  789. #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
  790. static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
  791. const cpumask_t *src2p, int nbits)
  792. {
  793. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  794. }
  795. #define cpus_andnot(dst, src1, src2) \
  796. __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
  797. static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
  798. const cpumask_t *src2p, int nbits)
  799. {
  800. return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  801. }
  802. #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
  803. static inline int __cpus_equal(const cpumask_t *src1p,
  804. const cpumask_t *src2p, int nbits)
  805. {
  806. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  807. }
  808. #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
  809. static inline int __cpus_intersects(const cpumask_t *src1p,
  810. const cpumask_t *src2p, int nbits)
  811. {
  812. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  813. }
  814. #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
  815. static inline int __cpus_subset(const cpumask_t *src1p,
  816. const cpumask_t *src2p, int nbits)
  817. {
  818. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  819. }
  820. #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
  821. static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
  822. {
  823. return bitmap_empty(srcp->bits, nbits);
  824. }
  825. #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
  826. static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
  827. {
  828. return bitmap_weight(srcp->bits, nbits);
  829. }
  830. #define cpus_shift_left(dst, src, n) \
  831. __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
  832. static inline void __cpus_shift_left(cpumask_t *dstp,
  833. const cpumask_t *srcp, int n, int nbits)
  834. {
  835. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  836. }
  837. #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
  838. #endif /* __LINUX_CPUMASK_H */