Skip to contentSkip to conversation

mock files for advanced io_uring testing

RFC Superseded Pavel Begunkov 1 participant 3 messages May 22, 2025 io_uring

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover mock files for advanced io_uring testing

There are core io_uring features that are hard to test without having some specific file types. That includes various read/write and polling edge cases, but the problem got even more distinct with the introduction of commands and io_uring adding new features for them.

For improving testing we need a finer control over file behaviour. Add special / mock files we can create and configure. First, the file is implementing a set of io_uring commands that uses various cmd io_uring features.
Chase also suggestedd adding read/write/poll file operations and varying their characteristics: pollability, nowait support, whether the IO is async, etc., which will be added in the future.

Liburing tests:

https://github.com/isilence/liburing/tree/mock-file-tests
git https://github.com/isilence/liburing.git mock-file-tests

Pavel Begunkov (2):
io_uring/mock: add basic infra for test mock files io_uring/mock: add cmd using vectored regbufs

 init/Kconfig         |  11 +++
 io_uring/Makefile    |   1 +
 io_uring/mock_file.c | 207 +++++++++++++++++++++++++++++++++++++++++++
 io_uring/mock_file.h |  34 +++++++
 4 files changed, 253 insertions(+)
 create mode 100644 io_uring/mock_file.c
 create mode 100644 io_uring/mock_file.h
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 io_uring/mock: add basic infra for test mock files

io_uring commands provide an ioctl style interface for files to implement file specific operations. io_uring provides many features and advanced api to commands, and it's getting hard to test as it requires specific files/devices.

Add basic infrastucture for creating special mock files that will be implementing the cmd api and using various io_uring features we want to test. It'll also be useful to test some more obscure read/write/polling edge cases in the future.

---
 init/Kconfig         |  11 ++++
 io_uring/Makefile    |   1 +
 io_uring/mock_file.c | 139 +++++++++++++++++++++++++++++++++++++++++++
 io_uring/mock_file.h |  22 +++++++
 4 files changed, 173 insertions(+)
 create mode 100644 io_uring/mock_file.c
 create mode 100644 io_uring/mock_file.h
init/Kconfig +11 -0
index 63f5974b9fa6..856b37c2de8d 100644
@@ -1774,6 +1774,17 @@ config GCOV_PROFILE_URING
 	  the io_uring subsystem, hence this should only be enabled for
 	  specific test purposes.
 
+config IO_URING_MOCK_FILE
+	tristate "Enable io_uring mock files (Experimental)" if EXPERT
+	default n
+	depends on IO_URING
+	help
+	  Enable mock files for io_uring subststem testing. The ABI might
+	  still change, so it's still experimental and should only be enabled
+	  for specific test purposes.
+
+	  If unsure, say N.
+
 config ADVISE_SYSCALLS
 	bool "Enable madvise/fadvise syscalls" if EXPERT
 	default y
io_uring/Makefile +1 -0
index 11a739927a62..a630f369828d 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o opdef.o kbuf.o rsrc.o notif.o \
 					statx.o timeout.o fdinfo.o cancel.o \
 					waitid.o register.o truncate.o \
 					memmap.o alloc_cache.o
+obj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o
 obj-$(CONFIG_IO_URING_ZCRX)	+= zcrx.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/mock_file.c new file +139 -0

139 lines not on this page · raw message

io_uring/mock_file.h new file +22 -0
new file mode 100644
index 000000000000..de7318a5b1f1
@@ -0,0 +1,22 @@
+#ifndef IOU_MOCK_H
+#define IOU_MOCK_H
+
+#include <linux/types.h>
+
+struct io_uring_mock_probe {
+	__u64		features;
+	__u64		__resv[9];
+};
+
+struct io_uring_mock_create {
+	__u32		out_fd;
+	__u32		flags;
+	__u64		__resv[15];
+};
+
+enum {
+	IORING_MOCK_MGR_CMD_PROBE,
+	IORING_MOCK_MGR_CMD_CREATE,
+};
+
+#endif
\ No newline at end of file
Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/2 io_uring/mock: add cmd using vectored regbufs

In reply to Pavel Begunkov

There is a command api allowing to import vectored registered buffers, add a new mock command that uses the feature and simply copies the specified registered buffer into user space or vice versa.

