futex.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. union {
  15. u32 __user *uaddr;
  16. struct futex_waitv __user *uwaitv;
  17. };
  18. unsigned long futex_val;
  19. unsigned long futex_mask;
  20. unsigned long futexv_owned;
  21. u32 futex_flags;
  22. unsigned int futex_nr;
  23. bool futexv_unqueued;
  24. };
  25. struct io_futex_data {
  26. struct futex_q q;
  27. struct io_kiocb *req;
  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));
  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_kiocb *req, struct io_tw_state *ts)
  40. {
  41. req->async_data = NULL;
  42. hlist_del_init(&req->hash_node);
  43. io_req_task_complete(req, ts);
  44. }
  45. static void io_futex_complete(struct io_kiocb *req, struct io_tw_state *ts)
  46. {
  47. struct io_futex_data *ifd = req->async_data;
  48. struct io_ring_ctx *ctx = req->ctx;
  49. io_tw_lock(ctx, ts);
  50. if (!io_alloc_cache_put(&ctx->futex_cache, ifd))
  51. kfree(ifd);
  52. __io_futex_complete(req, ts);
  53. }
  54. static void io_futexv_complete(struct io_kiocb *req, struct io_tw_state *ts)
  55. {
  56. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  57. struct futex_vector *futexv = req->async_data;
  58. io_tw_lock(req->ctx, ts);
  59. if (!iof->futexv_unqueued) {
  60. int res;
  61. res = futex_unqueue_multiple(futexv, iof->futex_nr);
  62. if (res != -1)
  63. io_req_set_res(req, res, 0);
  64. }
  65. kfree(req->async_data);
  66. req->flags &= ~REQ_F_ASYNC_DATA;
  67. __io_futex_complete(req, ts);
  68. }
  69. static bool io_futexv_claim(struct io_futex *iof)
  70. {
  71. if (test_bit(0, &iof->futexv_owned) ||
  72. test_and_set_bit_lock(0, &iof->futexv_owned))
  73. return false;
  74. return true;
  75. }
  76. static bool __io_futex_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
  77. {
  78. /* futex wake already done or in progress */
  79. if (req->opcode == IORING_OP_FUTEX_WAIT) {
  80. struct io_futex_data *ifd = req->async_data;
  81. if (!futex_unqueue(&ifd->q))
  82. return false;
  83. req->io_task_work.func = io_futex_complete;
  84. } else {
  85. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  86. if (!io_futexv_claim(iof))
  87. return false;
  88. req->io_task_work.func = io_futexv_complete;
  89. }
  90. hlist_del_init(&req->hash_node);
  91. io_req_set_res(req, -ECANCELED, 0);
  92. io_req_task_work_add(req);
  93. return true;
  94. }
  95. int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
  96. unsigned int issue_flags)
  97. {
  98. struct hlist_node *tmp;
  99. struct io_kiocb *req;
  100. int nr = 0;
  101. if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_FD_FIXED))
  102. return -ENOENT;
  103. io_ring_submit_lock(ctx, issue_flags);
  104. hlist_for_each_entry_safe(req, tmp, &ctx->futex_list, hash_node) {
  105. if (req->cqe.user_data != cd->data &&
  106. !(cd->flags & IORING_ASYNC_CANCEL_ANY))
  107. continue;
  108. if (__io_futex_cancel(ctx, req))
  109. nr++;
  110. if (!(cd->flags & IORING_ASYNC_CANCEL_ALL))
  111. break;
  112. }
  113. io_ring_submit_unlock(ctx, issue_flags);
  114. if (nr)
  115. return nr;
  116. return -ENOENT;
  117. }
  118. bool io_futex_remove_all(struct io_ring_ctx *ctx, struct task_struct *task,
  119. bool cancel_all)
  120. {
  121. struct hlist_node *tmp;
  122. struct io_kiocb *req;
  123. bool found = false;
  124. lockdep_assert_held(&ctx->uring_lock);
  125. hlist_for_each_entry_safe(req, tmp, &ctx->futex_list, hash_node) {
  126. if (!io_match_task_safe(req, task, cancel_all))
  127. continue;
  128. hlist_del_init(&req->hash_node);
  129. __io_futex_cancel(ctx, req);
  130. found = true;
  131. }
  132. return found;
  133. }
  134. int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  135. {
  136. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  137. u32 flags;
  138. if (unlikely(sqe->len || sqe->futex_flags || sqe->buf_index ||
  139. sqe->file_index))
  140. return -EINVAL;
  141. iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
  142. iof->futex_val = READ_ONCE(sqe->addr2);
  143. iof->futex_mask = READ_ONCE(sqe->addr3);
  144. flags = READ_ONCE(sqe->fd);
  145. if (flags & ~FUTEX2_VALID_MASK)
  146. return -EINVAL;
  147. iof->futex_flags = futex2_to_flags(flags);
  148. if (!futex_flags_valid(iof->futex_flags))
  149. return -EINVAL;
  150. if (!futex_validate_input(iof->futex_flags, iof->futex_val) ||
  151. !futex_validate_input(iof->futex_flags, iof->futex_mask))
  152. return -EINVAL;
  153. return 0;
  154. }
  155. static void io_futex_wakev_fn(struct wake_q_head *wake_q, struct futex_q *q)
  156. {
  157. struct io_kiocb *req = q->wake_data;
  158. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  159. if (!io_futexv_claim(iof))
  160. return;
  161. if (unlikely(!__futex_wake_mark(q)))
  162. return;
  163. io_req_set_res(req, 0, 0);
  164. req->io_task_work.func = io_futexv_complete;
  165. io_req_task_work_add(req);
  166. }
  167. int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  168. {
  169. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  170. struct futex_vector *futexv;
  171. int ret;
  172. /* No flags or mask supported for waitv */
  173. if (unlikely(sqe->fd || sqe->buf_index || sqe->file_index ||
  174. sqe->addr2 || sqe->futex_flags || sqe->addr3))
  175. return -EINVAL;
  176. iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
  177. iof->futex_nr = READ_ONCE(sqe->len);
  178. if (!iof->futex_nr || iof->futex_nr > FUTEX_WAITV_MAX)
  179. return -EINVAL;
  180. futexv = kcalloc(iof->futex_nr, sizeof(*futexv), GFP_KERNEL);
  181. if (!futexv)
  182. return -ENOMEM;
  183. ret = futex_parse_waitv(futexv, iof->uwaitv, iof->futex_nr,
  184. io_futex_wakev_fn, req);
  185. if (ret) {
  186. kfree(futexv);
  187. return ret;
  188. }
  189. iof->futexv_owned = 0;
  190. iof->futexv_unqueued = 0;
  191. req->flags |= REQ_F_ASYNC_DATA;
  192. req->async_data = futexv;
  193. return 0;
  194. }
  195. static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q)
  196. {
  197. struct io_futex_data *ifd = container_of(q, struct io_futex_data, q);
  198. struct io_kiocb *req = ifd->req;
  199. if (unlikely(!__futex_wake_mark(q)))
  200. return;
  201. io_req_set_res(req, 0, 0);
  202. req->io_task_work.func = io_futex_complete;
  203. io_req_task_work_add(req);
  204. }
  205. static struct io_futex_data *io_alloc_ifd(struct io_ring_ctx *ctx)
  206. {
  207. struct io_futex_data *ifd;
  208. ifd = io_alloc_cache_get(&ctx->futex_cache);
  209. if (ifd)
  210. return ifd;
  211. return kmalloc(sizeof(struct io_futex_data), GFP_NOWAIT);
  212. }
  213. int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
  214. {
  215. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  216. struct futex_vector *futexv = req->async_data;
  217. struct io_ring_ctx *ctx = req->ctx;
  218. int ret, woken = -1;
  219. io_ring_submit_lock(ctx, issue_flags);
  220. ret = futex_wait_multiple_setup(futexv, iof->futex_nr, &woken);
  221. /*
  222. * Error case, ret is < 0. Mark the request as failed.
  223. */
  224. if (unlikely(ret < 0)) {
  225. io_ring_submit_unlock(ctx, issue_flags);
  226. req_set_fail(req);
  227. io_req_set_res(req, ret, 0);
  228. kfree(futexv);
  229. req->async_data = NULL;
  230. req->flags &= ~REQ_F_ASYNC_DATA;
  231. return IOU_OK;
  232. }
  233. /*
  234. * 0 return means that we successfully setup the waiters, and that
  235. * nobody triggered a wakeup while we were doing so. If the wakeup
  236. * happened post setup, the task_work will be run post this issue and
  237. * under the submission lock. 1 means We got woken while setting up,
  238. * let that side do the completion. Note that
  239. * futex_wait_multiple_setup() will have unqueued all the futexes in
  240. * this case. Mark us as having done that already, since this is
  241. * different from normal wakeup.
  242. */
  243. if (!ret) {
  244. /*
  245. * If futex_wait_multiple_setup() returns 0 for a
  246. * successful setup, then the task state will not be
  247. * runnable. This is fine for the sync syscall, as
  248. * it'll be blocking unless we already got one of the
  249. * futexes woken, but it obviously won't work for an
  250. * async invocation. Mark us runnable again.
  251. */
  252. __set_current_state(TASK_RUNNING);
  253. hlist_add_head(&req->hash_node, &ctx->futex_list);
  254. } else {
  255. iof->futexv_unqueued = 1;
  256. if (woken != -1)
  257. io_req_set_res(req, woken, 0);
  258. }
  259. io_ring_submit_unlock(ctx, issue_flags);
  260. return IOU_ISSUE_SKIP_COMPLETE;
  261. }
  262. int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
  263. {
  264. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  265. struct io_ring_ctx *ctx = req->ctx;
  266. struct io_futex_data *ifd = NULL;
  267. struct futex_hash_bucket *hb;
  268. int ret;
  269. if (!iof->futex_mask) {
  270. ret = -EINVAL;
  271. goto done;
  272. }
  273. io_ring_submit_lock(ctx, issue_flags);
  274. ifd = io_alloc_ifd(ctx);
  275. if (!ifd) {
  276. ret = -ENOMEM;
  277. goto done_unlock;
  278. }
  279. req->async_data = ifd;
  280. ifd->q = futex_q_init;
  281. ifd->q.bitset = iof->futex_mask;
  282. ifd->q.wake = io_futex_wake_fn;
  283. ifd->req = req;
  284. ret = futex_wait_setup(iof->uaddr, iof->futex_val, iof->futex_flags,
  285. &ifd->q, &hb);
  286. if (!ret) {
  287. hlist_add_head(&req->hash_node, &ctx->futex_list);
  288. io_ring_submit_unlock(ctx, issue_flags);
  289. futex_queue(&ifd->q, hb);
  290. return IOU_ISSUE_SKIP_COMPLETE;
  291. }
  292. done_unlock:
  293. io_ring_submit_unlock(ctx, issue_flags);
  294. done:
  295. if (ret < 0)
  296. req_set_fail(req);
  297. io_req_set_res(req, ret, 0);
  298. kfree(ifd);
  299. return IOU_OK;
  300. }
  301. int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags)
  302. {
  303. struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
  304. int ret;
  305. /*
  306. * Strict flags - ensure that waking 0 futexes yields a 0 result.
  307. * See commit 43adf8449510 ("futex: FLAGS_STRICT") for details.
  308. */
  309. ret = futex_wake(iof->uaddr, FLAGS_STRICT | iof->futex_flags,
  310. iof->futex_val, iof->futex_mask);
  311. if (ret < 0)
  312. req_set_fail(req);
  313. io_req_set_res(req, ret, 0);
  314. return IOU_OK;
  315. }