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