Skip to contentSkip to conversation

add example for using dmabuf backed zcrx

PATCH Applied Pavel Begunkov 2 participants 4 messages May 6, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

cover add example for using dmabuf backed zcrx

Update the headers and add a option to create a dmabuf backed area in the zcrx example.

Pavel Begunkov (2):
Update io_uring.h with zcrx dmabuf interface
examples/zcrx: udmabuf backed areas

 examples/zcrx.c                 | 55 +++++++++++++++++++++++++++++++++
 src/include/liburing/io_uring.h |  6 +++-
 2 files changed, 60 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/2 Update io_uring.h with zcrx dmabuf interface

---
 src/include/liburing/io_uring.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/include/liburing/io_uring.h +5 -1
index 572ff59f..0baa3272 100644
@@ -955,12 +955,16 @@ struct io_uring_zcrx_offsets {
 	__u64	__resv[2];
 };
 
+enum io_uring_zcrx_area_flags {
+	IORING_ZCRX_AREA_DMABUF		= 1,
+};
+
 struct io_uring_zcrx_area_reg {
 	__u64	addr;
 	__u64	len;
 	__u64	rq_area_token;
 	__u32	flags;
-	__u32	__resv1;
+	__u32	dmabuf_fd;
 	__u64	__resv2[2];
 };
 
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/2 examples/zcrx: udmabuf backed areas

In reply to Pavel Begunkov

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.

---
 examples/zcrx.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
examples/zcrx.c +55 -0
index 6b06e4fa..c5e1b42f 100644
@@ -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);
Signature · 1 line
2.48.1
maintainer Applied
#3 lore raw
In reply to Pavel Begunkov
Quoted 8 lines · Pavel Begunkov

Update the headers and add a option to create a dmabuf backed area in the zcrx example.

Pavel Begunkov (2):
Update io_uring.h with zcrx dmabuf interface
examples/zcrx: udmabuf backed areas

[...]

Applied, thanks!

[1/2] Update io_uring.h with zcrx dmabuf interface

      commit: 425f6d5c9b9371519990935e5412568fe736cc0f
[2/2] examples/zcrx: udmabuf backed areas
      commit: 93d3a7a70b4a6d6ade45a02915dda81c2cdf9810

Best regards,

Signature · 1 line
Jens Axboe