1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/fs.h>
5 #include <linux/file.h>
6 #include <linux/io_uring.h>
7 
8 #include <uapi/linux/io_uring.h>
9 
10 #include "../kernel/futex/futex.h"
11 #include "io_uring.h"
12 #include "alloc_cache.h"
13 #include "futex.h"
14 
15 struct io_futex {
16 	struct file	*file;
17 	union {
18 		u32 __user			*uaddr;
19 		struct futex_waitv __user	*uwaitv;
20 	};
21 	unsigned long	futex_val;
22 	unsigned long	futex_mask;
23 	unsigned long	futexv_owned;
24 	u32		futex_flags;
25 	unsigned int	futex_nr;
26 	bool		futexv_unqueued;
27 };
28 
29 struct io_futex_data {
30 	struct futex_q	q;
31 	struct io_kiocb	*req;
32 };
33 
34 #define IO_FUTEX_ALLOC_CACHE_MAX	32
35 
io_futex_cache_init(struct io_ring_ctx * ctx)36 bool io_futex_cache_init(struct io_ring_ctx *ctx)
37 {
38 	return io_alloc_cache_init(&ctx->futex_cache, IO_FUTEX_ALLOC_CACHE_MAX,
39 				sizeof(struct io_futex_data), 0);
40 }
41 
io_futex_cache_free(struct io_ring_ctx * ctx)42 void io_futex_cache_free(struct io_ring_ctx *ctx)
43 {
44 	io_alloc_cache_free(&ctx->futex_cache, kfree);
45 }
46 
__io_futex_complete(struct io_kiocb * req,io_tw_token_t tw)47 static void __io_futex_complete(struct io_kiocb *req, io_tw_token_t tw)
48 {
49 	req->async_data = NULL;
50 	hlist_del_init(&req->hash_node);
51 	io_req_task_complete(req, tw);
52 }
53 
io_futex_complete(struct io_kiocb * req,io_tw_token_t tw)54 static void io_futex_complete(struct io_kiocb *req, io_tw_token_t tw)
55 {
56 	struct io_ring_ctx *ctx = req->ctx;
57 
58 	io_tw_lock(ctx, tw);
59 	io_cache_free(&ctx->futex_cache, req->async_data);
60 	__io_futex_complete(req, tw);
61 }
62 
io_futexv_complete(struct io_kiocb * req,io_tw_token_t tw)63 static void io_futexv_complete(struct io_kiocb *req, io_tw_token_t tw)
64 {
65 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
66 	struct futex_vector *futexv = req->async_data;
67 
68 	io_tw_lock(req->ctx, tw);
69 
70 	if (!iof->futexv_unqueued) {
71 		int res;
72 
73 		res = futex_unqueue_multiple(futexv, iof->futex_nr);
74 		if (res != -1)
75 			io_req_set_res(req, res, 0);
76 	}
77 
78 	kfree(req->async_data);
79 	req->flags &= ~REQ_F_ASYNC_DATA;
80 	__io_futex_complete(req, tw);
81 }
82 
io_futexv_claim(struct io_futex * iof)83 static bool io_futexv_claim(struct io_futex *iof)
84 {
85 	if (test_bit(0, &iof->futexv_owned) ||
86 	    test_and_set_bit_lock(0, &iof->futexv_owned))
87 		return false;
88 	return true;
89 }
90 
__io_futex_cancel(struct io_kiocb * req)91 static bool __io_futex_cancel(struct io_kiocb *req)
92 {
93 	/* futex wake already done or in progress */
94 	if (req->opcode == IORING_OP_FUTEX_WAIT) {
95 		struct io_futex_data *ifd = req->async_data;
96 
97 		if (!futex_unqueue(&ifd->q))
98 			return false;
99 		req->io_task_work.func = io_futex_complete;
100 	} else {
101 		struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
102 
103 		if (!io_futexv_claim(iof))
104 			return false;
105 		req->io_task_work.func = io_futexv_complete;
106 	}
107 
108 	hlist_del_init(&req->hash_node);
109 	io_req_set_res(req, -ECANCELED, 0);
110 	io_req_task_work_add(req);
111 	return true;
112 }
113 
io_futex_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd,unsigned int issue_flags)114 int io_futex_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
115 		    unsigned int issue_flags)
116 {
117 	return io_cancel_remove(ctx, cd, issue_flags, &ctx->futex_list, __io_futex_cancel);
118 }
119 
io_futex_remove_all(struct io_ring_ctx * ctx,struct io_uring_task * tctx,bool cancel_all)120 bool io_futex_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
121 			 bool cancel_all)
122 {
123 	return io_cancel_remove_all(ctx, tctx, &ctx->futex_list, cancel_all, __io_futex_cancel);
124 }
125 
io_futex_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)126 int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
127 {
128 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
129 	u32 flags;
130 
131 	if (unlikely(sqe->len || sqe->futex_flags || sqe->buf_index ||
132 		     sqe->file_index))
133 		return -EINVAL;
134 
135 	iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
136 	iof->futex_val = READ_ONCE(sqe->addr2);
137 	iof->futex_mask = READ_ONCE(sqe->addr3);
138 	flags = READ_ONCE(sqe->fd);
139 
140 	if (flags & ~FUTEX2_VALID_MASK)
141 		return -EINVAL;
142 
143 	iof->futex_flags = futex2_to_flags(flags);
144 	if (!futex_flags_valid(iof->futex_flags))
145 		return -EINVAL;
146 
147 	if (!futex_validate_input(iof->futex_flags, iof->futex_val) ||
148 	    !futex_validate_input(iof->futex_flags, iof->futex_mask))
149 		return -EINVAL;
150 
151 	return 0;
152 }
153 
io_futex_wakev_fn(struct wake_q_head * wake_q,struct futex_q * q)154 static void io_futex_wakev_fn(struct wake_q_head *wake_q, struct futex_q *q)
155 {
156 	struct io_kiocb *req = q->wake_data;
157 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
158 
159 	if (!io_futexv_claim(iof))
160 		return;
161 	if (unlikely(!__futex_wake_mark(q)))
162 		return;
163 
164 	io_req_set_res(req, 0, 0);
165 	req->io_task_work.func = io_futexv_complete;
166 	io_req_task_work_add(req);
167 }
168 
io_futexv_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)169 int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
170 {
171 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
172 	struct futex_vector *futexv;
173 	int ret;
174 
175 	/* No flags or mask supported for waitv */
176 	if (unlikely(sqe->fd || sqe->buf_index || sqe->file_index ||
177 		     sqe->addr2 || sqe->futex_flags || sqe->addr3))
178 		return -EINVAL;
179 
180 	iof->uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
181 	iof->futex_nr = READ_ONCE(sqe->len);
182 	if (!iof->futex_nr || iof->futex_nr > FUTEX_WAITV_MAX)
183 		return -EINVAL;
184 
185 	futexv = kcalloc(iof->futex_nr, sizeof(*futexv), GFP_KERNEL);
186 	if (!futexv)
187 		return -ENOMEM;
188 
189 	ret = futex_parse_waitv(futexv, iof->uwaitv, iof->futex_nr,
190 				io_futex_wakev_fn, req);
191 	if (ret) {
192 		kfree(futexv);
193 		return ret;
194 	}
195 
196 	iof->futexv_owned = 0;
197 	iof->futexv_unqueued = 0;
198 	req->flags |= REQ_F_ASYNC_DATA;
199 	req->async_data = futexv;
200 	return 0;
201 }
202 
io_futex_wake_fn(struct wake_q_head * wake_q,struct futex_q * q)203 static void io_futex_wake_fn(struct wake_q_head *wake_q, struct futex_q *q)
204 {
205 	struct io_futex_data *ifd = container_of(q, struct io_futex_data, q);
206 	struct io_kiocb *req = ifd->req;
207 
208 	if (unlikely(!__futex_wake_mark(q)))
209 		return;
210 
211 	io_req_set_res(req, 0, 0);
212 	req->io_task_work.func = io_futex_complete;
213 	io_req_task_work_add(req);
214 }
215 
io_futexv_wait(struct io_kiocb * req,unsigned int issue_flags)216 int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags)
217 {
218 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
219 	struct futex_vector *futexv = req->async_data;
220 	struct io_ring_ctx *ctx = req->ctx;
221 	int ret, woken = -1;
222 
223 	io_ring_submit_lock(ctx, issue_flags);
224 
225 	ret = futex_wait_multiple_setup(futexv, iof->futex_nr, &woken);
226 
227 	/*
228 	 * Error case, ret is < 0. Mark the request as failed.
229 	 */
230 	if (unlikely(ret < 0)) {
231 		io_ring_submit_unlock(ctx, issue_flags);
232 		req_set_fail(req);
233 		io_req_set_res(req, ret, 0);
234 		kfree(futexv);
235 		req->async_data = NULL;
236 		req->flags &= ~REQ_F_ASYNC_DATA;
237 		return IOU_OK;
238 	}
239 
240 	/*
241 	 * 0 return means that we successfully setup the waiters, and that
242 	 * nobody triggered a wakeup while we were doing so. If the wakeup
243 	 * happened post setup, the task_work will be run post this issue and
244 	 * under the submission lock. 1 means We got woken while setting up,
245 	 * let that side do the completion. Note that
246 	 * futex_wait_multiple_setup() will have unqueued all the futexes in
247 	 * this case. Mark us as having done that already, since this is
248 	 * different from normal wakeup.
249 	 */
250 	if (!ret) {
251 		/*
252 		 * If futex_wait_multiple_setup() returns 0 for a
253 		 * successful setup, then the task state will not be
254 		 * runnable. This is fine for the sync syscall, as
255 		 * it'll be blocking unless we already got one of the
256 		 * futexes woken, but it obviously won't work for an
257 		 * async invocation. Mark us runnable again.
258 		 */
259 		__set_current_state(TASK_RUNNING);
260 		hlist_add_head(&req->hash_node, &ctx->futex_list);
261 	} else {
262 		iof->futexv_unqueued = 1;
263 		if (woken != -1)
264 			io_req_set_res(req, woken, 0);
265 	}
266 
267 	io_ring_submit_unlock(ctx, issue_flags);
268 	return IOU_ISSUE_SKIP_COMPLETE;
269 }
270 
io_futex_wait(struct io_kiocb * req,unsigned int issue_flags)271 int io_futex_wait(struct io_kiocb *req, unsigned int issue_flags)
272 {
273 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
274 	struct io_ring_ctx *ctx = req->ctx;
275 	struct io_futex_data *ifd = NULL;
276 	struct futex_hash_bucket *hb;
277 	int ret;
278 
279 	if (!iof->futex_mask) {
280 		ret = -EINVAL;
281 		goto done;
282 	}
283 
284 	io_ring_submit_lock(ctx, issue_flags);
285 	ifd = io_cache_alloc(&ctx->futex_cache, GFP_NOWAIT);
286 	if (!ifd) {
287 		ret = -ENOMEM;
288 		goto done_unlock;
289 	}
290 
291 	req->async_data = ifd;
292 	ifd->q = futex_q_init;
293 	ifd->q.bitset = iof->futex_mask;
294 	ifd->q.wake = io_futex_wake_fn;
295 	ifd->req = req;
296 
297 	ret = futex_wait_setup(iof->uaddr, iof->futex_val, iof->futex_flags,
298 			       &ifd->q, &hb);
299 	if (!ret) {
300 		hlist_add_head(&req->hash_node, &ctx->futex_list);
301 		io_ring_submit_unlock(ctx, issue_flags);
302 
303 		futex_queue(&ifd->q, hb, NULL);
304 		return IOU_ISSUE_SKIP_COMPLETE;
305 	}
306 
307 done_unlock:
308 	io_ring_submit_unlock(ctx, issue_flags);
309 done:
310 	if (ret < 0)
311 		req_set_fail(req);
312 	io_req_set_res(req, ret, 0);
313 	kfree(ifd);
314 	return IOU_OK;
315 }
316 
io_futex_wake(struct io_kiocb * req,unsigned int issue_flags)317 int io_futex_wake(struct io_kiocb *req, unsigned int issue_flags)
318 {
319 	struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex);
320 	int ret;
321 
322 	/*
323 	 * Strict flags - ensure that waking 0 futexes yields a 0 result.
324 	 * See commit 43adf8449510 ("futex: FLAGS_STRICT") for details.
325 	 */
326 	ret = futex_wake(iof->uaddr, FLAGS_STRICT | iof->futex_flags,
327 			 iof->futex_val, iof->futex_mask);
328 	if (ret < 0)
329 		req_set_fail(req);
330 	io_req_set_res(req, ret, 0);
331 	return IOU_OK;
332 }
333