cpumask.h 28 KB

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