xref: /linux/io_uring/waitid.c (revision cf72cbb39da84b6f02f90c07f33b102fc10b16f0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for async notification of waitid
4  */
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
7 #include <linux/fs.h>
8 #include <linux/file.h>
9 #include <linux/compat.h>
10 #include <linux/io_uring.h>
11 
12 #include <uapi/linux/io_uring.h>
13 
14 #include "io_uring.h"
15 #include "cancel.h"
16 #include "waitid.h"
17 #include "../kernel/exit.h"
18 
19 static void io_waitid_cb(struct io_tw_req tw_req, io_tw_token_t tw);
20 
21 #define IO_WAITID_CANCEL_FLAG	BIT(31)
22 #define IO_WAITID_REF_MASK	GENMASK(30, 0)
23 
24 struct io_waitid {
25 	struct file *file;
26 	int which;
27 	pid_t upid;
28 	int options;
29 	atomic_t refs;
30 	struct wait_queue_head *head;
31 	struct siginfo __user *infop;
32 	struct waitid_info info;
33 };
34 
35 static void io_waitid_free(struct io_kiocb *req)
36 {
37 	struct io_waitid_async *iwa = req->async_data;
38 
39 	put_pid(iwa->wo.wo_pid);
40 	io_req_async_data_free(req);
41 }
42 
43 static bool io_waitid_compat_copy_si(struct io_waitid *iw, int signo)
44 {
45 	struct compat_siginfo __user *infop;
46 	bool ret;
47 
48 	infop = (struct compat_siginfo __user *) iw->infop;
49 
50 	if (!user_write_access_begin(infop, sizeof(*infop)))
51 		return false;
52 
53 	unsafe_put_user(signo, &infop->si_signo, Efault);
54 	unsafe_put_user(0, &infop->si_errno, Efault);
55 	unsafe_put_user(iw->info.cause, &infop->si_code, Efault);
56 	unsafe_put_user(iw->info.pid, &infop->si_pid, Efault);
57 	unsafe_put_user(iw->info.uid, &infop->si_uid, Efault);
58 	unsafe_put_user(iw->info.status, &infop->si_status, Efault);
59 	ret = true;
60 done:
61 	user_write_access_end();
62 	return ret;
63 Efault:
64 	ret = false;
65 	goto done;
66 }
67 
68 static bool io_waitid_copy_si(struct io_kiocb *req, int signo)
69 {
70 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
71 	bool ret;
72 
73 	if (!iw->infop)
74 		return true;
75 
76 	if (io_is_compat(req->ctx))
77 		return io_waitid_compat_copy_si(iw, signo);
78 
79 	if (!user_write_access_begin(iw->infop, sizeof(*iw->infop)))
80 		return false;
81 
82 	unsafe_put_user(signo, &iw->infop->si_signo, Efault);
83 	unsafe_put_user(0, &iw->infop->si_errno, Efault);
84 	unsafe_put_user(iw->info.cause, &iw->infop->si_code, Efault);
85 	unsafe_put_user(iw->info.pid, &iw->infop->si_pid, Efault);
86 	unsafe_put_user(iw->info.uid, &iw->infop->si_uid, Efault);
87 	unsafe_put_user(iw->info.status, &iw->infop->si_status, Efault);
88 	ret = true;
89 done:
90 	user_write_access_end();
91 	return ret;
92 Efault:
93 	ret = false;
94 	goto done;
95 }
96 
97 static int io_waitid_finish(struct io_kiocb *req, int ret)
98 {
99 	int signo = 0;
100 
101 	if (ret > 0) {
102 		signo = SIGCHLD;
103 		ret = 0;
104 	}
105 
106 	if (!io_waitid_copy_si(req, signo))
107 		ret = -EFAULT;
108 	io_waitid_free(req);
109 	return ret;
110 }
111 
112 static void io_waitid_remove_wq(struct io_kiocb *req)
113 {
114 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
115 	struct wait_queue_head *head;
116 
117 	head = smp_load_acquire(&iw->head);
118 	if (head) {
119 		struct io_waitid_async *iwa = req->async_data;
120 
121 		smp_store_release(&iw->head, NULL);
122 		spin_lock_irq(&head->lock);
123 		list_del_init(&iwa->wo.child_wait.entry);
124 		spin_unlock_irq(&head->lock);
125 	}
126 }
127 
128 static void io_waitid_complete(struct io_kiocb *req, int ret, bool copy_si)
129 {
130 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
131 
132 	/* anyone completing better be holding a reference */
133 	WARN_ON_ONCE(!(atomic_read(&iw->refs) & IO_WAITID_REF_MASK));
134 
135 	lockdep_assert_held(&req->ctx->uring_lock);
136 
137 	hlist_del_init(&req->hash_node);
138 	io_waitid_remove_wq(req);
139 
140 	if (copy_si)
141 		ret = io_waitid_finish(req, ret);
142 	else
143 		io_waitid_free(req);
144 	if (ret < 0)
145 		req_set_fail(req);
146 	io_req_set_res(req, ret, 0);
147 }
148 
149 static bool __io_waitid_cancel(struct io_kiocb *req, bool copy_si)
150 {
151 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
152 
153 	lockdep_assert_held(&req->ctx->uring_lock);
154 
155 	/*
156 	 * Mark us canceled regardless of ownership. This will prevent a
157 	 * potential retry from a spurious wakeup.
158 	 */
159 	atomic_or(IO_WAITID_CANCEL_FLAG, &iw->refs);
160 
161 	/* claim ownership */
162 	if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
163 		return false;
164 
165 	io_waitid_complete(req, -ECANCELED, copy_si);
166 	io_req_queue_tw_complete(req, -ECANCELED);
167 	return true;
168 }
169 
170 static bool io_waitid_cancel_cb(struct io_kiocb *req)
171 {
172 	return __io_waitid_cancel(req, true);
173 }
174 
175 static bool io_waitid_cancel_nocopy_cb(struct io_kiocb *req)
176 {
177 	return __io_waitid_cancel(req, false);
178 }
179 
180 int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
181 		     unsigned int issue_flags)
182 {
183 	return io_cancel_remove(ctx, cd, issue_flags, &ctx->waitid_list, io_waitid_cancel_cb);
184 }
185 
186 bool io_waitid_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
187 			  bool cancel_all)
188 {
189 	return io_cancel_remove_all(ctx, tctx, &ctx->waitid_list, cancel_all,
190 				       tctx ? io_waitid_cancel_cb : io_waitid_cancel_nocopy_cb);
191 }
192 
193 static inline bool io_waitid_drop_issue_ref(struct io_kiocb *req)
194 {
195 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
196 
197 	if (!atomic_sub_return(1, &iw->refs))
198 		return false;
199 
200 	io_waitid_remove_wq(req);
201 
202 	/*
203 	 * Wakeup triggered, racing with us. It was prevented from
204 	 * completing because of that, queue up the tw to do that.
205 	 */
206 	req->io_task_work.func = io_waitid_cb;
207 	io_req_task_work_add(req);
208 	return true;
209 }
210 
211 static void io_waitid_cb(struct io_tw_req tw_req, io_tw_token_t tw)
212 {
213 	struct io_kiocb *req = tw_req.req;
214 	struct io_waitid_async *iwa = req->async_data;
215 	struct io_ring_ctx *ctx = req->ctx;
216 	int ret;
217 
218 	io_tw_lock(ctx, tw);
219 	if (unlikely(tw.cancel)) {
220 		io_waitid_complete(req, -ECANCELED, false);
221 		io_req_task_complete(tw_req, tw);
222 		return;
223 	}
224 
225 	ret = __do_wait(&iwa->wo);
226 
227 	/*
228 	 * If we get -ERESTARTSYS here, we need to re-arm and check again
229 	 * to ensure we get another callback. If the retry works, then we can
230 	 * just remove ourselves from the waitqueue again and finish the
231 	 * request.
232 	 */
233 	if (unlikely(ret == -ERESTARTSYS)) {
234 		struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
235 
236 		/* Don't retry if cancel found it meanwhile */
237 		ret = -ECANCELED;
238 		if (!(atomic_read(&iw->refs) & IO_WAITID_CANCEL_FLAG)) {
239 			iw->head = &current->signal->wait_chldexit;
240 			add_wait_queue(iw->head, &iwa->wo.child_wait);
241 			ret = __do_wait(&iwa->wo);
242 			if (ret == -ERESTARTSYS) {
243 				/* retry armed, drop our ref */
244 				io_waitid_drop_issue_ref(req);
245 				return;
246 			}
247 			/* fall through to complete, will kill waitqueue */
248 		}
249 	}
250 
251 	io_waitid_complete(req, ret, true);
252 	io_req_task_complete(tw_req, tw);
253 }
254 
255 static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode,
256 			  int sync, void *key)
257 {
258 	struct wait_opts *wo = container_of(wait, struct wait_opts, child_wait);
259 	struct io_waitid_async *iwa = container_of(wo, struct io_waitid_async, wo);
260 	struct io_kiocb *req = iwa->req;
261 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
262 	struct task_struct *p = key;
263 
264 	if (!pid_child_should_wake(wo, p))
265 		return 0;
266 
267 	list_del_init(&wait->entry);
268 	smp_store_release(&iw->head, NULL);
269 
270 	/* cancel is in progress */
271 	if (atomic_fetch_inc(&iw->refs) & IO_WAITID_REF_MASK)
272 		return 1;
273 
274 	req->io_task_work.func = io_waitid_cb;
275 	__io_req_task_work_add(req, IOU_F_TWQ_IN_WAKE);
276 	return 1;
277 }
278 
279 int io_waitid_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
280 {
281 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
282 	struct io_waitid_async *iwa;
283 
284 	if (sqe->addr || sqe->buf_index || sqe->addr3 || sqe->waitid_flags)
285 		return -EINVAL;
286 
287 	iwa = io_uring_alloc_async_data(NULL, req);
288 	if (unlikely(!iwa))
289 		return -ENOMEM;
290 	iwa->req = req;
291 
292 	iw->which = READ_ONCE(sqe->len);
293 	iw->upid = READ_ONCE(sqe->fd);
294 	iw->options = READ_ONCE(sqe->file_index);
295 	iw->head = NULL;
296 	iw->infop = u64_to_user_ptr(READ_ONCE(sqe->addr2));
297 	memset(&iw->info, 0, sizeof(iw->info));
298 	return 0;
299 }
300 
301 int io_waitid(struct io_kiocb *req, unsigned int issue_flags)
302 {
303 	struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid);
304 	struct io_waitid_async *iwa = req->async_data;
305 	struct io_ring_ctx *ctx = req->ctx;
306 	int ret;
307 
308 	ret = kernel_waitid_prepare(&iwa->wo, iw->which, iw->upid, &iw->info,
309 					iw->options, NULL);
310 	if (ret)
311 		goto done;
312 
313 	/*
314 	 * Mark the request as busy upfront, in case we're racing with the
315 	 * wakeup. If we are, then we'll notice when we drop this initial
316 	 * reference again after arming.
317 	 */
318 	atomic_set(&iw->refs, 1);
319 
320 	/*
321 	 * Cancel must hold the ctx lock, so there's no risk of cancelation
322 	 * finding us until a) we remain on the list, and b) the lock is
323 	 * dropped. We only need to worry about racing with the wakeup
324 	 * callback.
325 	 */
326 	io_ring_submit_lock(ctx, issue_flags);
327 
328 	/*
329 	 * iw->head is valid under the ring lock, and as long as the request
330 	 * is on the waitid_list where cancelations may find it.
331 	 */
332 	iw->head = &current->signal->wait_chldexit;
333 	hlist_add_head(&req->hash_node, &ctx->waitid_list);
334 
335 	init_waitqueue_func_entry(&iwa->wo.child_wait, io_waitid_wait);
336 	iwa->wo.child_wait.private = req->tctx->task;
337 	add_wait_queue(iw->head, &iwa->wo.child_wait);
338 
339 	ret = __do_wait(&iwa->wo);
340 	if (ret == -ERESTARTSYS) {
341 		/*
342 		 * Nobody else grabbed a reference, it'll complete when we get
343 		 * a waitqueue callback, or if someone cancels it.
344 		 */
345 		if (!io_waitid_drop_issue_ref(req)) {
346 			io_ring_submit_unlock(ctx, issue_flags);
347 			return IOU_ISSUE_SKIP_COMPLETE;
348 		}
349 
350 		/*
351 		 * Wakeup triggered, racing with us. It was prevented from
352 		 * completing because of that, queue up the tw to do that.
353 		 */
354 		io_ring_submit_unlock(ctx, issue_flags);
355 		return IOU_ISSUE_SKIP_COMPLETE;
356 	}
357 
358 	hlist_del_init(&req->hash_node);
359 	io_waitid_remove_wq(req);
360 	ret = io_waitid_finish(req, ret);
361 
362 	io_ring_submit_unlock(ctx, issue_flags);
363 done:
364 	if (ret < 0)
365 		req_set_fail(req);
366 	io_req_set_res(req, ret, 0);
367 	return IOU_COMPLETE;
368 }
369