cpumask.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/slab.h>
  3. #include <linux/kernel.h>
  4. #include <linux/bitops.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/export.h>
  7. #include <linux/bootmem.h>
  8. /**
  9. * cpumask_next - get the next cpu in a cpumask
  10. * @n: the cpu prior to the place to search (ie. return will be > @n)
  11. * @srcp: the cpumask pointer
  12. *
  13. * Returns >= nr_cpu_ids if no further cpus set.
  14. */
  15. unsigned int cpumask_next(int n, const struct cpumask *srcp)
  16. {
  17. /* -1 is a legal arg here. */
  18. if (n != -1)
  19. cpumask_check(n);
  20. return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1);
  21. }
  22. EXPORT_SYMBOL(cpumask_next);
  23. /**
  24. * cpumask_next_and - get the next cpu in *src1p & *src2p
  25. * @n: the cpu prior to the place to search (ie. return will be > @n)
  26. * @src1p: the first cpumask pointer
  27. * @src2p: the second cpumask pointer
  28. *
  29. * Returns >= nr_cpu_ids if no further cpus set in both.
  30. */
  31. int cpumask_next_and(int n, const struct cpumask *src1p,
  32. const struct cpumask *src2p)
  33. {
  34. /* -1 is a legal arg here. */
  35. if (n != -1)
  36. cpumask_check(n);
  37. return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
  38. nr_cpumask_bits, n + 1);
  39. }
  40. EXPORT_SYMBOL(cpumask_next_and);
  41. /**
  42. * cpumask_any_but - return a "random" in a cpumask, but not this one.
  43. * @mask: the cpumask to search
  44. * @cpu: the cpu to ignore.
  45. *
  46. * Often used to find any cpu but smp_processor_id() in a mask.
  47. * Returns >= nr_cpu_ids if no cpus set.
  48. */
  49. int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
  50. {
  51. unsigned int i;
  52. cpumask_check(cpu);
  53. for_each_cpu(i, mask)
  54. if (i != cpu)
  55. break;
  56. return i;
  57. }
  58. EXPORT_SYMBOL(cpumask_any_but);
  59. /**
  60. * cpumask_next_wrap - helper to implement for_each_cpu_wrap
  61. * @n: the cpu prior to the place to search
  62. * @mask: the cpumask pointer
  63. * @start: the start point of the iteration
  64. * @wrap: assume @n crossing @start terminates the iteration
  65. *
  66. * Returns >= nr_cpu_ids on completion
  67. *
  68. * Note: the @wrap argument is required for the start condition when
  69. * we cannot assume @start is set in @mask.
  70. */
  71. int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
  72. {
  73. int next;
  74. again:
  75. next = cpumask_next(n, mask);
  76. if (wrap && n < start && next >= start) {
  77. return nr_cpumask_bits;
  78. } else if (next >= nr_cpumask_bits) {
  79. wrap = true;
  80. n = -1;
  81. goto again;
  82. }
  83. return next;
  84. }
  85. EXPORT_SYMBOL(cpumask_next_wrap);
  86. /* These are not inline because of header tangles. */
  87. #ifdef CONFIG_CPUMASK_OFFSTACK
  88. /**
  89. * alloc_cpumask_var_node - allocate a struct cpumask on a given node
  90. * @mask: pointer to cpumask_var_t where the cpumask is returned
  91. * @flags: GFP_ flags
  92. *
  93. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  94. * a nop returning a constant 1 (in <linux/cpumask.h>)
  95. * Returns TRUE if memory allocation succeeded, FALSE otherwise.
  96. *
  97. * In addition, mask will be NULL if this fails. Note that gcc is
  98. * usually smart enough to know that mask can never be NULL if
  99. * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
  100. * too.
  101. */
  102. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  103. {
  104. *mask = kmalloc_node(cpumask_size(), flags, node);
  105. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  106. if (!*mask) {
  107. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  108. dump_stack();
  109. }
  110. #endif
  111. return *mask != NULL;
  112. }
  113. EXPORT_SYMBOL(alloc_cpumask_var_node);
  114. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  115. {
  116. return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
  117. }
  118. EXPORT_SYMBOL(zalloc_cpumask_var_node);
  119. /**
  120. * alloc_cpumask_var - allocate a struct cpumask
  121. * @mask: pointer to cpumask_var_t where the cpumask is returned
  122. * @flags: GFP_ flags
  123. *
  124. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  125. * a nop returning a constant 1 (in <linux/cpumask.h>).
  126. *
  127. * See alloc_cpumask_var_node.
  128. */
  129. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  130. {
  131. return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
  132. }
  133. EXPORT_SYMBOL(alloc_cpumask_var);
  134. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  135. {
  136. return alloc_cpumask_var(mask, flags | __GFP_ZERO);
  137. }
  138. EXPORT_SYMBOL(zalloc_cpumask_var);
  139. /**
  140. * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
  141. * @mask: pointer to cpumask_var_t where the cpumask is returned
  142. *
  143. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  144. * a nop (in <linux/cpumask.h>).
  145. * Either returns an allocated (zero-filled) cpumask, or causes the
  146. * system to panic.
  147. */
  148. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  149. {
  150. *mask = memblock_virt_alloc(cpumask_size(), 0);
  151. }
  152. /**
  153. * free_cpumask_var - frees memory allocated for a struct cpumask.
  154. * @mask: cpumask to free
  155. *
  156. * This is safe on a NULL mask.
  157. */
  158. void free_cpumask_var(cpumask_var_t mask)
  159. {
  160. kfree(mask);
  161. }
  162. EXPORT_SYMBOL(free_cpumask_var);
  163. /**
  164. * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
  165. * @mask: cpumask to free
  166. */
  167. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  168. {
  169. memblock_free_early(__pa(mask), cpumask_size());
  170. }
  171. #endif
  172. /**
  173. * cpumask_local_spread - select the i'th cpu with local numa cpu's first
  174. * @i: index number
  175. * @node: local numa_node
  176. *
  177. * This function selects an online CPU according to a numa aware policy;
  178. * local cpus are returned first, followed by non-local ones, then it
  179. * wraps around.
  180. *
  181. * It's not very efficient, but useful for setup.
  182. */
  183. unsigned int cpumask_local_spread(unsigned int i, int node)
  184. {
  185. int cpu;
  186. /* Wrap: we always want a cpu. */
  187. i %= num_online_cpus();
  188. if (node == -1) {
  189. for_each_cpu(cpu, cpu_online_mask)
  190. if (i-- == 0)
  191. return cpu;
  192. } else {
  193. /* NUMA first. */
  194. for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
  195. if (i-- == 0)
  196. return cpu;
  197. for_each_cpu(cpu, cpu_online_mask) {
  198. /* Skip NUMA nodes, done above. */
  199. if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
  200. continue;
  201. if (i-- == 0)
  202. return cpu;
  203. }
  204. }
  205. BUG();
  206. }
  207. EXPORT_SYMBOL(cpumask_local_spread);