Skip to contentSkip to conversation

io_uring/mock: add basic infra for test mock files

PATCH Applied Pavel Begunkov 2 participants 8 messages Jun 30, 2025 – Jul 2, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

v5 cover 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, which was initially suggested by Chase.

v5: moves headers to uapi/
taint the kernel
v4: require CAP_ADMIN and limit to KASAN builds
v3: fix memleak, + release fop callback
v2: add rw support with basic options
implement features not as bitmask but sequence number

Pavel Begunkov (6):
io_uring/mock: add basic infra for test mock files io_uring/mock: add cmd using vectored regbufs io_uring/mock: add sync read/write io_uring/mock: allow to choose FMODE_NOWAIT io_uring/mock: support for async read/write io_uring/mock: add trivial poll handler

 MAINTAINERS                             |   1 +
 include/uapi/linux/io_uring/mock_file.h |  47 +++
 init/Kconfig                            |  11 +
 io_uring/Makefile                       |   1 +
 io_uring/mock_file.c                    | 363 ++++++++++++++++++++++++
 5 files changed, 423 insertions(+)
 create mode 100644 include/uapi/linux/io_uring/mock_file.h
 create mode 100644 io_uring/mock_file.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH v5 1/6 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.

---
 MAINTAINERS                             |   1 +
 include/uapi/linux/io_uring/mock_file.h |  22 ++++
 init/Kconfig                            |  11 ++
 io_uring/Makefile                       |   1 +
 io_uring/mock_file.c                    | 148 ++++++++++++++++++++++++
 5 files changed, 183 insertions(+)
 create mode 100644 include/uapi/linux/io_uring/mock_file.h
 create mode 100644 io_uring/mock_file.c
MAINTAINERS +1 -0
index a92290fffa16..088348e3b3ae 100644
@@ -12679,6 +12679,7 @@ F:	include/linux/io_uring.h
 F:	include/linux/io_uring_types.h
 F:	include/trace/events/io_uring.h
 F:	include/uapi/linux/io_uring.h
+F:	include/uapi/linux/io_uring/
 F:	io_uring/
 
 IPMI SUBSYSTEM
include/uapi/linux/io_uring/mock_file.h new file +22 -0
new file mode 100644
index 000000000000..a44273fd526d
@@ -0,0 +1,22 @@
+#ifndef LINUX_IO_URING_MOCK_FILE_H
+#define LINUX_IO_URING_MOCK_FILE_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
init/Kconfig +11 -0
index af4c2f085455..c40a7c65fb4c 100644
@@ -1801,6 +1801,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 d97c6b51d584..b3f1bd492804 100644
@@ -21,3 +21,4 @@ obj-$(CONFIG_EPOLL)		+= epoll.o
 obj-$(CONFIG_NET_RX_BUSY_POLL)	+= napi.o
 obj-$(CONFIG_NET) += net.o cmd_net.o
 obj-$(CONFIG_PROC_FS) += fdinfo.o
+obj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o
io_uring/mock_file.c new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw

PATCH v5 2/6 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.

---
 include/uapi/linux/io_uring/mock_file.h | 14 +++++
 io_uring/mock_file.c                    | 70 ++++++++++++++++++++++++-
 2 files changed, 83 insertions(+), 1 deletion(-)
include/uapi/linux/io_uring/mock_file.h +14 -0
index a44273fd526d..73aca477d5c8 100644
@@ -3,6 +3,12 @@
 
 #include <linux/types.h>
 
