blk-mq: Fix a race condition in blk_mq_mark_tag_wait()
PATCH v2 1/1 blk-mq: Fix a race condition in blk_mq_mark_tag_wait()
Because the hctx lock is not held around the only blk_mq_tag_wakeup_all() call in the block layer, the wait queue entry removal in blk_mq_dispatch_wake() is protected by the wait queue lock only. Since the hctx->dispatch_wait entry can occur on any of the SBQ_WAIT_QUEUES, the wait queue presence check, adding .dispatch_wait to a wait queue and removing the wait queue entry must all be protected by both the hctx lock and the wait queue lock.
---
Changes compared to v1: made sure that the hctx lock is also held around the
list_del() call in blk_mq_dispatch_wake().
block/blk-mq-debugfs.c | 4 ++-- block/blk-mq-sched.c | 8 +++---- block/blk-mq.c | 48 ++++++++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 26 deletions(-)
block/blk-mq-debugfs.c +2 -2
@@ -399,7 +399,7 @@ static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos) {struct blk_mq_hw_ctx *hctx = m->private; -spin_lock (&hctx->lock); +spin_lock_irq (&hctx->lock);return seq_list_start (&hctx->dispatch, *pos); }
@@ -415,7 +415,7 @@ static void hctx_dispatch_stop(struct seq_file *m, void *v) {struct blk_mq_hw_ctx *hctx = m->private; -spin_unlock (&hctx->lock); +spin_unlock_irq (&hctx->lock); }static const struct seq_operations hctx_dispatch_seq_ops = {
block/blk-mq-sched.c +4 -4
@@ -190,10 +190,10 @@ void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)* more fair dispatch. */ if (!list_empty_careful (&hctx->dispatch)) { -spin_lock (&hctx->lock); +spin_lock_irq (&hctx->lock);if (!list_empty (&hctx->dispatch))list_splice_init (&hctx->dispatch, &rq_list); -spin_unlock (&hctx->lock); +spin_unlock_irq (&hctx->lock); }/*
@@ -368,9 +368,9 @@ static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, {/* dispatch flush rq directly */ if (rq->rq_flags &RQF_FLUSH_SEQ ) { -spin_lock (&hctx->lock); +spin_lock_irq (&hctx->lock);list_add (&rq->queuelist, &hctx->dispatch); -spin_unlock (&hctx->lock); +spin_unlock_irq (&hctx->lock);return true; }
block/blk-mq.c +28 -20
104 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 8 lines · Bart Van Assche
Because the hctx lock is not held around the only
blk_mq_tag_wakeup_all()call in the block layer, the wait queue entry removal inblk_mq_dispatch_wake()is protected by the wait queue lock only. Since thehctx->dispatch_waitentry can occur on any of theSBQ_WAIT_QUEUES, the wait queue presence check, adding .dispatch_wait to a wait queue and removing the wait queue entry must all be protected by both the hctx lock and the wait queue lock.
Actually we don't need to use hctx->lock for protecting hctx->dispatch_wait, and one new lock of hctx->dispatch_wait_lock is enough, please see the following patch:
https://marc.info/?l=linux-block&m=152998658713265&w=2
Then we can avoid to disable irq when acquiring hctx->lock.
Thanks,
Ming
Quoted 10 lines · Ming Lei
Quoted 8 lines · Bart Van Assche
Because the hctx lock is not held around the only
blk_mq_tag_wakeup_all()call in the block layer, the wait queue entry removal inblk_mq_dispatch_wake()is protected by the wait queue lock only. Since thehctx->dispatch_waitentry can occur on any of theSBQ_WAIT_QUEUES, the wait queue presence check, adding .dispatch_wait to a wait queue and removing the wait queue entry must all be protected by both the hctx lock and the wait queue lock.
Quoted 7 lines
Actually we don't need to use
hctx->lockfor protectinghctx->dispatch_wait, and one new lock ofhctx->dispatch_wait_lockis enough, please see the following patch:https://marc.info/?l=linux-block&m=152998658713265&w=2Then we can avoid to disable irq when acquiring
hctx->lock.
I think it's more a matter of taste than a technical decision to choose which patch goes upstream.
Bart.
Jens Axboe
maintainerQuoted 15 lines · Bart Van Assche
Quoted 14 lines · Ming Lei
Quoted 8 lines · Bart Van Assche
Because the hctx lock is not held around the only
blk_mq_tag_wakeup_all()call in the block layer, the wait queue entry removal inblk_mq_dispatch_wake()is protected by the wait queue lock only. Since thehctx->dispatch_waitentry can occur on any of theSBQ_WAIT_QUEUES, the wait queue presence check, adding .dispatch_wait to a wait queue and removing the wait queue entry must all be protected by both the hctx lock and the wait queue lock.Actually we don't need to use
hctx->lockfor protectinghctx->dispatch_wait, and one new lock ofhctx->dispatch_wait_lockis enough, please see the following patch:
Quoted 6 lines
Quoted 3 lines
https://marc.info/?l=linux-block&m=152998658713265&w=2Then we can avoid to disable irq when acquiring
hctx->lock.I think it's more a matter of taste than a technical decision to choose which patch goes upstream.
I do think the split lock is cleaner in this case, since it avoids making hctx->lock irq disabling.
Signature · 1 line
Jens Axboe