Skip to contentSkip to conversation

block: build default queue map via irq_create_affinity_masks

PATCH In review Ming Lei 4 participants 6 messages Jun 30, 2021 – Jul 8, 2021 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: build default queue map via irq_create_affinity_masks

The default queue mapping builder of blk_mq_map_queues doesn't take NUMA topo into account, so the built mapping is pretty bad, since CPUs belonging to different NUMA node are assigned to same queue. It is observed that IOPS drops by ~30% when running two jobs on same hctx of null_blk from two CPUs belonging to two NUMA nodes compared with from same NUMA node.

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

Lots of drivers may benefit from the change, such as nvme pci poll, nvme tcp, ...

---
 block/blk-mq-cpumap.c | 60 +++++++++----------------------------------
 1 file changed, 12 insertions(+), 48 deletions(-)
block/blk-mq-cpumap.c +12 -48
index 3db84d3197f1..946e373296a3 100644
@@ -10,67 +10,31 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/cpu.h>
+#include <linux/interrupt.h>
 
 #include <linux/blk-mq.h>
 #include "blk.h"
 #include "blk-mq.h"
 
-static int queue_index(struct blk_mq_queue_map *qmap,
-		       unsigned int nr_queues, const int q)
-{
-	return qmap->queue_offset + (q % nr_queues);
-}
-
-static int get_first_sibling(unsigned int cpu)
-{
-	unsigned int ret;
-
-	ret = cpumask_first(topology_sibling_cpumask(cpu));
-	if (ret < nr_cpu_ids)
-		return ret;
-
-	return cpu;
-}
-
 int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
 {
+	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity affd = {0};
 	unsigned int *map = qmap->mq_map;
 	unsigned int nr_queues = qmap->nr_queues;
-	unsigned int cpu, first_sibling, q = 0;
+	unsigned int q;
 
-	for_each_possible_cpu(cpu)
-		map[cpu] = -1;
+	masks = irq_create_affinity_masks(nr_queues, &affd);
+	if (!masks)
+		return -ENOMEM;
 
-	/*
-	 * 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++);
-	}
+	for (q = 0; q < nr_queues; q++) {
+		unsigned int cpu;
 
-	for_each_possible_cpu(cpu) {
-		if (map[cpu] != -1)
-			continue;
-		/*
-		 * First do sequential mapping between CPUs and queues.
-		 * In case we still have CPUs to map, and we have some number of
-		 * threads per cores then map sibling threads to the same queue
-		 * for performance optimizations.
-		 */
-		if (q < nr_queues) {
-			map[cpu] = queue_index(qmap, nr_queues, q++);
-		} else {
-			first_sibling = get_first_sibling(cpu);
-			if (first_sibling == cpu)
-				map[cpu] = queue_index(qmap, nr_queues, q++);
-			else
-				map[cpu] = map[first_sibling];
-		}
+		for_each_cpu(cpu, &masks[q].mask)
+			map[cpu] = q;
 	}
-
+	kfree(masks);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Signature · 1 line
2.31.1
author
#1 lore raw
Quoted 87 lines · Ming Lei

The default queue mapping builder of blk_mq_map_queues doesn't take NUMA topo into account, so the built mapping is pretty bad, since CPUs belonging to different NUMA node are assigned to same queue. It is observed that IOPS drops by ~30% when running two jobs on same hctx of null_blk from two CPUs belonging to two NUMA nodes compared with from same NUMA node.

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

Lots of drivers may benefit from the change, such as nvme pci poll, nvme tcp, ...

---
 block/blk-mq-cpumap.c | 60 +++++++++----------------------------------
 1 file changed, 12 insertions(+), 48 deletions(-)
block/blk-mq-cpumap.c +9 -39
index 3db84d3197f1..946e373296a3 100644
@@ -10,67 +10,31 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/cpu.h>
+#include <linux/interrupt.h>
 
 #include <linux/blk-mq.h>
 #include "blk.h"
 #include "blk-mq.h"
 