+enum {
+	IORING_MOCK_FEAT_CMD_COPY,
+
+	IORING_MOCK_FEAT_END,
+};
+
 struct io_uring_mock_probe {
 	__u64		features;
 	__u64		__resv[9];
@@ -19,4 +25,12 @@ enum {
 	IORING_MOCK_MGR_CMD_CREATE,
 };
 
+enum {
+	IORING_MOCK_CMD_COPY_REGBUF,
+};
+
+enum {
+	IORING_MOCK_COPY_FROM			= 1,
+};
+
 #endif
io_uring/mock_file.c +69 -1

84 lines not on this page · raw message

Signature · 1 line
2.49.0
#3 lore raw

PATCH v5 3/6 io_uring/mock: add sync read/write

In reply to Pavel Begunkov

Add support for synchronous zero read/write for mock files.

---
 include/uapi/linux/io_uring/mock_file.h |  4 +-
 io_uring/mock_file.c                    | 67 +++++++++++++++++++++++--
 2 files changed, 65 insertions(+), 6 deletions(-)
include/uapi/linux/io_uring/mock_file.h +3 -1
index 73aca477d5c8..de27295bb365 100644
@@ -5,6 +5,7 @@
 
 enum {
 	IORING_MOCK_FEAT_CMD_COPY,
+	IORING_MOCK_FEAT_RW_ZERO,
 
 	IORING_MOCK_FEAT_END,
 };
@@ -17,7 +18,8 @@ struct io_uring_mock_probe {
 struct io_uring_mock_create {
 	__u32		out_fd;
 	__u32		flags;
-	__u64		__resv[15];
+	__u64		file_size;
+	__u64		__resv[14];
 };
 
 enum {
io_uring/mock_file.c +62 -5

111 lines not on this page · raw message

Signature · 1 line
2.49.0
#4 lore raw

PATCH v5 4/6 io_uring/mock: allow to choose FMODE_NOWAIT

In reply to Pavel Begunkov

Add an option to choose whether the file supports FMODE_NOWAIT, that changes the execution path io_uring request takes.

---
 include/uapi/linux/io_uring/mock_file.h | 5 +++++
 io_uring/mock_file.c                    | 8 +++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
include/uapi/linux/io_uring/mock_file.h +5 -0
index de27295bb365..125949d2b5ce 100644
@@ -6,6 +6,7 @@
 enum {
 	IORING_MOCK_FEAT_CMD_COPY,
 	IORING_MOCK_FEAT_RW_ZERO,
+	IORING_MOCK_FEAT_RW_NOWAIT,
 
 	IORING_MOCK_FEAT_END,
 };
@@ -15,6 +16,10 @@ struct io_uring_mock_probe {
 	__u64		__resv[9];
 };
 
+enum {
+	IORING_MOCK_CREATE_F_SUPPORT_NOWAIT			= 1,
+};
+
 struct io_uring_mock_create {
 	__u32		out_fd;
 	__u32		flags;
io_uring/mock_file.c +7 -1
index 90160ccb50f0..0eb1d3bd6368 100644
@@ -131,6 +131,8 @@ static const struct file_operations io_mock_fops = {
 	.llseek		= io_mock_llseek,
 };
 
+#define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
+
 static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)
 {
 	const struct io_uring_sqe *sqe = cmd->sqe;
@@ -157,7 +159,9 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
 	memset(&mc, 0, sizeof(mc));
 	if (copy_from_user(&mc, uarg, uarg_size))
 		return -EFAULT;
-	if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)) || mc.flags)
+	if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)))
+		return -EINVAL;
+	if (mc.flags & ~IO_VALID_CREATE_FLAGS)
 		return -EINVAL;
 	if (mc.file_size > SZ_1G)
 		return -EINVAL;
@@ -180,6 +184,8 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
 	file->f_mode |= FMODE_READ | FMODE_CAN_READ |
 			FMODE_WRITE | FMODE_CAN_WRITE |
 			FMODE_LSEEK;
