blk-mq-cpumap.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <linux/kernel.h>
  2. #include <linux/threads.h>
  3. #include <linux/module.h>
  4. #include <linux/mm.h>
  5. #include <linux/smp.h>
  6. #include <linux/cpu.h>
  7. #include <linux/blk-mq.h>
  8. #include "blk.h"
  9. #include "blk-mq.h"
  10. static void show_map(unsigned int *map, unsigned int nr)
  11. {
  12. int i;
  13. pr_info("blk-mq: CPU -> queue map\n");
  14. for_each_online_cpu(i)
  15. pr_info(" CPU%2u -> Queue %u\n", i, map[i]);
  16. }
  17. static int cpu_to_queue_index(unsigned int nr_cpus, unsigned int nr_queues,
  18. const int cpu)
  19. {
  20. return cpu / ((nr_cpus + nr_queues - 1) / nr_queues);
  21. }
  22. static int get_first_sibling(unsigned int cpu)
  23. {
  24. unsigned int ret;
  25. ret = cpumask_first(topology_thread_cpumask(cpu));
  26. if (ret < nr_cpu_ids)
  27. return ret;
  28. return cpu;
  29. }
  30. int blk_mq_update_queue_map(unsigned int *map, unsigned int nr_queues)
  31. {
  32. unsigned int i, nr_cpus, nr_uniq_cpus, queue, first_sibling;
  33. cpumask_var_t cpus;
  34. if (!alloc_cpumask_var(&cpus, GFP_ATOMIC))
  35. return 1;
  36. cpumask_clear(cpus);
  37. nr_cpus = nr_uniq_cpus = 0;
  38. for_each_online_cpu(i) {
  39. nr_cpus++;
  40. first_sibling = get_first_sibling(i);
  41. if (!cpumask_test_cpu(first_sibling, cpus))
  42. nr_uniq_cpus++;
  43. cpumask_set_cpu(i, cpus);
  44. }
  45. queue = 0;
  46. for_each_possible_cpu(i) {
  47. if (!cpu_online(i)) {
  48. map[i] = 0;
  49. continue;
  50. }
  51. /*
  52. * Easy case - we have equal or more hardware queues. Or
  53. * there are no thread siblings to take into account. Do
  54. * 1:1 if enough, or sequential mapping if less.
  55. */
  56. if (nr_queues >= nr_cpus || nr_cpus == nr_uniq_cpus) {
  57. map[i] = cpu_to_queue_index(nr_cpus, nr_queues, queue);
  58. queue++;
  59. continue;
  60. }
  61. /*
  62. * Less then nr_cpus queues, and we have some number of
  63. * threads per cores. Map sibling threads to the same
  64. * queue.
  65. */
  66. first_sibling = get_first_sibling(i);
  67. if (first_sibling == i) {
  68. map[i] = cpu_to_queue_index(nr_uniq_cpus, nr_queues,
  69. queue);
  70. queue++;
  71. } else
  72. map[i] = map[first_sibling];
  73. }
  74. show_map(map, nr_cpus);
  75. free_cpumask_var(cpus);
  76. return 0;
  77. }
  78. unsigned int *blk_mq_make_queue_map(struct blk_mq_reg *reg)
  79. {
  80. unsigned int *map;
  81. /* If cpus are offline, map them to first hctx */
  82. map = kzalloc_node(sizeof(*map) * num_possible_cpus(), GFP_KERNEL,
  83. reg->numa_node);
  84. if (!map)
  85. return NULL;
  86. if (!blk_mq_update_queue_map(map, reg->nr_hw_queues))
  87. return map;
  88. kfree(map);
  89. return NULL;
  90. }