Skip to contentSkip to conversation

block: mq flush: fix race between IPI handler and mq flush worker

PATCH In review Ming Lei 3 participants 21 messages May 19, 2014 – May 28, 2014

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: mq flush: fix race between IPI handler and mq flush worker

In 'struct request', both 'csd' and 'requeue_work' are defined inside one union, unfortunately, the below race can be triggered:

- one PREFLUSH request may be queued inside the previous POSTFLUSH request's IPI handler, and both the two requests share one request instance(q->flush_rq)

- IPI interrupt for the previous POSTFLUSH request comes, and generic_smp_call_function_single_interrupt() is called to run the remote complete handler by csd->func();

- the POSTFLUSH flush request's end_io callback(flush_end_io) is called

- finally inside the path, blk_flush_queue_rq() calls INIT_WORK(&rq->requeue_work,...) and schedule the work for queuing a new PREFLUSH request

- csd->func(csd->info) returns, and csd_unlock(csd) is called, see generic_smp_call_function_single_interrupt()

- then the initialized rq->requeue_work is changed by csd_unlock(): the lowerest bit of requeue_work.func is observed that it was cleared, and function address can be unaligned on x86.

- the work func of mq_flush_run() will never be run

- fs sync hangs forever

This patch introduces one exclusive work_struct inside request_queue for queuing flush request only to fix the problem.

---
Another simple fix is to disable ipi for flush request, but looks this one should be better.

 block/blk-flush.c      |   43 +++++++++++++++++++++++++++++++++++++++----
 include/linux/blkdev.h |    1 +
 2 files changed, 40 insertions(+), 4 deletions(-)
block/blk-flush.c +39 -4
index ec7a224..42e8a34 100644
@@ -130,21 +130,56 @@ static void blk_flush_restore_request(struct request *rq)
 	blk_clear_rq_complete(rq);
 }
 
