projects/linux·blob master · refs

1401 lines · 36.1 KiB

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