From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org, linux-block@vger.kernel.org,
linux-nvme@lists.infradead.org
Cc: linux-fsdevel@vger.kernel.org, Keith Busch <kbusch@kernel.org>,
David Wei <dw@davidwei.uk>,
Vishal Verma <vishal1.verma@intel.com>,
asml.silence@gmail.com
Subject: [RFC 10/12] io_uring/rsrc: add dmabuf-backed buffer registeration
Date: Fri, 27 Jun 2025 16:10:37 +0100 [thread overview]
Message-ID: <5c11f982536aa26bd03e8d8962919a140a08e473.1751035820.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1751035820.git.asml.silence@gmail.com>
Add an ability to register a dmabuf backed io_uring buffer. It also
needs know which device to use for attachment, for that it takes
target_fd and extracts the device through the new file op. Unlike normal
buffers, it also retains the target file so that any imports from
ineligible requests can be rejected in next patches.
Suggested-by: Vishal Verma <vishal1.verma@intel.com>
Suggested-by: David Wei <dw@davidwei.uk>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
io_uring/rsrc.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++-
io_uring/rsrc.h | 1 +
2 files changed, 118 insertions(+), 1 deletion(-)
diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 274274b80b96..f44aa2670bc5 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -10,6 +10,8 @@
#include <linux/compat.h>
#include <linux/io_uring.h>
#include <linux/io_uring/cmd.h>
+#include <linux/dma-map-ops.h>
+#include <linux/dma-buf.h>
#include <uapi/linux/io_uring.h>
@@ -18,6 +20,7 @@
#include "rsrc.h"
#include "memmap.h"
#include "register.h"
+#include "dmabuf.h"
struct io_rsrc_update {
struct file *file;
@@ -793,6 +796,117 @@ bool io_check_coalesce_buffer(struct page **page_array, int nr_pages,
return true;
}
+struct io_regbuf_dma {
+ struct io_dmabuf dmabuf;
+ struct dmavec *dmav;
+ struct file *target_file;
+};
+
+static void io_release_reg_dmabuf(struct io_regbuf_dma *db)
+{
+ if (db->dmav)
+ kfree(db->dmav);
+ io_dmabuf_release(&db->dmabuf);
+ if (db->target_file)
+ fput(db->target_file);
+
+ kfree(db);
+}
+
+static void io_release_reg_dmabuf_cb(void *priv)
+{
+ io_release_reg_dmabuf(priv);
+}
+
+static struct io_rsrc_node *io_register_dmabuf(struct io_ring_ctx *ctx,
+ struct io_uring_reg_buffer *rb,
+ struct iovec *iov)
+{
+ struct io_rsrc_node *node = NULL;
+ struct io_mapped_ubuf *imu = NULL;
+ struct io_regbuf_dma *regbuf;
+ struct file *target_file;
+ struct scatterlist *sg;
+ struct device *dev;
+ unsigned int segments;
+ int ret, i;
+
+ if (iov->iov_base || iov->iov_len)
+ return ERR_PTR(-EFAULT);
+
+ regbuf = kzalloc(sizeof(*regbuf), GFP_KERNEL);
+ if (!regbuf) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ target_file = fget(rb->target_fd);
+ if (!target_file) {
+ ret = -EBADF;
+ goto err;
+ }
+ regbuf->target_file = target_file;
+
+ if (!target_file->f_op->get_dma_device) {
+ ret = -EOPNOTSUPP;
+ goto err;
+ }
+ dev = target_file->f_op->get_dma_device(target_file);
+ if (IS_ERR(dev)) {
+ ret = PTR_ERR(dev);
+ goto err;
+ }
+
+ ret = io_dmabuf_import(®buf->dmabuf, rb->dmabuf_fd, dev,
+ DMA_BIDIRECTIONAL);
+ if (ret)
+ goto err;
+
+ segments = regbuf->dmabuf.sgt->nents;
+ regbuf->dmav = kmalloc_array(segments, sizeof(regbuf->dmav[0]),
+ GFP_KERNEL_ACCOUNT);
+ if (!regbuf->dmav) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ for_each_sgtable_dma_sg(regbuf->dmabuf.sgt, sg, i) {
+ regbuf->dmav[i].addr = sg_dma_address(sg);
+ regbuf->dmav[i].len = sg_dma_len(sg);
+ }
+
+ node = io_rsrc_node_alloc(ctx, IORING_RSRC_BUFFER);
+ if (!node) {
+ ret = -ENOMEM;
+ goto err;
+ }
+ imu = io_alloc_imu(ctx, 0);
+ if (!imu) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ imu->nr_bvecs = segments;
+ imu->ubuf = 0;
+ imu->len = regbuf->dmabuf.len;
+ imu->folio_shift = 0;
+ imu->release = io_release_reg_dmabuf_cb;
+ imu->priv = regbuf;
+ imu->flags = IO_IMU_F_DMA;
+ imu->dir = IO_IMU_DEST | IO_IMU_SOURCE;
+ refcount_set(&imu->refs, 1);
+ node->buf = imu;
+ return node;
+err:
+ if (regbuf)
+ io_release_reg_dmabuf(regbuf);
+ if (imu)
+ io_free_imu(ctx, imu);
+ if (node)
+ io_cache_free(&ctx->node_cache, node);
+ return ERR_PTR(ret);
+}
+
static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
struct io_uring_reg_buffer *rb,
struct iovec *iov,
@@ -808,7 +922,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
bool coalesced = false;
if (rb->dmabuf_fd != -1 || rb->target_fd != -1)
- return NULL;
+ return io_register_dmabuf(ctx, rb, iov);
if (!iov->iov_base)
return NULL;
@@ -1100,6 +1214,8 @@ static int io_import_fixed(int ddir, struct iov_iter *iter,
offset = buf_addr - imu->ubuf;
+ if (imu->flags & IO_IMU_F_DMA)
+ return -EOPNOTSUPP;
if (imu->flags & IO_IMU_F_KBUF)
return io_import_kbuf(ddir, iter, imu, len, offset);
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index 15ad4a885ae5..f567ad82b76c 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -30,6 +30,7 @@ enum {
enum {
IO_IMU_F_KBUF = 1,
+ IO_IMU_F_DMA = 2,
};
struct io_mapped_ubuf {
--
2.49.0
next prev parent reply other threads:[~2025-06-27 15:09 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-27 15:10 [RFC 00/12] io_uring dmabuf read/write support Pavel Begunkov
2025-06-27 15:10 ` [RFC 01/12] file: add callback returning dev for dma operations Pavel Begunkov
2025-06-27 15:10 ` [RFC 02/12] iov_iter: introduce iter type for pre-registered dma Pavel Begunkov
2025-06-27 15:10 ` [RFC 03/12] block: move around bio flagging helpers Pavel Begunkov
2025-06-27 15:10 ` [RFC 04/12] block: introduce dmavec bio type Pavel Begunkov
2025-06-27 15:10 ` [RFC 05/12] block: implement ->get_dma_device callback Pavel Begunkov
2025-06-27 15:10 ` [RFC 06/12] nvme-pci: add support for user passed dma vectors Pavel Begunkov
2025-06-27 15:10 ` [RFC 07/12] io_uring/rsrc: extended reg buffer registration Pavel Begunkov
2025-06-27 15:10 ` [RFC 08/12] io_uring: add basic dmabuf helpers Pavel Begunkov
2025-06-27 15:10 ` [RFC 09/12] io_uring/rsrc: add imu flags Pavel Begunkov
2025-06-27 15:10 ` Pavel Begunkov [this message]
2025-06-27 15:10 ` [RFC 11/12] io_uring/rsrc: implement dmabuf regbuf import Pavel Begunkov
2025-06-27 15:10 ` [RFC 12/12] io_uring/rw: enable dma registered buffers Pavel Begunkov
2025-07-03 14:23 ` [RFC 00/12] io_uring dmabuf read/write support Christoph Hellwig
2025-07-03 14:37 ` Christian König
2025-07-07 11:15 ` Pavel Begunkov
2025-07-07 14:48 ` Christoph Hellwig
2025-07-07 15:41 ` Pavel Begunkov
2025-07-08 9:45 ` Christoph Hellwig
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=5c11f982536aa26bd03e8d8962919a140a08e473.1751035820.git.asml.silence@gmail.com \
--to=asml.silence@gmail.com \
--cc=dw@davidwei.uk \
--cc=io-uring@vger.kernel.org \
--cc=kbusch@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=vishal1.verma@intel.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