Skip to contentSkip to conversation

blk-mq: Rework blk-mq timeout handling again

PATCH Superseded Bart Van Assche 5 participants 22 messages May 22, 2018 – May 23, 2018 Block layer

v13 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

PATCH v13 1/1 blk-mq: Rework blk-mq timeout handling again

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

Fix this race as follows:
- Reduce the gstate field from 64 to 32 bits such that cmpxchg() can be used to update it. Introduce deadline_seq for updating the deadline on 32-bit systems.
- Remove the request member variables that became superfluous due to this change, namely gstate_seq and aborted_gstate_sync.
- Remove the request state information that became superfluous due to this patch, namely RQF_MQ_TIMEOUT_EXPIRED.
- Remove the code that became superfluous due to this change, namely the RCU lock and unlock statements in blk_mq_complete_request() and also the synchronize_rcu() call in the timeout handler.

Notes:
- Atomic instructions are only used to update the request state if a concurrent request state change could be in progress.
- blk_add_timer() has been split into two functions - one for the legacy block layer and one for blk-mq.

Cc Tejun Heo <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Keith Busch <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Ming Lei <[email protected]>
Cc Sebastian Ott <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Israel Rukshin <[email protected]>,
Cc Max Gurtovoy <[email protected]>

---

Changes compared to v12:
- Switched from cmpxchg64() to cmpxchg(). This became possible because the deadline is now updated before the request state.
- Introduced a new request state to ensure that completions that occur while the timeout function is in progress are not lost.
- Left out the ARM cmpxchg64() patch.

Changes compared to v11:
- Reworked patch 1/2: instead of introducing CONFIG_ARCH_HAVE_CMPXCHG64, make sure that cmpxchg64() is only defined if it can be used.

Changes compared to v10:
- In patch 1/2, added "default y if 64BIT" to the "config ARCH_HAVE_CMPXCHG64" entry in arch/Kconfig. Left out the "select ARCH_HAVE_CMPXCHG64" statements that became superfluous due to this change (alpha, arm64, powerpc and s390).
- Also in patch 1/2, only select ARCH_HAVE_CMPXCHG64 if X86_CMPXCHG64 has been selected.
- In patch 2/2, moved blk_mq_change_rq_state() from blk-mq.h to blk-mq.c.
- Added a comment header above __blk_mq_requeue_request() and blk_mq_requeue_request().
- Documented the MQ_RQ_* state transitions in block/blk-mq.h.
- Left out the fourth argument of blk_mq_rq_set_deadline().

Changes compared to v9:
- Addressed multiple comments related to patch 1/2: added CONFIG_ARCH_HAVE_CMPXCHG64 for riscv, modified features/locking/cmpxchg64/arch-support.txt as requested and made the order of the symbols in the arch/*/Kconfig alphabetical where possible.

Changes compared to v8:
- Split into two patches.
- Moved the spin_lock_init() call from blk_mq_rq_ctx_init() into blk_mq_init_request().
- Fixed the deadline set by blk_add_timer().
- Surrounded the das_lock member with #ifndef CONFIG_ARCH_HAVE_CMPXCHG64 / #endif.

Changes compared to v7:
- Fixed the generation number mechanism. Note: with this patch applied the behavior of the block layer does not depend on the generation number.
- Added more 32-bit architectures to the list of architectures on which cmpxchg64() should not be used.

Changes compared to v6:
- Used a union instead of bit manipulations to store multiple values into a single 64-bit field.
- Reduced the size of the timeout field from 64 to 32 bits.
- Made sure that the block layer still builds with this patch applied for the sh and mips architectures.
- Fixed two sparse warnings that were introduced by this patch in the WRITE_ONCE() calls.

Changes compared to v5:
- Restored the synchronize_rcu() call between marking a request for timeout handling and the actual timeout handling to avoid that timeout handling starts while .queue_rq() is still in progress if the timeout is very short.
- Only use cmpxchg() if another context could attempt to change the request state concurrently. Use WRITE_ONCE() otherwise.

Changes compared to v4:
- Addressed multiple review comments from Christoph. The most important are that atomic_long_cmpxchg() has been changed into cmpxchg() and also that there is now a nice and clean split between the legacy and blk-mq versions of blk_add_timer().
- Changed the patch name and modified the patch description because there is disagreement about whether or not the v4.16 blk-mq core can complete a single request twice. Kept the "Cc: stable" tag because of
https://bugzilla.kernel.org/show_bug.cgi?id=199077.

Changes compared to v3 (see also https://www.mail-archive.com/[email protected]/msg20073.html):
- Removed the spinlock again that was introduced to protect the request state.
v4 uses atomic_long_cmpxchg() instead.
- Split __deadline into two variables - one for the legacy block layer and one
for blk-mq.

Changes compared to v2 (https://www.mail-archive.com/[email protected]/msg18338.html):
- Rebased and retested on top of kernel v4.16.

Changes compared to v1 (https://www.mail-archive.com/[email protected]/msg18089.html):
- Removed the gstate and aborted_gstate members of struct request and used
the __deadline member to encode both the generation and state information.

 block/blk-core.c       |   9 +-
 block/blk-mq-debugfs.c |   4 +-
 block/blk-mq.c         | 250 ++++++++++++++++++++++++++-----------------------
 block/blk-mq.h         |  54 +++++------
 block/blk-timeout.c    | 107 +++++++++++++--------
 block/blk.h            |   1 +
 include/linux/blkdev.h |  44 ++++-----
 7 files changed, 250 insertions(+), 219 deletions(-)
block/blk-core.c +3 -6
index 341501c5e239..42b055292cdc 100644
@@ -198,12 +198,9 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
 	rq->internal_tag = -1;
 	rq->start_time_ns = ktime_get_ns();
 	rq->part = NULL;
-	seqcount_init(&rq->gstate_seq);
-	u64_stats_init(&rq->aborted_gstate_sync);
-	/*
-	 * See comment of blk_mq_init_request
-	 */
-	WRITE_ONCE(rq->gstate, MQ_RQ_GEN_INC);
+#ifndef CONFIG_64BIT
+	seqcount_init(&rq->deadline_seq);
+#endif
 }
 EXPORT_SYMBOL(blk_rq_init);
 
