Skip to contentSkip to conversation

Add scsi_done_direct() to complete request directly.

PATCH Applied Sebastian Andrzej Siewior 3 participants 10 messages Feb 1, 2022 – Feb 11, 2022

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

cover Add scsi_done_direct() to complete request directly.

This mini series adds scsi_done_direct() in order to complete scsi requests directly via blk_mq_complete_request_direct(). This used by the usb-storage driver.

Sebastian

#1 lore raw

PATCH 1/2 scsi: Add scsi_done_direct() for immediate completion.

Add scsi_done_direct() which behaves like scsi_done() except that it invokes blk_mq_complete_request_direct() in order to complete the request.
Callers from process context can complete the request directly instead waking ksoftirqd.

---
 drivers/scsi/scsi_lib.c  | 27 +++++++++++++++++++++++----
 include/scsi/scsi_cmnd.h |  1 +
 2 files changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/scsi_lib.c +23 -4
index 0a70aa763a961..778ef6d09616f 100644
@@ -1625,26 +1625,45 @@ static blk_status_t scsi_prepare_cmd(struct request *req)
 	return scsi_cmd_to_driver(cmd)->init_command(cmd);
 }
 
-void scsi_done(struct scsi_cmnd *cmd)
+static bool scsi_done_need_blk_compl(struct scsi_cmnd *cmd)
 {
 	switch (cmd->submitter) {
 	case SUBMITTED_BY_BLOCK_LAYER:
 		break;
 	case SUBMITTED_BY_SCSI_ERROR_HANDLER:
-		return scsi_eh_done(cmd);
+		scsi_eh_done(cmd);
+		return false;
 	case SUBMITTED_BY_SCSI_RESET_IOCTL:
-		return;
+		return false;
 	}
 
 	if (unlikely(blk_should_fake_timeout(scsi_cmd_to_rq(cmd)->q)))
-		return;
+		return false;
 	if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
+		return false;
+	return true;
+}
+
+void scsi_done(struct scsi_cmnd *cmd)
+{
+	if (!scsi_done_need_blk_compl(cmd))
 		return;
+
 	trace_scsi_dispatch_cmd_done(cmd);
 	blk_mq_complete_request(scsi_cmd_to_rq(cmd));
 }
 EXPORT_SYMBOL(scsi_done);
 
+void scsi_done_direct(struct scsi_cmnd *cmd)
+{
+	if (!scsi_done_need_blk_compl(cmd))
+		return;
+
+	trace_scsi_dispatch_cmd_done(cmd);
+	blk_mq_complete_request_direct(scsi_cmd_to_rq(cmd), scsi_complete);
+}
+EXPORT_SYMBOL(scsi_done_direct);
+
 static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
 {
 	struct scsi_device *sdev = q->queuedata;
include/scsi/scsi_cmnd.h +1 -0
index 6794d7322cbde..ff1c4b51f7aef 100644
@@ -168,6 +168,7 @@ static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
 }
 
 void scsi_done(struct scsi_cmnd *cmd);
+void scsi_done_direct(struct scsi_cmnd *cmd);
 
 extern void scsi_finish_command(struct scsi_cmnd *cmd);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] scsi: Add scsi_done_direct() for immediate completion.

drivers/scsi/scsi_lib.c static blk_status_t scsi_prepare_cmd(struct request *req) · line 1628

Quoted 2 lines · Sebastian Andrzej Siewior
drivers/scsi/scsi_lib.c +1 -1
-void scsi_done(struct scsi_cmnd *cmd)
+static bool scsi_done_need_blk_compl(struct scsi_cmnd *cmd)

I'm not happy about the name of this function. The word "need" in the function name suggests that this function does not modify any state. However, the body of the function shows that it may complete a SCSI command. How about renaming the existing scsi_done() function into scsi_done_internal() or so and adding a "bool complete_directly" argument to that function?

BTW, I only received patch 1/2 but not patch 2/2. Please Cc the linux-scsi mailing list for the entire patch series when reposting the patch series.

Thanks,

Bart.

#3 lore raw

Re: [PATCH 1/2] scsi: Add scsi_done_direct() for immediate completion.

Quoted 10 lines · Bart Van Assche

drivers/scsi/scsi_lib.c static blk_status_t scsi_prepare_cmd(struct request *req) · line 1628

Quoted 2 lines · Sebastian Andrzej Siewior
drivers/scsi/scsi_lib.c +1 -1
-void scsi_done(struct scsi_cmnd *cmd)
+static bool scsi_done_need_blk_compl(struct scsi_cmnd *cmd)

I'm not happy about the name of this function. The word "need" in the function name suggests that this function does not modify any state. However, the body of the function shows that it may complete a SCSI command. How about renaming the existing scsi_done() function into scsi_done_internal() or so and adding a "bool complete_directly" argument to that function?

Let me see what I can do.

Quoted 2 lines

BTW, I only received patch 1/2 but not patch 2/2. Please Cc the linux-scsi mailing list for the entire patch series when reposting the patch series.

I did and based on lore's archive it made it to the list:

	https://lore.kernel.org/linux-scsi/[email protected]/
Quoted 3 lines

Thanks,

Bart.

Sebastian

#4 lore raw

PATCH v2 1/2 scsi: Add scsi_done_direct() for immediate completion.

Add scsi_done_direct() which behaves like scsi_done() except that it invokes blk_mq_complete_request_direct() in order to complete the request.
Callers from process context can complete the request directly instead waking ksoftirqd.

---

Quoted 2 lines · To Bart Van Assche

Let me see what I can do.

