Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | 75bb462 | 2014-05-28 10:15:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * CPU <-> hardware queue mapping helpers |
| 4 | * |
| 5 | * Copyright (C) 2013-2014 Jens Axboe |
| 6 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/threads.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/smp.h> |
| 12 | #include <linux/cpu.h> |
| 13 | |
| 14 | #include <linux/blk-mq.h> |
| 15 | #include "blk.h" |
| 16 | #include "blk-mq.h" |
| 17 | |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 18 | static int queue_index(struct blk_mq_queue_map *qmap, |
| 19 | unsigned int nr_queues, const int q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 20 | { |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 21 | return qmap->queue_offset + (q % nr_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static int get_first_sibling(unsigned int cpu) |
| 25 | { |
| 26 | unsigned int ret; |
| 27 | |
Bartosz Golaszewski | 06931e6 | 2015-05-26 15:11:28 +0200 | [diff] [blame] | 28 | ret = cpumask_first(topology_sibling_cpumask(cpu)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 29 | if (ret < nr_cpu_ids) |
| 30 | return ret; |
| 31 | |
| 32 | return cpu; |
| 33 | } |
| 34 | |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 35 | int blk_mq_map_queues(struct blk_mq_queue_map *qmap) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 36 | { |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 37 | unsigned int *map = qmap->mq_map; |
| 38 | unsigned int nr_queues = qmap->nr_queues; |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 39 | unsigned int cpu, first_sibling, q = 0; |
| 40 | |
| 41 | for_each_possible_cpu(cpu) |
| 42 | map[cpu] = -1; |
| 43 | |
| 44 | /* |
| 45 | * Spread queues among present CPUs first for minimizing |
| 46 | * count of dead queues which are mapped by all un-present CPUs |
| 47 | */ |
| 48 | for_each_present_cpu(cpu) { |
| 49 | if (q >= nr_queues) |
| 50 | break; |
| 51 | map[cpu] = queue_index(qmap, nr_queues, q++); |
| 52 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 53 | |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 54 | for_each_possible_cpu(cpu) { |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 55 | if (map[cpu] != -1) |
| 56 | continue; |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 57 | /* |
| 58 | * First do sequential mapping between CPUs and queues. |
| 59 | * In case we still have CPUs to map, and we have some number of |
Bart Van Assche | ef025d7 | 2019-05-30 17:00:52 -0700 | [diff] [blame] | 60 | * threads per cores then map sibling threads to the same queue |
| 61 | * for performance optimizations. |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 62 | */ |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 63 | if (q < nr_queues) { |
| 64 | map[cpu] = queue_index(qmap, nr_queues, q++); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 65 | } else { |
| 66 | first_sibling = get_first_sibling(cpu); |
| 67 | if (first_sibling == cpu) |
Ming Lei | 556f36e | 2019-07-25 17:41:46 +0800 | [diff] [blame] | 68 | map[cpu] = queue_index(qmap, nr_queues, q++); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 69 | else |
| 70 | map[cpu] = map[first_sibling]; |
| 71 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
Christoph Hellwig | 9e5a7e2 | 2016-11-01 08:12:47 -0600 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(blk_mq_map_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 77 | |
Bart Van Assche | cd669f8 | 2019-05-30 17:00:53 -0700 | [diff] [blame] | 78 | /** |
| 79 | * blk_mq_hw_queue_to_node - Look up the memory node for a hardware queue index |
| 80 | * @qmap: CPU to hardware queue map. |
| 81 | * @index: hardware queue index. |
| 82 | * |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 83 | * We have no quick way of doing reverse lookups. This is only used at |
| 84 | * queue init time, so runtime isn't important. |
| 85 | */ |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 86 | int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int index) |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for_each_possible_cpu(i) { |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 91 | if (index == qmap->mq_map[i]) |
Raghavendra K T | bffed45 | 2015-12-02 16:59:05 +0530 | [diff] [blame] | 92 | return local_memory_node(cpu_to_node(i)); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return NUMA_NO_NODE; |
| 96 | } |