Skip to contentSkip to conversation

blk-mq: Fix a race condition in blk_mq_mark_tag_wait()

PATCH In review Bart Van Assche 3 participants 4 messages Jun 27, 2018 – Jun 28, 2018 Block layer

lore
Share & tools
lore
#0 lore raw

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.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>

---

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
index 1c4532e92938..05fd98fad820 100644
@@ -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
index 56c493c6cd90..07dd6fe9f134 100644
@@ -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
#1 lore raw
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 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.

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

#2 lore raw
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 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.

Quoted 7 lines

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.

I think it's more a matter of taste than a technical decision to choose which patch goes upstream.

Bart.

maintainer
#3 lore raw
Quoted 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 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.

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:

Quoted 6 lines
Quoted 3 lines
	https://marc.info/?l=linux-block&m=152998658713265&w=2

Then 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