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>
6b5d3ae20SJens Axboe #include <linux/resume_user_mode.h>
7c1755c25SBreno Leitao #include <linux/kasan.h>
8ab1c84d8SPavel Begunkov #include <linux/io_uring_types.h>
944648532SJens Axboe #include <uapi/linux/eventpoll.h>
10ab1c84d8SPavel Begunkov #include "io-wq.h"
11a6b21fbbSPavel Begunkov #include "slist.h"
12ab1c84d8SPavel Begunkov #include "filetable.h"
13de23077eSJens Axboe
14f3b44f92SJens Axboe #ifndef CREATE_TRACE_POINTS
15f3b44f92SJens Axboe #include <trace/events/io_uring.h>
16f3b44f92SJens Axboe #endif
17f3b44f92SJens Axboe
1897b388d7SJens Axboe enum {
198751d154SPavel Begunkov /*
208751d154SPavel Begunkov * A hint to not wake right away but delay until there are enough of
218751d154SPavel Begunkov * tw's queued to match the number of CQEs the task is waiting for.
228751d154SPavel Begunkov *
238751d154SPavel Begunkov * Must not be used wirh requests generating more than one CQE.
248751d154SPavel Begunkov * It's also ignored unless IORING_SETUP_DEFER_TASKRUN is set.
258751d154SPavel Begunkov */
2691c7884aSPavel Begunkov IOU_F_TWQ_LAZY_WAKE = 1,
278501fe70SPavel Begunkov };
288501fe70SPavel Begunkov
298501fe70SPavel Begunkov enum {
3097b388d7SJens Axboe IOU_OK = 0,
3197b388d7SJens Axboe IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED,
32114eccdfSDylan Yudaken
33114eccdfSDylan Yudaken /*
347cbd3aa5SJens Axboe * Requeue the task_work to restart operations on this request. The
357cbd3aa5SJens Axboe * actual value isn't important, should just be not an otherwise
367cbd3aa5SJens Axboe * valid error code, yet less than -MAX_ERRNO and valid internally.
377cbd3aa5SJens Axboe */
387cbd3aa5SJens Axboe IOU_REQUEUE = -3072,
397cbd3aa5SJens Axboe
407cbd3aa5SJens Axboe /*
4191482864SPavel Begunkov * Intended only when both IO_URING_F_MULTISHOT is passed
4291482864SPavel Begunkov * to indicate to the poll runner that multishot should be
43114eccdfSDylan Yudaken * removed and the result is set on req->cqe.res.
44114eccdfSDylan Yudaken */
45114eccdfSDylan Yudaken IOU_STOP_MULTISHOT = -ECANCELED,
4697b388d7SJens Axboe };
4797b388d7SJens Axboe
4820d6b633SPavel Begunkov bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow);
49056695bfSPavel Begunkov void io_req_cqe_overflow(struct io_kiocb *req);
50c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx);
51973fc83fSDylan Yudaken void io_req_defer_failed(struct io_kiocb *req, s32 res);
521bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags);
53b529c96aSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
54b6b2bb58SPavel Begunkov bool io_fill_cqe_req_aux(struct io_kiocb *req, bool defer, s32 res, u32 cflags);
559046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
569046c641SPavel Begunkov
579046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages);
589046c641SPavel Begunkov
599046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd);
609046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
619046c641SPavel Begunkov unsigned issue_flags);
629046c641SPavel Begunkov
638501fe70SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, unsigned flags);
649046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req);
659046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req);
66a282967cSPavel Begunkov void io_queue_iowq(struct io_kiocb *req, struct io_tw_state *ts_dont_use);
67a282967cSPavel Begunkov void io_req_task_complete(struct io_kiocb *req, struct io_tw_state *ts);
689046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret);
69a282967cSPavel Begunkov void io_req_task_submit(struct io_kiocb *req, struct io_tw_state *ts);
709046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb);
719046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
729046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task,
739046c641SPavel Begunkov struct io_ring_ctx *ctx);
749046c641SPavel Begunkov
756e76ac59SJosh Triplett int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
766e76ac59SJosh Triplett int start, int end);
776e76ac59SJosh Triplett
78a282967cSPavel Begunkov int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts);
799046c641SPavel Begunkov int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
809046c641SPavel Begunkov int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
81ec26c225SPavel Begunkov void __io_submit_flush_completions(struct io_ring_ctx *ctx);
829046c641SPavel Begunkov int io_req_prep_async(struct io_kiocb *req);
839046c641SPavel Begunkov
849046c641SPavel Begunkov struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
859046c641SPavel Begunkov void io_wq_submit_work(struct io_wq_work *work);
869046c641SPavel Begunkov
879046c641SPavel Begunkov void io_free_req(struct io_kiocb *req);
889046c641SPavel Begunkov void io_queue_next(struct io_kiocb *req);
8963809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx);
90bd1a3783SPavel Begunkov bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
919046c641SPavel Begunkov
929046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
939046c641SPavel Begunkov bool cancel_all);
949046c641SPavel Begunkov
954ca5f54aSJens Axboe void *io_mem_alloc(size_t size);
964ca5f54aSJens Axboe void io_mem_free(void *ptr);
974ca5f54aSJens Axboe
981658633cSJens Axboe #if defined(CONFIG_PROVE_LOCKING)
io_lockdep_assert_cq_locked(struct io_ring_ctx * ctx)991658633cSJens Axboe static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
1001658633cSJens Axboe {
1011658633cSJens Axboe lockdep_assert(in_task());
1021658633cSJens Axboe
1031658633cSJens Axboe if (ctx->flags & IORING_SETUP_IOPOLL) {
1041658633cSJens Axboe lockdep_assert_held(&ctx->uring_lock);
1051658633cSJens Axboe } else if (!ctx->task_complete) {
1061658633cSJens Axboe lockdep_assert_held(&ctx->completion_lock);
1071658633cSJens Axboe } else if (ctx->submitter_task) {
1081658633cSJens Axboe /*
1091658633cSJens Axboe * ->submitter_task may be NULL and we can still post a CQE,
1101658633cSJens Axboe * if the ring has been setup with IORING_SETUP_R_DISABLED.
1111658633cSJens Axboe * Not from an SQE, as those cannot be submitted, but via
1121658633cSJens Axboe * updating tagged resources.
1131658633cSJens Axboe */
1141658633cSJens Axboe if (ctx->submitter_task->flags & PF_EXITING)
1151658633cSJens Axboe lockdep_assert(current_work());
1161658633cSJens Axboe else
1171658633cSJens Axboe lockdep_assert(current == ctx->submitter_task);
1181658633cSJens Axboe }
1191658633cSJens Axboe }
1201658633cSJens Axboe #else
io_lockdep_assert_cq_locked(struct io_ring_ctx * ctx)1211658633cSJens Axboe static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx)
1221658633cSJens Axboe {
1231658633cSJens Axboe }
1241658633cSJens Axboe #endif
125f26cc959SPavel Begunkov
io_req_task_work_add(struct io_kiocb * req)126e52d2e58SPavel Begunkov static inline void io_req_task_work_add(struct io_kiocb *req)
127e52d2e58SPavel Begunkov {
1288501fe70SPavel Begunkov __io_req_task_work_add(req, 0);
129e52d2e58SPavel Begunkov }
130e52d2e58SPavel Begunkov
1319046c641SPavel Begunkov #define io_for_each_link(pos, head) \
1329046c641SPavel Begunkov for (pos = (head); pos; pos = pos->link)
133f3b44f92SJens Axboe
io_get_cqe_overflow(struct io_ring_ctx * ctx,struct io_uring_cqe ** ret,bool overflow)13459fbc409SPavel Begunkov static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx,
13559fbc409SPavel Begunkov struct io_uring_cqe **ret,
136aa1df3a3SPavel Begunkov bool overflow)
137f3b44f92SJens Axboe {
138f26cc959SPavel Begunkov io_lockdep_assert_cq_locked(ctx);
139f26cc959SPavel Begunkov
14020d6b633SPavel Begunkov if (unlikely(ctx->cqe_cached >= ctx->cqe_sentinel)) {
14120d6b633SPavel Begunkov if (unlikely(!io_cqe_cache_refill(ctx, overflow)))
14259fbc409SPavel Begunkov return false;
14320d6b633SPavel Begunkov }
14459fbc409SPavel Begunkov *ret = ctx->cqe_cached;
145f3b44f92SJens Axboe ctx->cached_cq_tail++;
146f3b44f92SJens Axboe ctx->cqe_cached++;
147b3659a65SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32)
148b3659a65SPavel Begunkov ctx->cqe_cached++;
14959fbc409SPavel Begunkov return true;
150f3b44f92SJens Axboe }
151f3b44f92SJens Axboe
io_get_cqe(struct io_ring_ctx * ctx,struct io_uring_cqe ** ret)15259fbc409SPavel Begunkov static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret)
153aa1df3a3SPavel Begunkov {
15459fbc409SPavel Begunkov return io_get_cqe_overflow(ctx, ret, false);
155f3b44f92SJens Axboe }
156f3b44f92SJens Axboe
io_fill_cqe_req(struct io_ring_ctx * ctx,struct io_kiocb * req)157093a650bSPavel Begunkov static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,
158093a650bSPavel Begunkov struct io_kiocb *req)
159f3b44f92SJens Axboe {
160f3b44f92SJens Axboe struct io_uring_cqe *cqe;
161f3b44f92SJens Axboe
162f3b44f92SJens Axboe /*
163f3b44f92SJens Axboe * If we can't get a cq entry, userspace overflowed the
164f3b44f92SJens Axboe * submission (by quite a lot). Increment the overflow count in
165f3b44f92SJens Axboe * the ring.
166f3b44f92SJens Axboe */
16759fbc409SPavel Begunkov if (unlikely(!io_get_cqe(ctx, &cqe)))
168f66f7342SPavel Begunkov return false;
169e0486f3fSDylan Yudaken
170a0727c73SPavel Begunkov if (trace_io_uring_complete_enabled())
171e0486f3fSDylan Yudaken trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
172e0486f3fSDylan Yudaken req->cqe.res, req->cqe.flags,
173b24c5d75SPavel Begunkov req->big_cqe.extra1, req->big_cqe.extra2);
174e0486f3fSDylan Yudaken
175f3b44f92SJens Axboe memcpy(cqe, &req->cqe, sizeof(*cqe));
176e8c328c3SPavel Begunkov if (ctx->flags & IORING_SETUP_CQE32) {
177b24c5d75SPavel Begunkov memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe));
178b24c5d75SPavel Begunkov memset(&req->big_cqe, 0, sizeof(req->big_cqe));
179e8c328c3SPavel Begunkov }
180f3b44f92SJens Axboe return true;
181f3b44f92SJens Axboe }
182f3b44f92SJens Axboe
req_set_fail(struct io_kiocb * req)183531113bbSJens Axboe static inline void req_set_fail(struct io_kiocb *req)
184531113bbSJens Axboe {
185531113bbSJens Axboe req->flags |= REQ_F_FAIL;
186531113bbSJens Axboe if (req->flags & REQ_F_CQE_SKIP) {
187531113bbSJens Axboe req->flags &= ~REQ_F_CQE_SKIP;
188531113bbSJens Axboe req->flags |= REQ_F_SKIP_LINK_CQES;
189531113bbSJens Axboe }
190531113bbSJens Axboe }
191531113bbSJens Axboe
io_req_set_res(struct io_kiocb * req,s32 res,u32 cflags)192de23077eSJens Axboe static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
193de23077eSJens Axboe {
194de23077eSJens Axboe req->cqe.res = res;
195de23077eSJens Axboe req->cqe.flags = cflags;
196de23077eSJens Axboe }
197de23077eSJens Axboe
req_has_async_data(struct io_kiocb * req)19899f15d8dSJens Axboe static inline bool req_has_async_data(struct io_kiocb *req)
19999f15d8dSJens Axboe {
20099f15d8dSJens Axboe return req->flags & REQ_F_ASYNC_DATA;
20199f15d8dSJens Axboe }
20299f15d8dSJens Axboe
io_put_file(struct io_kiocb * req)20317bc2837SJens Axboe static inline void io_put_file(struct io_kiocb *req)
204531113bbSJens Axboe {
20517bc2837SJens Axboe if (!(req->flags & REQ_F_FIXED_FILE) && req->file)
20617bc2837SJens Axboe fput(req->file);
207531113bbSJens Axboe }
208531113bbSJens Axboe
io_ring_submit_unlock(struct io_ring_ctx * ctx,unsigned issue_flags)209cd40cae2SJens Axboe static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
210cd40cae2SJens Axboe unsigned issue_flags)
211cd40cae2SJens Axboe {
212cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock);
213cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED)
214cd40cae2SJens Axboe mutex_unlock(&ctx->uring_lock);
215cd40cae2SJens Axboe }
216cd40cae2SJens Axboe
io_ring_submit_lock(struct io_ring_ctx * ctx,unsigned issue_flags)217cd40cae2SJens Axboe static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
218cd40cae2SJens Axboe unsigned issue_flags)
219cd40cae2SJens Axboe {
220cd40cae2SJens Axboe /*
221cd40cae2SJens Axboe * "Normal" inline submissions always hold the uring_lock, since we
222cd40cae2SJens Axboe * grab it from the system call. Same is true for the SQPOLL offload.
223cd40cae2SJens Axboe * The only exception is when we've detached the request and issue it
224cd40cae2SJens Axboe * from an async worker thread, grab the lock for that case.
225cd40cae2SJens Axboe */
226cd40cae2SJens Axboe if (issue_flags & IO_URING_F_UNLOCKED)
227cd40cae2SJens Axboe mutex_lock(&ctx->uring_lock);
228cd40cae2SJens Axboe lockdep_assert_held(&ctx->uring_lock);
229cd40cae2SJens Axboe }
230cd40cae2SJens Axboe
io_commit_cqring(struct io_ring_ctx * ctx)231f9ead18cSJens Axboe static inline void io_commit_cqring(struct io_ring_ctx *ctx)
232f9ead18cSJens Axboe {
233f9ead18cSJens Axboe /* order cqe stores with ring update */
234f9ead18cSJens Axboe smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
235f9ead18cSJens Axboe }
236f9ead18cSJens Axboe
io_poll_wq_wake(struct io_ring_ctx * ctx)2377b235dd8SPavel Begunkov static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
2387b235dd8SPavel Begunkov {
239bca39f39SPavel Begunkov if (wq_has_sleeper(&ctx->poll_wq))
2407b235dd8SPavel Begunkov __wake_up(&ctx->poll_wq, TASK_NORMAL, 0,
2417b235dd8SPavel Begunkov poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
2427b235dd8SPavel Begunkov }
2437b235dd8SPavel Begunkov
io_cqring_wake(struct io_ring_ctx * ctx)2446e7248adSPavel Begunkov static inline void io_cqring_wake(struct io_ring_ctx *ctx)
245f3b44f92SJens Axboe {
246f3b44f92SJens Axboe /*
24744648532SJens Axboe * Trigger waitqueue handler on all waiters on our waitqueue. This
24844648532SJens Axboe * won't necessarily wake up all the tasks, io_should_wake() will make
24944648532SJens Axboe * that decision.
25044648532SJens Axboe *
25144648532SJens Axboe * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
25244648532SJens Axboe * set in the mask so that if we recurse back into our own poll
25344648532SJens Axboe * waitqueue handlers, we know we have a dependency between eventfd or
25444648532SJens Axboe * epoll and should terminate multishot poll at that point.
255f3b44f92SJens Axboe */
2566e7248adSPavel Begunkov if (wq_has_sleeper(&ctx->cq_wait))
25744648532SJens Axboe __wake_up(&ctx->cq_wait, TASK_NORMAL, 0,
25844648532SJens Axboe poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
259f3b44f92SJens Axboe }
260f3b44f92SJens Axboe
io_sqring_full(struct io_ring_ctx * ctx)26117437f31SJens Axboe static inline bool io_sqring_full(struct io_ring_ctx *ctx)
26217437f31SJens Axboe {
26317437f31SJens Axboe struct io_rings *r = ctx->rings;
26417437f31SJens Axboe
26517437f31SJens Axboe return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
26617437f31SJens Axboe }
26717437f31SJens Axboe
io_sqring_entries(struct io_ring_ctx * ctx)26817437f31SJens Axboe static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
26917437f31SJens Axboe {
27017437f31SJens Axboe struct io_rings *rings = ctx->rings;
271e3ef728fSJens Axboe unsigned int entries;
27217437f31SJens Axboe
27317437f31SJens Axboe /* make sure SQ entry isn't read before tail */
274e3ef728fSJens Axboe entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
275e3ef728fSJens Axboe return min(entries, ctx->sq_entries);
27617437f31SJens Axboe }
27717437f31SJens Axboe
io_run_task_work(void)278c0e0d6baSDylan Yudaken static inline int io_run_task_work(void)
27917437f31SJens Axboe {
2807cfe7a09SJens Axboe /*
2817cfe7a09SJens Axboe * Always check-and-clear the task_work notification signal. With how
2827cfe7a09SJens Axboe * signaling works for task_work, we can find it set with nothing to
2837cfe7a09SJens Axboe * run. We need to clear it for that case, like get_signal() does.
2847cfe7a09SJens Axboe */
28546a525e1SJens Axboe if (test_thread_flag(TIF_NOTIFY_SIGNAL))
28617437f31SJens Axboe clear_notify_signal();
287b5d3ae20SJens Axboe /*
288b5d3ae20SJens Axboe * PF_IO_WORKER never returns to userspace, so check here if we have
289b5d3ae20SJens Axboe * notify work that needs processing.
290b5d3ae20SJens Axboe */
291b5d3ae20SJens Axboe if (current->flags & PF_IO_WORKER &&
2922f2bb1ffSJens Axboe test_thread_flag(TIF_NOTIFY_RESUME)) {
2932f2bb1ffSJens Axboe __set_current_state(TASK_RUNNING);
294b5d3ae20SJens Axboe resume_user_mode_work(NULL);
2952f2bb1ffSJens Axboe }
2967cfe7a09SJens Axboe if (task_work_pending(current)) {
29746a525e1SJens Axboe __set_current_state(TASK_RUNNING);
29817437f31SJens Axboe task_work_run();
299c0e0d6baSDylan Yudaken return 1;
30017437f31SJens Axboe }
30117437f31SJens Axboe
302c0e0d6baSDylan Yudaken return 0;
303c0e0d6baSDylan Yudaken }
304c0e0d6baSDylan Yudaken
io_task_work_pending(struct io_ring_ctx * ctx)305dac6a0eaSJens Axboe static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
306dac6a0eaSJens Axboe {
3071319dbd6SJens Axboe return task_work_pending(current) || !llist_empty(&ctx->work_llist);
308dac6a0eaSJens Axboe }
309dac6a0eaSJens Axboe
io_tw_lock(struct io_ring_ctx * ctx,struct io_tw_state * ts)310a282967cSPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, struct io_tw_state *ts)
311aa1e90f6SPavel Begunkov {
312a282967cSPavel Begunkov if (!ts->locked) {
313aa1e90f6SPavel Begunkov mutex_lock(&ctx->uring_lock);
314a282967cSPavel Begunkov ts->locked = true;
315aa1e90f6SPavel Begunkov }
316aa1e90f6SPavel Begunkov }
317aa1e90f6SPavel Begunkov
3189da070b1SPavel Begunkov /*
3199da070b1SPavel Begunkov * Don't complete immediately but use deferred completion infrastructure.
3209da070b1SPavel Begunkov * Protected by ->uring_lock and can only be used either with
3219da070b1SPavel Begunkov * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
3229da070b1SPavel Begunkov */
io_req_complete_defer(struct io_kiocb * req)3239da070b1SPavel Begunkov static inline void io_req_complete_defer(struct io_kiocb *req)
3249da070b1SPavel Begunkov __must_hold(&req->ctx->uring_lock)
325aa1e90f6SPavel Begunkov {
326aa1e90f6SPavel Begunkov struct io_submit_state *state = &req->ctx->submit_state;
327aa1e90f6SPavel Begunkov
3289da070b1SPavel Begunkov lockdep_assert_held(&req->ctx->uring_lock);
3299da070b1SPavel Begunkov
330aa1e90f6SPavel Begunkov wq_list_add_tail(&req->comp_list, &state->compl_reqs);
331aa1e90f6SPavel Begunkov }
332aa1e90f6SPavel Begunkov
io_commit_cqring_flush(struct io_ring_ctx * ctx)33346929b08SPavel Begunkov static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
33446929b08SPavel Begunkov {
335bca39f39SPavel Begunkov if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
336bca39f39SPavel Begunkov ctx->has_evfd || ctx->poll_activated))
33746929b08SPavel Begunkov __io_commit_cqring_flush(ctx);
33846929b08SPavel Begunkov }
33946929b08SPavel Begunkov
io_get_task_refs(int nr)34063809137SPavel Begunkov static inline void io_get_task_refs(int nr)
34163809137SPavel Begunkov {
34263809137SPavel Begunkov struct io_uring_task *tctx = current->io_uring;
34363809137SPavel Begunkov
34463809137SPavel Begunkov tctx->cached_refs -= nr;
34563809137SPavel Begunkov if (unlikely(tctx->cached_refs < 0))
34663809137SPavel Begunkov io_task_refs_refill(tctx);
34763809137SPavel Begunkov }
34863809137SPavel Begunkov
io_req_cache_empty(struct io_ring_ctx * ctx)349bd1a3783SPavel Begunkov static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
350bd1a3783SPavel Begunkov {
351bd1a3783SPavel Begunkov return !ctx->submit_state.free_list.next;
352bd1a3783SPavel Begunkov }
353bd1a3783SPavel Begunkov
354c1755c25SBreno Leitao extern struct kmem_cache *req_cachep;
355c1755c25SBreno Leitao
io_extract_req(struct io_ring_ctx * ctx)356c8576f3eSPavel Begunkov static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx)
357bd1a3783SPavel Begunkov {
358c1755c25SBreno Leitao struct io_kiocb *req;
359bd1a3783SPavel Begunkov
360c1755c25SBreno Leitao req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list);
361c1755c25SBreno Leitao wq_stack_extract(&ctx->submit_state.free_list);
362c1755c25SBreno Leitao return req;
363bd1a3783SPavel Begunkov }
364bd1a3783SPavel Begunkov
io_alloc_req(struct io_ring_ctx * ctx,struct io_kiocb ** req)365c8576f3eSPavel Begunkov static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req)
366c8576f3eSPavel Begunkov {
367c8576f3eSPavel Begunkov if (unlikely(io_req_cache_empty(ctx))) {
368c8576f3eSPavel Begunkov if (!__io_alloc_req_refill(ctx))
369c8576f3eSPavel Begunkov return false;
370c8576f3eSPavel Begunkov }
371c8576f3eSPavel Begunkov *req = io_extract_req(ctx);
372c8576f3eSPavel Begunkov return true;
373c8576f3eSPavel Begunkov }
374c8576f3eSPavel Begunkov
io_allowed_defer_tw_run(struct io_ring_ctx * ctx)375140102aeSPavel Begunkov static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
376140102aeSPavel Begunkov {
377140102aeSPavel Begunkov return likely(ctx->submitter_task == current);
378140102aeSPavel Begunkov }
379140102aeSPavel Begunkov
io_allowed_run_tw(struct io_ring_ctx * ctx)38076de6749SPavel Begunkov static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
38176de6749SPavel Begunkov {
3826567506bSPavel Begunkov return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
3836567506bSPavel Begunkov ctx->submitter_task == current);
38476de6749SPavel Begunkov }
38576de6749SPavel Begunkov
io_req_queue_tw_complete(struct io_kiocb * req,s32 res)386833b5dffSPavel Begunkov static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
387833b5dffSPavel Begunkov {
388833b5dffSPavel Begunkov io_req_set_res(req, res, 0);
389833b5dffSPavel Begunkov req->io_task_work.func = io_req_task_complete;
390833b5dffSPavel Begunkov io_req_task_work_add(req);
391833b5dffSPavel Begunkov }
392833b5dffSPavel Begunkov
39396c7d4f8SBreno Leitao /*
39496c7d4f8SBreno Leitao * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each
39596c7d4f8SBreno Leitao * slot.
39696c7d4f8SBreno Leitao */
uring_sqe_size(struct io_ring_ctx * ctx)39796c7d4f8SBreno Leitao static inline size_t uring_sqe_size(struct io_ring_ctx *ctx)
39896c7d4f8SBreno Leitao {
39996c7d4f8SBreno Leitao if (ctx->flags & IORING_SETUP_SQE128)
40096c7d4f8SBreno Leitao return 2 * sizeof(struct io_uring_sqe);
40196c7d4f8SBreno Leitao return sizeof(struct io_uring_sqe);
40296c7d4f8SBreno Leitao }
403de23077eSJens Axboe #endif
404