+	if (mc.flags & IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
+		file->f_mode |= FMODE_NOWAIT;
 
 	mc.out_fd = fd;
 	if (copy_to_user(uarg, &mc, uarg_size)) {
Signature · 1 line
2.49.0
#5 lore raw

PATCH v5 5/6 io_uring/mock: support for async read/write

In reply to Pavel Begunkov

Let the user to specify a delay to read/write request. io_uring will start a timer, return -EIOCBQUEUED and complete the request asynchronously after the delay pass.

---
 include/uapi/linux/io_uring/mock_file.h |  4 +-
 io_uring/mock_file.c                    | 59 +++++++++++++++++++++++--
 2 files changed, 58 insertions(+), 5 deletions(-)
include/uapi/linux/io_uring/mock_file.h +3 -1
index 125949d2b5ce..c8fa77e39c68 100644
@@ -7,6 +7,7 @@ enum {
 	IORING_MOCK_FEAT_CMD_COPY,
 	IORING_MOCK_FEAT_RW_ZERO,
 	IORING_MOCK_FEAT_RW_NOWAIT,
+	IORING_MOCK_FEAT_RW_ASYNC,
 
 	IORING_MOCK_FEAT_END,
 };
@@ -24,7 +25,8 @@ struct io_uring_mock_create {
 	__u32		out_fd;
 	__u32		flags;
 	__u64		file_size;
-	__u64		__resv[14];
+	__u64		rw_delay_ns;
+	__u64		__resv[13];
 };
 
 enum {
io_uring/mock_file.c +55 -4

102 lines not on this page · raw message

Signature · 1 line
2.49.0
#6 lore raw

PATCH v5 6/6 io_uring/mock: add trivial poll handler

In reply to Pavel Begunkov

Add a flag that enables polling on the mock file. For now it's trivially says that there is always data available, it'll be extended in the future.

---
 include/uapi/linux/io_uring/mock_file.h |  2 ++
 io_uring/mock_file.c                    | 37 +++++++++++++++++++++++--
 2 files changed, 37 insertions(+), 2 deletions(-)
include/uapi/linux/io_uring/mock_file.h +2 -0
index c8fa77e39c68..debeee8e4527 100644
@@ -8,6 +8,7 @@ enum {
 	IORING_MOCK_FEAT_RW_ZERO,
 	IORING_MOCK_FEAT_RW_NOWAIT,
 	IORING_MOCK_FEAT_RW_ASYNC,
+	IORING_MOCK_FEAT_POLL,
 
 	IORING_MOCK_FEAT_END,
 };
@@ -19,6 +20,7 @@ struct io_uring_mock_probe {
 
 enum {
 	IORING_MOCK_CREATE_F_SUPPORT_NOWAIT			= 1,
+	IORING_MOCK_CREATE_F_POLL				= 2,
 };
 
 struct io_uring_mock_create {
io_uring/mock_file.c +35 -2
index ed6a5505763e..45d3735b2708 100644
@@ -6,6 +6,7 @@
 #include <linux/anon_inodes.h>
 #include <linux/ktime.h>
 #include <linux/hrtimer.h>
+#include <linux/poll.h>
 
 #include <linux/io_uring/cmd.h>
 #include <linux/io_uring_types.h>
@@ -20,6 +21,8 @@ struct io_mock_iocb {
 struct io_mock_file {
 	size_t			size;
 	u64			rw_delay_ns;
+	bool			pollable;
+	struct wait_queue_head	poll_wq;
 };
 
 #define IO_VALID_COPY_CMD_FLAGS		IORING_MOCK_COPY_FROM
@@ -161,6 +164,18 @@ static loff_t io_mock_llseek(struct file *file, loff_t offset, int whence)
 	return fixed_size_llseek(file, offset, whence, mf->size);
 }
 
+static __poll_t io_mock_poll(struct file *file, struct poll_table_struct *pt)
+{
+	struct io_mock_file *mf = file->private_data;
+	__poll_t mask = 0;
+
+	poll_wait(file, &mf->poll_wq, pt);
+
+	mask |= EPOLLOUT | EPOLLWRNORM;
+	mask |= EPOLLIN | EPOLLRDNORM;
+	return mask;
+}
+
 static int io_mock_release(struct inode *inode, struct file *file)
 {
 	struct io_mock_file *mf = file->private_data;
@@ -178,10 +193,22 @@ static const struct file_operations io_mock_fops = {
 	.llseek		= io_mock_llseek,
 };
 
-#define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
+static const struct file_operations io_mock_poll_fops = {
+	.owner		= THIS_MODULE,
+	.release	= io_mock_release,
+	.uring_cmd	= io_mock_cmd,
+	.read_iter	= io_mock_read_iter,
+	.write_iter	= io_mock_write_iter,
+	.llseek		= io_mock_llseek,
+	.poll		= io_mock_poll,
+};
+
+#define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT | \
+				IORING_MOCK_CREATE_F_POLL)
 
 static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)
 {
+	const struct file_operations *fops = &io_mock_fops;
 	const struct io_uring_sqe *sqe = cmd->sqe;
 	struct io_uring_mock_create mc, __user *uarg;
 	struct io_mock_file *mf = NULL;
@@ -223,9 +250,15 @@ static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flag
 	if (fd < 0)
 		goto fail;
 
+	init_waitqueue_head(&mf->poll_wq);
 	mf->size = mc.file_size;
 	mf->rw_delay_ns = mc.rw_delay_ns;
-	file = anon_inode_create_getfile("[io_uring_mock]", &io_mock_fops,
+	if (mc.flags & IORING_MOCK_CREATE_F_POLL) {
+		fops = &io_mock_poll_fops;
+		mf->pollable = true;
+	}
+
+	file = anon_inode_create_getfile("[io_uring_mock]", fops,
 					 mf, O_RDWR | O_CLOEXEC, NULL);
 	if (IS_ERR(file)) {
 		ret = PTR_ERR(file);
Signature · 1 line
2.49.0
maintainer Applied
#7 lore raw
In reply to Pavel Begunkov
Quoted 11 lines · Pavel Begunkov

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, which was initially suggested by Chase.

[...]

Applied, thanks!

[1/6] io_uring/mock: add basic infra for test mock files
(no commit info)
[2/6] io_uring/mock: add cmd using vectored regbufs
(no commit info)
[3/6] io_uring/mock: add sync read/write
(no commit info)
[4/6] io_uring/mock: allow to choose FMODE_NOWAIT
(no commit info)
[5/6] io_uring/mock: support for async read/write
(no commit info)
[6/6] io_uring/mock: add trivial poll handler
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe