futex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/fs.h>
  5. #include <linux/file.h>
  6. #include <linux/io_uring.h>
  7. #include <uapi/linux/io_uring.h>
  8. #include "../kernel/futex/futex.h"
  9. #include "io_uring.h"
  10. #include "alloc_cache.h"
  11. #include "futex.h"
  12. struct io_futex {
  13. struct file *file;
  14. void __user *uaddr;
  15. unsigned long futex_val;
  16. unsigned long futex_mask;
  17. u32 futex_flags;
  18. unsigned int futex_nr;
  19. bool futexv_unqueued;
  20. };
  21. struct io_futex_data {
  22. struct futex_q q;
  23. struct io_kiocb *req;
  24. };
  25. struct io_futexv_data {
  26. unsigned long owned;
  27. struct futex_vector futexv[];
  28. };
  29. #define IO_FUTEX_ALLOC_CACHE_MAX 32
  30. bool io_futex_cache_init(struct io_ring_ctx *ctx)
  31. {
  32. return io_alloc_cache_init(&ctx->futex_cache, IO_FUTEX_ALLOC_CACHE_MAX,
  33. sizeof(struct io_futex_data), 0);
  34. }
  35. void io_futex_cache_free(struct io_ring_ctx *ctx)
  36. {
  37. io_alloc_cache_free(&ctx->futex_cache, kfree);
  38. }
  39. static void __io_futex_complete(struct io_tw_req tw_req, io_tw_token_t tw)
  40. {
  41. hlist_del_init(&tw_req.req->hash_node);
  42. io_req_task_complete(tw_req, tw);
  43. }
  44. static void io_futex_complete(struct io_tw_req tw_req, io_tw_token_t tw)
  45. {
  46. struct io_kiocb *req = tw_req.req;
  47. struct io_ring_ctx *ctx = req->ctx;
  48. io_tw_lock(ctx, tw);
  49. io_cache_free(&ctx->futex_cache, req->async_data);
  50. io_req_async_data_clear(req, 0);
  51. __io_futex_complete(tw_req, tw);
  52. }
  53. static void io_futexv_complete(struct io_tw_req tw_req, io_tw_token_t tw)
  54. {
  55. struct io_kiocb *req = tw_req.req;
  56. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  57. struct io_futexv_data *ifd = req->async_data;
  58. io_tw_lock(req->ctx, tw);
  59. if (!iof->futexv_unqueued) {
  60. int res;
  61. res = futex_unqueue_multiple(ifd->futexv, iof->futex_nr);
  62. if (res != -1)
  63. io_req_set_res(req, res, 0);
  64. }
  65. io_req_async_data_free(req);
  66. __io_futex_complete(tw_req, tw);
  67. }
  68. static bool io_futexv_claim(struct io_futexv_data *ifd)
  69. {
  70. if (test_bit(0, &ifd->owned) || test_and_set_bit_lock(0, &ifd->owned))
  71. return false;
  72. return true;
  73. }
  74. static bool __io_futex_cancel(struct io_kiocb *req)
  75. {
  76. /* futex wake already done or in progress */
  77. if (req->opcode == IORING_OP_FUTEX_WAIT) {
  78. struct io_futex_data *ifd = req->async_data;
  79. if (!futex_unqueue(&ifd->q))
  80. return false;
  81. req->io_task_work.func = io_futex_complete;
  82. } else {
  83. struct io_futexv_data *ifd = req->async_data;
  84. if (!io_futexv_claim(ifd))
  85. return false;
  86. req->io_task_work.func = io_futexv_complete;
  87. }
  88. hlist_del_init(&req->hash_node);
  89. io_req_set_res(req, -ECANCELED, 0);
  90. io_req_task_work_add(req);
  91. return true;
  92. }
  93. int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
  94. unsigned int issue_flags)
  95. {
  96. return io_cancel_remove(ctx, cd, issue_flags, &ctx->futex_list, __io_futex_cancel);
  97. }
  98. bool io_futex_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
  99. bool cancel_all)
  100. {
  101. return io_cancel_remove_all(ctx, tctx, &ctx->futex_list, cancel_all, __io_futex_cancel);
  102. }
  103. int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  104. {
  105. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  106. u32 flags;
  107. if (unlikely(sqe->len || sqe->futex_flags || sqe->buf_index ||
  108. sqe->file_index))
  109. return -EINVAL;
  110. iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
  111. iof->futex_val = READ_ONCE(sqe->addr2);
  112. iof->futex_mask = READ_ONCE(sqe->addr3);
  113. flags = READ_ONCE(sqe->fd);
  114. if (flags & ~FUTEX2_VALID_MASK)
  115. return -EINVAL;
  116. iof->futex_flags = futex2_to_flags(flags);
  117. if (!futex_flags_valid(iof->futex_flags))
  118. return -EINVAL;
  119. if (!futex_validate_input(iof->futex_flags, iof->futex_val) ||
  120. !futex_validate_input(iof->futex_flags, iof->futex_mask))
  121. return -EINVAL;
  122. /* Mark as inflight, so file exit cancelation will find it */
  123. io_req_track_inflight(req);
  124. return 0;
  125. }
  126. static void io_futex_wakev_fn(struct wake_q_head *wake_q, struct futex_q *q)
  127. {
  128. struct io_kiocb *req = q->wake_data;
  129. struct io_futexv_data *ifd = req->async_data;
  130. if (!io_futexv_claim(ifd))
  131. return;
  132. if (unlikely(!__futex_wake_mark(q)))
  133. return;
  134. io_req_set_res(req, 0, 0);
  135. req->io_task_work.func = io_futexv_complete;
  136. io_req_task_work_add(req);
  137. }
  138. int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  139. {
  140. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  141. struct io_futexv_data *ifd;
  142. int ret;
  143. /* No flags or mask supported for waitv */
  144. if (unlikely(sqe->fd || sqe->buf_index || sqe->file_index ||
  145. sqe->addr2 || sqe->futex_flags || sqe->addr3))
  146. return -EINVAL;
  147. iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
  148. iof->futex_nr = READ_ONCE(sqe->len);
  149. if (!iof->futex_nr || iof->futex_nr > FUTEX_WAITV_MAX)
  150. return -EINVAL;
  151. ifd = kzalloc_flex(struct io_futexv_data, futexv, iof->futex_nr,
  152. GFP_KERNEL_ACCOUNT);
  153. if (!ifd)
  154. return -ENOMEM;
  155. ret = futex_parse_waitv(ifd->futexv, iof->uaddr, iof->futex_nr,
  156. io_futex_wakev_fn, req);
  157. if (ret) {
  158. kfree(ifd);
  159. return ret;
  160. }
  161. /* Mark as inflight, so file exit cancelation will find it */
  162. io_req_track_inflight(req);
  163. iof->futexv_unqueued = 0;
  164. req->flags |= REQ_F_ASYNC_DATA;
  165. req->async_data = ifd;
  166. return 0;
  167. }
  168. static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q)
  169. {
  170. struct io_futex_data *ifd = container_of(q, struct io_futex_data, q);
  171. struct io_kiocb *req = ifd->req;
  172. if (unlikely(!__futex_wake_mark(q)))
  173. return;
  174. io_req_set_res(req, 0, 0);
  175. req->io_task_work.func = io_futex_complete;
  176. io_req_task_work_add(req);
  177. }
  178. int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
  179. {
  180. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  181. struct io_futexv_data *ifd = req->async_data;
  182. struct io_ring_ctx *ctx = req->ctx;
  183. int ret, woken = -1;
  184. io_ring_submit_lock(ctx, issue_flags);
  185. ret = futex_wait_multiple_setup(ifd->futexv, iof->futex_nr, &woken);
  186. /*
  187. * Error case, ret is < 0. Mark the request as failed.
  188. */
  189. if (unlikely(ret < 0)) {
  190. io_ring_submit_unlock(ctx, issue_flags);
  191. req_set_fail(req);
  192. io_req_set_res(req, ret, 0);
  193. io_req_async_data_free(req);
  194. return IOU_COMPLETE;
  195. }
  196. /*
  197. * 0 return means that we successfully setup the waiters, and that
  198. * nobody triggered a wakeup while we were doing so. If the wakeup
  199. * happened post setup, the task_work will be run post this issue and
  200. * under the submission lock. 1 means We got woken while setting up,
  201. * let that side do the completion. Note that
  202. * futex_wait_multiple_setup() will have unqueued all the futexes in
  203. * this case. Mark us as having done that already, since this is
  204. * different from normal wakeup.
  205. */
  206. if (!ret) {
  207. /*
  208. * If futex_wait_multiple_setup() returns 0 for a
  209. * successful setup, then the task state will not be
  210. * runnable. This is fine for the sync syscall, as
  211. * it'll be blocking unless we already got one of the
  212. * futexes woken, but it obviously won't work for an
  213. * async invocation. Mark us runnable again.
  214. */
  215. __set_current_state(TASK_RUNNING);
  216. hlist_add_head(&req->hash_node, &ctx->futex_list);
  217. } else {
  218. iof->futexv_unqueued = 1;
  219. if (woken != -1)
  220. io_req_set_res(req, woken, 0);
  221. }
  222. io_ring_submit_unlock(ctx, issue_flags);
  223. return IOU_ISSUE_SKIP_COMPLETE;
  224. }
  225. int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
  226. {
  227. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  228. struct io_ring_ctx *ctx = req->ctx;
  229. struct io_futex_data *ifd = NULL;
  230. int ret;
  231. if (!iof->futex_mask) {
  232. ret = -EINVAL;
  233. goto done;
  234. }
  235. io_ring_submit_lock(ctx, issue_flags);
  236. ifd = io_cache_alloc(&ctx->futex_cache, GFP_NOWAIT);
  237. if (!ifd) {
  238. ret = -ENOMEM;
  239. goto done_unlock;
  240. }
  241. req->flags |= REQ_F_ASYNC_DATA;
  242. req->async_data = ifd;
  243. ifd->q = futex_q_init;
  244. ifd->q.bitset = iof->futex_mask;
  245. ifd->q.wake = io_futex_wake_fn;
  246. ifd->req = req;
  247. ret = futex_wait_setup(iof->uaddr, iof->futex_val, iof->futex_flags,
  248. &ifd->q, NULL, NULL);
  249. if (!ret) {
  250. hlist_add_head(&req->hash_node, &ctx->futex_list);
  251. io_ring_submit_unlock(ctx, issue_flags);
  252. return IOU_ISSUE_SKIP_COMPLETE;
  253. }
  254. done_unlock:
  255. io_ring_submit_unlock(ctx, issue_flags);
  256. done:
  257. if (ret < 0)
  258. req_set_fail(req);
  259. io_req_set_res(req, ret, 0);
  260. io_req_async_data_free(req);
  261. return IOU_COMPLETE;
  262. }
  263. int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags)
  264. {
  265. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  266. int ret;
  267. /*
  268. * Strict flags - ensure that waking 0 futexes yields a 0 result.
  269. * See commit 43adf8449510 ("futex: FLAGS_STRICT") for details.
  270. */
  271. ret = futex_wake(iof->uaddr, FLAGS_STRICT | iof->futex_flags,
  272. iof->futex_val, iof->futex_mask);
  273. if (ret < 0)
  274. req_set_fail(req);
  275. io_req_set_res(req, ret, 0);
  276. return IOU_COMPLETE;
  277. }