]> git.ipfire.org Git - thirdparty/linux.git/blame - io_uring/io_uring.h
io_uring/net: use unsigned for flags
[thirdparty/linux.git] / io_uring / io_uring.h
CommitLineData
de23077e
JA
1#ifndef IOU_CORE_H
2#define IOU_CORE_H
3
4#include <linux/errno.h>
cd40cae2 5#include <linux/lockdep.h>
ab1c84d8
PB
6#include <linux/io_uring_types.h>
7#include "io-wq.h"
a6b21fbb 8#include "slist.h"
ab1c84d8 9#include "filetable.h"
de23077e 10
f3b44f92
JA
11#ifndef CREATE_TRACE_POINTS
12#include <trace/events/io_uring.h>
13#endif
14
97b388d7
JA
15enum {
16 IOU_OK = 0,
17 IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED,
114eccdf
DY
18
19 /*
20 * Intended only when both REQ_F_POLLED and REQ_F_APOLL_MULTISHOT
21 * are set to indicate to the poll runner that multishot should be
22 * removed and the result is set on req->cqe.res.
23 */
24 IOU_STOP_MULTISHOT = -ECANCELED,
97b388d7
JA
25};
26
faf88dde 27struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx);
68494a65 28bool io_req_cqe_overflow(struct io_kiocb *req);
9046c641
PB
29int io_run_task_work_sig(void);
30void io_req_complete_failed(struct io_kiocb *req, s32 res);
31void __io_req_complete(struct io_kiocb *req, unsigned issue_flags);
32void io_req_complete_post(struct io_kiocb *req);
33void __io_req_complete_post(struct io_kiocb *req);
52120f0f
DY
34bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
35 bool allow_overflow);
eb42cebb
PB
36bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
37 bool allow_overflow);
9046c641
PB
38void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
39
40struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages);
41
42struct file *io_file_get_normal(struct io_kiocb *req, int fd);
43struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
44 unsigned issue_flags);
45
f6b543fd
JA
46static inline bool io_req_ffs_set(struct io_kiocb *req)
47{
48 return req->flags & REQ_F_FIXED_FILE;
49}
50
9046c641
PB
51bool io_is_uring_fops(struct file *file);
52bool io_alloc_async_data(struct io_kiocb *req);
53void io_req_task_work_add(struct io_kiocb *req);
9046c641
PB
54void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags);
55void io_req_task_queue(struct io_kiocb *req);
56void io_queue_iowq(struct io_kiocb *req, bool *dont_use);
57void io_req_task_complete(struct io_kiocb *req, bool *locked);
58void io_req_task_queue_fail(struct io_kiocb *req, int ret);
59void io_req_task_submit(struct io_kiocb *req, bool *locked);
60void tctx_task_work(struct callback_head *cb);
61__cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
62int io_uring_alloc_task_context(struct task_struct *task,
63 struct io_ring_ctx *ctx);
64
65int io_poll_issue(struct io_kiocb *req, bool *locked);
66int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr);
67int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin);
68void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node);
69int io_req_prep_async(struct io_kiocb *req);
70
71struct io_wq_work *io_wq_free_work(struct io_wq_work *work);
72void io_wq_submit_work(struct io_wq_work *work);
73
74void io_free_req(struct io_kiocb *req);
75void io_queue_next(struct io_kiocb *req);
e70cb608 76void __io_put_task(struct task_struct *task, int nr);
63809137 77void io_task_refs_refill(struct io_uring_task *tctx);
9046c641
PB
78
79bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
80 bool cancel_all);
81
82#define io_for_each_link(pos, head) \
83 for (pos = (head); pos; pos = pos->link)
f3b44f92 84
25399321
PB
85static inline void io_cq_lock(struct io_ring_ctx *ctx)
86 __acquires(ctx->completion_lock)
87{
88 spin_lock(&ctx->completion_lock);
89}
90
91void io_cq_unlock_post(struct io_ring_ctx *ctx);
92
f3b44f92
JA
93static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
94{
95 if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) {
96 struct io_uring_cqe *cqe = ctx->cqe_cached;
97
f3b44f92
JA
98 ctx->cached_cq_tail++;
99 ctx->cqe_cached++;
b3659a65
PB
100 if (ctx->flags & IORING_SETUP_CQE32)
101 ctx->cqe_cached++;
f3b44f92
JA
102 return cqe;
103 }
104
105 return __io_get_cqe(ctx);
106}
107
108static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx,
109 struct io_kiocb *req)
110{
111 struct io_uring_cqe *cqe;
112
e8c328c3
PB
113 /*
114 * If we can't get a cq entry, userspace overflowed the
115 * submission (by quite a lot). Increment the overflow count in
116 * the ring.
117 */
118 cqe = io_get_cqe(ctx);
119 if (unlikely(!cqe))
120 return io_req_cqe_overflow(req);
e0486f3f
DY
121
122 trace_io_uring_complete(req->ctx, req, req->cqe.user_data,
123 req->cqe.res, req->cqe.flags,
124 (req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0,
125 (req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0);
126
e8c328c3
PB
127 memcpy(cqe, &req->cqe, sizeof(*cqe));
128
129 if (ctx->flags & IORING_SETUP_CQE32) {
f3b44f92
JA
130 u64 extra1 = 0, extra2 = 0;
131
132 if (req->flags & REQ_F_CQE32_INIT) {
133 extra1 = req->extra1;
134 extra2 = req->extra2;
135 }
136
e8c328c3
PB
137 WRITE_ONCE(cqe->big_cqe[0], extra1);
138 WRITE_ONCE(cqe->big_cqe[1], extra2);
f3b44f92 139 }
e8c328c3 140 return true;
f3b44f92
JA
141}
142
531113bb
JA
143static inline void req_set_fail(struct io_kiocb *req)
144{
145 req->flags |= REQ_F_FAIL;
146 if (req->flags & REQ_F_CQE_SKIP) {
147 req->flags &= ~REQ_F_CQE_SKIP;
148 req->flags |= REQ_F_SKIP_LINK_CQES;
149 }
150}
151
de23077e
JA
152static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags)
153{
154 req->cqe.res = res;
155 req->cqe.flags = cflags;
156}
157
99f15d8d
JA
158static inline bool req_has_async_data(struct io_kiocb *req)
159{
160 return req->flags & REQ_F_ASYNC_DATA;
161}
162
531113bb
JA
163static inline void io_put_file(struct file *file)
164{
165 if (file)
166 fput(file);
167}
168
cd40cae2
JA
169static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx,
170 unsigned issue_flags)
171{
172 lockdep_assert_held(&ctx->uring_lock);
173 if (issue_flags & IO_URING_F_UNLOCKED)
174 mutex_unlock(&ctx->uring_lock);
175}
176
177static inline void io_ring_submit_lock(struct io_ring_ctx *ctx,
178 unsigned issue_flags)
179{
180 /*
181 * "Normal" inline submissions always hold the uring_lock, since we
182 * grab it from the system call. Same is true for the SQPOLL offload.
183 * The only exception is when we've detached the request and issue it
184 * from an async worker thread, grab the lock for that case.
185 */
186 if (issue_flags & IO_URING_F_UNLOCKED)
187 mutex_lock(&ctx->uring_lock);
188 lockdep_assert_held(&ctx->uring_lock);
189}
190
f9ead18c
JA
191static inline void io_commit_cqring(struct io_ring_ctx *ctx)
192{
193 /* order cqe stores with ring update */
194 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
195}
196
f3b44f92
JA
197static inline void io_cqring_wake(struct io_ring_ctx *ctx)
198{
199 /*
200 * wake_up_all() may seem excessive, but io_wake_function() and
201 * io_should_wake() handle the termination of the loop and only
202 * wake as many waiters as we need to.
203 */
204 if (wq_has_sleeper(&ctx->cq_wait))
205 wake_up_all(&ctx->cq_wait);
206}
207
17437f31
JA
208static inline bool io_sqring_full(struct io_ring_ctx *ctx)
209{
210 struct io_rings *r = ctx->rings;
211
212 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
213}
214
215static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
216{
217 struct io_rings *rings = ctx->rings;
218
219 /* make sure SQ entry isn't read before tail */
220 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
221}
222
223static inline bool io_run_task_work(void)
224{
625d38b3 225 if (test_thread_flag(TIF_NOTIFY_SIGNAL)) {
17437f31
JA
226 __set_current_state(TASK_RUNNING);
227 clear_notify_signal();
228 if (task_work_pending(current))
229 task_work_run();
230 return true;
231 }
232
233 return false;
234}
235
aa1e90f6
PB
236static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
237{
238 if (!*locked) {
239 mutex_lock(&ctx->uring_lock);
240 *locked = true;
241 }
242}
243
9da070b1
PB
244/*
245 * Don't complete immediately but use deferred completion infrastructure.
246 * Protected by ->uring_lock and can only be used either with
247 * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex.
248 */
249static inline void io_req_complete_defer(struct io_kiocb *req)
250 __must_hold(&req->ctx->uring_lock)
aa1e90f6
PB
251{
252 struct io_submit_state *state = &req->ctx->submit_state;
253
9da070b1
PB
254 lockdep_assert_held(&req->ctx->uring_lock);
255
aa1e90f6
PB
256 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
257}
258
46929b08
PB
259static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
260{
261 if (unlikely(ctx->off_timeout_used || ctx->drain_active || ctx->has_evfd))
262 __io_commit_cqring_flush(ctx);
263}
264
e70cb608
PB
265/* must to be called somewhat shortly after putting a request */
266static inline void io_put_task(struct task_struct *task, int nr)
267{
268 if (likely(task == current))
269 task->io_uring->cached_refs += nr;
270 else
271 __io_put_task(task, nr);
272}
273
63809137
PB
274static inline void io_get_task_refs(int nr)
275{
276 struct io_uring_task *tctx = current->io_uring;
277
278 tctx->cached_refs -= nr;
279 if (unlikely(tctx->cached_refs < 0))
280 io_task_refs_refill(tctx);
281}
282
de23077e 283#endif