affinity.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <linux/group_cpus.h>
  11. static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
  12. {
  13. affd->nr_sets = 1;
  14. affd->set_size[0] = affvecs;
  15. }
  16. /**
  17. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  18. * @nvecs: The total number of vectors
  19. * @affd: Description of the affinity requirements
  20. *
  21. * Returns the irq_affinity_desc pointer or NULL if allocation failed.
  22. */
  23. struct irq_affinity_desc *
  24. irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
  25. {
  26. unsigned int affvecs, curvec, usedvecs, i;
  27. struct irq_affinity_desc *masks = NULL;
  28. /*
  29. * Determine the number of vectors which need interrupt affinities
  30. * assigned. If the pre/post request exhausts the available vectors
  31. * then nothing to do here except for invoking the calc_sets()
  32. * callback so the device driver can adjust to the situation.
  33. */
  34. if (nvecs > affd->pre_vectors + affd->post_vectors)
  35. affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
  36. else
  37. affvecs = 0;
  38. /*
  39. * Simple invocations do not provide a calc_sets() callback. Install
  40. * the generic one.
  41. */
  42. if (!affd->calc_sets)
  43. affd->calc_sets = default_calc_sets;
  44. /* Recalculate the sets */
  45. affd->calc_sets(affd, affvecs);
  46. if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
  47. return NULL;
  48. /* Nothing to assign? */
  49. if (!affvecs)
  50. return NULL;
  51. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  52. if (!masks)
  53. return NULL;
  54. /* Fill out vectors at the beginning that don't need affinity */
  55. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  56. cpumask_copy(&masks[curvec].mask, irq_default_affinity);
  57. /*
  58. * Spread on present CPUs starting from affd->pre_vectors. If we
  59. * have multiple sets, build each sets affinity mask separately.
  60. */
  61. for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
  62. unsigned int this_vecs = affd->set_size[i];
  63. int j;
  64. struct cpumask *result = group_cpus_evenly(this_vecs);
  65. if (!result) {
  66. kfree(masks);
  67. return NULL;
  68. }
  69. for (j = 0; j < this_vecs; j++)
  70. cpumask_copy(&masks[curvec + j].mask, &result[j]);
  71. kfree(result);
  72. curvec += this_vecs;
  73. usedvecs += this_vecs;
  74. }
  75. /* Fill out vectors at the end that don't need affinity */
  76. if (usedvecs >= affvecs)
  77. curvec = affd->pre_vectors + affvecs;
  78. else
  79. curvec = affd->pre_vectors + usedvecs;
  80. for (; curvec < nvecs; curvec++)
  81. cpumask_copy(&masks[curvec].mask, irq_default_affinity);
  82. /* Mark the managed interrupts */
  83. for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
  84. masks[i].is_managed = 1;
  85. return masks;
  86. }
  87. /**
  88. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  89. * @minvec: The minimum number of vectors available
  90. * @maxvec: The maximum number of vectors available
  91. * @affd: Description of the affinity requirements
  92. */
  93. unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
  94. const struct irq_affinity *affd)
  95. {
  96. unsigned int resv = affd->pre_vectors + affd->post_vectors;
  97. unsigned int set_vecs;
  98. if (resv > minvec)
  99. return 0;
  100. if (affd->calc_sets) {
  101. set_vecs = maxvec - resv;
  102. } else {
  103. cpus_read_lock();
  104. set_vecs = cpumask_weight(cpu_possible_mask);
  105. cpus_read_unlock();
  106. }
  107. return resv + min(set_vecs, maxvec - resv);
  108. }