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> |
de23077e JA |
6 | #include "io_uring_types.h" |
7 | ||
f3b44f92 JA |
8 | #ifndef CREATE_TRACE_POINTS |
9 | #include <trace/events/io_uring.h> | |
10 | #endif | |
11 | ||
97b388d7 JA |
12 | enum { |
13 | IOU_OK = 0, | |
14 | IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED, | |
15 | }; | |
16 | ||
faf88dde | 17 | struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx); |
68494a65 | 18 | bool io_req_cqe_overflow(struct io_kiocb *req); |
f3b44f92 | 19 | |
f3b44f92 JA |
20 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
21 | { | |
22 | if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) { | |
23 | struct io_uring_cqe *cqe = ctx->cqe_cached; | |
24 | ||
f3b44f92 JA |
25 | ctx->cached_cq_tail++; |
26 | ctx->cqe_cached++; | |
b3659a65 PB |
27 | if (ctx->flags & IORING_SETUP_CQE32) |
28 | ctx->cqe_cached++; | |
f3b44f92 JA |
29 | return cqe; |
30 | } | |
31 | ||
32 | return __io_get_cqe(ctx); | |
33 | } | |
34 | ||
35 | static inline bool __io_fill_cqe_req(struct io_ring_ctx *ctx, | |
36 | struct io_kiocb *req) | |
37 | { | |
38 | struct io_uring_cqe *cqe; | |
39 | ||
ae5735c6 PB |
40 | trace_io_uring_complete(req->ctx, req, req->cqe.user_data, |
41 | req->cqe.res, req->cqe.flags, | |
42 | (req->flags & REQ_F_CQE32_INIT) ? req->extra1 : 0, | |
43 | (req->flags & REQ_F_CQE32_INIT) ? req->extra2 : 0); | |
e8c328c3 PB |
44 | /* |
45 | * If we can't get a cq entry, userspace overflowed the | |
46 | * submission (by quite a lot). Increment the overflow count in | |
47 | * the ring. | |
48 | */ | |
49 | cqe = io_get_cqe(ctx); | |
50 | if (unlikely(!cqe)) | |
51 | return io_req_cqe_overflow(req); | |
52 | memcpy(cqe, &req->cqe, sizeof(*cqe)); | |
53 | ||
54 | if (ctx->flags & IORING_SETUP_CQE32) { | |
f3b44f92 JA |
55 | u64 extra1 = 0, extra2 = 0; |
56 | ||
57 | if (req->flags & REQ_F_CQE32_INIT) { | |
58 | extra1 = req->extra1; | |
59 | extra2 = req->extra2; | |
60 | } | |
61 | ||
e8c328c3 PB |
62 | WRITE_ONCE(cqe->big_cqe[0], extra1); |
63 | WRITE_ONCE(cqe->big_cqe[1], extra2); | |
f3b44f92 | 64 | } |
e8c328c3 | 65 | return true; |
f3b44f92 JA |
66 | } |
67 | ||
531113bb JA |
68 | static inline void req_set_fail(struct io_kiocb *req) |
69 | { | |
70 | req->flags |= REQ_F_FAIL; | |
71 | if (req->flags & REQ_F_CQE_SKIP) { | |
72 | req->flags &= ~REQ_F_CQE_SKIP; | |
73 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
74 | } | |
75 | } | |
76 | ||
de23077e JA |
77 | static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags) |
78 | { | |
79 | req->cqe.res = res; | |
80 | req->cqe.flags = cflags; | |
81 | } | |
82 | ||
99f15d8d JA |
83 | static inline bool req_has_async_data(struct io_kiocb *req) |
84 | { | |
85 | return req->flags & REQ_F_ASYNC_DATA; | |
86 | } | |
87 | ||
531113bb JA |
88 | static inline void io_put_file(struct file *file) |
89 | { | |
90 | if (file) | |
91 | fput(file); | |
92 | } | |
93 | ||
cd40cae2 JA |
94 | static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, |
95 | unsigned issue_flags) | |
96 | { | |
97 | lockdep_assert_held(&ctx->uring_lock); | |
98 | if (issue_flags & IO_URING_F_UNLOCKED) | |
99 | mutex_unlock(&ctx->uring_lock); | |
100 | } | |
101 | ||
102 | static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, | |
103 | unsigned issue_flags) | |
104 | { | |
105 | /* | |
106 | * "Normal" inline submissions always hold the uring_lock, since we | |
107 | * grab it from the system call. Same is true for the SQPOLL offload. | |
108 | * The only exception is when we've detached the request and issue it | |
109 | * from an async worker thread, grab the lock for that case. | |
110 | */ | |
111 | if (issue_flags & IO_URING_F_UNLOCKED) | |
112 | mutex_lock(&ctx->uring_lock); | |
113 | lockdep_assert_held(&ctx->uring_lock); | |
114 | } | |
115 | ||
f9ead18c JA |
116 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
117 | { | |
118 | /* order cqe stores with ring update */ | |
119 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
120 | } | |
121 | ||
f3b44f92 JA |
122 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
123 | { | |
124 | /* | |
125 | * wake_up_all() may seem excessive, but io_wake_function() and | |
126 | * io_should_wake() handle the termination of the loop and only | |
127 | * wake as many waiters as we need to. | |
128 | */ | |
129 | if (wq_has_sleeper(&ctx->cq_wait)) | |
130 | wake_up_all(&ctx->cq_wait); | |
131 | } | |
132 | ||
17437f31 JA |
133 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
134 | { | |
135 | struct io_rings *r = ctx->rings; | |
136 | ||
137 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; | |
138 | } | |
139 | ||
140 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) | |
141 | { | |
142 | struct io_rings *rings = ctx->rings; | |
143 | ||
144 | /* make sure SQ entry isn't read before tail */ | |
145 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
146 | } | |
147 | ||
148 | static inline bool io_run_task_work(void) | |
149 | { | |
150 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { | |
151 | __set_current_state(TASK_RUNNING); | |
152 | clear_notify_signal(); | |
153 | if (task_work_pending(current)) | |
154 | task_work_run(); | |
155 | return true; | |
156 | } | |
157 | ||
158 | return false; | |
159 | } | |
160 | ||
aa1e90f6 PB |
161 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
162 | { | |
163 | if (!*locked) { | |
164 | mutex_lock(&ctx->uring_lock); | |
165 | *locked = true; | |
166 | } | |
167 | } | |
168 | ||
169 | static inline void io_req_add_compl_list(struct io_kiocb *req) | |
170 | { | |
171 | struct io_submit_state *state = &req->ctx->submit_state; | |
172 | ||
173 | if (!(req->flags & REQ_F_CQE_SKIP)) | |
174 | state->flush_cqes = true; | |
175 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
176 | } | |
177 | ||
73572984 | 178 | int io_run_task_work_sig(void); |
329061d3 | 179 | void io_req_complete_failed(struct io_kiocb *req, s32 res); |
99f15d8d | 180 | void __io_req_complete(struct io_kiocb *req, unsigned issue_flags); |
59915143 JA |
181 | void io_req_complete_post(struct io_kiocb *req); |
182 | void __io_req_complete_post(struct io_kiocb *req); | |
d245bca6 | 183 | bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); |
f9ead18c | 184 | void io_cqring_ev_posted(struct io_ring_ctx *ctx); |
f3b44f92 | 185 | void __io_commit_cqring_flush(struct io_ring_ctx *ctx); |
f9ead18c | 186 | |
3b77495a | 187 | struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages); |
329061d3 | 188 | |
531113bb JA |
189 | struct file *io_file_get_normal(struct io_kiocb *req, int fd); |
190 | struct file *io_file_get_fixed(struct io_kiocb *req, int fd, | |
191 | unsigned issue_flags); | |
cd40cae2 | 192 | |
cd40cae2 | 193 | bool io_is_uring_fops(struct file *file); |
99f15d8d JA |
194 | bool io_alloc_async_data(struct io_kiocb *req); |
195 | void io_req_task_work_add(struct io_kiocb *req); | |
f3b44f92 | 196 | void io_req_task_prio_work_add(struct io_kiocb *req); |
59915143 | 197 | void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags); |
f3b44f92 JA |
198 | void io_req_task_queue(struct io_kiocb *req); |
199 | void io_queue_iowq(struct io_kiocb *req, bool *dont_use); | |
59915143 JA |
200 | void io_req_task_complete(struct io_kiocb *req, bool *locked); |
201 | void io_req_task_queue_fail(struct io_kiocb *req, int ret); | |
329061d3 | 202 | void io_req_task_submit(struct io_kiocb *req, bool *locked); |
c9f06aa7 | 203 | void tctx_task_work(struct callback_head *cb); |
17437f31 JA |
204 | __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
205 | int io_uring_alloc_task_context(struct task_struct *task, | |
206 | struct io_ring_ctx *ctx); | |
207 | ||
329061d3 | 208 | int io_poll_issue(struct io_kiocb *req, bool *locked); |
17437f31 JA |
209 | int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); |
210 | int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); | |
f3b44f92 JA |
211 | void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node); |
212 | int io_req_prep_async(struct io_kiocb *req); | |
59915143 | 213 | |
c9f06aa7 JA |
214 | struct io_wq_work *io_wq_free_work(struct io_wq_work *work); |
215 | void io_wq_submit_work(struct io_wq_work *work); | |
216 | ||
59915143 JA |
217 | void io_free_req(struct io_kiocb *req); |
218 | void io_queue_next(struct io_kiocb *req); | |
219 | ||
329061d3 JA |
220 | bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, |
221 | bool cancel_all); | |
222 | ||
59915143 JA |
223 | #define io_for_each_link(pos, head) \ |
224 | for (pos = (head); pos; pos = pos->link) | |
531113bb | 225 | |
de23077e | 226 | #endif |