Linux 4.14.5
[linux/fpc-iii.git] / block / blk-mq-cpumap.c
blob9f8cffc8a701ec24c87729b9ef4a1048cc7d205f
1 /*
2 * CPU <-> hardware queue mapping helpers
4 * Copyright (C) 2013-2014 Jens Axboe
5 */
6 #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>
13 #include <linux/blk-mq.h>
14 #include "blk.h"
15 #include "blk-mq.h"
17 static int cpu_to_queue_index(unsigned int nr_queues, const int cpu)
20 * Non present CPU will be mapped to queue index 0.
22 if (!cpu_present(cpu))
23 return 0;
24 return cpu % nr_queues;
27 static int get_first_sibling(unsigned int cpu)
29 unsigned int ret;
31 ret = cpumask_first(topology_sibling_cpumask(cpu));
32 if (ret < nr_cpu_ids)
33 return ret;
35 return cpu;
38 int blk_mq_map_queues(struct blk_mq_tag_set *set)
40 unsigned int *map = set->mq_map;
41 unsigned int nr_queues = set->nr_hw_queues;
42 unsigned int cpu, first_sibling;
44 for_each_possible_cpu(cpu) {
46 * First do sequential mapping between CPUs and queues.
47 * In case we still have CPUs to map, and we have some number of
48 * threads per cores then map sibling threads to the same queue for
49 * performace optimizations.
51 if (cpu < nr_queues) {
52 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
53 } else {
54 first_sibling = get_first_sibling(cpu);
55 if (first_sibling == cpu)
56 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
57 else
58 map[cpu] = map[first_sibling];
62 return 0;
64 EXPORT_SYMBOL_GPL(blk_mq_map_queues);
67 * We have no quick way of doing reverse lookups. This is only used at
68 * queue init time, so runtime isn't important.
70 int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
72 int i;
74 for_each_possible_cpu(i) {
75 if (index == mq_map[i])
76 return local_memory_node(cpu_to_node(i));
79 return NUMA_NO_NODE;