---
 io_uring/mock_file.c | 70 +++++++++++++++++++++++++++++++++++++++++++-
 io_uring/mock_file.h | 12 ++++++++
 2 files changed, 81 insertions(+), 1 deletion(-)
io_uring/mock_file.c +69 -1
index e8ec0aeddbae..45fc5c9f939e 100644
@@ -9,8 +9,76 @@
 #include <linux/io_uring_types.h>
 #include "mock_file.h"
 
+#define IO_VALID_COPY_CMD_FLAGS		IORING_MOCK_COPY_FROM
+
+static int io_copy_regbuf(struct iov_iter *reg_iter, void __user *ubuf)
+{
+	size_t ret, copied = 0;
+	size_t buflen = PAGE_SIZE;
+	void *tmp_buf;
+
+	tmp_buf = kzalloc(buflen, GFP_KERNEL);
+	if (!tmp_buf)
+		return -ENOMEM;
+
+	while (iov_iter_count(reg_iter)) {
+		size_t len = min(iov_iter_count(reg_iter), buflen);
+
+		if (iov_iter_rw(reg_iter) == ITER_SOURCE) {
+			ret = copy_from_iter(tmp_buf, len, reg_iter);
+			if (ret <= 0)
+				break;
+			if (copy_to_user(ubuf, tmp_buf, ret))
+				break;
+		} else {
+			if (copy_from_user(tmp_buf, ubuf, len))
+				break;
+			ret = copy_to_iter(tmp_buf, len, reg_iter);
+			if (ret <= 0)
+				break;
+		}
+		ubuf += ret;
+		copied += ret;
+	}
+
+	kfree(tmp_buf);
+	return copied;
+}
+
+static int io_cmd_copy_regbuf(struct io_uring_cmd *cmd, unsigned int issue_flags)
+{
+	const struct io_uring_sqe *sqe = cmd->sqe;
+	const struct iovec __user *iovec;
+	unsigned flags, iovec_len;
+	struct iov_iter iter;
+	void __user *ubuf;
+	int dir, ret;
+
+	ubuf = u64_to_user_ptr(READ_ONCE(sqe->addr3));
+	iovec = u64_to_user_ptr(READ_ONCE(sqe->addr));
+	iovec_len = READ_ONCE(sqe->len);
+	flags = READ_ONCE(sqe->file_index);
+
+	if (unlikely(sqe->ioprio || sqe->__pad1))
+		return -EINVAL;
+	if (flags & ~IO_VALID_COPY_CMD_FLAGS)
+		return -EINVAL;
+
+	dir = (flags & IORING_MOCK_COPY_FROM) ? ITER_SOURCE : ITER_DEST;
+	ret = io_uring_cmd_import_fixed_vec(cmd, iovec, iovec_len, dir, &iter,
+					    issue_flags);
+	if (ret)
+		return ret;
+	ret = io_copy_regbuf(&iter, ubuf);
+	return ret ? ret : -EFAULT;
+}
+
 static int io_mock_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
 {
+	switch (cmd->cmd_op) {
+	case IORING_MOCK_CMD_COPY_REGBUF:
+		return io_cmd_copy_regbuf(cmd, issue_flags);
+	}
 	return -ENOTSUPP;
 }
 
@@ -85,7 +153,7 @@ static int io_probe_mock(struct io_uring_cmd *cmd)
 	if (!mem_is_zero(&mp, sizeof(mp)))
 		return -EINVAL;
 
-	mp.features = 0;
+	mp.features = IORING_MOCK_FEAT_COPY_CMD;
 
 	if (copy_to_user(uarg, &mp, uarg_size))
 		return -EFAULT;
io_uring/mock_file.h +12 -0
index de7318a5b1f1..9a6f47bab01e 100644
@@ -3,6 +3,10 @@
 
 #include <linux/types.h>
 
+enum {
+	IORING_MOCK_FEAT_COPY_CMD		= 1,
+};
+
 struct io_uring_mock_probe {
 	__u64		features;
 	__u64		__resv[9];
@@ -19,4 +23,12 @@ enum {
 	IORING_MOCK_MGR_CMD_CREATE,
 };
 
+enum {
+	IORING_MOCK_CMD_COPY_REGBUF,
+};
+
+enum {
+	IORING_MOCK_COPY_FROM			= 1,
+};
+
 #endif
\ No newline at end of file
Signature · 1 line
2.49.0