Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yingjie Wang" <x-professor@outlook.com>
Cc: "linux-scsi@vger.kernel.org" <linux-scsi@vger.kernel.org>
Subject: Re: [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O
Date: Sat, 21 Nov 2026 08:41:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-226841@kernel.org> (raw)
In-Reply-To: <scsi-pool-v5.1795249365940711260.2.x-professor@outlook.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] scsi: bsg: per-device spinlock contention on sense buffer
  pool per I/O
- [Medium] scsi: bsg: in-flight uring_cmd depth clamped to 64 with late
  pool check
--

commit 5e758c01d49b423d6603c6f22a4f49d0ad99b0ff
Author: Yingjie Wang <x-professor@outlook.com>

scsi: bsg: Release io_uring requests from end-I/O

This commit snapshots SCSI completion status and sense data in the request
end-I/O callback using the BSG completion buffer pool so the block request
can be freed before io_uring task work runs.

> diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
> index c17cad7983260..d1ad956df9d6f 100644
> --- a/drivers/scsi/scsi_bsg.c
> +++ b/drivers/scsi/scsi_bsg.c

[ ... ]

> @@ -78,9 +82,34 @@ static enum rq_end_io_ret scsi_bsg_uring_cmd_done(struct request *req,
>  						  const struct io_comp_batch *iocb)
>  {
>  	struct io_uring_cmd *ioucmd = req->end_io_data;
> +	struct scsi_bsg_uring_cmd_pdu *pdu = scsi_bsg_uring_cmd_pdu(ioucmd);
> +	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
> +	u8 driver_status = 0;
> +	u8 sense_len = 0;
> +
> +	if (scsi_status_is_check_condition(scmd->result)) {
> +		driver_status = DRIVER_SENSE;
> +		if (pdu->sense_buffer) {
> +			sense_len = min_t(unsigned int, pdu->max_response_len,
> +					  scmd->sense_len);
> +			if (sense_len)
> +				memcpy(pdu->sense_buffer, scmd->sense_buffer,
> +				       sense_len);
> +		}
> +	}
> +
> +	pdu->res2 = bsg_scsi_res2_build(status_byte(scmd->result),
> +					driver_status, host_byte(scmd->result),
> +					sense_len, scmd->resid_len);
> +
> +	/* Return the buffer now if there is no sense data to copy. */
> +	if (!sense_len && pdu->sense_buffer) {
> +		bsg_uring_cmd_put_completion_buf(ioucmd, pdu->sense_buffer);
> +		pdu->sense_buffer = NULL;
> +	}

[Severity: Medium]
Does checking out and returning a sense buffer on every command serialize
submissions and completions on the per-device pool->lock?

Because scsi_bsg_uring_cmd() calls bsg_uring_cmd_get_completion_buf() up
front whenever the caller provides a response buffer, every I/O acquires
spin_lock_irqsave(&pool->lock, flags) and modifies pool->checked_out twice
(once on submission and once here or in task work), even when the command
succeeds with SAM_STAT_GOOD and produces no sense data:

block/bsg.c:bsg_uring_cmd_get_completion_buf() {
    ...
	spin_lock_irqsave(&pool->lock, flags);
	index = find_first_zero_bit(pool->checked_out, BSG_DEFAULT_CMDS);
	if (index < BSG_DEFAULT_CMDS) {
		__set_bit(index, pool->checked_out);
		buf = pool->bufs + index * SCSI_SENSE_BUFFERSIZE;
	}
	spin_unlock_irqrestore(&pool->lock, flags);
    ...
}

Allocating the completion buffer in scsi_bsg_uring_cmd_done() only on
SAM_STAT_CHECK_CONDITION would keep the normal completion path lockless.

[ ... ]

> @@ -168,8 +200,25 @@ static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *iouc
>  		goto out_free_req;
>  	}
>  
> -	pdu->response_addr = cmd->response;
> -	pdu->max_response_len = cmd->max_response_len;
> +	pdu->response_addr = response_addr;
> +	pdu->max_response_len = max_response_len;
> +
> +	/*
> +	 * Take a sense buffer before mapping data: unmapping a bounced READ
> +	 * copies back even if no I/O ran, so rejecting it afterwards would
> +	 * overwrite the caller's data. No response buffer means no sense copy.
> +	 */
> +	if (response_addr && max_response_len) {
> +		pdu->sense_buffer =
> +			bsg_uring_cmd_get_completion_buf(ioucmd,
> +							 SCSI_SENSE_BUFFERSIZE);
> +		if (!pdu->sense_buffer) {
> +			ret = -EAGAIN;
> +			goto out_free_req;
> +		}
> +	} else {
> +		pdu->sense_buffer = NULL;
> +	}

[Severity: Medium]
Does reserving a slot from bd->completion_pool at submission time clamp
concurrent in-flight commands with a sense buffer to BSG_DEFAULT_CMDS (64)
per device, regardless of the device's blk-mq queue depth?

In addition, bsg_uring_cmd_get_completion_buf() is called after
scsi_alloc_request() and copy_from_user().  When all 64 slots are in use,
inline submission with IO_URING_F_NONBLOCK allocates and frees a blk-mq
request, returns -EAGAIN, and causes io_uring to punt the SQE to an io-wq
worker, which allocates and frees a request a second time before failing
again with -EAGAIN.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/scsi-pool-v5.1795249365940711260.0.x-professor@outlook.com?part=2

      reply	other threads:[~2026-11-21  8:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  8:31 [PATCH v5 0/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
2026-11-21  8:31 ` [PATCH v5 1/2] block: bsg: add a per-device pool of uring_cmd completion buffers Yingjie Wang
2026-11-21  8:31 ` [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
2026-11-21  8:41   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-226841@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=x-professor@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox