2 * CPU <-> hardware queue mapping helpers
4 * Copyright (C) 2013-2014 Jens Axboe
6 #include <linux/kernel.h>
7 #include <linux/threads.h>
8 #include <linux/module.h>
10 #include <linux/smp.h>
11 #include <linux/cpu.h>
13 #include <linux/blk-mq.h>
17 static int cpu_to_queue_index(unsigned int nr_queues, const int cpu,
18 const struct cpumask *online_mask)
21 * Non online CPU will be mapped to queue index 0.
23 if (!cpumask_test_cpu(cpu, online_mask))
25 return cpu % nr_queues;
28 static int get_first_sibling(unsigned int cpu)
32 ret = cpumask_first(topology_sibling_cpumask(cpu));
39 int blk_mq_map_queues(struct blk_mq_tag_set *set)
41 unsigned int *map = set->mq_map;
42 unsigned int nr_queues = set->nr_hw_queues;
43 const struct cpumask *online_mask = cpu_online_mask;
44 unsigned int cpu, first_sibling;
46 for_each_possible_cpu(cpu) {
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.
53 if (cpu < nr_queues) {
54 map[cpu] = cpu_to_queue_index(nr_queues, cpu, online_mask);
56 first_sibling = get_first_sibling(cpu);
57 if (first_sibling == cpu)
58 map[cpu] = cpu_to_queue_index(nr_queues, cpu, online_mask);
60 map[cpu] = map[first_sibling];
66 EXPORT_SYMBOL_GPL(blk_mq_map_queues);
69 * We have no quick way of doing reverse lookups. This is only used at
70 * queue init time, so runtime isn't important.
72 int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
76 for_each_possible_cpu(i) {
77 if (index == mq_map[i])
78 return local_memory_node(cpu_to_node(i));