mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yingjie Wang <x-professor@outlook.com>
To: "Martin K. Petersen" <mkp@kernel.org>
Cc: "James.Bottomley@HansenPartnership.com"
	<James.Bottomley@HansenPartnership.com>,
	FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>,
	"axboe@kernel.dk" <axboe@kernel.dk>,
	"linux-scsi@vger.kernel.org" <linux-scsi@vger.kernel.org>,
	"linux-block@vger.kernel.org" <linux-block@vger.kernel.org>,
	"bvanassche@acm.org" <bvanassche@acm.org>,
	"jthoughton@google.com" <jthoughton@google.com>,
	"yangxiuwei@kylinos.cn" <yangxiuwei@kylinos.cn>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH v4 1/2] block: bsg: add a per-file pool of uring_cmd completion buffers
Date: Wed, 18 Nov 2026 15:37:28 +0000	[thread overview]
Message-ID: <23f5517740a03a6e1759d9f2cd1f4c170fc60485.1795016239.git.x-professor@outlook.com> (raw)
In-Reply-To: <cover.1795016239.git.x-professor@outlook.com>

A bsg uring_cmd driver may need to pass data that must outlive the
block request to its task work: scsi_bsg has to snapshot sense data
when it stops keeping completed requests until task work runs (next
patch).

Let a bsg driver request a pool of fixed-size buffers for each open
file, allocated at open() time and freed at release().  The driver
declares the buffer size it needs when the queue is registered with
bsg_register_queue(), and checks a buffer out with
bsg_uring_cmd_get_completion_buf() at issue time.  When the pool is
exhausted, the caller can reject the command with -EAGAIN instead of
allocating or sleeping in the I/O path.

On a kernel built with io_uring, bsg allocates a pool at open() for
drivers that register a nonzero completion buffer size.  The pool lives
as long as the file, so charge it to the opener's memory cgroup with
GFP_KERNEL_ACCOUNT.  Without io_uring no uring_cmd can be issued, so no
pool is allocated.

The pool depth is BSG_DEFAULT_CMDS (64), the value bsg reports by
default through SG_GET_COMMAND_Q.  For SCSI the buffers take 6 KiB per
open file.  A buffer stays checked out from issue until task work
returns it, so the depth caps the number of commands per file that
hold a buffer.  The depth is deliberately not tied to the device queue
depth: end-I/O releases the request tag, so the number of commands
waiting for task work is not bounded by can_queue.  If the pool were
sized to a device queue depth of 1, its only buffer could still be
held by a completed command waiting for task work after that command's
tag was freed; the next command would then fail with -EAGAIN although
a tag is available.

Each buffer must be returned exactly once, using
bsg_uring_cmd_put_completion_buf() before the command completes or
fails.  The pool warns if a buffer is returned while it is already free.

io_uring holds a reference on the file for the lifetime of a request,
and a driver returns its buffer before completing the command, so
bsg_release() cannot free a pool that still has buffers checked out.

The SCSI registration passes SCSI_SENSE_BUFFERSIZE; transport bsg
registers no uring_cmd handler and passes 0, so it allocates no pool.
No in-tree caller checks buffers out yet; scsi_bsg adopts the pool in
the next patch.

Suggested-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 block/bsg-lib.c         |   3 +-
 block/bsg.c             | 141 +++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/scsi_bsg.c |   2 +-
 include/linux/bsg.h     |   6 +-
 4 files changed, 147 insertions(+), 5 deletions(-)

diff --git a/block/bsg-lib.c b/block/bsg-lib.c
index 536cee4430a7..6f9aedd9c8e9 100644
--- a/block/bsg-lib.c
+++ b/block/bsg-lib.c
@@ -400,7 +400,8 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
 
 	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
 
