Skip to contentSkip to conversation

blk-mq: avoid to synchronize rcu inside blk_cleanup_queue()

PATCH Superseded Ming Lei 3 participants 7 messages Jun 20, 2018 – Jun 22, 2018 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blk-mq: avoid to synchronize rcu inside blk_cleanup_queue()

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Cc Andrew Jones <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-core.c | 8 ++++++--
 block/blk-mq.c   | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
block/blk-core.c +6 -2
index cf0ee764b908..f0129e20b773 100644
@@ -766,9 +766,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +4 -1
index 70c65bb6c013..63680b243466 100644
@@ -2351,6 +2351,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared = true;
 
 	mutex_lock(&set->tag_list_lock);
 	list_del_rcu(&q->tag_set_list);
@@ -2359,9 +2360,11 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		set->flags &= ~BLK_MQ_F_TAG_SHARED;
 		/* update existing queue */
 		blk_mq_update_tag_set_depth(set, false);
+		shared = true;
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 
Signature · 1 line
2.9.5
Tested-by on v1 1/1
#1 lore raw
Quoted 64 lines · Ming Lei

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Cc Andrew Jones <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-core.c | 8 ++++++--
 block/blk-mq.c   | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
block/blk-core.c +6 -2
index cf0ee764b908..f0129e20b773 100644
@@ -766,9 +766,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +1 -0
index 70c65bb6c013..63680b243466 100644
@@ -2351,6 +2351,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared = true;
 
 	mutex_lock(&set->tag_list_lock);
 	list_del_rcu(&q->tag_set_list);

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2363

Quoted 4 lines
block/blk-mq.c +1 -0
@@ -2359,9 +2360,11 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		set->flags &= ~BLK_MQ_F_TAG_SHARED;
 		/* update existing queue */
 		blk_mq_update_tag_set_depth(set, false);
+		shared = true;

I guess this should be '= false'.

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2370

Quoted 8 lines
block/blk-mq.c +2 -1
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 

With the '= false' change I tested this and it resolves the issue for me.

Thanks,
drew

maintainer
#5 lore raw
In reply to Ming Lei
Quoted 14 lines · Ming Lei

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

Quoted 5 lines

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Looks reasonable to me. But this is something that we've been breaking multiple times over the years, any chance you could add a blktests test for it?

Signature · 1 line
Jens Axboe
maintainer
#2 lore raw
In reply to Andrew Jones
Quoted 72 lines · Andrew Jones
Quoted 64 lines · Ming Lei

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Cc Andrew Jones <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-core.c | 8 ++++++--
 block/blk-mq.c   | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
block/blk-core.c +6 -2
index cf0ee764b908..f0129e20b773 100644
@@ -766,9 +766,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +1 -0
index 70c65bb6c013..63680b243466 100644
@@ -2351,6 +2351,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared = true;
 
 	mutex_lock(&set->tag_list_lock);
 	list_del_rcu(&q->tag_set_list);

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2363

Quoted 4 lines
block/blk-mq.c +1 -0
@@ -2359,9 +2360,11 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		set->flags &= ~BLK_MQ_F_TAG_SHARED;
 		/* update existing queue */
 		blk_mq_update_tag_set_depth(set, false);
+		shared = true;

I guess this should be '= false'.

Quoted 10 lines

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2370

Quoted 8 lines
block/blk-mq.c +2 -1
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 

With the '= false' change I tested this and it resolves the issue for me.

That logic still doesn't look correct to me. Does the below work?

block/blk-core.c +6 -2
index afd2596ea3d3..222d4fc0e524 100644
@@ -762,9 +762,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +6 -1
index 8e57b84e50e9..18ad2b95ff63 100644
@@ -2351,8 +2351,12 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared;
 
 	mutex_lock(&set->tag_list_lock);
