cpumask.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. for_each_cpu(i, mask)
  63. if (i != cpu)
  64. break;
  65. return i;
  66. }
  67. /* These are not inline because of header tangles. */
  68. #ifdef CONFIG_CPUMASK_OFFSTACK
  69. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  70. {
  71. if (likely(slab_is_available()))
  72. *mask = kmalloc(cpumask_size(), flags);
  73. else {
  74. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  75. printk(KERN_ERR
  76. "=> alloc_cpumask_var: kmalloc not available!\n");
  77. dump_stack();
  78. #endif
  79. *mask = NULL;
  80. }
  81. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  82. if (!*mask) {
  83. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  84. dump_stack();
  85. }
  86. #endif
  87. return *mask != NULL;
  88. }
  89. EXPORT_SYMBOL(alloc_cpumask_var);
  90. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  91. {
  92. *mask = alloc_bootmem(cpumask_size());
  93. }
  94. void free_cpumask_var(cpumask_var_t mask)
  95. {
  96. kfree(mask);
  97. }
  98. EXPORT_SYMBOL(free_cpumask_var);
  99. void free_bootmem_cpumask_var(cpumask_var_t mask)
  100. {
  101. free_bootmem((unsigned long)mask, cpumask_size());
  102. }
  103. #endif