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