| 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/blk-mq.h> |
| 7 | #include <linux/mm.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/fsnotify.h> |
| 10 | #include <linux/poll.h> |
| 11 | #include <linux/nospec.h> |
| 12 | #include <linux/compat.h> |
| 13 | #include <linux/io_uring/cmd.h> |
| 14 | #include <linux/indirect_call_wrapper.h> |
| 15 | |
| 16 | #include <uapi/linux/io_uring.h> |
| 17 | |
| 18 | #include "io_uring.h" |
| 19 | #include "opdef.h" |
| 20 | #include "kbuf.h" |
| 21 | #include "alloc_cache.h" |
| 22 | #include "rsrc.h" |
| 23 | #include "poll.h" |
| 24 | #include "rw.h" |
| 25 | |
| 26 | static void io_complete_rw(struct kiocb *kiocb, long res); |
| 27 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res); |
| 28 | |
| 29 | struct io_rw { |
| 30 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 31 | struct kiocb kiocb; |
| 32 | u64 addr; |
| 33 | u32 len; |
| 34 | rwf_t flags; |
| 35 | }; |
| 36 | |
| 37 | static bool io_file_supports_nowait(struct io_kiocb *req, __poll_t mask) |
| 38 | { |
| 39 | /* If FMODE_NOWAIT is set for a file, we're golden */ |
| 40 | if (req->flags & REQ_F_SUPPORT_NOWAIT) |
| 41 | return true; |
| 42 | /* No FMODE_NOWAIT, if we can poll, check the status */ |
| 43 | if (io_file_can_poll(req)) { |
| 44 | struct poll_table_struct pt = { ._key = mask }; |
| 45 | |
| 46 | return vfs_poll(req->file, &pt) & mask; |
| 47 | } |
| 48 | /* No FMODE_NOWAIT support, and file isn't pollable. Tough luck. */ |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | static int io_iov_compat_buffer_select_prep(struct io_rw *rw) |
| 53 | { |
| 54 | struct compat_iovec __user *uiov = u64_to_user_ptr(rw->addr); |
| 55 | struct compat_iovec iov; |
| 56 | |
| 57 | if (copy_from_user(&iov, uiov, sizeof(iov))) |
| 58 | return -EFAULT; |
| 59 | rw->len = iov.iov_len; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int io_iov_buffer_select_prep(struct io_kiocb *req) |
| 64 | { |
| 65 | struct iovec __user *uiov; |
| 66 | struct iovec iov; |
| 67 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 68 | |
| 69 | if (rw->len != 1) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | if (io_is_compat(req->ctx)) |
| 73 | return io_iov_compat_buffer_select_prep(rw); |
| 74 | |
| 75 | uiov = u64_to_user_ptr(rw->addr); |
| 76 | if (copy_from_user(&iov, uiov, sizeof(*uiov))) |
| 77 | return -EFAULT; |
| 78 | rw->len = iov.iov_len; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int io_import_vec(int ddir, struct io_kiocb *req, |
| 83 | struct io_async_rw *io, |
| 84 | const struct iovec __user *uvec, |
| 85 | size_t uvec_segs) |
| 86 | { |
| 87 | int ret, nr_segs; |
| 88 | struct iovec *iov; |
| 89 | |
| 90 | if (io->vec.iovec) { |
| 91 | nr_segs = io->vec.nr; |
| 92 | iov = io->vec.iovec; |
| 93 | } else { |
| 94 | nr_segs = 1; |
| 95 | iov = &io->fast_iov; |
| 96 | } |
| 97 | |
| 98 | ret = __import_iovec(ddir, uvec, uvec_segs, nr_segs, &iov, &io->iter, |
| 99 | io_is_compat(req->ctx)); |
| 100 | if (unlikely(ret < 0)) |
| 101 | return ret; |
| 102 | if (iov) { |
| 103 | req->flags |= REQ_F_NEED_CLEANUP; |
| 104 | io_vec_reset_iovec(&io->vec, iov, io->iter.nr_segs); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int __io_import_rw_buffer(int ddir, struct io_kiocb *req, |
| 110 | struct io_async_rw *io, |
| 111 | unsigned int issue_flags) |
| 112 | { |
| 113 | const struct io_issue_def *def = &io_issue_defs[req->opcode]; |
| 114 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 115 | void __user *buf = u64_to_user_ptr(rw->addr); |
| 116 | size_t sqe_len = rw->len; |
| 117 | |
| 118 | if (def->vectored && !(req->flags & REQ_F_BUFFER_SELECT)) |
| 119 | return io_import_vec(ddir, req, io, buf, sqe_len); |
| 120 | |
| 121 | if (io_do_buffer_select(req)) { |
| 122 | buf = io_buffer_select(req, &sqe_len, io->buf_group, issue_flags); |
| 123 | if (!buf) |
| 124 | return -ENOBUFS; |
| 125 | rw->addr = (unsigned long) buf; |
| 126 | rw->len = sqe_len; |
| 127 | } |
| 128 | return import_ubuf(ddir, buf, sqe_len, &io->iter); |
| 129 | } |
| 130 | |
| 131 | static inline int io_import_rw_buffer(int rw, struct io_kiocb *req, |
| 132 | struct io_async_rw *io, |
| 133 | unsigned int issue_flags) |
| 134 | { |
| 135 | int ret; |
| 136 | |
| 137 | ret = __io_import_rw_buffer(rw, req, io, issue_flags); |
| 138 | if (unlikely(ret < 0)) |
| 139 | return ret; |
| 140 | |
| 141 | iov_iter_save_state(&io->iter, &io->iter_state); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void io_rw_recycle(struct io_kiocb *req, unsigned int issue_flags) |
| 146 | { |
| 147 | struct io_async_rw *rw = req->async_data; |
| 148 | |
| 149 | if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) |
| 150 | return; |
| 151 | |
| 152 | io_alloc_cache_vec_kasan(&rw->vec); |
| 153 | if (rw->vec.nr > IO_VEC_CACHE_SOFT_CAP) |
| 154 | io_vec_free(&rw->vec); |
| 155 | |
| 156 | if (io_alloc_cache_put(&req->ctx->rw_cache, rw)) { |
| 157 | req->async_data = NULL; |
| 158 | req->flags &= ~REQ_F_ASYNC_DATA; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void io_req_rw_cleanup(struct io_kiocb *req, unsigned int issue_flags) |
| 163 | { |
| 164 | /* |
| 165 | * Disable quick recycling for anything that's gone through io-wq. |
| 166 | * In theory, this should be fine to cleanup. However, some read or |
| 167 | * write iter handling touches the iovec AFTER having called into the |
| 168 | * handler, eg to reexpand or revert. This means we can have: |
| 169 | * |
| 170 | * task io-wq |
| 171 | * issue |
| 172 | * punt to io-wq |
| 173 | * issue |
| 174 | * blkdev_write_iter() |
| 175 | * ->ki_complete() |
| 176 | * io_complete_rw() |
| 177 | * queue tw complete |
| 178 | * run tw |
| 179 | * req_rw_cleanup |
| 180 | * iov_iter_count() <- look at iov_iter again |
| 181 | * |
| 182 | * which can lead to a UAF. This is only possible for io-wq offload |
| 183 | * as the cleanup can run in parallel. As io-wq is not the fast path, |
| 184 | * just leave cleanup to the end. |
| 185 | * |
| 186 | * This is really a bug in the core code that does this, any issue |
| 187 | * path should assume that a successful (or -EIOCBQUEUED) return can |
| 188 | * mean that the underlying data can be gone at any time. But that |
| 189 | * should be fixed seperately, and then this check could be killed. |
| 190 | */ |
| 191 | if (!(req->flags & (REQ_F_REISSUE | REQ_F_REFCOUNT))) { |
| 192 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 193 | io_rw_recycle(req, issue_flags); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static int io_rw_alloc_async(struct io_kiocb *req) |
| 198 | { |
| 199 | struct io_ring_ctx *ctx = req->ctx; |
| 200 | struct io_async_rw *rw; |
| 201 | |
| 202 | rw = io_uring_alloc_async_data(&ctx->rw_cache, req); |
| 203 | if (!rw) |
| 204 | return -ENOMEM; |
| 205 | if (rw->vec.iovec) |
| 206 | req->flags |= REQ_F_NEED_CLEANUP; |
| 207 | rw->bytes_done = 0; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static inline void io_meta_save_state(struct io_async_rw *io) |
| 212 | { |
| 213 | io->meta_state.seed = io->meta.seed; |
| 214 | iov_iter_save_state(&io->meta.iter, &io->meta_state.iter_meta); |
| 215 | } |
| 216 | |
| 217 | static inline void io_meta_restore(struct io_async_rw *io, struct kiocb *kiocb) |
| 218 | { |
| 219 | if (kiocb->ki_flags & IOCB_HAS_METADATA) { |
| 220 | io->meta.seed = io->meta_state.seed; |
| 221 | iov_iter_restore(&io->meta.iter, &io->meta_state.iter_meta); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static int io_prep_rw_pi(struct io_kiocb *req, struct io_rw *rw, int ddir, |
| 226 | u64 attr_ptr, u64 attr_type_mask) |
| 227 | { |
| 228 | struct io_uring_attr_pi pi_attr; |
| 229 | struct io_async_rw *io; |
| 230 | int ret; |
| 231 | |
| 232 | if (copy_from_user(&pi_attr, u64_to_user_ptr(attr_ptr), |
| 233 | sizeof(pi_attr))) |
| 234 | return -EFAULT; |
| 235 | |
| 236 | if (pi_attr.rsvd) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | io = req->async_data; |
| 240 | io->meta.flags = pi_attr.flags; |
| 241 | io->meta.app_tag = pi_attr.app_tag; |
| 242 | io->meta.seed = pi_attr.seed; |
| 243 | ret = import_ubuf(ddir, u64_to_user_ptr(pi_attr.addr), |
| 244 | pi_attr.len, &io->meta.iter); |
| 245 | if (unlikely(ret < 0)) |
| 246 | return ret; |
| 247 | req->flags |= REQ_F_HAS_METADATA; |
| 248 | io_meta_save_state(io); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static int __io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 253 | int ddir) |
| 254 | { |
| 255 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 256 | struct io_async_rw *io; |
| 257 | unsigned ioprio; |
| 258 | u64 attr_type_mask; |
| 259 | int ret; |
| 260 | |
| 261 | if (io_rw_alloc_async(req)) |
| 262 | return -ENOMEM; |
| 263 | io = req->async_data; |
| 264 | |
| 265 | rw->kiocb.ki_pos = READ_ONCE(sqe->off); |
| 266 | /* used for fixed read/write too - just read unconditionally */ |
| 267 | req->buf_index = READ_ONCE(sqe->buf_index); |
| 268 | io->buf_group = req->buf_index; |
| 269 | |
| 270 | ioprio = READ_ONCE(sqe->ioprio); |
| 271 | if (ioprio) { |
| 272 | ret = ioprio_check_cap(ioprio); |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | |
| 276 | rw->kiocb.ki_ioprio = ioprio; |
| 277 | } else { |
| 278 | rw->kiocb.ki_ioprio = get_current_ioprio(); |
| 279 | } |
| 280 | rw->kiocb.dio_complete = NULL; |
| 281 | rw->kiocb.ki_flags = 0; |
| 282 | rw->kiocb.ki_write_stream = READ_ONCE(sqe->write_stream); |
| 283 | |
| 284 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 285 | rw->kiocb.ki_complete = io_complete_rw_iopoll; |
| 286 | else |
| 287 | rw->kiocb.ki_complete = io_complete_rw; |
| 288 | |
| 289 | rw->addr = READ_ONCE(sqe->addr); |
| 290 | rw->len = READ_ONCE(sqe->len); |
| 291 | rw->flags = READ_ONCE(sqe->rw_flags); |
| 292 | |
| 293 | attr_type_mask = READ_ONCE(sqe->attr_type_mask); |
| 294 | if (attr_type_mask) { |
| 295 | u64 attr_ptr; |
| 296 | |
| 297 | /* only PI attribute is supported currently */ |
| 298 | if (attr_type_mask != IORING_RW_ATTR_FLAG_PI) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | attr_ptr = READ_ONCE(sqe->attr_ptr); |
| 302 | return io_prep_rw_pi(req, rw, ddir, attr_ptr, attr_type_mask); |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int io_rw_do_import(struct io_kiocb *req, int ddir) |
| 308 | { |
| 309 | if (io_do_buffer_select(req)) |
| 310 | return 0; |
| 311 | |
| 312 | return io_import_rw_buffer(ddir, req, req->async_data, 0); |
| 313 | } |
| 314 | |
| 315 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 316 | int ddir) |
| 317 | { |
| 318 | int ret; |
| 319 | |
| 320 | ret = __io_prep_rw(req, sqe, ddir); |
| 321 | if (unlikely(ret)) |
| 322 | return ret; |
| 323 | |
| 324 | return io_rw_do_import(req, ddir); |
| 325 | } |
| 326 | |
| 327 | int io_prep_read(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 328 | { |
| 329 | return io_prep_rw(req, sqe, ITER_DEST); |
| 330 | } |
| 331 | |
| 332 | int io_prep_write(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 333 | { |
| 334 | return io_prep_rw(req, sqe, ITER_SOURCE); |
| 335 | } |
| 336 | |
| 337 | static int io_prep_rwv(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 338 | int ddir) |
| 339 | { |
| 340 | int ret; |
| 341 | |
| 342 | ret = io_prep_rw(req, sqe, ddir); |
| 343 | if (unlikely(ret)) |
| 344 | return ret; |
| 345 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 346 | return 0; |
| 347 | |
| 348 | /* |
| 349 | * Have to do this validation here, as this is in io_read() rw->len |
| 350 | * might have chanaged due to buffer selection |
| 351 | */ |
| 352 | return io_iov_buffer_select_prep(req); |
| 353 | } |
| 354 | |
| 355 | int io_prep_readv(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 356 | { |
| 357 | return io_prep_rwv(req, sqe, ITER_DEST); |
| 358 | } |
| 359 | |
| 360 | int io_prep_writev(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 361 | { |
| 362 | return io_prep_rwv(req, sqe, ITER_SOURCE); |
| 363 | } |
| 364 | |
| 365 | static int io_init_rw_fixed(struct io_kiocb *req, unsigned int issue_flags, |
| 366 | int ddir) |
| 367 | { |
| 368 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 369 | struct io_async_rw *io = req->async_data; |
| 370 | int ret; |
| 371 | |
| 372 | if (io->bytes_done) |
| 373 | return 0; |
| 374 | |
| 375 | ret = io_import_reg_buf(req, &io->iter, rw->addr, rw->len, ddir, |
| 376 | issue_flags); |
| 377 | iov_iter_save_state(&io->iter, &io->iter_state); |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | int io_prep_read_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 382 | { |
| 383 | return __io_prep_rw(req, sqe, ITER_DEST); |
| 384 | } |
| 385 | |
| 386 | int io_prep_write_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 387 | { |
| 388 | return __io_prep_rw(req, sqe, ITER_SOURCE); |
| 389 | } |
| 390 | |
| 391 | static int io_rw_import_reg_vec(struct io_kiocb *req, |
| 392 | struct io_async_rw *io, |
| 393 | int ddir, unsigned int issue_flags) |
| 394 | { |
| 395 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 396 | unsigned uvec_segs = rw->len; |
| 397 | int ret; |
| 398 | |
| 399 | ret = io_import_reg_vec(ddir, &io->iter, req, &io->vec, |
| 400 | uvec_segs, issue_flags); |
| 401 | if (unlikely(ret)) |
| 402 | return ret; |
| 403 | iov_iter_save_state(&io->iter, &io->iter_state); |
| 404 | req->flags &= ~REQ_F_IMPORT_BUFFER; |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int io_rw_prep_reg_vec(struct io_kiocb *req) |
| 409 | { |
| 410 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 411 | struct io_async_rw *io = req->async_data; |
| 412 | const struct iovec __user *uvec; |
| 413 | |
| 414 | uvec = u64_to_user_ptr(rw->addr); |
| 415 | return io_prep_reg_iovec(req, &io->vec, uvec, rw->len); |
| 416 | } |
| 417 | |
| 418 | int io_prep_readv_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 419 | { |
| 420 | int ret; |
| 421 | |
| 422 | ret = __io_prep_rw(req, sqe, ITER_DEST); |
| 423 | if (unlikely(ret)) |
| 424 | return ret; |
| 425 | return io_rw_prep_reg_vec(req); |
| 426 | } |
| 427 | |
| 428 | int io_prep_writev_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 429 | { |
| 430 | int ret; |
| 431 | |
| 432 | ret = __io_prep_rw(req, sqe, ITER_SOURCE); |
| 433 | if (unlikely(ret)) |
| 434 | return ret; |
| 435 | return io_rw_prep_reg_vec(req); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Multishot read is prepared just like a normal read/write request, only |
| 440 | * difference is that we set the MULTISHOT flag. |
| 441 | */ |
| 442 | int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 443 | { |
| 444 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 445 | int ret; |
| 446 | |
| 447 | /* must be used with provided buffers */ |
| 448 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | ret = __io_prep_rw(req, sqe, ITER_DEST); |
| 452 | if (unlikely(ret)) |
| 453 | return ret; |
| 454 | |
| 455 | if (rw->addr || rw->len) |
| 456 | return -EINVAL; |
| 457 | |
| 458 | req->flags |= REQ_F_APOLL_MULTISHOT; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | void io_readv_writev_cleanup(struct io_kiocb *req) |
| 463 | { |
| 464 | lockdep_assert_held(&req->ctx->uring_lock); |
| 465 | io_rw_recycle(req, 0); |
| 466 | } |
| 467 | |
| 468 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
| 469 | { |
| 470 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 471 | |
| 472 | if (rw->kiocb.ki_pos != -1) |
| 473 | return &rw->kiocb.ki_pos; |
| 474 | |
| 475 | if (!(req->file->f_mode & FMODE_STREAM)) { |
| 476 | req->flags |= REQ_F_CUR_POS; |
| 477 | rw->kiocb.ki_pos = req->file->f_pos; |
| 478 | return &rw->kiocb.ki_pos; |
| 479 | } |
| 480 | |
| 481 | rw->kiocb.ki_pos = 0; |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 486 | { |
| 487 | #ifdef CONFIG_BLOCK |
| 488 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 489 | umode_t mode = file_inode(req->file)->i_mode; |
| 490 | struct io_async_rw *io = req->async_data; |
| 491 | struct io_ring_ctx *ctx = req->ctx; |
| 492 | |
| 493 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 494 | return false; |
| 495 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 496 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
| 497 | return false; |
| 498 | /* |
| 499 | * If ref is dying, we might be running poll reap from the exit work. |
| 500 | * Don't attempt to reissue from that path, just let it fail with |
| 501 | * -EAGAIN. |
| 502 | */ |
| 503 | if (percpu_ref_is_dying(&ctx->refs)) |
| 504 | return false; |
| 505 | |
| 506 | io_meta_restore(io, &rw->kiocb); |
| 507 | iov_iter_restore(&io->iter, &io->iter_state); |
| 508 | return true; |
| 509 | #else |
| 510 | return false; |
| 511 | #endif |
| 512 | } |
| 513 | |
| 514 | static void io_req_end_write(struct io_kiocb *req) |
| 515 | { |
| 516 | if (req->flags & REQ_F_ISREG) { |
| 517 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 518 | |
| 519 | kiocb_end_write(&rw->kiocb); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Trigger the notifications after having done some IO, and finish the write |
| 525 | * accounting, if any. |
| 526 | */ |
| 527 | static void io_req_io_end(struct io_kiocb *req) |
| 528 | { |
| 529 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 530 | |
| 531 | if (rw->kiocb.ki_flags & IOCB_WRITE) { |
| 532 | io_req_end_write(req); |
| 533 | fsnotify_modify(req->file); |
| 534 | } else { |
| 535 | fsnotify_access(req->file); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static void __io_complete_rw_common(struct io_kiocb *req, long res) |
| 540 | { |
| 541 | if (res == req->cqe.res) |
| 542 | return; |
| 543 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 544 | req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE; |
| 545 | } else { |
| 546 | req_set_fail(req); |
| 547 | req->cqe.res = res; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | static inline int io_fixup_rw_res(struct io_kiocb *req, long res) |
| 552 | { |
| 553 | struct io_async_rw *io = req->async_data; |
| 554 | |
| 555 | /* add previously done IO, if any */ |
| 556 | if (req_has_async_data(req) && io->bytes_done > 0) { |
| 557 | if (res < 0) |
| 558 | res = io->bytes_done; |
| 559 | else |
| 560 | res += io->bytes_done; |
| 561 | } |
| 562 | return res; |
| 563 | } |
| 564 | |
| 565 | void io_req_rw_complete(struct io_kiocb *req, io_tw_token_t tw) |
| 566 | { |
| 567 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 568 | struct kiocb *kiocb = &rw->kiocb; |
| 569 | |
| 570 | if ((kiocb->ki_flags & IOCB_DIO_CALLER_COMP) && kiocb->dio_complete) { |
| 571 | long res = kiocb->dio_complete(rw->kiocb.private); |
| 572 | |
| 573 | io_req_set_res(req, io_fixup_rw_res(req, res), 0); |
| 574 | } |
| 575 | |
| 576 | io_req_io_end(req); |
| 577 | |
| 578 | if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)) |
| 579 | req->cqe.flags |= io_put_kbuf(req, req->cqe.res, 0); |
| 580 | |
| 581 | io_req_rw_cleanup(req, 0); |
| 582 | io_req_task_complete(req, tw); |
| 583 | } |
| 584 | |
| 585 | static void io_complete_rw(struct kiocb *kiocb, long res) |
| 586 | { |
| 587 | struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb); |
| 588 | struct io_kiocb *req = cmd_to_io_kiocb(rw); |
| 589 | |
| 590 | if (!kiocb->dio_complete || !(kiocb->ki_flags & IOCB_DIO_CALLER_COMP)) { |
| 591 | __io_complete_rw_common(req, res); |
| 592 | io_req_set_res(req, io_fixup_rw_res(req, res), 0); |
| 593 | } |
| 594 | req->io_task_work.func = io_req_rw_complete; |
| 595 | __io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE); |
| 596 | } |
| 597 | |
| 598 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
| 599 | { |
| 600 | struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb); |
| 601 | struct io_kiocb *req = cmd_to_io_kiocb(rw); |
| 602 | |
| 603 | if (kiocb->ki_flags & IOCB_WRITE) |
| 604 | io_req_end_write(req); |
| 605 | if (unlikely(res != req->cqe.res)) { |
| 606 | if (res == -EAGAIN && io_rw_should_reissue(req)) |
| 607 | req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE; |
| 608 | else |
| 609 | req->cqe.res = res; |
| 610 | } |
| 611 | |
| 612 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
| 613 | smp_store_release(&req->iopoll_completed, 1); |
| 614 | } |
| 615 | |
| 616 | static inline void io_rw_done(struct io_kiocb *req, ssize_t ret) |
| 617 | { |
| 618 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 619 | |
| 620 | /* IO was queued async, completion will happen later */ |
| 621 | if (ret == -EIOCBQUEUED) |
| 622 | return; |
| 623 | |
| 624 | /* transform internal restart error codes */ |
| 625 | if (unlikely(ret < 0)) { |
| 626 | switch (ret) { |
| 627 | case -ERESTARTSYS: |
| 628 | case -ERESTARTNOINTR: |
| 629 | case -ERESTARTNOHAND: |
| 630 | case -ERESTART_RESTARTBLOCK: |
| 631 | /* |
| 632 | * We can't just restart the syscall, since previously |
| 633 | * submitted sqes may already be in progress. Just fail |
| 634 | * this IO with EINTR. |
| 635 | */ |
| 636 | ret = -EINTR; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 642 | io_complete_rw_iopoll(&rw->kiocb, ret); |
| 643 | else |
| 644 | io_complete_rw(&rw->kiocb, ret); |
| 645 | } |
| 646 | |
| 647 | static int kiocb_done(struct io_kiocb *req, ssize_t ret, |
| 648 | unsigned int issue_flags) |
| 649 | { |
| 650 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 651 | unsigned final_ret = io_fixup_rw_res(req, ret); |
| 652 | |
| 653 | if (ret >= 0 && req->flags & REQ_F_CUR_POS) |
| 654 | req->file->f_pos = rw->kiocb.ki_pos; |
| 655 | if (ret >= 0 && !(req->ctx->flags & IORING_SETUP_IOPOLL)) { |
| 656 | __io_complete_rw_common(req, ret); |
| 657 | /* |
| 658 | * Safe to call io_end from here as we're inline |
| 659 | * from the submission path. |
| 660 | */ |
| 661 | io_req_io_end(req); |
| 662 | io_req_set_res(req, final_ret, io_put_kbuf(req, ret, issue_flags)); |
| 663 | io_req_rw_cleanup(req, issue_flags); |
| 664 | return IOU_COMPLETE; |
| 665 | } else { |
| 666 | io_rw_done(req, ret); |
| 667 | } |
| 668 | |
| 669 | return IOU_ISSUE_SKIP_COMPLETE; |
| 670 | } |
| 671 | |
| 672 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 673 | { |
| 674 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 679 | * by looping over ->read() or ->write() manually. |
| 680 | */ |
| 681 | static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter) |
| 682 | { |
| 683 | struct io_kiocb *req = cmd_to_io_kiocb(rw); |
| 684 | struct kiocb *kiocb = &rw->kiocb; |
| 685 | struct file *file = kiocb->ki_filp; |
| 686 | ssize_t ret = 0; |
| 687 | loff_t *ppos; |
| 688 | |
| 689 | /* |
| 690 | * Don't support polled IO through this interface, and we can't |
| 691 | * support non-blocking either. For the latter, this just causes |
| 692 | * the kiocb to be handled from an async context. |
| 693 | */ |
| 694 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 695 | return -EOPNOTSUPP; |
| 696 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
| 697 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) |
| 698 | return -EAGAIN; |
| 699 | if ((req->flags & REQ_F_BUF_NODE) && req->buf_node->buf->is_kbuf) |
| 700 | return -EFAULT; |
| 701 | |
| 702 | ppos = io_kiocb_ppos(kiocb); |
| 703 | |
| 704 | while (iov_iter_count(iter)) { |
| 705 | void __user *addr; |
| 706 | size_t len; |
| 707 | ssize_t nr; |
| 708 | |
| 709 | if (iter_is_ubuf(iter)) { |
| 710 | addr = iter->ubuf + iter->iov_offset; |
| 711 | len = iov_iter_count(iter); |
| 712 | } else if (!iov_iter_is_bvec(iter)) { |
| 713 | addr = iter_iov_addr(iter); |
| 714 | len = iter_iov_len(iter); |
| 715 | } else { |
| 716 | addr = u64_to_user_ptr(rw->addr); |
| 717 | len = rw->len; |
| 718 | } |
| 719 | |
| 720 | if (ddir == READ) |
| 721 | nr = file->f_op->read(file, addr, len, ppos); |
| 722 | else |
| 723 | nr = file->f_op->write(file, addr, len, ppos); |
| 724 | |
| 725 | if (nr < 0) { |
| 726 | if (!ret) |
| 727 | ret = nr; |
| 728 | break; |
| 729 | } |
| 730 | ret += nr; |
| 731 | if (!iov_iter_is_bvec(iter)) { |
| 732 | iov_iter_advance(iter, nr); |
| 733 | } else { |
| 734 | rw->addr += nr; |
| 735 | rw->len -= nr; |
| 736 | if (!rw->len) |
| 737 | break; |
| 738 | } |
| 739 | if (nr != len) |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
| 748 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 749 | * This gets called when the page is unlocked, and we generally expect that to |
| 750 | * happen when the page IO is completed and the page is now uptodate. This will |
| 751 | * queue a task_work based retry of the operation, attempting to copy the data |
| 752 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 753 | * do a thread based blocking retry of the operation. That's the unexpected |
| 754 | * slow path. |
| 755 | */ |
| 756 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 757 | int sync, void *arg) |
| 758 | { |
| 759 | struct wait_page_queue *wpq; |
| 760 | struct io_kiocb *req = wait->private; |
| 761 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 762 | struct wait_page_key *key = arg; |
| 763 | |
| 764 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 765 | |
| 766 | if (!wake_page_match(wpq, key)) |
| 767 | return 0; |
| 768 | |
| 769 | rw->kiocb.ki_flags &= ~IOCB_WAITQ; |
| 770 | list_del_init(&wait->entry); |
| 771 | io_req_task_queue(req); |
| 772 | return 1; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * This controls whether a given IO request should be armed for async page |
| 777 | * based retry. If we return false here, the request is handed to the async |
| 778 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 779 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 780 | * will either succeed because the page is now uptodate and unlocked, or it |
| 781 | * will register a callback when the page is unlocked at IO completion. Through |
| 782 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 783 | * That retry will attempt the buffered read again. The retry will generally |
| 784 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 785 | * async worker threads for a blocking retry. |
| 786 | */ |
| 787 | static bool io_rw_should_retry(struct io_kiocb *req) |
| 788 | { |
| 789 | struct io_async_rw *io = req->async_data; |
| 790 | struct wait_page_queue *wait = &io->wpq; |
| 791 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 792 | struct kiocb *kiocb = &rw->kiocb; |
| 793 | |
| 794 | /* |
| 795 | * Never retry for NOWAIT or a request with metadata, we just complete |
| 796 | * with -EAGAIN. |
| 797 | */ |
| 798 | if (req->flags & (REQ_F_NOWAIT | REQ_F_HAS_METADATA)) |
| 799 | return false; |
| 800 | |
| 801 | /* Only for buffered IO */ |
| 802 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
| 803 | return false; |
| 804 | |
| 805 | /* |
| 806 | * just use poll if we can, and don't attempt if the fs doesn't |
| 807 | * support callback based unlocks |
| 808 | */ |
| 809 | if (io_file_can_poll(req) || |
| 810 | !(req->file->f_op->fop_flags & FOP_BUFFER_RASYNC)) |
| 811 | return false; |
| 812 | |
| 813 | wait->wait.func = io_async_buf_func; |
| 814 | wait->wait.private = req; |
| 815 | wait->wait.flags = 0; |
| 816 | INIT_LIST_HEAD(&wait->wait.entry); |
| 817 | kiocb->ki_flags |= IOCB_WAITQ; |
| 818 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 819 | kiocb->ki_waitq = wait; |
| 820 | return true; |
| 821 | } |
| 822 | |
| 823 | static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter) |
| 824 | { |
| 825 | struct file *file = rw->kiocb.ki_filp; |
| 826 | |
| 827 | if (likely(file->f_op->read_iter)) |
| 828 | return file->f_op->read_iter(&rw->kiocb, iter); |
| 829 | else if (file->f_op->read) |
| 830 | return loop_rw_iter(READ, rw, iter); |
| 831 | else |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | |
| 835 | static bool need_complete_io(struct io_kiocb *req) |
| 836 | { |
| 837 | return req->flags & REQ_F_ISREG || |
| 838 | S_ISBLK(file_inode(req->file)->i_mode); |
| 839 | } |
| 840 | |
| 841 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type) |
| 842 | { |
| 843 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 844 | struct kiocb *kiocb = &rw->kiocb; |
| 845 | struct io_ring_ctx *ctx = req->ctx; |
| 846 | struct file *file = req->file; |
| 847 | int ret; |
| 848 | |
| 849 | if (unlikely(!(file->f_mode & mode))) |
| 850 | return -EBADF; |
| 851 | |
| 852 | if (!(req->flags & REQ_F_FIXED_FILE)) |
| 853 | req->flags |= io_file_get_flags(file); |
| 854 | |
| 855 | kiocb->ki_flags = file->f_iocb_flags; |
| 856 | ret = kiocb_set_rw_flags(kiocb, rw->flags, rw_type); |
| 857 | if (unlikely(ret)) |
| 858 | return ret; |
| 859 | kiocb->ki_flags |= IOCB_ALLOC_CACHE; |
| 860 | |
| 861 | /* |
| 862 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 863 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 864 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 865 | */ |
| 866 | if (kiocb->ki_flags & IOCB_NOWAIT || |
| 867 | ((file->f_flags & O_NONBLOCK && !(req->flags & REQ_F_SUPPORT_NOWAIT)))) |
| 868 | req->flags |= REQ_F_NOWAIT; |
| 869 | |
| 870 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 871 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
| 872 | return -EOPNOTSUPP; |
| 873 | kiocb->private = NULL; |
| 874 | kiocb->ki_flags |= IOCB_HIPRI; |
| 875 | req->iopoll_completed = 0; |
| 876 | if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { |
| 877 | /* make sure every req only blocks once*/ |
| 878 | req->flags &= ~REQ_F_IOPOLL_STATE; |
| 879 | req->iopoll_start = ktime_get_ns(); |
| 880 | } |
| 881 | } else { |
| 882 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | if (req->flags & REQ_F_HAS_METADATA) { |
| 887 | struct io_async_rw *io = req->async_data; |
| 888 | |
| 889 | /* |
| 890 | * We have a union of meta fields with wpq used for buffered-io |
| 891 | * in io_async_rw, so fail it here. |
| 892 | */ |
| 893 | if (!(req->file->f_flags & O_DIRECT)) |
| 894 | return -EOPNOTSUPP; |
| 895 | kiocb->ki_flags |= IOCB_HAS_METADATA; |
| 896 | kiocb->private = &io->meta; |
| 897 | } |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static int __io_read(struct io_kiocb *req, unsigned int issue_flags) |
| 903 | { |
| 904 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
| 905 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 906 | struct io_async_rw *io = req->async_data; |
| 907 | struct kiocb *kiocb = &rw->kiocb; |
| 908 | ssize_t ret; |
| 909 | loff_t *ppos; |
| 910 | |
| 911 | if (req->flags & REQ_F_IMPORT_BUFFER) { |
| 912 | ret = io_rw_import_reg_vec(req, io, ITER_DEST, issue_flags); |
| 913 | if (unlikely(ret)) |
| 914 | return ret; |
| 915 | } else if (io_do_buffer_select(req)) { |
| 916 | ret = io_import_rw_buffer(ITER_DEST, req, io, issue_flags); |
| 917 | if (unlikely(ret < 0)) |
| 918 | return ret; |
| 919 | } |
| 920 | ret = io_rw_init_file(req, FMODE_READ, READ); |
| 921 | if (unlikely(ret)) |
| 922 | return ret; |
| 923 | req->cqe.res = iov_iter_count(&io->iter); |
| 924 | |
| 925 | if (force_nonblock) { |
| 926 | /* If the file doesn't support async, just async punt */ |
| 927 | if (unlikely(!io_file_supports_nowait(req, EPOLLIN))) |
| 928 | return -EAGAIN; |
| 929 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 930 | } else { |
| 931 | /* Ensure we clear previously set non-block flag */ |
| 932 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 933 | } |
| 934 | |
| 935 | ppos = io_kiocb_update_pos(req); |
| 936 | |
| 937 | ret = rw_verify_area(READ, req->file, ppos, req->cqe.res); |
| 938 | if (unlikely(ret)) |
| 939 | return ret; |
| 940 | |
| 941 | ret = io_iter_do_read(rw, &io->iter); |
| 942 | |
| 943 | /* |
| 944 | * Some file systems like to return -EOPNOTSUPP for an IOCB_NOWAIT |
| 945 | * issue, even though they should be returning -EAGAIN. To be safe, |
| 946 | * retry from blocking context for either. |
| 947 | */ |
| 948 | if (ret == -EOPNOTSUPP && force_nonblock) |
| 949 | ret = -EAGAIN; |
| 950 | |
| 951 | if (ret == -EAGAIN) { |
| 952 | /* If we can poll, just do that. */ |
| 953 | if (io_file_can_poll(req)) |
| 954 | return -EAGAIN; |
| 955 | /* IOPOLL retry should happen for io-wq threads */ |
| 956 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 957 | goto done; |
| 958 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 959 | if (req->flags & REQ_F_NOWAIT) |
| 960 | goto done; |
| 961 | ret = 0; |
| 962 | } else if (ret == -EIOCBQUEUED) { |
| 963 | return IOU_ISSUE_SKIP_COMPLETE; |
| 964 | } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock || |
| 965 | (req->flags & REQ_F_NOWAIT) || !need_complete_io(req) || |
| 966 | (issue_flags & IO_URING_F_MULTISHOT)) { |
| 967 | /* read all, failed, already did sync or don't want to retry */ |
| 968 | goto done; |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * Don't depend on the iter state matching what was consumed, or being |
| 973 | * untouched in case of error. Restore it and we'll advance it |
| 974 | * manually if we need to. |
| 975 | */ |
| 976 | iov_iter_restore(&io->iter, &io->iter_state); |
| 977 | io_meta_restore(io, kiocb); |
| 978 | |
| 979 | do { |
| 980 | /* |
| 981 | * We end up here because of a partial read, either from |
| 982 | * above or inside this loop. Advance the iter by the bytes |
| 983 | * that were consumed. |
| 984 | */ |
| 985 | iov_iter_advance(&io->iter, ret); |
| 986 | if (!iov_iter_count(&io->iter)) |
| 987 | break; |
| 988 | io->bytes_done += ret; |
| 989 | iov_iter_save_state(&io->iter, &io->iter_state); |
| 990 | |
| 991 | /* if we can retry, do so with the callbacks armed */ |
| 992 | if (!io_rw_should_retry(req)) { |
| 993 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 994 | return -EAGAIN; |
| 995 | } |
| 996 | |
| 997 | req->cqe.res = iov_iter_count(&io->iter); |
| 998 | /* |
| 999 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 1000 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 1001 | * desired page gets unlocked. We can also get a partial read |
| 1002 | * here, and if we do, then just retry at the new offset. |
| 1003 | */ |
| 1004 | ret = io_iter_do_read(rw, &io->iter); |
| 1005 | if (ret == -EIOCBQUEUED) |
| 1006 | return IOU_ISSUE_SKIP_COMPLETE; |
| 1007 | /* we got some bytes, but not all. retry. */ |
| 1008 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 1009 | iov_iter_restore(&io->iter, &io->iter_state); |
| 1010 | } while (ret > 0); |
| 1011 | done: |
| 1012 | /* it's faster to check here then delegate to kfree */ |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | int io_read(struct io_kiocb *req, unsigned int issue_flags) |
| 1017 | { |
| 1018 | int ret; |
| 1019 | |
| 1020 | ret = __io_read(req, issue_flags); |
| 1021 | if (ret >= 0) |
| 1022 | return kiocb_done(req, ret, issue_flags); |
| 1023 | |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags) |
| 1028 | { |
| 1029 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 1030 | unsigned int cflags = 0; |
| 1031 | int ret; |
| 1032 | |
| 1033 | /* |
| 1034 | * Multishot MUST be used on a pollable file |
| 1035 | */ |
| 1036 | if (!io_file_can_poll(req)) |
| 1037 | return -EBADFD; |
| 1038 | |
| 1039 | /* make it sync, multishot doesn't support async execution */ |
| 1040 | rw->kiocb.ki_complete = NULL; |
| 1041 | ret = __io_read(req, issue_flags); |
| 1042 | |
| 1043 | /* |
| 1044 | * If we get -EAGAIN, recycle our buffer and just let normal poll |
| 1045 | * handling arm it. |
| 1046 | */ |
| 1047 | if (ret == -EAGAIN) { |
| 1048 | /* |
| 1049 | * Reset rw->len to 0 again to avoid clamping future mshot |
| 1050 | * reads, in case the buffer size varies. |
| 1051 | */ |
| 1052 | if (io_kbuf_recycle(req, issue_flags)) |
| 1053 | rw->len = 0; |
| 1054 | return IOU_RETRY; |
| 1055 | } else if (ret <= 0) { |
| 1056 | io_kbuf_recycle(req, issue_flags); |
| 1057 | if (ret < 0) |
| 1058 | req_set_fail(req); |
| 1059 | } else if (!(req->flags & REQ_F_APOLL_MULTISHOT)) { |
| 1060 | cflags = io_put_kbuf(req, ret, issue_flags); |
| 1061 | } else { |
| 1062 | /* |
| 1063 | * Any successful return value will keep the multishot read |
| 1064 | * armed, if it's still set. Put our buffer and post a CQE. If |
| 1065 | * we fail to post a CQE, or multishot is no longer set, then |
| 1066 | * jump to the termination path. This request is then done. |
| 1067 | */ |
| 1068 | cflags = io_put_kbuf(req, ret, issue_flags); |
| 1069 | rw->len = 0; /* similarly to above, reset len to 0 */ |
| 1070 | |
| 1071 | if (io_req_post_cqe(req, ret, cflags | IORING_CQE_F_MORE)) { |
| 1072 | if (issue_flags & IO_URING_F_MULTISHOT) |
| 1073 | /* |
| 1074 | * Force retry, as we might have more data to |
| 1075 | * be read and otherwise it won't get retried |
| 1076 | * until (if ever) another poll is triggered. |
| 1077 | */ |
| 1078 | io_poll_multishot_retry(req); |
| 1079 | |
| 1080 | return IOU_RETRY; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Either an error, or we've hit overflow posting the CQE. For any |
| 1086 | * multishot request, hitting overflow will terminate it. |
| 1087 | */ |
| 1088 | io_req_set_res(req, ret, cflags); |
| 1089 | io_req_rw_cleanup(req, issue_flags); |
| 1090 | return IOU_COMPLETE; |
| 1091 | } |
| 1092 | |
| 1093 | static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb) |
| 1094 | { |
| 1095 | struct inode *inode; |
| 1096 | bool ret; |
| 1097 | |
| 1098 | if (!(req->flags & REQ_F_ISREG)) |
| 1099 | return true; |
| 1100 | if (!(kiocb->ki_flags & IOCB_NOWAIT)) { |
| 1101 | kiocb_start_write(kiocb); |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | inode = file_inode(kiocb->ki_filp); |
| 1106 | ret = sb_start_write_trylock(inode->i_sb); |
| 1107 | if (ret) |
| 1108 | __sb_writers_release(inode->i_sb, SB_FREEZE_WRITE); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
| 1112 | int io_write(struct io_kiocb *req, unsigned int issue_flags) |
| 1113 | { |
| 1114 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
| 1115 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 1116 | struct io_async_rw *io = req->async_data; |
| 1117 | struct kiocb *kiocb = &rw->kiocb; |
| 1118 | ssize_t ret, ret2; |
| 1119 | loff_t *ppos; |
| 1120 | |
| 1121 | if (req->flags & REQ_F_IMPORT_BUFFER) { |
| 1122 | ret = io_rw_import_reg_vec(req, io, ITER_SOURCE, issue_flags); |
| 1123 | if (unlikely(ret)) |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | ret = io_rw_init_file(req, FMODE_WRITE, WRITE); |
| 1128 | if (unlikely(ret)) |
| 1129 | return ret; |
| 1130 | req->cqe.res = iov_iter_count(&io->iter); |
| 1131 | |
| 1132 | if (force_nonblock) { |
| 1133 | /* If the file doesn't support async, just async punt */ |
| 1134 | if (unlikely(!io_file_supports_nowait(req, EPOLLOUT))) |
| 1135 | goto ret_eagain; |
| 1136 | |
| 1137 | /* Check if we can support NOWAIT. */ |
| 1138 | if (!(kiocb->ki_flags & IOCB_DIRECT) && |
| 1139 | !(req->file->f_op->fop_flags & FOP_BUFFER_WASYNC) && |
| 1140 | (req->flags & REQ_F_ISREG)) |
| 1141 | goto ret_eagain; |
| 1142 | |
| 1143 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 1144 | } else { |
| 1145 | /* Ensure we clear previously set non-block flag */ |
| 1146 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 1147 | } |
| 1148 | |
| 1149 | ppos = io_kiocb_update_pos(req); |
| 1150 | |
| 1151 | ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res); |
| 1152 | if (unlikely(ret)) |
| 1153 | return ret; |
| 1154 | |
| 1155 | if (unlikely(!io_kiocb_start_write(req, kiocb))) |
| 1156 | return -EAGAIN; |
| 1157 | kiocb->ki_flags |= IOCB_WRITE; |
| 1158 | |
| 1159 | if (likely(req->file->f_op->write_iter)) |
| 1160 | ret2 = req->file->f_op->write_iter(kiocb, &io->iter); |
| 1161 | else if (req->file->f_op->write) |
| 1162 | ret2 = loop_rw_iter(WRITE, rw, &io->iter); |
| 1163 | else |
| 1164 | ret2 = -EINVAL; |
| 1165 | |
| 1166 | /* |
| 1167 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 1168 | * retry them without IOCB_NOWAIT. |
| 1169 | */ |
| 1170 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 1171 | ret2 = -EAGAIN; |
| 1172 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 1173 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
| 1174 | goto done; |
| 1175 | if (!force_nonblock || ret2 != -EAGAIN) { |
| 1176 | /* IOPOLL retry should happen for io-wq threads */ |
| 1177 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1178 | goto ret_eagain; |
| 1179 | |
| 1180 | if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) { |
| 1181 | trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2, |
| 1182 | req->cqe.res, ret2); |
| 1183 | |
| 1184 | /* This is a partial write. The file pos has already been |
| 1185 | * updated, setup the async struct to complete the request |
| 1186 | * in the worker. Also update bytes_done to account for |
| 1187 | * the bytes already written. |
| 1188 | */ |
| 1189 | iov_iter_save_state(&io->iter, &io->iter_state); |
| 1190 | io->bytes_done += ret2; |
| 1191 | |
| 1192 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1193 | io_req_end_write(req); |
| 1194 | return -EAGAIN; |
| 1195 | } |
| 1196 | done: |
| 1197 | return kiocb_done(req, ret2, issue_flags); |
| 1198 | } else { |
| 1199 | ret_eagain: |
| 1200 | iov_iter_restore(&io->iter, &io->iter_state); |
| 1201 | io_meta_restore(io, kiocb); |
| 1202 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1203 | io_req_end_write(req); |
| 1204 | return -EAGAIN; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | int io_read_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 1209 | { |
| 1210 | int ret; |
| 1211 | |
| 1212 | ret = io_init_rw_fixed(req, issue_flags, ITER_DEST); |
| 1213 | if (unlikely(ret)) |
| 1214 | return ret; |
| 1215 | |
| 1216 | return io_read(req, issue_flags); |
| 1217 | } |
| 1218 | |
| 1219 | int io_write_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 1220 | { |
| 1221 | int ret; |
| 1222 | |
| 1223 | ret = io_init_rw_fixed(req, issue_flags, ITER_SOURCE); |
| 1224 | if (unlikely(ret)) |
| 1225 | return ret; |
| 1226 | |
| 1227 | return io_write(req, issue_flags); |
| 1228 | } |
| 1229 | |
| 1230 | void io_rw_fail(struct io_kiocb *req) |
| 1231 | { |
| 1232 | int res; |
| 1233 | |
| 1234 | res = io_fixup_rw_res(req, req->cqe.res); |
| 1235 | io_req_set_res(req, res, req->cqe.flags); |
| 1236 | } |
| 1237 | |
| 1238 | static int io_uring_classic_poll(struct io_kiocb *req, struct io_comp_batch *iob, |
| 1239 | unsigned int poll_flags) |
| 1240 | { |
| 1241 | struct file *file = req->file; |
| 1242 | |
| 1243 | if (req->opcode == IORING_OP_URING_CMD) { |
| 1244 | struct io_uring_cmd *ioucmd; |
| 1245 | |
| 1246 | ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
| 1247 | return file->f_op->uring_cmd_iopoll(ioucmd, iob, poll_flags); |
| 1248 | } else { |
| 1249 | struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); |
| 1250 | |
| 1251 | return file->f_op->iopoll(&rw->kiocb, iob, poll_flags); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static u64 io_hybrid_iopoll_delay(struct io_ring_ctx *ctx, struct io_kiocb *req) |
| 1256 | { |
| 1257 | struct hrtimer_sleeper timer; |
| 1258 | enum hrtimer_mode mode; |
| 1259 | ktime_t kt; |
| 1260 | u64 sleep_time; |
| 1261 | |
| 1262 | if (req->flags & REQ_F_IOPOLL_STATE) |
| 1263 | return 0; |
| 1264 | |
| 1265 | if (ctx->hybrid_poll_time == LLONG_MAX) |
| 1266 | return 0; |
| 1267 | |
| 1268 | /* Using half the running time to do schedule */ |
| 1269 | sleep_time = ctx->hybrid_poll_time / 2; |
| 1270 | |
| 1271 | kt = ktime_set(0, sleep_time); |
| 1272 | req->flags |= REQ_F_IOPOLL_STATE; |
| 1273 | |
| 1274 | mode = HRTIMER_MODE_REL; |
| 1275 | hrtimer_setup_sleeper_on_stack(&timer, CLOCK_MONOTONIC, mode); |
| 1276 | hrtimer_set_expires(&timer.timer, kt); |
| 1277 | set_current_state(TASK_INTERRUPTIBLE); |
| 1278 | hrtimer_sleeper_start_expires(&timer, mode); |
| 1279 | |
| 1280 | if (timer.task) |
| 1281 | io_schedule(); |
| 1282 | |
| 1283 | hrtimer_cancel(&timer.timer); |
| 1284 | __set_current_state(TASK_RUNNING); |
| 1285 | destroy_hrtimer_on_stack(&timer.timer); |
| 1286 | return sleep_time; |
| 1287 | } |
| 1288 | |
| 1289 | static int io_uring_hybrid_poll(struct io_kiocb *req, |
| 1290 | struct io_comp_batch *iob, unsigned int poll_flags) |
| 1291 | { |
| 1292 | struct io_ring_ctx *ctx = req->ctx; |
| 1293 | u64 runtime, sleep_time; |
| 1294 | int ret; |
| 1295 | |
| 1296 | sleep_time = io_hybrid_iopoll_delay(ctx, req); |
| 1297 | ret = io_uring_classic_poll(req, iob, poll_flags); |
| 1298 | runtime = ktime_get_ns() - req->iopoll_start - sleep_time; |
| 1299 | |
| 1300 | /* |
| 1301 | * Use minimum sleep time if we're polling devices with different |
| 1302 | * latencies. We could get more completions from the faster ones. |
| 1303 | */ |
| 1304 | if (ctx->hybrid_poll_time > runtime) |
| 1305 | ctx->hybrid_poll_time = runtime; |
| 1306 | |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
| 1310 | int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
| 1311 | { |
| 1312 | struct io_wq_work_node *pos, *start, *prev; |
| 1313 | unsigned int poll_flags = 0; |
| 1314 | DEFINE_IO_COMP_BATCH(iob); |
| 1315 | int nr_events = 0; |
| 1316 | |
| 1317 | /* |
| 1318 | * Only spin for completions if we don't have multiple devices hanging |
| 1319 | * off our complete list. |
| 1320 | */ |
| 1321 | if (ctx->poll_multi_queue || force_nonspin) |
| 1322 | poll_flags |= BLK_POLL_ONESHOT; |
| 1323 | |
| 1324 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
| 1325 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
| 1326 | int ret; |
| 1327 | |
| 1328 | /* |
| 1329 | * Move completed and retryable entries to our local lists. |
| 1330 | * If we find a request that requires polling, break out |
| 1331 | * and complete those lists first, if we have entries there. |
| 1332 | */ |
| 1333 | if (READ_ONCE(req->iopoll_completed)) |
| 1334 | break; |
| 1335 | |
| 1336 | if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) |
| 1337 | ret = io_uring_hybrid_poll(req, &iob, poll_flags); |
| 1338 | else |
| 1339 | ret = io_uring_classic_poll(req, &iob, poll_flags); |
| 1340 | |
| 1341 | if (unlikely(ret < 0)) |
| 1342 | return ret; |
| 1343 | else if (ret) |
| 1344 | poll_flags |= BLK_POLL_ONESHOT; |
| 1345 | |
| 1346 | /* iopoll may have completed current req */ |
| 1347 | if (!rq_list_empty(&iob.req_list) || |
| 1348 | READ_ONCE(req->iopoll_completed)) |
| 1349 | break; |
| 1350 | } |
| 1351 | |
| 1352 | if (!rq_list_empty(&iob.req_list)) |
| 1353 | iob.complete(&iob); |
| 1354 | else if (!pos) |
| 1355 | return 0; |
| 1356 | |
| 1357 | prev = start; |
| 1358 | wq_list_for_each_resume(pos, prev) { |
| 1359 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
| 1360 | |
| 1361 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
| 1362 | if (!smp_load_acquire(&req->iopoll_completed)) |
| 1363 | break; |
| 1364 | nr_events++; |
| 1365 | req->cqe.flags = io_put_kbuf(req, req->cqe.res, 0); |
| 1366 | if (req->opcode != IORING_OP_URING_CMD) |
| 1367 | io_req_rw_cleanup(req, 0); |
| 1368 | } |
| 1369 | if (unlikely(!nr_events)) |
| 1370 | return 0; |
| 1371 | |
| 1372 | pos = start ? start->next : ctx->iopoll_list.first; |
| 1373 | wq_list_cut(&ctx->iopoll_list, prev, start); |
| 1374 | |
| 1375 | if (WARN_ON_ONCE(!wq_list_empty(&ctx->submit_state.compl_reqs))) |
| 1376 | return 0; |
| 1377 | ctx->submit_state.compl_reqs.first = pos; |
| 1378 | __io_submit_flush_completions(ctx); |
| 1379 | return nr_events; |
| 1380 | } |
| 1381 | |
| 1382 | void io_rw_cache_free(const void *entry) |
| 1383 | { |
| 1384 | struct io_async_rw *rw = (struct io_async_rw *) entry; |
| 1385 | |
| 1386 | io_vec_free(&rw->vec); |
| 1387 | kfree(rw); |
| 1388 | } |