1de23077eSJens Axboe #ifndef IOU_CORE_H 2de23077eSJens Axboe #define IOU_CORE_H 3de23077eSJens Axboe 4de23077eSJens Axboe #include <linux/errno.h> 5cd40cae2SJens Axboe #include <linux/lockdep.h> 6ab1c84d8SPavel Begunkov #include <linux/io_uring_types.h> 7ab1c84d8SPavel Begunkov #include "io-wq.h" 8ab1c84d8SPavel Begunkov #include "filetable.h" 9de23077eSJens Axboe 10f3b44f92SJens Axboe #ifndef CREATE_TRACE_POINTS 11f3b44f92SJens Axboe #include <trace/events/io_uring.h> 12f3b44f92SJens Axboe #endif 13f3b44f92SJens Axboe 1497b388d7SJens Axboe enum { 1597b388d7SJens Axboe IOU_OK = 0, 1697b388d7SJens Axboe IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED, 1797b388d7SJens Axboe }; 1897b388d7SJens Axboe 19faf88ddeSPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx); 2068494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req); 21*9046c641SPavel Begunkov int io_run_task_work_sig(void); 22*9046c641SPavel Begunkov void io_req_complete_failed(struct io_kiocb *req, s32 res); 23*9046c641SPavel Begunkov void __io_req_complete(struct io_kiocb *req, unsigned issue_flags); 24*9046c641SPavel Begunkov void io_req_complete_post(struct io_kiocb *req); 25*9046c641SPavel Begunkov void __io_req_complete_post(struct io_kiocb *req); 26*9046c641SPavel Begunkov bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 27*9046c641SPavel Begunkov void io_cqring_ev_posted(struct io_ring_ctx *ctx); 28*9046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx); 29*9046c641SPavel Begunkov 30*9046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages); 31*9046c641SPavel Begunkov 32*9046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd); 33*9046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 34*9046c641SPavel Begunkov unsigned issue_flags); 35*9046c641SPavel Begunkov 36*9046c641SPavel Begunkov bool io_is_uring_fops(struct file *file); 37*9046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req); 38*9046c641SPavel Begunkov void io_req_task_work_add(struct io_kiocb *req); 39*9046c641SPavel Begunkov void io_req_task_prio_work_add(struct io_kiocb *req); 40*9046c641SPavel Begunkov void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags); 41*9046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req); 42*9046c641SPavel Begunkov void io_queue_iowq(struct io_kiocb *req, bool *dont_use); 43*9046c641SPavel Begunkov void io_req_task_complete(struct io_kiocb *req, bool *locked); 44*9046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret); 45*9046c641SPavel Begunkov void io_req_task_submit(struct io_kiocb *req, bool *locked); 46*9046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb); 47*9046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); 48*9046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task, 49*9046c641SPavel Begunkov struct io_ring_ctx *ctx); 50*9046c641SPavel Begunkov 51*9046c641SPavel Begunkov int io_poll_issue(struct io_kiocb *req, bool *locked); 52*9046c641SPavel Begunkov int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); 53*9046c641SPavel Begunkov int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); 54*9046c641SPavel Begunkov void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node); 55*9046c641SPavel Begunkov int io_req_prep_async(struct io_kiocb *req); 56*9046c641SPavel Begunkov 57*9046c641SPavel Begunkov struct io_wq_work *io_wq_free_work(struct io_wq_work *work); 58*9046c641SPavel Begunkov void io_wq_submit_work(struct io_wq_work *work); 59*9046c641SPavel Begunkov 60*9046c641SPavel Begunkov void io_free_req(struct io_kiocb *req); 61*9046c641SPavel Begunkov void io_queue_next(struct io_kiocb *req); 62*9046c641SPavel Begunkov 63*9046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, 64*9046c641SPavel Begunkov bool cancel_all); 65*9046c641SPavel Begunkov 66*9046c641SPavel Begunkov #define io_for_each_link(pos, head) \ 67*9046c641SPavel Begunkov for (pos = (head); pos; pos = pos->link) 68f3b44f92SJens Axboe 69f3b44f92SJens Axboe static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) 70f3b44f92SJens Axboe { 71f3b44f92SJens Axboe if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) { 72f3b44f92SJens Axboe struct io_uring_cqe *cqe = ctx->cqe_cached; 73f3b44f92SJens Axboe 74f3b44f92SJens Axboe ctx->cached_cq_tail++; 75f3b44f92SJens Axboe ctx->cqe_cached++; 76b3659a65SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32) 77b3659a65SPavel Begunkov ctx->cqe_cached++; 78f3b44f92SJens Axboe return cqe; 79f3b44f92SJens Axboe } 80f3b44f92SJens Axboe 81f3b44f92SJens Axboe return __io_get_cqe(ctx); 82f3b44f92SJens Axboe } 83f3b44f92SJens Axboe 84f3b44f92SJens Axboe static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx, 85f3b44f92SJens Axboe struct io_kiocb *req) 86f3b44f92SJens Axboe { 87f3b44f92SJens Axboe struct io_uring_cqe *cqe; 88f3b44f92SJens Axboe 89f3b44f92SJens Axboe trace_io_uring_complete(req->ctx, req, req->cqe.user_data, 90ae5735c6SPavel Begunkov req->cqe.res, req->cqe.flags, 91ae5735c6SPavel Begunkov (req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0, 92ae5735c6SPavel Begunkov (req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0); 93f3b44f92SJens Axboe /* 94f3b44f92SJens Axboe * If we can't get a cq entry, userspace overflowed the 95f3b44f92SJens Axboe * submission (by quite a lot). Increment the overflow count in 96f3b44f92SJens Axboe * the ring. 97f3b44f92SJens Axboe */ 98f3b44f92SJens Axboe cqe = io_get_cqe(ctx); 99e8c328c3SPavel Begunkov if (unlikely(!cqe)) 100e8c328c3SPavel Begunkov return io_req_cqe_overflow(req); 101f3b44f92SJens Axboe memcpy(cqe, &req->cqe, sizeof(*cqe)); 102e8c328c3SPavel Begunkov 103e8c328c3SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32) { 104f3b44f92SJens Axboe u64 extra1 = 0, extra2 = 0; 105f3b44f92SJens Axboe 106f3b44f92SJens Axboe if (req->flags & REQ_F_CQE32_INIT) { 107f3b44f92SJens Axboe extra1 = req->extra1; 108f3b44f92SJens Axboe extra2 = req->extra2; 109f3b44f92SJens Axboe } 110f3b44f92SJens Axboe 111f3b44f92SJens Axboe WRITE_ONCE(cqe->big_cqe[0], extra1); 112f3b44f92SJens Axboe WRITE_ONCE(cqe->big_cqe[1], extra2); 113e8c328c3SPavel Begunkov } 114f3b44f92SJens Axboe return true; 115f3b44f92SJens Axboe } 116f3b44f92SJens Axboe 117531113bbSJens Axboe static inline void req_set_fail(struct io_kiocb *req) 118531113bbSJens Axboe { 119531113bbSJens Axboe req->flags |= REQ_F_FAIL; 120531113bbSJens Axboe if (req->flags & REQ_F_CQE_SKIP) { 121531113bbSJens Axboe req->flags &= ~REQ_F_CQE_SKIP; 122531113bbSJens Axboe req->flags |= REQ_F_SKIP_LINK_CQES; 123531113bbSJens Axboe } 124531113bbSJens Axboe } 125531113bbSJens Axboe 126de23077eSJens Axboe static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags) 127de23077eSJens Axboe { 128de23077eSJens Axboe req->cqe.res = res; 129de23077eSJens Axboe req->cqe.flags = cflags; 130de23077eSJens Axboe } 131de23077eSJens Axboe 13299f15d8dSJens Axboe static inline bool req_has_async_data(struct io_kiocb *req) 13399f15d8dSJens Axboe { 13499f15d8dSJens Axboe return req->flags & REQ_F_ASYNC_DATA; 13599f15d8dSJens Axboe } 13699f15d8dSJens Axboe 137531113bbSJens Axboe static inline void io_put_file(struct file *file) 138531113bbSJens Axboe { 139531113bbSJens Axboe if (file) 140531113bbSJens Axboe fput(file); 141531113bbSJens Axboe } 142531113bbSJens Axboe 143cd40cae2SJens Axboe static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, 144cd40cae2SJens Axboe unsigned issue_flags) 145cd40cae2SJens Axboe { 146cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock); 147cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED) 148cd40cae2SJens Axboe mutex_unlock(&ctx->uring_lock); 149cd40cae2SJens Axboe } 150cd40cae2SJens Axboe 151cd40cae2SJens Axboe static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, 152cd40cae2SJens Axboe unsigned issue_flags) 153cd40cae2SJens Axboe { 154cd40cae2SJens Axboe /* 155cd40cae2SJens Axboe * "Normal" inline submissions always hold the uring_lock, since we 156cd40cae2SJens Axboe * grab it from the system call. Same is true for the SQPOLL offload. 157cd40cae2SJens Axboe * The only exception is when we've detached the request and issue it 158cd40cae2SJens Axboe * from an async worker thread, grab the lock for that case. 159cd40cae2SJens Axboe */ 160cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED) 161cd40cae2SJens Axboe mutex_lock(&ctx->uring_lock); 162cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock); 163cd40cae2SJens Axboe } 164cd40cae2SJens Axboe 165f9ead18cSJens Axboe static inline void io_commit_cqring(struct io_ring_ctx *ctx) 166f9ead18cSJens Axboe { 167f9ead18cSJens Axboe /* order cqe stores with ring update */ 168f9ead18cSJens Axboe smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); 169f9ead18cSJens Axboe } 170f9ead18cSJens Axboe 171f3b44f92SJens Axboe static inline void io_cqring_wake(struct io_ring_ctx *ctx) 172f3b44f92SJens Axboe { 173f3b44f92SJens Axboe /* 174f3b44f92SJens Axboe * wake_up_all() may seem excessive, but io_wake_function() and 175f3b44f92SJens Axboe * io_should_wake() handle the termination of the loop and only 176f3b44f92SJens Axboe * wake as many waiters as we need to. 177f3b44f92SJens Axboe */ 178f3b44f92SJens Axboe if (wq_has_sleeper(&ctx->cq_wait)) 179f3b44f92SJens Axboe wake_up_all(&ctx->cq_wait); 180f3b44f92SJens Axboe } 181f3b44f92SJens Axboe 18217437f31SJens Axboe static inline bool io_sqring_full(struct io_ring_ctx *ctx) 18317437f31SJens Axboe { 18417437f31SJens Axboe struct io_rings *r = ctx->rings; 18517437f31SJens Axboe 18617437f31SJens Axboe return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; 18717437f31SJens Axboe } 18817437f31SJens Axboe 18917437f31SJens Axboe static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) 19017437f31SJens Axboe { 19117437f31SJens Axboe struct io_rings *rings = ctx->rings; 19217437f31SJens Axboe 19317437f31SJens Axboe /* make sure SQ entry isn't read before tail */ 19417437f31SJens Axboe return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; 19517437f31SJens Axboe } 19617437f31SJens Axboe 19717437f31SJens Axboe static inline bool io_run_task_work(void) 19817437f31SJens Axboe { 19917437f31SJens Axboe if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { 20017437f31SJens Axboe __set_current_state(TASK_RUNNING); 20117437f31SJens Axboe clear_notify_signal(); 20217437f31SJens Axboe if (task_work_pending(current)) 20317437f31SJens Axboe task_work_run(); 20417437f31SJens Axboe return true; 20517437f31SJens Axboe } 20617437f31SJens Axboe 20717437f31SJens Axboe return false; 20817437f31SJens Axboe } 20917437f31SJens Axboe 210aa1e90f6SPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) 211aa1e90f6SPavel Begunkov { 212aa1e90f6SPavel Begunkov if (!*locked) { 213aa1e90f6SPavel Begunkov mutex_lock(&ctx->uring_lock); 214aa1e90f6SPavel Begunkov *locked = true; 215aa1e90f6SPavel Begunkov } 216aa1e90f6SPavel Begunkov } 217aa1e90f6SPavel Begunkov 218aa1e90f6SPavel Begunkov static inline void io_req_add_compl_list(struct io_kiocb *req) 219aa1e90f6SPavel Begunkov { 220aa1e90f6SPavel Begunkov struct io_submit_state *state = &req->ctx->submit_state; 221aa1e90f6SPavel Begunkov 222aa1e90f6SPavel Begunkov if (!(req->flags & REQ_F_CQE_SKIP)) 223aa1e90f6SPavel Begunkov state->flush_cqes = true; 224aa1e90f6SPavel Begunkov wq_list_add_tail(&req->comp_list, &state->compl_reqs); 225aa1e90f6SPavel Begunkov } 226aa1e90f6SPavel Begunkov 227de23077eSJens Axboe #endif 228