affinity.c 5.4 KB

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