cpumask.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
  71. {
  72. if (likely(slab_is_available()))
  73. *mask = kmalloc(cpumask_size(), flags);
  74. else {
  75. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  76. printk(KERN_ERR
  77. "=> alloc_cpumask_var: kmalloc not available!\n");
  78. dump_stack();
  79. #endif
  80. *mask = NULL;
  81. }
  82. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  83. if (!*mask) {
  84. printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
  85. dump_stack();
  86. }
  87. #endif
  88. return *mask != NULL;
  89. }
  90. EXPORT_SYMBOL(alloc_cpumask_var);
  91. void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
  92. {
  93. *mask = alloc_bootmem(cpumask_size());
  94. }
  95. void free_cpumask_var(cpumask_var_t mask)
  96. {
  97. kfree(mask);
  98. }
  99. EXPORT_SYMBOL(free_cpumask_var);
  100. void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  101. {
  102. free_bootmem((unsigned long)mask, cpumask_size());
  103. }
  104. #endif