Skip to contentSkip to conversation

block: misc changes

PATCH In review Ming Lei 4 participants 13 messages Mar 27, 2017 – Mar 29, 2017

Subsystems (2)
lore
Share & tools
lore
author
#6 lore raw

PATCH v3 3/4 block: rename blk_mq_freeze_queue_start()

In reply to Ming Lei

As the .q_usage_counter is used by both legacy and mq path, we need to block new I/O if queue becomes dead in blk_queue_enter().

So rename it and we can use this function in both paths.

---
 block/blk-core.c                  |  2 +-
 block/blk-mq.c                    | 10 +++++-----
 drivers/block/mtip32xx/mtip32xx.c |  2 +-
 drivers/nvme/host/core.c          |  2 +-
 include/linux/blk-mq.h            |  2 +-
 5 files changed, 9 insertions(+), 9 deletions(-)
block/blk-core.c +1 -1
index 5e8963bc98d9..60f364e1d36b 100644
@@ -670,7 +670,7 @@ int blk_queue_enter(struct request_queue *q, bool nowait)
 			return -EBUSY;
 
 		/*
-		 * read pair of barrier in blk_mq_freeze_queue_start(),
+		 * read pair of barrier in blk_freeze_queue_start(),
 		 * we need to order reading __PERCPU_REF_DEAD flag of
 		 * .q_usage_counter and reading .mq_freeze_depth,
 		 * otherwise the following wait may never return if the
block/blk-mq.c +5 -5
index b36f0481ba0e..5370b4f750ff 100644
@@ -68,7 +68,7 @@ static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
 	sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
 }
 
-void blk_mq_freeze_queue_start(struct request_queue *q)
+void blk_freeze_queue_start(struct request_queue *q)
 {
 	int freeze_depth;
 
@@ -78,7 +78,7 @@ void blk_mq_freeze_queue_start(struct request_queue *q)
 		blk_mq_run_hw_queues(q, false);
 	}
 }
-EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
+EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
 
 void blk_mq_freeze_queue_wait(struct request_queue *q)
 {
@@ -108,7 +108,7 @@ void blk_freeze_queue(struct request_queue *q)
 	 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
 	 * exported to drivers as the only user for unfreeze is blk_mq.
 	 */
-	blk_mq_freeze_queue_start(q);
+	blk_freeze_queue_start(q);
 	blk_mq_freeze_queue_wait(q);
 }
 
@@ -746,7 +746,7 @@ static void blk_mq_timeout_work(struct work_struct *work)
 	 * percpu_ref_tryget directly, because we need to be able to
 	 * obtain a reference even in the short window between the queue
 	 * starting to freeze, by dropping the first reference in
-	 * blk_mq_freeze_queue_start, and the moment the last request is
+	 * blk_freeze_queue_start, and the moment the last request is
 	 * consumed, marked by the instant q_usage_counter reaches
 	 * zero.
 	 */
@@ -2376,7 +2376,7 @@ static void blk_mq_queue_reinit_work(void)
 	 * take place in parallel.
 	 */
 	list_for_each_entry(q, &all_q_list, all_q_node)
-		blk_mq_freeze_queue_start(q);
+		blk_freeze_queue_start(q);
 	list_for_each_entry(q, &all_q_list, all_q_node)
 		blk_mq_freeze_queue_wait(q);
 
drivers/block/mtip32xx/mtip32xx.c +1 -1
index f96ab717534c..c96c35ab39df 100644
@@ -4162,7 +4162,7 @@ static int mtip_block_remove(struct driver_data *dd)
 		dev_info(&dd->pdev->dev, "device %s surprise removal\n",
 						dd->disk->disk_name);
 
-	blk_mq_freeze_queue_start(dd->queue);
+	blk_freeze_queue_start(dd->queue);
 	blk_mq_stop_hw_queues(dd->queue);
 	blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
 
drivers/nvme/host/core.c +1 -1
index 9b3b57fef446..4a6d7f408769 100644
@@ -2386,7 +2386,7 @@ void nvme_start_freeze(struct nvme_ctrl *ctrl)
 
 	mutex_lock(&ctrl->namespaces_mutex);
 	list_for_each_entry(ns, &ctrl->namespaces, list)
-		blk_mq_freeze_queue_start(ns->queue);
+		blk_freeze_queue_start(ns->queue);
 	mutex_unlock(&ctrl->namespaces_mutex);
 }
 EXPORT_SYMBOL_GPL(nvme_start_freeze);
include/linux/blk-mq.h +1 -1
index 5b3e201c8d4f..ea2e9dcd3aef 100644
@@ -243,7 +243,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
 		busy_tag_iter_fn *fn, void *priv);
 void blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_unfreeze_queue(struct request_queue *q);