block/blk-mq-debugfs.c +2 -2
index 3080e18cb859..9c539ab2c0dc 100644
@@ -344,15 +344,15 @@ static const char *const rqf_name[] = {
 	RQF_NAME(STATS),
 	RQF_NAME(SPECIAL_PAYLOAD),
 	RQF_NAME(ZONE_WRITE_LOCKED),
-	RQF_NAME(MQ_TIMEOUT_EXPIRED),
 	RQF_NAME(MQ_POLL_SLEPT),
 };
 #undef RQF_NAME
 
 static const char *const blk_mq_rq_state_name_array[] = {
 	[MQ_RQ_IDLE]		= "idle",
-	[MQ_RQ_IN_FLIGHT]	= "in_flight",
+	[MQ_RQ_IN_FLIGHT]	= "in flight",
 	[MQ_RQ_COMPLETE]	= "complete",
+	[MQ_RQ_TIMED_OUT]	= "timed out",
 };
 
 static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
block/blk-mq.c +132 -118

383 lines not on this page · raw message

block/blk-mq.h +23 -31

84 lines not on this page · raw message

block/blk-timeout.c +66 -41

140 lines not on this page · raw message

block/blk.h +1 -0
index eaf1a8e87d11..ffd44cbf3095 100644
@@ -170,6 +170,7 @@ static inline bool bio_integrity_endio(struct bio *bio)
 void blk_timeout_work(struct work_struct *work);
 unsigned long blk_rq_timeout(unsigned long timeout);
 void blk_add_timer(struct request *req);
+void blk_mq_add_timer(struct request *req);
 void blk_delete_timer(struct request *);
 
 
include/linux/blkdev.h +23 -21

79 lines not on this page · raw message

Signature · 1 line
2.16.3
maintainer
#1 lore raw
Quoted 8 lines · Bart Van Assche

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

I'll take a look at this again, getting rid of cmpxchg64 makes me much more comfortable.

Signature · 1 line
Jens Axboe
maintainer
#2 lore raw
Quoted 11 lines · Jens Axboe
Quoted 8 lines · Bart Van Assche

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

Quoted 2 lines

I'll take a look at this again, getting rid of cmpxchg64 makes me much more comfortable.

FWIW, a quick pass on runtime testing works fine. As expected, it's more efficient than what's currently in the kernel, testing with both null_blk (1 and nr_cpus worth of queues), and nvme as well. A huge win is that we shrink the request size from 360 bytes to 312, and I did a small followup patch that brings that to 304. That's a 15% reduction, massive.

Signature · 1 line
Jens Axboe
maintainer
#3 lore raw
Quoted 15 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 8 lines · Bart Van Assche

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

Quoted 2 lines

I'll take a look at this again, getting rid of cmpxchg64 makes me much more comfortable.

Quoted 6 lines

FWIW, a quick pass on runtime testing works fine. As expected, it's more efficient than what's currently in the kernel, testing with both null_blk (1 and nr_cpus worth of queues), and nvme as well. A huge win is that we shrink the request size from 360 bytes to 312, and I did a small followup patch that brings that to 304. That's a 15% reduction, massive.

Ran into this, running block/014 from blktests:

[ 5744.949839] run blktests block/014 at 2018-05-22 12:41:25
[ 5750.723000] null: rq 00000000ff68f103 timed out
[ 5750.728181] WARNING: CPU: 45 PID: 2480 at block/blk-mq.c:585 __blk_mq_complete_request+0xa6/0x0
[ 5750.738187] Modules linked in: null_blk(+) configfs nvme nvme_core sb_edac x86_pkg_temp_therma]
[ 5750.765509] CPU: 45 PID: 2480 Comm: kworker/45:1H Not tainted 4.17.0-rc6+ #712
[ 5750.774087] Hardware name: Dell Inc. PowerEdge T630/0NT78X, BIOS 2.3.4 11/09/2016
[ 5750.783369] Workqueue: kblockd blk_mq_timeout_work
[ 5750.789223] RIP: 0010:__blk_mq_complete_request+0xa6/0x110
[ 5750.795850] RSP: 0018:ffff883ffb417d68 EFLAGS: 00010202
[ 5750.802187] RAX: 0000000000000003 RBX: ffff881ff100d800 RCX: 0000000000000000
[ 5750.810649] RDX: ffff88407fd9e040 RSI: ffff88407fd956b8 RDI: ffff881ff100d800
[ 5750.819119] RBP: ffffe8ffffd91800 R08: 0000000000000000 R09: ffffffff82066eb8
[ 5750.827588] R10: ffff883ffa386138 R11: ffff883ffa385900 R12: 0000000000000001
[ 5750.836050] R13: ffff881fe7da6000 R14: 0000000000000020 R15: 0000000000000002
[ 5750.844529] FS:  0000000000000000(0000) GS:ffff88407fd80000(0000) knlGS:0000000000000000
[ 5750.854482] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5750.861397] CR2: 00007ffc92f97f68 CR3: 000000000201d005 CR4: 00000000003606e0
[ 5750.869861] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5750.878333] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5750.886805] Call Trace:
[ 5750.890033]  bt_iter+0x42/0x50
[ 5750.894000]  blk_mq_queue_tag_busy_iter+0x12b/0x220
[ 5750.899941]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.904913]  ? __rcu_read_unlock+0x50/0x50
[ 5750.909978]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.914948]  blk_mq_timeout_work+0x14b/0x240
[ 5750.920220]  process_one_work+0x21b/0x510
[ 5750.925197]  worker_thread+0x3a/0x390
[ 5750.929781]  ? process_one_work+0x510/0x510
[ 5750.934944]  kthread+0x11c/0x140
[ 5750.939028]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 5750.945169]  ret_from_fork+0x1f/0x30
[ 5750.949656] Code: 48 02 00 00 80 e6 80 74 29 8b 95 80 00 00 00 44 39 e2 75 3b 48 89 df ff 90 2 
[ 5750.972139] ---[ end trace 40065cb1764bf500 ]---

