waitid.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for async notification of waitid
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/fs.h>
  8. #include <linux/file.h>
  9. #include <linux/compat.h>
  10. #include <linux/io_uring.h>
  11. #include <uapi/linux/io_uring.h>
  12. #include "io_uring.h"
  13. #include "cancel.h"
  14. #include "waitid.h"
  15. #include "../kernel/exit.h"
  16. static void io_waitid_cb(struct io_kiocb *req, struct io_tw_state *ts);
  17. #define IO_WAITID_CANCEL_FLAG BIT(31)
  18. #define IO_WAITID_REF_MASK GENMASK(30, 0)
  19. struct io_waitid {
  20. struct file *file;
  21. int which;
  22. pid_t upid;
  23. int options;
  24. atomic_t refs;
  25. struct wait_queue_head *head;
  26. struct siginfo __user *infop;
  27. struct waitid_info info;
  28. };
  29. static void io_waitid_free(struct io_kiocb *req)
  30. {
  31. struct io_waitid_async *iwa = req->async_data;
  32. put_pid(iwa->wo.wo_pid);
  33. kfree(req->async_data);
  34. req->async_data = NULL;
  35. req->flags &= ~REQ_F_ASYNC_DATA;
  36. }
  37. #ifdef CONFIG_COMPAT
  38. static bool io_waitid_compat_copy_si(struct io_waitid *iw, int signo)
  39. {
  40. struct compat_siginfo __user *infop;
  41. bool ret;
  42. infop = (struct compat_siginfo __user *) iw->infop;
  43. if (!user_write_access_begin(infop, sizeof(*infop)))
  44. return false;
  45. unsafe_put_user(signo, &infop->si_signo, Efault);
  46. unsafe_put_user(0, &infop->si_errno, Efault);
  47. unsafe_put_user(iw->info.cause, &infop->si_code, Efault);
  48. unsafe_put_user(iw->info.pid, &infop->si_pid, Efault);
  49. unsafe_put_user(iw->info.uid, &infop->si_uid, Efault);
  50. unsafe_put_user(iw->info.status, &infop->si_status, Efault);
  51. ret = true;
  52. done:
  53. user_write_access_end();
  54. return ret;
  55. Efault:
  56. ret = false;
  57. goto done;
  58. }
  59. #endif
  60. static bool io_waitid_copy_si(struct io_kiocb *req, int signo)
  61. {
  62. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  63. bool ret;
  64. if (!iw->infop)
  65. return true;
  66. #ifdef CONFIG_COMPAT
  67. if (req->ctx->compat)
  68. return io_waitid_compat_copy_si(iw, signo);
  69. #endif
  70. if (!user_write_access_begin(iw->infop, sizeof(*iw->infop)))
  71. return false;
  72. unsafe_put_user(signo, &iw->infop->si_signo, Efault);
  73. unsafe_put_user(0, &iw->infop->si_errno, Efault);
  74. unsafe_put_user(iw->info.cause, &iw->infop->si_code, Efault);
  75. unsafe_put_user(iw->info.pid, &iw->infop->si_pid, Efault);
  76. unsafe_put_user(iw->info.uid, &iw->infop->si_uid, Efault);
  77. unsafe_put_user(iw->info.status, &iw->infop->si_status, Efault);
  78. ret = true;
  79. done:
  80. user_write_access_end();
  81. return ret;
  82. Efault:
  83. ret = false;
  84. goto done;
  85. }
  86. static int io_waitid_finish(struct io_kiocb *req, int ret)
  87. {
  88. int signo = 0;
  89. if (ret > 0) {
  90. signo = SIGCHLD;
  91. ret = 0;
  92. }
  93. if (!io_waitid_copy_si(req, signo))
  94. ret = -EFAULT;
  95. io_waitid_free(req);
  96. return ret;
  97. }
  98. static void io_waitid_complete(struct io_kiocb *req, int ret)
  99. {
  100. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  101. /* anyone completing better be holding a reference */
  102. WARN_ON_ONCE(!(atomic_read(&iw->refs) & IO_WAITID_REF_MASK));
  103. lockdep_assert_held(&req->ctx->uring_lock);
  104. hlist_del_init(&req->hash_node);
  105. ret = io_waitid_finish(req, ret);
  106. if (ret < 0)
  107. req_set_fail(req);
  108. io_req_set_res(req, ret, 0);
  109. }
  110. static bool __io_waitid_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
  111. {
  112. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  113. struct io_waitid_async *iwa = req->async_data;
  114. /*
  115. * Mark us canceled regardless of ownership. This will prevent a
  116. * potential retry from a spurious wakeup.
  117. */
  118. atomic_or(IO_WAITID_CANCEL_FLAG, &iw->refs);
  119. /* claim ownership */
  120. if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
  121. return false;
  122. spin_lock_irq(&iw->head->lock);
  123. list_del_init(&iwa->wo.child_wait.entry);
  124. spin_unlock_irq(&iw->head->lock);
  125. io_waitid_complete(req, -ECANCELED);
  126. io_req_queue_tw_complete(req, -ECANCELED);
  127. return true;
  128. }
  129. int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
  130. unsigned int issue_flags)
  131. {
  132. struct hlist_node *tmp;
  133. struct io_kiocb *req;
  134. int nr = 0;
  135. if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_FD_FIXED))
  136. return -ENOENT;
  137. io_ring_submit_lock(ctx, issue_flags);
  138. hlist_for_each_entry_safe(req, tmp, &ctx->waitid_list, hash_node) {
  139. if (req->cqe.user_data != cd->data &&
  140. !(cd->flags & IORING_ASYNC_CANCEL_ANY))
  141. continue;
  142. if (__io_waitid_cancel(ctx, req))
  143. nr++;
  144. if (!(cd->flags & IORING_ASYNC_CANCEL_ALL))
  145. break;
  146. }
  147. io_ring_submit_unlock(ctx, issue_flags);
  148. if (nr)
  149. return nr;
  150. return -ENOENT;
  151. }
  152. bool io_waitid_remove_all(struct io_ring_ctx *ctx, struct task_struct *task,
  153. bool cancel_all)
  154. {
  155. struct hlist_node *tmp;
  156. struct io_kiocb *req;
  157. bool found = false;
  158. lockdep_assert_held(&ctx->uring_lock);
  159. hlist_for_each_entry_safe(req, tmp, &ctx->waitid_list, hash_node) {
  160. if (!io_match_task_safe(req, task, cancel_all))
  161. continue;
  162. hlist_del_init(&req->hash_node);
  163. __io_waitid_cancel(ctx, req);
  164. found = true;
  165. }
  166. return found;
  167. }
  168. static inline bool io_waitid_drop_issue_ref(struct io_kiocb *req)
  169. {
  170. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  171. struct io_waitid_async *iwa = req->async_data;
  172. if (!atomic_sub_return(1, &iw->refs))
  173. return false;
  174. /*
  175. * Wakeup triggered, racing with us. It was prevented from
  176. * completing because of that, queue up the tw to do that.
  177. */
  178. req->io_task_work.func = io_waitid_cb;
  179. io_req_task_work_add(req);
  180. remove_wait_queue(iw->head, &iwa->wo.child_wait);
  181. return true;
  182. }
  183. static void io_waitid_cb(struct io_kiocb *req, struct io_tw_state *ts)
  184. {
  185. struct io_waitid_async *iwa = req->async_data;
  186. struct io_ring_ctx *ctx = req->ctx;
  187. int ret;
  188. io_tw_lock(ctx, ts);
  189. ret = __do_wait(&iwa->wo);
  190. /*
  191. * If we get -ERESTARTSYS here, we need to re-arm and check again
  192. * to ensure we get another callback. If the retry works, then we can
  193. * just remove ourselves from the waitqueue again and finish the
  194. * request.
  195. */
  196. if (unlikely(ret == -ERESTARTSYS)) {
  197. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  198. /* Don't retry if cancel found it meanwhile */
  199. ret = -ECANCELED;
  200. if (!(atomic_read(&iw->refs) & IO_WAITID_CANCEL_FLAG)) {
  201. iw->head = &current->signal->wait_chldexit;
  202. add_wait_queue(iw->head, &iwa->wo.child_wait);
  203. ret = __do_wait(&iwa->wo);
  204. if (ret == -ERESTARTSYS) {
  205. /* retry armed, drop our ref */
  206. io_waitid_drop_issue_ref(req);
  207. return;
  208. }
  209. remove_wait_queue(iw->head, &iwa->wo.child_wait);
  210. }
  211. }
  212. io_waitid_complete(req, ret);
  213. io_req_task_complete(req, ts);
  214. }
  215. static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode,
  216. int sync, void *key)
  217. {
  218. struct wait_opts *wo = container_of(wait, struct wait_opts, child_wait);
  219. struct io_waitid_async *iwa = container_of(wo, struct io_waitid_async, wo);
  220. struct io_kiocb *req = iwa->req;
  221. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  222. struct task_struct *p = key;
  223. if (!pid_child_should_wake(wo, p))
  224. return 0;
  225. /* cancel is in progress */
  226. if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
  227. return 1;
  228. req->io_task_work.func = io_waitid_cb;
  229. io_req_task_work_add(req);
  230. list_del_init(&wait->entry);
  231. return 1;
  232. }
  233. int io_waitid_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  234. {
  235. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  236. if (sqe->addr || sqe->buf_index || sqe->addr3 || sqe->waitid_flags)
  237. return -EINVAL;
  238. iw->which = READ_ONCE(sqe->len);
  239. iw->upid = READ_ONCE(sqe->fd);
  240. iw->options = READ_ONCE(sqe->file_index);
  241. iw->infop = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  242. return 0;
  243. }
  244. int io_waitid(struct io_kiocb *req, unsigned int issue_flags)
  245. {
  246. struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
  247. struct io_ring_ctx *ctx = req->ctx;
  248. struct io_waitid_async *iwa;
  249. int ret;
  250. if (io_alloc_async_data(req))
  251. return -ENOMEM;
  252. iwa = req->async_data;
  253. iwa->req = req;
  254. ret = kernel_waitid_prepare(&iwa->wo, iw->which, iw->upid, &iw->info,
  255. iw->options, NULL);
  256. if (ret)
  257. goto done;
  258. /*
  259. * Mark the request as busy upfront, in case we're racing with the
  260. * wakeup. If we are, then we'll notice when we drop this initial
  261. * reference again after arming.
  262. */
  263. atomic_set(&iw->refs, 1);
  264. /*
  265. * Cancel must hold the ctx lock, so there's no risk of cancelation
  266. * finding us until a) we remain on the list, and b) the lock is
  267. * dropped. We only need to worry about racing with the wakeup
  268. * callback.
  269. */
  270. io_ring_submit_lock(ctx, issue_flags);
  271. hlist_add_head(&req->hash_node, &ctx->waitid_list);
  272. init_waitqueue_func_entry(&iwa->wo.child_wait, io_waitid_wait);
  273. iwa->wo.child_wait.private = req->task;
  274. iw->head = &current->signal->wait_chldexit;
  275. add_wait_queue(iw->head, &iwa->wo.child_wait);
  276. ret = __do_wait(&iwa->wo);
  277. if (ret == -ERESTARTSYS) {
  278. /*
  279. * Nobody else grabbed a reference, it'll complete when we get
  280. * a waitqueue callback, or if someone cancels it.
  281. */
  282. if (!io_waitid_drop_issue_ref(req)) {
  283. io_ring_submit_unlock(ctx, issue_flags);
  284. return IOU_ISSUE_SKIP_COMPLETE;
  285. }
  286. /*
  287. * Wakeup triggered, racing with us. It was prevented from
  288. * completing because of that, queue up the tw to do that.
  289. */
  290. io_ring_submit_unlock(ctx, issue_flags);
  291. return IOU_ISSUE_SKIP_COMPLETE;
  292. }
  293. hlist_del_init(&req->hash_node);
  294. remove_wait_queue(iw->head, &iwa->wo.child_wait);
  295. ret = io_waitid_finish(req, ret);
  296. io_ring_submit_unlock(ctx, issue_flags);
  297. done:
  298. if (ret < 0)
  299. req_set_fail(req);
  300. io_req_set_res(req, ret, 0);
  301. return IOU_OK;
  302. }