blob: 3eb169f15842c04723b51cbe9e63d47b9f5d156e [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * CPU <-> hardware queue mapping helpers
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 */
Jens Axboe320ae512013-10-24 09:20:05 +01006#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 Torvalds03ffbcd2017-07-03 16:50:31 -070017static int cpu_to_queue_index(unsigned int nr_queues, const int cpu)
Jens Axboe320ae512013-10-24 09:20:05 +010018{
Max Gurtovoyfe631452017-06-29 08:40:11 -060019 return cpu % nr_queues;
Jens Axboe320ae512013-10-24 09:20:05 +010020}
21
22static int get_first_sibling(unsigned int cpu)
23{
24 unsigned int ret;
25
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020026 ret = cpumask_first(topology_sibling_cpumask(cpu));
Jens Axboe320ae512013-10-24 09:20:05 +010027 if (ret < nr_cpu_ids)
28 return ret;
29
30 return cpu;
31}
32
Christoph Hellwigda695ba2016-09-14 16:18:55 +020033int blk_mq_map_queues(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +010034{
Christoph Hellwigda695ba2016-09-14 16:18:55 +020035 unsigned int *map = set->mq_map;
36 unsigned int nr_queues = set->nr_hw_queues;
Max Gurtovoyfe631452017-06-29 08:40:11 -060037 unsigned int cpu, first_sibling;
Jens Axboe320ae512013-10-24 09:20:05 +010038
Max Gurtovoyfe631452017-06-29 08:40:11 -060039 for_each_possible_cpu(cpu) {
40 /*
41 * First do sequential mapping between CPUs and queues.
42 * In case we still have CPUs to map, and we have some number of
43 * threads per cores then map sibling threads to the same queue for
44 * performace optimizations.
45 */
46 if (cpu < nr_queues) {
Linus Torvalds03ffbcd2017-07-03 16:50:31 -070047 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060048 } else {
49 first_sibling = get_first_sibling(cpu);
50 if (first_sibling == cpu)
Linus Torvalds03ffbcd2017-07-03 16:50:31 -070051 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060052 else
53 map[cpu] = map[first_sibling];
54 }
Jens Axboe320ae512013-10-24 09:20:05 +010055 }
56
Jens Axboe320ae512013-10-24 09:20:05 +010057 return 0;
58}
Christoph Hellwig9e5a7e22016-11-01 08:12:47 -060059EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Jens Axboe320ae512013-10-24 09:20:05 +010060
Jens Axboef14bbe72014-05-27 12:06:53 -060061/*
62 * We have no quick way of doing reverse lookups. This is only used at
63 * queue init time, so runtime isn't important.
64 */
65int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
66{
67 int i;
68
69 for_each_possible_cpu(i) {
70 if (index == mq_map[i])
Raghavendra K Tbffed452015-12-02 16:59:05 +053071 return local_memory_node(cpu_to_node(i));
Jens Axboef14bbe72014-05-27 12:06:53 -060072 }
73
74 return NUMA_NO_NODE;
75}