Something like this perhaps? The compiler not inline scsi_done_internal() so we maybe provide scsi_done() / _direct() as static inlines?

 drivers/scsi/scsi_lib.c  | 21 +++++++++++++++++++--
 include/scsi/scsi_cmnd.h |  1 +
 2 files changed, 20 insertions(+), 2 deletions(-)
drivers/scsi/scsi_lib.c +19 -2
index 0a70aa763a961..a1c18ba5e8d38 100644
@@ -1625,8 +1625,10 @@ static blk_status_t scsi_prepare_cmd(struct request *req)
 	return scsi_cmd_to_driver(cmd)->init_command(cmd);
 }
 
-void scsi_done(struct scsi_cmnd *cmd)
+static void scsi_done_internal(struct scsi_cmnd *cmd, bool complete_directly)
 {
+	struct request *req = scsi_cmd_to_rq(cmd);
+
 	switch (cmd->submitter) {
 	case SUBMITTED_BY_BLOCK_LAYER:
 		break;
@@ -1641,10 +1643,25 @@ void scsi_done(struct scsi_cmnd *cmd)
 	if (unlikely(test_and_set_bit(SCMD_STATE_COMPLETE, &cmd->state)))
 		return;
 	trace_scsi_dispatch_cmd_done(cmd);
-	blk_mq_complete_request(scsi_cmd_to_rq(cmd));
+
+	if (complete_directly)
+		blk_mq_complete_request_direct(req, scsi_complete);
+	else
+		blk_mq_complete_request(req);
+}
+
+void scsi_done(struct scsi_cmnd *cmd)
+{
+	scsi_done_internal(cmd, false);
 }
 EXPORT_SYMBOL(scsi_done);
 
+void scsi_done_direct(struct scsi_cmnd *cmd)
+{
+	scsi_done_internal(cmd, true);
+}
+EXPORT_SYMBOL(scsi_done_direct);
+
 static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
 {
 	struct scsi_device *sdev = q->queuedata;
include/scsi/scsi_cmnd.h +1 -0
index 6794d7322cbde..ff1c4b51f7aef 100644
@@ -168,6 +168,7 @@ static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
 }
 
 void scsi_done(struct scsi_cmnd *cmd);
+void scsi_done_direct(struct scsi_cmnd *cmd);
 
 extern void scsi_finish_command(struct scsi_cmnd *cmd);
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#5 lore raw

Re: [PATCH v2 1/2] scsi: Add scsi_done_direct() for immediate completion.

Quoted 5 lines · Sebastian Andrzej Siewior

Let me see what I can do.

Something like this perhaps? The compiler not inline scsi_done_internal() so we maybe provide scsi_done() / _direct() as static inlines?

In general declaring a static function inline in a .c file is frowned upon but I think in this case we should do that. With that change applied, feel free to add:

#6 lore raw

Re: [PATCH 1/2] scsi: Add scsi_done_direct() for immediate completion.

In reply to Sebastian Andrzej Siewior
Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Bart Van Assche

drivers/scsi/scsi_lib.c static blk_status_t scsi_prepare_cmd(struct request *req) · line 1628

Quoted 2 lines · Sebastian Andrzej Siewior
drivers/scsi/scsi_lib.c +1 -1
-void scsi_done(struct scsi_cmnd *cmd)
+static bool scsi_done_need_blk_compl(struct scsi_cmnd *cmd)

I'm not happy about the name of this function. The word "need" in the function name suggests that this function does not modify any state. However, the body of the function shows that it may complete a SCSI command. How about renaming the existing scsi_done() function into scsi_done_internal() or so and adding a "bool complete_directly" argument to that function?

Quoted 7 lines

Let me see what I can do.

Quoted 2 lines

BTW, I only received patch 1/2 but not patch 2/2. Please Cc the linux-scsi mailing list for the entire patch series when reposting the patch series.

I did and based on lore's archive it made it to the list:

	https://lore.kernel.org/linux-scsi/[email protected]/

I agree that patch 2/2 seems to have made it to the linux-scsi list. However, I can't find that patch in my mailbox nor in my spam folder. I think this is the first time that I did not receive a patch sent to the linux-scsi mailing list. Weird ...

Bart.

#7 lore raw

PATCH 2/2 usb: storage: Complete the scsi request directly.

In reply to Sebastian Andrzej Siewior

The storage driver completes its requests directly from a kernel thread.

Use scsi_done_direct() to avoid waking ksoftirqd.

---
 drivers/usb/storage/usb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/usb.c +1 -1
index 8b543f2c98575..ed7c6ad96a743 100644
@@ -417,7 +417,7 @@ static int usb_stor_control_thread(void * __us)
 		if (srb) {
 			usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",
 					srb->result);
-			scsi_done(srb);
+			scsi_done_direct(srb);
 		}
 	} /* for (;;) */
 
Signature · 1 line
2.34.1
#8 lore raw
In reply to Sebastian Andrzej Siewior

Sebastian,

Quoted 3 lines

This mini series adds scsi_done_direct() in order to complete scsi requests directly via blk_mq_complete_request_direct(). This used by the usb-storage driver.

Applied to 5.18/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#9 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 6 lines · Sebastian Andrzej Siewior

This mini series adds scsi_done_direct() in order to complete scsi requests directly via blk_mq_complete_request_direct(). This used by the usb-storage driver.

Sebastian

Applied to 5.18/scsi-queue, thanks!

[1/2] scsi: Add scsi_done_direct() for immediate completion.

      https://git.kernel.org/mkp/scsi/c/b84b6ec0f976
[2/2] usb: storage: Complete the scsi request directly.
      https://git.kernel.org/mkp/scsi/c/23fe075519c6
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering