public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: io-uring@vger.kernel.org, Pavel Begunkov <asml.silence@gmail.com>,
	Caleb Sander Mateos <csander@purestorage.com>,
	Stefan Metzmacher <metze@samba.org>,
	Ming Lei <ming.lei@redhat.com>
Subject: [PATCH V2 12/13] selftests/io_uring: add copy_user_to_fixed() and copy_fixed_to_user() bpf_memcpy tests
Date: Tue,  6 Jan 2026 18:11:21 +0800	[thread overview]
Message-ID: <20260106101126.4064990-13-ming.lei@redhat.com> (raw)
In-Reply-To: <20260106101126.4064990-1-ming.lei@redhat.com>

Add tests for IO_BPF_BUF_FIXED buffer type to verify uring_bpf_memcpy()
kfunc works correctly with registered fixed buffers.

Changes:
- Add io_uring_unregister_buffers() to mini_liburing.h
- Add fixed buffer index tracking (src_buf_index/dst_buf_index) to test_ctx
- Add register_fixed_bufs()/unregister_fixed_bufs() helpers to manage
  buffer registration with the io_uring ring
- Update allocate_buf()/free_buf() to handle IO_BPF_BUF_FIXED type
- Add copy_user_to_fixed(): Tests USER source to FIXED destination
- Add copy_fixed_to_user(): Tests FIXED source to USER destination

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 tools/testing/selftests/io_uring/bpf_memcpy.c | 98 ++++++++++++++++++-
 1 file changed, 97 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/io_uring/bpf_memcpy.c b/tools/testing/selftests/io_uring/bpf_memcpy.c
index 0fad6d0583c3..923b9d81b508 100644
--- a/tools/testing/selftests/io_uring/bpf_memcpy.c
+++ b/tools/testing/selftests/io_uring/bpf_memcpy.c
@@ -37,6 +37,10 @@ struct test_ctx {
 	struct iovec dst_vec[MAX_VECS];
 	int src_nr_vec;
 	int dst_nr_vec;
+
+	/* Fixed buffer support */
+	__u16 src_buf_index;
+	__u16 dst_buf_index;
 };
 
 static enum iou_test_status bpf_setup(struct test_ctx *ctx)
@@ -119,6 +123,7 @@ static int allocate_buf(char **buf, size_t size, __u8 buf_type,
 
 	switch (buf_type) {
 	case IO_BPF_BUF_USER:
+	case IO_BPF_BUF_FIXED:
 		p = aligned_alloc(4096, size);
 		if (!p)
 			return -ENOMEM;
@@ -150,6 +155,7 @@ static void free_buf(char *buf, __u8 buf_type)
 	switch (buf_type) {
 	case IO_BPF_BUF_USER:
 	case IO_BPF_BUF_VEC:
+	case IO_BPF_BUF_FIXED:
 		free(buf);
 		break;
 	default:
@@ -157,8 +163,48 @@ static void free_buf(char *buf, __u8 buf_type)
 	}
 }
 
+static enum iou_test_status register_fixed_bufs(struct test_ctx *ctx)
+{
+	struct iovec iovecs[2];
+	int nr_iovecs = 0;
+	int ret;
+
+	if (ctx->src_type == IO_BPF_BUF_FIXED) {
+		ctx->src_buf_index = nr_iovecs;
+		iovecs[nr_iovecs].iov_base = ctx->src_buf;
+		iovecs[nr_iovecs].iov_len = ctx->src_buf_size;
+		nr_iovecs++;
+	}
+
+	if (ctx->dst_type == IO_BPF_BUF_FIXED) {
+		ctx->dst_buf_index = nr_iovecs;
+		iovecs[nr_iovecs].iov_base = ctx->dst_buf;
+		iovecs[nr_iovecs].iov_len = ctx->dst_buf_size;
+		nr_iovecs++;
+	}
+
+	if (nr_iovecs == 0)
+		return IOU_TEST_PASS;
+
+	ret = io_uring_register_buffers(&ctx->ring, iovecs, nr_iovecs);
+	if (ret) {
+		IOU_ERR("Failed to register buffers: %d", ret);
+		return IOU_TEST_FAIL;
+	}
+
+	return IOU_TEST_PASS;
+}
+
+static void unregister_fixed_bufs(struct test_ctx *ctx)
+{
+	if (ctx->src_type == IO_BPF_BUF_FIXED ||
+	    ctx->dst_type == IO_BPF_BUF_FIXED)
+		io_uring_unregister_buffers(&ctx->ring);
+}
+
 static enum iou_test_status allocate_bufs(struct test_ctx *ctx)
 {
+	enum iou_test_status status;
 	int ret;
 
 	ret = allocate_buf(&ctx->src_buf, ctx->src_buf_size, ctx->src_type,
@@ -181,6 +227,16 @@ static enum iou_test_status allocate_bufs(struct test_ctx *ctx)
 	memset(ctx->src_buf, TEST_PATTERN, ctx->src_buf_size);
 	memset(ctx->dst_buf, 0, ctx->dst_buf_size);
 
+	/* Register fixed buffers if needed */
+	status = register_fixed_bufs(ctx);
+	if (status != IOU_TEST_PASS) {
+		free_buf(ctx->dst_buf, ctx->dst_type);
+		ctx->dst_buf = NULL;
+		free_buf(ctx->src_buf, ctx->src_type);
+		ctx->src_buf = NULL;
+		return status;
+	}
+
 	/* Build buffer descriptors */
 	memset(ctx->descs, 0, sizeof(ctx->descs));
 	ctx->descs[0].type = ctx->src_type;
@@ -189,6 +245,10 @@ static enum iou_test_status allocate_bufs(struct test_ctx *ctx)
 	if (ctx->src_type == IO_BPF_BUF_VEC) {
 		ctx->descs[0].addr = (__u64)(uintptr_t)ctx->src_vec;
 		ctx->descs[0].len = ctx->src_nr_vec;
+	} else if (ctx->src_type == IO_BPF_BUF_FIXED) {
+		ctx->descs[0].addr = (__u64)(uintptr_t)ctx->src_buf;
+		ctx->descs[0].len = ctx->src_buf_size;
+		ctx->descs[0].buf_index = ctx->src_buf_index;
 	} else {
 		ctx->descs[0].addr = (__u64)(uintptr_t)ctx->src_buf;
 		ctx->descs[0].len = ctx->src_buf_size;
@@ -197,6 +257,10 @@ static enum iou_test_status allocate_bufs(struct test_ctx *ctx)
 	if (ctx->dst_type == IO_BPF_BUF_VEC) {
 		ctx->descs[1].addr = (__u64)(uintptr_t)ctx->dst_vec;
 		ctx->descs[1].len = ctx->dst_nr_vec;
+	} else if (ctx->dst_type == IO_BPF_BUF_FIXED) {
+		ctx->descs[1].addr = (__u64)(uintptr_t)ctx->dst_buf;
+		ctx->descs[1].len = ctx->dst_buf_size;
+		ctx->descs[1].buf_index = ctx->dst_buf_index;
 	} else {
 		ctx->descs[1].addr = (__u64)(uintptr_t)ctx->dst_buf;
 		ctx->descs[1].len = ctx->dst_buf_size;
@@ -207,6 +271,8 @@ static enum iou_test_status allocate_bufs(struct test_ctx *ctx)
 
 static void free_bufs(struct test_ctx *ctx)
 {
+	unregister_fixed_bufs(ctx);
+
 	if (ctx->src_buf) {
 		free_buf(ctx->src_buf, ctx->src_type);
 		ctx->src_buf = NULL;
@@ -332,6 +398,28 @@ static enum iou_test_status copy_user_to_vec(struct test_ctx *ctx)
 	return test_copy(ctx);
 }
 
+static enum iou_test_status copy_user_to_fixed(struct test_ctx *ctx)
+{
+	ctx->src_type = IO_BPF_BUF_USER;
+	ctx->dst_type = IO_BPF_BUF_FIXED;
+	ctx->src_buf_size = TEST_BUF_SIZE;
+	ctx->dst_buf_size = TEST_BUF_SIZE;
+	ctx->desc = "USER -> FIXED";
+
+	return test_copy(ctx);
+}
+
+static enum iou_test_status copy_fixed_to_user(struct test_ctx *ctx)
+{
+	ctx->src_type = IO_BPF_BUF_FIXED;
+	ctx->dst_type = IO_BPF_BUF_USER;
+	ctx->src_buf_size = TEST_BUF_SIZE;
+	ctx->dst_buf_size = TEST_BUF_SIZE;
+	ctx->desc = "FIXED -> USER";
+
+	return test_copy(ctx);
+}
+
 static enum iou_test_status run(void *ctx_ptr)
 {
 	struct test_ctx *ctx = ctx_ptr;
@@ -349,6 +437,14 @@ static enum iou_test_status run(void *ctx_ptr)
 	if (status != IOU_TEST_PASS)
 		return status;
 
+	status = copy_user_to_fixed(ctx);
+	if (status != IOU_TEST_PASS)
+		return status;
+
+	status = copy_fixed_to_user(ctx);
+	if (status != IOU_TEST_PASS)
+		return status;
+
 	return IOU_TEST_PASS;
 }
 
@@ -366,7 +462,7 @@ static void cleanup(void *ctx_ptr)
 
 struct iou_test bpf_memcpy_test = {
 	.name = "bpf_memcpy",
-	.description = "Test uring_bpf_memcpy() kfunc with USER, VEC, and mixed buffer types",
+	.description = "Test uring_bpf_memcpy() kfunc with USER, VEC, FIXED buffer types",
 	.setup = setup,
 	.run = run,
 	.cleanup = cleanup,
-- 
2.47.0


  parent reply	other threads:[~2026-01-06 10:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-06 10:11 [PATCH v2 0/13] io_uring: add IORING_OP_BPF for extending io_uring Ming Lei
2026-01-06 10:11 ` [PATCH V2 01/13] io_uring: make io_import_fixed() global Ming Lei
2026-01-06 10:11 ` [PATCH V2 02/13] io_uring: refactor io_prep_reg_iovec() for BPF kfunc use Ming Lei
2026-01-06 10:11 ` [PATCH V2 03/13] io_uring: refactor io_import_reg_vec() " Ming Lei
2026-01-06 10:11 ` [PATCH V2 04/13] io_uring: prepare for extending io_uring with bpf Ming Lei
2026-01-06 10:11 ` [PATCH V2 05/13] io_uring: bpf: extend io_uring with bpf struct_ops Ming Lei
2026-01-06 10:11 ` [PATCH V2 06/13] io_uring: bpf: implement struct_ops registration Ming Lei
2026-01-06 10:11 ` [PATCH V2 07/13] io_uring: bpf: add BPF buffer descriptor for IORING_OP_BPF Ming Lei
2026-01-06 10:11 ` [PATCH V2 08/13] io_uring: bpf: add uring_bpf_memcpy() kfunc Ming Lei
2026-01-06 10:11 ` [PATCH V2 09/13] selftests/io_uring: update mini liburing Ming Lei
2026-01-06 10:11 ` [PATCH V2 10/13] selftests/io_uring: add BPF struct_ops and kfunc tests Ming Lei
2026-01-06 10:11 ` [PATCH V2 11/13] selftests/io_uring: add bpf_memcpy selftest for uring_bpf_memcpy() kfunc Ming Lei
2026-01-06 10:11 ` Ming Lei [this message]
2026-01-06 10:11 ` [PATCH V2 13/13] selftests/io_uring: add copy_user_to_reg_vec() and copy_reg_vec_to_user() bpf_memcpy tests Ming Lei

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=20260106101126.4064990-13-ming.lei@redhat.com \
    --to=ming.lei@redhat.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=csander@purestorage.com \
    --cc=io-uring@vger.kernel.org \
    --cc=metze@samba.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