Skip to contentSkip to conversation

blk-mq: Fix blk_execute_rq_nowait() handling of dying queues

PATCH In review Bart Van Assche 2 participants 4 messages Apr 11, 2017 – Apr 13, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blk-mq: Fix blk_execute_rq_nowait() handling of dying queues

Although blk_execute_rq_nowait() asks blk_mq_sched_insert_request() to run the queue, the function that should run the queue (__blk_mq_delay_run_hw_queue()) skips hardware queues for which .tags == NULL. Since blk_mq_free_tag_set() clears .tags this means if blk_execute_rq_nowait() is called after the tag set has been freed that the request that has been queued will never be executed. In my tests I noticed that every now and then an SG_IO request that got queued by multipathd on a dm device did not get executed. This resulted in either a memory leak complaint about the SG_IO code or the dm device becoming unremovable with e.g. the following state:

$ grep busy= /sys/kernel/debug/block/dm*/mq/*
/sys/kernel/debug/block/dm-0/mq/state:SAME_COMP STACKABLE IO_STAT INIT_DONE POLL REGISTERED, pg_init_in_progress=0, nr_valid_paths=4, flags= RETAIN_ATTACHED_HW_HANDLER, paths: [0:0] active=1 busy=0 dying dead [1:0] active=1 busy=0 dying dead [2:0] active=1 busy=0 dying dead [3:0] active=1 busy=0 dying dead
$ multipath -ll
mpathu (3600140572616d6469736b32000000000) dm-0 ##,##
size=984M features='3 retain_attached_hw_handler queue_mode mq' hwhandler='1 alua' wp=rw
|-+- policy='service-time 0' prio=0 status=active
|-+- policy='service-time 0' prio=0 status=undef
|-+- policy='service-time 0' prio=0 status=undef
`-+- policy='service-time 0' prio=0 status=undef

Avoid that blk_execute_rq_nowait() is called to queue a request onto a dying queue by changing the blk_freeze_queue_start() call in blk_set_queue_dying() into a blk_freeze_queue() call.

---
 block/blk-core.c | 9 +++++----
 block/blk-exec.c | 7 +++++--
 2 files changed, 10 insertions(+), 6 deletions(-)
block/blk-core.c +5 -4
index 8654aa0cef6d..21314b995887 100644
@@ -501,11 +501,12 @@ void blk_set_queue_dying(struct request_queue *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().
+	 * When queue DYING flag is set, we need to block new requests
+	 * from being queued. Hence call blk_freeze_queue() to make
+	 * new blk_queue_enter() calls fail and to wait until all pending
+	 * I/O has finished.
 	 */
-	blk_freeze_queue_start(q);
+	blk_freeze_queue(q);
 
 	if (q->mq_ops)
 		blk_mq_wake_waiters(q);
block/blk-exec.c +5 -2
index 8cd0e9bc8dc8..f7d9bed2cb15 100644
@@ -57,10 +57,13 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
 	rq->end_io = done;
 
 	/*
-	 * don't check dying flag for MQ because the request won't
-	 * be reused after dying flag is set
+	 * The blk_freeze_queue() call in blk_set_queue_dying() and the
+	 * test of the "dying" flag in blk_queue_enter() guarantee that
+	 * blk_execute_rq_nowait() won't be called anymore after the "dying"
+	 * flag has been set.
 	 */
 	if (q->mq_ops) {
+		WARN_ON_ONCE(blk_queue_dying(q));
 		blk_mq_sched_insert_request(rq, at_head, true, false, false);
 		return;
 	}
Signature · 1 line
2.12.2
#1 lore raw
Quoted 5 lines · Bart Van Assche

Although blk_execute_rq_nowait() asks blk_mq_sched_insert_request() to run the queue, the function that should run the queue (__blk_mq_delay_run_hw_queue()) skips hardware queues for which .tags =3D=3D NULL. Since blk_mq_free_tag_set() clears .tags this means if blk_execute_rq_nowait() is called after the tag set has been

Just wondering how that can happen, because we usually call blk_mq_free_tag_set()
after blk_cleanup_queue() is completed.

Quoted 8 lines

freed that the request that has been queued will never be executed. In my tests I noticed that every now and then an SG_IO request that got queued by multipathd on a dm device did not get executed. This resulted in either a memory leak complaint about the SG_IO code or the dm device becoming unremovable with e.g. the following state:

$ grep busy=3D /sys/kernel/debug/block/dm*/mq/*
/sys/kernel/debug/block/dm-0/mq/state:SAME_COMP STACKABLE IO_STAT INIT_DO=

NE POLL REGISTERED, pg_init_in_progress=3D0, nr_valid_paths=3D4, flags=3D R= ETAIN_ATTACHED_HW_HANDLER, paths: [0:0] active=3D1 busy=3D0 dying dead [1:0= ] active=3D1 busy=3D0 dying dead [2:0] active=3D1 busy=3D0 dying dead [3:0]=
active=3D1 busy=3D0 dying dead

Quoted 3 lines
$ multipath -ll
mpathu (3600140572616d6469736b32000000000) dm-0 ##,##
size=3D984M features=3D'3 retain_attached_hw_handler queue_mode mq' hwhan=

dler=3D'1 alua' wp=3Drw

Quoted 8 lines
|-+- policy=3D'service-time 0' prio=3D0 status=3Dactive
|-+- policy=3D'service-time 0' prio=3D0 status=3Dundef
|-+- policy=3D'service-time 0' prio=3D0 status=3Dundef

`-+- policy=3D'service-time 0' prio=3D0 status=3Dundef

Avoid that blk_execute_rq_nowait() is called to queue a request onto a dying queue by changing the blk_freeze_queue_start() call in blk_set_queue_dying() into a blk_freeze_queue() call.

blk_mq_freeze_queue_wait() is only for waiting for completion of pending IO= , so
could you explain it a bit why _wait() is required?

In this case, either blk_freeze_queue_start() or blk_freeze_queue() can't prevent the rq coming into queue, because we only hold/check q_usage_counte= r
before allocating a request, but blk_execute_rq_nowait() has got the reques= t
already.

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q, s=

Quoted 36 lines
---
 block/blk-core.c | 9 +++++----
 block/blk-exec.c | 7 +++++--
 2 files changed, 10 insertions(+), 6 deletions(-)
block/blk-core.c +5 -4
index 8654aa0cef6d..21314b995887 100644
@@ -501,11 +501,12 @@ void blk_set_queue_dying(struct request_queue *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().
+        * When queue DYING flag is set, we need to block new requests
+        * from being queued. Hence call blk_freeze_queue() to make
+        * new blk_queue_enter() calls fail and to wait until all pending
+        * I/O has finished.
         */