+static void __mq_flush_run(struct request *rq)
+{
+	memset(&rq->csd, 0, sizeof(rq->csd));
+	blk_mq_insert_request(rq, false, true, false);
+}
+
 static void mq_flush_run(struct work_struct *work)
 {
+	struct request_queue *q;
+	struct request *rq;
+
+	q = container_of(work, struct request_queue, flush_work);
+	rq = q->flush_rq;
+
+	__mq_flush_run(rq);
+}
+
+static void mq_data_flush_run(struct work_struct *work)
+{
 	struct request *rq;
 
 	rq = container_of(work, struct request, requeue_work);
 
-	memset(&rq->csd, 0, sizeof(rq->csd));
-	blk_mq_insert_request(rq, false, true, false);
+	__mq_flush_run(rq);
 }
 
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->requeue_work, mq_flush_run);
-		kblockd_schedule_work(&rq->requeue_work);
+		struct work_struct *wk;
+		work_func_t func;
+
+		/*
+		 * Flush request need use its own work_struct
+		 * instead of rq->requeue_work because it
+		 * will be changed by csd_unlock() in future
+		 * if the request(PREFLUSH) is being queued
+		 * from previous POSTFLUSH request's IPI
+		 * handler context.
+		 */
+		if (rq == rq->q->flush_rq) {
+			wk = &rq->q->flush_work;
+			func = mq_flush_run;
+		} else {
+			wk = &rq->requeue_work;
+			func = mq_data_flush_run;
+		}
+
+		INIT_WORK(wk, func);
+		kblockd_schedule_work(wk);
 		return false;
 	} else {
 		if (add_front)
include/linux/blkdev.h +1 -0
index 1f1ecc7..4c8f24c 100644
@@ -461,6 +461,7 @@ struct request_queue {
 	struct list_head	flush_queue[2];
 	struct list_head	flush_data_in_flight;
 	struct request		*flush_rq;
+	struct work_struct	flush_work;
 	spinlock_t		mq_flush_lock;
 
 	struct mutex		sysfs_lock;
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 2 lines · Ming Lei

Another simple fix is to disable ipi for flush request, but looks this one should be better.

I think the first thing is to bite the bullet and sort out and document the various unions in struct request for real.

For example the first union has the call_single_data for the blk-mq completions, while the second one has the ipi_list that is used by the old blk-softirq code.

If we get this right with a single union that contains a struct for each phase of the request we might find enough space to keep using the current way.

author
#2 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Ming Lei

Another simple fix is to disable ipi for flush request, but looks this one should be better.

I think the first thing is to bite the bullet and sort out and document the various unions in struct request for real.

I agree, unions should be documented in detail.

Quoted 2 lines

For example the first union has the call_single_data for the blk-mq

blk-softirq need rq->csd too in raise_blk_irq().

Quoted 2 lines

completions, while the second one has the ipi_list that is used by the old blk-softirq code.

Also we can put some mq specific fields and legacy fields into one union too.

Quoted 4 lines

If we get this right with a single union that contains a struct for each phase of the request we might find enough space to keep using the current way.

If we can figure it out, that should be better solution, but changing/ merging fields may affect performance too, and need careful verification.

Thanks,

Signature · 1 line
Ming Lei
author
#3 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Ming Lei

Another simple fix is to disable ipi for flush request, but looks this one should be better.

I think the first thing is to bite the bullet and sort out and document the various unions in struct request for real.

Considered the problem can be reproduced easily and cause data loss on kvm-guest, I hope fix can be seen in 3.15. Also it should be backported to 3.14 because someone may enable ctx->ipi_redirect.

Thinking it further, looks the patch is fine:

- sizeof(call_single_data) and sizeof(work_struct) is very close and both are not small, so it is reasonable to put the two in one union for saving request space

- the conflict on the two structures just happens with flush requests because rq->requeue_work is only used to queue flush requests

- in the flush sequence, the conflict won't happen on request with data, so it only happens with completing and queueing PREFLUSH/POSTFLUSH request.

Any comments and suggestions?

Thanks,

Signature · 1 line
Ming Lei
#4 lore raw
Quoted 3 lines · Ming Lei

- the conflict on the two structures just happens with flush requests because rq->requeue_work is only used to queue flush requests

Once we get non-trivial block drivers we'll need to be able to requeue arbitrary requests, that's why I added blk_mq_requeue_request. The scsi-mq work that I plant to submit for the next merge window is the prime example.

I'd really prefer to avoid breaking that use case if we can avoid it.

Note that the flush code already is very nasy for blk-mq and this just makes it worse.

One fix that would also help to sort out some of the flush issues would be to add a list of requests that need requeueing to the blk_mq context, which we can add requeusts to from irq context. The next time we run hw contexts for the queue we'll pick them up in user context and insert them.

author
#5 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Ming Lei

- the conflict on the two structures just happens with flush requests because rq->requeue_work is only used to queue flush requests

Once we get non-trivial block drivers we'll need to be able to requeue arbitrary requests, that's why I added blk_mq_requeue_request.

I am wondering if virtio-blk is trivial block driver, :-) And virtio-blk still supports requeuing with returning BLK_MQ_RQ_QUEUE_BUSY.

Quoted 2 lines

The scsi-mq work that I plant to submit for the next merge window is the prime example.

It depends if one scsi-mq req has to requeue itself with rq->requeue_work inside its own .softirq_done_fn. If yes, we can't put call_single_data and requeue_work into one union simply. From you last scsi-mq post, looks the request may do that if I understand correctly.

But scsi_cmnd has already one 'abort_work', and I am wondering why scsi-mq requeuing doesn't share its own requeue_work with abort_work, which seems doable since requeuing and aborting belong to different stage.

Quoted 2 lines

I'd really prefer to avoid breaking that use case if we can avoid it.

This patch won't break the coming scsi-mq, and it is a fix.

I'd like to figure out one patch to cover scsi-mq case if we can get that before 3.15 release since your 'respect-affinity' patch has enabled IPI at default already. Otherwise, we still have enough time to fix the issue for scsi-mq, don't we?

Quoted 4 lines

Note that the flush code already is very nasy for blk-mq and this just makes it worse.

I think the patch is clean and simple, with documenting the special conflict case clearly too.

Quoted 4 lines

One fix that would also help to sort out some of the flush issues would be to add a list of requests that need requeueing to the blk_mq context, which we can add requeusts to from irq context. The next time we run

That won't be easy to introduce a requeue_list for the purpose since we need to keep order of requests per blk-mq ctx.

Also lockless list won't work since there are both 'add_front'/'add_tail' requirement.

Quoted 2 lines

hw contexts for the queue we'll pick them up in user context and insert them.

IMO, the requests can be inserted to ctx list directly from irq context, but with cost of spin_lock_irqsave(&ctx->lock) everywhere.

Follows current ideas:
1), this patch with scsi-mq sharing abort_work together? 2), move requeue_work out of the union inside request 3), spin_lock_irqsave(&ctx->lock) everywhere and requeue request directly to ctx without using work

