affinity.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2016 Thomas Gleixner.
  3. * Copyright (C) 2016-2017 Christoph Hellwig.
  4. */
  5. #include <linux/interrupt.h>
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/cpu.h>
  9. static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
  10. int cpus_per_vec)
  11. {
  12. const struct cpumask *siblmsk;
  13. int cpu, sibl;
  14. for ( ; cpus_per_vec > 0; ) {
  15. cpu = cpumask_first(nmsk);
  16. /* Should not happen, but I'm too lazy to think about it */
  17. if (cpu >= nr_cpu_ids)
  18. return;
  19. cpumask_clear_cpu(cpu, nmsk);
  20. cpumask_set_cpu(cpu, irqmsk);
  21. cpus_per_vec--;
  22. /* If the cpu has siblings, use them first */
  23. siblmsk = topology_sibling_cpumask(cpu);
  24. for (sibl = -1; cpus_per_vec > 0; ) {
  25. sibl = cpumask_next(sibl, siblmsk);
  26. if (sibl >= nr_cpu_ids)
  27. break;
  28. if (!cpumask_test_and_clear_cpu(sibl, nmsk))
  29. continue;
  30. cpumask_set_cpu(sibl, irqmsk);
  31. cpus_per_vec--;
  32. }
  33. }
  34. }
  35. static cpumask_var_t *alloc_node_to_present_cpumask(void)
  36. {
  37. cpumask_var_t *masks;
  38. int node;
  39. masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
  40. if (!masks)
  41. return NULL;
  42. for (node = 0; node < nr_node_ids; node++) {
  43. if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
  44. goto out_unwind;
  45. }
  46. return masks;
  47. out_unwind:
  48. while (--node >= 0)
  49. free_cpumask_var(masks[node]);
  50. kfree(masks);
  51. return NULL;
  52. }
  53. static void free_node_to_present_cpumask(cpumask_var_t *masks)
  54. {
  55. int node;
  56. for (node = 0; node < nr_node_ids; node++)
  57. free_cpumask_var(masks[node]);
  58. kfree(masks);
  59. }
  60. static void build_node_to_present_cpumask(cpumask_var_t *masks)
  61. {
  62. int cpu;
  63. for_each_present_cpu(cpu)
  64. cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
  65. }
  66. static int get_nodes_in_cpumask(cpumask_var_t *node_to_present_cpumask,
  67. const struct cpumask *mask, nodemask_t *nodemsk)
  68. {
  69. int n, nodes = 0;
  70. /* Calculate the number of nodes in the supplied affinity mask */
  71. for_each_node(n) {
  72. if (cpumask_intersects(mask, node_to_present_cpumask[n])) {
  73. node_set(n, *nodemsk);
  74. nodes++;
  75. }
  76. }
  77. return nodes;
  78. }
  79. /**
  80. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  81. * @nvecs: The total number of vectors
  82. * @affd: Description of the affinity requirements
  83. *
  84. * Returns the masks pointer or NULL if allocation failed.
  85. */
  86. struct cpumask *
  87. irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
  88. {
  89. int n, nodes, cpus_per_vec, extra_vecs, curvec;
  90. int affv = nvecs - affd->pre_vectors - affd->post_vectors;
  91. int last_affv = affv + affd->pre_vectors;
  92. nodemask_t nodemsk = NODE_MASK_NONE;
  93. struct cpumask *masks;
  94. cpumask_var_t nmsk, *node_to_present_cpumask;
  95. /*
  96. * If there aren't any vectors left after applying the pre/post
  97. * vectors don't bother with assigning affinity.
  98. */
  99. if (!affv)
  100. return NULL;
  101. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  102. return NULL;
  103. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  104. if (!masks)
  105. goto out;
  106. node_to_present_cpumask = alloc_node_to_present_cpumask();
  107. if (!node_to_present_cpumask)
  108. goto out;
  109. /* Fill out vectors at the beginning that don't need affinity */
  110. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  111. cpumask_copy(masks + curvec, irq_default_affinity);
  112. /* Stabilize the cpumasks */
  113. get_online_cpus();
  114. build_node_to_present_cpumask(node_to_present_cpumask);
  115. nodes = get_nodes_in_cpumask(node_to_present_cpumask, cpu_present_mask,
  116. &nodemsk);
  117. /*
  118. * If the number of nodes in the mask is greater than or equal the
  119. * number of vectors we just spread the vectors across the nodes.
  120. */
  121. if (affv <= nodes) {
  122. for_each_node_mask(n, nodemsk) {
  123. cpumask_copy(masks + curvec,
  124. node_to_present_cpumask[n]);
  125. if (++curvec == last_affv)
  126. break;
  127. }
  128. goto done;
  129. }
  130. for_each_node_mask(n, nodemsk) {
  131. int ncpus, v, vecs_to_assign, vecs_per_node;
  132. /* Spread the vectors per node */
  133. vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes;
  134. /* Get the cpus on this node which are in the mask */
  135. cpumask_and(nmsk, cpu_present_mask, node_to_present_cpumask[n]);
  136. /* Calculate the number of cpus per vector */
  137. ncpus = cpumask_weight(nmsk);
  138. vecs_to_assign = min(vecs_per_node, ncpus);
  139. /* Account for rounding errors */
  140. extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
  141. for (v = 0; curvec < last_affv && v < vecs_to_assign;
  142. curvec++, v++) {
  143. cpus_per_vec = ncpus / vecs_to_assign;
  144. /* Account for extra vectors to compensate rounding errors */
  145. if (extra_vecs) {
  146. cpus_per_vec++;
  147. --extra_vecs;
  148. }
  149. irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
  150. }
  151. if (curvec >= last_affv)
  152. break;
  153. --nodes;
  154. }
  155. done:
  156. put_online_cpus();
  157. /* Fill out vectors at the end that don't need affinity */
  158. for (; curvec < nvecs; curvec++)
  159. cpumask_copy(masks + curvec, irq_default_affinity);
  160. free_node_to_present_cpumask(node_to_present_cpumask);
  161. out:
  162. free_cpumask_var(nmsk);
  163. return masks;
  164. }
  165. /**
  166. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  167. * @minvec: The minimum number of vectors available
  168. * @maxvec: The maximum number of vectors available
  169. * @affd: Description of the affinity requirements
  170. */
  171. int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
  172. {
  173. int resv = affd->pre_vectors + affd->post_vectors;
  174. int vecs = maxvec - resv;
  175. int ret;
  176. if (resv > minvec)
  177. return 0;
  178. get_online_cpus();
  179. ret = min_t(int, cpumask_weight(cpu_present_mask), vecs) + resv;
  180. put_online_cpus();
  181. return ret;
  182. }