Skip to contentSkip to conversation

blk-mq: complete req in softirq context in case of single queue

PATCH Superseded Ming Lei 3 participants 5 messages Sep 26, 2018 – Sep 28, 2018 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blk-mq: complete req in softirq context in case of single queue

Lot of controllers may have only one irq vector for completing IO request. And usually affinity of the only irq vector is all possible CPUs, however, on most of ARCH, there may be only one specific CPU for handling this interrupt.

So if all IOs are completed in hardirq context, it is inevitable to degrade IO performance because of increased irq latency.

This patch tries to address this issue by allowing to complete request in softirq context, like the legacy IO path.

IOPS is observed as ~13%+ in the following randread test on raid0 over virtio-scsi.

mdadm --create --verbose /dev/md0 --level=0 --chunk=1024 --raid-devices=8 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi

fio --time_based --name=benchmark --runtime=30 --filename=/dev/md0 --nrfiles=1 --ioengine=libaio --iodepth=32 --direct=1 --invalidate=1 --verify=0 --verify_fatal=0 --numjobs=32 --rw=randread --blocksize=4k

Cc Zach Marano <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
---
 block/blk-mq.c      | 14 ++++++++++++++
 block/blk-softirq.c |  7 +++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
block/blk-mq.c +14 -0
index 85a1c1a59c72..d4792c3ac983 100644
@@ -565,6 +565,20 @@ static void __blk_mq_complete_request(struct request *rq)
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
+	/*
+	 * Most of single queue controllers, there is only one irq vector
+	 * for handling IO completion, and the only irq's affinity is set
+	 * as all possible CPUs. On most of ARCHs, this affinity means the
+	 * irq is handled on one specific CPU.
+	 *
+	 * So complete IO reqeust in softirq context in case of single queue
+	 * for not degrading IO performance by irqsoff latency.
+	 */
+	if (rq->q->nr_hw_queues == 1) {
+		__blk_complete_request(rq);
+		return;
+	}
+
 	if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
 		rq->q->softirq_done_fn(rq);
 		return;
block/blk-softirq.c +5 -2
index 15c1f5e12eb8..b1df9b6c1731 100644
@@ -101,17 +101,20 @@ void __blk_complete_request(struct request *req)
 	struct request_queue *q = req->q;
 	unsigned long flags;
 	bool shared = false;
+	int rq_cpu;
 
 	BUG_ON(!q->softirq_done_fn);
 
+	rq_cpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
+
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 
 	/*
 	 * Select completion CPU
 	 */
