io_uring.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #ifndef IOU_CORE_H
  2. #define IOU_CORE_H
  3. #include <linux/errno.h>
  4. #include <linux/lockdep.h>
  5. #include <linux/resume_user_mode.h>
  6. #include <linux/kasan.h>
  7. #include <linux/poll.h>
  8. #include <linux/io_uring_types.h>
  9. #include <uapi/linux/eventpoll.h>
  10. #include "io-wq.h"
  11. #include "slist.h"
  12. #include "filetable.h"
  13. #ifndef CREATE_TRACE_POINTS
  14. #include <trace/events/io_uring.h>
  15. #endif
  16. enum {
  17. IOU_OK = 0,
  18. IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED,
  19. /*
  20. * Requeue the task_work to restart operations on this request. The
  21. * actual value isn't important, should just be not an otherwise
  22. * valid error code, yet less than -MAX_ERRNO and valid internally.
  23. */
  24. IOU_REQUEUE = -3072,
  25. /*
  26. * Intended only when both IO_URING_F_MULTISHOT is passed
  27. * to indicate to the poll runner that multishot should be
  28. * removed and the result is set on req->cqe.res.
  29. */
  30. IOU_STOP_MULTISHOT = -ECANCELED,
  31. };
  32. struct io_wait_queue {
  33. struct wait_queue_entry wq;
  34. struct io_ring_ctx *ctx;
  35. unsigned cq_tail;
  36. unsigned cq_min_tail;
  37. unsigned nr_timeouts;
  38. int hit_timeout;
  39. ktime_t min_timeout;
  40. ktime_t timeout;
  41. struct hrtimer t;
  42. #ifdef CONFIG_NET_RX_BUSY_POLL
  43. ktime_t napi_busy_poll_dt;
  44. bool napi_prefer_busy_poll;
  45. #endif
  46. };
  47. static inline bool io_should_wake(struct io_wait_queue *iowq)
  48. {
  49. struct io_ring_ctx *ctx = iowq->ctx;
  50. int dist = READ_ONCE(ctx->rings->cq.tail) - (int) iowq->cq_tail;
  51. /*
  52. * Wake up if we have enough events, or if a timeout occurred since we
  53. * started waiting. For timeouts, we always want to return to userspace,
  54. * regardless of event count.
  55. */
  56. return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
  57. }
  58. bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow);
  59. int io_run_task_work_sig(struct io_ring_ctx *ctx);
  60. void io_req_defer_failed(struct io_kiocb *req, s32 res);
  61. bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
  62. void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
  63. bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags);
  64. void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
  65. struct file *io_file_get_normal(struct io_kiocb *req, int fd);
  66. struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
  67. unsigned issue_flags);
  68. void __io_req_task_work_add(struct io_kiocb *req, unsigned flags);
  69. void io_req_task_work_add_remote(struct io_kiocb *req, struct io_ring_ctx *ctx,
  70. unsigned flags);
  71. bool io_alloc_async_data(struct io_kiocb *req);
  72. void io_req_task_queue(struct io_kiocb *req);
  73. void io_req_task_complete(struct io_kiocb *req, struct io_tw_state *ts);
  74. void io_req_task_queue_fail(struct io_kiocb *req, int ret);
  75. void io_req_task_submit(struct io_kiocb *req, struct io_tw_state *ts);
  76. struct llist_node *io_handle_tw_list(struct llist_node *node, unsigned int *count, unsigned int max_entries);
  77. struct llist_node *tctx_task_work_run(struct io_uring_task *tctx, unsigned int max_entries, unsigned int *count);
  78. void tctx_task_work(struct callback_head *cb);
  79. __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
  80. int io_uring_alloc_task_context(struct task_struct *task,
  81. struct io_ring_ctx *ctx);
  82. int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
  83. int start, int end);
  84. void io_req_queue_iowq(struct io_kiocb *req);
  85. int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts);
  86. int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
  87. int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
  88. void __io_submit_flush_completions(struct io_ring_ctx *ctx);
  89. struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
  90. void io_wq_submit_work(struct io_wq_work *work);
  91. void io_free_req(struct io_kiocb *req);
  92. void io_queue_next(struct io_kiocb *req);
  93. void io_task_refs_refill(struct io_uring_task *tctx);
  94. bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
  95. bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
  96. bool cancel_all);
  97. void io_activate_pollwq(struct io_ring_ctx *ctx);
  98. static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
  99. {
  100. #if defined(CONFIG_PROVE_LOCKING)
  101. lockdep_assert(in_task());
  102. if (ctx->flags & IORING_SETUP_IOPOLL) {
  103. lockdep_assert_held(&ctx->uring_lock);
  104. } else if (!ctx->task_complete) {
  105. lockdep_assert_held(&ctx->completion_lock);
  106. } else if (ctx->submitter_task) {
  107. /*
  108. * ->submitter_task may be NULL and we can still post a CQE,
  109. * if the ring has been setup with IORING_SETUP_R_DISABLED.
  110. * Not from an SQE, as those cannot be submitted, but via
  111. * updating tagged resources.
  112. */
  113. if (ctx->submitter_task->flags & PF_EXITING)
  114. lockdep_assert(current_work());
  115. else
  116. lockdep_assert(current == ctx->submitter_task);
  117. }
  118. #endif
  119. }
  120. static inline void io_req_task_work_add(struct io_kiocb *req)
  121. {
  122. __io_req_task_work_add(req, 0);
  123. }
  124. static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
  125. {
  126. if (!wq_list_empty(&ctx->submit_state.compl_reqs) ||
  127. ctx->submit_state.cq_flush)
  128. __io_submit_flush_completions(ctx);
  129. }
  130. #define io_for_each_link(pos, head) \
  131. for (pos = (head); pos; pos = pos->link)
  132. static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx,
  133. struct io_uring_cqe **ret,
  134. bool overflow)
  135. {
  136. io_lockdep_assert_cq_locked(ctx);
  137. if (unlikely(ctx->cqe_cached >= ctx->cqe_sentinel)) {
  138. if (unlikely(!io_cqe_cache_refill(ctx, overflow)))
  139. return false;
  140. }
  141. *ret = ctx->cqe_cached;
  142. ctx->cached_cq_tail++;
  143. ctx->cqe_cached++;
  144. if (ctx->flags & IORING_SETUP_CQE32)
  145. ctx->cqe_cached++;
  146. return true;
  147. }
  148. static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret)
  149. {
  150. return io_get_cqe_overflow(ctx, ret, false);
  151. }
  152. static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,
  153. struct io_kiocb *req)
  154. {
  155. struct io_uring_cqe *cqe;
  156. /*
  157. * If we can't get a cq entry, userspace overflowed the
  158. * submission (by quite a lot). Increment the overflow count in
  159. * the ring.
  160. */
  161. if (unlikely(!io_get_cqe(ctx, &cqe)))
  162. return false;
  163. if (trace_io_uring_complete_enabled())
  164. trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
  165. req->cqe.res, req->cqe.flags,
  166. req->big_cqe.extra1, req->big_cqe.extra2);
  167. memcpy(cqe, &req->cqe, sizeof(*cqe));
  168. if (ctx->flags & IORING_SETUP_CQE32) {
  169. memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe));
  170. memset(&req->big_cqe, 0, sizeof(req->big_cqe));
  171. }
  172. return true;
  173. }
  174. static inline void req_set_fail(struct io_kiocb *req)
  175. {
  176. req->flags |= REQ_F_FAIL;
  177. if (req->flags & REQ_F_CQE_SKIP) {
  178. req->flags &= ~REQ_F_CQE_SKIP;
  179. req->flags |= REQ_F_SKIP_LINK_CQES;
  180. }
  181. }
  182. static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
  183. {
  184. req->cqe.res = res;
  185. req->cqe.flags = cflags;
  186. }
  187. static inline bool req_has_async_data(struct io_kiocb *req)
  188. {
  189. return req->flags & REQ_F_ASYNC_DATA;
  190. }
  191. static inline void io_put_file(struct io_kiocb *req)
  192. {
  193. if (!(req->flags & REQ_F_FIXED_FILE) && req->file)
  194. fput(req->file);
  195. }
  196. static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
  197. unsigned issue_flags)
  198. {
  199. lockdep_assert_held(&ctx->uring_lock);
  200. if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
  201. mutex_unlock(&ctx->uring_lock);
  202. }
  203. static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
  204. unsigned issue_flags)
  205. {
  206. /*
  207. * "Normal" inline submissions always hold the uring_lock, since we
  208. * grab it from the system call. Same is true for the SQPOLL offload.
  209. * The only exception is when we've detached the request and issue it
  210. * from an async worker thread, grab the lock for that case.
  211. */
  212. if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
  213. mutex_lock(&ctx->uring_lock);
  214. lockdep_assert_held(&ctx->uring_lock);
  215. }
  216. static inline void io_commit_cqring(struct io_ring_ctx *ctx)
  217. {
  218. /* order cqe stores with ring update */
  219. smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
  220. }
  221. static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
  222. {
  223. if (wq_has_sleeper(&ctx->poll_wq))
  224. __wake_up(&ctx->poll_wq, TASK_NORMAL, 0,
  225. poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
  226. }
  227. static inline void io_cqring_wake(struct io_ring_ctx *ctx)
  228. {
  229. /*
  230. * Trigger waitqueue handler on all waiters on our waitqueue. This
  231. * won't necessarily wake up all the tasks, io_should_wake() will make
  232. * that decision.
  233. *
  234. * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
  235. * set in the mask so that if we recurse back into our own poll
  236. * waitqueue handlers, we know we have a dependency between eventfd or
  237. * epoll and should terminate multishot poll at that point.
  238. */
  239. if (wq_has_sleeper(&ctx->cq_wait))
  240. __wake_up(&ctx->cq_wait, TASK_NORMAL, 0,
  241. poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
  242. }
  243. static inline bool io_sqring_full(struct io_ring_ctx *ctx)
  244. {
  245. struct io_rings *r = ctx->rings;
  246. /*
  247. * SQPOLL must use the actual sqring head, as using the cached_sq_head
  248. * is race prone if the SQPOLL thread has grabbed entries but not yet
  249. * committed them to the ring. For !SQPOLL, this doesn't matter, but
  250. * since this helper is just used for SQPOLL sqring waits (or POLLOUT),
  251. * just read the actual sqring head unconditionally.
  252. */
  253. return READ_ONCE(r->sq.tail) - READ_ONCE(r->sq.head) == ctx->sq_entries;
  254. }
  255. static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
  256. {
  257. struct io_rings *rings = ctx->rings;
  258. unsigned int entries;
  259. /* make sure SQ entry isn't read before tail */
  260. entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
  261. return min(entries, ctx->sq_entries);
  262. }
  263. static inline int io_run_task_work(void)
  264. {
  265. bool ret = false;
  266. /*
  267. * Always check-and-clear the task_work notification signal. With how
  268. * signaling works for task_work, we can find it set with nothing to
  269. * run. We need to clear it for that case, like get_signal() does.
  270. */
  271. if (test_thread_flag(TIF_NOTIFY_SIGNAL))
  272. clear_notify_signal();
  273. /*
  274. * PF_IO_WORKER never returns to userspace, so check here if we have
  275. * notify work that needs processing.
  276. */
  277. if (current->flags & PF_IO_WORKER) {
  278. if (test_thread_flag(TIF_NOTIFY_RESUME)) {
  279. __set_current_state(TASK_RUNNING);
  280. resume_user_mode_work(NULL);
  281. }
  282. if (current->io_uring) {
  283. unsigned int count = 0;
  284. __set_current_state(TASK_RUNNING);
  285. tctx_task_work_run(current->io_uring, UINT_MAX, &count);
  286. if (count)
  287. ret = true;
  288. }
  289. }
  290. if (task_work_pending(current)) {
  291. __set_current_state(TASK_RUNNING);
  292. task_work_run();
  293. ret = true;
  294. }
  295. return ret;
  296. }
  297. static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
  298. {
  299. return task_work_pending(current) || !llist_empty(&ctx->work_llist);
  300. }
  301. static inline void io_tw_lock(struct io_ring_ctx *ctx, struct io_tw_state *ts)
  302. {
  303. lockdep_assert_held(&ctx->uring_lock);
  304. }
  305. /*
  306. * Don't complete immediately but use deferred completion infrastructure.
  307. * Protected by ->uring_lock and can only be used either with
  308. * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
  309. */
  310. static inline void io_req_complete_defer(struct io_kiocb *req)
  311. __must_hold(&req->ctx->uring_lock)
  312. {
  313. struct io_submit_state *state = &req->ctx->submit_state;
  314. lockdep_assert_held(&req->ctx->uring_lock);
  315. wq_list_add_tail(&req->comp_list, &state->compl_reqs);
  316. }
  317. static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
  318. {
  319. if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
  320. ctx->has_evfd || ctx->poll_activated))
  321. __io_commit_cqring_flush(ctx);
  322. }
  323. static inline void io_get_task_refs(int nr)
  324. {
  325. struct io_uring_task *tctx = current->io_uring;
  326. tctx->cached_refs -= nr;
  327. if (unlikely(tctx->cached_refs < 0))
  328. io_task_refs_refill(tctx);
  329. }
  330. static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
  331. {
  332. return !ctx->submit_state.free_list.next;
  333. }
  334. extern struct kmem_cache *req_cachep;
  335. extern struct kmem_cache *io_buf_cachep;
  336. static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx)
  337. {
  338. struct io_kiocb *req;
  339. req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list);
  340. wq_stack_extract(&ctx->submit_state.free_list);
  341. return req;
  342. }
  343. static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req)
  344. {
  345. if (unlikely(io_req_cache_empty(ctx))) {
  346. if (!__io_alloc_req_refill(ctx))
  347. return false;
  348. }
  349. *req = io_extract_req(ctx);
  350. return true;
  351. }
  352. static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
  353. {
  354. return likely(ctx->submitter_task == current);
  355. }
  356. static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
  357. {
  358. return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
  359. ctx->submitter_task == current);
  360. }
  361. static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
  362. {
  363. io_req_set_res(req, res, 0);
  364. req->io_task_work.func = io_req_task_complete;
  365. io_req_task_work_add(req);
  366. }
  367. /*
  368. * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each
  369. * slot.
  370. */
  371. static inline size_t uring_sqe_size(struct io_ring_ctx *ctx)
  372. {
  373. if (ctx->flags & IORING_SETUP_SQE128)
  374. return 2 * sizeof(struct io_uring_sqe);
  375. return sizeof(struct io_uring_sqe);
  376. }
  377. static inline bool io_file_can_poll(struct io_kiocb *req)
  378. {
  379. if (req->flags & REQ_F_CAN_POLL)
  380. return true;
  381. if (req->file && file_can_poll(req->file)) {
  382. req->flags |= REQ_F_CAN_POLL;
  383. return true;
  384. }
  385. return false;
  386. }
  387. static inline ktime_t io_get_time(struct io_ring_ctx *ctx)
  388. {
  389. if (ctx->clockid == CLOCK_MONOTONIC)
  390. return ktime_get();
  391. return ktime_get_with_offset(ctx->clock_offset);
  392. }
  393. enum {
  394. IO_CHECK_CQ_OVERFLOW_BIT,
  395. IO_CHECK_CQ_DROPPED_BIT,
  396. };
  397. static inline bool io_has_work(struct io_ring_ctx *ctx)
  398. {
  399. return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) ||
  400. !llist_empty(&ctx->work_llist);
  401. }
  402. #endif