-	bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn, NULL);
+	bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn,
+				      NULL, 0);
 	if (IS_ERR(bset->bd)) {
 		ret = PTR_ERR(bset->bd);
 		goto out_cleanup_queue;
diff --git a/block/bsg.c b/block/bsg.c
index b296fa373292..ccd9323c39f8 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -28,6 +28,7 @@ struct bsg_device {
 	int max_queue;
 	unsigned int timeout;
 	unsigned int reserved_size;
+	size_t completion_buf_size;
 	bsg_sg_io_fn *sg_io_fn;
 	bsg_uring_cmd_fn *uring_cmd_fn;
 };
@@ -40,6 +41,30 @@ static inline struct bsg_device *to_bsg_device(struct inode *inode)
 #define BSG_DEFAULT_CMDS	64
 #define BSG_MAX_DEVS		(1 << MINORBITS)
 
+/*
+ * Pool of fixed-size completion buffers, one per open file of a queue
+ * whose driver registered a buffer size.  A uring_cmd driver that returns
+ * data from task work can check a buffer out at issue time and return it
+ * from task work instead of allocating in the I/O path.
+ * bsg_register_queue() declares the buffer size the driver needs.  The
+ * pool is allocated at open() time, so no buffer is allocated in the I/O
+ * path, and is charged to the opener's memory cgroup; when the pool is
+ * exhausted the driver can reject the command with -EAGAIN instead of
+ * sleeping.  Keeping the pool per file makes its lifetime simple: io_uring
+ * holds a reference on the file for the lifetime of a request, and a
+ * driver returns its buffer before completing the command, so
+ * bsg_release() cannot free a pool that still has buffers checked out.
+ */
+struct bsg_completion_pool {
+	spinlock_t lock;		/* protects free_top and stack */
+	unsigned int depth;		/* number of buffers */
+	unsigned int free_top;		/* buffers currently available */
+	size_t buf_size;		/* size of each buffer */
+	u8 *bufs;			/* depth buffers of buf_size bytes */
+	u8 *checked_out;		/* per buffer: 1 while borrowed */
+	u32 stack[];			/* indices of the available buffers */
+};
+
 static DEFINE_IDA(bsg_minor_ida);
 static const struct class bsg_class;
 static int bsg_major;
@@ -73,15 +98,126 @@ static int bsg_sg_io(struct bsg_device *bd, bool open_for_write,
 	return ret;
 }
 
+static struct bsg_completion_pool *
+bsg_completion_pool_alloc(struct bsg_device *bd)
+{
+	struct bsg_completion_pool *pool;
+	unsigned int depth = BSG_DEFAULT_CMDS;
+	unsigned int i;
+
+	pool = kzalloc(struct_size(pool, stack, depth), GFP_KERNEL_ACCOUNT);
+	if (!pool)
+		return NULL;
+	pool->bufs = kcalloc(depth, bd->completion_buf_size,
+			     GFP_KERNEL_ACCOUNT);
+	pool->checked_out = kcalloc(depth, 1, GFP_KERNEL_ACCOUNT);
+	if (!pool->bufs || !pool->checked_out) {
+		kfree(pool->checked_out);
+		kfree(pool->bufs);
+		kfree(pool);
+		return NULL;
+	}
+	pool->depth = depth;
+	pool->free_top = depth;
+	pool->buf_size = bd->completion_buf_size;
+	spin_lock_init(&pool->lock);
+	for (i = 0; i < depth; i++)
+		pool->stack[i] = i;
+	return pool;
+}
+
+static void bsg_completion_pool_free(struct bsg_completion_pool *pool)
+{
+	kfree(pool->checked_out);
+	kfree(pool->bufs);
+	kfree(pool);
+}
+
+/**
+ * bsg_uring_cmd_get_completion_buf - check out a per-file completion buffer
+ * @ioucmd: the uring command being issued
+ * @len: required buffer size
+ *
+ * Check a buffer out of the pool of the opened bsg file.  The caller must
+ * return the buffer with bsg_uring_cmd_put_completion_buf() before it
+ * completes or fails the command, and must not reissue the command while
+ * the buffer is checked out.
+ *
+ * Return: the buffer, or NULL if the file has no pool, @len exceeds the
+ * registered buffer size, or the pool is exhausted.
+ */
+void *bsg_uring_cmd_get_completion_buf(struct io_uring_cmd *ioucmd, size_t len)
+{
+	struct bsg_completion_pool *pool = ioucmd->file->private_data;
+	unsigned int index;
+	void *buf;
+
+	if (!pool || WARN_ON_ONCE(len > pool->buf_size))
+		return NULL;
+
+	spin_lock(&pool->lock);
+	if (pool->free_top == 0) {
+		spin_unlock(&pool->lock);
+		return NULL;
+	}
+	index = pool->stack[--pool->free_top];
+	pool->checked_out[index] = 1;
+	buf = pool->bufs + (size_t)index * pool->buf_size;
+	spin_unlock(&pool->lock);
+	return buf;
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_get_completion_buf);
+
+/**
+ * bsg_uring_cmd_put_completion_buf - return a per-file completion buffer
+ * @ioucmd: the uring command being completed
+ * @buf: buffer previously returned by bsg_uring_cmd_get_completion_buf()
+ *
+ * Return the buffer to the pool of the opened bsg file.  Each buffer
+ * must be returned exactly once.  The pool warns if a buffer is returned
+ * while it is already free.
+ */
+void bsg_uring_cmd_put_completion_buf(struct io_uring_cmd *ioucmd, void *buf)
+{
+	struct bsg_completion_pool *pool = ioucmd->file->private_data;
+	unsigned int index;
+
+	if (WARN_ON_ONCE(!buf))
+		return;
+	index = ((u8 *)buf - pool->bufs) / pool->buf_size;
+	if (WARN_ON_ONCE(index >= pool->depth))
+		return;
+	spin_lock(&pool->lock);
+	if (WARN_ON_ONCE(!pool->checked_out[index])) {
+		spin_unlock(&pool->lock);
+		return;
+	}
+	pool->checked_out[index] = 0;
+	pool->stack[pool->free_top++] = index;
+	spin_unlock(&pool->lock);
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_put_completion_buf);
+
 static int bsg_open(struct inode *inode, struct file *file)
 {
-	if (!blk_get_queue(to_bsg_device(inode)->queue))
+	struct bsg_device *bd = to_bsg_device(inode);
+
+	if (!blk_get_queue(bd->queue))
 		return -ENXIO;
+	if (IS_ENABLED(CONFIG_IO_URING) && bd->completion_buf_size) {
+		file->private_data = bsg_completion_pool_alloc(bd);
+		if (!file->private_data) {
+			blk_put_queue(bd->queue);
+			return -ENOMEM;
+		}
+	}
 	return 0;
 }
 
 static int bsg_release(struct inode *inode, struct file *file)
 {
+	if (file->private_data)
+		bsg_completion_pool_free(file->private_data);
 	blk_put_queue(to_bsg_device(inode)->queue);
 	return 0;
 }
@@ -220,7 +356,7 @@ EXPORT_SYMBOL_GPL(bsg_unregister_queue);
 
 struct bsg_device *bsg_register_queue(struct request_queue *q,
 		struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn,
-		bsg_uring_cmd_fn *uring_cmd_fn)
+		bsg_uring_cmd_fn *uring_cmd_fn, size_t completion_buf_size)
 {
 	struct bsg_device *bd;
 	int ret;
@@ -231,6 +367,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
 	bd->max_queue = BSG_DEFAULT_CMDS;
 	bd->reserved_size = INT_MAX;
 	bd->queue = q;
+	bd->completion_buf_size = completion_buf_size;
 	bd->sg_io_fn = sg_io_fn;
 	bd->uring_cmd_fn = uring_cmd_fn;
 
diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index 5ffbd1843861..1c9cf77d0d6a 100644
--- a/drivers/scsi/scsi_bsg.c
+++ b/drivers/scsi/scsi_bsg.c
@@ -300,5 +300,5 @@ struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev)
 {
 	return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev,
 			dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn,
-			scsi_bsg_uring_cmd);
+			scsi_bsg_uring_cmd, SCSI_SENSE_BUFFERSIZE);
 }
