blob: 2cca4fc43f45a962120b7c775e32dacfb26566c3 [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
Max Gurtovoyfe631452017-06-29 08:40:11 -060017static int cpu_to_queue_index(unsigned int nr_queues, const int cpu,
18 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +010019{
Max Gurtovoyfe631452017-06-29 08:40:11 -060020 /*
21 * Non online CPU will be mapped to queue index 0.
22 */
23 if (!cpumask_test_cpu(cpu, online_mask))
24 return 0;
25 return cpu % nr_queues;
Jens Axboe320ae512013-10-24 09:20:05 +010026}
27
28static int get_first_sibling(unsigned int cpu)
29{
30 unsigned int ret;
31
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020032 ret = cpumask_first(topology_sibling_cpumask(cpu));
Jens Axboe320ae512013-10-24 09:20:05 +010033 if (ret < nr_cpu_ids)
34 return ret;
35
36 return cpu;
37}
38
Christoph Hellwigda695ba2016-09-14 16:18:55 +020039int blk_mq_map_queues(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +010040{
Christoph Hellwigda695ba2016-09-14 16:18:55 +020041 unsigned int *map = set->mq_map;
42 unsigned int nr_queues = set->nr_hw_queues;
43 const struct cpumask *online_mask = cpu_online_mask;
Max Gurtovoyfe631452017-06-29 08:40:11 -060044 unsigned int cpu, first_sibling;
Jens Axboe320ae512013-10-24 09:20:05 +010045
Max Gurtovoyfe631452017-06-29 08:40:11 -060046 for_each_possible_cpu(cpu) {
47 /*
48 * First do sequential mapping between CPUs and queues.
49 * In case we still have CPUs to map, and we have some number of
50 * threads per cores then map sibling threads to the same queue for
51 * performace optimizations.
52 */
53 if (cpu < nr_queues) {
54 map[cpu] = cpu_to_queue_index(nr_queues, cpu, online_mask);
55 } else {
56 first_sibling = get_first_sibling(cpu);
57 if (first_sibling == cpu)
58 map[cpu] = cpu_to_queue_index(nr_queues, cpu, online_mask);
59 else
60 map[cpu] = map[first_sibling];
61 }
Jens Axboe320ae512013-10-24 09:20:05 +010062 }
63
Jens Axboe320ae512013-10-24 09:20:05 +010064 return 0;
65}
Christoph Hellwig9e5a7e22016-11-01 08:12:47 -060066EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Jens Axboe320ae512013-10-24 09:20:05 +010067
Jens Axboef14bbe72014-05-27 12:06:53 -060068/*
69 * We have no quick way of doing reverse lookups. This is only used at
70 * queue init time, so runtime isn't important.
71 */
72int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
73{
74 int i;
75
76 for_each_possible_cpu(i) {
77 if (index == mq_map[i])
Raghavendra K Tbffed452015-12-02 16:59:05 +053078 return local_memory_node(cpu_to_node(i));
Jens Axboef14bbe72014-05-27 12:06:53 -060079 }
80
81 return NUMA_NO_NODE;
82}