mock_file.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/device.h>
  3. #include <linux/init.h>
  4. #include <linux/kernel.h>
  5. #include <linux/miscdevice.h>
  6. #include <linux/module.h>
  7. #include <linux/anon_inodes.h>
  8. #include <linux/ktime.h>
  9. #include <linux/hrtimer.h>
  10. #include <linux/poll.h>
  11. #include <linux/io_uring/cmd.h>
  12. #include <linux/io_uring_types.h>
  13. #include <uapi/linux/io_uring/mock_file.h>
  14. struct io_mock_iocb {
  15. struct kiocb *iocb;
  16. struct hrtimer timer;
  17. int res;
  18. };
  19. struct io_mock_file {
  20. size_t size;
  21. u64 rw_delay_ns;
  22. bool pollable;
  23. struct wait_queue_head poll_wq;
  24. };
  25. #define IO_VALID_COPY_CMD_FLAGS IORING_MOCK_COPY_FROM
  26. static int io_copy_regbuf(struct iov_iter *reg_iter, void __user *ubuf)
  27. {
  28. size_t ret, copied = 0;
  29. size_t buflen = PAGE_SIZE;
  30. void *tmp_buf;
  31. tmp_buf = kzalloc(buflen, GFP_KERNEL);
  32. if (!tmp_buf)
  33. return -ENOMEM;
  34. while (iov_iter_count(reg_iter)) {
  35. size_t len = min(iov_iter_count(reg_iter), buflen);
  36. if (iov_iter_rw(reg_iter) == ITER_SOURCE) {
  37. ret = copy_from_iter(tmp_buf, len, reg_iter);
  38. if (ret <= 0)
  39. break;
  40. if (copy_to_user(ubuf, tmp_buf, ret))
  41. break;
  42. } else {
  43. if (copy_from_user(tmp_buf, ubuf, len))
  44. break;
  45. ret = copy_to_iter(tmp_buf, len, reg_iter);
  46. if (ret <= 0)
  47. break;
  48. }
  49. ubuf += ret;
  50. copied += ret;
  51. }
  52. kfree(tmp_buf);
  53. return copied;
  54. }
  55. static int io_cmd_copy_regbuf(struct io_uring_cmd *cmd, unsigned int issue_flags)
  56. {
  57. const struct io_uring_sqe *sqe = cmd->sqe;
  58. const struct iovec __user *iovec;
  59. unsigned flags, iovec_len;
  60. struct iov_iter iter;
  61. void __user *ubuf;
  62. int dir, ret;
  63. ubuf = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  64. iovec = u64_to_user_ptr(READ_ONCE(sqe->addr));
  65. iovec_len = READ_ONCE(sqe->len);
  66. flags = READ_ONCE(sqe->file_index);
  67. if (unlikely(sqe->ioprio || sqe->__pad1))
  68. return -EINVAL;
  69. if (flags & ~IO_VALID_COPY_CMD_FLAGS)
  70. return -EINVAL;
  71. dir = (flags & IORING_MOCK_COPY_FROM) ? ITER_SOURCE : ITER_DEST;
  72. ret = io_uring_cmd_import_fixed_vec(cmd, iovec, iovec_len, dir, &iter,
  73. issue_flags);
  74. if (ret)
  75. return ret;
  76. ret = io_copy_regbuf(&iter, ubuf);
  77. return ret ? ret : -EFAULT;
  78. }
  79. static int io_mock_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
  80. {
  81. switch (cmd->cmd_op) {
  82. case IORING_MOCK_CMD_COPY_REGBUF:
  83. return io_cmd_copy_regbuf(cmd, issue_flags);
  84. }
  85. return -ENOTSUPP;
  86. }
  87. static enum hrtimer_restart io_mock_rw_timer_expired(struct hrtimer *timer)
  88. {
  89. struct io_mock_iocb *mio = container_of(timer, struct io_mock_iocb, timer);
  90. struct kiocb *iocb = mio->iocb;
  91. WRITE_ONCE(iocb->private, NULL);
  92. iocb->ki_complete(iocb, mio->res);
  93. kfree(mio);
  94. return HRTIMER_NORESTART;
  95. }
  96. static ssize_t io_mock_delay_rw(struct kiocb *iocb, size_t len)
  97. {
  98. struct io_mock_file *mf = iocb->ki_filp->private_data;
  99. struct io_mock_iocb *mio;
  100. mio = kzalloc_obj(*mio);
  101. if (!mio)
  102. return -ENOMEM;
  103. mio->iocb = iocb;
  104. mio->res = len;
  105. hrtimer_setup(&mio->timer, io_mock_rw_timer_expired,
  106. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  107. hrtimer_start(&mio->timer, ns_to_ktime(mf->rw_delay_ns),
  108. HRTIMER_MODE_REL);
  109. return -EIOCBQUEUED;
  110. }
  111. static ssize_t io_mock_read_iter(struct kiocb *iocb, struct iov_iter *to)
  112. {
  113. struct io_mock_file *mf = iocb->ki_filp->private_data;
  114. size_t len = iov_iter_count(to);
  115. size_t nr_zeroed;
  116. if (iocb->ki_pos + len > mf->size)
  117. return -EINVAL;
  118. nr_zeroed = iov_iter_zero(len, to);
  119. if (!mf->rw_delay_ns || nr_zeroed != len)
  120. return nr_zeroed;
  121. return io_mock_delay_rw(iocb, len);
  122. }
  123. static ssize_t io_mock_write_iter(struct kiocb *iocb, struct iov_iter *from)
  124. {
  125. struct io_mock_file *mf = iocb->ki_filp->private_data;
  126. size_t len = iov_iter_count(from);
  127. if (iocb->ki_pos + len > mf->size)
  128. return -EINVAL;
  129. if (!mf->rw_delay_ns) {
  130. iov_iter_advance(from, len);
  131. return len;
  132. }
  133. return io_mock_delay_rw(iocb, len);
  134. }
  135. static loff_t io_mock_llseek(struct file *file, loff_t offset, int whence)
  136. {
  137. struct io_mock_file *mf = file->private_data;
  138. return fixed_size_llseek(file, offset, whence, mf->size);
  139. }
  140. static __poll_t io_mock_poll(struct file *file, struct poll_table_struct *pt)
  141. {
  142. struct io_mock_file *mf = file->private_data;
  143. __poll_t mask = 0;
  144. poll_wait(file, &mf->poll_wq, pt);
  145. mask |= EPOLLOUT | EPOLLWRNORM;
  146. mask |= EPOLLIN | EPOLLRDNORM;
  147. return mask;
  148. }
  149. static int io_mock_release(struct inode *inode, struct file *file)
  150. {
  151. struct io_mock_file *mf = file->private_data;
  152. kfree(mf);
  153. return 0;
  154. }
  155. static const struct file_operations io_mock_fops = {
  156. .owner = THIS_MODULE,
  157. .release = io_mock_release,
  158. .uring_cmd = io_mock_cmd,
  159. .read_iter = io_mock_read_iter,
  160. .write_iter = io_mock_write_iter,
  161. .llseek = io_mock_llseek,
  162. };
  163. static const struct file_operations io_mock_poll_fops = {
  164. .owner = THIS_MODULE,
  165. .release = io_mock_release,
  166. .uring_cmd = io_mock_cmd,
  167. .read_iter = io_mock_read_iter,
  168. .write_iter = io_mock_write_iter,
  169. .llseek = io_mock_llseek,
  170. .poll = io_mock_poll,
  171. };
  172. #define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT | \
  173. IORING_MOCK_CREATE_F_POLL)
  174. static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)
  175. {
  176. const struct file_operations *fops = &io_mock_fops;
  177. const struct io_uring_sqe *sqe = cmd->sqe;
  178. struct io_uring_mock_create mc, __user *uarg;
  179. struct file *file;
  180. struct io_mock_file *mf __free(kfree) = NULL;
  181. size_t uarg_size;
  182. /*
  183. * It's a testing only driver that allows exercising edge cases
  184. * that wouldn't be possible to hit otherwise.
  185. */
  186. add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
  187. uarg = u64_to_user_ptr(READ_ONCE(sqe->addr));
  188. uarg_size = READ_ONCE(sqe->len);
  189. if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index)
  190. return -EINVAL;
  191. if (uarg_size != sizeof(mc))
  192. return -EINVAL;
  193. memset(&mc, 0, sizeof(mc));
  194. if (copy_from_user(&mc, uarg, uarg_size))
  195. return -EFAULT;
  196. if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)))
  197. return -EINVAL;
  198. if (mc.flags & ~IO_VALID_CREATE_FLAGS)
  199. return -EINVAL;
  200. if (mc.file_size > SZ_1G)
  201. return -EINVAL;
  202. if (mc.rw_delay_ns > NSEC_PER_SEC)
  203. return -EINVAL;
  204. mf = kzalloc_obj(*mf, GFP_KERNEL_ACCOUNT);
  205. if (!mf)
  206. return -ENOMEM;
  207. init_waitqueue_head(&mf->poll_wq);
  208. mf->size = mc.file_size;
  209. mf->rw_delay_ns = mc.rw_delay_ns;
  210. if (mc.flags & IORING_MOCK_CREATE_F_POLL) {
  211. fops = &io_mock_poll_fops;
  212. mf->pollable = true;
  213. }
  214. FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
  215. anon_inode_create_getfile("[io_uring_mock]", fops, mf,
  216. O_RDWR | O_CLOEXEC, NULL));
  217. if (fdf.err)
  218. return fdf.err;
  219. retain_and_null_ptr(mf);
  220. file = fd_prepare_file(fdf);
  221. file->f_mode |= FMODE_READ | FMODE_CAN_READ | FMODE_WRITE |
  222. FMODE_CAN_WRITE | FMODE_LSEEK;
  223. if (mc.flags & IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
  224. file->f_mode |= FMODE_NOWAIT;
  225. mc.out_fd = fd_prepare_fd(fdf);
  226. if (copy_to_user(uarg, &mc, uarg_size))
  227. return -EFAULT;
  228. fd_publish(fdf);
  229. return 0;
  230. }
  231. static int io_probe_mock(struct io_uring_cmd *cmd)
  232. {
  233. const struct io_uring_sqe *sqe = cmd->sqe;
  234. struct io_uring_mock_probe mp, __user *uarg;
  235. size_t uarg_size;
  236. uarg = u64_to_user_ptr(READ_ONCE(sqe->addr));
  237. uarg_size = READ_ONCE(sqe->len);
  238. if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index ||
  239. uarg_size != sizeof(mp))
  240. return -EINVAL;
  241. memset(&mp, 0, sizeof(mp));
  242. if (copy_from_user(&mp, uarg, uarg_size))
  243. return -EFAULT;
  244. if (!mem_is_zero(&mp, sizeof(mp)))
  245. return -EINVAL;
  246. mp.features = IORING_MOCK_FEAT_END;
  247. if (copy_to_user(uarg, &mp, uarg_size))
  248. return -EFAULT;
  249. return 0;
  250. }
  251. static int iou_mock_mgr_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
  252. {
  253. if (!capable(CAP_SYS_ADMIN))
  254. return -EPERM;
  255. switch (cmd->cmd_op) {
  256. case IORING_MOCK_MGR_CMD_PROBE:
  257. return io_probe_mock(cmd);
  258. case IORING_MOCK_MGR_CMD_CREATE:
  259. return io_create_mock_file(cmd, issue_flags);
  260. }
  261. return -EOPNOTSUPP;
  262. }
  263. static const struct file_operations iou_mock_dev_fops = {
  264. .owner = THIS_MODULE,
  265. .uring_cmd = iou_mock_mgr_cmd,
  266. };
  267. static struct miscdevice iou_mock_miscdev = {
  268. .minor = MISC_DYNAMIC_MINOR,
  269. .name = "io_uring_mock",
  270. .fops = &iou_mock_dev_fops,
  271. };
  272. static int __init io_mock_init(void)
  273. {
  274. int ret;
  275. ret = misc_register(&iou_mock_miscdev);
  276. if (ret < 0) {
  277. pr_err("Could not initialize io_uring mock device\n");
  278. return ret;
  279. }
  280. return 0;
  281. }
  282. static void __exit io_mock_exit(void)
  283. {
  284. misc_deregister(&iou_mock_miscdev);
  285. }
  286. module_init(io_mock_init)
  287. module_exit(io_mock_exit)
  288. MODULE_AUTHOR("Pavel Begunkov <asml.silence@gmail.com>");
  289. MODULE_DESCRIPTION("io_uring mock file");
  290. MODULE_LICENSE("GPL");