Any other ideas?

Thanks,
--
Ming Lei

#6 lore raw
Quoted 1 line · Ming Lei

I am wondering if virtio-blk is trivial block driver, :-)

It's about as simple as it gets.

Quoted 7 lines
Quoted 2 lines

The scsi-mq work that I plant to submit for the next merge window is the prime example.

It depends if one scsi-mq req has to requeue itself with rq->requeue_work inside its own .softirq_done_fn. If yes, we can't put call_single_data and requeue_work into one union simply. From you last scsi-mq post, looks the request may do that if I understand correctly.

Requeueing a request from the completion handler is indeed what we'll need with various more complete drivers.

Quoted 2 lines

I think the patch is clean and simple, with documenting the special conflict case clearly too.

While I can't say anything against the fact that it fixes the issue it's neither clean nor simple.

Quoted 5 lines

Follows current ideas:
1), this patch with scsi-mq sharing abort_work together? 2), move requeue_work out of the union inside request 3), spin_lock_irqsave(&ctx->lock) everywhere and requeue request directly to ctx without using work

I think Jens very much wanted to avoid irq disabling in the I/O path if possible. If we have a separate requeue list with it's separate lock we can avoid that unless we actually have to take requests of that requeue list. I can look into that implementation.

author
#7 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 1 line · Ming Lei

I am wondering if virtio-blk is trivial block driver, :-)

It's about as simple as it gets.

Quoted 3 lines
Quoted 2 lines

The scsi-mq work that I plant to submit for the next merge window is the prime example.

Quoted 7 lines
Quoted 4 lines

It depends if one scsi-mq req has to requeue itself with rq->requeue_work inside its own .softirq_done_fn. If yes, we can't put call_single_data and requeue_work into one union simply. From you last scsi-mq post, looks the request may do that if I understand correctly.

Requeueing a request from the completion handler is indeed what we'll need with various more complete drivers.

At least for scsi, the current scsi_cmnd->abort_work(reusing) should be enough for requeing the command. For other drivers, maybe they can make use of something like scsi_cmnd->abort_work too.

Quoted 6 lines
Quoted 3 lines

I think the patch is clean and simple, with documenting the special conflict case clearly too.

While I can't say anything against the fact that it fixes the issue it's neither clean nor simple.

It just uses q->flush_rq's own work_struct for requeuing itself, that is it. I'd like to see a cleaner/simpler solution without wasting space and extra complexity.

Quoted 11 lines
Quoted 6 lines

Follows current ideas:
1), this patch with scsi-mq sharing abort_work together? 2), move requeue_work out of the union inside request 3), spin_lock_irqsave(&ctx->lock) everywhere and requeue request directly to ctx without using work

I think Jens very much wanted to avoid irq disabling in the I/O path if possible. If we have a separate requeue list with it's separate lock we can avoid that unless we actually have to take requests of that requeue list. I can look into that implementation.

I am wondering if you can keep requests in order per blk-mq ctx since you introduce another list.

Thanks,

Signature · 1 line
Ming Lei
#8 lore raw

