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 | |
Jens Axboe | 843477d | 2018-10-24 13:16:11 -0600 | [diff] [blame] | 18 | static int cpu_to_queue_index(struct blk_mq_queue_map *qmap, |
| 19 | unsigned int nr_queues, const int cpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 20 | { |
Jens Axboe | 843477d | 2018-10-24 13:16:11 -0600 | [diff] [blame] | 21 | return qmap->queue_offset + (cpu % 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; |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 39 | unsigned int cpu, first_sibling; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 40 | |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 41 | for_each_possible_cpu(cpu) { |
| 42 | /* |
| 43 | * First do sequential mapping between CPUs and queues. |
| 44 | * 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^] | 45 | * threads per cores then map sibling threads to the same queue |
| 46 | * for performance optimizations. |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 47 | */ |
| 48 | if (cpu < nr_queues) { |
Jens Axboe | 843477d | 2018-10-24 13:16:11 -0600 | [diff] [blame] | 49 | map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 50 | } else { |
| 51 | first_sibling = get_first_sibling(cpu); |
| 52 | if (first_sibling == cpu) |
Jens Axboe | 843477d | 2018-10-24 13:16:11 -0600 | [diff] [blame] | 53 | map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu); |
Max Gurtovoy | fe63145 | 2017-06-29 08:40:11 -0600 | [diff] [blame] | 54 | else |
| 55 | map[cpu] = map[first_sibling]; |
| 56 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 59 | return 0; |
| 60 | } |
Christoph Hellwig | 9e5a7e2 | 2016-11-01 08:12:47 -0600 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(blk_mq_map_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 62 | |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 63 | /* |
| 64 | * We have no quick way of doing reverse lookups. This is only used at |
| 65 | * queue init time, so runtime isn't important. |
| 66 | */ |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | int i; |
| 70 | |
| 71 | for_each_possible_cpu(i) { |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 72 | if (index == qmap->mq_map[i]) |
Raghavendra K T | bffed45 | 2015-12-02 16:59:05 +0530 | [diff] [blame] | 73 | return local_memory_node(cpu_to_node(i)); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return NUMA_NO_NODE; |
| 77 | } |