cpumask.c 4.5 KB

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