Back to home page

LXR

 
 

    


0001 /*
0002  * CPU <-> hardware queue mapping helpers
0003  *
0004  * Copyright (C) 2013-2014 Jens Axboe
0005  */
0006 #include <linux/kernel.h>
0007 #include <linux/threads.h>
0008 #include <linux/module.h>
0009 #include <linux/mm.h>
0010 #include <linux/smp.h>
0011 #include <linux/cpu.h>
0012 
0013 #include <linux/blk-mq.h>
0014 #include "blk.h"
0015 #include "blk-mq.h"
0016 
0017 static int cpu_to_queue_index(unsigned int nr_cpus, unsigned int nr_queues,
0018                   const int cpu)
0019 {
0020     return cpu * nr_queues / nr_cpus;
0021 }
0022 
0023 static int get_first_sibling(unsigned int cpu)
0024 {
0025     unsigned int ret;
0026 
0027     ret = cpumask_first(topology_sibling_cpumask(cpu));
0028     if (ret < nr_cpu_ids)
0029         return ret;
0030 
0031     return cpu;
0032 }
0033 
0034 int blk_mq_map_queues(struct blk_mq_tag_set *set)
0035 {
0036     unsigned int *map = set->mq_map;
0037     unsigned int nr_queues = set->nr_hw_queues;
0038     const struct cpumask *online_mask = cpu_online_mask;
0039     unsigned int i, nr_cpus, nr_uniq_cpus, queue, first_sibling;
0040     cpumask_var_t cpus;
0041 
0042     if (!alloc_cpumask_var(&cpus, GFP_ATOMIC))
0043         return -ENOMEM;
0044 
0045     cpumask_clear(cpus);
0046     nr_cpus = nr_uniq_cpus = 0;
0047     for_each_cpu(i, online_mask) {
0048         nr_cpus++;
0049         first_sibling = get_first_sibling(i);
0050         if (!cpumask_test_cpu(first_sibling, cpus))
0051             nr_uniq_cpus++;
0052         cpumask_set_cpu(i, cpus);
0053     }
0054 
0055     queue = 0;
0056     for_each_possible_cpu(i) {
0057         if (!cpumask_test_cpu(i, online_mask)) {
0058             map[i] = 0;
0059             continue;
0060         }
0061 
0062         /*
0063          * Easy case - we have equal or more hardware queues. Or
0064          * there are no thread siblings to take into account. Do
0065          * 1:1 if enough, or sequential mapping if less.
0066          */
0067         if (nr_queues >= nr_cpus || nr_cpus == nr_uniq_cpus) {
0068             map[i] = cpu_to_queue_index(nr_cpus, nr_queues, queue);
0069             queue++;
0070             continue;
0071         }
0072 
0073         /*
0074          * Less then nr_cpus queues, and we have some number of
0075          * threads per cores. Map sibling threads to the same
0076          * queue.
0077          */
0078         first_sibling = get_first_sibling(i);
0079         if (first_sibling == i) {
0080             map[i] = cpu_to_queue_index(nr_uniq_cpus, nr_queues,
0081                             queue);
0082             queue++;
0083         } else
0084             map[i] = map[first_sibling];
0085     }
0086 
0087     free_cpumask_var(cpus);
0088     return 0;
0089 }
0090 EXPORT_SYMBOL_GPL(blk_mq_map_queues);
0091 
0092 /*
0093  * We have no quick way of doing reverse lookups. This is only used at
0094  * queue init time, so runtime isn't important.
0095  */
0096 int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
0097 {
0098     int i;
0099 
0100     for_each_possible_cpu(i) {
0101         if (index == mq_map[i])
0102             return local_memory_node(cpu_to_node(i));
0103     }
0104 
0105     return NUMA_NO_NODE;
0106 }