xref: /openbmc/linux/io_uring/io_uring.h (revision 1bec951c)
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>
744648532SJens Axboe #include <uapi/linux/eventpoll.h>
8ab1c84d8SPavel Begunkov #include "io-wq.h"
9a6b21fbbSPavel Begunkov #include "slist.h"
10ab1c84d8SPavel Begunkov #include "filetable.h"
11de23077eSJens Axboe 
12f3b44f92SJens Axboe #ifndef CREATE_TRACE_POINTS
13f3b44f92SJens Axboe #include <trace/events/io_uring.h>
14f3b44f92SJens Axboe #endif
15f3b44f92SJens Axboe 
1697b388d7SJens Axboe enum {
1797b388d7SJens Axboe 	IOU_OK			= 0,
1897b388d7SJens Axboe 	IOU_ISSUE_SKIP_COMPLETE	= -EIOCBQUEUED,
19114eccdfSDylan Yudaken 
20114eccdfSDylan Yudaken 	/*
2191482864SPavel Begunkov 	 * Intended only when both IO_URING_F_MULTISHOT is passed
2291482864SPavel Begunkov 	 * to indicate to the poll runner that multishot should be
23114eccdfSDylan Yudaken 	 * removed and the result is set on req->cqe.res.
24114eccdfSDylan Yudaken 	 */
25114eccdfSDylan Yudaken 	IOU_STOP_MULTISHOT	= -ECANCELED,
2697b388d7SJens Axboe };
2797b388d7SJens Axboe 
28aa1df3a3SPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow);
2968494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req);
30c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx);
31b3026767SDylan Yudaken int __io_run_local_work(struct io_ring_ctx *ctx, bool *locked);
32c0e0d6baSDylan Yudaken int io_run_local_work(struct io_ring_ctx *ctx);
339046c641SPavel Begunkov void io_req_complete_failed(struct io_kiocb *req, s32 res);
34*1bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags);
3552120f0fSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
3652120f0fSDylan Yudaken 		     bool allow_overflow);
37eb42cebbSPavel Begunkov bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
38eb42cebbSPavel Begunkov 		     bool allow_overflow);
399046c641SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
409046c641SPavel Begunkov 
41*1bec951cSPavel Begunkov static inline void io_req_complete_post_tw(struct io_kiocb *req, bool *locked)
42*1bec951cSPavel Begunkov {
43*1bec951cSPavel Begunkov 	unsigned flags = *locked ? 0 : IO_URING_F_UNLOCKED;
44*1bec951cSPavel Begunkov 
45*1bec951cSPavel Begunkov 	io_req_complete_post(req, flags);
46*1bec951cSPavel Begunkov }
47*1bec951cSPavel Begunkov 
489046c641SPavel Begunkov struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages);
499046c641SPavel Begunkov 
509046c641SPavel Begunkov struct file *io_file_get_normal(struct io_kiocb *req, int fd);
519046c641SPavel Begunkov struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
529046c641SPavel Begunkov 			       unsigned issue_flags);
539046c641SPavel Begunkov 
54f6b543fdSJens Axboe static inline bool io_req_ffs_set(struct io_kiocb *req)
55f6b543fdSJens Axboe {
56f6b543fdSJens Axboe 	return req->flags & REQ_F_FIXED_FILE;
57f6b543fdSJens Axboe }
58f6b543fdSJens Axboe 
59e52d2e58SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, bool allow_local);
609046c641SPavel Begunkov bool io_is_uring_fops(struct file *file);
619046c641SPavel Begunkov bool io_alloc_async_data(struct io_kiocb *req);
629046c641SPavel Begunkov void io_req_task_queue(struct io_kiocb *req);
639046c641SPavel Begunkov void io_queue_iowq(struct io_kiocb *req, bool *dont_use);
649046c641SPavel Begunkov void io_req_task_complete(struct io_kiocb *req, bool *locked);
659046c641SPavel Begunkov void io_req_task_queue_fail(struct io_kiocb *req, int ret);
669046c641SPavel Begunkov void io_req_task_submit(struct io_kiocb *req, bool *locked);
679046c641SPavel Begunkov void tctx_task_work(struct callback_head *cb);
689046c641SPavel Begunkov __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
699046c641SPavel Begunkov int io_uring_alloc_task_context(struct task_struct *task,
709046c641SPavel Begunkov 				struct io_ring_ctx *ctx);
719046c641SPavel Begunkov 
729046c641SPavel Begunkov int io_poll_issue(struct io_kiocb *req, bool *locked);
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);
83e70cb608SPavel Begunkov void __io_put_task(struct task_struct *task, int nr);
8463809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx);
85bd1a3783SPavel Begunkov bool __io_alloc_req_refill(struct io_ring_ctx *ctx);
869046c641SPavel Begunkov 
879046c641SPavel Begunkov bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
889046c641SPavel Begunkov 			bool cancel_all);
899046c641SPavel Begunkov 
90e52d2e58SPavel Begunkov static inline void io_req_task_work_add(struct io_kiocb *req)
91e52d2e58SPavel Begunkov {
92e52d2e58SPavel Begunkov 	__io_req_task_work_add(req, true);
93e52d2e58SPavel Begunkov }
94e52d2e58SPavel Begunkov 
959046c641SPavel Begunkov #define io_for_each_link(pos, head) \
969046c641SPavel Begunkov 	for (pos = (head); pos; pos = pos->link)
97f3b44f92SJens Axboe 
9825399321SPavel Begunkov static inline void io_cq_lock(struct io_ring_ctx *ctx)
9925399321SPavel Begunkov 	__acquires(ctx->completion_lock)
10025399321SPavel Begunkov {
10125399321SPavel Begunkov 	spin_lock(&ctx->completion_lock);
10225399321SPavel Begunkov }
10325399321SPavel Begunkov 
10425399321SPavel Begunkov void io_cq_unlock_post(struct io_ring_ctx *ctx);
10525399321SPavel Begunkov 
106aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe_overflow(struct io_ring_ctx *ctx,
107aa1df3a3SPavel Begunkov 						       bool overflow)
108f3b44f92SJens Axboe {
109f3b44f92SJens Axboe 	if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) {
110f3b44f92SJens Axboe 		struct io_uring_cqe *cqe = ctx->cqe_cached;
111f3b44f92SJens Axboe 
112f3b44f92SJens Axboe 		ctx->cached_cq_tail++;
113f3b44f92SJens Axboe 		ctx->cqe_cached++;
114b3659a65SPavel Begunkov 		if (ctx->flags & IORING_SETUP_CQE32)
115b3659a65SPavel Begunkov 			ctx->cqe_cached++;
116f3b44f92SJens Axboe 		return cqe;
117f3b44f92SJens Axboe 	}
118f3b44f92SJens Axboe 
119aa1df3a3SPavel Begunkov 	return __io_get_cqe(ctx, overflow);
120aa1df3a3SPavel Begunkov }
121aa1df3a3SPavel Begunkov 
122aa1df3a3SPavel Begunkov static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
123aa1df3a3SPavel Begunkov {
124aa1df3a3SPavel Begunkov 	return io_get_cqe_overflow(ctx, false);
125f3b44f92SJens Axboe }
126f3b44f92SJens Axboe 
127f3b44f92SJens Axboe static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx,
128f3b44f92SJens Axboe 				     struct io_kiocb *req)
129f3b44f92SJens Axboe {
130f3b44f92SJens Axboe 	struct io_uring_cqe *cqe;
131f3b44f92SJens Axboe 
132f3b44f92SJens Axboe 	/*
133f3b44f92SJens Axboe 	 * If we can't get a cq entry, userspace overflowed the
134f3b44f92SJens Axboe 	 * submission (by quite a lot). Increment the overflow count in
135f3b44f92SJens Axboe 	 * the ring.
136f3b44f92SJens Axboe 	 */
137f3b44f92SJens Axboe 	cqe = io_get_cqe(ctx);
138e8c328c3SPavel Begunkov 	if (unlikely(!cqe))
139e8c328c3SPavel Begunkov 		return io_req_cqe_overflow(req);
140e0486f3fSDylan Yudaken 
141e0486f3fSDylan Yudaken 	trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
142e0486f3fSDylan Yudaken 				req->cqe.res, req->cqe.flags,
143e0486f3fSDylan Yudaken 				(req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0,
144e0486f3fSDylan Yudaken 				(req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0);
145e0486f3fSDylan Yudaken 
146f3b44f92SJens Axboe 	memcpy(cqe, &req->cqe, sizeof(*cqe));
147e8c328c3SPavel Begunkov 
148e8c328c3SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32) {
149f3b44f92SJens Axboe 		u64 extra1 = 0, extra2 = 0;
150f3b44f92SJens Axboe 
151f3b44f92SJens Axboe 		if (req->flags & REQ_F_CQE32_INIT) {
152f3b44f92SJens Axboe 			extra1 = req->extra1;
153f3b44f92SJens Axboe 			extra2 = req->extra2;
154f3b44f92SJens Axboe 		}
155f3b44f92SJens Axboe 
156f3b44f92SJens Axboe 		WRITE_ONCE(cqe->big_cqe[0], extra1);
157f3b44f92SJens Axboe 		WRITE_ONCE(cqe->big_cqe[1], extra2);
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 
182531113bbSJens Axboe static inline void io_put_file(struct file *file)
183531113bbSJens Axboe {
184531113bbSJens Axboe 	if (file)
185531113bbSJens Axboe 		fput(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 
216fc86f9d3SPavel Begunkov /* requires smb_mb() prior, see wq_has_sleeper() */
217fc86f9d3SPavel Begunkov static inline void __io_cqring_wake(struct io_ring_ctx *ctx)
218f3b44f92SJens Axboe {
219f3b44f92SJens Axboe 	/*
22044648532SJens Axboe 	 * Trigger waitqueue handler on all waiters on our waitqueue. This
22144648532SJens Axboe 	 * won't necessarily wake up all the tasks, io_should_wake() will make
22244648532SJens Axboe 	 * that decision.
22344648532SJens Axboe 	 *
22444648532SJens Axboe 	 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter
22544648532SJens Axboe 	 * set in the mask so that if we recurse back into our own poll
22644648532SJens Axboe 	 * waitqueue handlers, we know we have a dependency between eventfd or
22744648532SJens Axboe 	 * epoll and should terminate multishot poll at that point.
228f3b44f92SJens Axboe 	 */
229fc86f9d3SPavel Begunkov 	if (waitqueue_active(&ctx->cq_wait))
23044648532SJens Axboe 		__wake_up(&ctx->cq_wait, TASK_NORMAL, 0,
23144648532SJens Axboe 				poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
232f3b44f92SJens Axboe }
233f3b44f92SJens Axboe 
234fc86f9d3SPavel Begunkov static inline void io_cqring_wake(struct io_ring_ctx *ctx)
235fc86f9d3SPavel Begunkov {
236fc86f9d3SPavel Begunkov 	smp_mb();
237fc86f9d3SPavel Begunkov 	__io_cqring_wake(ctx);
238fc86f9d3SPavel Begunkov }
239fc86f9d3SPavel Begunkov 
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;
25017437f31SJens Axboe 
25117437f31SJens Axboe 	/* make sure SQ entry isn't read before tail */
25217437f31SJens Axboe 	return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
25317437f31SJens Axboe }
25417437f31SJens Axboe 
255c0e0d6baSDylan Yudaken static inline int io_run_task_work(void)
25617437f31SJens Axboe {
25746a525e1SJens Axboe 	if (task_work_pending(current)) {
25846a525e1SJens Axboe 		if (test_thread_flag(TIF_NOTIFY_SIGNAL))
25917437f31SJens Axboe 			clear_notify_signal();
26046a525e1SJens Axboe 		__set_current_state(TASK_RUNNING);
26117437f31SJens Axboe 		task_work_run();
262c0e0d6baSDylan Yudaken 		return 1;
26317437f31SJens Axboe 	}
26417437f31SJens Axboe 
265c0e0d6baSDylan Yudaken 	return 0;
266c0e0d6baSDylan Yudaken }
267c0e0d6baSDylan Yudaken 
268dac6a0eaSJens Axboe static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
269dac6a0eaSJens Axboe {
270dac6a0eaSJens Axboe 	return test_thread_flag(TIF_NOTIFY_SIGNAL) ||
271dac6a0eaSJens Axboe 		!wq_list_empty(&ctx->work_llist);
272dac6a0eaSJens Axboe }
273dac6a0eaSJens Axboe 
274c0e0d6baSDylan Yudaken static inline int io_run_task_work_ctx(struct io_ring_ctx *ctx)
275c0e0d6baSDylan Yudaken {
276c0e0d6baSDylan Yudaken 	int ret = 0;
277c0e0d6baSDylan Yudaken 	int ret2;
278c0e0d6baSDylan Yudaken 
279c0e0d6baSDylan Yudaken 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
280c0e0d6baSDylan Yudaken 		ret = io_run_local_work(ctx);
281c0e0d6baSDylan Yudaken 
282c0e0d6baSDylan Yudaken 	/* want to run this after in case more is added */
283c0e0d6baSDylan Yudaken 	ret2 = io_run_task_work();
284c0e0d6baSDylan Yudaken 
285c0e0d6baSDylan Yudaken 	/* Try propagate error in favour of if tasks were run,
286c0e0d6baSDylan Yudaken 	 * but still make sure to run them if requested
287c0e0d6baSDylan Yudaken 	 */
288c0e0d6baSDylan Yudaken 	if (ret >= 0)
289c0e0d6baSDylan Yudaken 		ret += ret2;
290c0e0d6baSDylan Yudaken 
291c0e0d6baSDylan Yudaken 	return ret;
29217437f31SJens Axboe }
29317437f31SJens Axboe 
29444f87745SPavel Begunkov static inline int io_run_local_work_locked(struct io_ring_ctx *ctx)
29544f87745SPavel Begunkov {
296b3026767SDylan Yudaken 	bool locked;
297b3026767SDylan Yudaken 	int ret;
298b3026767SDylan Yudaken 
29944f87745SPavel Begunkov 	if (llist_empty(&ctx->work_llist))
30044f87745SPavel Begunkov 		return 0;
301b3026767SDylan Yudaken 
302b3026767SDylan Yudaken 	locked = true;
303b3026767SDylan Yudaken 	ret = __io_run_local_work(ctx, &locked);
304b3026767SDylan Yudaken 	/* shouldn't happen! */
305b3026767SDylan Yudaken 	if (WARN_ON_ONCE(!locked))
306b3026767SDylan Yudaken 		mutex_lock(&ctx->uring_lock);
307b3026767SDylan Yudaken 	return ret;
30844f87745SPavel Begunkov }
30944f87745SPavel Begunkov 
310aa1e90f6SPavel Begunkov static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
311aa1e90f6SPavel Begunkov {
312aa1e90f6SPavel Begunkov 	if (!*locked) {
313aa1e90f6SPavel Begunkov 		mutex_lock(&ctx->uring_lock);
314aa1e90f6SPavel Begunkov 		*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  */
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 
33346929b08SPavel Begunkov static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
33446929b08SPavel Begunkov {
33546929b08SPavel Begunkov 	if (unlikely(ctx->off_timeout_used || ctx->drain_active || ctx->has_evfd))
33646929b08SPavel Begunkov 		__io_commit_cqring_flush(ctx);
33746929b08SPavel Begunkov }
33846929b08SPavel Begunkov 
339e70cb608SPavel Begunkov /* must to be called somewhat shortly after putting a request */
340e70cb608SPavel Begunkov static inline void io_put_task(struct task_struct *task, int nr)
341e70cb608SPavel Begunkov {
342e70cb608SPavel Begunkov 	if (likely(task == current))
343e70cb608SPavel Begunkov 		task->io_uring->cached_refs += nr;
344e70cb608SPavel Begunkov 	else
345e70cb608SPavel Begunkov 		__io_put_task(task, nr);
346e70cb608SPavel Begunkov }
347e70cb608SPavel Begunkov 
34863809137SPavel Begunkov static inline void io_get_task_refs(int nr)
34963809137SPavel Begunkov {
35063809137SPavel Begunkov 	struct io_uring_task *tctx = current->io_uring;
35163809137SPavel Begunkov 
35263809137SPavel Begunkov 	tctx->cached_refs -= nr;
35363809137SPavel Begunkov 	if (unlikely(tctx->cached_refs < 0))
35463809137SPavel Begunkov 		io_task_refs_refill(tctx);
35563809137SPavel Begunkov }
35663809137SPavel Begunkov 
357bd1a3783SPavel Begunkov static inline bool io_req_cache_empty(struct io_ring_ctx *ctx)
358bd1a3783SPavel Begunkov {
359bd1a3783SPavel Begunkov 	return !ctx->submit_state.free_list.next;
360bd1a3783SPavel Begunkov }
361bd1a3783SPavel Begunkov 
362bd1a3783SPavel Begunkov static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
363bd1a3783SPavel Begunkov {
364bd1a3783SPavel Begunkov 	if (unlikely(io_req_cache_empty(ctx)))
365bd1a3783SPavel Begunkov 		return __io_alloc_req_refill(ctx);
366bd1a3783SPavel Begunkov 	return true;
367bd1a3783SPavel Begunkov }
368bd1a3783SPavel Begunkov 
369bd1a3783SPavel Begunkov static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
370bd1a3783SPavel Begunkov {
371bd1a3783SPavel Begunkov 	struct io_wq_work_node *node;
372bd1a3783SPavel Begunkov 
373bd1a3783SPavel Begunkov 	node = wq_stack_extract(&ctx->submit_state.free_list);
374bd1a3783SPavel Begunkov 	return container_of(node, struct io_kiocb, comp_list);
375bd1a3783SPavel Begunkov }
376bd1a3783SPavel Begunkov 
37776de6749SPavel Begunkov static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
37876de6749SPavel Begunkov {
3796567506bSPavel Begunkov 	return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
3806567506bSPavel Begunkov 		      ctx->submitter_task == current);
38176de6749SPavel Begunkov }
38276de6749SPavel Begunkov 
383833b5dffSPavel Begunkov static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res)
384833b5dffSPavel Begunkov {
385833b5dffSPavel Begunkov 	io_req_set_res(req, res, 0);
386833b5dffSPavel Begunkov 	req->io_task_work.func = io_req_task_complete;
387833b5dffSPavel Begunkov 	io_req_task_work_add(req);
388833b5dffSPavel Begunkov }
389833b5dffSPavel Begunkov 
390de23077eSJens Axboe #endif
391