-       blk_freeze_queue_start(q);
+       blk_freeze_queue(q);
 
        if (q->mq_ops)
                blk_mq_wake_waiters(q);
block/blk-exec.c +0 -0
index 8cd0e9bc8dc8..f7d9bed2cb15 100644
@@ -57,10 +57,13 @@ void blk_execute_rq_nowait(struct request_queue *q, s=

truct gendisk *bd_disk,

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,

Quoted 8 lines
block/blk-exec.c +3 -2
        rq->end_io =3D done;
 
        /*
-        * don't check dying flag for MQ because the request won't
-        * be reused after dying flag is set
+        * The blk_freeze_queue() call in blk_set_queue_dying() and the
+        * test of the "dying" flag in blk_queue_enter() guarantee that
+        * blk_execute_rq_nowait() won't be called anymore after the "dyi=

ng"

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk, · line 63

Quoted 1 line
block/blk-exec.c +1 -0
+        * flag has been set.

That never be guaranteed, see the following case:

1) blk_get_request() is called just before queue is set as dying in another= path

2) the request is allocated successfully and passed to blk_execute_rq_nowait() even
though queue has been set as dying

Thanks,
Ming Lei

#2 lore raw
Quoted 8 lines · Ming Lei

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q,=

Quoted 6 lines · Bart Van Assche

=20

block/blk-exec.c +0 -0
index 8cd0e9bc8dc8..f7d9bed2cb15 100644
@@ -57,10 +57,13 @@ void blk_execute_rq_nowait(struct request_queue *q,=

struct gendisk *bd_disk,

Quoted 8 lines

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,

Quoted 8 lines
block/blk-exec.c +0 -0
        rq->end_io =3D done;

=20

        /*
block/blk-exec.c +3 -2
-        * don't check dying flag for MQ because the request won't
-        * be reused after dying flag is set
+        * The blk_freeze_queue() call in blk_set_queue_dying() and the
+        * test of the "dying" flag in blk_queue_enter() guarantee that
+        * blk_execute_rq_nowait() won't be called anymore after the "d=

ying"

Quoted 5 lines
Quoted 1 line

+ * flag has been set.

=20
That never be guaranteed, see the following case:
=20
1) blk_get_request() is called just before queue is set as dying in anoth=

er path

Quoted 4 lines

=20
2) the request is allocated successfully and passed to blk_execute_rq_nowait() even
though queue has been set as dying

Hello Ming,

Shouldn't the blk-mq code guarantee that blk_execute_rq_nowait() won't be called anymore after the "dying" flag has been set? I think changing the blk_freeze_queue_start() call into blk_freeze_queue() in blk_set_queue_dyin= g()
is sufficient to achieve this.

Note: after I had posted this patch I have been able to reproduce the issue described in the patch description. Although I still think we need the patc= h
at the start of this e-mail thread, it doesn't fix the issue I described.

Bart.=

#3 lore raw
Quoted 27 lines · Bart Van Assche
Quoted 20 lines · Ming Lei
Quoted 6 lines · Bart Van Assche
block/blk-exec.c +0 -0
index 8cd0e9bc8dc8..f7d9bed2cb15 100644

block/blk-exec.c void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk, · line 63

Quoted 9 lines
block/blk-exec.c +4 -2
@@ -57,10 +57,13 @@ void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
        rq->end_io = done;
 
        /*
-        * don't check dying flag for MQ because the request won't
-        * be reused after dying flag is set
+        * The blk_freeze_queue() call in blk_set_queue_dying() and the
+        * test of the "dying" flag in blk_queue_enter() guarantee that
+        * blk_execute_rq_nowait() won't be called anymore after the "dying"
+        * flag has been set.

That never be guaranteed, see the following case:

Quoted 5 lines

1) blk_get_request() is called just before queue is set as dying in another path

2) the request is allocated successfully and passed to blk_execute_rq_nowait() even
though queue has been set as dying

Quoted 6 lines

Hello Ming,

Shouldn't the blk-mq code guarantee that blk_execute_rq_nowait() won't be called anymore after the "dying" flag has been set? I think changing the blk_freeze_queue_start() call into blk_freeze_queue() in blk_set_queue_dying() is sufficient to achieve this.

I have explained that this change isn't enough.

Quoted 4 lines

Note: after I had posted this patch I have been able to reproduce the issue described in the patch description. Although I still think we need the patch at the start of this e-mail thread, it doesn't fix the issue I described.

Since it fixes nothing, I don't suggest to do that.

Thanks,
Ming Lei