blob: 03a53482027150819aaf69769a5c07d0cd5b8a64 [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
Jens Axboe843477d2018-10-24 13:16:11 -060017static int cpu_to_queue_index(struct blk_mq_queue_map *qmap,
18 unsigned int nr_queues, const int cpu)
Jens Axboe320ae512013-10-24 09:20:05 +010019{
Jens Axboe843477d2018-10-24 13:16:11 -060020 return qmap->queue_offset + (cpu % nr_queues);
Jens Axboe320ae512013-10-24 09:20:05 +010021}
22
23static int get_first_sibling(unsigned int cpu)
24{
25 unsigned int ret;
26
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020027 ret = cpumask_first(topology_sibling_cpumask(cpu));
Jens Axboe320ae512013-10-24 09:20:05 +010028 if (ret < nr_cpu_ids)
29 return ret;
30
31 return cpu;
32}
33
Jens Axboeed76e322018-10-29 13:06:14 -060034int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
Jens Axboe320ae512013-10-24 09:20:05 +010035{
Jens Axboeed76e322018-10-29 13:06:14 -060036 unsigned int *map = qmap->mq_map;
37 unsigned int nr_queues = qmap->nr_queues;
Max Gurtovoyfe631452017-06-29 08:40:11 -060038 unsigned int cpu, first_sibling;
Jens Axboe320ae512013-10-24 09:20:05 +010039
Max Gurtovoyfe631452017-06-29 08:40:11 -060040 for_each_possible_cpu(cpu) {
41 /*
42 * First do sequential mapping between CPUs and queues.
43 * In case we still have CPUs to map, and we have some number of
44 * threads per cores then map sibling threads to the same queue for
45 * performace optimizations.
46 */
47 if (cpu < nr_queues) {
Jens Axboe843477d2018-10-24 13:16:11 -060048 map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060049 } else {
50 first_sibling = get_first_sibling(cpu);
51 if (first_sibling == cpu)
Jens Axboe843477d2018-10-24 13:16:11 -060052 map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060053 else
54 map[cpu] = map[first_sibling];
55 }
Jens Axboe320ae512013-10-24 09:20:05 +010056 }
57
Jens Axboe320ae512013-10-24 09:20:05 +010058 return 0;
59}
Christoph Hellwig9e5a7e22016-11-01 08:12:47 -060060EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Jens Axboe320ae512013-10-24 09:20:05 +010061
Jens Axboef14bbe72014-05-27 12:06:53 -060062/*
63 * We have no quick way of doing reverse lookups. This is only used at
64 * queue init time, so runtime isn't important.
65 */
Jens Axboeed76e322018-10-29 13:06:14 -060066int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int index)
Jens Axboef14bbe72014-05-27 12:06:53 -060067{
68 int i;
69
70 for_each_possible_cpu(i) {
Jens Axboeed76e322018-10-29 13:06:14 -060071 if (index == qmap->mq_map[i])
Raghavendra K Tbffed452015-12-02 16:59:05 +053072 return local_memory_node(cpu_to_node(i));
Jens Axboef14bbe72014-05-27 12:06:53 -060073 }
74
75 return NUMA_NO_NODE;
76}