cpumask.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* These are not inline because of header tangles. */
  42. #ifdef CONFIG_CPUMASK_OFFSTACK
  43. /**
  44. * alloc_cpumask_var_node - allocate a struct cpumask on a given node
  45. * @mask: pointer to cpumask_var_t where the cpumask is returned
  46. * @flags: GFP_ flags
  47. *
  48. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  49. * a nop returning a constant 1 (in <linux/cpumask.h>)
  50. * Returns TRUE if memory allocation succeeded, FALSE otherwise.
  51. *
  52. * In addition, mask will be NULL if this fails. Note that gcc is
  53. * usually smart enough to know that mask can never be NULL if
  54. * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
  55. * too.
  56. */
  57. bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  58. {
  59. *mask = kmalloc_node(cpumask_size(), flags, node);
  60. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  61. if (!*mask) {
  62. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  63. dump_stack();
  64. }
  65. #endif
  66. return *mask != NULL;
  67. }
  68. EXPORT_SYMBOL(alloc_cpumask_var_node);
  69. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  70. {
  71. return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
  72. }
  73. EXPORT_SYMBOL(zalloc_cpumask_var_node);
  74. /**
  75. * alloc_cpumask_var - allocate a struct cpumask
  76. * @mask: pointer to cpumask_var_t where the cpumask is returned
  77. * @flags: GFP_ flags
  78. *
  79. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  80. * a nop returning a constant 1 (in <linux/cpumask.h>).
  81. *
  82. * See alloc_cpumask_var_node.
  83. */
  84. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  85. {
  86. return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
  87. }
  88. EXPORT_SYMBOL(alloc_cpumask_var);
  89. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  90. {
  91. return alloc_cpumask_var(mask, flags | __GFP_ZERO);
  92. }
  93. EXPORT_SYMBOL(zalloc_cpumask_var);
  94. /**
  95. * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
  96. * @mask: pointer to cpumask_var_t where the cpumask is returned
  97. *
  98. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  99. * a nop (in <linux/cpumask.h>).
  100. * Either returns an allocated (zero-filled) cpumask, or causes the
  101. * system to panic.
  102. */
  103. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  104. {
  105. *mask = memblock_virt_alloc(cpumask_size(), 0);
  106. }
  107. /**
  108. * free_cpumask_var - frees memory allocated for a struct cpumask.
  109. * @mask: cpumask to free
  110. *
  111. * This is safe on a NULL mask.
  112. */
  113. void free_cpumask_var(cpumask_var_t mask)
  114. {
  115. kfree(mask);
  116. }
  117. EXPORT_SYMBOL(free_cpumask_var);
  118. /**
  119. * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
  120. * @mask: cpumask to free
  121. */
  122. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  123. {
  124. memblock_free_early(__pa(mask), cpumask_size());
  125. }
  126. #endif
  127. /**
  128. * cpumask_local_spread - select the i'th cpu with local numa cpu's first
  129. * @i: index number
  130. * @node: local numa_node
  131. *
  132. * This function selects an online CPU according to a numa aware policy;
  133. * local cpus are returned first, followed by non-local ones, then it
  134. * wraps around.
  135. *
  136. * It's not very efficient, but useful for setup.
  137. */
  138. unsigned int cpumask_local_spread(unsigned int i, int node)
  139. {
  140. int cpu;
  141. /* Wrap: we always want a cpu. */
  142. i %= num_online_cpus();
  143. if (node == -1) {
  144. for_each_cpu(cpu, cpu_online_mask)
  145. if (i-- == 0)
  146. return cpu;
  147. } else {
  148. /* NUMA first. */
  149. for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
  150. if (i-- == 0)
  151. return cpu;
  152. for_each_cpu(cpu, cpu_online_mask) {
  153. /* Skip NUMA nodes, done above. */
  154. if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
  155. continue;
  156. if (i-- == 0)
  157. return cpu;
  158. }
  159. }
  160. BUG();
  161. }
  162. EXPORT_SYMBOL(cpumask_local_spread);