tw.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Task work handling for io_uring
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/sched/signal.h>
  8. #include <linux/io_uring.h>
  9. #include <linux/indirect_call_wrapper.h>
  10. #include "io_uring.h"
  11. #include "tctx.h"
  12. #include "poll.h"
  13. #include "rw.h"
  14. #include "eventfd.h"
  15. #include "wait.h"
  16. void io_fallback_req_func(struct work_struct *work)
  17. {
  18. struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
  19. fallback_work.work);
  20. struct llist_node *node = llist_del_all(&ctx->fallback_llist);
  21. struct io_kiocb *req, *tmp;
  22. struct io_tw_state ts = {};
  23. percpu_ref_get(&ctx->refs);
  24. mutex_lock(&ctx->uring_lock);
  25. ts.cancel = io_should_terminate_tw(ctx);
  26. llist_for_each_entry_safe(req, tmp, node, io_task_work.node)
  27. req->io_task_work.func((struct io_tw_req){req}, ts);
  28. io_submit_flush_completions(ctx);
  29. mutex_unlock(&ctx->uring_lock);
  30. percpu_ref_put(&ctx->refs);
  31. }
  32. static void ctx_flush_and_put(struct io_ring_ctx *ctx, io_tw_token_t tw)
  33. {
  34. if (!ctx)
  35. return;
  36. if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
  37. atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
  38. io_submit_flush_completions(ctx);
  39. mutex_unlock(&ctx->uring_lock);
  40. percpu_ref_put(&ctx->refs);
  41. }
  42. /*
  43. * Run queued task_work, returning the number of entries processed in *count.
  44. * If more entries than max_entries are available, stop processing once this
  45. * is reached and return the rest of the list.
  46. */
  47. struct llist_node *io_handle_tw_list(struct llist_node *node,
  48. unsigned int *count,
  49. unsigned int max_entries)
  50. {
  51. struct io_ring_ctx *ctx = NULL;
  52. struct io_tw_state ts = { };
  53. do {
  54. struct llist_node *next = node->next;
  55. struct io_kiocb *req = container_of(node, struct io_kiocb,
  56. io_task_work.node);
  57. if (req->ctx != ctx) {
  58. ctx_flush_and_put(ctx, ts);
  59. ctx = req->ctx;
  60. mutex_lock(&ctx->uring_lock);
  61. percpu_ref_get(&ctx->refs);
  62. ts.cancel = io_should_terminate_tw(ctx);
  63. }
  64. INDIRECT_CALL_2(req->io_task_work.func,
  65. io_poll_task_func, io_req_rw_complete,
  66. (struct io_tw_req){req}, ts);
  67. node = next;
  68. (*count)++;
  69. if (unlikely(need_resched())) {
  70. ctx_flush_and_put(ctx, ts);
  71. ctx = NULL;
  72. cond_resched();
  73. }
  74. } while (node && *count < max_entries);
  75. ctx_flush_and_put(ctx, ts);
  76. return node;
  77. }
  78. static __cold void __io_fallback_tw(struct llist_node *node, bool sync)
  79. {
  80. struct io_ring_ctx *last_ctx = NULL;
  81. struct io_kiocb *req;
  82. while (node) {
  83. req = container_of(node, struct io_kiocb, io_task_work.node);
  84. node = node->next;
  85. if (last_ctx != req->ctx) {
  86. if (last_ctx) {
  87. if (sync)
  88. flush_delayed_work(&last_ctx->fallback_work);
  89. percpu_ref_put(&last_ctx->refs);
  90. }
  91. last_ctx = req->ctx;
  92. percpu_ref_get(&last_ctx->refs);
  93. }
  94. if (llist_add(&req->io_task_work.node, &last_ctx->fallback_llist))
  95. schedule_delayed_work(&last_ctx->fallback_work, 1);
  96. }
  97. if (last_ctx) {
  98. if (sync)
  99. flush_delayed_work(&last_ctx->fallback_work);
  100. percpu_ref_put(&last_ctx->refs);
  101. }
  102. }
  103. static void io_fallback_tw(struct io_uring_task *tctx, bool sync)
  104. {
  105. struct llist_node *node = llist_del_all(&tctx->task_list);
  106. __io_fallback_tw(node, sync);
  107. }
  108. struct llist_node *tctx_task_work_run(struct io_uring_task *tctx,
  109. unsigned int max_entries,
  110. unsigned int *count)
  111. {
  112. struct llist_node *node;
  113. node = llist_del_all(&tctx->task_list);
  114. if (node) {
  115. node = llist_reverse_order(node);
  116. node = io_handle_tw_list(node, count, max_entries);
  117. }
  118. /* relaxed read is enough as only the task itself sets ->in_cancel */
  119. if (unlikely(atomic_read(&tctx->in_cancel)))
  120. io_uring_drop_tctx_refs(current);
  121. trace_io_uring_task_work_run(tctx, *count);
  122. return node;
  123. }
  124. void tctx_task_work(struct callback_head *cb)
  125. {
  126. struct io_uring_task *tctx;
  127. struct llist_node *ret;
  128. unsigned int count = 0;
  129. tctx = container_of(cb, struct io_uring_task, task_work);
  130. ret = tctx_task_work_run(tctx, UINT_MAX, &count);
  131. /* can't happen */
  132. WARN_ON_ONCE(ret);
  133. }
  134. /*
  135. * Sets IORING_SQ_TASKRUN in the sq_flags shared with userspace, using the
  136. * RCU protected rings pointer to be safe against concurrent ring resizing.
  137. */
  138. static void io_ctx_mark_taskrun(struct io_ring_ctx *ctx)
  139. {
  140. lockdep_assert_in_rcu_read_lock();
  141. if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) {
  142. struct io_rings *rings = rcu_dereference(ctx->rings_rcu);
  143. atomic_or(IORING_SQ_TASKRUN, &rings->sq_flags);
  144. }
  145. }
  146. void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
  147. {
  148. struct io_ring_ctx *ctx = req->ctx;
  149. unsigned nr_wait, nr_tw, nr_tw_prev;
  150. struct llist_node *head;
  151. /* See comment above IO_CQ_WAKE_INIT */
  152. BUILD_BUG_ON(IO_CQ_WAKE_FORCE <= IORING_MAX_CQ_ENTRIES);
  153. /*
  154. * We don't know how many requests there are in the link and whether
  155. * they can even be queued lazily, fall back to non-lazy.
  156. */
  157. if (req->flags & IO_REQ_LINK_FLAGS)
  158. flags &= ~IOU_F_TWQ_LAZY_WAKE;
  159. guard(rcu)();
  160. head = READ_ONCE(ctx->work_llist.first);
  161. do {
  162. nr_tw_prev = 0;
  163. if (head) {
  164. struct io_kiocb *first_req = container_of(head,
  165. struct io_kiocb,
  166. io_task_work.node);
  167. /*
  168. * Might be executed at any moment, rely on
  169. * SLAB_TYPESAFE_BY_RCU to keep it alive.
  170. */
  171. nr_tw_prev = READ_ONCE(first_req->nr_tw);
  172. }
  173. /*
  174. * Theoretically, it can overflow, but that's fine as one of
  175. * previous adds should've tried to wake the task.
  176. */
  177. nr_tw = nr_tw_prev + 1;
  178. if (!(flags & IOU_F_TWQ_LAZY_WAKE))
  179. nr_tw = IO_CQ_WAKE_FORCE;
  180. req->nr_tw = nr_tw;
  181. req->io_task_work.node.next = head;
  182. } while (!try_cmpxchg(&ctx->work_llist.first, &head,
  183. &req->io_task_work.node));
  184. /*
  185. * cmpxchg implies a full barrier, which pairs with the barrier
  186. * in set_current_state() on the io_cqring_wait() side. It's used
  187. * to ensure that either we see updated ->cq_wait_nr, or waiters
  188. * going to sleep will observe the work added to the list, which
  189. * is similar to the wait/wawke task state sync.
  190. */
  191. if (!head) {
  192. io_ctx_mark_taskrun(ctx);
  193. if (ctx->has_evfd)
  194. io_eventfd_signal(ctx, false);
  195. }
  196. nr_wait = atomic_read(&ctx->cq_wait_nr);
  197. /* not enough or no one is waiting */
  198. if (nr_tw < nr_wait)
  199. return;
  200. /* the previous add has already woken it up */
  201. if (nr_tw_prev >= nr_wait)
  202. return;
  203. wake_up_state(ctx->submitter_task, TASK_INTERRUPTIBLE);
  204. }
  205. void io_req_normal_work_add(struct io_kiocb *req)
  206. {
  207. struct io_uring_task *tctx = req->tctx;
  208. struct io_ring_ctx *ctx = req->ctx;
  209. /* task_work already pending, we're done */
  210. if (!llist_add(&req->io_task_work.node, &tctx->task_list))
  211. return;
  212. /*
  213. * Doesn't need to use ->rings_rcu, as resizing isn't supported for
  214. * !DEFER_TASKRUN.
  215. */
  216. if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
  217. atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
  218. /* SQPOLL doesn't need the task_work added, it'll run it itself */
  219. if (ctx->flags & IORING_SETUP_SQPOLL) {
  220. __set_notify_signal(tctx->task);
  221. return;
  222. }
  223. if (likely(!task_work_add(tctx->task, &tctx->task_work, ctx->notify_method)))
  224. return;
  225. io_fallback_tw(tctx, false);
  226. }
  227. void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags)
  228. {
  229. if (WARN_ON_ONCE(!(req->ctx->flags & IORING_SETUP_DEFER_TASKRUN)))
  230. return;
  231. __io_req_task_work_add(req, flags);
  232. }
  233. void __cold io_move_task_work_from_local(struct io_ring_ctx *ctx)
  234. {
  235. struct llist_node *node = llist_del_all(&ctx->work_llist);
  236. __io_fallback_tw(node, false);
  237. node = llist_del_all(&ctx->retry_llist);
  238. __io_fallback_tw(node, false);
  239. }
  240. static bool io_run_local_work_continue(struct io_ring_ctx *ctx, int events,
  241. int min_events)
  242. {
  243. if (!io_local_work_pending(ctx))
  244. return false;
  245. if (events < min_events)
  246. return true;
  247. if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
  248. atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
  249. return false;
  250. }
  251. static int __io_run_local_work_loop(struct llist_node **node,
  252. io_tw_token_t tw,
  253. int events)
  254. {
  255. int ret = 0;
  256. while (*node) {
  257. struct llist_node *next = (*node)->next;
  258. struct io_kiocb *req = container_of(*node, struct io_kiocb,
  259. io_task_work.node);
  260. INDIRECT_CALL_2(req->io_task_work.func,
  261. io_poll_task_func, io_req_rw_complete,
  262. (struct io_tw_req){req}, tw);
  263. *node = next;
  264. if (++ret >= events)
  265. break;
  266. }
  267. return ret;
  268. }
  269. static int __io_run_local_work(struct io_ring_ctx *ctx, io_tw_token_t tw,
  270. int min_events, int max_events)
  271. {
  272. struct llist_node *node;
  273. unsigned int loops = 0;
  274. int ret = 0;
  275. if (WARN_ON_ONCE(ctx->submitter_task != current))
  276. return -EEXIST;
  277. if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
  278. atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
  279. again:
  280. tw.cancel = io_should_terminate_tw(ctx);
  281. min_events -= ret;
  282. ret = __io_run_local_work_loop(&ctx->retry_llist.first, tw, max_events);
  283. if (ctx->retry_llist.first)
  284. goto retry_done;
  285. /*
  286. * llists are in reverse order, flip it back the right way before
  287. * running the pending items.
  288. */
  289. node = llist_reverse_order(llist_del_all(&ctx->work_llist));
  290. ret += __io_run_local_work_loop(&node, tw, max_events - ret);
  291. ctx->retry_llist.first = node;
  292. loops++;
  293. if (io_run_local_work_continue(ctx, ret, min_events))
  294. goto again;
  295. retry_done:
  296. io_submit_flush_completions(ctx);
  297. if (io_run_local_work_continue(ctx, ret, min_events))
  298. goto again;
  299. trace_io_uring_local_work_run(ctx, ret, loops);
  300. return ret;
  301. }
  302. int io_run_local_work_locked(struct io_ring_ctx *ctx, int min_events)
  303. {
  304. struct io_tw_state ts = {};
  305. if (!io_local_work_pending(ctx))
  306. return 0;
  307. return __io_run_local_work(ctx, ts, min_events,
  308. max(IO_LOCAL_TW_DEFAULT_MAX, min_events));
  309. }
  310. int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events)
  311. {
  312. struct io_tw_state ts = {};
  313. int ret;
  314. mutex_lock(&ctx->uring_lock);
  315. ret = __io_run_local_work(ctx, ts, min_events, max_events);
  316. mutex_unlock(&ctx->uring_lock);
  317. return ret;
  318. }