xref: /openbmc/linux/io_uring/io_uring.h (revision 20d6b633)
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 	/*
3491482864SPavel Begunkov 	 * Intended only when both IO_URING_F_MULTISHOT is passed
3591482864SPavel Begunkov 	 * to indicate to the poll runner that multishot should be
36114eccdfSDylan Yudaken 	 * removed and the result is set on req->cqe.res.
37114eccdfSDylan Yudaken 	 */
38114eccdfSDylan Yudaken 	IOU_STOP_MULTISHOT	= -ECANCELED,
3997b388d7SJens Axboe };
4097b388d7SJens Axboe 
41*20d6b633SPavel Begunkov bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow);
42056695bfSPavel Begunkov void io_req_cqe_overflow(struct io_kiocb *req);
43c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx);
44973fc83fSDylan Yudaken void io_req_defer_failed(struct io_kiocb *req, s32 res);
451bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags);
46b529c96aSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags);
47b6b2bb58SPavel Begunkov bool io_fill_cqe_req_aux(struct io_kiocb *req, bool defer, s32 res, u32 cflags);
489046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
499046c641SPavel Begunkov 
509046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages);
519046c641SPavel Begunkov 
529046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd);
539046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
549046c641SPavel Begunkov 			       unsigned issue_flags);
559046c641SPavel Begunkov 
568501fe70SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, unsigned flags);
579046c641SPavel Begunkov bool io_is_uring_fops(struct file *file);
589046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req);
599046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req);
60a282967cSPavel Begunkov void io_queue_iowq(struct io_kiocb *req, struct io_tw_state *ts_dont_use);
61a282967cSPavel Begunkov void io_req_task_complete(struct io_kiocb *req, struct io_tw_state *ts);
629046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret);
63a282967cSPavel Begunkov void io_req_task_submit(struct io_kiocb *req, struct io_tw_state *ts);
649046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb);
659046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
669046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task,
679046c641SPavel Begunkov 				struct io_ring_ctx *ctx);
689046c641SPavel Begunkov 
696e76ac59SJosh Triplett int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
706e76ac59SJosh Triplett 				     int start, int end);
716e76ac59SJosh Triplett 
72a282967cSPavel Begunkov int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts);
739046c641SPavel Begunkov int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
749046c641SPavel Begunkov int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
759046c641SPavel Begunkov void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node);
769046c641SPavel Begunkov int io_req_prep_async(struct io_kiocb *req);
779046c641SPavel Begunkov 
789046c641SPavel Begunkov struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
799046c641SPavel Begunkov void io_wq_submit_work(struct io_wq_work *work);
809046c641SPavel Begunkov 
819046c641SPavel Begunkov void io_free_req(struct io_kiocb *req);
829046c641SPavel Begunkov void io_queue_next(struct io_kiocb *req);
8363809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx);
84bd1a3783SPavel Begunkov bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
859046c641SPavel Begunkov 
869046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
879046c641SPavel Begunkov 			bool cancel_all);
889046c641SPavel Begunkov 
89f26cc959SPavel Begunkov #define io_lockdep_assert_cq_locked(ctx)				\
90f26cc959SPavel Begunkov 	do {								\
918ce4269eSPavel Begunkov 		lockdep_assert(in_task());				\
928ce4269eSPavel Begunkov 									\
93f26cc959SPavel Begunkov 		if (ctx->flags & IORING_SETUP_IOPOLL) {			\
94f26cc959SPavel Begunkov 			lockdep_assert_held(&ctx->uring_lock);		\
95f26cc959SPavel Begunkov 		} else if (!ctx->task_complete) {			\
96f26cc959SPavel Begunkov 			lockdep_assert_held(&ctx->completion_lock);	\
97f26cc959SPavel Begunkov 		} else if (ctx->submitter_task->flags & PF_EXITING) {	\
98f26cc959SPavel Begunkov 			lockdep_assert(current_work());			\
99f26cc959SPavel Begunkov 		} else {						\
100f26cc959SPavel Begunkov 			lockdep_assert(current == ctx->submitter_task);	\
101f26cc959SPavel Begunkov 		}							\
102f26cc959SPavel Begunkov 	} while (0)
103f26cc959SPavel Begunkov 
104e52d2e58SPavel Begunkov static inline void io_req_task_work_add(struct io_kiocb *req)
105e52d2e58SPavel Begunkov {
1068501fe70SPavel Begunkov 	__io_req_task_work_add(req, 0);
107e52d2e58SPavel Begunkov }
108e52d2e58SPavel Begunkov 
1099046c641SPavel Begunkov #define io_for_each_link(pos, head) \
1109046c641SPavel Begunkov 	for (pos = (head); pos; pos = pos->link)
111f3b44f92SJens Axboe 
112aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe_overflow(struct io_ring_ctx *ctx,
113aa1df3a3SPavel Begunkov 						       bool overflow)
114f3b44f92SJens Axboe {
115*20d6b633SPavel Begunkov 	struct io_uring_cqe *cqe;
116*20d6b633SPavel Begunkov 
117f26cc959SPavel Begunkov 	io_lockdep_assert_cq_locked(ctx);
118f26cc959SPavel Begunkov 
119*20d6b633SPavel Begunkov 	if (unlikely(ctx->cqe_cached >= ctx->cqe_sentinel)) {
120*20d6b633SPavel Begunkov 		if (unlikely(!io_cqe_cache_refill(ctx, overflow)))
121*20d6b633SPavel Begunkov 			return NULL;
122*20d6b633SPavel Begunkov 	}
123*20d6b633SPavel Begunkov 	cqe = ctx->cqe_cached;
124f3b44f92SJens Axboe 	ctx->cached_cq_tail++;
125f3b44f92SJens Axboe 	ctx->cqe_cached++;
126b3659a65SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32)
127b3659a65SPavel Begunkov 		ctx->cqe_cached++;
128f3b44f92SJens Axboe 	return cqe;
129f3b44f92SJens Axboe }
130f3b44f92SJens Axboe 
131aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
132aa1df3a3SPavel Begunkov {
133aa1df3a3SPavel Begunkov 	return io_get_cqe_overflow(ctx, false);
134f3b44f92SJens Axboe }
135f3b44f92SJens Axboe 
13600b0db56SPavel Begunkov static inline bool io_fill_cqe_req(struct io_ring_ctx *ctx, struct io_kiocb *req)
137f3b44f92SJens Axboe {
138f3b44f92SJens Axboe 	struct io_uring_cqe *cqe;
139f3b44f92SJens Axboe 
140f3b44f92SJens Axboe 	/*
141f3b44f92SJens Axboe 	 * If we can't get a cq entry, userspace overflowed the
142f3b44f92SJens Axboe 	 * submission (by quite a lot). Increment the overflow count in
143f3b44f92SJens Axboe 	 * the ring.
144f3b44f92SJens Axboe 	 */
145f3b44f92SJens Axboe 	cqe = io_get_cqe(ctx);
146e8c328c3SPavel Begunkov 	if (unlikely(!cqe))
147f66f7342SPavel Begunkov 		return false;
148e0486f3fSDylan Yudaken 
149a0727c73SPavel Begunkov 	if (trace_io_uring_complete_enabled())
150e0486f3fSDylan Yudaken 		trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
151e0486f3fSDylan Yudaken 					req->cqe.res, req->cqe.flags,
152b24c5d75SPavel Begunkov 					req->big_cqe.extra1, req->big_cqe.extra2);
153e0486f3fSDylan Yudaken 
154f3b44f92SJens Axboe 	memcpy(cqe, &req->cqe, sizeof(*cqe));
155e8c328c3SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32) {
156b24c5d75SPavel Begunkov 		memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe));
157b24c5d75SPavel Begunkov 		memset(&req->big_cqe, 0, sizeof(req->big_cqe));
158e8c328c3SPavel Begunkov 	}
159f3b44f92SJens Axboe 	return true;
160f3b44f92SJens Axboe }
161f3b44f92SJens Axboe 
162531113bbSJens Axboe static inline void req_set_fail(struct io_kiocb *req)
163531113bbSJens Axboe {
164531113bbSJens Axboe 	req->flags |= REQ_F_FAIL;
165531113bbSJens Axboe 	if (req->flags & REQ_F_CQE_SKIP) {
166531113bbSJens Axboe 		req->flags &= ~REQ_F_CQE_SKIP;
167531113bbSJens Axboe 		req->flags |= REQ_F_SKIP_LINK_CQES;
168531113bbSJens Axboe 	}
169531113bbSJens Axboe }
170531113bbSJens Axboe 
171de23077eSJens Axboe static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
172de23077eSJens Axboe {
173de23077eSJens Axboe 	req->cqe.res = res;
174de23077eSJens Axboe 	req->cqe.flags = cflags;
175de23077eSJens Axboe }
176de23077eSJens Axboe 
17799f15d8dSJens Axboe static inline bool req_has_async_data(struct io_kiocb *req)
17899f15d8dSJens Axboe {
17999f15d8dSJens Axboe 	return req->flags & REQ_F_ASYNC_DATA;
18099f15d8dSJens Axboe }
18199f15d8dSJens Axboe 
18217bc2837SJens Axboe static inline void io_put_file(struct io_kiocb *req)
183531113bbSJens Axboe {
18417bc2837SJens Axboe 	if (!(req->flags & REQ_F_FIXED_FILE) && req->file)
18517bc2837SJens Axboe 		fput(req->file);
186531113bbSJens Axboe }
187531113bbSJens Axboe 
188cd40cae2SJens Axboe static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
189cd40cae2SJens Axboe 					 unsigned issue_flags)
190cd40cae2SJens Axboe {
191cd40cae2SJens Axboe 	lockdep_assert_held(&ctx->uring_lock);
192cd40cae2SJens Axboe 	if (issue_flags & IO_URING_F_UNLOCKED)
193cd40cae2SJens Axboe 		mutex_unlock(&ctx->uring_lock);
194cd40cae2SJens Axboe }
195cd40cae2SJens Axboe 
196cd40cae2SJens Axboe static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
197cd40cae2SJens Axboe 				       unsigned issue_flags)
198cd40cae2SJens Axboe {
199cd40cae2SJens Axboe 	/*
200cd40cae2SJens Axboe 	 * "Normal" inline submissions always hold the uring_lock, since we
201cd40cae2SJens Axboe 	 * grab it from the system call. Same is true for the SQPOLL offload.
202cd40cae2SJens Axboe 	 * The only exception is when we've detached the request and issue it
203cd40cae2SJens Axboe 	 * from an async worker thread, grab the lock for that case.
204cd40cae2SJens Axboe 	 */
205cd40cae2SJens Axboe 	if (issue_flags & IO_URING_F_UNLOCKED)
206cd40cae2SJens Axboe 		mutex_lock(&ctx->uring_lock);
207cd40cae2SJens Axboe 	lockdep_assert_held(&ctx->uring_lock);
208cd40cae2SJens Axboe }
209cd40cae2SJens Axboe 
210f9ead18cSJens Axboe static inline void io_commit_cqring(struct io_ring_ctx *ctx)
211f9ead18cSJens Axboe {
212f9ead18cSJens Axboe 	/* order cqe stores with ring update */
213f9ead18cSJens Axboe 	smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
214f9ead18cSJens Axboe }
215f9ead18cSJens Axboe 
2167b235dd8SPavel Begunkov static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
2177b235dd8SPavel Begunkov {
218bca39f39SPavel Begunkov 	if (wq_has_sleeper(&ctx->poll_wq))
2197b235dd8SPavel Begunkov 		__wake_up(&ctx->poll_wq, TASK_NORMAL, 0,
2207b235dd8SPavel Begunkov 				poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
2217b235dd8SPavel Begunkov }
2227b235dd8SPavel Begunkov 
2236e7248adSPavel Begunkov static inline void io_cqring_wake(struct io_ring_ctx *ctx)
224f3b44f92SJens Axboe {
225f3b44f92SJens Axboe 	/*
22644648532SJens Axboe 	 * Trigger waitqueue handler on all waiters on our waitqueue. This
22744648532SJens Axboe 	 * won't necessarily wake up all the tasks, io_should_wake() will make
22844648532SJens Axboe 	 * that decision.
22944648532SJens Axboe 	 *
23044648532SJens Axboe 	 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
23144648532SJens Axboe 	 * set in the mask so that if we recurse back into our own poll
23244648532SJens Axboe 	 * waitqueue handlers, we know we have a dependency between eventfd or
23344648532SJens Axboe 	 * epoll and should terminate multishot poll at that point.
234f3b44f92SJens Axboe 	 */
2356e7248adSPavel Begunkov 	if (wq_has_sleeper(&ctx->cq_wait))
23644648532SJens Axboe 		__wake_up(&ctx->cq_wait, TASK_NORMAL, 0,
23744648532SJens Axboe 				poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
238f3b44f92SJens Axboe }
239f3b44f92SJens Axboe 
24017437f31SJens Axboe static inline bool io_sqring_full(struct io_ring_ctx *ctx)
24117437f31SJens Axboe {
24217437f31SJens Axboe 	struct io_rings *r = ctx->rings;
24317437f31SJens Axboe 
24417437f31SJens Axboe 	return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
24517437f31SJens Axboe }
24617437f31SJens Axboe 
24717437f31SJens Axboe static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
24817437f31SJens Axboe {
24917437f31SJens Axboe 	struct io_rings *rings = ctx->rings;
250e3ef728fSJens Axboe 	unsigned int entries;
25117437f31SJens Axboe 
25217437f31SJens Axboe 	/* make sure SQ entry isn't read before tail */
253e3ef728fSJens Axboe 	entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
254e3ef728fSJens Axboe 	return min(entries, ctx->sq_entries);
25517437f31SJens Axboe }
25617437f31SJens Axboe 
257c0e0d6baSDylan Yudaken static inline int io_run_task_work(void)
25817437f31SJens Axboe {
2597cfe7a09SJens Axboe 	/*
2607cfe7a09SJens Axboe 	 * Always check-and-clear the task_work notification signal. With how
2617cfe7a09SJens Axboe 	 * signaling works for task_work, we can find it set with nothing to
2627cfe7a09SJens Axboe 	 * run. We need to clear it for that case, like get_signal() does.
2637cfe7a09SJens Axboe 	 */
26446a525e1SJens Axboe 	if (test_thread_flag(TIF_NOTIFY_SIGNAL))
26517437f31SJens Axboe 		clear_notify_signal();
266b5d3ae20SJens Axboe 	/*
267b5d3ae20SJens Axboe 	 * PF_IO_WORKER never returns to userspace, so check here if we have
268b5d3ae20SJens Axboe 	 * notify work that needs processing.
269b5d3ae20SJens Axboe 	 */
270b5d3ae20SJens Axboe 	if (current->flags & PF_IO_WORKER &&
2712f2bb1ffSJens Axboe 	    test_thread_flag(TIF_NOTIFY_RESUME)) {
2722f2bb1ffSJens Axboe 		__set_current_state(TASK_RUNNING);
273b5d3ae20SJens Axboe 		resume_user_mode_work(NULL);
2742f2bb1ffSJens Axboe 	}
2757cfe7a09SJens Axboe 	if (task_work_pending(current)) {
27646a525e1SJens Axboe 		__set_current_state(TASK_RUNNING);
27717437f31SJens Axboe 		task_work_run();
278c0e0d6baSDylan Yudaken 		return 1;
27917437f31SJens Axboe 	}
28017437f31SJens Axboe 
281c0e0d6baSDylan Yudaken 	return 0;
282c0e0d6baSDylan Yudaken }
283c0e0d6baSDylan Yudaken 
284dac6a0eaSJens Axboe static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
285dac6a0eaSJens Axboe {
2866434ec01SJens Axboe 	return task_work_pending(current) || !wq_list_empty(&ctx->work_llist);
287dac6a0eaSJens Axboe }
288dac6a0eaSJens Axboe 
289a282967cSPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, struct io_tw_state *ts)
290aa1e90f6SPavel Begunkov {
291a282967cSPavel Begunkov 	if (!ts->locked) {
292aa1e90f6SPavel Begunkov 		mutex_lock(&ctx->uring_lock);
293a282967cSPavel Begunkov 		ts->locked = true;
294aa1e90f6SPavel Begunkov 	}
295aa1e90f6SPavel Begunkov }
296aa1e90f6SPavel Begunkov 
2979da070b1SPavel Begunkov /*
2989da070b1SPavel Begunkov  * Don't complete immediately but use deferred completion infrastructure.
2999da070b1SPavel Begunkov  * Protected by ->uring_lock and can only be used either with
3009da070b1SPavel Begunkov  * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
3019da070b1SPavel Begunkov  */
3029da070b1SPavel Begunkov static inline void io_req_complete_defer(struct io_kiocb *req)
3039da070b1SPavel Begunkov 	__must_hold(&req->ctx->uring_lock)
304aa1e90f6SPavel Begunkov {
305aa1e90f6SPavel Begunkov 	struct io_submit_state *state = &req->ctx->submit_state;
306aa1e90f6SPavel Begunkov 
3079da070b1SPavel Begunkov 	lockdep_assert_held(&req->ctx->uring_lock);
3089da070b1SPavel Begunkov 
309aa1e90f6SPavel Begunkov 	wq_list_add_tail(&req->comp_list, &state->compl_reqs);
310aa1e90f6SPavel Begunkov }
311aa1e90f6SPavel Begunkov 
31246929b08SPavel Begunkov static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
31346929b08SPavel Begunkov {
314bca39f39SPavel Begunkov 	if (unlikely(ctx->off_timeout_used || ctx->drain_active ||
315bca39f39SPavel Begunkov 		     ctx->has_evfd || ctx->poll_activated))
31646929b08SPavel Begunkov 		__io_commit_cqring_flush(ctx);
31746929b08SPavel Begunkov }
31846929b08SPavel Begunkov 
31963809137SPavel Begunkov static inline void io_get_task_refs(int nr)
32063809137SPavel Begunkov {
32163809137SPavel Begunkov 	struct io_uring_task *tctx = current->io_uring;
32263809137SPavel Begunkov 
32363809137SPavel Begunkov 	tctx->cached_refs -= nr;
32463809137SPavel Begunkov 	if (unlikely(tctx->cached_refs < 0))
32563809137SPavel Begunkov 		io_task_refs_refill(tctx);
32663809137SPavel Begunkov }
32763809137SPavel Begunkov 
328bd1a3783SPavel Begunkov static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
329bd1a3783SPavel Begunkov {
330bd1a3783SPavel Begunkov 	return !ctx->submit_state.free_list.next;
331bd1a3783SPavel Begunkov }
332bd1a3783SPavel Begunkov 
333c1755c25SBreno Leitao extern struct kmem_cache *req_cachep;
334c1755c25SBreno Leitao 
335c8576f3eSPavel Begunkov static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx)
336bd1a3783SPavel Begunkov {
337c1755c25SBreno Leitao 	struct io_kiocb *req;
338bd1a3783SPavel Begunkov 
339c1755c25SBreno Leitao 	req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list);
340c1755c25SBreno Leitao 	wq_stack_extract(&ctx->submit_state.free_list);
341c1755c25SBreno Leitao 	return req;
342bd1a3783SPavel Begunkov }
343bd1a3783SPavel Begunkov 
344c8576f3eSPavel Begunkov static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req)
345c8576f3eSPavel Begunkov {
346c8576f3eSPavel Begunkov 	if (unlikely(io_req_cache_empty(ctx))) {
347c8576f3eSPavel Begunkov 		if (!__io_alloc_req_refill(ctx))
348c8576f3eSPavel Begunkov 			return false;
349c8576f3eSPavel Begunkov 	}
350c8576f3eSPavel Begunkov 	*req = io_extract_req(ctx);
351c8576f3eSPavel Begunkov 	return true;
352c8576f3eSPavel Begunkov }
353c8576f3eSPavel Begunkov 
354140102aeSPavel Begunkov static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
355140102aeSPavel Begunkov {
356140102aeSPavel Begunkov 	return likely(ctx->submitter_task == current);
357140102aeSPavel Begunkov }
358140102aeSPavel Begunkov 
35976de6749SPavel Begunkov static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
36076de6749SPavel Begunkov {
3616567506bSPavel Begunkov 	return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
3626567506bSPavel Begunkov 		      ctx->submitter_task == current);
36376de6749SPavel Begunkov }
36476de6749SPavel Begunkov 
365833b5dffSPavel Begunkov static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
366833b5dffSPavel Begunkov {
367833b5dffSPavel Begunkov 	io_req_set_res(req, res, 0);
368833b5dffSPavel Begunkov 	req->io_task_work.func = io_req_task_complete;
369833b5dffSPavel Begunkov 	io_req_task_work_add(req);
370833b5dffSPavel Begunkov }
371833b5dffSPavel Begunkov 
37296c7d4f8SBreno Leitao /*
37396c7d4f8SBreno Leitao  * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each
37496c7d4f8SBreno Leitao  * slot.
37596c7d4f8SBreno Leitao  */
37696c7d4f8SBreno Leitao static inline size_t uring_sqe_size(struct io_ring_ctx *ctx)
37796c7d4f8SBreno Leitao {
37896c7d4f8SBreno Leitao 	if (ctx->flags & IORING_SETUP_SQE128)
37996c7d4f8SBreno Leitao 		return 2 * sizeof(struct io_uring_sqe);
38096c7d4f8SBreno Leitao 	return sizeof(struct io_uring_sqe);
38196c7d4f8SBreno Leitao }
382de23077eSJens Axboe #endif
383