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