-void blk_mq_freeze_queue_start(struct request_queue *q);
+void blk_freeze_queue_start(struct request_queue *q);
 void blk_mq_freeze_queue_wait(struct request_queue *q);
 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
 				     unsigned long timeout);
Signature · 1 line
2.9.3

Ming Lei

author
#0 lore raw

v3 cover block: misc changes

Hi,

The 1st patch add comments on blk-mq races with timeout handler.

The other 3 patches improves handling for dying queue:
- the 2nd one adds one barrier in blk_queue_enter() for avoiding hanging caused by out-of-order
- the 3rd and 4th patches block new I/O entering queue after queue is set as dying

V3:
- tweak comments as suggested by Bart Van Assche

V2:
- add one missing barrier in blk_queue_enter()

V1:
- add comments on races related with timeout handler
- add Tested-by & Reviewed-by tag

thanks,
Ming

Ming Lei (4):
blk-mq: comment on races related with timeout handler
block: add a read barrier in blk_queue_enter()
block: rename blk_mq_freeze_queue_start()
block: block new I/O just after queue is set as dying

 block/blk-core.c                  | 16 ++++++++++++++++
 block/blk-mq.c                    | 32 +++++++++++++++++++++++++++-----
 drivers/block/mtip32xx/mtip32xx.c |  2 +-
 drivers/nvme/host/core.c          |  2 +-
 include/linux/blk-mq.h            |  2 +-
 5 files changed, 46 insertions(+), 8 deletions(-)
Signature · 1 line
2.9.3
author
#3 lore raw

PATCH v3 2/4 block: add a read barrier in blk_queue_enter()

Without the barrier, reading DEAD flag of .q_usage_counter and reading .mq_freeze_depth may be reordered, then the following wait_event_interruptible() may never return.

---
 block/blk-core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
block/blk-core.c +9 -0
index ad388d5e309a..5e8963bc98d9 100644
@@ -669,6 +669,15 @@ int blk_queue_enter(struct request_queue *q, bool nowait)
 		if (nowait)
 			return -EBUSY;
 
+		/*
+		 * read pair of barrier in blk_mq_freeze_queue_start(),
+		 * we need to order reading __PERCPU_REF_DEAD flag of
+		 * .q_usage_counter and reading .mq_freeze_depth,
+		 * otherwise the following wait may never return if the
+		 * two reads are reordered.
+		 */
+		smp_rmb();
+
 		ret = wait_event_interruptible(q->mq_freeze_wq,
 				!atomic_read(&q->mq_freeze_depth) ||
 				blk_queue_dying(q));
Signature · 1 line
2.9.3
author
#1 lore raw

PATCH v3 1/4 blk-mq: comment on races related with timeout handler

In reply to Ming Lei

This patch adds comment on two races related with timeout handler:

- requeue from queue busy vs. timeout
- rq free & reallocation vs. timeout

Both the races themselves and current solution aren't explicit enough, so add comments on them.

---
 block/blk-mq.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
block/blk-mq.c +22 -0
index c212b9644a9f..b36f0481ba0e 100644
@@ -523,6 +523,15 @@ void blk_mq_start_request(struct request *rq)
 }
 EXPORT_SYMBOL(blk_mq_start_request);
 
+/*
+ * When we reach here because queue is busy, REQ_ATOM_COMPLETE
+ * flag isn't set yet, so there may be race with timeout hanlder,
+ * but given rq->deadline is just set in .queue_rq() under
+ * this situation, the race won't be possible in reality because
+ * rq->timeout should be set as big enough to cover the window
+ * between blk_mq_start_request() called from .queue_rq() and
+ * clearing REQ_ATOM_STARTED here.
+ */
 static void __blk_mq_requeue_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
@@ -696,6 +705,19 @@ static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
 	if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
 		return;
 