which is this:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE);                    

hitting when blk_mq_terminate_expired() completes the request through BLK_EH_HANDLED.

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
Quoted 60 lines · Jens Axboe
Quoted 15 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 8 lines · Bart Van Assche

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

Quoted 2 lines

I'll take a look at this again, getting rid of cmpxchg64 makes me much more comfortable.

Quoted 6 lines

FWIW, a quick pass on runtime testing works fine. As expected, it's more efficient than what's currently in the kernel, testing with both null_blk (1 and nr_cpus worth of queues), and nvme as well. A huge win is that we shrink the request size from 360 bytes to 312, and I did a small followup patch that brings that to 304. That's a 15% reduction, massive.

Ran into this, running block/014 from blktests:

[ 5744.949839] run blktests block/014 at 2018-05-22 12:41:25
[ 5750.723000] null: rq 00000000ff68f103 timed out
[ 5750.728181] WARNING: CPU: 45 PID: 2480 at block/blk-mq.c:585 __blk_mq_complete_request+0xa6/0x0
[ 5750.738187] Modules linked in: null_blk(+) configfs nvme nvme_core sb_edac x86_pkg_temp_therma]
[ 5750.765509] CPU: 45 PID: 2480 Comm: kworker/45:1H Not tainted 4.17.0-rc6+ #712
[ 5750.774087] Hardware name: Dell Inc. PowerEdge T630/0NT78X, BIOS 2.3.4 11/09/2016
[ 5750.783369] Workqueue: kblockd blk_mq_timeout_work
[ 5750.789223] RIP: 0010:__blk_mq_complete_request+0xa6/0x110
[ 5750.795850] RSP: 0018:ffff883ffb417d68 EFLAGS: 00010202
[ 5750.802187] RAX: 0000000000000003 RBX: ffff881ff100d800 RCX: 0000000000000000
[ 5750.810649] RDX: ffff88407fd9e040 RSI: ffff88407fd956b8 RDI: ffff881ff100d800
[ 5750.819119] RBP: ffffe8ffffd91800 R08: 0000000000000000 R09: ffffffff82066eb8
[ 5750.827588] R10: ffff883ffa386138 R11: ffff883ffa385900 R12: 0000000000000001
[ 5750.836050] R13: ffff881fe7da6000 R14: 0000000000000020 R15: 0000000000000002
[ 5750.844529] FS:  0000000000000000(0000) GS:ffff88407fd80000(0000) knlGS:0000000000000000
[ 5750.854482] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5750.861397] CR2: 00007ffc92f97f68 CR3: 000000000201d005 CR4: 00000000003606e0
[ 5750.869861] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5750.878333] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5750.886805] Call Trace:
[ 5750.890033]  bt_iter+0x42/0x50
[ 5750.894000]  blk_mq_queue_tag_busy_iter+0x12b/0x220
[ 5750.899941]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.904913]  ? __rcu_read_unlock+0x50/0x50
[ 5750.909978]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.914948]  blk_mq_timeout_work+0x14b/0x240
[ 5750.920220]  process_one_work+0x21b/0x510
[ 5750.925197]  worker_thread+0x3a/0x390
[ 5750.929781]  ? process_one_work+0x510/0x510
[ 5750.934944]  kthread+0x11c/0x140
[ 5750.939028]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 5750.945169]  ret_from_fork+0x1f/0x30
[ 5750.949656] Code: 48 02 00 00 80 e6 80 74 29 8b 95 80 00 00 00 44 39 e2 75 3b 48 89 df ff 90 2 
[ 5750.972139] ---[ end trace 40065cb1764bf500 ]---
Quoted 3 lines

which is this:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE);

That check looks wrong, since TIMED_OUT -> COMPLETE is also a valid state transition. So that check should be:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE &&
                     blk_mq_rq_state(rq) != MQ_RQ_TIMED_OUT);
Signature · 1 line
Jens Axboe
maintainer
#5 lore raw
Quoted 63 lines · Jens Axboe
Quoted 62 lines · Jens Axboe
Quoted 15 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 8 lines · Bart Van Assche

Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has been called.

Quoted 2 lines

I'll take a look at this again, getting rid of cmpxchg64 makes me much more comfortable.

Quoted 6 lines

FWIW, a quick pass on runtime testing works fine. As expected, it's more efficient than what's currently in the kernel, testing with both null_blk (1 and nr_cpus worth of queues), and nvme as well. A huge win is that we shrink the request size from 360 bytes to 312, and I did a small followup patch that brings that to 304. That's a 15% reduction, massive.

Ran into this, running block/014 from blktests:

