From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com
Subject: [PATCH liburing 2/2] examples/zcrx: udmabuf backed areas
Date: Tue, 6 May 2025 11:21:51 +0100 [thread overview]
Message-ID: <73c4937901b2d874f651ff0fa1d6b0cf632f3f01.1746526793.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1746526793.git.asml.silence@gmail.com>
Add an example of how to create dmabuf backed area. For that we use
udmabuf and mmap it into user space, however in more realistic scenarios
won't have direct access to the memory and will need to use dmabuf
provider specific api, e.g. OpenCl.
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
examples/zcrx.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/examples/zcrx.c b/examples/zcrx.c
index 6b06e4fa..c5e1b42f 100644
--- a/examples/zcrx.c
+++ b/examples/zcrx.c
@@ -37,6 +37,10 @@
#include <sys/wait.h>
#include <linux/mman.h>
+#include <linux/memfd.h>
+#include <linux/dma-buf.h>
+#include <linux/udmabuf.h>
+
#include "liburing.h"
#include "helpers.h"
@@ -56,6 +60,7 @@ static long page_size;
enum {
AREA_TYPE_NORMAL,
AREA_TYPE_HUGE_PAGES,
+ AREA_TYPE_DMABUF,
__AREA_TYPE_MAX,
};
@@ -83,6 +88,9 @@ static bool stop;
static size_t received;
static __u32 zcrx_id;
+static int dmabuf_fd;
+static int memfd;
+
static inline size_t get_refill_ring_size(unsigned int rq_entries)
{
ring_size = rq_entries * sizeof(struct io_uring_zcrx_rqe);
@@ -91,11 +99,58 @@ static inline size_t get_refill_ring_size(unsigned int rq_entries)
return T_ALIGN_UP(ring_size, page_size);
}
+static void zcrx_populate_area_udmabuf(struct io_uring_zcrx_area_reg *area_reg)
+{
+ struct udmabuf_create create;
+ int ret, devfd;
+
+ devfd = open("/dev/udmabuf", O_RDWR);
+ if (devfd < 0)
+ t_error(1, devfd, "Failed to open udmabuf dev");
+
+ memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING);
+ if (memfd < 0)
+ t_error(1, memfd, "Failed to open udmabuf dev");
+
+ ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK);
+ if (ret < 0)
+ t_error(1, 0, "Failed to set seals");
+
+ ret = ftruncate(memfd, AREA_SIZE);
+ if (ret == -1)
+ t_error(1, 0, "Failed to resize udmabuf");
+
+ memset(&create, 0, sizeof(create));
+ create.memfd = memfd;
+ create.offset = 0;
+ create.size = AREA_SIZE;
+ dmabuf_fd = ioctl(devfd, UDMABUF_CREATE, &create);
+ if (dmabuf_fd < 0)
+ t_error(1, dmabuf_fd, "Failed to create udmabuf");
+
+ area_ptr = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
+ dmabuf_fd, 0);
+ if (area_ptr == MAP_FAILED)
+ t_error(1, 0, "Failed to mmap udmabuf");
+
+ memset(area_reg, 0, sizeof(*area_reg));
+ area_reg->addr = 0; /* offset into dmabuf */
+ area_reg->len = AREA_SIZE;
+ area_reg->flags |= IORING_ZCRX_AREA_DMABUF;
+ area_reg->dmabuf_fd = dmabuf_fd;
+
+ close(devfd);
+}
+
static void zcrx_populate_area(struct io_uring_zcrx_area_reg *area_reg)
{
unsigned flags = MAP_PRIVATE | MAP_ANONYMOUS;
unsigned prot = PROT_READ | PROT_WRITE;
+ if (cfg_area_type == AREA_TYPE_DMABUF) {
+ zcrx_populate_area_udmabuf(area_reg);
+ return;
+ }
if (cfg_area_type == AREA_TYPE_NORMAL) {
area_ptr = mmap(NULL, AREA_SIZE, prot,
flags, 0, 0);
--
2.48.1
next prev parent reply other threads:[~2025-05-06 10:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 10:21 [PATCH liburing 0/2] add example for using dmabuf backed zcrx Pavel Begunkov
2025-05-06 10:21 ` [PATCH liburing 1/2] Update io_uring.h with zcrx dmabuf interface Pavel Begunkov
2025-05-06 10:21 ` Pavel Begunkov [this message]
2025-05-06 13:54 ` [PATCH liburing 0/2] add example for using dmabuf backed zcrx Jens Axboe
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=73c4937901b2d874f651ff0fa1d6b0cf632f3f01.1746526793.git.asml.silence@gmail.com \
--to=asml.silence@gmail.com \
--cc=io-uring@vger.kernel.org \
/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