Here is my counter proposal that requeues via two lists and a work struct in the request_queue. I've also tested it with scsi-mq.

Quoted 1 line

From 95f15eda9a87d8545e1dff1b996d9227dfca1129 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Wed, 21 May 2014 19:37:11 +0200
Subject: blk-mq: add helper to insert requests from irq context

---
 block/blk-flush.c      |   16 ++++----------
 block/blk-mq.c         |   54 +++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/blk-mq.h |    3 +++
 include/linux/blkdev.h |    6 +++++-
 4 files changed, 65 insertions(+), 14 deletions(-)
block/blk-flush.c +4 -12
index ec7a224..ef608b3 100644
@@ -130,21 +130,13 @@ static void blk_flush_restore_request(struct request *rq)
 	blk_clear_rq_complete(rq);
 }
 
-static void mq_flush_run(struct work_struct *work)
-{
-	struct request *rq;
-
-	rq = container_of(work, struct request, requeue_work);
-
-	memset(&rq->csd, 0, sizeof(rq->csd));
-	blk_mq_insert_request(rq, false, true, false);
-}
-
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->requeue_work, mq_flush_run);
-		kblockd_schedule_work(&rq->requeue_work);
+		struct request_queue *q = rq->q;
+
+		blk_mq_add_to_requeue_list(rq, add_front);
+		blk_mq_kick_requeue_list(q);
 		return false;
 	} else {
 		if (add_front)
block/blk-mq.c +53 -1

69 lines not on this page · raw message

include/linux/blk-mq.h +3 -0
index f76bb18..81bb7f1 100644
@@ -173,6 +173,9 @@ void __blk_mq_end_io(struct request *rq, int error);
 
 void blk_mq_requeue_request(struct request *rq);
 
+void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
+void blk_mq_kick_requeue_list(struct request_queue *q);
+
 void blk_mq_complete_request(struct request *rq);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
include/linux/blkdev.h +5 -1
index b0104ba..e137348 100644
@@ -99,7 +99,6 @@ struct request {
 	struct list_head queuelist;
 	union {
 		struct call_single_data csd;
-		struct work_struct requeue_work;
 		unsigned long fifo_time;
 	};
 
@@ -463,6 +462,11 @@ struct request_queue {
 	struct request		*flush_rq;
 	spinlock_t		mq_flush_lock;
 
+	struct list_head	requeue_list;
+	struct list_head	requeue_head_list;
+	spinlock_t		requeue_lock;
+	struct work_struct	requeue_work;
+
 	struct mutex		sysfs_lock;
 
 	int			bypass_depth;
Signature · 1 line
1.7.10.4
maintainer
#9 lore raw
Quoted 3 lines · Christoph Hellwig

Here is my counter proposal that requeues via two lists and a work struct in the request_queue. I've also tested it with scsi-mq.

I like this, moves the state out of the request. But how about we consolidate the two requeue requests lists, and just mark the request as needing head insertion instead? Just add a cmd_flags flag, REQ_REQUEUE_HEAD or something.

Signature · 1 line
Jens Axboe
#10 lore raw
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Christoph Hellwig

Here is my counter proposal that requeues via two lists and a work struct in the request_queue. I've also tested it with scsi-mq.

I like this, moves the state out of the request. But how about we consolidate the two requeue requests lists, and just mark the request as needing head insertion instead? Just add a cmd_flags flag, REQ_REQUEUE_HEAD or something.

That should be doable, just didn't like introducing even more flags.

maintainer
#11 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Christoph Hellwig

Here is my counter proposal that requeues via two lists and a work struct in the request_queue. I've also tested it with scsi-mq.

Quoted 6 lines
Quoted 4 lines

I like this, moves the state out of the request. But how about we consolidate the two requeue requests lists, and just mark the request as needing head insertion instead? Just add a cmd_flags flag, REQ_REQUEUE_HEAD or something.

That should be doable, just didn't like introducing even more flags.

Space is plenty big again now... The down side is that the splice trick wont work, but if we have more than a few requests on the requeue list, we're doing it wrong anyway.

Signature · 1 line
Jens Axboe
#12 lore raw
Quoted 3 lines · Jens Axboe

Space is plenty big again now... The down side is that the splice trick wont work, but if we have more than a few requests on the requeue list, we're doing it wrong anyway.

I don't see a problem with the splice. How about this version (untested so far)?

Quoted 1 line

From 95f15eda9a87d8545e1dff1b996d9227dfca1129 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Wed, 21 May 2014 19:37:11 +0200
Subject: blk-mq: add helper to insert requests from irq context

---
 block/blk-flush.c      |   16 ++++----------
 block/blk-mq.c         |   54 +++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/blk-mq.h |    3 +++
 include/linux/blkdev.h |    6 +++++-
 4 files changed, 65 insertions(+), 14 deletions(-)
block/blk-flush.c +4 -12
index ec7a224..ef608b3 100644
@@ -130,21 +130,13 @@ static void blk_flush_restore_request(struct request *rq)
 	blk_clear_rq_complete(rq);
 }
 
-static void mq_flush_run(struct work_struct *work)
-{
-	struct request *rq;
-
-	rq = container_of(work, struct request, requeue_work);
-
-	memset(&rq->csd, 0, sizeof(rq->csd));
-	blk_mq_insert_request(rq, false, true, false);
-}
-
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->requeue_work, mq_flush_run);
-		kblockd_schedule_work(&rq->requeue_work);
+		struct request_queue *q = rq->q;
+
+		blk_mq_add_to_requeue_list(rq, add_front);
+		blk_mq_kick_requeue_list(q);
 		return false;
 	} else {
 		if (add_front)
block/blk-mq.c +53 -1

69 lines not on this page · raw message

include/linux/blk-mq.h +3 -0
index f76bb18..81bb7f1 100644
@@ -173,6 +173,9 @@ void __blk_mq_end_io(struct request *rq, int error);
 
 void blk_mq_requeue_request(struct request *rq);
 
+void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
+void blk_mq_kick_requeue_list(struct request_queue *q);
+
 void blk_mq_complete_request(struct request *rq);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
include/linux/blkdev.h +5 -1
index b0104ba..e137348 100644
@@ -99,7 +99,6 @@ struct request {
 	struct list_head queuelist;
 	union {
 		struct call_single_data csd;
-		struct work_struct requeue_work;
 		unsigned long fifo_time;
 	};
 
@@ -463,6 +462,11 @@ struct request_queue {
 	struct request		*flush_rq;
 	spinlock_t		mq_flush_lock;
 
+	struct list_head	requeue_list;
+	struct list_head	requeue_head_list;
+	spinlock_t		requeue_lock;
+	struct work_struct	requeue_work;
+
 	struct mutex		sysfs_lock;
 
 	int			bypass_depth;
Signature · 1 line
1.7.10.4
maintainer
#13 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Jens Axboe

Space is plenty big again now... The down side is that the splice trick wont work, but if we have more than a few requests on the requeue list, we're doing it wrong anyway.

I don't see a problem with the splice. How about this version (untested so far)?

True, I guess it's iterated afterwards anyway, so that's where you just check it.

But I think you sent the old one again, not the new variant :-)

Signature · 1 line
Jens Axboe
#14 lore raw
Quoted 1 line · Jens Axboe

But I think you sent the old one again, not the new variant :-)

Oh well, next try:

Quoted 1 line

From ff3685b6052a31fbafc72551cdf7c123cd2b4634 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Wed, 21 May 2014 19:37:11 +0200
Subject: blk-mq: add helper to insert requests from irq context

---
 block/blk-flush.c      |   16 +++---------
 block/blk-mq.c         |   64 +++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/blk-mq.h |    3 +++
 include/linux/blkdev.h |    5 +++-
 4 files changed, 74 insertions(+), 14 deletions(-)
block/blk-flush.c +4 -12
index ec7a224..ef608b3 100644
@@ -130,21 +130,13 @@ static void blk_flush_restore_request(struct request *rq)
 	blk_clear_rq_complete(rq);
 }
 
