blk-mq: Fix stall due to recursive flush plug
PATCH v2 1/1 blk-mq: Fix stall due to recursive flush plug
We have seen rare IO stalls as follows:
* blk_mq_plug_issue_direct() is entered with an mq_list containing two requests.
* For the first request, it sets last == false and enters the driver's queue_rq callback.
* The driver queue_rq callback indirectly calls schedule() which calls blk_flush_plug(). This may happen if the driver has the BLK_MQ_F_BLOCKING flag set and is allowed to sleep in ->queue_rq.
* blk_flush_plug() handles the remaining request in the mq_list. mq_list is now empty.
* The original call to queue_rq resumes (with last == false).
* The loop in blk_mq_plug_issue_direct() terminates because there are no remaining requests in mq_list.
The IO is now stalled because the last request submitted to the driver had last == false and there was no subsequent call to commit_rqs().
Fix this by returning early in blk_mq_flush_plug_list() if rq_count is 0 which it will be in the recursive case, rather than checking if the mq_list is empty. At the same time, adjust one of the callers to skip the mq_list empty check as it is not necessary.
---
In v2:
* Update commit message and add a comment.
block/blk-core.c | 3 +-- block/blk-mq.c | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-)
block/blk-core.c +1 -2
@@ -1144,8 +1144,7 @@ void __blk_flush_plug(struct blk_plug *plug, bool from_schedule) {if (!list_empty (&plug->cb_list))flush_plug_callbacks (plug, from_schedule); -if (!rq_list_empty (plug->mq_list)) -blk_mq_flush_plug_list (plug, from_schedule); +blk_mq_flush_plug_list (plug, from_schedule);/* * Unconditionally flush out cached requests, even if the unplug * event came from schedule. Since we know hold references to the
block/blk-mq.c +8 -1
@@ -2742,7 +2742,14 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) {struct request *rq; -if (rq_list_empty (plug->mq_list)) +/* +* We may have been called recursively midway through handling +* plug->mq_list via a schedule() in the driver's queue_rq() callback. +* To avoid mq_list changing under our feet, clear rq_count early and +* bail out specifically if rq_count is 0 rather than checking +* whether the mq_list is empty. +*/ +if (plug->rq_count ==0 )return ; plug->rq_count =0 ;
Signature · 1 line
2.39.3
Quoted 23 lines · Ross Lagerwall
block/blk-core.c +1
-2@@ -1144,8 +1144,7 @@ void __blk_flush_plug(struct blk_plug *plug, bool from_schedule) {if (!list_empty (&plug->cb_list))flush_plug_callbacks (plug, from_schedule); -if (!rq_list_empty (plug->mq_list)) -blk_mq_flush_plug_list (plug, from_schedule); +blk_mq_flush_plug_list (plug, from_schedule);/* * Unconditionally flush out cached requests, even if the unplug * event came from schedule. Since we know hold references to the block/blk-mq.c +0
-1@@ -2742,7 +2742,14 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) {struct request *rq; -if (rq_list_empty (plug->mq_list))
block/blk-mq.c void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) · line 2754
Quoted 10 lines
block/blk-mq.c +8
-0+/* +* We may have been called recursively midway through handling +* plug->mq_list via a schedule() in the driver's queue_rq() callback. +* To avoid mq_list changing under our feet, clear rq_count early and +* bail out specifically if rq_count is 0 rather than checking +* whether the mq_list is empty. +*/ +if (plug->rq_count ==0 )return ; plug->rq_count =0 ;
Quoted 15 lines · Ross Lagerwall
We have seen rare IO stalls as follows:
*
blk_mq_plug_issue_direct()is entered with anmq_listcontaining two requests.
* For the first request, it sets last == false and enters the driver'squeue_rqcallback.
* The driverqueue_rqcallback indirectly callsschedule()which callsblk_flush_plug(). This may happen if the driver has theBLK_MQ_F_BLOCKINGflag set and is allowed to sleep in ->queue_rq.
*blk_flush_plug()handles the remaining request in themq_list.mq_listis now empty.
* The original call toqueue_rqresumes (with last == false).
* The loop inblk_mq_plug_issue_direct()terminates because there are no remaining requests inmq_list.
Quoted 1 line
[...]
Applied, thanks!
[1/1] blk-mq: Fix stall due to recursive flush plug
commit: 70904263512a74a3b8941dd9e6e515ca6fc57821
Best regards,
Signature · 1 line
Jens Axboe