[ 5744.949839] run blktests block/014 at 2018-05-22 12:41:25
[ 5750.723000] null: rq 00000000ff68f103 timed out
[ 5750.728181] WARNING: CPU: 45 PID: 2480 at block/blk-mq.c:585 __blk_mq_complete_request+0xa6/0x0
[ 5750.738187] Modules linked in: null_blk(+) configfs nvme nvme_core sb_edac x86_pkg_temp_therma]
[ 5750.765509] CPU: 45 PID: 2480 Comm: kworker/45:1H Not tainted 4.17.0-rc6+ #712
[ 5750.774087] Hardware name: Dell Inc. PowerEdge T630/0NT78X, BIOS 2.3.4 11/09/2016
[ 5750.783369] Workqueue: kblockd blk_mq_timeout_work
[ 5750.789223] RIP: 0010:__blk_mq_complete_request+0xa6/0x110
[ 5750.795850] RSP: 0018:ffff883ffb417d68 EFLAGS: 00010202
[ 5750.802187] RAX: 0000000000000003 RBX: ffff881ff100d800 RCX: 0000000000000000
[ 5750.810649] RDX: ffff88407fd9e040 RSI: ffff88407fd956b8 RDI: ffff881ff100d800
[ 5750.819119] RBP: ffffe8ffffd91800 R08: 0000000000000000 R09: ffffffff82066eb8
[ 5750.827588] R10: ffff883ffa386138 R11: ffff883ffa385900 R12: 0000000000000001
[ 5750.836050] R13: ffff881fe7da6000 R14: 0000000000000020 R15: 0000000000000002
[ 5750.844529] FS:  0000000000000000(0000) GS:ffff88407fd80000(0000) knlGS:0000000000000000
[ 5750.854482] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5750.861397] CR2: 00007ffc92f97f68 CR3: 000000000201d005 CR4: 00000000003606e0
[ 5750.869861] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5750.878333] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5750.886805] Call Trace:
[ 5750.890033]  bt_iter+0x42/0x50
[ 5750.894000]  blk_mq_queue_tag_busy_iter+0x12b/0x220
[ 5750.899941]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.904913]  ? __rcu_read_unlock+0x50/0x50
[ 5750.909978]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.914948]  blk_mq_timeout_work+0x14b/0x240
[ 5750.920220]  process_one_work+0x21b/0x510
[ 5750.925197]  worker_thread+0x3a/0x390
[ 5750.929781]  ? process_one_work+0x510/0x510
[ 5750.934944]  kthread+0x11c/0x140
[ 5750.939028]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 5750.945169]  ret_from_fork+0x1f/0x30
[ 5750.949656] Code: 48 02 00 00 80 e6 80 74 29 8b 95 80 00 00 00 44 39 e2 75 3b 48 89 df ff 90 2 
[ 5750.972139] ---[ end trace 40065cb1764bf500 ]---

which is this:

Quoted 7 lines
Quoted 1 line
        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE);

That check looks wrong, since TIMED_OUT -> COMPLETE is also a valid state transition. So that check should be:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE &&
                     blk_mq_rq_state(rq) != MQ_RQ_TIMED_OUT);

I guess it would be cleaner to actually do the transition, in blk_mq_rq_timed_out():

        case BLK_EH_HANDLED:                                                    
                if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,                
                                                MQ_RQ_COMPLETE))                
                        __blk_mq_complete_request(req);                         
                break;        

This works for me.

Signature · 1 line
Jens Axboe
#6 lore raw
Quoted 10 lines · Jens Axboe

I guess it would be cleaner to actually do the transition, in blk_mq_rq_timed_out():

        case BLK_EH_HANDLED:                                                    
                if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,                
                                                MQ_RQ_COMPLETE))                
                        __blk_mq_complete_request(req);                         
                break;        

This works for me.

Works for me as well on manual fault injection tests.

I think this change above goes back to Christoph's point earlier on usage of BLK_EH_HANDLED. Is the driver supposed to return BLK_EH_NOT_HANDLED when the driver actually knows the request has been completed before returning the status?

maintainer
#7 lore raw
Quoted 10 lines · Keith Busch
Quoted 9 lines · Jens Axboe

I guess it would be cleaner to actually do the transition, in blk_mq_rq_timed_out():

        case BLK_EH_HANDLED:                                                    
                if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,                
                                                MQ_RQ_COMPLETE))                
                        __blk_mq_complete_request(req);                         
                break;        
Quoted 8 lines
Quoted 1 line

This works for me.

Works for me as well on manual fault injection tests.

I think this change above goes back to Christoph's point earlier on usage of BLK_EH_HANDLED. Is the driver supposed to return BLK_EH_NOT_HANDLED when the driver actually knows the request has been completed before returning the status?

If the driver knows it's completed, it'd have to return BLK_EH_NOT_HANDLED. Or BLK_EH_HANDLED would work too, since the above state transition would then fail.

Signature · 1 line
Jens Axboe
#10 lore raw
In reply to Jens Axboe

T24gVHVlLCAyMDE4LTA1LTIyIGF0IDEzOjM4IC0wNjAwLCBKZW5zIEF4Ym9lIHdyb3RlOg0KPiBP biA1LzIyLzE4IDE6MDMgUE0sIEplbnMgQXhib2Ugd3JvdGU6DQo+ID4gT24gNS8yMi8xOCAxMjo0 NyBQTSwgSmVucyBBeGJvZSB3cm90ZToNCj4gPiA+IFJhbiBpbnRvIHRoaX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VyIGlzDQpydW5uaW5nIGNhbiBiZSBpZ25vcmVkLg0KDQpUaGFua3MsDQoNCkJhcnQu DQoNCg==

#15 lore raw
In reply to Bart Van Assche
Quoted 10 lines · Bart Van Assche
block/blk-mq.c +5 -1
@@ -848,13 +843,22 @@ static void blk_mq_rq_timed_out(struct request *req, bool reserved)
 	case BLK_EH_RESET_TIMER:
+		blk_mq_add_timer(req);
 		/*
+		 * The loop is for the unlikely case of a race with the
+		 * completion code. There is no need to reset the deadline
+		 * if the transition to the in-flight state fails because
+		 * the deadline only matters in the in-flight state.
 		 */
-		blk_mq_rq_update_aborted_gstate(req, 0);

block/blk-mq.c static void blk_mq_rq_timed_out(struct request *req, bool reserved) · line 859

Quoted 10 lines
block/blk-mq.c +9 -1
-		blk_add_timer(req);
+		while (true) {
+			if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,
+						   MQ_RQ_IN_FLIGHT))
+				break;
+			if (blk_mq_rq_state(req) == MQ_RQ_COMPLETE) {
+				__blk_mq_complete_request(req);
+				break;
+			}
+		}