-static void mq_flush_run(struct work_struct *work)
-{
-	struct request *rq;
-
-	rq = container_of(work, struct request, requeue_work);
-
-	memset(&rq->csd, 0, sizeof(rq->csd));
-	blk_mq_insert_request(rq, false, true, false);
-}
-
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->requeue_work, mq_flush_run);
-		kblockd_schedule_work(&rq->requeue_work);
+		struct request_queue *q = rq->q;
+
+		blk_mq_add_to_requeue_list(rq, add_front);
+		blk_mq_kick_requeue_list(q);
 		return false;
 	} else {
 		if (add_front)
block/blk-mq.c +63 -1

79 lines not on this page · raw message

include/linux/blk-mq.h +3 -0
index f76bb18..81bb7f1 100644
@@ -173,6 +173,9 @@ void __blk_mq_end_io(struct request *rq, int error);
 
 void blk_mq_requeue_request(struct request *rq);
 
+void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
+void blk_mq_kick_requeue_list(struct request_queue *q);
+
 void blk_mq_complete_request(struct request *rq);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
include/linux/blkdev.h +4 -1
index b0104ba..e90e169 100644
@@ -99,7 +99,6 @@ struct request {
 	struct list_head queuelist;
 	union {
 		struct call_single_data csd;
-		struct work_struct requeue_work;
 		unsigned long fifo_time;
 	};
 
@@ -463,6 +462,10 @@ struct request_queue {
 	struct request		*flush_rq;
 	spinlock_t		mq_flush_lock;
 
+	struct list_head	requeue_list;
+	spinlock_t		requeue_lock;
+	struct work_struct	requeue_work;
+
 	struct mutex		sysfs_lock;
 
 	int			bypass_depth;
Signature · 1 line
1.7.10.4
maintainer
#15 lore raw
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Jens Axboe

But I think you sent the old one again, not the new variant :-)

Oh well, next try:

This looks good to me. Was trying to think of ways to reduce that to one list iteration, but I think it's cleaner to just retain the two separate ones.

Reusing REQ_SOFTBARRIER is fine as well, not used in blk-mq otherwise.

Let me know when you have runtime verified it.

Signature · 1 line
Jens Axboe
author
#16 lore raw
Quoted 6 lines · Jens Axboe
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Jens Axboe

But I think you sent the old one again, not the new variant :-)

Oh well, next try:

Quoted 7 lines

This looks good to me. Was trying to think of ways to reduce that to one list iteration, but I think it's cleaner to just retain the two separate ones.

Reusing REQ_SOFTBARRIER is fine as well, not used in blk-mq otherwise.

Let me know when you have runtime verified it.

Looks writing over ext4(especially sync writing) can survive with Christoph's patch now, thanks Christoph.

Reported-and-tested-by Ming Lei <[email protected]>

Thanks,

Signature · 1 line
Ming Lei
maintainer
#17 lore raw
Quoted 11 lines · Ming Lei
Quoted 10 lines · Jens Axboe
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Jens Axboe

But I think you sent the old one again, not the new variant :-)

