cpumask.c 5.3 KB

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