affinity.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <linux/interrupt.h>
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/cpu.h>
  5. static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
  6. int cpus_per_vec)
  7. {
  8. const struct cpumask *siblmsk;
  9. int cpu, sibl;
  10. for ( ; cpus_per_vec > 0; ) {
  11. cpu = cpumask_first(nmsk);
  12. /* Should not happen, but I'm too lazy to think about it */
  13. if (cpu >= nr_cpu_ids)
  14. return;
  15. cpumask_clear_cpu(cpu, nmsk);
  16. cpumask_set_cpu(cpu, irqmsk);
  17. cpus_per_vec--;
  18. /* If the cpu has siblings, use them first */
  19. siblmsk = topology_sibling_cpumask(cpu);
  20. for (sibl = -1; cpus_per_vec > 0; ) {
  21. sibl = cpumask_next(sibl, siblmsk);
  22. if (sibl >= nr_cpu_ids)
  23. break;
  24. if (!cpumask_test_and_clear_cpu(sibl, nmsk))
  25. continue;
  26. cpumask_set_cpu(sibl, irqmsk);
  27. cpus_per_vec--;
  28. }
  29. }
  30. }
  31. static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
  32. {
  33. int n, nodes = 0;
  34. /* Calculate the number of nodes in the supplied affinity mask */
  35. for_each_online_node(n) {
  36. if (cpumask_intersects(mask, cpumask_of_node(n))) {
  37. node_set(n, *nodemsk);
  38. nodes++;
  39. }
  40. }
  41. return nodes;
  42. }
  43. /**
  44. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  45. * @nvecs: The total number of vectors
  46. * @affd: Description of the affinity requirements
  47. *
  48. * Returns the masks pointer or NULL if allocation failed.
  49. */
  50. struct cpumask *
  51. irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
  52. {
  53. int n, nodes, vecs_per_node, cpus_per_vec, extra_vecs, curvec;
  54. int affv = nvecs - affd->pre_vectors - affd->post_vectors;
  55. int last_affv = affv + affd->pre_vectors;
  56. nodemask_t nodemsk = NODE_MASK_NONE;
  57. struct cpumask *masks;
  58. cpumask_var_t nmsk;
  59. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  60. return NULL;
  61. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  62. if (!masks)
  63. goto out;
  64. /* Fill out vectors at the beginning that don't need affinity */
  65. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  66. cpumask_copy(masks + curvec, irq_default_affinity);
  67. /* Stabilize the cpumasks */
  68. get_online_cpus();
  69. nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);
  70. /*
  71. * If the number of nodes in the mask is greater than or equal the
  72. * number of vectors we just spread the vectors across the nodes.
  73. */
  74. if (affv <= nodes) {
  75. for_each_node_mask(n, nodemsk) {
  76. cpumask_copy(masks + curvec, cpumask_of_node(n));
  77. if (++curvec == last_affv)
  78. break;
  79. }
  80. goto done;
  81. }
  82. /* Spread the vectors per node */
  83. vecs_per_node = affv / nodes;
  84. /* Account for rounding errors */
  85. extra_vecs = affv - (nodes * vecs_per_node);
  86. for_each_node_mask(n, nodemsk) {
  87. int ncpus, v, vecs_to_assign = vecs_per_node;
  88. /* Get the cpus on this node which are in the mask */
  89. cpumask_and(nmsk, cpu_online_mask, cpumask_of_node(n));
  90. /* Calculate the number of cpus per vector */
  91. ncpus = cpumask_weight(nmsk);
  92. for (v = 0; curvec < last_affv && v < vecs_to_assign;
  93. curvec++, v++) {
  94. cpus_per_vec = ncpus / vecs_to_assign;
  95. /* Account for extra vectors to compensate rounding errors */
  96. if (extra_vecs) {
  97. cpus_per_vec++;
  98. if (!--extra_vecs)
  99. vecs_per_node++;
  100. }
  101. irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
  102. }
  103. if (curvec >= last_affv)
  104. break;
  105. }
  106. done:
  107. put_online_cpus();
  108. /* Fill out vectors at the end that don't need affinity */
  109. for (; curvec < nvecs; curvec++)
  110. cpumask_copy(masks + curvec, irq_default_affinity);
  111. out:
  112. free_cpumask_var(nmsk);
  113. return masks;
  114. }
  115. /**
  116. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  117. * @maxvec: The maximum number of vectors available
  118. * @affd: Description of the affinity requirements
  119. */
  120. int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
  121. {
  122. int resv = affd->pre_vectors + affd->post_vectors;
  123. int vecs = maxvec - resv;
  124. int cpus;
  125. /* Stabilize the cpumasks */
  126. get_online_cpus();
  127. cpus = cpumask_weight(cpu_online_mask);
  128. put_online_cpus();
  129. return min(cpus, vecs) + resv;
  130. }