Oh well, next try:

This looks good to me. Was trying to think of ways to reduce that to one list iteration, but I think it's cleaner to just retain the two separate ones.

Quoted 8 lines
Quoted 3 lines

Reusing REQ_SOFTBARRIER is fine as well, not used in blk-mq otherwise.

Let me know when you have runtime verified it.

Looks writing over ext4(especially sync writing) can survive with Christoph's patch now, thanks Christoph.

Reported-and-tested-by Ming Lei <[email protected]>

Great! I'll queue it up here too, then.

Signature · 1 line
Jens Axboe
maintainer
#18 lore raw
Quoted 14 lines · Jens Axboe
Quoted 13 lines · Ming Lei
Quoted 12 lines · Jens Axboe
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Jens Axboe

But I think you sent the old one again, not the new variant :-)

Oh well, next try:

This looks good to me. Was trying to think of ways to reduce that to one list iteration, but I think it's cleaner to just retain the two separate ones.

Reusing REQ_SOFTBARRIER is fine as well, not used in blk-mq otherwise.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Let me know when you have runtime verified it.

Looks writing over ext4(especially sync writing) can survive with Christoph's patch now, thanks Christoph.

Reported-and-tested-by Ming Lei <[email protected]>

Great! I'll queue it up here too, then.

Christoph, I'll just run a few tests and then queue it up in the morning. Can you send a properly signed-off patch with a commit message as well? I was writing one up, but I still need the signed-off-by.

