xref: /openbmc/linux/io_uring/io_uring.h (revision b5d3ae20)
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>
6*b5d3ae20SJens Axboe #include <linux/resume_user_mode.h>
7ab1c84d8SPavel Begunkov #include <linux/io_uring_types.h>
844648532SJens Axboe #include <uapi/linux/eventpoll.h>
9ab1c84d8SPavel Begunkov #include "io-wq.h"
10a6b21fbbSPavel Begunkov #include "slist.h"
11ab1c84d8SPavel Begunkov #include "filetable.h"
12de23077eSJens Axboe 
13f3b44f92SJens Axboe #ifndef CREATE_TRACE_POINTS
14f3b44f92SJens Axboe #include <trace/events/io_uring.h>
15f3b44f92SJens Axboe #endif
16f3b44f92SJens Axboe 
1797b388d7SJens Axboe enum {
1897b388d7SJens Axboe 	IOU_OK			= 0,
1997b388d7SJens Axboe 	IOU_ISSUE_SKIP_COMPLETE	= -EIOCBQUEUED,
20114eccdfSDylan Yudaken 
21114eccdfSDylan Yudaken 	/*
2291482864SPavel Begunkov 	 * Intended only when both IO_URING_F_MULTISHOT is passed
2391482864SPavel Begunkov 	 * to indicate to the poll runner that multishot should be
24114eccdfSDylan Yudaken 	 * removed and the result is set on req->cqe.res.
25114eccdfSDylan Yudaken 	 */
26114eccdfSDylan Yudaken 	IOU_STOP_MULTISHOT	= -ECANCELED,
2797b388d7SJens Axboe };
2897b388d7SJens Axboe 
29aa1df3a3SPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow);
3068494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req);
31c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx);
32973fc83fSDylan Yudaken void io_req_defer_failed(struct io_kiocb *req, s32 res);
331bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags);
34b529c96aSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
359b8c5475SDylan Yudaken bool io_aux_cqe(struct io_ring_ctx *ctx, bool defer, u64 user_data, s32 res, u32 cflags,
36eb42cebbSPavel Begunkov 		bool allow_overflow);
379046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
389046c641SPavel Begunkov 
399046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages);
409046c641SPavel Begunkov 
419046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd);
429046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
439046c641SPavel Begunkov 			       unsigned issue_flags);
449046c641SPavel Begunkov 
45f6b543fdSJens Axboe static inline bool io_req_ffs_set(struct io_kiocb *req)
46f6b543fdSJens Axboe {
47f6b543fdSJens Axboe 	return req->flags & REQ_F_FIXED_FILE;
48f6b543fdSJens Axboe }
49f6b543fdSJens Axboe 
50e52d2e58SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, bool allow_local);
519046c641SPavel Begunkov bool io_is_uring_fops(struct file *file);
529046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req);
539046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req);
549046c641SPavel Begunkov void io_queue_iowq(struct io_kiocb *req, bool *dont_use);
559046c641SPavel Begunkov void io_req_task_complete(struct io_kiocb *req, bool *locked);
569046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret);
579046c641SPavel Begunkov void io_req_task_submit(struct io_kiocb *req, bool *locked);
589046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb);
599046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
609046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task,
619046c641SPavel Begunkov 				struct io_ring_ctx *ctx);
629046c641SPavel Begunkov 
639046c641SPavel Begunkov int io_poll_issue(struct io_kiocb *req, bool *locked);
649046c641SPavel Begunkov int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
659046c641SPavel Begunkov int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
669046c641SPavel Begunkov void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node);
679046c641SPavel Begunkov int io_req_prep_async(struct io_kiocb *req);
689046c641SPavel Begunkov 
699046c641SPavel Begunkov struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
709046c641SPavel Begunkov void io_wq_submit_work(struct io_wq_work *work);
719046c641SPavel Begunkov 
729046c641SPavel Begunkov void io_free_req(struct io_kiocb *req);
739046c641SPavel Begunkov void io_queue_next(struct io_kiocb *req);
7463809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx);
75bd1a3783SPavel Begunkov bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
769046c641SPavel Begunkov 
779046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
789046c641SPavel Begunkov 			bool cancel_all);
799046c641SPavel Begunkov 
80f26cc959SPavel Begunkov #define io_lockdep_assert_cq_locked(ctx)				\
81f26cc959SPavel Begunkov 	do {								\
82f26cc959SPavel Begunkov 		if (ctx->flags & IORING_SETUP_IOPOLL) {			\
83f26cc959SPavel Begunkov 			lockdep_assert_held(&ctx->uring_lock);		\
84f26cc959SPavel Begunkov 		} else if (!ctx->task_complete) {			\
85f26cc959SPavel Begunkov 			lockdep_assert_held(&ctx->completion_lock);	\
86f26cc959SPavel Begunkov 		} else if (ctx->submitter_task->flags & PF_EXITING) {	\
87f26cc959SPavel Begunkov 			lockdep_assert(current_work());			\
88f26cc959SPavel Begunkov 		} else {						\
89f26cc959SPavel Begunkov 			lockdep_assert(current == ctx->submitter_task);	\
90f26cc959SPavel Begunkov 		}							\
91f26cc959SPavel Begunkov 	} while (0)
92f26cc959SPavel Begunkov 
93e52d2e58SPavel Begunkov static inline void io_req_task_work_add(struct io_kiocb *req)
94e52d2e58SPavel Begunkov {
95e52d2e58SPavel Begunkov 	__io_req_task_work_add(req, true);
96e52d2e58SPavel Begunkov }
97e52d2e58SPavel Begunkov 
989046c641SPavel Begunkov #define io_for_each_link(pos, head) \
999046c641SPavel Begunkov 	for (pos = (head); pos; pos = pos->link)
100f3b44f92SJens Axboe 
10125399321SPavel Begunkov void io_cq_unlock_post(struct io_ring_ctx *ctx);
10225399321SPavel Begunkov 
103aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe_overflow(struct io_ring_ctx *ctx,
104aa1df3a3SPavel Begunkov 						       bool overflow)
105f3b44f92SJens Axboe {
106f26cc959SPavel Begunkov 	io_lockdep_assert_cq_locked(ctx);
107f26cc959SPavel Begunkov 
108f3b44f92SJens Axboe 	if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) {
109f3b44f92SJens Axboe 		struct io_uring_cqe *cqe = ctx->cqe_cached;
110f3b44f92SJens Axboe 
111f3b44f92SJens Axboe 		ctx->cached_cq_tail++;
112f3b44f92SJens Axboe 		ctx->cqe_cached++;
113b3659a65SPavel Begunkov 		if (ctx->flags & IORING_SETUP_CQE32)
114b3659a65SPavel Begunkov 			ctx->cqe_cached++;
115f3b44f92SJens Axboe 		return cqe;
116f3b44f92SJens Axboe 	}
117f3b44f92SJens Axboe 
118aa1df3a3SPavel Begunkov 	return __io_get_cqe(ctx, overflow);
119aa1df3a3SPavel Begunkov }
120aa1df3a3SPavel Begunkov 
121aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
122aa1df3a3SPavel Begunkov {
123aa1df3a3SPavel Begunkov 	return io_get_cqe_overflow(ctx, false);
124f3b44f92SJens Axboe }
125f3b44f92SJens Axboe 
126f3b44f92SJens Axboe static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx,
127f3b44f92SJens Axboe 				     struct io_kiocb *req)
128f3b44f92SJens Axboe {
129f3b44f92SJens Axboe 	struct io_uring_cqe *cqe;
130f3b44f92SJens Axboe 
131f3b44f92SJens Axboe 	/*
132f3b44f92SJens Axboe 	 * If we can't get a cq entry, userspace overflowed the
133f3b44f92SJens Axboe 	 * submission (by quite a lot). Increment the overflow count in
134f3b44f92SJens Axboe 	 * the ring.
135f3b44f92SJens Axboe 	 */
136f3b44f92SJens Axboe 	cqe = io_get_cqe(ctx);
137e8c328c3SPavel Begunkov 	if (unlikely(!cqe))
138f66f7342SPavel Begunkov 		return false;
139e0486f3fSDylan Yudaken 
140e0486f3fSDylan Yudaken 	trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
141e0486f3fSDylan Yudaken 				req->cqe.res, req->cqe.flags,
142e0486f3fSDylan Yudaken 				(req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0,
143e0486f3fSDylan Yudaken 				(req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0);
144e0486f3fSDylan Yudaken 
145f3b44f92SJens Axboe 	memcpy(cqe, &req->cqe, sizeof(*cqe));
146e8c328c3SPavel Begunkov 
147e8c328c3SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32) {
148f3b44f92SJens Axboe 		u64 extra1 = 0, extra2 = 0;
149f3b44f92SJens Axboe 
150f3b44f92SJens Axboe 		if (req->flags & REQ_F_CQE32_INIT) {
151f3b44f92SJens Axboe 			extra1 = req->extra1;
152f3b44f92SJens Axboe 			extra2 = req->extra2;
153f3b44f92SJens Axboe 		}
154f3b44f92SJens Axboe 
155f3b44f92SJens Axboe 		WRITE_ONCE(cqe->big_cqe[0], extra1);
156f3b44f92SJens Axboe 		WRITE_ONCE(cqe->big_cqe[1], extra2);
157e8c328c3SPavel Begunkov 	}
158f3b44f92SJens Axboe 	return true;
159f3b44f92SJens Axboe }
160f3b44f92SJens Axboe 
161f66f7342SPavel Begunkov static inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,
162f66f7342SPavel Begunkov 				   struct io_kiocb *req)
163f66f7342SPavel Begunkov {
164f66f7342SPavel Begunkov 	if (likely(__io_fill_cqe_req(ctx, req)))
165f66f7342SPavel Begunkov 		return true;
166f66f7342SPavel Begunkov 	return io_req_cqe_overflow(req);
167f66f7342SPavel Begunkov }
168f66f7342SPavel Begunkov 
169531113bbSJens Axboe static inline void req_set_fail(struct io_kiocb *req)
170531113bbSJens Axboe {
171531113bbSJens Axboe 	req->flags |= REQ_F_FAIL;
172531113bbSJens Axboe 	if (req->flags & REQ_F_CQE_SKIP) {
173531113bbSJens Axboe 		req->flags &= ~REQ_F_CQE_SKIP;
174531113bbSJens Axboe 		req->flags |= REQ_F_SKIP_LINK_CQES;
175531113bbSJens Axboe 	}
176531113bbSJens Axboe }
177531113bbSJens Axboe 
178de23077eSJens Axboe static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
179de23077eSJens Axboe {
180de23077eSJens Axboe 	req->cqe.res = res;
181de23077eSJens Axboe 	req->cqe.flags = cflags;
182de23077eSJens Axboe }
183de23077eSJens Axboe 
18499f15d8dSJens Axboe static inline bool req_has_async_data(struct io_kiocb *req)
18599f15d8dSJens Axboe {
18699f15d8dSJens Axboe 	return req->flags & REQ_F_ASYNC_DATA;
18799f15d8dSJens Axboe }
18899f15d8dSJens Axboe 
189531113bbSJens Axboe static inline void io_put_file(struct file *file)
190531113bbSJens Axboe {
191531113bbSJens Axboe 	if (file)
192531113bbSJens Axboe 		fput(file);
193531113bbSJens Axboe }
194531113bbSJens Axboe 
195cd40cae2SJens Axboe static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
196cd40cae2SJens Axboe 					 unsigned issue_flags)
197cd40cae2SJens Axboe {
198cd40cae2SJens Axboe 	lockdep_assert_held(&ctx->uring_lock);
199cd40cae2SJens Axboe 	if (issue_flags & IO_URING_F_UNLOCKED)
200cd40cae2SJens Axboe 		mutex_unlock(&ctx->uring_lock);
201cd40cae2SJens Axboe }
202cd40cae2SJens Axboe 
203cd40cae2SJens Axboe static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
204cd40cae2SJens Axboe 				       unsigned issue_flags)
205cd40cae2SJens Axboe {
206cd40cae2SJens Axboe 	/*
207cd40cae2SJens Axboe 	 * "Normal" inline submissions always hold the uring_lock, since we
208cd40cae2SJens Axboe 	 * grab it from the system call. Same is true for the SQPOLL offload.
209cd40cae2SJens Axboe 	 * The only exception is when we've detached the request and issue it
210cd40cae2SJens Axboe 	 * from an async worker thread, grab the lock for that case.
211cd40cae2SJens Axboe 	 */
212cd40cae2SJens Axboe 	if (issue_flags & IO_URING_F_UNLOCKED)
213cd40cae2SJens Axboe 		mutex_lock(&ctx->uring_lock);
214cd40cae2SJens Axboe 	lockdep_assert_held(&ctx->uring_lock);
215cd40cae2SJens Axboe }
216cd40cae2SJens Axboe 
217f9ead18cSJens Axboe static inline void io_commit_cqring(struct io_ring_ctx *ctx)
218f9ead18cSJens Axboe {
219f9ead18cSJens Axboe 	/* order cqe stores with ring update */
220f9ead18cSJens Axboe 	smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
221f9ead18cSJens Axboe }
222f9ead18cSJens Axboe 
2237b235dd8SPavel Begunkov static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
2247b235dd8SPavel Begunkov {
225bca39f39SPavel Begunkov 	if (wq_has_sleeper(&ctx->poll_wq))
2267b235dd8SPavel Begunkov 		__wake_up(&ctx->poll_wq, TASK_NORMAL, 0,
2277b235dd8SPavel Begunkov 				poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
2287b235dd8SPavel Begunkov }
2297b235dd8SPavel Begunkov 
230fc86f9d3SPavel Begunkov /* requires smb_mb() prior, see wq_has_sleeper() */
231fc86f9d3SPavel Begunkov static inline void __io_cqring_wake(struct io_ring_ctx *ctx)
232f3b44f92SJens Axboe {
233f3b44f92SJens Axboe 	/*
23444648532SJens Axboe 	 * Trigger waitqueue handler on all waiters on our waitqueue. This
23544648532SJens Axboe 	 * won't necessarily wake up all the tasks, io_should_wake() will make
23644648532SJens Axboe 	 * that decision.
23744648532SJens Axboe 	 *
23844648532SJens Axboe 	 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
23944648532SJens Axboe 	 * set in the mask so that if we recurse back into our own poll
24044648532SJens Axboe 	 * waitqueue handlers, we know we have a dependency between eventfd or
24144648532SJens Axboe 	 * epoll and should terminate multishot poll at that point.
242f3b44f92SJens Axboe 	 */
243fc86f9d3SPavel Begunkov 	if (waitqueue_active(&ctx->cq_wait))
24444648532SJens Axboe 		__wake_up(&ctx->cq_wait, TASK_NORMAL, 0,
24544648532SJens Axboe 				poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
246f3b44f92SJens Axboe }
247f3b44f92SJens Axboe 
248fc86f9d3SPavel Begunkov static inline void io_cqring_wake(struct io_ring_ctx *ctx)
249fc86f9d3SPavel Begunkov {
250fc86f9d3SPavel Begunkov 	smp_mb();
251fc86f9d3SPavel Begunkov 	__io_cqring_wake(ctx);
252fc86f9d3SPavel Begunkov }
253fc86f9d3SPavel Begunkov 
25417437f31SJens Axboe static inline bool io_sqring_full(struct io_ring_ctx *ctx)
25517437f31SJens Axboe {
25617437f31SJens Axboe 	struct io_rings *r = ctx->rings;
25717437f31SJens Axboe 
25817437f31SJens Axboe 	return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
25917437f31SJens Axboe }
26017437f31SJens Axboe 
26117437f31SJens Axboe static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
26217437f31SJens Axboe {
26317437f31SJens Axboe 	struct io_rings *rings = ctx->rings;
26417437f31SJens Axboe 
26517437f31SJens Axboe 	/* make sure SQ entry isn't read before tail */
26617437f31SJens Axboe 	return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
26717437f31SJens Axboe }
26817437f31SJens Axboe 
269c0e0d6baSDylan Yudaken static inline int io_run_task_work(void)
27017437f31SJens Axboe {
2717cfe7a09SJens Axboe 	/*
2727cfe7a09SJens Axboe 	 * Always check-and-clear the task_work notification signal. With how
2737cfe7a09SJens Axboe 	 * signaling works for task_work, we can find it set with nothing to
2747cfe7a09SJens Axboe 	 * run. We need to clear it for that case, like get_signal() does.
2757cfe7a09SJens Axboe 	 */
27646a525e1SJens Axboe 	if (test_thread_flag(TIF_NOTIFY_SIGNAL))
27717437f31SJens Axboe 		clear_notify_signal();
278*b5d3ae20SJens Axboe 	/*
279*b5d3ae20SJens Axboe 	 * PF_IO_WORKER never returns to userspace, so check here if we have
280*b5d3ae20SJens Axboe 	 * notify work that needs processing.
281*b5d3ae20SJens Axboe 	 */
282*b5d3ae20SJens Axboe 	if (current->flags & PF_IO_WORKER &&
283*b5d3ae20SJens Axboe 	    test_thread_flag(TIF_NOTIFY_RESUME))
284*b5d3ae20SJens Axboe 		resume_user_mode_work(NULL);
2857cfe7a09SJens Axboe 	if (task_work_pending(current)) {
28646a525e1SJens Axboe 		__set_current_state(TASK_RUNNING);
28717437f31SJens Axboe 		task_work_run();
288c0e0d6baSDylan Yudaken 		return 1;
28917437f31SJens Axboe 	}
29017437f31SJens Axboe 
291c0e0d6baSDylan Yudaken 	return 0;
292c0e0d6baSDylan Yudaken }
293c0e0d6baSDylan Yudaken 
294dac6a0eaSJens Axboe static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
295dac6a0eaSJens Axboe {
2966434ec01SJens Axboe 	return task_work_pending(current) || !wq_list_empty(&ctx->work_llist);
297dac6a0eaSJens Axboe }
298dac6a0eaSJens Axboe 
299aa1e90f6SPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
300aa1e90f6SPavel Begunkov {
301aa1e90f6SPavel Begunkov 	if (!*locked) {
302aa1e90f6SPavel Begunkov 		mutex_lock(&ctx->uring_lock);
303aa1e90f6SPavel Begunkov 		*locked = true;
304aa1e90f6SPavel Begunkov 	}
305aa1e90f6SPavel Begunkov }
306aa1e90f6SPavel Begunkov 
3079da070b1SPavel Begunkov /*
3089da070b1SPavel Begunkov  * Don't complete immediately but use deferred completion infrastructure.
3099da070b1SPavel Begunkov  * Protected by ->uring_lock and can only be used either with
3109da070b1SPavel Begunkov  * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
3119da070b1SPavel Begunkov  */
3129da070b1SPavel Begunkov static inline void io_req_complete_defer(struct io_kiocb *req)
3139da070b1SPavel Begunkov 	__must_hold(&req->ctx->uring_lock)
314aa1e90f6SPavel Begunkov {
315aa1e90f6SPavel Begunkov 	struct io_submit_state *state = &req->ctx->submit_state;
316aa1e90f6SPavel Begunkov 
3179da070b1SPavel Begunkov 	lockdep_assert_held(&req->ctx->uring_lock);
3189da070b1SPavel Begunkov 
319aa1e90f6SPavel Begunkov 	wq_list_add_tail(&req->comp_list, &state->compl_reqs);
320aa1e90f6SPavel Begunkov }
321aa1e90f6SPavel Begunkov 
32246929b08SPavel Begunkov static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
32346929b08SPavel Begunkov {
324bca39f39SPavel Begunkov 	if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
325bca39f39SPavel Begunkov 		     ctx->has_evfd || ctx->poll_activated))
32646929b08SPavel Begunkov 		__io_commit_cqring_flush(ctx);
32746929b08SPavel Begunkov }
32846929b08SPavel Begunkov 
32963809137SPavel Begunkov static inline void io_get_task_refs(int nr)
33063809137SPavel Begunkov {
33163809137SPavel Begunkov 	struct io_uring_task *tctx = current->io_uring;
33263809137SPavel Begunkov 
33363809137SPavel Begunkov 	tctx->cached_refs -= nr;
33463809137SPavel Begunkov 	if (unlikely(tctx->cached_refs < 0))
33563809137SPavel Begunkov 		io_task_refs_refill(tctx);
33663809137SPavel Begunkov }
33763809137SPavel Begunkov 
338bd1a3783SPavel Begunkov static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
339bd1a3783SPavel Begunkov {
340bd1a3783SPavel Begunkov 	return !ctx->submit_state.free_list.next;
341bd1a3783SPavel Begunkov }
342bd1a3783SPavel Begunkov 
343bd1a3783SPavel Begunkov static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
344bd1a3783SPavel Begunkov {
345bd1a3783SPavel Begunkov 	if (unlikely(io_req_cache_empty(ctx)))
346bd1a3783SPavel Begunkov 		return __io_alloc_req_refill(ctx);
347bd1a3783SPavel Begunkov 	return true;
348bd1a3783SPavel Begunkov }
349bd1a3783SPavel Begunkov 
350bd1a3783SPavel Begunkov static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
351bd1a3783SPavel Begunkov {
352bd1a3783SPavel Begunkov 	struct io_wq_work_node *node;
353bd1a3783SPavel Begunkov 
354bd1a3783SPavel Begunkov 	node = wq_stack_extract(&ctx->submit_state.free_list);
355bd1a3783SPavel Begunkov 	return container_of(node, struct io_kiocb, comp_list);
356bd1a3783SPavel Begunkov }
357bd1a3783SPavel Begunkov 
358140102aeSPavel Begunkov static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
359140102aeSPavel Begunkov {
360140102aeSPavel Begunkov 	return likely(ctx->submitter_task == current);
361140102aeSPavel Begunkov }
362140102aeSPavel Begunkov 
36376de6749SPavel Begunkov static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
36476de6749SPavel Begunkov {
3656567506bSPavel Begunkov 	return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
3666567506bSPavel Begunkov 		      ctx->submitter_task == current);
36776de6749SPavel Begunkov }
36876de6749SPavel Begunkov 
369833b5dffSPavel Begunkov static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
370833b5dffSPavel Begunkov {
371833b5dffSPavel Begunkov 	io_req_set_res(req, res, 0);
372833b5dffSPavel Begunkov 	req->io_task_work.func = io_req_task_complete;
373833b5dffSPavel Begunkov 	io_req_task_work_add(req);
374833b5dffSPavel Begunkov }
375833b5dffSPavel Begunkov 
376de23077eSJens Axboe #endif
377