+
+	shared = !list_is_singular(&set->tag_list);
+
 	list_del_rcu(&q->tag_set_list);
 	if (list_is_singular(&set->tag_list)) {
 		/* just transitioned to unshared */
@@ -2361,7 +2365,8 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		blk_mq_update_tag_set_depth(set, false);
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 
Signature · 1 line
Jens Axboe
author
#3 lore raw
Quoted 112 lines · Jens Axboe
Quoted 72 lines · Andrew Jones
Quoted 64 lines · Ming Lei

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Cc Andrew Jones <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-core.c | 8 ++++++--
 block/blk-mq.c   | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
block/blk-core.c +6 -2
index cf0ee764b908..f0129e20b773 100644
@@ -766,9 +766,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +1 -0
index 70c65bb6c013..63680b243466 100644
@@ -2351,6 +2351,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared = true;
 
 	mutex_lock(&set->tag_list_lock);
 	list_del_rcu(&q->tag_set_list);

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2363

Quoted 4 lines
block/blk-mq.c +1 -0
@@ -2359,9 +2360,11 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		set->flags &= ~BLK_MQ_F_TAG_SHARED;
 		/* update existing queue */
 		blk_mq_update_tag_set_depth(set, false);
+		shared = true;

I guess this should be '= false'.

Quoted 10 lines

block/blk-mq.c static void blk_mq_del_queue_tag_set(struct request_queue *q) · line 2372

Quoted 8 lines
block/blk-mq.c +2 -1
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 

With the '= false' change I tested this and it resolves the issue for me.

That logic still doesn't look correct to me. Does the below work?

block/blk-core.c +6 -2
index afd2596ea3d3..222d4fc0e524 100644
@@ -762,9 +762,13 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * make sure all in-progress dispatch are completed because
 	 * blk_freeze_queue() can only complete all requests, and
 	 * dispatch may still be in-progress since we dispatch requests
-	 * from more than one contexts
+	 * from more than one contexts.
+	 *
+	 * No need to quiesce queue if it isn't initialized yet since
+	 * blk_freeze_queue() should be enough for cases of passthrough
+	 * request.
 	 */
-	if (q->mq_ops)
+	if (q->mq_ops && blk_queue_init_done(q))
 		blk_mq_quiesce_queue(q);
 
 	/* for synchronous bio-based driver finish in-flight integrity i/o */
block/blk-mq.c +0 -0
index 8e57b84e50e9..18ad2b95ff63 100644

block/blk-mq.c static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, · line 2358

Quoted 8 lines
block/blk-mq.c +3 -0
@@ -2351,8 +2351,12 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared;
 
 	mutex_lock(&set->tag_list_lock);
+
+	shared = !list_is_singular(&set->tag_list);

Or:

	shared = set->flags & BLK_MQ_F_TAG_SHARED;
Quoted 18 lines
block/blk-mq.c +3 -1
+
 	list_del_rcu(&q->tag_set_list);
 	if (list_is_singular(&set->tag_list)) {
 		/* just transitioned to unshared */
@@ -2361,7 +2365,8 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)
 		blk_mq_update_tag_set_depth(set, false);
 	}
 	mutex_unlock(&set->tag_list_lock);
-	synchronize_rcu();
+	if (shared)
+		synchronize_rcu();
 	INIT_LIST_HEAD(&q->tag_set_list);
 }
 
Signature · 2 lines
-- 
Jens Axboe

Will prepare a V2 soon.

Thanks,
Ming

author
#6 lore raw
In reply to Jens Axboe
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Ming Lei

SCSI probing may synchronously create and destroy a lot of request_queues for non-existent devices. Any synchronize_rcu() in queue creation or destroy path may introduce long latency during booting, see detailed description in comment of blk_register_queue().

This patch removes two synchronize_rcu() inside blk_cleanup_queue() for this case:

1) commit c2856ae2f315d75(blk-mq: quiesce queue before freeing queue) need synchronize_rcu() for implementing blk_mq_quiesce_queue(), but when queue isn't initialized, it isn't necessary to do that since only pass-through requests are involved, no original issue in scsi_execute() at all.

2) when only one request queue is attached to tags, no necessary to call synchronize_rcu() too.

Quoted 6 lines
Quoted 2 lines

Without this patch, it may take more 20+ seconds for virtio-scsi to complete disk probe. With this patch, the time becomes less than 100ms.

Looks reasonable to me. But this is something that we've been breaking multiple times over the years, any chance you could add a blktests test for it?

Looks a good idea, I guess it can be triggered on scsi_debug too, will cook a patch later.

thanks,
Ming

maintainer
#4 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei
Quoted 10 lines
block/blk-mq.c +1 -0
index 8e57b84e50e9..18ad2b95ff63 100644
@@ -2351,8 +2351,12 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
 static void blk_mq_del_queue_tag_set(struct request_queue *q)
 {
 	struct blk_mq_tag_set *set = q->tag_set;
+	bool shared;
 
Quoted 6 lines

block/blk-mq.c static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, · line 2358

Quoted 3 lines
block/blk-mq.c +2 -0
 	mutex_lock(&set->tag_list_lock);
+
+	shared = !list_is_singular(&set->tag_list);

Or:

	shared = set->flags & BLK_MQ_F_TAG_SHARED;

That'd work too, yeah.

Signature · 1 line
Jens Axboe