affinity.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_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_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_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_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_cpumask[n])) {
  74. node_set(n, *nodemsk);
  75. nodes++;
  76. }
  77. }
  78. return nodes;
  79. }
  80. static int irq_build_affinity_masks(const struct irq_affinity *affd,
  81. int startvec, int numvecs,
  82. cpumask_var_t *node_to_cpumask,
  83. const struct cpumask *cpu_mask,
  84. struct cpumask *nmsk,
  85. struct cpumask *masks)
  86. {
  87. int n, nodes, cpus_per_vec, extra_vecs, done = 0;
  88. int last_affv = affd->pre_vectors + numvecs;
  89. int curvec = startvec;
  90. nodemask_t nodemsk = NODE_MASK_NONE;
  91. if (!cpumask_weight(cpu_mask))
  92. return 0;
  93. nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
  94. /*
  95. * If the number of nodes in the mask is greater than or equal the
  96. * number of vectors we just spread the vectors across the nodes.
  97. */
  98. if (numvecs <= nodes) {
  99. for_each_node_mask(n, nodemsk) {
  100. cpumask_or(masks + curvec, masks + curvec, node_to_cpumask[n]);
  101. if (++curvec == last_affv)
  102. curvec = affd->pre_vectors;
  103. }
  104. done = numvecs;
  105. goto out;
  106. }
  107. for_each_node_mask(n, nodemsk) {
  108. int ncpus, v, vecs_to_assign, vecs_per_node;
  109. /* Spread the vectors per node */
  110. vecs_per_node = (numvecs - (curvec - affd->pre_vectors)) / nodes;
  111. /* Get the cpus on this node which are in the mask */
  112. cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
  113. /* Calculate the number of cpus per vector */
  114. ncpus = cpumask_weight(nmsk);
  115. vecs_to_assign = min(vecs_per_node, ncpus);
  116. /* Account for rounding errors */
  117. extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
  118. for (v = 0; curvec < last_affv && v < vecs_to_assign;
  119. curvec++, v++) {
  120. cpus_per_vec = ncpus / vecs_to_assign;
  121. /* Account for extra vectors to compensate rounding errors */
  122. if (extra_vecs) {
  123. cpus_per_vec++;
  124. --extra_vecs;
  125. }
  126. irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
  127. }
  128. done += v;
  129. if (done >= numvecs)
  130. break;
  131. if (curvec >= last_affv)
  132. curvec = affd->pre_vectors;
  133. --nodes;
  134. }
  135. out:
  136. return done;
  137. }
  138. /**
  139. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  140. * @nvecs: The total number of vectors
  141. * @affd: Description of the affinity requirements
  142. *
  143. * Returns the masks pointer or NULL if allocation failed.
  144. */
  145. struct cpumask *
  146. irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
  147. {
  148. int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
  149. int curvec, usedvecs;
  150. cpumask_var_t nmsk, npresmsk, *node_to_cpumask;
  151. struct cpumask *masks = NULL;
  152. /*
  153. * If there aren't any vectors left after applying the pre/post
  154. * vectors don't bother with assigning affinity.
  155. */
  156. if (nvecs == affd->pre_vectors + affd->post_vectors)
  157. return NULL;
  158. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  159. return NULL;
  160. if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
  161. goto outcpumsk;
  162. node_to_cpumask = alloc_node_to_cpumask();
  163. if (!node_to_cpumask)
  164. goto outnpresmsk;
  165. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  166. if (!masks)
  167. goto outnodemsk;
  168. /* Fill out vectors at the beginning that don't need affinity */
  169. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  170. cpumask_copy(masks + curvec, irq_default_affinity);
  171. /* Stabilize the cpumasks */
  172. get_online_cpus();
  173. build_node_to_cpumask(node_to_cpumask);
  174. /* Spread on present CPUs starting from affd->pre_vectors */
  175. usedvecs = irq_build_affinity_masks(affd, curvec, affvecs,
  176. node_to_cpumask, cpu_present_mask,
  177. nmsk, masks);
  178. /*
  179. * Spread on non present CPUs starting from the next vector to be
  180. * handled. If the spreading of present CPUs already exhausted the
  181. * vector space, assign the non present CPUs to the already spread
  182. * out vectors.
  183. */
  184. if (usedvecs >= affvecs)
  185. curvec = affd->pre_vectors;
  186. else
  187. curvec = affd->pre_vectors + usedvecs;
  188. cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
  189. usedvecs += irq_build_affinity_masks(affd, curvec, affvecs,
  190. node_to_cpumask, npresmsk,
  191. nmsk, masks);
  192. put_online_cpus();
  193. /* Fill out vectors at the end that don't need affinity */
  194. if (usedvecs >= affvecs)
  195. curvec = affd->pre_vectors + affvecs;
  196. else
  197. curvec = affd->pre_vectors + usedvecs;
  198. for (; curvec < nvecs; curvec++)
  199. cpumask_copy(masks + curvec, irq_default_affinity);
  200. outnodemsk:
  201. free_node_to_cpumask(node_to_cpumask);
  202. outnpresmsk:
  203. free_cpumask_var(npresmsk);
  204. outcpumsk:
  205. free_cpumask_var(nmsk);
  206. return masks;
  207. }
  208. /**
  209. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  210. * @minvec: The minimum number of vectors available
  211. * @maxvec: The maximum number of vectors available
  212. * @affd: Description of the affinity requirements
  213. */
  214. int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
  215. {
  216. int resv = affd->pre_vectors + affd->post_vectors;
  217. int vecs = maxvec - resv;
  218. int ret;
  219. if (resv > minvec)
  220. return 0;
  221. get_online_cpus();
  222. ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv;
  223. put_online_cpus();
  224. return ret;
  225. }