I'm having some trouble triggering this case where the state is already MQ_RQ_COMPLETE, so I'm just trying to figure this out through inspection.

It looks like the new blk_mq_complete_request() already called __blk_mq_complete_request() when it gets the state to MQ_RQ_COMPLETE, so doing that again completes it a second time.

#16 lore raw

T24gVHVlLCAyMDE4LTA1LTIyIGF0IDE0OjMzIC0wNjAwLCBLZWl0aCBCdXNjaCB3cm90ZToNCj4g T24gVHVlLCBNYXkgMjIsIDIwMTggYXQgMDk6MjU6MTVBTSAtMDcwMCwgQmFydCBWYW4gQXNzY2hl IHdyb3RlOg0KPiA+IEBAIC04NDgsMTMgKzg0MywyMiBAQCBzdGF0aWMgdm9pZCBibGtfbXFfcnFf dGltZWRfb3V0KHN0cnVjdCByZXF1ZXN0ICpyZXEsIGJvb2wgcmVzZXJ2ZWQpDQo+ID4gIAljYXNl IEJMS19FSF9SRVNFVF9USU1FUjoNCj4gPiArCQlibGtfbXFfYWRkX3RpbWVyKHJlc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IA0KPiBJdCBsb29rcyBsaWtlIHRo ZSBuZXcgYmxrX21xX2NvbXBsZXRlX3JlcXVlc3QoKSBhbHJlYWR5IGNhbGxlZA0KPiBfX2Jsa19t cV9jb21wbGV0ZV9yZXF1ZXN0KCkgd2hlbiBpdCBnZXRzIHRoZSBzdGF0ZSB0byBNUV9SUV9DT01Q TEVURSwNCj4gc28gZG9pbmcgdGhhdCBhZ2FpbiBjb21wbGV0ZXMgaXQgYSBzZWNvbmQgdGltZS4N Cg0KSGVsbG8gS2VpdGgsDQoNCkhhdmUgeW91IG5vdGljZWQgdGhhdCBpZiBibGtfbXFfY29tcGxl dGVfcmVxdWVzdCgpIGVuY291bnRlcnMgYSByZXF1ZXN0IHdpdGgNCnN0YXRlIE1RX1JRX1RJTUVE X09VVCB0aGF0IGl0IGRvZXNuJ3QgY2FsbCBfX2Jsa19tcV9jb21wbGV0ZV9yZXF1ZXN0KCk/IEkg dGhpbmsNCnRoZSBjb2RlIGluIGJsa19tcV9jb21wbGV0ZV9yZXF1ZXN0KCkgdG9nZXRoZXIgd2l0 aCB0aGUgYWJvdmUgY29kZSBndWFyYW50ZWVzDQp0aGF0IF9fYmxrX21xX2NvbXBsZXRlX3JlcXVl c3QoKSBpcyBvbmx5IGNhbGxlZCBvbmNlIHBlciByZXF1ZXN0IGdlbmVyYXRpb24uDQoNCkJhcnQu DQoNCg0KDQo=

maintainer
#11 lore raw
In reply to Bart Van Assche
Quoted 79 lines · Bart Van Assche
Quoted 52 lines · Jens Axboe
Quoted 40 lines · Jens Axboe
Quoted 39 lines · Jens Axboe

Ran into this, running block/014 from blktests:

[ 5744.949839] run blktests block/014 at 2018-05-22 12:41:25
[ 5750.723000] null: rq 00000000ff68f103 timed out
[ 5750.728181] WARNING: CPU: 45 PID: 2480 at block/blk-mq.c:585 __blk_mq_complete_request+0xa6/0x0
[ 5750.738187] Modules linked in: null_blk(+) configfs nvme nvme_core sb_edac x86_pkg_temp_therma]
[ 5750.765509] CPU: 45 PID: 2480 Comm: kworker/45:1H Not tainted 4.17.0-rc6+ #712
[ 5750.774087] Hardware name: Dell Inc. PowerEdge T630/0NT78X, BIOS 2.3.4 11/09/2016
[ 5750.783369] Workqueue: kblockd blk_mq_timeout_work
[ 5750.789223] RIP: 0010:__blk_mq_complete_request+0xa6/0x110
[ 5750.795850] RSP: 0018:ffff883ffb417d68 EFLAGS: 00010202
[ 5750.802187] RAX: 0000000000000003 RBX: ffff881ff100d800 RCX: 0000000000000000
[ 5750.810649] RDX: ffff88407fd9e040 RSI: ffff88407fd956b8 RDI: ffff881ff100d800
[ 5750.819119] RBP: ffffe8ffffd91800 R08: 0000000000000000 R09: ffffffff82066eb8
[ 5750.827588] R10: ffff883ffa386138 R11: ffff883ffa385900 R12: 0000000000000001
[ 5750.836050] R13: ffff881fe7da6000 R14: 0000000000000020 R15: 0000000000000002
[ 5750.844529] FS:  0000000000000000(0000) GS:ffff88407fd80000(0000) knlGS:0000000000000000
[ 5750.854482] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5750.861397] CR2: 00007ffc92f97f68 CR3: 000000000201d005 CR4: 00000000003606e0
[ 5750.869861] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5750.878333] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5750.886805] Call Trace:
[ 5750.890033]  bt_iter+0x42/0x50
[ 5750.894000]  blk_mq_queue_tag_busy_iter+0x12b/0x220
[ 5750.899941]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.904913]  ? __rcu_read_unlock+0x50/0x50
[ 5750.909978]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.914948]  blk_mq_timeout_work+0x14b/0x240
[ 5750.920220]  process_one_work+0x21b/0x510
[ 5750.925197]  worker_thread+0x3a/0x390
[ 5750.929781]  ? process_one_work+0x510/0x510
[ 5750.934944]  kthread+0x11c/0x140
[ 5750.939028]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 5750.945169]  ret_from_fork+0x1f/0x30
[ 5750.949656] Code: 48 02 00 00 80 e6 80 74 29 8b 95 80 00 00 00 44 39 e2 75 3b 48 89 df ff 90 2 
[ 5750.972139] ---[ end trace 40065cb1764bf500 ]---

which is this:

Quoted 7 lines
Quoted 1 line
        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE);

That check looks wrong, since TIMED_OUT -> COMPLETE is also a valid state transition. So that check should be:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE &&
                     blk_mq_rq_state(rq) != MQ_RQ_TIMED_OUT);

I guess it would be cleaner to actually do the transition, in blk_mq_rq_timed_out():

Quoted 7 lines
        case BLK_EH_HANDLED:                                                    
                if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,                
                                                MQ_RQ_COMPLETE))                
                        __blk_mq_complete_request(req);                         
                break;        

This works for me.

Hello Jens,

Thanks for having reported this. How about using the following change to suppress that warning:

block/blk-mq.c +1 -0
index bb99c03e7a34..84e55ea55baf 100644
@@ -844,6 +844,7 @@ static void blk_mq_rq_timed_out(struct request *req, bool reserved)
 
 	switch (ret) {
 	case BLK_EH_HANDLED:
+		blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT, MQ_RQ_COMPLETE);
 		__blk_mq_complete_request(req);
 		break;
 	case BLK_EH_RESET_TIMER:
Quoted 3 lines

I think this will work better than what was proposed in your last e-mail. I'm afraid that with that change that a completion that occurs while the timeout handler is running can be ignored.

What if that races with eg requeue? We get the completion from IRQ, decide we need to requeue/restart the request rather than complete it.

Signature · 1 line
Jens Axboe
#17 lore raw
In reply to Bart Van Assche
Quoted 4 lines · Bart Van Assche

Have you noticed that if blk_mq_complete_request() encounters a request with state MQ_RQ_TIMED_OUT that it doesn't call __blk_mq_complete_request()? I think the code in blk_mq_complete_request() together with the above code guarantees that __blk_mq_complete_request() is only called once per request generation.

Right, my mistake. I noticed that when I saw your reply on the EH_HANDLED case, so looks fine.

#18 lore raw
In reply to Bart Van Assche
Quoted 5 lines · Bart Van Assche

Have you noticed that if blk_mq_complete_request() encounters a request with state MQ_RQ_TIMED_OUT that it doesn't call __blk_mq_complete_request()? I think the code in blk_mq_complete_request() together with the above code guarantees that __blk_mq_complete_request() is only called once per request generation.

Okay, now to the BLK_EH_NOT_HANDLED case: that's supposedly the correct status to return if the driver knows blk_mq_complete_request() was called prior to returning from the timeout handler, so we need a similiar check there, right?

#12 lore raw
In reply to Jens Axboe

T24gVHVlLCAyMDE4LTA1LTIyIGF0IDE0OjM4IC0wNjAwLCBKZW5zIEF4Ym9lIHdyb3RlOg0KPiBP biA1LzIyLzE4IDI6MzMgUE0sIEJhcnQgVmFuIEFzc2NoZSB3cm90ZToNCj4gPiBUaGFua3MgZm9y IGhhdmluZyByZXBvcnRlZCB0aGlzLiBIb3cgYWJvdXQgdXNpbmcgdGhlIGZvbGxvd2luZyBjaGFu Z2UgdG8gc3VwcHJlc3MNCj4gPiB0aGF0IHdhcm5pbmc6DQo+ID4gDQo+ID4gZGlmZiAtLWdpdCBh L2Jsb2NrL2Jsay1tcS5jIGIvYmxvY2svYmxrLW1xLmMNCj4gPiBpbmRleCBiYjk5YzAzZTdhMzQu Ljg0ZTU1ZWE1NWJhZiAxMDA2NDQNCj4gPiAtLS0gYS9ibG9jay9ibGstbXEuYw0KPiA+ICsrKyBi L2Jsb2NrL2Jsay1tcS5jDQo+ID4gQEAgLTg0NCw2ICs4NDQsNyBAQCBzdGF0aWMgdm9pZCBibGtf bXFfcnFfdGltZWRfb3V0KHN0cnVjdCByZXF1ZXN0ICpyZXEsIGJvb2wgcmVzZXJ2ZWQpDQo+ID4g IA0KPiA+ICAJc3dpdGNoIChyZXQpIHsNCj4gPiAgCWNhc2UgQkxLX0VIX0hBTkRMRUQ6DQo+ID4g KwkJYmxrX21xX2NoYW5nZV9ycV9zdGF0ZShyZXEsIE1RX1JRX1RJTUVEX09VVCwgTVFfUlFfQ09N UExFVEUpOw0KPiA+ICAJCV9fYmxrX21x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gpIGVuY291bnRlcnMgYSByZXF1ZXN0IHdpdGgNCj4gPiBzdGF0ZSBNUV9SUV9U SU1FRF9PVVQgdGhhdCBpdCBkb2Vzbid0IGNhbGwgX19ibGtfbXFfY29tcGxldGVfcmVxdWVzdCgp PyBJIHRoaW5rDQo+ID4gdGhlIGNvZGUgaW4gYmxrX21xX2NvbXBsZXRlX3JlcXVlc3QoKSB0b2dl dGhlciB3aXRoIHRoZSBhYm92ZSBjb2RlIGd1YXJhbnRlZXMNCj4gPiB0aGF0IF9fYmxrX21xX2Nv bXBsZXRlX3JlcXVlc3QoKSBpcyBvbmx5IGNhbGxlZCBvbmNlIHBlciByZXF1ZXN0IGdlbmVyYXRp b24uDQo+IA0KPiBPa2F5LCBub3cgdG8gdGhlIEJMS19FSF9OT1RfSEFORExFRCBjYXNlOiB0aGF0 J3Mgc3VwcG9zZWRseSB0aGUgY29ycmVjdA0KPiBzdGF0dXMgdG8gcmV0dXJuIGlmIHRoZSBkcml2 ZXIga25vd3MgYmxrX21xX2NvbXBsZXRlX3JlcXVlc3QoKSB3YXMgY2FsbGVkDQo+IHByaW9yIHRv IHJldHVybmluZyBmcm9tIHRoZSB0aW1lb3V0IGhhbmRsZXIsIHNvIHdlIG5lZWQgYSBzaW1pbGlh ciBjaGVjaw0KPiB0aGVyZSwgcmlnaHQ/DQoNCkdvb2QgY2F0Y2guIFRvIG1lIHRoYXQgc2VlbXMg bGlrZSB0aGUgcmlnaHQgcGxhY2UgdG8gaGFuZGxlIHRoYXQgY2FzZS4NCg0KQmFydC4=

#8 lore raw
In reply to Jens Axboe
Quoted 7 lines · Jens Axboe
Quoted 3 lines

of BLK_EH_HANDLED. Is the driver supposed to return BLK_EH_NOT_HANDLED when the driver actually knows the request has been completed before returning the status?

If the driver knows it's completed, it'd have to return BLK_EH_NOT_HANDLED. Or BLK_EH_HANDLED would work too, since the above state transition would then fail.

Btw, I think we should just kill off BLK_EH_HANDLED. WIP totally untested progress toward that is here:

http://git.infradead.org/users/hch/block.git/shortlog/refs/heads/blk-eh-ret

The only real missing bit is SCSI overloading the value for internal use.

maintainer
#9 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Jens Axboe
Quoted 3 lines

of BLK_EH_HANDLED. Is the driver supposed to return BLK_EH_NOT_HANDLED when the driver actually knows the request has been completed before returning the status?

If the driver knows it's completed, it'd have to return BLK_EH_NOT_HANDLED. Or BLK_EH_HANDLED would work too, since the above state transition would then fail.

Quoted 7 lines

Btw, I think we should just kill off BLK_EH_HANDLED. WIP totally untested progress toward that is here:

http://git.infradead.org/users/hch/block.git/shortlog/refs/heads/blk-eh-ret

The only real missing bit is SCSI overloading the value for internal use.

I think that's a great idea, the use cases aren't clear at all, driver writers will get this wrong.

Signature · 1 line
Jens Axboe
maintainer
#13 lore raw
In reply to Bart Van Assche
Quoted 25 lines · Bart Van Assche
Quoted 17 lines · Jens Axboe
Quoted 16 lines · Bart Van Assche

Thanks for having reported this. How about using the following change to suppress that warning:

block/blk-mq.c +1 -0
index bb99c03e7a34..84e55ea55baf 100644
@@ -844,6 +844,7 @@ static void blk_mq_rq_timed_out(struct request *req, bool reserved)
 
 	switch (ret) {
 	case BLK_EH_HANDLED:
+		blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT, MQ_RQ_COMPLETE);
 		__blk_mq_complete_request(req);
 		break;
 	case BLK_EH_RESET_TIMER:
Quoted 6 lines
Quoted 3 lines

I think this will work better than what was proposed in your last e-mail. I'm afraid that with that change that a completion that occurs while the timeout handler is running can be ignored.

What if that races with eg requeue? We get the completion from IRQ, decide we need to requeue/restart the request rather than complete it.

Quoted 2 lines

Shouldn't block drivers that requeue a request from inside their timeout handler return BLK_EH_NOT_HANDLED instead of BLK_EH_HANDLED?

Yeah, I guess they should. See reply to Christoph as well, making this simpler would be a great idea. Could be done as a prep patch (series) to this one.

Signature · 1 line
Jens Axboe
#14 lore raw
In reply to Jens Axboe
Quoted 80 lines · Jens Axboe
Quoted 79 lines · Bart Van Assche
Quoted 52 lines · Jens Axboe
Quoted 40 lines · Jens Axboe
Quoted 39 lines · Jens Axboe

Ran into this, running block/014 from blktests:

[ 5744.949839] run blktests block/014 at 2018-05-22 12:41:25
[ 5750.723000] null: rq 00000000ff68f103 timed out
[ 5750.728181] WARNING: CPU: 45 PID: 2480 at block/blk-mq.c:585 __blk_mq_complete_request+0xa6/0x0
[ 5750.738187] Modules linked in: null_blk(+) configfs nvme nvme_core sb_edac x86_pkg_temp_therma]
[ 5750.765509] CPU: 45 PID: 2480 Comm: kworker/45:1H Not tainted 4.17.0-rc6+ #712
[ 5750.774087] Hardware name: Dell Inc. PowerEdge T630/0NT78X, BIOS 2.3.4 11/09/2016
[ 5750.783369] Workqueue: kblockd blk_mq_timeout_work
[ 5750.789223] RIP: 0010:__blk_mq_complete_request+0xa6/0x110
[ 5750.795850] RSP: 0018:ffff883ffb417d68 EFLAGS: 00010202
[ 5750.802187] RAX: 0000000000000003 RBX: ffff881ff100d800 RCX: 0000000000000000
[ 5750.810649] RDX: ffff88407fd9e040 RSI: ffff88407fd956b8 RDI: ffff881ff100d800
[ 5750.819119] RBP: ffffe8ffffd91800 R08: 0000000000000000 R09: ffffffff82066eb8
[ 5750.827588] R10: ffff883ffa386138 R11: ffff883ffa385900 R12: 0000000000000001
[ 5750.836050] R13: ffff881fe7da6000 R14: 0000000000000020 R15: 0000000000000002
[ 5750.844529] FS:  0000000000000000(0000) GS:ffff88407fd80000(0000) knlGS:0000000000000000
[ 5750.854482] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5750.861397] CR2: 00007ffc92f97f68 CR3: 000000000201d005 CR4: 00000000003606e0
[ 5750.869861] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 5750.878333] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 5750.886805] Call Trace:
[ 5750.890033]  bt_iter+0x42/0x50
[ 5750.894000]  blk_mq_queue_tag_busy_iter+0x12b/0x220
[ 5750.899941]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.904913]  ? __rcu_read_unlock+0x50/0x50
[ 5750.909978]  ? blk_mq_tag_to_rq+0x20/0x20
[ 5750.914948]  blk_mq_timeout_work+0x14b/0x240
[ 5750.920220]  process_one_work+0x21b/0x510
[ 5750.925197]  worker_thread+0x3a/0x390
[ 5750.929781]  ? process_one_work+0x510/0x510
[ 5750.934944]  kthread+0x11c/0x140
[ 5750.939028]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 5750.945169]  ret_from_fork+0x1f/0x30
[ 5750.949656] Code: 48 02 00 00 80 e6 80 74 29 8b 95 80 00 00 00 44 39 e2 75 3b 48 89 df ff 90 2 
[ 5750.972139] ---[ end trace 40065cb1764bf500 ]---

