cpumask.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/memblock.h>
  8. #include <linux/numa.h>
  9. /**
  10. * cpumask_next_wrap - helper to implement for_each_cpu_wrap
  11. * @n: the cpu prior to the place to search
  12. * @mask: the cpumask pointer
  13. * @start: the start point of the iteration
  14. * @wrap: assume @n crossing @start terminates the iteration
  15. *
  16. * Return: >= nr_cpu_ids on completion
  17. *
  18. * Note: the @wrap argument is required for the start condition when
  19. * we cannot assume @start is set in @mask.
  20. */
  21. unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
  22. {
  23. unsigned int next;
  24. again:
  25. next = cpumask_next(n, mask);
  26. if (wrap && n < start && next >= start) {
  27. return nr_cpumask_bits;
  28. } else if (next >= nr_cpumask_bits) {
  29. wrap = true;
  30. n = -1;
  31. goto again;
  32. }
  33. return next;
  34. }
  35. EXPORT_SYMBOL(cpumask_next_wrap);
  36. /* These are not inline because of header tangles. */
  37. #ifdef CONFIG_CPUMASK_OFFSTACK
  38. /**
  39. * alloc_cpumask_var_node - allocate a struct cpumask on a given node
  40. * @mask: pointer to cpumask_var_t where the cpumask is returned
  41. * @flags: GFP_ flags
  42. * @node: memory node from which to allocate or %NUMA_NO_NODE
  43. *
  44. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  45. * a nop returning a constant 1 (in <linux/cpumask.h>).
  46. *
  47. * Return: TRUE if memory allocation succeeded, FALSE otherwise.
  48. *
  49. * In addition, mask will be NULL if this fails. Note that gcc is
  50. * usually smart enough to know that mask can never be NULL if
  51. * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
  52. * too.
  53. */
  54. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  55. {
  56. *mask = kmalloc_node(cpumask_size(), flags, node);
  57. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  58. if (!*mask) {
  59. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  60. dump_stack();
  61. }
  62. #endif
  63. return *mask != NULL;
  64. }
  65. EXPORT_SYMBOL(alloc_cpumask_var_node);
  66. /**
  67. * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
  68. * @mask: pointer to cpumask_var_t where the cpumask is returned
  69. *
  70. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  71. * a nop (in <linux/cpumask.h>).
  72. * Either returns an allocated (zero-filled) cpumask, or causes the
  73. * system to panic.
  74. */
  75. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  76. {
  77. *mask = memblock_alloc(cpumask_size(), SMP_CACHE_BYTES);
  78. if (!*mask)
  79. panic("%s: Failed to allocate %u bytes\n", __func__,
  80. cpumask_size());
  81. }
  82. /**
  83. * free_cpumask_var - frees memory allocated for a struct cpumask.
  84. * @mask: cpumask to free
  85. *
  86. * This is safe on a NULL mask.
  87. */
  88. void free_cpumask_var(cpumask_var_t mask)
  89. {
  90. kfree(mask);
  91. }
  92. EXPORT_SYMBOL(free_cpumask_var);
  93. /**
  94. * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
  95. * @mask: cpumask to free
  96. */
  97. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  98. {
  99. memblock_free(mask, cpumask_size());
  100. }
  101. #endif
  102. /**
  103. * cpumask_local_spread - select the i'th cpu based on NUMA distances
  104. * @i: index number
  105. * @node: local numa_node
  106. *
  107. * Return: online CPU according to a numa aware policy; local cpus are returned
  108. * first, followed by non-local ones, then it wraps around.
  109. *
  110. * For those who wants to enumerate all CPUs based on their NUMA distances,
  111. * i.e. call this function in a loop, like:
  112. *
  113. * for (i = 0; i < num_online_cpus(); i++) {
  114. * cpu = cpumask_local_spread(i, node);
  115. * do_something(cpu);
  116. * }
  117. *
  118. * There's a better alternative based on for_each()-like iterators:
  119. *
  120. * for_each_numa_hop_mask(mask, node) {
  121. * for_each_cpu_andnot(cpu, mask, prev)
  122. * do_something(cpu);
  123. * prev = mask;
  124. * }
  125. *
  126. * It's simpler and more verbose than above. Complexity of iterator-based
  127. * enumeration is O(sched_domains_numa_levels * nr_cpu_ids), while
  128. * cpumask_local_spread() when called for each cpu is
  129. * O(sched_domains_numa_levels * nr_cpu_ids * log(nr_cpu_ids)).
  130. */
  131. unsigned int cpumask_local_spread(unsigned int i, int node)
  132. {
  133. unsigned int cpu;
  134. /* Wrap: we always want a cpu. */
  135. i %= num_online_cpus();
  136. cpu = sched_numa_find_nth_cpu(cpu_online_mask, i, node);
  137. WARN_ON(cpu >= nr_cpu_ids);
  138. return cpu;
  139. }
  140. EXPORT_SYMBOL(cpumask_local_spread);
  141. static DEFINE_PER_CPU(int, distribute_cpu_mask_prev);
  142. /**
  143. * cpumask_any_and_distribute - Return an arbitrary cpu within src1p & src2p.
  144. * @src1p: first &cpumask for intersection
  145. * @src2p: second &cpumask for intersection
  146. *
  147. * Iterated calls using the same srcp1 and srcp2 will be distributed within
  148. * their intersection.
  149. *
  150. * Return: >= nr_cpu_ids if the intersection is empty.
  151. */
  152. unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
  153. const struct cpumask *src2p)
  154. {
  155. unsigned int next, prev;
  156. /* NOTE: our first selection will skip 0. */
  157. prev = __this_cpu_read(distribute_cpu_mask_prev);
  158. next = find_next_and_bit_wrap(cpumask_bits(src1p), cpumask_bits(src2p),
  159. nr_cpumask_bits, prev + 1);
  160. if (next < nr_cpu_ids)
  161. __this_cpu_write(distribute_cpu_mask_prev, next);
  162. return next;
  163. }
  164. EXPORT_SYMBOL(cpumask_any_and_distribute);
  165. /**
  166. * cpumask_any_distribute - Return an arbitrary cpu from srcp
  167. * @srcp: &cpumask for selection
  168. *
  169. * Return: >= nr_cpu_ids if the intersection is empty.
  170. */
  171. unsigned int cpumask_any_distribute(const struct cpumask *srcp)
  172. {
  173. unsigned int next, prev;
  174. /* NOTE: our first selection will skip 0. */
  175. prev = __this_cpu_read(distribute_cpu_mask_prev);
  176. next = find_next_bit_wrap(cpumask_bits(srcp), nr_cpumask_bits, prev + 1);
  177. if (next < nr_cpu_ids)
  178. __this_cpu_write(distribute_cpu_mask_prev, next);
  179. return next;
  180. }
  181. EXPORT_SYMBOL(cpumask_any_distribute);