Skip to contentSkip to conversation

blk-mq: export setting request completion state

PATCH Superseded Keith Busch 6 participants 18 messages Jul 19, 2018 – Jul 23, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 blk-mq: export setting request completion state

This is preparing for drivers that want to directly alter the state of their requests. No functional change here.

---
 block/blk-mq.c         | 4 +---
 include/linux/blk-mq.h | 9 +++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
block/blk-mq.c +1 -3
index 22326612a5d3..f50559718b71 100644
@@ -558,10 +558,8 @@ static void __blk_mq_complete_request(struct request *rq)
 	bool shared = false;
 	int cpu;
 
-	if (cmpxchg(&rq->state, MQ_RQ_IN_FLIGHT, MQ_RQ_COMPLETE) !=
-			MQ_RQ_IN_FLIGHT)
+	if (blk_mq_mark_complete(rq))
 		return;
-
 	if (rq->internal_tag != -1)
 		blk_mq_sched_completed_request(rq);
 
include/linux/blk-mq.h +9 -0
index 9b0fd11ce89a..0ae115e95476 100644
@@ -289,6 +289,15 @@ void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
 
 void blk_mq_quiesce_queue_nowait(struct request_queue *q);
 
+/*
+ * Returns true if request was not in flight.
+ */
+static inline bool blk_mq_mark_complete(struct request *rq)
+{
+	return (cmpxchg(&rq->state, MQ_RQ_IN_FLIGHT, MQ_RQ_COMPLETE) !=
+			MQ_RQ_IN_FLIGHT);
+}
+
 /*
  * Driver command data is immediately after the request. So subtract request
  * size to get back to the original request, add request size to get the PDU.
Signature · 1 line
2.14.4
#1 lore raw

PATCH 2/2 scsi: set timed out out mq requests to complete

The scsi block layer requires requests claimed be the error handling be completed by the error handler. A previous commit allowed completions to proceed for blk-mq, breaking that assumption.

This patch prevents completions that may race with the timeout handler by marking the state to complete, restoring the previous behavior.

Fixes 12f5b931 ("blk-mq: Remove generation seqeunce")

---

Tested using Jianchao's abort injection to scsi_debug described here:

https://lore.kernel.org/lkml/[email protected]/

 drivers/scsi/scsi_error.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/scsi_error.c +5 -1
index 8932ae81a15a..86ee10b2c775 100644
@@ -286,6 +286,9 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 	enum blk_eh_timer_return rtn = BLK_EH_DONE;
 	struct Scsi_Host *host = scmd->device->host;
 
+	if (req->q->mq_ops && blk_mq_mark_complete(req))
+		return rtn;
+
 	trace_scsi_dispatch_cmd_timeout(scmd);
 	scsi_log_completion(scmd, TIMEOUT_ERROR);
 
@@ -300,7 +303,8 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_scmd_add(scmd);
 		}
-	}
+	} else if (req->q->mq_ops)
+		WRITE_ONCE(req->state, MQ_RQ_IN_FLIGHT);
 
 	return rtn;
 }
Signature · 1 line
2.14.4
#2 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 3 lines · Keith Busch

The scsi block layer requires requests claimed be the error handling be completed by the error handler. A previous commit allowed completions to proceed for blk-mq, breaking that assumption.

Ahm the first sentence is a bit hard to parse, sorry. Did you mean something like:

"The scsi block later requires requests claimed by the error handling to be completed by the error handler"?

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

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 10 lines · Johannes Thumshirn
Quoted 3 lines · Keith Busch

The scsi block layer requires requests claimed be the error handling be completed by the error handler. A previous commit allowed completions to proceed for blk-mq, breaking that assumption.

Ahm the first sentence is a bit hard to parse, sorry. Did you mean something like:

"The scsi block later requires requests claimed by the error handling to be completed by the error handler"?

Ugh, sorry. Yes, you are correct on the intended phrasing.

#5 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 2 lines · Christoph Hellwig

Has this been tested? Especially with the blk_abort_request corner case?

Yes, that's what Jianchao's modification to scsi_debug used.

#6 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

In reply to Keith Busch

drivers/scsi/scsi_er=

Quoted 1 line · Keith Busch
drivers/scsi/scsi_er= renamed from drivers/scsi/scsi_error.c +0 -0

ror.c

Quoted 4 lines

index 8932ae81a15a..86ee10b2c775 100644
--- a/drivers/scsi/scsi_error.c
+-+-+- b/drivers/scsi/scsi_error.c
@@ -286,6 +-286,9 @@ enum blk_eh_timer_return=

scsi_times_out(struct request *req)

Quoted 1 line

enum blk_eh_timer_return rtn = BLK_EH_DONE+=

ADs-

Quoted 1 line

struct Scsi_Host *host = scmd->device->host;

-

Quoted 2 lines

=20
+- if (req->q->mq_ops && blk_mq_mark_co=

mplete(req))

Quoted 6 lines
+-		return rtn;
+-
drivers/scsi/scsi_er= renamed from drivers/scsi/scsi_error.c +0 -0
 	trace_scsi_dispatch_cmd_timeout(scmd);
 	scsi_log_completion(scmd, TIMEOUT_ERROR);

=20
@@ -300,7 +-303,8 @@ enum blk_eh_timer_return=

scsi_times_out(struct request *req)

Quoted 9 lines
drivers/scsi/scsi_er= renamed from drivers/scsi/scsi_error.c +2 -1
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_scmd_add(scmd);
 		}
-	}
+-	} else if (req->q->mq_ops)
+-		WRITE_ONCE(req->state, MQ_RQ_IN_FLIGHT);

=20
return rtn;
}

Modifying the completion state and req->state from the SCSI core are la= yering
violations. Have you considered to move the above changes into blk_mq� F8-rq_timed_out()?
An additional benefit of that approach is that the req->q->mq_o= ps checks can be
left out.

Thanks,

Bart.=

#7 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 23 lines · Bart Van Assche
Quoted 22 lines · Keith Busch
drivers/scsi/scsi_error.c +5 -1
index 8932ae81a15a..86ee10b2c775 100644
@@ -286,6 +286,9 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 	enum blk_eh_timer_return rtn = BLK_EH_DONE;
 	struct Scsi_Host *host = scmd->device->host;
 
+	if (req->q->mq_ops && blk_mq_mark_complete(req))
+		return rtn;
+
 	trace_scsi_dispatch_cmd_timeout(scmd);
 	scsi_log_completion(scmd, TIMEOUT_ERROR);
 
@@ -300,7 +303,8 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_scmd_add(scmd);
 		}
-	}
+	} else if (req->q->mq_ops)
+		WRITE_ONCE(req->state, MQ_RQ_IN_FLIGHT);
 
Quoted 7 lines

drivers/scsi/scsi_error.c enum blk_eh_timer_return scsi_times_out(struct request *req) · line 310

Quoted 2 lines
drivers/scsi/scsi_error.c +0 -0
 	return rtn;
 }

Modifying the completion state and req->state from the SCSI core are layering violations. Have you considered to move the above changes into blk_mq_rq_timed_out()? An additional benefit of that approach is that the req->q->mq_ops checks can be left out.

SCSI is the only block driver that wants this behavior. Moving it back to generic where it used to be breaks other block drivers.

#8 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 3 lines · Keith Busch
Quoted 2 lines · Bart Van Assche

drivers/scsi=

Quoted 1 line · Keith Busch
drivers/scsi= renamed from drivers/scsi/scsi_error.c +0 -0

/scsi_error.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

index 8932ae81a15a..86ee10b2c775 100644
--- a/drivers/scsi/scsi_error.c
+-+-+- b/drivers/scsi/scsi_error.c
@@ -286,6 +-286,9 @@ enum blk_eh_time=

r_return scsi_times_out(struct request *req)

Quoted 1 line
Quoted 1 line
Quoted 1 line

enum blk_eh_timer_return rtn = BLK_=

EH_DONE;

Quoted 1 line
Quoted 1 line
Quoted 1 line

struct Scsi_Host *host = scmd->device-+=

AD4-host;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

=20
+- if (req->q->mq_ops && blk_mq_

-mark_complete(req))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+-		return rtn;
+-

trace_scsi_dispatch_cmd_timeout(scmd)�

Ds-

Quoted 1 line
Quoted 1 line
Quoted 1 line

scsi_log_completion(scmd, TIMEOUT_ERROR)�

s-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

=20
@@ -300,7 +-303,8 @@ enum blk_eh_time=

r_return scsi_times_out(struct request *req)

Quoted 1 line
Quoted 1 line
Quoted 1 line

set_host_byte(scmd, DID_TIME_OUT);

-

Quoted 5 lines
Quoted 5 lines

drivers/scsi=

Quoted 5 lines
drivers/scsi= renamed from drivers/scsi/scsi_error.c +2 -1
 			scsi_eh_scmd_add(scmd);
 		}
-	}
+-	} else if (req->q->mq_ops)
+-		WRITE_ONCE(req->state, MQ_RQ_IN_F=

LIGHT);

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

=20
return rtn;
}

=20
Modifying the completion state and req->state from the SCSI=

core are layering

Quoted 1 line
Quoted 1 line

violations. Have you considered to move the above changes into =

blk_mq_rq_timed_out()?

Quoted 1 line
Quoted 1 line

An additional benefit of that approach is that the req->q-+=

AD4-mq_ops checks can be

Quoted 3 lines
Quoted 1 line

left out.

=20
SCSI is the only block driver that wants this behavior. Moving it bac=

k

Quoted 1 line

to generic where it used to be breaks other block drivers.

That's new to me. What would break in the NVMe driver if the above change w= ould be
present in the block layer?

Thanks,

Bart.

#9 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 6 lines · Bart Van Assche
Quoted 2 lines · Keith Busch

SCSI is the only block driver that wants this behavior. Moving it back to generic where it used to be breaks other block drivers.

That's new to me. What would break in the NVMe driver if the above change would be present in the block layer?

This is what causes the block layer to lose completions, and most drivers don't want the kernel to lose their completions.

#10 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 3 lines · Keith Busch
Quoted 2 lines · Bart Van Assche
Quoted 1 line · Keith Busch

SCSI is the only block driver that wants this behavior. M=

oving it back

Quoted 1 line
Quoted 1 line
Quoted 1 line

to generic where it used to be breaks other block drivers=

.

Quoted 2 lines
Quoted 2 lines

=20
That's new to me. What would break in the NVMe driver if the ab=

ove change would be

Quoted 3 lines
Quoted 1 line

present in the block layer?

=20
This is what causes the block layer to lose completions, and most dri=

vers

Quoted 1 line

don't want the kernel to lose their completions.

Hello Keith,

Have you considered to introduce a fourth state for block layer requests to avoid that completions that occur while a timeout handler is in progress ge= t
lost? That would avoid that completions get lost not only for the NVMe driv= er
but also for SCSI drivers. See e.g. the MQ_RQ_TIMED_OUT state i= n
https://www.mail-archive.com/[email protected]/msg22196.html

Thanks,

Bart.=

#11 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 12 lines · Bart Van Assche
Quoted 10 lines · Keith Busch
Quoted 6 lines · Bart Van Assche
Quoted 2 lines · Keith Busch

SCSI is the only block driver that wants this behavior. Moving it back to generic where it used to be breaks other block drivers.

That's new to me. What would break in the NVMe driver if the above change would be present in the block layer?

This is what causes the block layer to lose completions, and most drivers don't want the kernel to lose their completions.

Quoted 7 lines

Hello Keith,

Have you considered to introduce a fourth state for block layer requests to avoid that completions that occur while a timeout handler is in progress get lost? That would avoid that completions get lost not only for the NVMe driver but also for SCSI drivers. See e.g. the MQ_RQ_TIMED_OUT state in
https://www.mail-archive.com/[email protected]/msg22196.html

Yes, I've considered that, and I really want to use it, but scsi may still reference a freed request in scmd_eh_abort_handler that way.

#12 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 3 lines · Keith Busch
Quoted 2 lines · Bart Van Assche
Quoted 1 line · Keith Busch

On Fri, Jul 20, 2018 at 04:03:18PM +-0000, Bart Van Assch=

Quoted 1 line · e wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line

On Fri, 2018-07-20 at 09:56 -0600, Keith Busch wrot=

e:

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

SCSI is the only block driver that wants this=

behavior. Moving it back

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

to generic where it used to be breaks other b=

lock drivers.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

=20
That's new to me. What would break in the NVMe driv=

er if the above change would be

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

present in the block layer?

=20
This is what causes the block layer to lose completions, =

and most drivers

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

don't want the kernel to lose their completions.

=20
Hello Keith,
=20
Have you considered to introduce a fourth state for block layer=

requests to

Quoted 1 line
Quoted 1 line

avoid that completions that occur while a timeout handler is in=

progress get

Quoted 1 line
Quoted 1 line

lost? That would avoid that completions get lost not only for t=

he NVMe driver

Quoted 1 line
Quoted 1 line

but also for SCSI drivers. See e.g. the MQ_RQ_TIMED_

-OUT state in

Quoted 1 line
Quoted 1 line

https://www.mail-archive.com/[email protected]/ms=

g22196.html

Quoted 3 lines

=20
Yes, I've considered that, and I really want to use it, but scsi may still reference a freed request in scmd_eh_abort_handler =

that way.

I think that's a misunderstanding. If scsi_times_out() queues an ab= ort
asynchronously then it tells the block layer through its return value that = the
SCSI core still owns the request and hence that the block layer should igno= re any
completions that occur until the SCSI core calls scsi_finish_comman= d(). That
scsi_finish_command() will trigger a call to __blk_mq_ -end_request(). The
scsi_times_out() return value I was referring to is called BLK_= EH_DONE today and
was called BLK_EH_NOT_HANDLED in kernel version v4.17.

This also means that I got the BLK_EH_NOT_HANDLED case wrong in=
"blk-mq: Rework
blk-mq timeout handling again": in that case concurrent a blk_mq� 8-complete_request()
call should be ignored instead of triggering request completion.

Bart.=

#13 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 11 lines · Bart Van Assche

I think that's a misunderstanding. If scsi_times_out() queues an abort asynchronously then it tells the block layer through its return value that the SCSI core still owns the request and hence that the block layer should ignore any completions that occur until the SCSI core calls scsi_finish_command(). That scsi_finish_command() will trigger a call to __blk_mq_end_request(). The scsi_times_out() return value I was referring to is called BLK_EH_DONE today and was called BLK_EH_NOT_HANDLED in kernel version v4.17.

This also means that I got the BLK_EH_NOT_HANDLED case wrong in "blk-mq: Rework blk-mq timeout handling again": in that case concurrent a blk_mq_complete_request() call should be ignored instead of triggering request completion.

I definitely think it's worth revisiting that for the longer term.

For near term, I don't want scsi error handling broken for 4.18, but also not revert the changes that fixed all the other drivers. Restoring the old behavior that scsi wants isolated to the scsi driver seems like the lowest touch option.

My patch restores the state that scsi had in 4.17. It still has that gap that may lose requests forever when the scsi LLD always returns BLK_EH_RESET_TIMER (see virtio-scsi, for example). That gap existed prior, so that's not new with my patch. Maybe we can fix that with a slight modification to my previous patch. It looks like SCSI really wants to block completions only when it hands off the command to the error handler, so we don't need to have the inflight -> compete -> inflight transition, and the following is all that's needed:

---

drivers/scsi/scsi_error.c +2 -0
index 8932ae81a15a..902c30d3c0ed 100644
@@ -296,6 +296,8 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 		rtn = host->hostt->eh_timed_out(scmd);
 
 	if (rtn == BLK_EH_DONE) {
+		if (req->q->mq_ops && blk_mq_mark_complete(req))
+			return rtn;
 		if (scsi_abort_command(scmd) != SUCCESS) {
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_scmd_add(scmd);

--

#14 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 8 lines · Keith Busch

My patch restores the state that scsi had in 4.17. It still has that gap that may lose requests forever when the scsi LLD always returns BLK_EH_RESET_TIMER (see virtio-scsi, for example). That gap existed prior, so that's not new with my patch. Maybe we can fix that with a slight modification to my previous patch. It looks like SCSI really wants to block completions only when it hands off the command to the error handler, so we don't need to have the inflight -> compete -> inflight transition, and the following is all that's needed:

Btw, one thing we should do in blk-mq and scsi is to make the time optional. If the blk_mq driver doesn't even have a timeout structure there is no point in timing out requests and enter the timeout handler ever. Same for those scsi drivers always returning BLK_EH_RESET_TIMER.

Whether never having timeouts is a good idea is a different discussion, but as long as we have such drivers we should handle them somewhat sane.

drivers/scsi/scsi_error.c enum blk_eh_timer_return scsi_times_out(struct request *req) · line 300

Quoted 11 lines

---

drivers/scsi/scsi_error.c +2 -0
index 8932ae81a15a..902c30d3c0ed 100644
@@ -296,6 +296,8 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 		rtn = host->hostt->eh_timed_out(scmd);
 
 	if (rtn == BLK_EH_DONE) {
+		if (req->q->mq_ops && blk_mq_mark_complete(req))
+			return rtn;

This looks pretty sensible to me as a band-aid. It just needs a very detailed comment explaining what is going on here.

#15 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

Quoted 2 lines · On Mon, 2018-07-23 at 10:12 +-0200, [email protected] wrote:

Btw, one thing we should do in blk-mq and scsi is to make the time optional. If the blk_mq driver doesn't even have a timeout struc=

ture

Quoted 1 line

there is no point in timing out requests and enter the timeout handle=

r

Quoted 1 line

ever.

Are there any blk-mq drivers that do not define a timeout handler and that use shared tags? I think such drivers need periodic calls to blk_mq_ -tag_idle().
Do you perhaps want to happen these calls from another context?

Thanks,

Bart.=

#16 lore raw

Re: [PATCH 2/2] scsi: set timed out out mq requests to complete

In reply to [email protected]
Quoted 7 lines · On Mon, Jul 23, 2018 at 10:12:31AM +0200, [email protected] wrote:
Quoted 7 lines
drivers/scsi/scsi_error.c +0 -0
index 8932ae81a15a..902c30d3c0ed 100644
@@ -296,6 +296,8 @@ enum blk_eh_timer_return scsi_times_out(struct request *req)
 		rtn = host->hostt->eh_timed_out(scmd);
 
Quoted 6 lines

drivers/scsi/scsi_error.c enum blk_eh_timer_return scsi_times_out(struct request *req) · line 300

Quoted 3 lines
drivers/scsi/scsi_error.c +2 -0
 	if (rtn == BLK_EH_DONE) {
+		if (req->q->mq_ops && blk_mq_mark_complete(req))
+			return rtn;

This looks pretty sensible to me as a band-aid. It just needs a very detailed comment explaining what is going on here.

Sounds good, v2 will be sent shortly.

#17 lore raw
In reply to Keith Busch

include/linux/blk-mq.h void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues); · line 299

Quoted 8 lines
include/linux/blk-mq.h +8 -0
+/*
+ * Returns true if request was not in flight.
+ */
+static inline bool blk_mq_mark_complete(struct request *rq)
+{
+	return (cmpxchg(&rq->state, MQ_RQ_IN_FLIGHT, MQ_RQ_COMPLETE) !=
+			MQ_RQ_IN_FLIGHT);
+}

This needs a much better comment describing when and how to use it.

Also the outer braces in the return statement are not required.