Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Suggested-by: chase xd <sl1589472800@gmail.com>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/93f21b0af58c1367a2b22635d5a7d694ad0272fc.1750599274.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Pavel Begunkov and committed by
Jens Axboe
3a0ae385 94b20309

+183
+1
MAINTAINERS
··· 12679 12679 F: include/linux/io_uring_types.h 12680 12680 F: include/trace/events/io_uring.h 12681 12681 F: include/uapi/linux/io_uring.h 12682 + F: include/uapi/linux/io_uring/ 12682 12683 F: io_uring/ 12683 12684 12684 12685 IPMI SUBSYSTEM
+22
include/uapi/linux/io_uring/mock_file.h
··· 1 + #ifndef LINUX_IO_URING_MOCK_FILE_H 2 + #define LINUX_IO_URING_MOCK_FILE_H 3 + 4 + #include <linux/types.h> 5 + 6 + struct io_uring_mock_probe { 7 + __u64 features; 8 + __u64 __resv[9]; 9 + }; 10 + 11 + struct io_uring_mock_create { 12 + __u32 out_fd; 13 + __u32 flags; 14 + __u64 __resv[15]; 15 + }; 16 + 17 + enum { 18 + IORING_MOCK_MGR_CMD_PROBE, 19 + IORING_MOCK_MGR_CMD_CREATE, 20 + }; 21 + 22 + #endif
+11
init/Kconfig
··· 1801 1801 the io_uring subsystem, hence this should only be enabled for 1802 1802 specific test purposes. 1803 1803 1804 + config IO_URING_MOCK_FILE 1805 + tristate "Enable io_uring mock files (Experimental)" if EXPERT 1806 + default n 1807 + depends on IO_URING 1808 + help 1809 + Enable mock files for io_uring subststem testing. The ABI might 1810 + still change, so it's still experimental and should only be enabled 1811 + for specific test purposes. 1812 + 1813 + If unsure, say N. 1814 + 1804 1815 config ADVISE_SYSCALLS 1805 1816 bool "Enable madvise/fadvise syscalls" if EXPERT 1806 1817 default y
+1
io_uring/Makefile
··· 21 21 obj-$(CONFIG_NET_RX_BUSY_POLL) += napi.o 22 22 obj-$(CONFIG_NET) += net.o cmd_net.o 23 23 obj-$(CONFIG_PROC_FS) += fdinfo.o 24 + obj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o
+148
io_uring/mock_file.c
··· 1 + #include <linux/device.h> 2 + #include <linux/init.h> 3 + #include <linux/kernel.h> 4 + #include <linux/miscdevice.h> 5 + #include <linux/module.h> 6 + #include <linux/anon_inodes.h> 7 + 8 + #include <linux/io_uring/cmd.h> 9 + #include <linux/io_uring_types.h> 10 + #include <uapi/linux/io_uring/mock_file.h> 11 + 12 + static int io_mock_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) 13 + { 14 + return -ENOTSUPP; 15 + } 16 + 17 + static const struct file_operations io_mock_fops = { 18 + .owner = THIS_MODULE, 19 + .uring_cmd = io_mock_cmd, 20 + }; 21 + 22 + static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags) 23 + { 24 + const struct io_uring_sqe *sqe = cmd->sqe; 25 + struct io_uring_mock_create mc, __user *uarg; 26 + struct file *file = NULL; 27 + size_t uarg_size; 28 + int fd, ret; 29 + 30 + /* 31 + * It's a testing only driver that allows exercising edge cases 32 + * that wouldn't be possible to hit otherwise. 33 + */ 34 + add_taint(TAINT_TEST, LOCKDEP_STILL_OK); 35 + 36 + uarg = u64_to_user_ptr(READ_ONCE(sqe->addr)); 37 + uarg_size = READ_ONCE(sqe->len); 38 + 39 + if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index) 40 + return -EINVAL; 41 + if (uarg_size != sizeof(mc)) 42 + return -EINVAL; 43 + 44 + memset(&mc, 0, sizeof(mc)); 45 + if (copy_from_user(&mc, uarg, uarg_size)) 46 + return -EFAULT; 47 + if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)) || mc.flags) 48 + return -EINVAL; 49 + 50 + fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 51 + if (fd < 0) 52 + return fd; 53 + 54 + file = anon_inode_create_getfile("[io_uring_mock]", &io_mock_fops, 55 + NULL, O_RDWR | O_CLOEXEC, NULL); 56 + if (IS_ERR(file)) { 57 + ret = PTR_ERR(file); 58 + goto fail; 59 + } 60 + 61 + mc.out_fd = fd; 62 + if (copy_to_user(uarg, &mc, uarg_size)) { 63 + fput(file); 64 + ret = -EFAULT; 65 + goto fail; 66 + } 67 + 68 + fd_install(fd, file); 69 + return 0; 70 + fail: 71 + put_unused_fd(fd); 72 + return ret; 73 + } 74 + 75 + static int io_probe_mock(struct io_uring_cmd *cmd) 76 + { 77 + const struct io_uring_sqe *sqe = cmd->sqe; 78 + struct io_uring_mock_probe mp, __user *uarg; 79 + size_t uarg_size; 80 + 81 + uarg = u64_to_user_ptr(READ_ONCE(sqe->addr)); 82 + uarg_size = READ_ONCE(sqe->len); 83 + 84 + if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index || 85 + uarg_size != sizeof(mp)) 86 + return -EINVAL; 87 + 88 + memset(&mp, 0, sizeof(mp)); 89 + if (copy_from_user(&mp, uarg, uarg_size)) 90 + return -EFAULT; 91 + if (!mem_is_zero(&mp, sizeof(mp))) 92 + return -EINVAL; 93 + 94 + mp.features = 0; 95 + 96 + if (copy_to_user(uarg, &mp, uarg_size)) 97 + return -EFAULT; 98 + return 0; 99 + } 100 + 101 + static int iou_mock_mgr_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) 102 + { 103 + if (!capable(CAP_SYS_ADMIN)) 104 + return -EPERM; 105 + 106 + switch (cmd->cmd_op) { 107 + case IORING_MOCK_MGR_CMD_PROBE: 108 + return io_probe_mock(cmd); 109 + case IORING_MOCK_MGR_CMD_CREATE: 110 + return io_create_mock_file(cmd, issue_flags); 111 + } 112 + return -EOPNOTSUPP; 113 + } 114 + 115 + static const struct file_operations iou_mock_dev_fops = { 116 + .owner = THIS_MODULE, 117 + .uring_cmd = iou_mock_mgr_cmd, 118 + }; 119 + 120 + static struct miscdevice iou_mock_miscdev = { 121 + .minor = MISC_DYNAMIC_MINOR, 122 + .name = "io_uring_mock", 123 + .fops = &iou_mock_dev_fops, 124 + }; 125 + 126 + static int __init io_mock_init(void) 127 + { 128 + int ret; 129 + 130 + ret = misc_register(&iou_mock_miscdev); 131 + if (ret < 0) { 132 + pr_err("Could not initialize io_uring mock device\n"); 133 + return ret; 134 + } 135 + return 0; 136 + } 137 + 138 + static void __exit io_mock_exit(void) 139 + { 140 + misc_deregister(&iou_mock_miscdev); 141 + } 142 + 143 + module_init(io_mock_init) 144 + module_exit(io_mock_exit) 145 + 146 + MODULE_AUTHOR("Pavel Begunkov <asml.silence@gmail.com>"); 147 + MODULE_DESCRIPTION("io_uring mock file"); 148 + MODULE_LICENSE("GPL");