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