affinity.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <linux/interrupt.h>
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/cpu.h>
  5. static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
  6. int cpus_per_vec)
  7. {
  8. const struct cpumask *siblmsk;
  9. int cpu, sibl;
  10. for ( ; cpus_per_vec > 0; ) {
  11. cpu = cpumask_first(nmsk);
  12. /* Should not happen, but I'm too lazy to think about it */
  13. if (cpu >= nr_cpu_ids)
  14. return;
  15. cpumask_clear_cpu(cpu, nmsk);
  16. cpumask_set_cpu(cpu, irqmsk);
  17. cpus_per_vec--;
  18. /* If the cpu has siblings, use them first */
  19. siblmsk = topology_sibling_cpumask(cpu);
  20. for (sibl = -1; cpus_per_vec > 0; ) {
  21. sibl = cpumask_next(sibl, siblmsk);
  22. if (sibl >= nr_cpu_ids)
  23. break;
  24. if (!cpumask_test_and_clear_cpu(sibl, nmsk))
  25. continue;
  26. cpumask_set_cpu(sibl, irqmsk);
  27. cpus_per_vec--;
  28. }
  29. }
  30. }
  31. static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
  32. {
  33. int n, nodes = 0;
  34. /* Calculate the number of nodes in the supplied affinity mask */
  35. for_each_online_node(n) {
  36. if (cpumask_intersects(mask, cpumask_of_node(n))) {
  37. node_set(n, *nodemsk);
  38. nodes++;
  39. }
  40. }
  41. return nodes;
  42. }
  43. /**
  44. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  45. * @affinity: The affinity mask to spread. If NULL cpu_online_mask
  46. * is used
  47. * @nvecs: The number of vectors
  48. *
  49. * Returns the masks pointer or NULL if allocation failed.
  50. */
  51. struct cpumask *irq_create_affinity_masks(const struct cpumask *affinity,
  52. int nvec)
  53. {
  54. int n, nodes, vecs_per_node, cpus_per_vec, extra_vecs, curvec = 0;
  55. nodemask_t nodemsk = NODE_MASK_NONE;
  56. struct cpumask *masks;
  57. cpumask_var_t nmsk;
  58. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  59. return NULL;
  60. masks = kzalloc(nvec * sizeof(*masks), GFP_KERNEL);
  61. if (!masks)
  62. goto out;
  63. /* Stabilize the cpumasks */
  64. get_online_cpus();
  65. /* If the supplied affinity mask is NULL, use cpu online mask */
  66. if (!affinity)
  67. affinity = cpu_online_mask;
  68. nodes = get_nodes_in_cpumask(affinity, &nodemsk);
  69. /*
  70. * If the number of nodes in the mask is greater than or equal the
  71. * number of vectors we just spread the vectors across the nodes.
  72. */
  73. if (nvec <= nodes) {
  74. for_each_node_mask(n, nodemsk) {
  75. cpumask_copy(masks + curvec, cpumask_of_node(n));
  76. if (++curvec == nvec)
  77. break;
  78. }
  79. goto outonl;
  80. }
  81. /* Spread the vectors per node */
  82. vecs_per_node = nvec / nodes;
  83. /* Account for rounding errors */
  84. extra_vecs = nvec - (nodes * vecs_per_node);
  85. for_each_node_mask(n, nodemsk) {
  86. int ncpus, v, vecs_to_assign = vecs_per_node;
  87. /* Get the cpus on this node which are in the mask */
  88. cpumask_and(nmsk, affinity, cpumask_of_node(n));
  89. /* Calculate the number of cpus per vector */
  90. ncpus = cpumask_weight(nmsk);
  91. for (v = 0; curvec < nvec && v < vecs_to_assign; curvec++, v++) {
  92. cpus_per_vec = ncpus / vecs_to_assign;
  93. /* Account for extra vectors to compensate rounding errors */
  94. if (extra_vecs) {
  95. cpus_per_vec++;
  96. if (!--extra_vecs)
  97. vecs_per_node++;
  98. }
  99. irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
  100. }
  101. if (curvec >= nvec)
  102. break;
  103. }
  104. outonl:
  105. put_online_cpus();
  106. out:
  107. free_cpumask_var(nmsk);
  108. return masks;
  109. }
  110. /**
  111. * irq_calc_affinity_vectors - Calculate to optimal number of vectors for a given affinity mask
  112. * @affinity: The affinity mask to spread. If NULL cpu_online_mask
  113. * is used
  114. * @maxvec: The maximum number of vectors available
  115. */
  116. int irq_calc_affinity_vectors(const struct cpumask *affinity, int maxvec)
  117. {
  118. int cpus, ret;
  119. /* Stabilize the cpumasks */
  120. get_online_cpus();
  121. /* If the supplied affinity mask is NULL, use cpu online mask */
  122. if (!affinity)
  123. affinity = cpu_online_mask;
  124. cpus = cpumask_weight(affinity);
  125. ret = (cpus < maxvec) ? cpus : maxvec;
  126. put_online_cpus();
  127. return ret;
  128. }