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