diff --git a/include/linux/bsg.h b/include/linux/bsg.h
index 162730bfc2d8..3f33ead2c3ee 100644
--- a/include/linux/bsg.h
+++ b/include/linux/bsg.h
@@ -17,7 +17,11 @@ typedef int (bsg_uring_cmd_fn)(struct request_queue *q, struct io_uring_cmd *iou
 
 struct bsg_device *bsg_register_queue(struct request_queue *q,
 		struct device *parent, const char *name,
-		bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn);
+		bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn,
+		size_t completion_buf_size);
 void bsg_unregister_queue(struct bsg_device *bcd);
 
+void *bsg_uring_cmd_get_completion_buf(struct io_uring_cmd *ioucmd, size_t len);
+void bsg_uring_cmd_put_completion_buf(struct io_uring_cmd *ioucmd, void *buf);
+
 #endif /* _LINUX_BSG_H */
-- 
2.43.0


  reply	other threads:[~2026-11-18 15:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 15:37 [PATCH v4 0/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
2026-11-18 15:37 ` Yingjie Wang [this message]
2026-11-18 15:37 ` [PATCH v4 2/2] " Yingjie Wang

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=23f5517740a03a6e1759d9f2cd1f4c170fc60485.1795016239.git.x-professor@outlook.com \
    --to=x-professor@outlook.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=fujita.tomonori@lab.ntt.co.jp \
    --cc=jthoughton@google.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=yangxiuwei@kylinos.cn \
    /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

all inboxes | Powered by JetHome®