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