cpumask.c 4.4 KB

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