cpumask.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (likely(slab_is_available()))
  87. *mask = kmalloc_node(cpumask_size(), flags, node);
  88. else {
  89. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  90. printk(KERN_ERR
  91. "=> alloc_cpumask_var: kmalloc not available!\n");
  92. #endif
  93. *mask = NULL;
  94. }
  95. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  96. if (!*mask) {
  97. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  98. dump_stack();
  99. }
  100. #endif
  101. /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */
  102. if (*mask) {
  103. unsigned char *ptr = (unsigned char *)cpumask_bits(*mask);
  104. unsigned int tail;
  105. tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long);
  106. memset(ptr + cpumask_size() - tail, 0, tail);
  107. }
  108. return *mask != NULL;
  109. }
  110. EXPORT_SYMBOL(alloc_cpumask_var_node);
  111. /**
  112. * alloc_cpumask_var - allocate a struct cpumask
  113. * @mask: pointer to cpumask_var_t where the cpumask is returned
  114. * @flags: GFP_ flags
  115. *
  116. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  117. * a nop returning a constant 1 (in <linux/cpumask.h>).
  118. *
  119. * See alloc_cpumask_var_node.
  120. */
  121. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  122. {
  123. return alloc_cpumask_var_node(mask, flags, numa_node_id());
  124. }
  125. EXPORT_SYMBOL(alloc_cpumask_var);
  126. /**
  127. * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
  128. * @mask: pointer to cpumask_var_t where the cpumask is returned
  129. *
  130. * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
  131. * a nop (in <linux/cpumask.h>).
  132. * Either returns an allocated (zero-filled) cpumask, or causes the
  133. * system to panic.
  134. */
  135. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  136. {
  137. *mask = alloc_bootmem(cpumask_size());
  138. }
  139. /**
  140. * free_cpumask_var - frees memory allocated for a struct cpumask.
  141. * @mask: cpumask to free
  142. *
  143. * This is safe on a NULL mask.
  144. */
  145. void free_cpumask_var(cpumask_var_t mask)
  146. {
  147. kfree(mask);
  148. }
  149. EXPORT_SYMBOL(free_cpumask_var);
  150. /**
  151. * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
  152. * @mask: cpumask to free
  153. */
  154. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  155. {
  156. free_bootmem((unsigned long)mask, cpumask_size());
  157. }
  158. #endif