-static int queue_index(struct blk_mq_queue_map *qmap,
-		       unsigned int nr_queues, const int q)
-{
-	return qmap->queue_offset + (q % nr_queues);
-}
-
-static int get_first_sibling(unsigned int cpu)
-{
-	unsigned int ret;
-
-	ret = cpumask_first(topology_sibling_cpumask(cpu));
-	if (ret < nr_cpu_ids)
-		return ret;
-
-	return cpu;
-}
-
 int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
 {
+	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity affd = {0};
 	unsigned int *map = qmap->mq_map;
 	unsigned int nr_queues = qmap->nr_queues;
-	unsigned int cpu, first_sibling, q = 0;
+	unsigned int q;
 
-	for_each_possible_cpu(cpu)
-		map[cpu] = -1;
+	masks = irq_create_affinity_masks(nr_queues, &affd);
+	if (!masks)
+		return -ENOMEM;
 
-	/*
-	 * 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++);
-	}
+	for (q = 0; q < nr_queues; q++) {
+		unsigned int cpu;
 
-	for_each_possible_cpu(cpu) {
-		if (map[cpu] != -1)
-			continue;
-		/*
-		 * First do sequential mapping between CPUs and queues.
-		 * In case we still have CPUs to map, and we have some number of
-		 * threads per cores then map sibling threads to the same queue
-		 * for performance optimizations.
-		 */
-		if (q < nr_queues) {

block/blk-mq-cpumap.c line 35

Quoted 10 lines
block/blk-mq-cpumap.c +2 -8
-			map[cpu] = queue_index(qmap, nr_queues, q++);
-		} else {
-			first_sibling = get_first_sibling(cpu);
-			if (first_sibling == cpu)
-				map[cpu] = queue_index(qmap, nr_queues, q++);
-			else
-				map[cpu] = map[first_sibling];
-		}
+		for_each_cpu(cpu, &masks[q].mask)
+			map[cpu] = q;

oops, the above line should have been:

			map[cpu] = qmap->queue_offset + q;

Thanks,
Ming

#2 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei

The default queue mapping builder of blk_mq_map_queues doesn't take NUMA topo into account, so the built mapping is pretty bad, since CPUs belonging to different NUMA node are assigned to same queue. It is observed that IOPS drops by ~30% when running two jobs on same hctx of null_blk from two CPUs belonging to two NUMA nodes compared with from same NUMA node.

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

This looks sensible, but adding Thomas to see if he is fine with using an irq function like this. Maybe it needs to move out of the irq code and grow a new name if we use it like this.

Quoted 96 lines

Lots of drivers may benefit from the change, such as nvme pci poll, nvme tcp, ...

---
 block/blk-mq-cpumap.c | 60 +++++++++----------------------------------
 1 file changed, 12 insertions(+), 48 deletions(-)
block/blk-mq-cpumap.c +12 -48
index 3db84d3197f1..946e373296a3 100644
@@ -10,67 +10,31 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/cpu.h>
+#include <linux/interrupt.h>
 
 #include <linux/blk-mq.h>
 #include "blk.h"
 #include "blk-mq.h"
 
-static int queue_index(struct blk_mq_queue_map *qmap,
-		       unsigned int nr_queues, const int q)
-{
-	return qmap->queue_offset + (q % nr_queues);
-}
-
-static int get_first_sibling(unsigned int cpu)
-{
-	unsigned int ret;
-
-	ret = cpumask_first(topology_sibling_cpumask(cpu));
-	if (ret < nr_cpu_ids)
-		return ret;
-
-	return cpu;
-}
-
 int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
 {
+	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity affd = {0};
 	unsigned int *map = qmap->mq_map;
 	unsigned int nr_queues = qmap->nr_queues;
-	unsigned int cpu, first_sibling, q = 0;
+	unsigned int q;
 
-	for_each_possible_cpu(cpu)
-		map[cpu] = -1;
+	masks = irq_create_affinity_masks(nr_queues, &affd);
+	if (!masks)
+		return -ENOMEM;
 
-	/*
-	 * 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++);
-	}
+	for (q = 0; q < nr_queues; q++) {
+		unsigned int cpu;
 
-	for_each_possible_cpu(cpu) {
-		if (map[cpu] != -1)
-			continue;
-		/*
-		 * First do sequential mapping between CPUs and queues.
-		 * In case we still have CPUs to map, and we have some number of
-		 * threads per cores then map sibling threads to the same queue
-		 * for performance optimizations.
-		 */
-		if (q < nr_queues) {
-			map[cpu] = queue_index(qmap, nr_queues, q++);
-		} else {
-			first_sibling = get_first_sibling(cpu);
-			if (first_sibling == cpu)
-				map[cpu] = queue_index(qmap, nr_queues, q++);
-			else
-				map[cpu] = map[first_sibling];
-		}
+		for_each_cpu(cpu, &masks[q].mask)
+			map[cpu] = q;
 	}
-
+	kfree(masks);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Signature · 1 line
2.31.1

---end quoted text---

#3 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

This looks sensible, but adding Thomas to see if he is fine with using an irq function like this. Maybe it needs to move out of the irq code and grow a new name if we use it like this.

Yes, making it less irq centric and sticking it into lib makes sense.

The usage sites then can have their specific wrappers around it.

Thanks,

tglx

#4 lore raw
In reply to Ming Lei
Quoted 24 lines · Ming Lei

The default queue mapping builder of blk_mq_map_queues doesn't take NUMA topo into account, so the built mapping is pretty bad, since CPUs belonging to different NUMA node are assigned to same queue. It is observed that IOPS drops by ~30% when running two jobs on same hctx of null_blk from two CPUs belonging to two NUMA nodes compared with from same NUMA node.

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

Lots of drivers may benefit from the change, such as nvme pci poll, nvme tcp, ...

---
  block/blk-mq-cpumap.c | 60 +++++++++----------------------------------
  1 file changed, 12 insertions(+), 48 deletions(-)
block/blk-mq-cpumap.c +0 -0
index 3db84d3197f1..946e373296a3 100644

block/blk-mq-cpumap.c line 13

Quoted 4 lines
block/blk-mq-cpumap.c +1 -0
@@ -10,67 +10,31 @@
  #include <linux/mm.h>
  #include <linux/smp.h>
  #include <linux/cpu.h>
+#include <linux/interrupt.h>

Similar to what Christoph mentioned, seems strange to be including interrupt.h

Quoted 16 lines
block/blk-mq-cpumap.c +0 -11
  #include <linux/blk-mq.h>
  #include "blk.h"
  #include "blk-mq.h"
 
-static int queue_index(struct blk_mq_queue_map *qmap,
-		       unsigned int nr_queues, const int q)
-{
-	return qmap->queue_offset + (q % nr_queues);
-}
-
-static int get_first_sibling(unsigned int cpu)
-{
-	unsigned int ret;
-
-	ret = cpumask_first(topology_sibling_cpumask(cpu));

block/blk-mq-cpumap.c line 22

Quoted 10 lines
block/blk-mq-cpumap.c +2 -6
-	if (ret < nr_cpu_ids)
-		return ret;
-
-	return cpu;
-}
-
  int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
  {
+	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity affd = {0};

should this be simply {}? I forget...

block/blk-mq-cpumap.c line 29

Quoted 10 lines
block/blk-mq-cpumap.c +4 -3
  	unsigned int *map = qmap->mq_map;
  	unsigned int nr_queues = qmap->nr_queues;
-	unsigned int cpu, first_sibling, q = 0;
+	unsigned int q;
 
-	for_each_possible_cpu(cpu)
-		map[cpu] = -1;
+	masks = irq_create_affinity_masks(nr_queues, &affd);
+	if (!masks)
+		return -ENOMEM;

should we fall back on something else here? Seems that this function does not fail just because out of memory.

Thanks,
John

author
#5 lore raw
Quoted 20 lines · John Garry
Quoted 19 lines · Ming Lei

The default queue mapping builder of blk_mq_map_queues doesn't take NUMA topo into account, so the built mapping is pretty bad, since CPUs belonging to different NUMA node are assigned to same queue. It is observed that IOPS drops by ~30% when running two jobs on same hctx of null_blk from two CPUs belonging to two NUMA nodes compared with from same NUMA node.

Address the issue by reusing irq_create_affinity_masks() for building the default queue mapping, so that we can re-use the mapping created for managed irq.

Lots of drivers may benefit from the change, such as nvme pci poll, nvme tcp, ...

---
  block/blk-mq-cpumap.c | 60 +++++++++----------------------------------
  1 file changed, 12 insertions(+), 48 deletions(-)
Quoted 12 lines

block/blk-mq-cpumap.c line 13

Quoted 9 lines
block/blk-mq-cpumap.c +1 -0
index 3db84d3197f1..946e373296a3 100644
@@ -10,67 +10,31 @@
  #include <linux/mm.h>
  #include <linux/smp.h>
  #include <linux/cpu.h>
+#include <linux/interrupt.h>

Similar to what Christoph mentioned, seems strange to be including interrupt.h

The ideal way is to abstract & move the affinity building code into lib/, but it needs to refactor kernel/irq/affinity.c a bit.

Also here each queue means one blk-mq hw queue, it is still not too strange to associate it with interrupt and re-use the interrupt affinity building code.

Let's see how Thomas thinks about this usage.

Quoted 19 lines
Quoted 19 lines
block/blk-mq-cpumap.c +0 -15
  #include <linux/blk-mq.h>
  #include "blk.h"
  #include "blk-mq.h"
-static int queue_index(struct blk_mq_queue_map *qmap,
-		       unsigned int nr_queues, const int q)
-{
-	return qmap->queue_offset + (q % nr_queues);
-}
-
-static int get_first_sibling(unsigned int cpu)
-{
-	unsigned int ret;
-
-	ret = cpumask_first(topology_sibling_cpumask(cpu));
-	if (ret < nr_cpu_ids)
-		return ret;
-
-	return cpu;
Quoted 8 lines

block/blk-mq-cpumap.c line 22

Quoted 6 lines
block/blk-mq-cpumap.c +2 -2
-}
-
  int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
  {
+	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity affd = {0};

should this be simply {}? I forget...

I think both should be fine, and two usages can be found in kernel code.

Quoted 13 lines

block/blk-mq-cpumap.c line 29

Quoted 10 lines
block/blk-mq-cpumap.c +4 -3
  	unsigned int *map = qmap->mq_map;
  	unsigned int nr_queues = qmap->nr_queues;
-	unsigned int cpu, first_sibling, q = 0;
+	unsigned int q;
-	for_each_possible_cpu(cpu)
-		map[cpu] = -1;
+	masks = irq_create_affinity_masks(nr_queues, &affd);
+	if (!masks)
+		return -ENOMEM;

should we fall back on something else here? Seems that this function does not fail just because out of memory.

The default case is nr_set == 1, so the only failure is out of memory, and irq_create_affinity_masks() basically creates cpumask for each vector/queue and assigns possible CPUs among these vector/queue.

Thanks,
Ming