epoll.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/file.h>
  5. #include <linux/fs.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/io_uring.h>
  8. #include <linux/eventpoll.h>
  9. #include <uapi/linux/io_uring.h>
  10. #include "io_uring.h"
  11. #include "epoll.h"
  12. struct io_epoll {
  13. struct file *file;
  14. int epfd;
  15. int op;
  16. int fd;
  17. struct epoll_event event;
  18. };
  19. struct io_epoll_wait {
  20. struct file *file;
  21. int maxevents;
  22. struct epoll_event __user *events;
  23. };
  24. int io_epoll_ctl_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  25. {
  26. struct io_epoll *epoll = io_kiocb_to_cmd(req, struct io_epoll);
  27. if (sqe->buf_index || sqe->splice_fd_in)
  28. return -EINVAL;
  29. epoll->epfd = READ_ONCE(sqe->fd);
  30. epoll->op = READ_ONCE(sqe->len);
  31. epoll->fd = READ_ONCE(sqe->off);
  32. if (ep_op_has_event(epoll->op)) {
  33. struct epoll_event __user *ev;
  34. ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
  35. if (copy_from_user(&epoll->event, ev, sizeof(*ev)))
  36. return -EFAULT;
  37. }
  38. return 0;
  39. }
  40. int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
  41. {
  42. struct io_epoll *ie = io_kiocb_to_cmd(req, struct io_epoll);
  43. int ret;
  44. bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
  45. ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
  46. if (force_nonblock && ret == -EAGAIN)
  47. return -EAGAIN;
  48. if (ret < 0)
  49. req_set_fail(req);
  50. io_req_set_res(req, ret, 0);
  51. return IOU_COMPLETE;
  52. }
  53. int io_epoll_wait_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  54. {
  55. struct io_epoll_wait *iew = io_kiocb_to_cmd(req, struct io_epoll_wait);
  56. if (sqe->off || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
  57. return -EINVAL;
  58. iew->maxevents = READ_ONCE(sqe->len);
  59. iew->events = u64_to_user_ptr(READ_ONCE(sqe->addr));
  60. return 0;
  61. }
  62. int io_epoll_wait(struct io_kiocb *req, unsigned int issue_flags)
  63. {
  64. struct io_epoll_wait *iew = io_kiocb_to_cmd(req, struct io_epoll_wait);
  65. int ret;
  66. ret = epoll_sendevents(req->file, iew->events, iew->maxevents);
  67. if (ret == 0)
  68. return -EAGAIN;
  69. if (ret < 0)
  70. req_set_fail(req);
  71. io_req_set_res(req, ret, 0);
  72. return IOU_COMPLETE;
  73. }