affinity.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  96. return NULL;
  97. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  98. if (!masks)
  99. goto out;
  100. node_to_present_cpumask = alloc_node_to_present_cpumask();
  101. if (!node_to_present_cpumask)
  102. goto out;
  103. /* Fill out vectors at the beginning that don't need affinity */
  104. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  105. cpumask_copy(masks + curvec, irq_default_affinity);
  106. /* Stabilize the cpumasks */
  107. get_online_cpus();
  108. build_node_to_present_cpumask(node_to_present_cpumask);
  109. nodes = get_nodes_in_cpumask(node_to_present_cpumask, cpu_present_mask,
  110. &nodemsk);
  111. /*
  112. * If the number of nodes in the mask is greater than or equal the
  113. * number of vectors we just spread the vectors across the nodes.
  114. */
  115. if (affv <= nodes) {
  116. for_each_node_mask(n, nodemsk) {
  117. cpumask_copy(masks + curvec,
  118. node_to_present_cpumask[n]);
  119. if (++curvec == last_affv)
  120. break;
  121. }
  122. goto done;
  123. }
  124. for_each_node_mask(n, nodemsk) {
  125. int ncpus, v, vecs_to_assign, vecs_per_node;
  126. /* Spread the vectors per node */
  127. vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes;
  128. /* Get the cpus on this node which are in the mask */
  129. cpumask_and(nmsk, cpu_present_mask, node_to_present_cpumask[n]);
  130. /* Calculate the number of cpus per vector */
  131. ncpus = cpumask_weight(nmsk);
  132. vecs_to_assign = min(vecs_per_node, ncpus);
  133. /* Account for rounding errors */
  134. extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
  135. for (v = 0; curvec < last_affv && v < vecs_to_assign;
  136. curvec++, v++) {
  137. cpus_per_vec = ncpus / vecs_to_assign;
  138. /* Account for extra vectors to compensate rounding errors */
  139. if (extra_vecs) {
  140. cpus_per_vec++;
  141. --extra_vecs;
  142. }
  143. irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
  144. }
  145. if (curvec >= last_affv)
  146. break;
  147. --nodes;
  148. }
  149. done:
  150. put_online_cpus();
  151. /* Fill out vectors at the end that don't need affinity */
  152. for (; curvec < nvecs; curvec++)
  153. cpumask_copy(masks + curvec, irq_default_affinity);
  154. free_node_to_present_cpumask(node_to_present_cpumask);
  155. out:
  156. free_cpumask_var(nmsk);
  157. return masks;
  158. }
  159. /**
  160. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  161. * @maxvec: The maximum number of vectors available
  162. * @affd: Description of the affinity requirements
  163. */
  164. int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
  165. {
  166. int resv = affd->pre_vectors + affd->post_vectors;
  167. int vecs = maxvec - resv;
  168. int ret;
  169. get_online_cpus();
  170. ret = min_t(int, cpumask_weight(cpu_present_mask), vecs) + resv;
  171. put_online_cpus();
  172. return ret;
  173. }