Signature · 1 line
Jens Axboe
Tested-by Ming Lei
#19 lore raw
Quoted 3 lines · Jens Axboe

Christoph, I'll just run a few tests and then queue it up in the morning. Can you send a properly signed-off patch with a commit message as well? I was writing one up, but I still need the signed-off-by.

Attached.

Quoted 1 line

From 125823de325211c3e96dad884b0d1a52ec04947d Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Wed, 21 May 2014 19:37:11 +0200
Subject: blk-mq: add helper to insert requests from irq context

Both the cache flush state machine and the SCSI midlayer want to submit requests from irq context, and the current per-request requeue_work unfortunately causes corruption due to sharing with the csd field for flushes. Replace them with a per-request_queue list of requests to be requeued.

Based on an earlier test by Ming Lei.

---
 block/blk-flush.c      |   16 +++---------
 block/blk-mq.c         |   64 +++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/blk-mq.h |    3 +++
 include/linux/blkdev.h |    5 +++-
 4 files changed, 74 insertions(+), 14 deletions(-)
block/blk-flush.c +4 -12
index ec7a224..ef608b3 100644
@@ -130,21 +130,13 @@ static void blk_flush_restore_request(struct request *rq)
 	blk_clear_rq_complete(rq);
 }
 
-static void mq_flush_run(struct work_struct *work)
-{
-	struct request *rq;
-
-	rq = container_of(work, struct request, requeue_work);
-
-	memset(&rq->csd, 0, sizeof(rq->csd));
-	blk_mq_insert_request(rq, false, true, false);
-}
-
 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
 {
 	if (rq->q->mq_ops) {
-		INIT_WORK(&rq->requeue_work, mq_flush_run);
-		kblockd_schedule_work(&rq->requeue_work);
+		struct request_queue *q = rq->q;
+
+		blk_mq_add_to_requeue_list(rq, add_front);
+		blk_mq_kick_requeue_list(q);
 		return false;
 	} else {
 		if (add_front)
block/blk-mq.c +63 -1

79 lines not on this page · raw message

include/linux/blk-mq.h +3 -0
index f76bb18..81bb7f1 100644
@@ -173,6 +173,9 @@ void __blk_mq_end_io(struct request *rq, int error);
 
 void blk_mq_requeue_request(struct request *rq);
 
+void blk_mq_add_to_requeue_list(struct request *rq, bool at_head);
+void blk_mq_kick_requeue_list(struct request_queue *q);
+
 void blk_mq_complete_request(struct request *rq);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
include/linux/blkdev.h +4 -1
index b0104ba..e90e169 100644
@@ -99,7 +99,6 @@ struct request {
 	struct list_head queuelist;
 	union {
 		struct call_single_data csd;
-		struct work_struct requeue_work;
 		unsigned long fifo_time;
 	};
 
@@ -463,6 +462,10 @@ struct request_queue {
 	struct request		*flush_rq;
 	spinlock_t		mq_flush_lock;
 
+	struct list_head	requeue_list;
+	spinlock_t		requeue_lock;
+	struct work_struct	requeue_work;
+
 	struct mutex		sysfs_lock;
 
 	int			bypass_depth;
Signature · 1 line
1.7.10.4
maintainer
#20 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Jens Axboe

Christoph, I'll just run a few tests and then queue it up in the morning. Can you send a properly signed-off patch with a commit message as well? I was writing one up, but I still need the signed-off-by.

Attached.

Thanks, added.

Signature · 1 line
Jens Axboe