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" 8*a6b21fbbSPavel Begunkov #include "slist.h" 9ab1c84d8SPavel Begunkov #include "filetable.h" 10de23077eSJens Axboe 11f3b44f92SJens Axboe #ifndef CREATE_TRACE_POINTS 12f3b44f92SJens Axboe #include <trace/events/io_uring.h> 13f3b44f92SJens Axboe #endif 14f3b44f92SJens Axboe 1597b388d7SJens Axboe enum { 1697b388d7SJens Axboe IOU_OK = 0, 1797b388d7SJens Axboe IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED, 1897b388d7SJens Axboe }; 1997b388d7SJens Axboe 20faf88ddeSPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx); 2168494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req); 229046c641SPavel Begunkov int io_run_task_work_sig(void); 239046c641SPavel Begunkov void io_req_complete_failed(struct io_kiocb *req, s32 res); 249046c641SPavel Begunkov void __io_req_complete(struct io_kiocb *req, unsigned issue_flags); 259046c641SPavel Begunkov void io_req_complete_post(struct io_kiocb *req); 269046c641SPavel Begunkov void __io_req_complete_post(struct io_kiocb *req); 279046c641SPavel Begunkov bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 289046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx); 299046c641SPavel Begunkov 309046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages); 319046c641SPavel Begunkov 329046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd); 339046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 349046c641SPavel Begunkov unsigned issue_flags); 359046c641SPavel Begunkov 369046c641SPavel Begunkov bool io_is_uring_fops(struct file *file); 379046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req); 389046c641SPavel Begunkov void io_req_task_work_add(struct io_kiocb *req); 399046c641SPavel Begunkov void io_req_task_prio_work_add(struct io_kiocb *req); 409046c641SPavel Begunkov void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags); 419046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req); 429046c641SPavel Begunkov void io_queue_iowq(struct io_kiocb *req, bool *dont_use); 439046c641SPavel Begunkov void io_req_task_complete(struct io_kiocb *req, bool *locked); 449046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret); 459046c641SPavel Begunkov void io_req_task_submit(struct io_kiocb *req, bool *locked); 469046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb); 479046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); 489046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task, 499046c641SPavel Begunkov struct io_ring_ctx *ctx); 509046c641SPavel Begunkov 519046c641SPavel Begunkov int io_poll_issue(struct io_kiocb *req, bool *locked); 529046c641SPavel Begunkov int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); 539046c641SPavel Begunkov int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); 549046c641SPavel Begunkov void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node); 559046c641SPavel Begunkov int io_req_prep_async(struct io_kiocb *req); 569046c641SPavel Begunkov 579046c641SPavel Begunkov struct io_wq_work *io_wq_free_work(struct io_wq_work *work); 589046c641SPavel Begunkov void io_wq_submit_work(struct io_wq_work *work); 599046c641SPavel Begunkov 609046c641SPavel Begunkov void io_free_req(struct io_kiocb *req); 619046c641SPavel Begunkov void io_queue_next(struct io_kiocb *req); 629046c641SPavel Begunkov 639046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, 649046c641SPavel Begunkov bool cancel_all); 659046c641SPavel Begunkov 669046c641SPavel Begunkov #define io_for_each_link(pos, head) \ 679046c641SPavel Begunkov for (pos = (head); pos; pos = pos->link) 68f3b44f92SJens Axboe 6925399321SPavel Begunkov static inline void io_cq_lock(struct io_ring_ctx *ctx) 7025399321SPavel Begunkov __acquires(ctx->completion_lock) 7125399321SPavel Begunkov { 7225399321SPavel Begunkov spin_lock(&ctx->completion_lock); 7325399321SPavel Begunkov } 7425399321SPavel Begunkov 7525399321SPavel Begunkov void io_cq_unlock_post(struct io_ring_ctx *ctx); 7625399321SPavel Begunkov 77f3b44f92SJens Axboe static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) 78f3b44f92SJens Axboe { 79f3b44f92SJens Axboe if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) { 80f3b44f92SJens Axboe struct io_uring_cqe *cqe = ctx->cqe_cached; 81f3b44f92SJens Axboe 82f3b44f92SJens Axboe ctx->cached_cq_tail++; 83f3b44f92SJens Axboe ctx->cqe_cached++; 84b3659a65SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32) 85b3659a65SPavel Begunkov ctx->cqe_cached++; 86f3b44f92SJens Axboe return cqe; 87f3b44f92SJens Axboe } 88f3b44f92SJens Axboe 89f3b44f92SJens Axboe return __io_get_cqe(ctx); 90f3b44f92SJens Axboe } 91f3b44f92SJens Axboe 92f3b44f92SJens Axboe static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx, 93f3b44f92SJens Axboe struct io_kiocb *req) 94f3b44f92SJens Axboe { 95f3b44f92SJens Axboe struct io_uring_cqe *cqe; 96f3b44f92SJens Axboe 97f3b44f92SJens Axboe trace_io_uring_complete(req->ctx, req, req->cqe.user_data, 98ae5735c6SPavel Begunkov req->cqe.res, req->cqe.flags, 99ae5735c6SPavel Begunkov (req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0, 100ae5735c6SPavel Begunkov (req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0); 101f3b44f92SJens Axboe /* 102f3b44f92SJens Axboe * If we can't get a cq entry, userspace overflowed the 103f3b44f92SJens Axboe * submission (by quite a lot). Increment the overflow count in 104f3b44f92SJens Axboe * the ring. 105f3b44f92SJens Axboe */ 106f3b44f92SJens Axboe cqe = io_get_cqe(ctx); 107e8c328c3SPavel Begunkov if (unlikely(!cqe)) 108e8c328c3SPavel Begunkov return io_req_cqe_overflow(req); 109f3b44f92SJens Axboe memcpy(cqe, &req->cqe, sizeof(*cqe)); 110e8c328c3SPavel Begunkov 111e8c328c3SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32) { 112f3b44f92SJens Axboe u64 extra1 = 0, extra2 = 0; 113f3b44f92SJens Axboe 114f3b44f92SJens Axboe if (req->flags & REQ_F_CQE32_INIT) { 115f3b44f92SJens Axboe extra1 = req->extra1; 116f3b44f92SJens Axboe extra2 = req->extra2; 117f3b44f92SJens Axboe } 118f3b44f92SJens Axboe 119f3b44f92SJens Axboe WRITE_ONCE(cqe->big_cqe[0], extra1); 120f3b44f92SJens Axboe WRITE_ONCE(cqe->big_cqe[1], extra2); 121e8c328c3SPavel Begunkov } 122f3b44f92SJens Axboe return true; 123f3b44f92SJens Axboe } 124f3b44f92SJens Axboe 125531113bbSJens Axboe static inline void req_set_fail(struct io_kiocb *req) 126531113bbSJens Axboe { 127531113bbSJens Axboe req->flags |= REQ_F_FAIL; 128531113bbSJens Axboe if (req->flags & REQ_F_CQE_SKIP) { 129531113bbSJens Axboe req->flags &= ~REQ_F_CQE_SKIP; 130531113bbSJens Axboe req->flags |= REQ_F_SKIP_LINK_CQES; 131531113bbSJens Axboe } 132531113bbSJens Axboe } 133531113bbSJens Axboe 134de23077eSJens Axboe static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags) 135de23077eSJens Axboe { 136de23077eSJens Axboe req->cqe.res = res; 137de23077eSJens Axboe req->cqe.flags = cflags; 138de23077eSJens Axboe } 139de23077eSJens Axboe 14099f15d8dSJens Axboe static inline bool req_has_async_data(struct io_kiocb *req) 14199f15d8dSJens Axboe { 14299f15d8dSJens Axboe return req->flags & REQ_F_ASYNC_DATA; 14399f15d8dSJens Axboe } 14499f15d8dSJens Axboe 145531113bbSJens Axboe static inline void io_put_file(struct file *file) 146531113bbSJens Axboe { 147531113bbSJens Axboe if (file) 148531113bbSJens Axboe fput(file); 149531113bbSJens Axboe } 150531113bbSJens Axboe 151cd40cae2SJens Axboe static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, 152cd40cae2SJens Axboe unsigned issue_flags) 153cd40cae2SJens Axboe { 154cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock); 155cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED) 156cd40cae2SJens Axboe mutex_unlock(&ctx->uring_lock); 157cd40cae2SJens Axboe } 158cd40cae2SJens Axboe 159cd40cae2SJens Axboe static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, 160cd40cae2SJens Axboe unsigned issue_flags) 161cd40cae2SJens Axboe { 162cd40cae2SJens Axboe /* 163cd40cae2SJens Axboe * "Normal" inline submissions always hold the uring_lock, since we 164cd40cae2SJens Axboe * grab it from the system call. Same is true for the SQPOLL offload. 165cd40cae2SJens Axboe * The only exception is when we've detached the request and issue it 166cd40cae2SJens Axboe * from an async worker thread, grab the lock for that case. 167cd40cae2SJens Axboe */ 168cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED) 169cd40cae2SJens Axboe mutex_lock(&ctx->uring_lock); 170cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock); 171cd40cae2SJens Axboe } 172cd40cae2SJens Axboe 173f9ead18cSJens Axboe static inline void io_commit_cqring(struct io_ring_ctx *ctx) 174f9ead18cSJens Axboe { 175f9ead18cSJens Axboe /* order cqe stores with ring update */ 176f9ead18cSJens Axboe smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); 177f9ead18cSJens Axboe } 178f9ead18cSJens Axboe 179f3b44f92SJens Axboe static inline void io_cqring_wake(struct io_ring_ctx *ctx) 180f3b44f92SJens Axboe { 181f3b44f92SJens Axboe /* 182f3b44f92SJens Axboe * wake_up_all() may seem excessive, but io_wake_function() and 183f3b44f92SJens Axboe * io_should_wake() handle the termination of the loop and only 184f3b44f92SJens Axboe * wake as many waiters as we need to. 185f3b44f92SJens Axboe */ 186f3b44f92SJens Axboe if (wq_has_sleeper(&ctx->cq_wait)) 187f3b44f92SJens Axboe wake_up_all(&ctx->cq_wait); 188f3b44f92SJens Axboe } 189f3b44f92SJens Axboe 19017437f31SJens Axboe static inline bool io_sqring_full(struct io_ring_ctx *ctx) 19117437f31SJens Axboe { 19217437f31SJens Axboe struct io_rings *r = ctx->rings; 19317437f31SJens Axboe 19417437f31SJens Axboe return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; 19517437f31SJens Axboe } 19617437f31SJens Axboe 19717437f31SJens Axboe static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) 19817437f31SJens Axboe { 19917437f31SJens Axboe struct io_rings *rings = ctx->rings; 20017437f31SJens Axboe 20117437f31SJens Axboe /* make sure SQ entry isn't read before tail */ 20217437f31SJens Axboe return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; 20317437f31SJens Axboe } 20417437f31SJens Axboe 20517437f31SJens Axboe static inline bool io_run_task_work(void) 20617437f31SJens Axboe { 207625d38b3SPavel Begunkov if (test_thread_flag(TIF_NOTIFY_SIGNAL)) { 20817437f31SJens Axboe __set_current_state(TASK_RUNNING); 20917437f31SJens Axboe clear_notify_signal(); 21017437f31SJens Axboe if (task_work_pending(current)) 21117437f31SJens Axboe task_work_run(); 21217437f31SJens Axboe return true; 21317437f31SJens Axboe } 21417437f31SJens Axboe 21517437f31SJens Axboe return false; 21617437f31SJens Axboe } 21717437f31SJens Axboe 218aa1e90f6SPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) 219aa1e90f6SPavel Begunkov { 220aa1e90f6SPavel Begunkov if (!*locked) { 221aa1e90f6SPavel Begunkov mutex_lock(&ctx->uring_lock); 222aa1e90f6SPavel Begunkov *locked = true; 223aa1e90f6SPavel Begunkov } 224aa1e90f6SPavel Begunkov } 225aa1e90f6SPavel Begunkov 2269da070b1SPavel Begunkov /* 2279da070b1SPavel Begunkov * Don't complete immediately but use deferred completion infrastructure. 2289da070b1SPavel Begunkov * Protected by ->uring_lock and can only be used either with 2299da070b1SPavel Begunkov * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex. 2309da070b1SPavel Begunkov */ 2319da070b1SPavel Begunkov static inline void io_req_complete_defer(struct io_kiocb *req) 2329da070b1SPavel Begunkov __must_hold(&req->ctx->uring_lock) 233aa1e90f6SPavel Begunkov { 234aa1e90f6SPavel Begunkov struct io_submit_state *state = &req->ctx->submit_state; 235aa1e90f6SPavel Begunkov 2369da070b1SPavel Begunkov lockdep_assert_held(&req->ctx->uring_lock); 2379da070b1SPavel Begunkov 238aa1e90f6SPavel Begunkov wq_list_add_tail(&req->comp_list, &state->compl_reqs); 239aa1e90f6SPavel Begunkov } 240aa1e90f6SPavel Begunkov 24146929b08SPavel Begunkov static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx) 24246929b08SPavel Begunkov { 24346929b08SPavel Begunkov if (unlikely(ctx->off_timeout_used || ctx->drain_active || ctx->has_evfd)) 24446929b08SPavel Begunkov __io_commit_cqring_flush(ctx); 24546929b08SPavel Begunkov } 24646929b08SPavel Begunkov 247de23077eSJens Axboe #endif 248