Blame view

block/blk-mq-cpumap.c 2.17 KB
3dcf60bcb   Christoph Hellwig   block: add SPDX t...
1
  // SPDX-License-Identifier: GPL-2.0
75bb4625b   Jens Axboe   blk-mq: add file ...
2
3
4
5
6
  /*
   * CPU <-> hardware queue mapping helpers
   *
   * Copyright (C) 2013-2014 Jens Axboe
   */
320ae51fe   Jens Axboe   blk-mq: new multi...
7
8
9
10
11
12
13
14
15
16
  #include <linux/kernel.h>
  #include <linux/threads.h>
  #include <linux/module.h>
  #include <linux/mm.h>
  #include <linux/smp.h>
  #include <linux/cpu.h>
  
  #include <linux/blk-mq.h>
  #include "blk.h"
  #include "blk-mq.h"
556f36e90   Ming Lei   blk-mq: balance m...
17
18
  static int queue_index(struct blk_mq_queue_map *qmap,
  		       unsigned int nr_queues, const int q)
320ae51fe   Jens Axboe   blk-mq: new multi...
19
  {
556f36e90   Ming Lei   blk-mq: balance m...
20
  	return qmap->queue_offset + (q % nr_queues);
320ae51fe   Jens Axboe   blk-mq: new multi...
21
22
23
24
25
  }
  
  static int get_first_sibling(unsigned int cpu)
  {
  	unsigned int ret;
06931e622   Bartosz Golaszewski   sched/topology: R...
26
  	ret = cpumask_first(topology_sibling_cpumask(cpu));
320ae51fe   Jens Axboe   blk-mq: new multi...
27
28
29
30
31
  	if (ret < nr_cpu_ids)
  		return ret;
  
  	return cpu;
  }
ed76e329d   Jens Axboe   blk-mq: abstract ...
32
  int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
320ae51fe   Jens Axboe   blk-mq: new multi...
33
  {
ed76e329d   Jens Axboe   blk-mq: abstract ...
34
35
  	unsigned int *map = qmap->mq_map;
  	unsigned int nr_queues = qmap->nr_queues;
556f36e90   Ming Lei   blk-mq: balance m...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  	unsigned int cpu, first_sibling, q = 0;
  
  	for_each_possible_cpu(cpu)
  		map[cpu] = -1;
  
  	/*
  	 * Spread queues among present CPUs first for minimizing
  	 * count of dead queues which are mapped by all un-present CPUs
  	 */
  	for_each_present_cpu(cpu) {
  		if (q >= nr_queues)
  			break;
  		map[cpu] = queue_index(qmap, nr_queues, q++);
  	}
320ae51fe   Jens Axboe   blk-mq: new multi...
50

fe631457f   Max Gurtovoy   blk-mq: map all H...
51
  	for_each_possible_cpu(cpu) {
556f36e90   Ming Lei   blk-mq: balance m...
52
53
  		if (map[cpu] != -1)
  			continue;
320ae51fe   Jens Axboe   blk-mq: new multi...
54
  		/*
fe631457f   Max Gurtovoy   blk-mq: map all H...
55
56
  		 * First do sequential mapping between CPUs and queues.
  		 * In case we still have CPUs to map, and we have some number of
ef025d7ec   Bart Van Assche   blk-mq: Fix spell...
57
58
  		 * threads per cores then map sibling threads to the same queue
  		 * for performance optimizations.
320ae51fe   Jens Axboe   blk-mq: new multi...
59
  		 */
556f36e90   Ming Lei   blk-mq: balance m...
60
61
  		if (q < nr_queues) {
  			map[cpu] = queue_index(qmap, nr_queues, q++);
fe631457f   Max Gurtovoy   blk-mq: map all H...
62
63
64
  		} else {
  			first_sibling = get_first_sibling(cpu);
  			if (first_sibling == cpu)
556f36e90   Ming Lei   blk-mq: balance m...
65
  				map[cpu] = queue_index(qmap, nr_queues, q++);
fe631457f   Max Gurtovoy   blk-mq: map all H...
66
67
  			else
  				map[cpu] = map[first_sibling];
320ae51fe   Jens Axboe   blk-mq: new multi...
68
  		}
320ae51fe   Jens Axboe   blk-mq: new multi...
69
  	}
320ae51fe   Jens Axboe   blk-mq: new multi...
70
71
  	return 0;
  }
9e5a7e229   Christoph Hellwig   blk-mq: export bl...
72
  EXPORT_SYMBOL_GPL(blk_mq_map_queues);
320ae51fe   Jens Axboe   blk-mq: new multi...
73

cd669f88b   Bart Van Assche   blk-mq: Document ...
74
75
76
77
78
  /**
   * blk_mq_hw_queue_to_node - Look up the memory node for a hardware queue index
   * @qmap: CPU to hardware queue map.
   * @index: hardware queue index.
   *
f14bbe77a   Jens Axboe   blk-mq: pass in s...
79
80
81
   * We have no quick way of doing reverse lookups. This is only used at
   * queue init time, so runtime isn't important.
   */
ed76e329d   Jens Axboe   blk-mq: abstract ...
82
  int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int index)
f14bbe77a   Jens Axboe   blk-mq: pass in s...
83
84
85
86
  {
  	int i;
  
  	for_each_possible_cpu(i) {
ed76e329d   Jens Axboe   blk-mq: abstract ...
87
  		if (index == qmap->mq_map[i])
576e85c5e   Xianting Tian   blk-mq: remove th...
88
  			return cpu_to_node(i);
f14bbe77a   Jens Axboe   blk-mq: pass in s...
89
90
91
92
  	}
  
  	return NUMA_NO_NODE;
  }