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/module.h>
  6. #include <linux/bootmem.h>
  7. int __first_cpu(const cpumask_t *srcp)
  8. {
  9. return min_t(int, NR_CPUS, find_first_bit(srcp->bits, NR_CPUS));
  10. }
  11. EXPORT_SYMBOL(__first_cpu);
  12. int __next_cpu(int n, const cpumask_t *srcp)
  13. {
  14. return min_t(int, NR_CPUS, find_next_bit(srcp->bits, NR_CPUS, n+1));
  15. }
  16. EXPORT_SYMBOL(__next_cpu);
  17. #if NR_CPUS > 64
  18. int __next_cpu_nr(int n, const cpumask_t *srcp)
  19. {
  20. return min_t(int, nr_cpu_ids,
  21. find_next_bit(srcp->bits, nr_cpu_ids, n+1));
  22. }
  23. EXPORT_SYMBOL(__next_cpu_nr);
  24. #endif
  25. int __any_online_cpu(const cpumask_t *mask)
  26. {
  27. int cpu;
  28. for_each_cpu_mask(cpu, *mask) {
  29. if (cpu_online(cpu))
  30. break;
  31. }
  32. return cpu;
  33. }
  34. EXPORT_SYMBOL(__any_online_cpu);
  35. /**
  36. * cpumask_next_and - get the next cpu in *src1p & *src2p
  37. * @n: the cpu prior to the place to search (ie. return will be > @n)
  38. * @src1p: the first cpumask pointer
  39. * @src2p: the second cpumask pointer
  40. *
  41. * Returns >= nr_cpu_ids if no further cpus set in both.
  42. */
  43. int cpumask_next_and(int n, const struct cpumask *src1p,
  44. const struct cpumask *src2p)
  45. {
  46. while ((n = cpumask_next(n, src1p)) < nr_cpu_ids)
  47. if (cpumask_test_cpu(n, src2p))
  48. break;
  49. return n;
  50. }
  51. EXPORT_SYMBOL(cpumask_next_and);
  52. /**
  53. * cpumask_any_but - return a "random" in a cpumask, but not this one.
  54. * @mask: the cpumask to search
  55. * @cpu: the cpu to ignore.
  56. *
  57. * Often used to find any cpu but smp_processor_id() in a mask.
  58. * Returns >= nr_cpu_ids if no cpus set.
  59. */
  60. int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
  61. {
  62. unsigned int i;
  63. cpumask_check(cpu);
  64. for_each_cpu(i, mask)
  65. if (i != cpu)
  66. break;
  67. return i;
  68. }
  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. /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */
  95. if (*mask) {
  96. unsigned char *ptr = (unsigned char *)cpumask_bits(*mask);
  97. unsigned int tail;
  98. tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long);
  99. memset(ptr + cpumask_size() - tail, 0, tail);
  100. }
  101. return *mask != NULL;
  102. }
  103. EXPORT_SYMBOL(alloc_cpumask_var_node);
  104. bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
  105. {
  106. return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
  107. }
  108. EXPORT_SYMBOL(zalloc_cpumask_var_node);
  109. /**
  110. * alloc_cpumask_var - allocate a struct cpumask
  111. * @mask: pointer to cpumask_var_t where the cpumask is returned
  112. * @flags: GFP_ flags
  113. *
  114. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  115. * a nop returning a constant 1 (in <linux/cpumask.h>).
  116. *
  117. * See alloc_cpumask_var_node.
  118. */
  119. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  120. {
  121. return alloc_cpumask_var_node(mask, flags, numa_node_id());
  122. }
  123. EXPORT_SYMBOL(alloc_cpumask_var);
  124. bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  125. {
  126. return alloc_cpumask_var(mask, flags | __GFP_ZERO);
  127. }
  128. EXPORT_SYMBOL(zalloc_cpumask_var);
  129. /**
  130. * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
  131. * @mask: pointer to cpumask_var_t where the cpumask is returned
  132. *
  133. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  134. * a nop (in <linux/cpumask.h>).
  135. * Either returns an allocated (zero-filled) cpumask, or causes the
  136. * system to panic.
  137. */
  138. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  139. {
  140. *mask = alloc_bootmem(cpumask_size());
  141. }
  142. /**
  143. * free_cpumask_var - frees memory allocated for a struct cpumask.
  144. * @mask: cpumask to free
  145. *
  146. * This is safe on a NULL mask.
  147. */
  148. void free_cpumask_var(cpumask_var_t mask)
  149. {
  150. kfree(mask);
  151. }
  152. EXPORT_SYMBOL(free_cpumask_var);
  153. /**
  154. * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
  155. * @mask: cpumask to free
  156. */
  157. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  158. {
  159. free_bootmem((unsigned long)mask, cpumask_size());
  160. }
  161. #endif