| 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_kiocb *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(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(ctx: 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_complete(struct io_kiocb *req, int ret) |
| 113 | { |
| 114 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 115 | |
| 116 | /* anyone completing better be holding a reference */ |
| 117 | WARN_ON_ONCE(!(atomic_read(&iw->refs) & IO_WAITID_REF_MASK)); |
| 118 | |
| 119 | lockdep_assert_held(&req->ctx->uring_lock); |
| 120 | |
| 121 | hlist_del_init(n: &req->hash_node); |
| 122 | |
| 123 | ret = io_waitid_finish(req, ret); |
| 124 | if (ret < 0) |
| 125 | req_set_fail(req); |
| 126 | io_req_set_res(req, res: ret, cflags: 0); |
| 127 | } |
| 128 | |
| 129 | static bool __io_waitid_cancel(struct io_kiocb *req) |
| 130 | { |
| 131 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 132 | struct io_waitid_async *iwa = req->async_data; |
| 133 | |
| 134 | /* |
| 135 | * Mark us canceled regardless of ownership. This will prevent a |
| 136 | * potential retry from a spurious wakeup. |
| 137 | */ |
| 138 | atomic_or(IO_WAITID_CANCEL_FLAG, v: &iw->refs); |
| 139 | |
| 140 | /* claim ownership */ |
| 141 | if (atomic_fetch_inc(v: &iw->refs) & IO_WAITID_REF_MASK) |
| 142 | return false; |
| 143 | |
| 144 | spin_lock_irq(lock: &iw->head->lock); |
| 145 | list_del_init(entry: &iwa->wo.child_wait.entry); |
| 146 | spin_unlock_irq(lock: &iw->head->lock); |
| 147 | io_waitid_complete(req, ret: -ECANCELED); |
| 148 | io_req_queue_tw_complete(req, res: -ECANCELED); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, |
| 153 | unsigned int issue_flags) |
| 154 | { |
| 155 | return io_cancel_remove(ctx, cd, issue_flags, list: &ctx->waitid_list, cancel: __io_waitid_cancel); |
| 156 | } |
| 157 | |
| 158 | bool io_waitid_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx, |
| 159 | bool cancel_all) |
| 160 | { |
| 161 | return io_cancel_remove_all(ctx, tctx, list: &ctx->waitid_list, cancel_all, cancel: __io_waitid_cancel); |
| 162 | } |
| 163 | |
| 164 | static inline bool io_waitid_drop_issue_ref(struct io_kiocb *req) |
| 165 | { |
| 166 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 167 | struct io_waitid_async *iwa = req->async_data; |
| 168 | |
| 169 | if (!atomic_sub_return(i: 1, v: &iw->refs)) |
| 170 | return false; |
| 171 | |
| 172 | /* |
| 173 | * Wakeup triggered, racing with us. It was prevented from |
| 174 | * completing because of that, queue up the tw to do that. |
| 175 | */ |
| 176 | req->io_task_work.func = io_waitid_cb; |
| 177 | io_req_task_work_add(req); |
| 178 | remove_wait_queue(wq_head: iw->head, wq_entry: &iwa->wo.child_wait); |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | static void io_waitid_cb(struct io_kiocb *req, io_tw_token_t tw) |
| 183 | { |
| 184 | struct io_waitid_async *iwa = req->async_data; |
| 185 | struct io_ring_ctx *ctx = req->ctx; |
| 186 | int ret; |
| 187 | |
| 188 | io_tw_lock(ctx, tw); |
| 189 | |
| 190 | ret = __do_wait(wo: &iwa->wo); |
| 191 | |
| 192 | /* |
| 193 | * If we get -ERESTARTSYS here, we need to re-arm and check again |
| 194 | * to ensure we get another callback. If the retry works, then we can |
| 195 | * just remove ourselves from the waitqueue again and finish the |
| 196 | * request. |
| 197 | */ |
| 198 | if (unlikely(ret == -ERESTARTSYS)) { |
| 199 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 200 | |
| 201 | /* Don't retry if cancel found it meanwhile */ |
| 202 | ret = -ECANCELED; |
| 203 | if (!(atomic_read(v: &iw->refs) & IO_WAITID_CANCEL_FLAG)) { |
| 204 | iw->head = ¤t->signal->wait_chldexit; |
| 205 | add_wait_queue(wq_head: iw->head, wq_entry: &iwa->wo.child_wait); |
| 206 | ret = __do_wait(wo: &iwa->wo); |
| 207 | if (ret == -ERESTARTSYS) { |
| 208 | /* retry armed, drop our ref */ |
| 209 | io_waitid_drop_issue_ref(req); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | remove_wait_queue(wq_head: iw->head, wq_entry: &iwa->wo.child_wait); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | io_waitid_complete(req, ret); |
| 218 | io_req_task_complete(req, tw); |
| 219 | } |
| 220 | |
| 221 | static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode, |
| 222 | int sync, void *key) |
| 223 | { |
| 224 | struct wait_opts *wo = container_of(wait, struct wait_opts, child_wait); |
| 225 | struct io_waitid_async *iwa = container_of(wo, struct io_waitid_async, wo); |
| 226 | struct io_kiocb *req = iwa->req; |
| 227 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 228 | struct task_struct *p = key; |
| 229 | |
| 230 | if (!pid_child_should_wake(wo, p)) |
| 231 | return 0; |
| 232 | |
| 233 | list_del_init(entry: &wait->entry); |
| 234 | |
| 235 | /* cancel is in progress */ |
| 236 | if (atomic_fetch_inc(v: &iw->refs) & IO_WAITID_REF_MASK) |
| 237 | return 1; |
| 238 | |
| 239 | req->io_task_work.func = io_waitid_cb; |
| 240 | io_req_task_work_add(req); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int io_waitid_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 245 | { |
| 246 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 247 | struct io_waitid_async *iwa; |
| 248 | |
| 249 | if (sqe->addr || sqe->buf_index || sqe->addr3 || sqe->waitid_flags) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | iwa = io_uring_alloc_async_data(NULL, req); |
| 253 | if (!unlikely(iwa)) |
| 254 | return -ENOMEM; |
| 255 | iwa->req = req; |
| 256 | |
| 257 | iw->which = READ_ONCE(sqe->len); |
| 258 | iw->upid = READ_ONCE(sqe->fd); |
| 259 | iw->options = READ_ONCE(sqe->file_index); |
| 260 | iw->infop = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | int io_waitid(struct io_kiocb *req, unsigned int issue_flags) |
| 265 | { |
| 266 | struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); |
| 267 | struct io_waitid_async *iwa = req->async_data; |
| 268 | struct io_ring_ctx *ctx = req->ctx; |
| 269 | int ret; |
| 270 | |
| 271 | ret = kernel_waitid_prepare(wo: &iwa->wo, which: iw->which, upid: iw->upid, infop: &iw->info, |
| 272 | options: iw->options, NULL); |
| 273 | if (ret) |
| 274 | goto done; |
| 275 | |
| 276 | /* |
| 277 | * Mark the request as busy upfront, in case we're racing with the |
| 278 | * wakeup. If we are, then we'll notice when we drop this initial |
| 279 | * reference again after arming. |
| 280 | */ |
| 281 | atomic_set(v: &iw->refs, i: 1); |
| 282 | |
| 283 | /* |
| 284 | * Cancel must hold the ctx lock, so there's no risk of cancelation |
| 285 | * finding us until a) we remain on the list, and b) the lock is |
| 286 | * dropped. We only need to worry about racing with the wakeup |
| 287 | * callback. |
| 288 | */ |
| 289 | io_ring_submit_lock(ctx, issue_flags); |
| 290 | hlist_add_head(n: &req->hash_node, h: &ctx->waitid_list); |
| 291 | |
| 292 | init_waitqueue_func_entry(wq_entry: &iwa->wo.child_wait, func: io_waitid_wait); |
| 293 | iwa->wo.child_wait.private = req->tctx->task; |
| 294 | iw->head = ¤t->signal->wait_chldexit; |
| 295 | add_wait_queue(wq_head: iw->head, wq_entry: &iwa->wo.child_wait); |
| 296 | |
| 297 | ret = __do_wait(wo: &iwa->wo); |
| 298 | if (ret == -ERESTARTSYS) { |
| 299 | /* |
| 300 | * Nobody else grabbed a reference, it'll complete when we get |
| 301 | * a waitqueue callback, or if someone cancels it. |
| 302 | */ |
| 303 | if (!io_waitid_drop_issue_ref(req)) { |
| 304 | io_ring_submit_unlock(ctx, issue_flags); |
| 305 | return IOU_ISSUE_SKIP_COMPLETE; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Wakeup triggered, racing with us. It was prevented from |
| 310 | * completing because of that, queue up the tw to do that. |
| 311 | */ |
| 312 | io_ring_submit_unlock(ctx, issue_flags); |
| 313 | return IOU_ISSUE_SKIP_COMPLETE; |
| 314 | } |
| 315 | |
| 316 | hlist_del_init(n: &req->hash_node); |
| 317 | remove_wait_queue(wq_head: iw->head, wq_entry: &iwa->wo.child_wait); |
| 318 | ret = io_waitid_finish(req, ret); |
| 319 | |
| 320 | io_ring_submit_unlock(ctx, issue_flags); |
| 321 | done: |
| 322 | if (ret < 0) |
| 323 | req_set_fail(req); |
| 324 | io_req_set_res(req, res: ret, cflags: 0); |
| 325 | return IOU_COMPLETE; |
| 326 | } |
| 327 | |