-	if (req->cpu != -1) {
-		ccpu = req->cpu;
+	if (rq_cpu != -1) {
+		ccpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
 		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
 			shared = cpus_share_cache(cpu, ccpu);
 	} else
Signature · 1 line
2.9.5
#1 lore raw

Hi Ming,

Quoted 4 lines · Ming Lei

Lot of controllers may have only one irq vector for completing IO request. And usually affinity of the only irq vector is all possible CPUs, however, on most of ARCH, there may be only one specific CPU for handling this interrupt.

Does this indicate the hardware would always notify the same cpu even if several cpu are set in the affinity? Is this the case in virtio or all hardwares?

xen pv driver is in this case. No matter how many cpu are set in affinity, the xen hypervisor only notifies the 1st cpu in the affinity (via xen event channel).

I have an extra basic question perhaps not related to this patch:

Why not delay other cases in softirq as well? (perhaps this is a question about mq but not for patch).

Thank you very much!

Dongli Zhang

Quoted 77 lines

So if all IOs are completed in hardirq context, it is inevitable to degrade IO performance because of increased irq latency.

This patch tries to address this issue by allowing to complete request in softirq context, like the legacy IO path.

IOPS is observed as ~13%+ in the following randread test on raid0 over virtio-scsi.

mdadm --create --verbose /dev/md0 --level=0 --chunk=1024 --raid-devices=8 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi

fio --time_based --name=benchmark --runtime=30 --filename=/dev/md0 --nrfiles=1 --ioengine=libaio --iodepth=32 --direct=1 --invalidate=1 --verify=0 --verify_fatal=0 --numjobs=32 --rw=randread --blocksize=4k

Cc Zach Marano <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
---
 block/blk-mq.c      | 14 ++++++++++++++
 block/blk-softirq.c |  7 +++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
block/blk-mq.c +14 -0
index 85a1c1a59c72..d4792c3ac983 100644
@@ -565,6 +565,20 @@ static void __blk_mq_complete_request(struct request *rq)
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
+	/*
+	 * Most of single queue controllers, there is only one irq vector
+	 * for handling IO completion, and the only irq's affinity is set
+	 * as all possible CPUs. On most of ARCHs, this affinity means the
+	 * irq is handled on one specific CPU.
+	 *
+	 * So complete IO reqeust in softirq context in case of single queue
+	 * for not degrading IO performance by irqsoff latency.
+	 */
+	if (rq->q->nr_hw_queues == 1) {
+		__blk_complete_request(rq);
+		return;
+	}
+
 	if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
 		rq->q->softirq_done_fn(rq);
 		return;
block/blk-softirq.c +5 -2
index 15c1f5e12eb8..b1df9b6c1731 100644
@@ -101,17 +101,20 @@ void __blk_complete_request(struct request *req)
 	struct request_queue *q = req->q;
 	unsigned long flags;
 	bool shared = false;
+	int rq_cpu;
 
 	BUG_ON(!q->softirq_done_fn);
 
+	rq_cpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
+
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 
 	/*
 	 * Select completion CPU
 	 */
-	if (req->cpu != -1) {
-		ccpu = req->cpu;
+	if (rq_cpu != -1) {
+		ccpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
 		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
 			shared = cpus_share_cache(cpu, ccpu);
 	} else
author
#2 lore raw

Hi Dongli,

Quoted 10 lines · Dongli Zhang

Hi Ming,

Quoted 4 lines · Ming Lei

Lot of controllers may have only one irq vector for completing IO request. And usually affinity of the only irq vector is all possible CPUs, however, on most of ARCH, there may be only one specific CPU for handling this interrupt.

Does this indicate the hardware would always notify the same cpu even if several cpu are set in the affinity? Is this the case in virtio or all hardwares?

It should be in all hardware, please see the following explanation from Thomas Gleixner:

https://lkml.org/lkml/2018/4/4/734

Quoted 3 lines

xen pv driver is in this case. No matter how many cpu are set in affinity, the xen hypervisor only notifies the 1st cpu in the affinity (via xen event channel).

That is understood, since emulated devices actually follow the hardware interrupt model in reality.

Quoted 6 lines

I have an extra basic question perhaps not related to this patch:

Why not delay other cases in softirq as well? (perhaps this is a question about mq but not for patch).

In case of multiple hw queues, such as NVMe, one hw queue is mapped to one single CPU usually, so it is fine to just complete the IO in hw irq context.

Thanks,
Ming

Quoted 80 lines
Quoted 80 lines

So if all IOs are completed in hardirq context, it is inevitable to degrade IO performance because of increased irq latency.

This patch tries to address this issue by allowing to complete request in softirq context, like the legacy IO path.

IOPS is observed as ~13%+ in the following randread test on raid0 over virtio-scsi.

mdadm --create --verbose /dev/md0 --level=0 --chunk=1024 --raid-devices=8 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi

fio --time_based --name=benchmark --runtime=30 --filename=/dev/md0 --nrfiles=1 --ioengine=libaio --iodepth=32 --direct=1 --invalidate=1 --verify=0 --verify_fatal=0 --numjobs=32 --rw=randread --blocksize=4k

Cc Zach Marano <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
---
 block/blk-mq.c      | 14 ++++++++++++++
 block/blk-softirq.c |  7 +++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
block/blk-mq.c +14 -0
index 85a1c1a59c72..d4792c3ac983 100644
@@ -565,6 +565,20 @@ static void __blk_mq_complete_request(struct request *rq)
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
+	/*
+	 * Most of single queue controllers, there is only one irq vector
+	 * for handling IO completion, and the only irq's affinity is set
+	 * as all possible CPUs. On most of ARCHs, this affinity means the
+	 * irq is handled on one specific CPU.
+	 *
+	 * So complete IO reqeust in softirq context in case of single queue
+	 * for not degrading IO performance by irqsoff latency.
+	 */
+	if (rq->q->nr_hw_queues == 1) {
+		__blk_complete_request(rq);
+		return;
+	}
+
 	if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
 		rq->q->softirq_done_fn(rq);
 		return;
block/blk-softirq.c +5 -2
index 15c1f5e12eb8..b1df9b6c1731 100644
@@ -101,17 +101,20 @@ void __blk_complete_request(struct request *req)
 	struct request_queue *q = req->q;
 	unsigned long flags;
 	bool shared = false;
+	int rq_cpu;
 
 	BUG_ON(!q->softirq_done_fn);
 
+	rq_cpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
+
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 
 	/*
 	 * Select completion CPU
 	 */
-	if (req->cpu != -1) {
-		ccpu = req->cpu;
+	if (rq_cpu != -1) {
+		ccpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
 		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
 			shared = cpus_share_cache(cpu, ccpu);
 	} else
Signature · 1 line
Ming
#3 lore raw
In reply to Ming Lei

Hi Ming

Quoted 71 lines · Ming Lei

Lot of controllers may have only one irq vector for completing IO request. And usually affinity of the only irq vector is all possible CPUs, however, on most of ARCH, there may be only one specific CPU for handling this interrupt.

So if all IOs are completed in hardirq context, it is inevitable to degrade IO performance because of increased irq latency.

This patch tries to address this issue by allowing to complete request in softirq context, like the legacy IO path.

IOPS is observed as ~13%+ in the following randread test on raid0 over virtio-scsi.

mdadm --create --verbose /dev/md0 --level=0 --chunk=1024 --raid-devices=8 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi

fio --time_based --name=benchmark --runtime=30 --filename=/dev/md0 --nrfiles=1 --ioengine=libaio --iodepth=32 --direct=1 --invalidate=1 --verify=0 --verify_fatal=0 --numjobs=32 --rw=randread --blocksize=4k

Cc Zach Marano <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
---
 block/blk-mq.c      | 14 ++++++++++++++
 block/blk-softirq.c |  7 +++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
block/blk-mq.c +14 -0
index 85a1c1a59c72..d4792c3ac983 100644
@@ -565,6 +565,20 @@ static void __blk_mq_complete_request(struct request *rq)
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
+	/*
+	 * Most of single queue controllers, there is only one irq vector
+	 * for handling IO completion, and the only irq's affinity is set
+	 * as all possible CPUs. On most of ARCHs, this affinity means the
+	 * irq is handled on one specific CPU.
+	 *
+	 * So complete IO reqeust in softirq context in case of single queue
+	 * for not degrading IO performance by irqsoff latency.
+	 */
+	if (rq->q->nr_hw_queues == 1) {
+		__blk_complete_request(rq);
+		return;
+	}
+
 	if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
 		rq->q->softirq_done_fn(rq);
 		return;
block/blk-softirq.c +3 -0
index 15c1f5e12eb8..b1df9b6c1731 100644
@@ -101,17 +101,20 @@ void __blk_complete_request(struct request *req)
 	struct request_queue *q = req->q;
 	unsigned long flags;
 	bool shared = false;
+	int rq_cpu;
 
 	BUG_ON(!q->softirq_done_fn);
 
+	rq_cpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
+
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 

block/blk-softirq.c void __blk_complete_request(struct request *req) · line 120

Quoted 10 lines
block/blk-softirq.c +2 -2
 	/*
 	 * Select completion CPU
 	 */
-	if (req->cpu != -1) {
-		ccpu = req->cpu;
+	if (rq_cpu != -1) {
+		ccpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
 		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
 			shared = cpus_share_cache(cpu, ccpu);
 	} else

;

Only QUEUE_FLAG_SAME_COMP is set, we will try to complete the request on the cpu where it is allocated.

For the blk-legacy, the req->cpu is -1 when QUEUE_FLAG_SAME_COMP is not set.

blk_queue_bio

	if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
		req->cpu = raw_smp_processor_id()

But for the blk-mq, the req->mq_ctx->cpu must be valid. So we should check the QUEUE_FLAG_SAME_COMP for the blk-mq case.

On the other hand, how about split the softirq completion part out of __blk_complete_request ? It is confused when find __blk_complete_request in __blk_mq_complete_request.

Thanks
Jianchao

author
#4 lore raw
Quoted 91 lines · jianchao.wang

Hi Ming

Quoted 71 lines · Ming Lei

Lot of controllers may have only one irq vector for completing IO request. And usually affinity of the only irq vector is all possible CPUs, however, on most of ARCH, there may be only one specific CPU for handling this interrupt.

So if all IOs are completed in hardirq context, it is inevitable to degrade IO performance because of increased irq latency.

This patch tries to address this issue by allowing to complete request in softirq context, like the legacy IO path.

IOPS is observed as ~13%+ in the following randread test on raid0 over virtio-scsi.

mdadm --create --verbose /dev/md0 --level=0 --chunk=1024 --raid-devices=8 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi

fio --time_based --name=benchmark --runtime=30 --filename=/dev/md0 --nrfiles=1 --ioengine=libaio --iodepth=32 --direct=1 --invalidate=1 --verify=0 --verify_fatal=0 --numjobs=32 --rw=randread --blocksize=4k

Cc Zach Marano <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
---
 block/blk-mq.c      | 14 ++++++++++++++
 block/blk-softirq.c |  7 +++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
block/blk-mq.c +14 -0
index 85a1c1a59c72..d4792c3ac983 100644
@@ -565,6 +565,20 @@ static void __blk_mq_complete_request(struct request *rq)
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
+	/*
+	 * Most of single queue controllers, there is only one irq vector
+	 * for handling IO completion, and the only irq's affinity is set
+	 * as all possible CPUs. On most of ARCHs, this affinity means the
+	 * irq is handled on one specific CPU.
+	 *
+	 * So complete IO reqeust in softirq context in case of single queue
+	 * for not degrading IO performance by irqsoff latency.
+	 */
+	if (rq->q->nr_hw_queues == 1) {
+		__blk_complete_request(rq);
+		return;
+	}
+
 	if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
 		rq->q->softirq_done_fn(rq);
 		return;
block/blk-softirq.c +3 -0
index 15c1f5e12eb8..b1df9b6c1731 100644
@@ -101,17 +101,20 @@ void __blk_complete_request(struct request *req)
 	struct request_queue *q = req->q;
 	unsigned long flags;
 	bool shared = false;
+	int rq_cpu;
 
 	BUG_ON(!q->softirq_done_fn);
 
+	rq_cpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
+
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 

block/blk-softirq.c void __blk_complete_request(struct request *req) · line 120

Quoted 10 lines
block/blk-softirq.c +2 -2
 	/*
 	 * Select completion CPU
 	 */
-	if (req->cpu != -1) {
-		ccpu = req->cpu;
+	if (rq_cpu != -1) {
+		ccpu = q->mq_ops ? req->mq_ctx->cpu : req->cpu;
 		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
 			shared = cpus_share_cache(cpu, ccpu);
 	} else

;

Only QUEUE_FLAG_SAME_COMP is set, we will try to complete the request on the cpu where it is allocated.

For the blk-legacy, the req->cpu is -1 when QUEUE_FLAG_SAME_COMP is not set.

Quoted 7 lines

blk_queue_bio

	if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
		req->cpu = raw_smp_processor_id()

But for the blk-mq, the req->mq_ctx->cpu must be valid. So we should check the QUEUE_FLAG_SAME_COMP for the blk-mq case.

Good catch, will fix it in V2.

Quoted 4 lines

On the other hand, how about split the softirq completion part out of __blk_complete_request ? It is confused when find __blk_complete_request in __blk_mq_complete_request.

__blk_complete_request() is just a common helper between blk-mq and legacy path. We have such usages already, so looks it is fine.

Thanks,
Ming