Jens Axboe | 75bb462 | 2014-05-28 10:15:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * CPU <-> hardware queue mapping helpers |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Jens Axboe |
| 5 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/threads.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/smp.h> |
| 11 | #include <linux/cpu.h> |
| 12 | |
| 13 | #include <linux/blk-mq.h> |
| 14 | #include "blk.h" |
| 15 | #include "blk-mq.h" |
| 16 | |
Linus Torvalds | 03ffbcd | 2017-07-03 16:50:31 -0700 | [diff] [blame] | 17 | static int cpu_to_queue_index(unsigned int nr_queues, const int cpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 18 | { |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 19 | /* |
Christoph Hellwig | 76451d7 | 2017-07-18 17:04:40 +0200 | [diff] [blame] | 20 | * Non present CPU will be mapped to queue index 0. |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 21 | */ |
Christoph Hellwig | 76451d7 | 2017-07-18 17:04:40 +0200 | [diff] [blame] | 22 | if (!cpu_present(cpu)) |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 23 | return 0; |
| 24 | return cpu % nr_queues; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static int get_first_sibling(unsigned int cpu) |
| 28 | { |
| 29 | unsigned int ret; |
| 30 | |
Bartosz Golaszewski | 06931e6 | 2015-05-26 15:11:28 +0200 | [diff] [blame] | 31 | ret = cpumask_first(topology_sibling_cpumask(cpu)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 32 | if (ret < nr_cpu_ids) |
| 33 | return ret; |
| 34 | |
| 35 | return cpu; |
| 36 | } |
| 37 | |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 38 | int blk_mq_map_queues(struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 39 | { |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 40 | unsigned int *map = set->mq_map; |
| 41 | unsigned int nr_queues = set->nr_hw_queues; |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 42 | unsigned int cpu, first_sibling; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 43 | |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 44 | for_each_possible_cpu(cpu) { |
| 45 | /* |
| 46 | * First do sequential mapping between CPUs and queues. |
| 47 | * In case we still have CPUs to map, and we have some number of |
| 48 | * threads per cores then map sibling threads to the same queue for |
| 49 | * performace optimizations. |
| 50 | */ |
| 51 | if (cpu < nr_queues) { |
Linus Torvalds | 03ffbcd | 2017-07-03 16:50:31 -0700 | [diff] [blame] | 52 | map[cpu] = cpu_to_queue_index(nr_queues, cpu); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 53 | } else { |
| 54 | first_sibling = get_first_sibling(cpu); |
| 55 | if (first_sibling == cpu) |
Linus Torvalds | 03ffbcd | 2017-07-03 16:50:31 -0700 | [diff] [blame] | 56 | map[cpu] = cpu_to_queue_index(nr_queues, cpu); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 57 | else |
| 58 | map[cpu] = map[first_sibling]; |
| 59 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 62 | return 0; |
| 63 | } |
Christoph Hellwig | 9e5a7e2 | 2016-11-01 08:12:47 -0600 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(blk_mq_map_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 65 | |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 66 | /* |
| 67 | * We have no quick way of doing reverse lookups. This is only used at |
| 68 | * queue init time, so runtime isn't important. |
| 69 | */ |
| 70 | int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index) |
| 71 | { |
| 72 | int i; |
| 73 | |
| 74 | for_each_possible_cpu(i) { |
| 75 | if (index == mq_map[i]) |
Raghavendra K T | bffed45 | 2015-12-02 16:59:05 +0530 | [diff] [blame] | 76 | return local_memory_node(cpu_to_node(i)); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return NUMA_NO_NODE; |
| 80 | } |