which is this:

Quoted 7 lines
Quoted 1 line
        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE);

That check looks wrong, since TIMED_OUT -> COMPLETE is also a valid state transition. So that check should be:

        WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_COMPLETE &&
                     blk_mq_rq_state(rq) != MQ_RQ_TIMED_OUT);

I guess it would be cleaner to actually do the transition, in blk_mq_rq_timed_out():

Quoted 7 lines
        case BLK_EH_HANDLED:                                                    
                if (blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT,                
                                                MQ_RQ_COMPLETE))                
                        __blk_mq_complete_request(req);                         
                break;        

This works for me.

Hello Jens,

Thanks for having reported this. How about using the following change to suppress that warning:

block/blk-mq.c +1 -0
index bb99c03e7a34..84e55ea55baf 100644
@@ -844,6 +844,7 @@ static void blk_mq_rq_timed_out(struct request *req, bool reserved)
 
 	switch (ret) {
 	case BLK_EH_HANDLED:
+		blk_mq_change_rq_state(req, MQ_RQ_TIMED_OUT, MQ_RQ_COMPLETE);
 		__blk_mq_complete_request(req);
 		break;
 	case BLK_EH_RESET_TIMER:
Quoted 6 lines
Quoted 3 lines

I think this will work better than what was proposed in your last e-mail. I'm afraid that with that change that a completion that occurs while the timeout handler is running can be ignored.

What if that races with eg requeue? We get the completion from IRQ, decide we need to requeue/restart the request rather than complete it.

If drivers need to requeue this request, which should have been done in its .complete.

But if the requeue is done via IRQ handler directly, that may be one existed race between normal completion vs. timeout, and Bart's patch doesn't change situation too.

thanks,
Ming

#20 lore raw
In reply to Bart Van Assche
Quoted 23 lines · Bart Van Assche
block/blk-mq.c +23 -0
+static bool blk_mq_change_rq_state(struct request *rq,
+				   enum mq_rq_state old_state,
+				   enum mq_rq_state new_state)
+{
+	union blk_generation_and_state gstate = READ_ONCE(rq->gstate);
+	union blk_generation_and_state old_val = gstate;
+	union blk_generation_and_state new_val = gstate;
+
+	old_val.state = old_state;
+	new_val.state = new_state;
+	if (new_state == MQ_RQ_IN_FLIGHT)
+		new_val.generation++;
+	/*
+	 * For transitions from state in-flight to another state cmpxchg()
+	 * must be used. For other state transitions it is safe to use
+	 * WRITE_ONCE().
+	 */
+	if (old_state != MQ_RQ_IN_FLIGHT) {
+		WRITE_ONCE(rq->gstate.val, new_val.val);
+		return true;
+	}
+	return blk_mq_set_rq_state(rq, old_val, new_val);
+}

<snip>

Quoted 25 lines

void blk_mq_complete_request(struct request *rq)
{

block/blk-mq.c +1 -17
 	struct request_queue *q = rq->q;
-	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
-	int srcu_idx;
 
 	if (unlikely(blk_should_fake_timeout(q)))
 		return;
 
-	/*
-	 * If @rq->aborted_gstate equals the current instance, timeout is
-	 * claiming @rq and we lost.  This is synchronized through
-	 * hctx_lock().  See blk_mq_timeout_work() for details.
-	 *
-	 * Completion path never blocks and we can directly use RCU here
-	 * instead of hctx_lock() which can be either RCU or SRCU.
-	 * However, that would complicate paths which want to synchronize
-	 * against us.  Let stay in sync with the issue path so that
-	 * hctx_lock() covers both issue and completion paths.
-	 */
-	hctx_lock(hctx, &srcu_idx);
-	if (blk_mq_rq_aborted_gstate(rq) != rq->gstate)
-		__blk_mq_complete_request(rq);
-	hctx_unlock(hctx, srcu_idx);
+	/* The loop is for the unlikely case of a race with the timeout code. */

block/blk-mq.c static u64 blk_mq_rq_aborted_gstate(struct request *rq) · line 655

Quoted 10 lines
block/blk-mq.c +9 -0
+	while (true) {
+		if (blk_mq_change_rq_state(rq, MQ_RQ_IN_FLIGHT,
+					   MQ_RQ_COMPLETE)) {
+			__blk_mq_complete_request(rq);
+			break;
+		}
+		if (blk_mq_change_rq_state(rq, MQ_RQ_TIMED_OUT, MQ_RQ_COMPLETE))
+			break;
+	}
 }

Looks like the cmpxchg is also needed if old_state is MQ_RQ_TIMED_OUT, otherwise its guaranteed to return 'true' and there's no point to the loop and 'if' check.

#21 lore raw
Quoted 3 lines · Keith Busch

Looks like the cmpxchg is also needed if old_state is MQ_RQ_TIMED_OUT, otherwise its guaranteed to return 'true' and there's no point to the loop and 'if' check.

And I see v14 is already posted with that fix! :)