+	/*
+	 * The rq being checked may have been freed and reallocated
+	 * out already here, we avoid this race by checking rq->deadline
+	 * and REQ_ATOM_COMPLETE flag together:
+	 *
+	 * - if rq->deadline is observed as new value because of
+	 *   reusing, the rq won't be timed out because of timing.
+	 * - if rq->deadline is observed as previous value,
+	 *   REQ_ATOM_COMPLETE flag won't be cleared in reuse path
+	 *   because we put a barrier between setting rq->deadline
+	 *   and clearing the flag in blk_mq_start_request(), so
+	 *   this rq won't be timed out too.
+	 */
 	if (time_after_eq(jiffies, rq->deadline)) {
 		if (!blk_mark_rq_complete(rq))
 			blk_mq_rq_timed_out(rq, reserved);
Signature · 1 line
2.9.3
author
#8 lore raw

PATCH v3 4/4 block: block new I/O just after queue is set as dying

In reply to Ming Lei

Before commit 780db2071a(blk-mq: decouble blk-mq freezing from generic bypassing), the dying flag is checked before entering queue, and Tejun converts the checking into .mq_freeze_depth, and assumes the counter is increased just after dying flag is set. Unfortunately we doesn't do that in blk_set_queue_dying().

This patch calls blk_freeze_queue_start() in blk_set_queue_dying(), so that we can block new I/O coming once the queue is set as dying.

Given blk_set_queue_dying() is always called in remove path of block device, and queue will be cleaned up later, we don't need to worry about undoing the counter.

---
 block/blk-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
block/blk-core.c +10 -3
index 60f364e1d36b..e22c4ea002ec 100644
@@ -500,6 +500,13 @@ void blk_set_queue_dying(struct request_queue *q)
 	queue_flag_set(QUEUE_FLAG_DYING, q);
 	spin_unlock_irq(q->queue_lock);
 
+	/*
+	 * When queue DYING flag is set, we need to block new req
+	 * entering queue, so we call blk_freeze_queue_start() to
+	 * prevent I/O from crossing blk_queue_enter().
+	 */
+	blk_freeze_queue_start(q);
+
 	if (q->mq_ops)
 		blk_mq_wake_waiters(q);
 	else {
@@ -672,9 +679,9 @@ int blk_queue_enter(struct request_queue *q, bool nowait)
 		/*
 		 * read pair of barrier in blk_freeze_queue_start(),
 		 * we need to order reading __PERCPU_REF_DEAD flag of
-		 * .q_usage_counter and reading .mq_freeze_depth,
-		 * otherwise the following wait may never return if the
-		 * two reads are reordered.
+		 * .q_usage_counter and reading .mq_freeze_depth or
+		 * queue dying flag, otherwise the following wait may
+		 * never return if the two reads are reordered.
 		 */
 		smp_rmb();
 
Signature · 1 line
2.9.3
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] blk-mq: comment on races related with timeout handler

In reply to Ming Lei
Quoted 6 lines · Ming Lei

This patch adds comment on two races related with timeout handler:

- requeue from queue busy vs. timeout
- rq free & reallocation vs. timeout

Quoted 7 lines

Both the races themselves and current solution aren't explicit enough, so add comments on them.

---

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v3 3/4
#7 lore raw

Re: [PATCH v3 3/4] block: rename blk_mq_freeze_queue_start()

In reply to Ming Lei
Quoted 11 lines · Ming Lei

As the .q_usage_counter is used by both legacy and mq path, we need to block new I/O if queue becomes dead in blk_queue_enter().

So rename it and we can use this function in both paths.

---

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v3 4/4
#9 lore raw

Re: [PATCH v3 4/4] block: block new I/O just after queue is set as dying

In reply to Ming Lei
Quoted 13 lines · Ming Lei

Before commit 780db2071a(blk-mq: decouble blk-mq freezing from generic bypassing), the dying flag is checked before entering queue, and Tejun converts the checking into .mq_freeze_depth, and assumes the counter is increased just after dying flag is set. Unfortunately we doesn't do that in blk_set_queue_dying().

This patch calls blk_freeze_queue_start() in blk_set_queue_dying(), so that we can block new I/O coming once the queue is set as dying.

Given blk_set_queue_dying() is always called in remove path of block device, and queue will be cleaned up later, we don't need to worry about undoing the counter.

Quoted 5 lines

---

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v3 2/4
#4 lore raw

Re: [PATCH v3 2/4] block: add a read barrier in blk_queue_enter()

In reply to Ming Lei
Quoted 7 lines · Ming Lei

Without the barrier, reading DEAD flag of .q_usage_counter and reading .mq_freeze_depth may be reordered, then the following wait_event_interruptible() may never return.

---

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v3 2/4
#5 lore raw

Re: [PATCH v3 2/4] block: add a read barrier in blk_queue_enter()

In reply to Ming Lei

block/blk-core.c int blk_queue_enter(struct request_queue *q, bool no=

Quoted 5 lines · Ming Lei
block/blk-core.c +0 -0
index ad388d5e309a..5e8963bc98d9 100644
@@ -669,6 +669,15 @@ int blk_queue_enter(struct request_queue *q, bool no=

wait)

Quoted 5 lines
block/blk-core.c +0 -0
 		if (nowait)
 			return -EBUSY;

=20

block/blk-core.c +2 -0
+		/*
+		 * read pair of barrier in blk_mq_freeze_queue_start(),

block/blk-core.c int blk_queue_enter(struct request_queue *q, bool nowait) · line 683

Quoted 10 lines
block/blk-core.c +7 -0
+		 * we need to order reading __PERCPU_REF_DEAD flag of
+		 * .q_usage_counter and reading .mq_freeze_depth,
+		 * otherwise the following wait may never return if the
+		 * two reads are reordered.
+		 */
+		smp_rmb();
+
 		ret =3D wait_event_interruptible(q->mq_freeze_wq,
 				!atomic_read(&q->mq_freeze_depth) ||
 				blk_queue_dying(q));

Since patch 4/4 modifies the comment introduced by this patch, I would have preferred that patches 2/4 and 4/4 would have been combined into a single patch. Anyway:

Reviewed-by on v3 4/4
#10 lore raw

Re: [PATCH v3 4/4] block: block new I/O just after queue is set as dying

In reply to Ming Lei

block/blk-core.c int blk_queue_enter(struct request_queue *q, bool now=

Quoted 40 lines · Ming Lei

Before commit 780db2071a(blk-mq: decouble blk-mq freezing from generic bypassing), the dying flag is checked before entering queue, and Tejun converts the checking into .mq_freeze_depth, and assumes the counter is increased just after dying flag is set. Unfortunately we doesn't do that in blk_set_queue_dying(). =20
This patch calls blk_freeze_queue_start() in blk_set_queue_dying(), so that we can block new I/O coming once the queue is set as dying. =20
Given blk_set_queue_dying() is always called in remove path of block device, and queue will be cleaned up later, we don't need to worry about undoing the counter.
=20

---
 block/blk-core.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

=20

block/blk-core.c +0 -0
index 60f364e1d36b..e22c4ea002ec 100644
@@ -500,6 +500,13 @@ void blk_set_queue_dying(struct request_queue *q)
 	queue_flag_set(QUEUE_FLAG_DYING, q);
 	spin_unlock_irq(q->queue_lock);

=20

block/blk-core.c +6 -0
+	/*
+	 * When queue DYING flag is set, we need to block new req
+	 * entering queue, so we call blk_freeze_queue_start() to
+	 * prevent I/O from crossing blk_queue_enter().
+	 */
+	blk_freeze_queue_start(q);
	if (q->mq_ops)
block/blk-core.c +0 -0
 		blk_mq_wake_waiters(q);
 	else {
@@ -672,9 +679,9 @@ int blk_queue_enter(struct request_queue *q, bool now=

ait)

Quoted 11 lines
block/blk-core.c +3 -3
 		/*
 		 * read pair of barrier in blk_freeze_queue_start(),
 		 * we need to order reading __PERCPU_REF_DEAD flag of
-		 * .q_usage_counter and reading .mq_freeze_depth,
-		 * otherwise the following wait may never return if the
-		 * two reads are reordered.
+		 * .q_usage_counter and reading .mq_freeze_depth or
+		 * queue dying flag, otherwise the following wait may
+		 * never return if the two reads are reordered.
 		 */
 		smp_rmb();
Quoted 1 line

=20

An explanation of why that crossing can happen is still missing above the blk_freeze_queue_start() call. Additionally, I'm still wondering whether or not we need "Cc: stable" tags for the patches in this series. But since the code looks fine:

Quoted 1 line

=

author
#11 lore raw
In reply to Ming Lei
Quoted 4 lines · Ming Lei

Hi,

The 1st patch add comments on blk-mq races with timeout handler.

Quoted 8 lines

The other 3 patches improves handling for dying queue:
- the 2nd one adds one barrier in blk_queue_enter() for avoiding hanging caused by out-of-order
- the 3rd and 4th patches block new I/O entering queue after queue is set as dying

V3:
- tweak comments as suggested by Bart Van Assche

Hi Jens,

This patchset has been posted several times without real code change for a while, could you consider it for v4.12?

Thanks,
Ming

maintainer
#12 lore raw
Quoted 11 lines · Ming Lei
Quoted 10 lines · Ming Lei

Hi,

The 1st patch add comments on blk-mq races with timeout handler.

The other 3 patches improves handling for dying queue:
- the 2nd one adds one barrier in blk_queue_enter() for avoiding hanging caused by out-of-order
- the 3rd and 4th patches block new I/O entering queue after queue is set as dying

Quoted 7 lines
Quoted 2 lines

V3:
- tweak comments as suggested by Bart Van Assche

Hi Jens,

This patchset has been posted several times without real code change for a while, could you consider it for v4.12?

We can queue it up now. I do agree that patch 2+4 should have been folded into one, but that's not a huge deal.

Signature · 1 line
Jens Axboe